Ejemplo n.º 1
0
Archivo: main.py Proyecto: D611F21/DP
def main() -> Tuple[DataFrame, DataFrame, DataFrame]:
    args = get_arguments()

    data = load_data(args.from_date,
                     args.to_date,
                     data_path=args.data_path,
                     date_columns=['timestamp'])

    if data is None or len(data) == 0:
        print('No data found')
        return None

    iter_progress = tqdm if args.progress else lambda it: it

    orig_data, anon_data, props = anonymize_data(data, args.user_limit,
                                                 args.entry_limit,
                                                 args.probability,
                                                 iter_progress)

    if args.verbose:
        print_data_extract({'Original': orig_data, 'Anonymized': anon_data})

    save_df(args.out_dir, args.save_orig, orig_data, 'Original data')
    save_df(args.out_dir, args.save_anon, anon_data, 'Anonymized data')

    if args.verbose:
        print_props(props)

    return (orig_data, anon_data, props)
Ejemplo n.º 2
0
    hmm_thresh = 0.9 # call tract if posterior probability is higher than this value
    e_11 = .050702 # from simulation
    e_01 = 7.405e-4 # from simulation
    
    #read command line arguments
    parser = argparse.ArgumentParser(description="infer admixure tracts")
    parser.add_argument("-i", type=str, dest="i",help="absolute path of the input file")
    parser.add_argument("-o", type=str, dest="o",help="absolute path of the output path")
    parser.add_argument("-r", type=float, dest="r",help="recombination rate (per bp per generation)", default=r)
    parser.add_argument("-t", type=float, dest="t",help="admixture time (in generations)", default=t)
    parser.add_argument("-m", type=float, dest="m",help="admixture proportion", default=m)
    parser.add_argument("-it", type=float, dest="it",help="threshold at which to call a site informative", default=info_thresh)
    parser.add_argument("-ht", type=float, dest="ht",help="confidence level at which to call tracts", default=hmm_thresh)
    args=parser.parse_args()    
    r,t,m,info_thresh,hmm_thresh,infile_path,outfile_path = args.r,args.t,args.m,args.it,args.ht,args.i,args.o
    
    print "loading data"
    names,positions,af_yri,af_den,haps = fm.load_data(infile_path)
    
    print 'preprocessing data'
    obs_mat = fm.compute_obs(positions,af_yri,af_den,haps,info_thresh)
    
    print 'initializing HMM'
    hmm = init_hmm(r, t, m, positions,hmm_thresh,e_11,e_01)
    
    print 'posterior decoding'
    outfile = open(outfile_path,'wb')
    posterior_decoding_fb(hmm,obs_mat,positions,names,outfile)

    
    
