-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChairProcessing.py
260 lines (217 loc) · 9.33 KB
/
ChairProcessing.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import joblib
from utils import normalize_MPU9250_data, split_df, get_intervals_from_moments, EventIntervals
import ChairAnalyser
from ChairAnalyser import ChairAnalyser
from GeneralAnalyser import plot_measurements, plot_measurements_pairwise\
# from GeneralAnalyser import plot_measurements_iop
import itertools
from sklearn.preprocessing import StandardScaler
import argparse
# plt.interactive(True)
pd.options.display.max_columns = 15
pic_prefix = 'pic/'
import sys
print(sys.argv)
del sys.argv[1:]
parser = argparse.ArgumentParser()
parser.add_argument('--interval', default=1, type=float)
parser.add_argument('--shift', default=1, type=int) # -1 --- before, 0 - at the moment, 1 - after
parser.add_argument('--visualisation', default=False, type=int)
parser.add_argument('--verbose', default=True, type=int)
parser.add_argument('--std_mode', default=True, type=int)
parser.add_argument('--iop', default=False, type=int)
parser.add_argument('--reaction_multiplier', default=5, type=float)
# parser.add_argument('--max_sessions_per_player', default=3, type=int)
args = parser.parse_args()
interval = args.interval
shift = args.shift
visualisation = args.visualisation
verbose = args.verbose
std_mode = args.std_mode
reaction_multiplier = args.reaction_multiplier
iop = args.iop
# if iop:
# plot_measurements = plot_measurements_iop
interval_start = interval * (shift / 2)
interval_end = interval * (shift / 2 + 1)
# TODO: extract "online" features
sessions_dict = joblib.load('data/sessions_dict')
gamedata_dict = joblib.load('data/gamedata_dict')
# def get_chair_features(df_chair, session_id):
# chair_analyser = ChairAnalyser(
# df=df_chair,
# pic_prefix=pic_prefix,
# sensor_name='chair',
# session_id=session_id,
# # measurement_interval=0.01,
# )
# nonstationary_values_portion = chair_analyser.get_nonstationary_values_portion()
# lean_back_portion = chair_analyser.get_lean_back_portion()
# oscillations = chair_analyser.get_oscillation_intensity()
#
# chair_features = pd.concat([nonstationary_values_portion, lean_back_portion, oscillations])
# chair_features.name = session_id
#
# return chair_features
chair_features_list = []
# ##### Testing zone
#
# session_id = 15
# df_chair = sessions_dict[session_id]['schairlog']
# get_chair_features(df_chair, session_id)
# # df_chair['time'] = pd.to_datetime(df_chair['time']).apply(lambda x: x.timestamp())
#
# chair_analyser = ChairAnalyser(df_chair, pic_prefix=pic_prefix, measurement_interval=0.01, name=session_id)
#
# shootout_times_start_end = gamedata_dict[session_id]['shootout_times_start_end']
#
# shootouts_dict = {
# 'label': 'shootouts',
# 'intervals_list': shootout_times_start_end,
# }
#
# mask_dicts_list = [shootouts_dict]
#
# chair_analyser.plot_measurements_timeline(sensors=['acc', 'gyro'], mask_dicts_list=mask_dicts_list)
#
#
#
# get_chair_features(df_chair, session_id)
#
# End Testing zone
# #####
### Visualisation params
# if visualisation:
sensors_columns_dict = {
# 'schairlog': ['acc_x', 'acc_y', 'acc_z', 'gyro_x', 'gyro_y', 'gyro_z'],
'schairlog': ['acc_x', 'gyro_x', 'acc_y', 'gyro_y', 'acc_z', 'gyro_z'],
}
print(std_mode)
if std_mode:
# sensors_columns_dict['schairlog'] = [value + f'_std_{int(interval * 1000)}ms' for value in sensors_columns_dict['schairlog']]
sensors_columns_dict['schairlog'] = [value + f'_std' for value in sensors_columns_dict['schairlog']]
sensors_list = list(sensors_columns_dict.keys())
# sensors_columns_list = []
# for sensor in sensors_columns_dict:
# for column in sensors_columns_dict[sensor]:
# sensors_columns_list.append([sensor, column])
# session_id = 15
# session_data_dict = sessions_dict[session_id]
#
# import importlib
# del ChairAnalyser
# import ChairAnalyser
# importlib.reload(ChairAnalyser)
# from ChairAnalyser import ChairAnalyser
for session_id, session_data_dict in sessions_dict.items():
# df_dict = {}
if not set(sensors_list).issubset(set(session_data_dict.keys())):
continue
if verbose:
print(f'processing session_id {session_id}')
if session_id in gamedata_dict:
moments_kills = gamedata_dict[session_id]['times_kills']
moments_death = gamedata_dict[session_id]['times_is_killed']
# duration = 1
print('Calculating intervals')
intervals_shootout = gamedata_dict[session_id]['shootout_times_start_end']
# # intervals_kills = get_intervals_from_moments(moments_kills, interval_start=-duration, interval_end=duration)
# # intervals_death = get_intervals_from_moments(moments_death, interval_start=-duration, interval_end=duration)
# intervals_kills = get_intervals_from_moments(moments_kills, interval_start=0, interval_end=2*duration)
# intervals_death = get_intervals_from_moments(moments_death, interval_start=0, interval_end=2*duration)
intervals_kills = get_intervals_from_moments(moments_kills, interval_start=interval_start, interval_end=interval_end)
intervals_death = get_intervals_from_moments(moments_death, interval_start=interval_start, interval_end=interval_end)
event_intervals_shootout = EventIntervals(intervals_list=intervals_shootout, label='shootout', color='blue')
event_intervals_kills = EventIntervals(intervals_list=intervals_kills, label='kill', color='green')
event_intervals_death = EventIntervals(intervals_list=intervals_death, label='death', color='red')
events_intervals_list = [event_intervals_shootout, event_intervals_death, event_intervals_kills]
else:
events_intervals_list = None
print('Extracting features')
sensor_name = 'schairlog'
for sensor_name in sensors_columns_dict:
df = session_data_dict[sensor_name].copy()
if sensor_name == 'schairlog':
chair_analyser = ChairAnalyser(
df,
pic_prefix=pic_prefix,
sensor_name='Chair', # Manual assignment
session_id=session_id,
events_intervals_list=events_intervals_list,
interval=interval,
reaction_multiplier=reaction_multiplier,
)
chair_features = chair_analyser.get_features()
# chair_features = get_chair_features(df, session_id) # TMP
chair_features_list.append(chair_features)
if (not visualisation) or (events_intervals_list is None):
continue
analyser_column_pairs_list = []
for sensor_name in sensors_columns_dict:
df = session_data_dict[sensor_name].copy()
# if sensor_name == 'schairlog':
# chair_features = get_chair_features(df, session_id) # TMP
# chair_features_list.append(chair_features)
# ss = StandardScaler()
# # df.values = ss.fit_transform(df.values)
# df.loc[:, sensors_columns_dict[sensor_name]] = ss.fit_transform(df.loc[:, sensors_columns_dict[sensor_name]])
### WARNING: it is CUSTOM PART
if sensor_name == 'schairlog':
chair_analyser = ChairAnalyser(
df,
pic_prefix=pic_prefix,
sensor_name='Chair', # Manual assignment
session_id=session_id,
events_intervals_list=events_intervals_list,
interval=interval,
reaction_multiplier=reaction_multiplier,
)
# chair_analyser.get_floating_features() # Need to be refactored
chair_analyser._append_floating_features(interval=interval)
for column in sensors_columns_dict[sensor_name]:
analyser_column_pairs_list.append([chair_analyser, column])
# print(chair_analyser.df.columns)
plot_measurements(
analyser_column_pairs_list=analyser_column_pairs_list,
pic_prefix=pic_prefix,
session_id=session_id,
event_intervals_list=events_intervals_list,
figsize=(30, 20),
plot_suptitle=True,
alpha=1,
alpha_background=0.4,
sharex=True,
fontsize=30,
)
### CODE BELOW IS PROBABLY OK AND USEFUL FOR PAIRWISE PLOTS
# columns = ['acc_x', 'acc_y', 'acc_z', 'gyro_x', 'gyro_y', 'gyro_z']
# # columns = ['gyro_x', 'gyro_y', 'gyro_z'] # Gyro's plots are much more interesting. Others are almost 1-dimensional
# pairwise_combinations = itertools.combinations(columns, 2)
# analyser_column_pairs_pairs_list = []
#
# for pairwise_combination in pairwise_combinations:
# col_1, col_2 = pairwise_combination
# analyser_column_pairs_pairs_list.append([[chair_analyser, col_1], [chair_analyser, col_2]])
#
# plot_measurements_pairwise(
# analyser_column_pairs_pairs_list=analyser_column_pairs_pairs_list, # TODO: data should be normalized to explore acc measurements
# pic_prefix=pic_prefix,
# session_id=session_id,
# event_intervals_list=events_intervals_list,
# # n_rows=1,
# # n_cols=1,
# figsize=(30, 20),
# plot_suptitle=True,
# alpha=0.1,
# alpha_background=0.05,
# point_size=0.5,
# sharex='none',
# )
df_chair_features = pd.DataFrame(chair_features_list)
df_chair_features.reset_index(inplace=True)
df_chair_features.rename(columns={'index': 'session_id'}, inplace=True)
df_chair_features.to_csv('data/chair_features.csv', index=False)