Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def main():
    for i in s_info.session_arr:
        i = 2
        annotation_file = 'session' + str(i) + '.annotation.csv'
        annotation_list = s_annotation.annotation_csv_importer(
            s_info.clean_dataset_folder + annotation_file)

        raw_file = 'session' + str(i) + '_DAK.raw.csv'
        raw_ankle_data = w_utils.raw_csv_importer(s_info.clean_dataset_folder +
                                                  raw_file)

        selected_st, selected_et = get_walking_or_standing_annotation(
            annotation_list)
        n_subplots = len(selected_st) + 1
        #generate subplot grid
        ncols = np.ceil(np.sqrt(n_subplots))
        nrows = np.ceil(n_subplots / float(ncols))
        c = 1
        consolidate_figure = pyplot.figure(figsize=(20, 10))
        for st, et in zip(selected_st, selected_et):
            lbound = annotation_list.ix[st, s_annotation.st_col]
            rbound = annotation_list.ix[et, s_annotation.et_col]
            lbound = lbound - timedelta(seconds=5)
            rbound = rbound + timedelta(seconds=5)
            selected_raw = s_raw.select_raw_by_ts(raw_ankle_data, lbound,
                                                  rbound)
            selected_annotation = s_annotation.select_annotation_by_ts(
                annotation_list, lbound, rbound)
            selected_raw = s_viewer._prepare_raw_for_plot(selected_raw)
            print selected_annotation
            ax = consolidate_figure.add_subplot(nrows, ncols, c)
            s_viewer.get_singlesensor_raw_plot(selected_raw,
                                               selected_annotation,
                                               subplots=False,
                                               ax=ax,
                                               figsize=None)
            single_figure = pyplot.figure(figsize=(20, 10))
            s_viewer.get_singlesensor_raw_plot(selected_raw,
                                               selected_annotation,
                                               ax=single_figure.gca(),
                                               subplots=False)
            figfile = 'session' + str(i) + '_walking_episode' + str(
                c) + '.rawplot.png'
            single_figure.savefig(s_info.post_figure_folder + figfile)
            pyplot.close(single_figure)
            c += 1
        # add legend
        ax = consolidate_figure.add_subplot(nrows, ncols, c)
        s_viewer.get_legends_plot()
        figfile = 'session' + str(i) + '_walking_episodes.rawplot.png'
        consolidate_figure.savefig(s_info.post_figure_folder + figfile)
        pyplot.close(consolidate_figure)
        sys.exit(1)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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 main():
    testfile_raw = "../puff_corrected_dataset/session7_DW.raw.csv"
    testfile_annotation = "../puff_corrected_dataset/session7.annotation.csv"
    raw_df = w_utils.raw_csv_importer(testfile_raw)
    annotation_df = s_annotation.annotation_csv_importer(testfile_annotation)
    mean_raw = raw_df.mean()
    std_raw = raw_df.std()
    # select by label
    labels = [[
        'right-puff',
    ], [
        'right-puff',
    ], [
        'walking',
    ], [
        'eating-a-meal',
    ], ['sitting', 'not-smoking', 'no-activity']]
    seeds = [0, 10, 2, 3, 10]
    for label, seed in zip(labels, seeds):
        lbound, rbound = s_annotation.generate_bounds_by_labels(
            annotation_df,
            duration=timedelta(seconds=30),
            labels=label,
            seed=seed)

        selected_a = s_annotation.select_annotation_by_ts(annotation_df,
                                                          lbound=lbound,
                                                          rbound=rbound)
        selected_raw = s_raw.select_raw_by_ts(raw_df,
                                              lbound=lbound,
                                              rbound=rbound)
        # test_filtering(selected_raw, selected_a)
        # test_linearRegression(selected_raw, selected_a)
        pyplot.figure()
        pyplot.suptitle(label)
        # test_preprocessing(selected_raw, selected_a, mean_raw, std_raw)
        test_peak_rate_computation(selected_raw, selected_a, mean_raw, std_raw)
    pyplot.show()
def main():
    # generate legend plot
    fhandle = s_viewer.get_legends_plot()
    pyplot.show()
    # fname = s_info.puff_figure_folder + 'legend.png'
    # fhandle.savefig(fname)
    # pyplot.close(fhandle)
    sys.exit(1)
    for i in s_info.session_arr:
        # i=7
        # read in raw and annotation
        annotation_file = 'session' + str(i) + '.annotation.csv'
        raw_files = [
            'session' + str(i) + '_' + code + '.raw.csv'
            for code in s_info.sensor_codes
        ]
        raw_datas = [
            w_utils.raw_csv_importer(s_info.clean_dataset_folder + raw_file)
            for raw_file in raw_files
        ]
        consolidate_raw = w_utils.raw_data_consolidator(
            raw_datas,
            sessions=[
                i,
            ] * len(raw_datas),
            sensors=s_info.sensor_codes)
        annotation_data = s_annotation.annotation_csv_importer(
            s_info.puff_corrected_folder +
            annotation_file)  # use puff corrected annotation
        # annotation_data = s_annotation.annotation_csv_importer(s_info.clean_dataset_folder + annotation_file) # use clean annotation without puff correction

        # get all puff annotations
        puff_st_markers = (
            annotation_data[s_annotation.puff_col] != 'no-puff') & (
                (annotation_data[s_annotation.puff_col].shift(1) == 'no-puff')
                | pd.isnull(annotation_data[s_annotation.puff_col].shift(-1)))
        puff_et_markers = (
            annotation_data[s_annotation.puff_col] != 'no-puff') & (
                (annotation_data[s_annotation.puff_col].shift(-1) == 'no-puff')
                | pd.isnull(annotation_data[s_annotation.puff_col].shift(-1)))
        c = 0
        for st, et in zip(
                annotation_data[puff_st_markers][s_annotation.st_col],
                annotation_data[puff_et_markers][s_annotation.et_col]):
            # get raw and annotation for current puff
            middle_time = st + (et - st) / 2
            from datetime import timedelta
            lbound = middle_time - timedelta(seconds=15)
            rbound = middle_time + timedelta(seconds=15)
            selected_puff_raw = s_raw.select_raw_by_ts(consolidate_raw,
                                                       lbound=lbound,
                                                       rbound=rbound,
                                                       by='sensor')
            selected_annotation = s_annotation.select_annotation_by_ts(
                annotation_data, lbound=lbound, rbound=rbound)

            selected_annotations = [
                selected_annotation.copy() for s in s_info.sensor_codes
            ]
            consolidate_selected_annotation = s_annotation.annotation_data_consolidator(
                selected_annotations, sensors=s_info.sensor_codes)

            # plot multisensor figure and save them
            if annotation_data[annotation_data[s_annotation.st_col] == st][
                    s_annotation.puff_col] == 'left-puff':
                side = 'L'
            else:
                side = 'R'
            fig = s_viewer.get_multisensor_raw_plot(
                selected_puff_raw,
                consolidate_selected_annotation,
                subplots=False)
            fname = s_info.puff_figure_folder + 'session' + str(
                i) + '_puff' + str(c) + '_' + side + '.rawplot.png'
            fig.savefig(fname)
            pyplot.close(fig)
            print fname + " written"
            c += 1