def generate_random_plot_given_labels(session, labels, time_offsets): """ Generate random raw data plot including given labels Generate random raw data plot with given labels on it, multisensor plot used, call pyplot.show() to show plot in time. Plot will not be saved automatically Args: session: session number which is used to choose session data files labels: string list contains labels we want to be involved in plot time_offsets: list of label time offsets for each session """ sensors = [3] rawfiles = ["../raw_dataset/session"+ str(session)+"_sensor"+ str(sensor) +".raw.csv" for sensor in sensors] annotationfiles = ["../raw_dataset/session"+str(session)+"_sensor" + str(sensor) + ".annotation.csv" for sensor in sensors] sessions = [str(session) for sensor in sensors] time_offset = [time_offsets[session-1] for sensor in sensors] raw_data = w_utils.raw_csv_consolidator(rawfiles, sessions=sessions, sensors=sensors) annotation_data = s_annotation.annotation_csv_consolidator(annotationfiles,sessions=sessions, time_offsets=time_offset, sensors=sensors) annotation_single = s_annotation.annotation_csv_consolidator([annotationfiles[0],],sessions=[sessions[0],], time_offsets=[time_offset[0],]) raw_data = s_raw.preprocess_raw(raw_data, annotation_data, grace_period = timedelta(seconds=0), by='sensor') lbound, rbound = s_annotation.generate_bounds_by_labels(annotation_single, duration=timedelta(minutes=1, seconds=30), labels=labels) raw_data = s_raw.select_raw_by_ts(raw_data, lbound, rbound, by='sensor') annotation_data = s_annotation.select_annotation_by_ts(annotation_data, lbound, rbound, by='sensor') # s_viewer.get_multisensor_raw_plot(raw_data, labels=annotation_data, subplots=False) # multi steps post_value_names, posture_data = s_raw.transform_raw(raw_data, transform_type='post-distance') raw_datas = [raw_data, posture_data] labels = [annotation_data, annotation_data] titles = ['raw','post-distance'] s_viewer.get_multistep_view_plot(raw_datas, labels, titles, subplots=False) pyplot.show()
def generate_activity_views(raw_filenames, annotation_filenames, prediction_filenames, visualtest_filenames, sessions, time_offsets): """Generate activity views for each provided dataset Generate synthetic activity view plot in time series with predicted and true puffing, smoking, visual test results and labels. Save the plots to files in evaluation folder Args: raw_filenames: list of raw data filepath strings for each data session annotation_filenames: list of annotation data filepath strings for each data session prediction_filenames: list of model prediction data filepath strings for each data session visualtest_filenames: list of visual test result filepath strings for each data session sessions: list of session numbers for each data session time_offsets: list of label time offsets for each data session """ for rawfile, annotationfile, predictionfile, visualtestfile,session in zip(raw_filenames, annotation_filenames, prediction_filenames, visualtest_filenames,sessions): time_offset = time_offsets[session-1] raw_data = w_utils.raw_csv_consolidator([rawfile,], sessions=[session,]) annotation_data = s_annotation.annotation_csv_consolidator([annotationfile,],sessions=[session,], time_offsets=[time_offset,]) raw_data = s_raw.preprocess_raw(raw_data, annotation_data, grace_period = timedelta(seconds=0)) # single session, so we don't need group by prediction_data = pd.read_csv(predictionfile, names=['prob','puffs']) visualtest_data = pd.read_csv(visualtestfile, header=0) s_viewer.get_activity_view_plot(raw_data, annotation_data, prediction_data=prediction_data, visualtest_data=visualtest_data, title='session'+str(session)) filename = "../evaluation_result/activity_views/activity_view_session"+str(session)+'.png' # pyplot.show() pyplot.savefig(filename) pyplot.clf()
def main(raw_filenames, annotation_filenames, sessions, subjects, sensors, time_offsets): """ Main function for visual test on dataset Generate 20 random sequences of 5 min with and without labels on multisensor plot, and save them to files Args: raw_filenames: raw data filepath strings for each sensor of current session annotation_filenames: annotation filepath strings for each sensor of current session sessions: session numbers for each sensor subjects: subject strings for each sensor of current session sensors: sensor numbers of current session time_offsets: list of label time offset for each sensor of current session """ raw_data = w_utils.raw_csv_consolidator(raw_filenames, sessions, subjects, sensors) annotation_data = s_annotation.annotation_csv_consolidator( annotation_filenames, time_offsets, sessions, subjects, sensors) raw_data = s_raw.preprocess_raw(raw_data, annotation_data, by='sensor') # index of plot which needs to be regenerated regen = [11, 19] secs = [] for count in regen: try: lbound, rbound = w_utils.generate_random_bounds( raw_data, timedelta(minutes=5)) # count = 4 # lbound = w_utils.convert_fromstring("2012-05-03 12:43:16", annotation.annotation_tstr_format) # rbound = w_utils.convert_fromstring("2012-05-03 12:48:16", annotation.annotation_tstr_format) random_raw = s_raw.select_raw_by_ts(raw_data, lbound, rbound, by='sensor') random_annotation = s_annotation.select_annotation_by_ts( annotation_data, lbound, rbound, by='sensor') s_viewer.get_multisensor_raw_plot(random_raw, labels=random_annotation, subplots=False) true_filename = "../visual_test_data/session" + str( num_session) + "/true/true" + str(count) + '.png' pyplot.savefig(true_filename) s_viewer.get_multisensor_raw_plot(random_raw, subplots=False) test_filename = "../visual_test_data/session" + str( num_session) + "/test/test" + str(count) + '.png' pyplot.savefig(test_filename) # count += 1 secs.append(lbound) except IndexError: continue # print the list of time ranges that is randomly generated for s in secs: print s
def load_smoking_df(session=1, sensors=[ 'DW', ], kind='corrected'): """ Load wockets raw dataframe and smoking annotation dataframe given various options Args: session: default is session 1, session number to be loaded sensors: default is 'DW', sensor code array to specify sensors to be loaded type: default is 'corrected', what type of dataset to be loaded, currently support "raw", "clean" and "corrected" Return: raw_df, annotation_df """ if kind == 'raw': folder = s_info.raw_dataset_folder elif kind == 'clean': folder = s_info.clean_dataset_folder elif kind == 'corrected': folder = s_info.puff_corrected_folder else: raise RuntimeError raw_filenames = [ folder + '/session' + str(session) + '_' + sensor + '.raw.csv' for sensor in sensors ] annotation_filenames = [ folder + '/session' + str(session) + '.annotation.csv' for sensor in sensors ] print "======start loading wockets raw csv files: ========" print '\n'.join(raw_filenames) raw_df = w_utils.raw_csv_consolidator(raw_filenames, sessions=[ session, ] * len(sensors), sensors=sensors) print "======start loading smoking annotation csv files: ========" print '\n'.join(annotation_filenames) annotation_df = s_annotation.annotation_csv_consolidator( annotation_filenames, sessions=[ session, ] * len(sensors), sensors=sensors) print "=====================end loading files======================" return raw_df, annotation_df
def unit_tests(rawfile, annotfile, predictfile, visualfile): raw_data = w_utils.raw_csv_consolidator([ rawfile, ], sessions=[ 1, ], subjects=[ 5, ], sensors=[ 'wrist', ]) annotation_data = s_annotation.annotation_csv_consolidator([ annotfile, ], sessions=[ 1, ], subjects=[ 5, ], sensors=[ 'wrist', ]) raw_data = s_raw.preprocess_raw(raw_data, annotation_data, grace_period=timedelta(seconds=0)) # prediction_data = pd.read_csv(predictfile, names=['prob','puffs']) # visualtest_data = pd.read_csv(visualfile, header=0) # activity_viewer(raw_data, annotation_data, prediction_data=prediction_data, visualtest_data=visualtest_data) # print annotation_data # lbound, rbound = annot.generate_random_annotation_bounds(annotation_data, timedelta(minutes=1)) lbound, rbound = s_annotation.generate_bounds_by_labels( annotation_data, duration=timedelta(minutes=5), labels=['walking', 'smoking']) raw_data = s_raw.preprocess_raw(raw_data, annotation_data) raw_data = s_raw.select_raw_by_ts(raw_data, lbound, rbound)
def generate_puffing_example_plots(folder, example_file, time_offsets): """ Generate examples of puffing plots Generate examples of puffing plots with subplots of different preprocessing skill and save them as files. Call pyplot.show() afterwards to show plots in time Args: folder: path string to save the generated examples example_file: csv file path string which contains info of puffing examples time_offsets: list of label offset for each session """ examples = pd.read_csv(example_file, header=0) for idx, row in examples.iterrows(): session = row[0] if isinstance(row[1], int): sensors = [row[1],] else: sensors = row[1] use = row[4] case = row[5] lbound = w_utils.convert_fromstring(row[2], timeformat) rbound = w_utils.convert_fromstring(row[3], timeformat) rawfiles = ["../raw_dataset/session"+ str(session)+"_sensor"+ str(sensor) +".raw.csv" for sensor in sensors] annotationfiles = ["../raw_dataset/session"+str(session)+"_sensor" + str(sensor) + ".annotation.csv" for sensor in sensors] sessions = [str(session) for sensor in sensors] time_offset = [time_offsets[session-1] for sensor in sensors] raw_data = w_utils.raw_csv_consolidator(rawfiles, sessions=sessions, sensors=sensors) annotation_data = s_annotation.annotation_csv_consolidator(annotationfiles,sessions=sessions, time_offsets=time_offset, sensors=sensors) raw_data = s_raw.preprocess_raw(raw_data, annotation_data, grace_period = timedelta(seconds=0), by='sensor') raw_data = s_raw.select_raw_by_ts(raw_data, lbound, rbound, by='sensor') annotation_data = s_annotation.annotation_select_by_ts(annotation_data, lbound, rbound, by='sensor') orientation_values_name, orientation_data = s_raw.transform_raw(raw_data, transform_type='orientation') # get orientation measurement post_distance_values_name, post_distance_data = s_raw.transform_raw(raw_data, transform_type='post-distance') # get post distance measurement datas = [raw_data, orientation_data, post_distance_data] annotations = [annotation_data, annotation_data, annotation_data] fig_to_save = s_viewer.get_multistep_view_plot(datas, annotations, titles=['raw','orientation','post distance'], subplots=True, sharex=True, appear='gray') fig_to_save.set_size_inches(30,10) filename = str(idx) + "_session" + str(session) + "_sensor" + str(sensor) + "_" + use + "_" + case + ".png" fig_to_save.savefig(folder + filename) pyplot.clf()
def unit_test(): import viewer as s_viewer testfile_raw = "../../test.raw.csv" testfile_annotation = "../../test.annotation.csv" consol_raw = w_utils.raw_csv_consolidator([ testfile_raw, ], sessions=[ 5, ]) consol_annotation = s_annotation.annotation_csv_consolidator([ testfile_annotation, ], time_offsets=[ 0, ], sessions=[ 5, ]) raw_data = preprocess_raw(consol_raw, consol_annotation, by='session') new_value_names, new_data = transform_raw(raw_data) lbound, rbound = w_utils.generate_random_bounds( new_data, duration=timedelta(minutes=5)) random_raw = select_raw_by_ts(new_data, lbound, rbound) random_annotation = s_annotation.select_annotation_by_ts( consol_annotation, lbound, rbound) # s_viewer.get_simple_raw_plot(random_raw, labels=random_annotation, subplots=False) lowpass_raw = filter_raw(random_raw, new_value_names, filter_type='dcblock') # new_value_names, random_raw = transform_raw(random_raw, value_names=new_value_names, transform='fft') s_viewer.get_multistep_view_plot([random_raw, lowpass_raw], labels=random_annotation, subplots=True) # s_viewer.get_simple_raw_plot(random_raw, labels=random_annotation, subplots=False) # pyplot.show() # random_raw = scale_raw(random_raw,4,-4) # random_raw = random_raw.set_index([utils.raw_ts_name, 'session']) # random_raw.plot() pyplot.show()