'acc_Acceleration x (m/s^2)', 'acc_Acceleration y (m/s^2)', 'acc_Acceleration z (m/s^2)', 'press_Pressure (hPa)', 'gyr_Gyroscope x (rad/s)', 'gyr_Gyroscope y (rad/s)', 'gyr_Gyroscope z (rad/s)', 'mag_Magnetic field x (muT)', 'mag_Magnetic field y (muT)', 'mag_Magnetic field z (muT)', 'linacc_Linear Acceleration x (m/s^2)', 'linacc_Linear Acceleration y (m/s^2)', 'linacc_Linear Acceleration z (m/s^2)', 'hr_Heart Rate' ] dataset = NumAbs.abstract_numerical(dataset, selected_predictor_cols, ws, 'mean') dataset = NumAbs.abstract_numerical(dataset, selected_predictor_cols, ws, 'std') CatAbs = CategoricalAbstraction() dataset = CatAbs.abstract_categorical(dataset, ['label'], ['like'], 0.03, ws, 2) # # Now we move to the frequency domain, with the same window size. # # FreqAbs = FourierTransformation() # fs = float(1000)/milliseconds_per_instance # # periodic_predictor_cols = [c for c in dataset.columns if (not ('label' in c))] # # data_table = FreqAbs.abstract_frequency(copy.deepcopy(dataset), ['mag_Magnetic field x (muT)'], int(float(10000)/milliseconds_per_instance), fs) # # # Spectral analysis. # # DataViz.plot_dataset(data_table, ['mag_Magnetic field x (muT)_max_freq', 'mag_Magnetic field x (muT)_freq_weighted', 'mag_Magnetic field x (muT)_pse', 'label'], ['like', 'like', 'like', 'like'], ['line', 'line', 'line','points'])
ws = int(float(0.3 * 60000) / milliseconds_per_instance) selected_predictor_cols = [c for c in dataset.columns if not 'label' in c] dataset = NumAbs.abstract_numerical(dataset, selected_predictor_cols, ws, 'mean') dataset = NumAbs.abstract_numerical(dataset, selected_predictor_cols, ws, 'std') DataViz.plot_dataset( dataset, [ 'gravity.x', 'gravity.y', 'gravity.z', 'userAcceleration.x', 'userAcceleration.y', 'userAcceleration.z', 'label' ], ['like', 'like', 'like', 'like', 'like', 'like', 'like'], ['line', 'line', 'line', 'line', 'line', 'line', 'points']) CatAbs = CategoricalAbstraction() dataset = CatAbs.abstract_categorical( dataset, ['label'], ['like'], 0.03, int(float(5 * 60000) / milliseconds_per_instance), 2) # Now we move to the frequency domain, with the same window size. FreqAbs = FourierTransformation() fs = float(1000) / milliseconds_per_instance periodic_predictor_cols = [ 'gravity.x', 'gravity.y', 'gravity.z', 'userAcceleration.x', 'userAcceleration.y', 'userAcceleration.z' ] data_table = FreqAbs.abstract_frequency( copy.deepcopy(dataset), ['userAcceleration.x'],
def main(): # Read the result from the previous chapter convert the index to datetime try: dataset = pd.read_csv(DATA_PATH / DATASET_FNAME, index_col=0) dataset.index = pd.to_datetime(dataset.index) except IOError as e: print( 'File not found, try to run previous crowdsignals scripts first!') raise e # Create an instance of visualization class to plot the results DataViz = VisualizeDataset(__file__) # Compute the number of milliseconds covered by an instance based on the first two rows milliseconds_per_instance = (dataset.index[1] - dataset.index[0]).microseconds / 1000 # Create objects for feature abstraction NumAbs = NumericalAbstraction() CatAbs = CategoricalAbstraction() FreqAbs = FourierTransformation() if FLAGS.mode == 'time': # Focus on the time domain first # Set the window sizes to the number of instances representing 5 seconds, 30 seconds and 5 minutes window_sizes = [ int(float(5000) / milliseconds_per_instance), int(float(0.5 * 60000) / milliseconds_per_instance), int(float(5 * 60000) / milliseconds_per_instance) ] dataset_copy = copy.deepcopy(dataset) for ws in window_sizes: print( f'Abstracting numerical features for window size {ws * milliseconds_per_instance / 1000}s.' ) dataset_copy = NumAbs.abstract_numerical( data_table=dataset_copy, cols=['acc_phone_x'], window_size=ws, aggregation_function='mean') dataset_copy = NumAbs.abstract_numerical( data_table=dataset_copy, cols=['acc_phone_x'], window_size=ws, aggregation_function='std') DataViz.plot_dataset(data_table=dataset_copy, columns=[ 'acc_phone_x', 'acc_phone_x_temp_mean', 'acc_phone_x_temp_std', 'label' ], match=['exact', 'like', 'like', 'like'], display=['line', 'line', 'line', 'points']) elif FLAGS.mode == 'frequency': # Move to the frequency domain with the same window size fs = 1000.0 / milliseconds_per_instance ws = int(10000.0 / milliseconds_per_instance) data_table = FreqAbs.abstract_frequency( data_table=copy.deepcopy(dataset), cols=['acc_phone_x'], window_size=ws, sampling_rate=fs) # Spectral analysis DataViz.plot_dataset(data_table=data_table, columns=[ 'acc_phone_x_max_freq', 'acc_phone_x_freq_weighted', 'acc_phone_x_pse', 'label' ], match=['like', 'like', 'like', 'like'], display=['line', 'line', 'line', 'points']) elif FLAGS.mode == 'final': ws = int(float(0.5 * 60000) / milliseconds_per_instance) fs = 1000.0 / milliseconds_per_instance # Abstract time domain features and plot the result selected_predictor_cols = [ c for c in dataset.columns if 'label' not in c ] print('Calculating mean and std for selected predictor cols.') dataset = NumAbs.abstract_numerical(data_table=dataset, cols=selected_predictor_cols, window_size=ws, aggregation_function='mean') dataset = NumAbs.abstract_numerical(data_table=dataset, cols=selected_predictor_cols, window_size=ws, aggregation_function='std') DataViz.plot_dataset(data_table=dataset, columns=[ 'acc_phone_x', 'gyr_phone_x', 'hr_watch_rate', 'light_phone_lux', 'mag_phone_x', 'press_phone_', 'pca_1', 'label' ], match=[ 'like', 'like', 'like', 'like', 'like', 'like', 'like', 'like' ], display=[ 'line', 'line', 'line', 'line', 'line', 'line', 'line', 'points' ]) # Abstract categorical features print('Abstracting categorical features.') dataset = CatAbs.abstract_categorical( data_table=dataset, cols=['label'], match=['like'], min_support=0.03, window_size=int(float(5 * 60000) / milliseconds_per_instance), max_pattern_size=2) # Abstract frequency domain features periodic_predictor_cols = [ 'acc_phone_x', 'acc_phone_y', 'acc_phone_z', 'acc_watch_x', 'acc_watch_y', 'acc_watch_z', 'gyr_phone_x', 'gyr_phone_y', 'gyr_phone_z', 'gyr_watch_x', 'gyr_watch_y', 'gyr_watch_z', 'mag_phone_x', 'mag_phone_y', 'mag_phone_z', 'mag_watch_x', 'mag_watch_y', 'mag_watch_z' ] print('Abstracting frequency features.') dataset = FreqAbs.abstract_frequency(data_table=dataset, cols=periodic_predictor_cols, window_size=ws, sampling_rate=fs) # Take a certain percentage of overlap in the windows, otherwise training examples will be too much alike # Set the allowed percentage of overlap window_overlap = FLAGS.overlap skip_points = int((1 - window_overlap) * ws) dataset = dataset.iloc[::skip_points, :] # Plot the final dataset DataViz.plot_dataset(data_table=dataset, columns=[ 'acc_phone_x', 'gyr_phone_x', 'hr_watch_rate', 'light_phone_lux', 'mag_phone_x', 'press_phone_', 'pca_1', 'label' ], match=[ 'like', 'like', 'like', 'like', 'like', 'like', 'like', 'like' ], display=[ 'line', 'line', 'line', 'line', 'line', 'line', 'line', 'points' ]) # Store the generated dataset dataset.to_csv(DATA_PATH / RESULT_FNAME)