Ejemplo n.º 3
0
def main_x():
    try:
        # startup message
        global FirstTimeInitiating
        if (FirstTimeInitiating):
            print("Program initiated on " +
                  TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))
            FirstTimeInitiating = False
        else:
            pass

        # get user input
        try:
            # Update storage file?
            if sys.argv[1].lower().find('yes') != -1:
                print("Clear command received, clearing storage file...")
                FM.clear_data_file(dailyShiftStore)
                print("Storage file cleared successfully")
            else:
                pass
            # specify location
            global LOCATION
            if sys.argv[1].lower().find('lamont') != -1:
                print("Specified location: LAMONT")
                LOCATION = LAMONT
            elif sys.argv[1].lower().find('cabot') != -1:
                print("Specified location: CABOT")
                LOCATION = CABOT
            else:
                print("Location not specified."
                      )  # pass if location not specified

            sys.argv = []  # clear input
        except IndexError:
            pass  # ignore this error
        '''
        # Alternative code for requesting user input

        global FirstTimeInitiating
        if FirstTimeInitiating:
            if askyesno('Verify', 'Update storage data?'):
                print("Clear command received, clearing storage file...")
                FM.clear_data_file(dailyShiftStore)
                print("Storage file cleared successfully")
                FirstTimeInitiating = False
            else:
                pass  # ignore
        else:
            pass  # ignore
        '''

        # variable to hold dersied shifts start times in a list
        shiftsOfTheDay = []
        # check the mode (calendar mode or scheduler mode)
        if calendarMode:

            # check if record of shifts already exists in file, check date timestamp
            timestamp_storedData = None  # variable to hold timestamp from stored file
            try:
                storedData = FM.load_data(dailyShiftStore)
                timestamp_storedData = storedData[
                    0]  # never use index with an empty tuple
            except EOFError:
                pass  # do not alter value of timestamp stored data
            if (timestamp_storedData == SC.today_raw_date()):

                # data already exists thus retrieve shifts of the day ie index 1 of tuple
                print("Valid up-to-date shift data found in storage")
                shiftsOfTheDay = storedData[1]
                print("Storage data retrieved!")

                # Show retrieved data on GUI
                print("Today's available shifts:")
                for each_tuple in shiftsOfTheDay:
                    print(SC.datetime_tuple_to_string_format(each_tuple))
                if shiftsOfTheDay == []:  # print none if no shifts available
                    print(None)

            else:  # get new data and write to file
                GUI.BAR_MODE = "loading"
                # initiation sequence
                print(
                    "Calendar mode active. \nRetrieving data from shiftboard website"
                )
                BH.launch_browser(address)  # go to shiftbaord website
                BH.login(email, password)  # login
                BH.goto_next_week(weekNo)  # go to weekNo weeks ahead
                # extract table data from shiftboard
                parsedShiftboardTable_for_calendar = BH.shiftboard_parser(
                    BH.tableRowXpath)
                # remove taken shifts
                parsedShiftboardTable_for_calendar = BH.remove_taken_shifts(
                    parsedShiftboardTable_for_calendar)
                print("Shift filtering complete")
                # extract day's shifts in a list of tuples
                extractedDayShifts = BH.extract_day_shift_time(
                    parsedShiftboardTable_for_calendar, weekNo)
                # convert shift time ranges to datetimes and get list of tuples of shifts
                tuple_of_date_and_shift_tuples = BH.shift_time_to_datetime_parser(
                    extractedDayShifts)
                list_of_tuples_shift_times_of_day = tuple_of_date_and_shift_tuples[
                    1]  # list of shifts for this day
                # use calendar_manager functions to retrieve day schedule with free periods
                print("Extracting schedule data from calendar")
                search_date = tuple_of_date_and_shift_tuples[
                    0]  # get date on tuple list of extractedDayShifts
                list_free_periods = CLDM.retrieve_schedule_of_the_day(
                    search_date)  # list of free periods for this day
                # use calendar_manager functions to compare shift time to free periods
                for each_shift in list_of_tuples_shift_times_of_day:
                    # check if free at shift times and add start times of shifts that work to shiftsOfTheDay
                    isFree = CLDM.check_if_free_comparator(
                        each_shift, list_free_periods)
                    if isFree:
                        # store start time and end times of each shift to shiftsOfTheDay list.
                        shiftsOfTheDay.append(each_shift)

                    else:
                        pass  # do nothing
                # remove duplicates from shiftsOfTheDay, also changes order of elements ie sets are unordered
                print("List of shifts compiled successfully")
                shiftsOfTheDay = list(set(shiftsOfTheDay))
                # store to file for future sessions, include datestamp stored in dailyShiftStore
                print("Writing list of shifts to storage file...")
                FM.write_data(dailyShiftStore,
                              (SC.today_raw_date(), shiftsOfTheDay))
                print("Stored successfully")

        else:

            # schedule mode
            # get list of dates of desired shifts two weeks from now from scheduler
            shiftsOfTheDay = SC.get_desired_shift_dates(desiredShifts, weekNo)

        # switch to shift listening mode and enter loop
        print("Listening for shifts from " + LOCATION + "...")
        GUI.BAR_MODE = "listening"  # switch display mode

        # Make the program loop forever
        while True:

            # get approaching shift two weeks from now, (30 seconds earlier)
            shiftToTake = SC.check_if_shift_approching(shiftsOfTheDay, weekNo,
                                                       30)

            # if exit button is pressed in GUI
            if GUI.exit_thread:
                break

            # check if its sleeping time
            elif SC.today_time(wakeHour, wakeMinute) > TZDT.now():
                # go to sleep
                print("Sleeping time zzz")
                break

            # check if desired shifts is empty
            elif shiftsOfTheDay == []:
                # exit loop if empty
                if calendarMode:
                    print(
                        "You have no free periods or there are no more shifts available on \n"
                        + shift_check_date.strftime("%b %d %Y"))
                else:
                    print("Your schedule is empty on " +
                          shift_check_date.strftime("%b %d %Y"))
                # exit
                break

            # check if shift is approaching
            elif shiftToTake is None:

                # keep waiting for shift to approach
                pass
                # SPC.spinner()

            # run the code below if approaching
            else:
                # GUI
                GUI.BAR_MODE = "loading"

                # wait for internet connectivity
                print("Checking for internet connectivity at " +
                      TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))
                while (not BH.connected()):
                    # check if program has been aborted
                    if GUI.exit_thread:
                        break
                    else:
                        pass  # keep waiting for internet connectivity

                # connection successful
                print("Internet connection established at " +
                      TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))

                # launch browser
                BH.launch_browser(address)

                # enter username and password and log in
                BH.login(email, password)

                # go to two weeks ahead
                BH.goto_next_week(weekNo)

                # parse shiftboard table
                parsedShiftboardTable = BH.shiftboard_parser(BH.tableRowXpath)

                # remove taken shifts
                parsedShiftboardTable = BH.remove_taken_shifts(
                    parsedShiftboardTable)

                # get shift confirm button
                confirmBtn = BH.grab_shift(shiftToTake, parsedShiftboardTable,
                                           LOCATION)

                # check if get shift confirm button is empty
                if confirmBtn is None:
                    # remove shift time from list and update record to file
                    print("Could NOT find shift on shiftboard")
                    shift_remover(shiftToTake, shiftsOfTheDay)
                    # then call main
                    main_x()
                else:
                    pass  # ignore if shift found

                # create function to abort shift grabbing if need be
                def abort_grabbing():
                    shift_remover(shiftToTake, shiftsOfTheDay)  # remove shift
                    main_x()  # call main to abort

                # abort message
                ABORT_MESSAGE = SC.datetime_tuple_to_string_format(
                    shiftToTake
                ) + '''\nThis shift will be taken in less than 30 seconds. Click below to cancel'''

                # confirm shift, wait until the time reaches
                GUI.BAR_MODE = "flashing"  # change bar GUI

                # ask user if shift grabbing should be aborted
                GUI.final_shift_notification(15000, ABORT_MESSAGE,
                                             abort_grabbing)

                while ((TZDT.now() + timedelta(weeks=weekNo)) <
                       (shiftToTake[0]) + timedelta(milliseconds=1)):
                    # check if program has been aborted
                    if GUI.exit_thread:
                        break
                    else:
                        pass  # ignore

                # confirm
                BH.confirm_shift(confirmBtn)
                GUI.BAR_MODE = "loading"

                # remove confirmed shift from list for this session and record to file
                shift_remover(shiftToTake, shiftsOfTheDay)

                # switch to shift listening mode mode once done
                print("Done.\nListening for shifts from " + LOCATION + "...")
                GUI.BAR_MODE = "listening"
                # BH.delay(5)
                # BH.exit_sequence()
            BH.delay(1)  # delay for 1 second, decrease CPU cycle rate

        # print loop exit message
        timestamp = TZDT.now()
        print("Loop exit at :" + timestamp.strftime("%b %d %Y %I:%M:%S %p"))

        # set arbitrary variable for wakeTime, value will be updated below
        wakeTime = timestamp
        # find wake up time by increasing timestamp by 1 day, fix this with if statements
        if SC.today_time(wakeHour, wakeMinute) < TZDT.now() < SC.today_time(
                23, 59):
            wakeTime = timestamp + timedelta(days=1)
        else:
            pass  # dont add a day ie wake up in the morning the same day

        # extract current date from wakeTime
        wakeTime_year, wakeTime_month, wakeTime_day = SC.date_to_tuple(
            wakeTime)
        # date at 8:45am on the next day
        wakeTime = datetime(wakeTime_year, wakeTime_month, wakeTime_day,
                            wakeHour, wakeMinute)
        # activate sleepmode and wait until 8:45am the next day to wake
        if not GUI.exit_thread:
            print("Sleep mode activated at :" +
                  TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))
            GUI.BAR_MODE = "sleeping"
        # uncomment when ready to commence
        while TZDT.now() < wakeTime:
            # if exit button pressed
            if GUI.exit_thread:
                BH.exit_sequence()
                GUI.exit_success = True  # indicate to GUI that exit was successful
                quit()  # exit all python scripts
                break
            else:
                GUI.gray_fade_in()
                GUI.gray_fade_out()
            BH.delay(3)  # decrease CPU cycle rate by sleeeping
        # if exit button NOT pressed
        if not GUI.exit_thread:
            # wake up sequence
            print("Sleep mode deactivated. Waking up at :" +
                  TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))
            GUI.BAR_MODE = "listening"
            main_x()  # call main to wake the program and listen for shifts
        else:
            pass
    except Exception as e:
        # print the exception raised
        print("PROGRAM ERROR DETECTED: ")
        print(str(e) + "\n")

        # wait for internet connectivity
        print("Checking for internet connectivity at " +
              TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))
        while (not BH.connected()):
            # check if program has been aborted
            if GUI.exit_thread:
                break
            else:
                pass  # keep waiting for internet connectivity

        # connection successful
        print("Internet connection established at " +
              TZDT.now().strftime("%b %d %Y %I:%M:%S %p"))

        # call main to re initiate the program
        main_x()
