def show_different_filter_configurations(alpha=0.8): samples_vehicle_1 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-guindo/vehicle-samples-2017-09-19-192509.csv' ) samples_power_on = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/static-power-on.csv') # samples_power_off = read_csv_acc_samples_file('c:\\users/rafael/desktop/static-power-off.csv') samples_power_on = samples_power_on[new_start:] samples_vehicle_1 = samples_vehicle_1[new_start:] # samples_power_off = samples_power_off[new_start:] filter_gravity(samples_power_on, alpha=alpha) filter_gravity(samples_vehicle_1, alpha=alpha) # filter_gravity(samples_power_off, alpha=alpha) normalize_time(samples_power_on) normalize_time(samples_vehicle_1) # normalize_time(samples_power_off) ts_top_threshold = 150 samples_power_on = list( filter(lambda x: x.timestamp < ts_top_threshold, samples_power_on)) samples_vehicle_1 = list( filter(lambda x: x.timestamp < ts_top_threshold, samples_vehicle_1)) # samples_power_off = list(filter(lambda x: x.timestamp < ts_top_threshold, samples_power_off)) # samples_power_on = samples_power_on[:ts_top_threshold] # samples_power_off = samples_power_off[:ts_top_threshold] # plot_activity_data(samples_power_on, 'Powered on, alpha={}'.format(alpha)) # plot_activity_data(samples_vehicle_1, 'V1, alpha={}'.format(alpha)) # plot_activity_data(samples_power_off, 'Powered off, alpha={}'.format(alpha)) # plot the magnitude vectors mv_static_1 = calculate_magnitude_vector(samples_power_on) mv_vehicle_1 = calculate_magnitude_vector(samples_vehicle_1) vectors = [mv_static_1, mv_vehicle_1] activities = ['Static', 'V1'] vectors = [mv_static_1, mv_vehicle_1] activities = ['Static', 'V1'] plot_magnitude_vectors(vectors, activities)
def calculate_global_statistics(files, remove_gravity=True): for f in files: samples = read_csv_acc_samples_file(f) if remove_gravity: filter_gravity(samples) samples = samples[new_start:] mv = calculate_magnitude_vector(samples) mean_value = np.mean(mv) print('For {} mean is {}'.format(f, mean_value))
def plot_accelerations_and_mag_vectors( samples_list: List[List[AccelerometerSample]], activity_labels): plt.style.use('bmh') plt.rcParams['font.family'] = 'Gotham XNarrow' plt.rcParams['font.serif'] = 'Gotham XNarrow' plt.rcParams['font.monospace'] = 'Monaco' plt.rcParams['font.size'] = 10 plt.rcParams['axes.labelsize'] = 12 plt.rcParams['axes.labelweight'] = 'normal' plt.rcParams['axes.titlesize'] = 14 plt.rcParams['xtick.labelsize'] = 10 plt.rcParams['ytick.labelsize'] = 10 plt.rcParams['legend.fontsize'] = 7 plt.rcParams['figure.titlesize'] = 13 plt.rcParams['legend.fontsize'] = 9 fig, axes = plt.subplots(4) i = 0 for samples in samples_list: xs = list(map(lambda x: x.timestamp, samples)) ys = list(map(lambda x: x.x, samples)) axes[0].plot(xs, ys, label='{}'.format(activity_labels[i]), linewidth='1') ys = list(map(lambda x: x.y, samples)) axes[1].plot(xs, ys, label='{}'.format(activity_labels[i]), linewidth='1') ys = list(map(lambda x: x.z, samples)) axes[2].plot(xs, ys, label='{}'.format(activity_labels[i]), linewidth='1') mv = calculate_magnitude_vector(samples) axes[3].plot(xs, mv, label='MV {}'.format(activity_labels[i]), linewidth='1') i += 1 axes[0].legend() axes[1].legend() axes[2].legend() axes[3].legend()
def show_magnitude_vectors_of_activities(files, labels, remove_gravity=True): mvs = [] for file in files: print('reading {}'.format(file)) samples = read_csv_acc_samples_file(file) if remove_gravity: filter_gravity(samples) samples = samples[20:] # samples = samples[2500:7500] mv = calculate_magnitude_vector(samples) mvs.append(mv) plot_magnitude_vectors(mvs, labels)
def plot_some_activities_data(samples_list, activities_string): plt.style.use('bmh') plt.rcParams['font.family'] = 'Gotham XNarrow' plt.rcParams['font.serif'] = 'Gotham XNarrow' plt.rcParams['font.monospace'] = 'Monaco' plt.rcParams['font.size'] = 10 plt.rcParams['axes.labelsize'] = 12 plt.rcParams['axes.labelweight'] = 'normal' plt.rcParams['axes.titlesize'] = 14 plt.rcParams['xtick.labelsize'] = 10 plt.rcParams['ytick.labelsize'] = 10 plt.rcParams['legend.fontsize'] = 7 plt.rcParams['figure.titlesize'] = 13 plt.rcParams['legend.fontsize'] = 9 fig, axes = plt.subplots(4, len(activities_string)) shown_y_label = False for i in range(0, len(activities_string)): samples = samples_list[i] xs = list(map(lambda x: x.timestamp, samples)) ys = list(map(lambda x: x.x, samples)) axes[0][i].plot(xs, ys, label='X axis', linewidth='1') axes[0][i].legend() axes[0][i].set_title(activities_string[i]) ys = list(map(lambda x: x.y, samples)) axes[1][i].plot(xs, ys, label='Y axis', linewidth='1') axes[1][i].legend() ys = list(map(lambda x: x.z, samples)) axes[2][i].plot(xs, ys, label='Z axis', linewidth='1') axes[2][i].legend() ys = calculate_magnitude_vector(samples) axes[3][i].plot(xs, ys, label='Magnitude vector', linewidth='1') axes[3][i].legend() axes[3][i].set_xlabel('Time') if not shown_y_label: axes[0][i].set_ylabel('Acc (Earth G off)') axes[1][i].set_ylabel('Acc (Earth G off)') axes[2][i].set_ylabel('Acc (Earth G off)') axes[3][i].set_ylabel('Magnitude vector') shown_y_label = True
def show_magnitude_vector_per_alpha(alpha_values, file_path): vectors = [] samples = read_csv_acc_samples_file(file_path) normalize_time(samples) timestamps = list(map(lambda x: x.timestamp, samples)) for alpha in alpha_values: samples = read_csv_acc_samples_file(file_path) normalize_time(samples) filter_gravity(samples, alpha=alpha) mv = calculate_magnitude_vector(samples) vectors.append(mv) plot_magnitude_vectors_per_alpha(vectors, timestamps, alpha_values, single=True)
def plot_activity_data(samples: List[AccelerometerSample], label, path_to_save=None): plt.style.use('bmh') plt.rcParams['font.family'] = 'Gotham XNarrow' plt.rcParams['font.serif'] = 'Gotham XNarrow' plt.rcParams['font.monospace'] = 'Monaco' plt.rcParams['font.size'] = 10 plt.rcParams['axes.labelsize'] = 12 plt.rcParams['axes.labelweight'] = 'normal' plt.rcParams['axes.titlesize'] = 14 plt.rcParams['xtick.labelsize'] = 10 plt.rcParams['ytick.labelsize'] = 10 # plt.rcParams['legend.fontsize'] = 11 plt.rcParams['legend.fontsize'] = 7 plt.rcParams['figure.titlesize'] = 13 plt.rcParams['legend.fontsize'] = 9 fig, axes = plt.subplots(4, sharex=True) fig.suptitle(label) xs = list(map(lambda x: x.timestamp, samples)) ys = list(map(lambda x: x.x, samples)) axes[0].plot(ys, label='X axis', linewidth='1') axes[0].set_ylabel('Acc (Earth G off)') axes[0].legend() ys = list(map(lambda x: x.y, samples)) axes[1].plot(ys, label='Y axis', linewidth='1') axes[1].set_ylabel('Acc (Earth G off)') axes[1].legend() ys = list(map(lambda x: x.z, samples)) axes[2].plot(ys, label='Z axis', linewidth='1') axes[2].set_ylabel('Acc (Earth G off)') axes[2].legend() ys = calculate_magnitude_vector(samples) axes[3].plot(ys, label='Magnitude vector', linewidth='1') axes[3].set_ylabel('Magnitude vector') axes[3].legend() axes[3].set_xlabel('Time') if path_to_save is not None: plt.savefig(path_to_save, format='pdf', bbox_inches='tight')
def plot_all_activities_data(samples_static: List[AccelerometerSample], samples_walking: List[AccelerometerSample], samples_running: List[AccelerometerSample], samples_vehicle: List[AccelerometerSample], path_to_save=None): plt.style.use('bmh') plt.rcParams['font.family'] = 'Gotham XNarrow' plt.rcParams['font.serif'] = 'Gotham XNarrow' plt.rcParams['font.monospace'] = 'Monaco' plt.rcParams['font.size'] = 10 plt.rcParams['axes.labelsize'] = 12 plt.rcParams['axes.labelweight'] = 'normal' plt.rcParams['axes.titlesize'] = 14 plt.rcParams['xtick.labelsize'] = 10 plt.rcParams['ytick.labelsize'] = 10 # plt.rcParams['legend.fontsize'] = 11 plt.rcParams['legend.fontsize'] = 7 plt.rcParams['figure.titlesize'] = 13 plt.rcParams['legend.fontsize'] = 9 # fig, axes = plt.subplots(4, 4, sharex=True) fig, axes = plt.subplots(4, 4) above_title = '-' shown_y_label = False for i in range(0, 4): if i == 0: samples = samples_static above_title = 'Static' if i == 1: samples = samples_walking above_title = 'Walking' if i == 2: samples = samples_running above_title = 'Running' if i == 3: samples = samples_vehicle above_title = 'Vehicle' xs = list(map(lambda x: x.timestamp, samples)) ys = list(map(lambda x: x.x, samples)) axes[0][i].plot(xs, ys, label='X axis', linewidth='1') axes[0][i].legend() axes[0][i].set_title(above_title) ys = list(map(lambda x: x.y, samples)) axes[1][i].plot(xs, ys, label='Y axis', linewidth='1') axes[1][i].legend() ys = list(map(lambda x: x.z, samples)) axes[2][i].plot(xs, ys, label='Z axis', linewidth='1') axes[2][i].legend() ys = calculate_magnitude_vector(samples) axes[3][i].plot(xs, ys, label='Magnitude vector', linewidth='1') axes[3][i].legend() axes[3][i].set_xlabel('Time') if not shown_y_label: axes[0][i].set_ylabel('Acc (Earth G off)') axes[1][i].set_ylabel('Acc (Earth G off)') axes[2][i].set_ylabel('Acc (Earth G off)') axes[3][i].set_ylabel('Magnitude vector') shown_y_label = True # plt.tight_layout() # axis.zaxis.labelpad = 15 if path_to_save is not None: plt.savefig(path_to_save, format='pdf', bbox_inches='tight')
def do_work(): home = str(Path.home()) + '/' desktop = home + 'desktop/' samples_vehicle_1 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-guindo/vehicle-samples-2017-09-19-192509.csv' ) samples_vehicle_2 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-guindo/vehicle-samples-2017-09-20-081651.csv' ) samples_vehicle_3 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-guindo/vehicle-samples-2017-09-20-092617.csv' ) samples_vehicle_4 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-arena/vehicle-samples-2017-09-20-131224.csv' ) samples_vehicle_5 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/vehicle/focus-arena/vehicle-samples-2017-09-20-140314.csv' ) samples_static_1 = read_csv_acc_samples_file( 'c:\\users/rafael/desktop/static/static-samples-2017-09-21-183652-ui.csv' ) # filter_gravity_per_window(samples_vehicle_1) # filter_gravity_per_window(samples_vehicle_2) # filter_gravity_per_window(samples_vehicle_3) # filter_gravity_per_window(samples_vehicle_4) # filter_gravity_per_window(samples_vehicle_5) # filter_gravity_per_window(samples_static_1) filter_gravity(samples_vehicle_1) filter_gravity(samples_vehicle_2) filter_gravity(samples_vehicle_3) filter_gravity(samples_vehicle_4) filter_gravity(samples_vehicle_5) filter_gravity(samples_static_1) samples_vehicle_1 = samples_vehicle_1[new_start:] samples_vehicle_2 = samples_vehicle_2[new_start:] samples_vehicle_3 = samples_vehicle_3[new_start:] samples_vehicle_4 = samples_vehicle_4[new_start:] samples_vehicle_5 = samples_vehicle_5[new_start:] samples_static_1 = samples_static_1[new_start:] normalize_time(samples_vehicle_1) normalize_time(samples_vehicle_2) normalize_time(samples_vehicle_3) normalize_time(samples_vehicle_4) normalize_time(samples_vehicle_5) normalize_time(samples_static_1) plot_activity_data(samples_vehicle_1, 'Vehicle one') plot_activity_data(samples_vehicle_2, 'Vehicle two') plot_activity_data(samples_vehicle_3, 'Vehicle three') plot_activity_data(samples_vehicle_4, 'Vehicle four') plot_activity_data(samples_vehicle_5, 'Vehicle five') plot_activity_data(samples_static_1, 'Static one') # Plot of magnitude vectors top_threshold = 500 static_1_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_static_1)) vehicle_1_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_vehicle_1)) vehicle_2_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_vehicle_2)) vehicle_3_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_vehicle_3)) vehicle_4_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_vehicle_4)) vehicle_5_portion = list( filter(lambda x: x.timestamp < top_threshold, samples_vehicle_5)) mv_static_1 = calculate_magnitude_vector(static_1_portion) mv_vehicle_1 = calculate_magnitude_vector(vehicle_1_portion) mv_vehicle_2 = calculate_magnitude_vector(vehicle_2_portion) mv_vehicle_3 = calculate_magnitude_vector(vehicle_3_portion) mv_vehicle_4 = calculate_magnitude_vector(vehicle_4_portion) mv_vehicle_5 = calculate_magnitude_vector(vehicle_5_portion) vectors = [ mv_static_1, mv_vehicle_1, mv_vehicle_2, mv_vehicle_3, mv_vehicle_4, mv_vehicle_5 ] activities = ['Static', 'V1', 'V2', 'V3', 'V4', 'V5'] vectors = [mv_static_1, mv_vehicle_5] activities = ['Static', 'V5'] # plot_magnitude_vectors(vectors, activities) # Plot of statistics statistics_static_1 = get_statistics_per_window(samples_static_1) statistics_vehicle_1 = get_statistics_per_window(samples_vehicle_1) statistics_vehicle_2 = get_statistics_per_window(samples_vehicle_2) statistics_vehicle_3 = get_statistics_per_window(samples_vehicle_3) statistics_vehicle_4 = get_statistics_per_window(samples_vehicle_4) statistics_vehicle_5 = get_statistics_per_window(samples_vehicle_5) statistics_ids = ['std_dev', 'mean'] plot_statistics(statistics_static_1, statistics_vehicle_1, statistics_vehicle_2, statistics_vehicle_3, statistics_ids, ['Static', 'Vehicle 1', 'Vehicle 2', 'Vehicle 3']) # plot_statistics(statistics_static, statistics_walking, statistics_running, statistics_vehicle, statistics_ids) plot_statistics(statistics_static_1, [], [], statistics_vehicle_1, statistics_ids, ['Static', 'Vehicle 1', 'Vehicle 2', 'Vehicle 3']) plt.show()