Esempio n. 1
0
def get_obj_function_results(s, alg_name, metric, verbose=True):
    if metric == 'get_res':
        return s.res
    elif metric == 'both':
        rmse = sqrt(
            mean_squared_error(s.res['sc_manual'], s.res['sc_' + alg_name]))
        mape = mean_absolute_percentage_error(s.res['sc_manual'],
                                              s.res['sc_' + alg_name],
                                              handle_zeros=True)
        if verbose: print('\tResult: RMSE is ' + str(round(rmse, 2)))
        return rmse, mape
    elif metric == 'sc_mape':
        mape = mean_absolute_percentage_error(s.res['sc_manual'],
                                              s.res['sc_' + alg_name],
                                              handle_zeros=True)
        if verbose:
            print('\tResult: Mean Absolute Percentage Error is ' +
                  str(round(mape, 2)))
        return mape
    elif metric == 'sc_rmse':
        rmse = sqrt(
            mean_squared_error(s.res['sc_manual'], s.res['sc_' + alg_name]))
        if verbose: print('\tResult: RMSE ' + str(round(rmse, 2)))
        return rmse
    elif metric == 'asym_rmse':
        nanval = 0.5
        asym_rmse = sqrt(
            mean_squared_error(
                s.apdm_measures['apdm_toe_off_asymmetry_median'],
                s.res['step_time_asymmetry_median_' +
                      alg_name].fillna(nanval)))
        if verbose:
            print('\tResult: Toe off asymmetry RMSE is ' +
                  str(round(asym_rmse, 2)))
        return asym_rmse
    elif metric == 'apdm_cad_rmse':
        nanval = 50
        apdm_cad_rmse = sqrt(
            mean_squared_error(
                s.apdm_measures['cadence'],
                s.res['cadence_apdm_' + alg_name].fillna(nanval)))
        if verbose:
            print('\tResult: APDM cadence RMSE is ' +
                  str(round(apdm_cad_rmse, 2)))
        return apdm_cad_rmse
    else:  # rmse
        rmse = sqrt(
            mean_squared_error(s.res['sc_manual'], s.res['sc_' + alg_name]))
        if verbose: print('\tResult: RMSE ' + str(round(rmse, 2)))
        return rmse
Esempio n. 2
0
def objective_step_detection_two_sides_overlap(p):
    # Load input data to algorithms
    s = create_sd_class_for_obj_functions()

    # Set sample ids for dataset
    s.select_specific_samples(p['sample_ids'])

    signal_to_use = p['signal_to_use']
    if signal_to_use == 'vertical' and p['do_windows_if_vertical']:
        vert_win = p['vert_win']
    else:
        vert_win = None
    smoothing = p['smoothing']
    mva_win = p['mva_win']
    butter_freq = p['butter_freq']
    peak_type = p['peak_type']
    peak_param1 = None
    peak_param2 = None
    if p['peak_type'] == 'scipy':
        peak_param1 = p['p1_sc']
        peak_param2 = p['p2_sc']
    elif p['peak_type'] == 'peak_utils':
        peak_param1 = p['p1_pu']
        peak_param2 = p['p2_pu']
    win_size_merge = p['win_size_merge']
    win_size_remove_adjacent_peaks = p['win_size_remove_adjacent_peaks']
    metric = p['metric']
    if 'verbose' not in p:
        verbose = True
    else:
        verbose = p['verbose']
    max_dist_from_apdm = p['max_dist_from_apdm']

    s.step_detection_two_sides_overlap(
        signal_to_use=signal_to_use,
        smoothing=smoothing,
        mva_win=mva_win,
        vert_win=vert_win,
        butter_freq=butter_freq,
        peak_type=peak_type,
        peak_param1=peak_param1,
        peak_param2=peak_param2,
        win_size_merge=win_size_merge,
        win_size_remove_adjacent_peaks=win_size_remove_adjacent_peaks,
        verbose=verbose)

    # ********** Calculate RMSE and/or MAPE
    s.add_gait_metrics(verbose=False, max_dist_from_apdm=max_dist_from_apdm)
    rmse = sqrt(mean_squared_error(s.res['sc_manual'], s.res['sc_overlap']))
    mape = mean_absolute_percentage_error(s.res['sc_manual'],
                                          s.res['sc_overlap'],
                                          handle_zeros=True)

    nanval = 0.5
    asym_rmse = sqrt(
        mean_squared_error(
            s.apdm_measures['toe_off_asymmetry_median'],
            s.res['step_time_asymmetry2_median_overlap'].fillna(nanval)))

    if metric == 'both':
        if verbose: print('\tResult: RMSE is ' + str(round(rmse, 2)))
        return rmse, mape
    elif metric == 'sc_mape':
        if verbose:
            print('\tResult: Mean Absolute Percentage Error is ' +
                  str(round(mape, 2)))
        return mape
    elif metric == 'sc_rmse':
        if verbose: print('\tResult: RMSE ' + str(round(rmse, 2)))
        return rmse
    elif metric == 'get_res':
        return s.res
    elif metric == 'asym_rmse':
        if verbose:
            print('\tResult: Toe off asymmetry RMSE is ' +
                  str(round(asym_rmse, 2)))
        return asym_rmse
    else:  # rmse
        if verbose: print('\tResult: RMSE ' + str(round(rmse, 2)))
        return rmse
