-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_generation.py
278 lines (228 loc) · 8.81 KB
/
feature_generation.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import warnings
from typing import Iterable
import pandas as pd
import numpy as np
import glob
from joblib import Parallel, delayed
from tqdm import tqdm, trange
from datetime import timedelta
import sys
from custom_feature_preprocessing import CUSTOM_CAN, CUSTOM_PHASES, CUSTOM_INTERVENTION
from remove_intervention import distance, remove_intervention
from aggregation_functions import FUNCTIONS
COLUMN_RENAMES = {
"VehicleSpeed": "velocity",
"SteeringWheelAngle": "steer",
"SteeringWheelAngularVelocity": "steer_vel",
"BrakingPressure": "brake",
"PedalForce": "gas",
"LongitudinalAcceleration": "acc",
"LateralAcceleration": "latacc",
}
def interpolate_timestamps(df):
"""
Interpolate blood glucose values with same frequency as CAN data was recorded (50Hz).
"""
interpolate_columns = ["bg_biosen", "bg_contour"]
resampler = df.resample("0.02S")
interpolated = (
resampler.interpolate(method="time")[interpolate_columns]
.add_suffix("_interpolate")
.round(2)
)
return pd.concat([interpolated], axis=1)
def preprocess_bg_data(df):
"""
Changes index to timestamp index,
drops all columns except bg_biosen, bg_contour,
drops bg values where valid == False.
"""
subject = df["subject_id"].iloc[0]
df.index = pd.to_datetime(df["timestamp"])
df.index = df.index.tz_convert("Europe/Zurich")
df = df[["bg_biosen", "bg_contour", "cgm", "valid"]]
df = df.where(df["valid"].fillna(True)).drop("valid", axis=1).dropna(how="all")
if df["cgm"].isna().all():
print(f"No CGM values for subject {subject}, setting to -1")
df["cgm"] = -1.0
return df
def get_can_features(subject, data: pd.DataFrame, features, bg, epoch_width: int = 60):
input_data = data.copy()
inputs = trange(0, len(input_data) - 1, 50, desc=str(subject))
results = Parallel(n_jobs=1)(
delayed(__get_can_stats)(
input_data, features=features, bg=bg, epoch_width=epoch_width, i=k
)
for k in inputs
)
results = pd.DataFrame(list(filter(None, results)))
results.set_index("datetime", inplace=True)
results.sort_index(inplace=True)
return results
def get_stats(data, key_prefix: str = None):
data_nans = data.isna().sum()
if data_nans > 0:
warnings.warn(f"input data contains {data_nans} NaNs which will be removed")
data = data.dropna()
data = np.asanyarray(data)
results = {}
try:
if len(data) > 0:
for key, value in FUNCTIONS.items():
results[key] = value(data)
else:
for key in FUNCTIONS.keys():
results[key] = np.nan
except Exception as e:
print(e)
if key_prefix is not None:
results = {key_prefix + "_" + k: v for k, v in results.items()}
return results
def __get_can_stats(data: pd.DataFrame, features, bg, epoch_width: int, i: int):
epoch_width = timedelta(seconds=epoch_width)
requirement_1 = data.index[i + 1] - data.index[i] <= epoch_width
requirement_2 = data.index[-1] - data.index[i] >= epoch_width
if requirement_1 and requirement_2:
min_timestamp = data.index[i]
max_timestamp = min_timestamp + epoch_width
results = {"datetime": max_timestamp}
relevant_data = data.loc[
(data.index >= min_timestamp) & (data.index < max_timestamp)
]
for column in features:
column_results = get_stats(relevant_data[column], f"can_{column}")
results.update(column_results)
# BG: get values at end of window
if len(bg) > 0:
results.update(relevant_data.iloc[-1][bg].to_dict())
return results
else:
return None
def load_subject(
subject: int,
data_folder: str,
window_size_sec: int,
remove_intervention_phase: bool = True,
):
folder = glob.glob(f"{data_folder}/202*_{subject}/")[0]
study_part = 1 if subject < 300 else 2
can_data = pd.read_parquet(folder + "output/canlogger/can-aggregated.parquet")
can_data[list(COLUMN_RENAMES.keys())] = can_data[list(COLUMN_RENAMES.keys())].apply(
lambda x: pd.to_numeric(x, errors="raise", downcast="float")
) # make sure we have floats for the needed signals
phase_data = pd.read_csv(folder + "output/driving/phases.csv", parse_dates=[0, 1])
if study_part == 1: # study 1
gps_data = pd.read_parquet(folder + "output/candump/gps.parquet")
if subject in CUSTOM_PHASES.keys():
phase_data = CUSTOM_PHASES[subject](phase_data)
print("Preprocessed phase data for subject", subject)
elif subject in CUSTOM_CAN.keys():
can_data = CUSTOM_CAN[subject](folder, can_data, phase_data)
print("Preprocessed CAN data for subject", subject)
# else:
# print('No preprocessing necessary for subject', subject)
if remove_intervention_phase:
if subject in CUSTOM_INTERVENTION.keys():
phase_data = CUSTOM_INTERVENTION[subject](
gps_data, phase_data, int(subject)
)
else:
phase_data = remove_intervention(gps_data, phase_data, int(subject))
can_data.rename(columns=COLUMN_RENAMES, inplace=True)
data = can_data.loc[:, list(COLUMN_RENAMES.values())]
data["gas_vel"] = data["gas"].diff().fillna(0)
data["brake_vel"] = data["brake"].diff().fillna(0)
data["steer_vel"] = data["steer_vel"].astype(float)
data["steer_vel_abs"] = data["steer_vel"].abs()
data["steer_abs"] = data["steer"].abs()
data["acc_abs"] = data["acc"].abs()
data[data.columns.difference(["phase", "scenario"])] = data[
data.columns.difference(["phase", "scenario"])
].astype("float32")
# inteprolate and add bg_data
if os.path.exists(folder + "bg/bg.csv"):
bg_data = pd.read_csv(folder + "bg/bg.csv")
bg_data = preprocess_bg_data(bg_data)
interpolation = "ffill" # 'ffill' or 'time'
assert interpolation in ["ffill", "time"]
bg_data = (
bg_data.asfreq("0.02S")
.interpolate(method=interpolation)
.interpolate(method="bfill")
.astype("float32")
) # bfill for rows in beginning
data = pd.merge_asof(
data,
bg_data,
direction="nearest",
tolerance=timedelta(seconds=window_size_sec),
left_index=True,
right_index=True,
)
else:
raise ("No BG data for subject ", subject)
# bg_data = pd.DataFrame()
data["phase"] = 0
data["scenario"] = None
end = "end" if study_part == 1 else "last_start_pass"
for idx, phase in phase_data.iterrows():
data.loc[
((data.index > phase["start"]) & (data.index <= phase[end])),
["phase", "scenario"],
] = [phase["phase"], phase["scenario"]]
data.dropna(how="any", inplace=True)
features = []
for phase in [1, 2, 3, 4]:
relevant_data = data[data["phase"] == phase]
for scenario in relevant_data["scenario"].unique():
input_data = relevant_data[relevant_data["scenario"] == scenario].drop(
columns=["phase", "scenario"]
)
if len(input_data) == 0:
print(
"No data available for subject",
subject,
"in phase",
phase,
"and scenario",
scenario,
)
continue
df = get_can_features(
subject,
input_data,
features=data.columns.difference(
["phase", "scenario"] + bg_data.columns.to_list()
),
bg=bg_data.columns,
epoch_width=window_size_sec,
)
df["phase"] = phase
df["scenario"] = scenario
df["subject_id"] = subject
features.append(df)
features = pd.concat(features)
features["scenario"] = features["scenario"].str.title()
features.index = features.index.floor(freq="s")
return features
def generate_features(
data_folder: str,
subjects: list,
window_size_sec: int,
remove_intervention_phase: bool = True,
):
"""
Generates features with aggregation functions from aggregation_functions.py using
a sliding window approach with length window_size and a shift of 1s.
"""
print(f"Generating features for {len(subjects)} subjects: {sorted(subjects)}")
with Parallel(n_jobs=min(32, len(subjects))) as parallel:
features = parallel(
delayed(load_subject)(
subject, data_folder, window_size_sec, remove_intervention_phase
)
for subject in subjects
)
features = pd.concat(features)
return features