-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureExtraction.py
More file actions
79 lines (69 loc) · 3.19 KB
/
FeatureExtraction.py
File metadata and controls
79 lines (69 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pandas as pd
import numpy as np
from dataCleaning import read_run, column_clean, preprocessing
import pdb
def overall_cleaning():
df_p3_exo = read_run("P3_Exo_1_0.csv") # second run, male
df_p3_noexo = read_run("P3_NoExo_1_0.csv") # first run, male
df_p4_exo = read_run("P4_Exo_1_0.csv") # 1st run female
df_p4_noexo = read_run("P4_NoExo_1_0.csv") # 2nd female
df_p3_exo = column_clean(df_p3_exo, run_num = 2, gender = 'male')
df_p3_noexo = column_clean(df_p3_noexo, run_num = 1, gender = 'male')
df_p4_exo = column_clean(df_p4_exo, run_num = 2, gender = 'female')
df_p4_noexo = column_clean(df_p4_noexo, run_num = 1, gender = 'female')
combined_df = pd.concat([df_p3_exo, df_p3_noexo, df_p4_exo, df_p4_noexo], ignore_index=True)
dfs = [df_p3_exo, df_p3_noexo, df_p4_exo, df_p4_noexo] #jack's list for the data cleaning he does later.
# # Show the head of the data
# df_p3_exo.describe()
df_p3_noexo.head()
# df_p4_exo.head()
# df_p4_noexo.head()
# # Choose inputs
# features = df_p3_exo[['EMG 1 (mV)', 'ACC X (G)', 'ACC Y (G)', 'ACC Z (G)', 'GYRO X (deg/s)', 'GYRO Y (deg/s)', 'GYRO Z (deg/s)']].dropna()
# features.head()
feature_sets = []
# Run functions to extract features for each dataframe
#CP: does this make sure to remove the redundant time series columns?
#can keep ACC X Time Series (s) in each sensor group, and remove any other column with 'Time Series (s)' in its name
for df in dfs:
emg_features = compute_emg_features(df['EMG 1 (mV)'])
accel_features = compute_accel_features(df['ACC X (G)'], df['ACC Y (G)'], df['ACC Z (G)'])
gyro_features = compute_gyro_features(df['GYRO X (deg/s)'], df['GYRO Y (deg/s)'], df['GYRO Z (deg/s)'])
features = {
'emg': emg_features,
'accel': accel_features,
'gyro': gyro_features
}
feature_sets.append(features)
# feature_sets now contains extracted features for each df
p3exo_feats, p3noexo_feats, p4exo_feats, p4noexo_feats = feature_sets
return p3exo_feats, p3noexo_feats, p4exo_feats, p4noexo_feats
# Calculations for Feature Extraction from Project_Guide
def compute_emg_features(signal):
return {
'mean': np.mean(signal),
'max': np.max(signal),
'min': np.min(signal),
'std': np.std(signal),
'rms': np.sqrt(np.mean(signal**2))
}
def compute_accel_features(a_x, a_y, a_z):
a_mag = np.sqrt(a_x**2 + a_y**2 + a_z**2)
features = {
'peak_accel': np.max(a_mag),
'mean_accel': np.mean(a_mag),
'total_accel': np.sqrt(np.mean(a_x**2) + np.mean(a_y**2) + np.mean(a_z**2)),
'accel_range': np.max(a_mag) - np.min(a_mag)
}
return features
def compute_gyro_features(w_x, w_y, w_z):
w_mag = np.sqrt(w_x**2 + w_y**2 + w_z**2)
features = {
'peak_angular_vel': np.max(w_mag),
'mean_angular_vel': np.mean(w_mag),
'total_angular_vel': np.sqrt(np.mean(w_x**2) + np.mean(w_y**2) + np.mean(w_z**2)),
'angular_vel_range': np.max(w_mag) - np.min(w_mag)
}
return features
if __name__ == '__main__':
p3exo_feats, p3noexo_feats, p4exo_feats, p4noexo_feats = overall_cleaning()