-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_ExtractFeatures_Functions.py
262 lines (215 loc) · 10 KB
/
s3_ExtractFeatures_Functions.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
This script was created at 09-Dec-21
author: eachrist
"""
# ============ #
# Imports #
# ============= #
import os
import ast
import numpy as np
import pandas as pd
from tqdm import tqdm
from math import sqrt
from statistics import mean
from scipy.fftpack import fft
from scipy.stats import entropy, kurtosis, skew
from s__Helpers_Functions import linear_regression
from s0_cases_dictionaries import dict_cases
from s3_ExtractFeatures_Classes import FeaturesSns, FeaturesGes
# =============== #
# Functions #
# =============== #
def calculate_features_sns(user: int, module: str, data: np.ndarray, group: int, timestamp: float,
features_object: FeaturesSns, window: int, overlap: float, sample_rate: float):
overlap = int(overlap * window)
data_length = data.shape[0]
if data_length <= 1:
return features_object
if data_length < window:
window = data_length
flag = True
start = 0
start_time = timestamp
while data_length > 0:
stop = start + window
stop_time = timestamp + (stop - 1) * sample_rate
features_object.setUser(user)
features_object.setModule(module)
features_object.setStartTime(start_time)
features_object.setStopTime(stop_time)
features_object.setGroup(group)
features_object.setWindow(stop-start)
# Time features
window_data = data[start:stop]
features_object.setMean(window_data.mean())
features_object.setSTD(window_data.std())
features_object.setMax(window_data.max())
features_object.setMin(window_data.min())
features_object.setRange(window_data.ptp())
features_object.setPercentile25(np.percentile(window_data, 25))
features_object.setPercentile50(np.percentile(window_data, 50))
features_object.setPercentile75(np.percentile(window_data, 75))
features_object.setEntropy(entropy(window_data, base=2))
features_object.setKurtosis(kurtosis(window_data))
features_object.setSkewness(skew(window_data))
# Frequency features
window_dft = fft(window_data)
freq = np.fft.fftfreq(stop - start)
idx = (np.absolute(window_dft)).argsort()[-2:][::-1]
features_object.setAmplitude1(np.absolute(window_dft[idx[0]]))
features_object.setAmplitude2(np.absolute(window_dft[idx[1]]))
features_object.setFrequency2(freq[idx[1]])
features_object.setMeanFrequency(np.mean(freq))
if flag:
data_length += start - stop
flag = False
else:
data_length += start - stop + overlap
# Dynamic overlap
if data_length + overlap < window:
if data_length < overlap:
break
overlap = window - data_length
start = stop - overlap
start_time = timestamp + start * sample_rate
return features_object
def extract_features_sns(case: str, screen_path: str, data: pd.DataFrame, module: str):
print(' - Extracting ' + module + ' features.')
path_df = os.path.join(screen_path, 'ftr_' + module[0:3] + '.csv')
if not os.path.exists(path_df):
features = dict_cases[case]['FeatureExtraction']['sns']['lvl0_ftr'][module[0:3]]
sample_rate = dict_cases[case]['FeatureExtraction']['sns']['sample_rate']
window = dict_cases[case]['FeatureExtraction']['sns']['window'][module[0:3]]
overlap = dict_cases[case]['FeatureExtraction']['sns']['overlap'][module[0:3]]
features_objects = {}
for feature in features:
features_objects[feature] = FeaturesSns()
for user in tqdm(list(set(data['user']))):
user_data = data.loc[data['user'] == user]
timestamps = list(set(user_data['timestamp']))
timestamps.sort()
for idx, ts in enumerate(timestamps):
for feature in features:
data_to_window = user_data.loc[data['timestamp'] == ts][feature].to_numpy()
features_objects[feature] = calculate_features_sns(user, module[0:3], data_to_window, idx, ts,
features_objects[feature],
window, overlap, sample_rate)
df_features = features_objects[features[0]].create_dataframe(features[0], True)
if len(features) > 1:
for feature in features[1:]:
df_temp = features_objects[feature].create_dataframe(feature, False)
df_features = pd.concat([df_features, df_temp], axis=1)
df_features.to_csv(path_df, index=False)
print(' ' + module + ' features dataframe saved at: ', path_df)
else:
df_features = pd.read_csv(path_df)
print(' ' + module + ' features dataframe loaded from: ', path_df)
print('')
return df_features
def calculate_features_ges(gesture: pd.Series, features_object: FeaturesGes,
normalize: bool, default_width: float, default_height: float):
features_object.setUser(gesture['user'])
features_object.setStartTime(gesture['time_start'])
features_object.setStopTime(gesture['time_stop'])
features_object.setDuration(gesture['duration'])
if gesture['type'] == 'swipe':
features_object.setModule('swp')
if type(gesture['data']) == str:
ges_data = ast.literal_eval(gesture['data'])
else:
ges_data = gesture['data']
x_poss, y_poss = [ges_data[0]['x0']], [ges_data[0]['y0']]
for data in ges_data:
x_poss.append(data['moveX'])
y_poss.append(data['moveY'])
if normalize:
x_poss = [x * default_width / gesture['device_width'] for x in x_poss]
y_poss = [y * default_height / gesture['device_height'] for y in y_poss]
features_object.setMeanX(mean(x_poss))
features_object.setMeanY(mean(y_poss))
trace_length = 0
for idx in range(1, len(x_poss)):
dx = x_poss[idx] - x_poss[idx - 1]
dy = y_poss[idx] - y_poss[idx - 1]
trace_length += sqrt(dx ** 2 + dy ** 2)
features_object.setTraceLength(trace_length)
dx = abs(x_poss[-1] - x_poss[0])
dy = abs(y_poss[-1] - y_poss[0])
start_stop_length = sqrt(dx ** 2 + dy ** 2)
features_object.setStartStopLength(start_stop_length)
temp_dict = {
'hor': {
'trace_projection': dx,
'points': {},
'norm': {
True: default_width,
False: gesture['device_width']
}
},
'ver': {
'trace_projection': dy,
'points': {},
'norm': {
True: default_height,
False: gesture['device_height']
}
}
}
direction = 'hor' if dx >= dy else 'ver'
trace_projection = temp_dict[direction]['trace_projection']
features_object.setTraceProjection(trace_projection)
features_object.setScreenPercentage(trace_projection / temp_dict[direction]['norm'][normalize])
start_velocity = sqrt(ges_data[0]['vx'] ** 2 + ges_data[0]['vy'] ** 2)
features_object.setStartVelocity(start_velocity)
stop_velocity = sqrt(ges_data[-1]['vx'] ** 2 + ges_data[-1]['vy'] ** 2)
features_object.setStopVelocity(stop_velocity)
acceleration_hor = (ges_data[-1]['vx'] - ges_data[0]['vx']) / (gesture['duration'] * 0.001)
features_object.setAccelerationHor(acceleration_hor)
acceleration_ver = (ges_data[-1]['vy'] - ges_data[0]['vy']) / (gesture['duration'] * 0.001)
features_object.setAccelerationVer(acceleration_ver)
trace_stats = linear_regression(x_poss, y_poss)
features_object.setSlope(trace_stats['slope'])
features_object.setMeanSquareError(trace_stats['mean_squared_error'])
features_object.setMeanAbsError(trace_stats['mean_abs_error'])
features_object.setMedianAbsError(trace_stats['median_abs_error'])
features_object.setCoefDetermination(trace_stats['coef_determination'])
else:
features_object.setModule('tap')
features_object.setMeanX()
features_object.setMeanY()
features_object.setTraceLength()
features_object.setStartStopLength()
features_object.setScreenPercentage()
features_object.setTraceProjection()
features_object.setStartVelocity()
features_object.setStopVelocity()
features_object.setAccelerationHor()
features_object.setAccelerationVer()
features_object.setSlope()
features_object.setMeanSquareError()
features_object.setMeanAbsError()
features_object.setMedianAbsError()
features_object.setCoefDetermination()
return features_object
def extract_features_ges(case: str, screen_path: str, data: pd.DataFrame):
print(' - Extracting gestures features.')
path_df = os.path.join(screen_path, 'ftr_ges.csv')
if not os.path.exists(path_df):
normalize = dict_cases[case]['FeatureExtraction']['ges']['normalize']
default_width = dict_cases[case]['FeatureExtraction']['ges']['default_width']
default_height = dict_cases[case]['FeatureExtraction']['ges']['default_height']
features_object = FeaturesGes()
for user in tqdm(list(set(data['user']))):
user_data = data.loc[data['user'] == user]
user_data = user_data.sort_values(by=['time_start'], ignore_index=True)
for index, ges in user_data.iterrows():
features_object = calculate_features_ges(ges, features_object, normalize, default_width, default_height)
df_features = features_object.create_dataframe()
df_features.to_csv(path_df, index=False)
print(' gestures features dataframe saved at: ', path_df)
else:
df_features = pd.read_csv(path_df)
print(' gestures features dataframe loaded from: ', path_df)
print('')
return df_features