Ejemplo n.º 4
0
"""
###############################################################################
# import modules
from datetime import datetime  # , timedelta  # import datetime module
import requests  # moudle for making GET requests
from scheduler import date_to_tuple  # function for extracting date in tuple form
import file_manager as FM

# program variables
busyFreeAPI_url = 'https://api.freebusy.io/beta/week/'
debugMode = True
secret_file = 'secret.data'

# user variables
userEmail = FM.load_data(secret_file)[0]
timeZone = 'America/New_York'


###############################################################################
# program functions
###############################################################################


# function that removes unicode characters
# helper function for shiftboard_parser
# copied from browser_handler
def utf8_encoder(unicoded_string):
    return unicoded_string.encode('utf-8')

Ejemplo n.º 5
0
import timezone_datetime as TZDT

# program variables
debugMode = False
weekNo = 2
takenShifts = 'taken_shifts.txt'
wakeHour = 8  # am
wakeMinute = 50  # am
calendarMode = True
dailyShiftStore = 'daily_shift_store.txt'
secret_data = 'secret.data'
FirstTimeInitiating = True
shift_check_date = TZDT.now() + timedelta(weeks=weekNo)

# user variables
email = FM.load_data(secret_data)[0]  # get email from file
password = FM.load_data(secret_data)[1]  # get password from file
address = 'https://www.shiftboard.com/log-in/'  # shiftboard website address

# shift related variables
desiredShifts = SC.desiredShifts  # shifts to grab
LAMONT = 'LAMONT'
CABOT = 'CABOT Studios'
LOCATION = LAMONT  # system default for location

# program functions


# fuction to remove shift from shift list and update storage file
def shift_remover(_shift_to_remove, list_of_shifts):
    list_of_shifts.remove(_shift_to_remove)