def create_regression_performance_plot(data_file,
                                       metric,
                                       save_name='alg_performance.png',
                                       rotate=True,
                                       show_plot=False,
                                       set_y_lim=True,
                                       y_min=0,
                                       y_max=30):
    # Set tasks
    with open(join(c.pickle_path, 'task_filters'), 'rb') as fp:
        task_filters = pickle.load(fp)
    # tasks = ['all'] + task_filters['Task Name'].tolist()
    tasks = task_filters['Task Name'].tolist()
    tasks.remove('Tug')
    tasks.remove('Tug')
    a, b = tasks.index('Walk - Regular'), tasks.index('Walk - Fast')
    tasks[b], tasks[a] = tasks[a], tasks[b]
    xlabs = [
        'Cane'
        if x == 'Asymmetry - Imagine you have a cane in the right hand' else x
        for x in tasks
    ]
    xlabs = [
        'No shoe' if x == 'Asymmetry - No right shoe' else x for x in xlabs
    ]
    xlabs = [
        'Hands on side' if x == 'Walk - Both hands side' else x for x in xlabs
    ]
    xlabs = [x[6:] if 'Walk - ' in x else x for x in xlabs]

    # Set plot values
    means = []
    stds = []
    algs = None
    data = pd.read_csv(data_file)
    if 'Algorithm' in data.columns:
        analysis_type = 'folds'
    elif 'sc_manual' in data.columns:
        analysis_type = 'all'
    else:
        analysis_type = 'unknown'

    if analysis_type == 'folds':
        algs = data['Algorithm'].unique().tolist()
        algs.sort()
        for i in range(len(algs)):
            means_i = []
            stds_i = []
            for j in range(len(tasks)):
                vals = data[(data['Algorithm'] == algs[i])
                            & (data['WalkingTask'] == tasks[j])][metric]
                means_i.append(vals.mean())
                stds_i.append(vals.std())
            means.append(means_i)
            stds.append(stds_i)
    if analysis_type == 'all':
        algs = [
            data.columns[i] for i in range(len(data.columns))
            if 'sc_' in data.columns[i]
        ]
        true_label = 'sc_manual'
        if true_label in algs: algs.remove(true_label)
        # Set order
        if 'sc_fusion_high_level_union_one_stage' in algs:
            algs.insert(
                0,
                algs.pop(algs.index('sc_fusion_high_level_union_one_stage')))
        if 'sc_fusion_high_level_union_two_stages' in algs:
            algs.insert(
                0,
                algs.pop(algs.index('sc_fusion_high_level_union_two_stages')))
        if 'sc_fusion_high_level_intersect' in algs:
            algs.insert(0,
                        algs.pop(algs.index('sc_fusion_high_level_intersect')))
        if 'sc_fusion_low_level_diff' in algs:
            algs.insert(0, algs.pop(algs.index('sc_fusion_low_level_diff')))
        if 'sc_fusion_low_level_sum' in algs:
            algs.insert(0, algs.pop(algs.index('sc_fusion_low_level_sum')))
        if 'sc_rhs' in algs:
            algs.insert(0, algs.pop(algs.index('sc_rhs')))
        if 'sc_lhs' in algs:
            algs.insert(0, algs.pop(algs.index('sc_lhs')))

        # remove
        algs.pop(algs.index('sc_fusion_high_level_union_one_stage'))
        algs.pop(algs.index('sc_lhs'))
        algs.pop(algs.index('sc_fusion_high_level_intersect'))
        algs.pop(algs.index('sc_fusion_low_level_diff'))

        with open(join(c.pickle_path, 'metadata_sample'), 'rb') as fp:
            sample = pickle.load(fp)
        sample['TaskName'] = sample['TaskName'].replace(
            'Walk - Imagine you have a cane in the right hand',
            'Asymmetry - Imagine you have a cane in the right hand')
        sample['TaskName'] = sample['TaskName'].replace(
            'Walk - Without right shoe', 'Asymmetry - No right shoe')

        for i in range(len(algs)):
            means_i = []
            stds_i = []
            for j in range(len(tasks)):
                if tasks[j] == 'all':
                    sample_ids = data['SampleId'].as_matrix()
                else:
                    sample_ids = sample[sample['TaskName'] ==
                                        tasks[j]]['SampleId'].as_matrix()
                alg_vals = data.loc[data['SampleId'].isin(sample_ids)][
                    algs[i]].as_matrix()
                true_vals = data.loc[data['SampleId'].isin(
                    sample_ids)][true_label].as_matrix()

                if metric == 'PE':  # percent error
                    vals = 100 * (alg_vals - true_vals) / true_vals
                    mean_val = vals.mean()
                    std_val = vals.std()
                elif metric == 'MAPE':
                    mean_val, std_val = mean_absolute_percentage_error(
                        true_vals,
                        alg_vals,
                        handle_zeros=True,
                        return_std=True)
                else:  # default is 'RMSE'
                    mean_val = sqrt(mean_squared_error(true_vals, alg_vals))
                    std_val = 0

                means_i.append(mean_val)
                stds_i.append(std_val)
            means.append(means_i)
            stds.append(stds_i)

    # Plotting
    fig, ax = plt.subplots()

    # Set legend
    # algs = [alg.replace('sc_', '') for alg in algs]
    # algs = ['Left side only' if alg == 'lhs' else alg for alg in algs]
    # algs = ['Right side only' if alg == 'rhs' else alg for alg in algs]
    # algs = ['Fusion - Sum raw signal' if 'sum' in alg else alg for alg in algs]
    # algs = ['Fusion - Diff raw signal' if 'diff' in alg else alg for alg in algs]
    # algs = ['Fusion - Intersection of steps' if 'intersect' in alg else alg for alg in algs]
    # algs = ['Fusion - Union two stages' if 'two_sta' in alg else alg for alg in algs]
    # algs = ['Fusion - Union one stage' if 'one_sta' in alg else alg for alg in algs]

    algs = [alg.replace('sc_', '') for alg in algs]
    algs = ['Left side only' if alg == 'lhs' else alg for alg in algs]
    algs = ['Right only' if alg == 'rhs' else alg for alg in algs]
    algs = ['Sum raw signal' if 'sum' in alg else alg for alg in algs]
    algs = [
        'Fusion - Diff raw signal' if 'diff' in alg else alg for alg in algs
    ]
    algs = [
        'Fusion - Intersection of steps' if 'intersect' in alg else alg
        for alg in algs
    ]
    algs = ['Union of events' if 'two_sta' in alg else alg for alg in algs]
    algs = ['Union of events' if 'one_sta' in alg else alg for alg in algs]
    legends = algs

    groups = means
    errors = stds
    colors = ['r', 'm', 'b', 'c', 'y', 'g', 'k']

    x = range(len(groups[0]))
    list_dots = list()
    gap = 1.5
    for idx, group in enumerate(groups):
        left = [0.2 + i * gap + idx * 0.15 for i in x]
        dots = ax.errorbar(left,
                           group,
                           color=colors[idx],
                           yerr=errors[idx],
                           ecolor='k',
                           capsize=5,
                           fmt='o')
        list_dots.append(dots)

    # Legend
    list_dots = [list_dot[0] for list_dot in list_dots]
    #ax.legend(list_dots, legends, loc='upper center', bbox_to_anchor=(0.5, 1.12), ncol=2)
    ax.legend(list_dots,
              legends,
              loc='upper center',
              bbox_to_anchor=(0.5, 1.12),
              ncol=3)

    # set X tick labels
    ax.set_xticks([gap * i + 0.4 for i in x])
    if rotate:
        ax.set_xticklabels(xlabs, rotation=45, fontsize=11)
    else:
        xlabs = ['Right\nbag' if x == ' Right bag' else x for x in xlabs]
        xlabs = [
            'Hands\non side' if x == 'Hands on side' else x for x in xlabs
        ]
        ax.set_xticklabels(xlabs, fontsize=12)
    ax.set_xlim(0, gap * (max(x) + 1))
    ax.set_xlabel('Task type', fontsize=16)
    if set_y_lim:
        ax.set_ylim(y_min, y_max)

    # Hide the right and top spines
    ax.spines['bottom'].set_visible(True)
    ax.spines['left'].set_visible(True)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    # Only show ticks on the bottom spine
    ax.xaxis.set_ticks_position('bottom')

    # y label
    if metric == 'PE':
        metric = 'Percent error'
    ax.set_ylabel(metric, fontsize=18)
    plt.yticks(fontsize=12)

    # y=0 line
    plt.axhline(0, color='black', linestyle='--')

    plt.subplots_adjust(top=0.9, right=0.9, bottom=0.2)
    # plt.tight_layout()
    save_path = join(dirname(data_file), save_name)
    fig = plt.gcf()
    fig.set_size_inches(8, 5)
    plt.savefig(save_path)
    if show_plot:
        plt.show()