-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_feature_preprocessing.py
236 lines (181 loc) · 7.36 KB
/
custom_feature_preprocessing.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
from remove_intervention import distance, remove_intervention
def can_202(
folder: str, can_log_data: pd.DataFrame, phase_data: pd.DataFrame
) -> pd.DataFrame:
"""
CAN log failed to record the first 2min of phase 3, scenario highway;
retrieve data from CAN dump.
"""
can_log_data_filled = can_log_data.copy()
can_dump_data = pd.read_parquet(f"{folder}output/candump/driving.parquet")
phase_data["start"] = pd.to_datetime(phase_data["start"]).apply(
lambda row: row.tz_convert("Europe/Zurich")
)
phase_data["end"] = pd.to_datetime(phase_data["end"]).apply(
lambda row: row.tz_convert("Europe/Zurich")
)
phase_start = phase_data[
(phase_data["phase"] == 3) & (phase_data["scenario"] == "highway")
].start.iloc[0]
phase_end = phase_data[
(phase_data["phase"] == 3) & (phase_data["scenario"] == "highway")
].end.iloc[0]
relevant_data = can_dump_data.loc[phase_start:phase_end]
relevant_data = relevant_data.resample("0.02S").ffill().dropna()
# PedalForce in candump is in % (0-100) but we need it between 0-1 here to be aligned with the canlogger
relevant_data["PedalForce"] = relevant_data["PedalForce"] / 100
can_log_data_filled.loc[phase_start:phase_end, :] = relevant_data
return can_log_data_filled
def can_211(
folder: str, can_log_data: pd.DataFrame, phase_data: pd.DataFrame
) -> pd.DataFrame:
"""
CAN log failed to record the VehicleSpeed for 18s in phase 2, scenario highway and rural;
exact time: (09:54:09–09:54:27) and (10:25:47–10:26:05);
retrieve data from CAN dump.
"""
can_log_data_filled = can_log_data.copy()
can_dump_data = pd.read_parquet(f"{folder}/output/candump/driving.parquet")
phase_start = phase_data[
(phase_data["phase"] == 1) & (phase_data["scenario"] == "highway")
].start.iloc[0]
phase_end = phase_data[
(phase_data["phase"] == 2) & (phase_data["scenario"] == "rural")
].end.iloc[0]
relevant_data = can_dump_data.loc[phase_start:phase_end, "VehicleSpeed"]
relevant_data = relevant_data.resample("0.02S").ffill().dropna()
can_log_data_filled.loc[phase_start:phase_end, "VehicleSpeed"] = relevant_data
return can_log_data_filled
def can_214(
folder: str, can_log_data: pd.DataFrame, phase_data: pd.DataFrame
) -> pd.DataFrame:
"""
CAN log failed to record phase 1, scenario city;
retrieve data from CAN dump.
"""
can_log_data_filled = can_log_data.copy()
can_dump_data = pd.read_parquet(f"{folder}output/candump/driving.parquet")
phase_start = phase_data[
(phase_data["phase"] == 1) & (phase_data["scenario"] == "city")
].start.iloc[0]
phase_end = phase_data[
(phase_data["phase"] == 1) & (phase_data["scenario"] == "city")
].end.iloc[0]
relevant_data = can_dump_data.loc[phase_start:phase_end]
relevant_data = relevant_data.resample("0.02S").ffill().dropna()
# PedalForce in candump is in % (0-100) but we need it between 0-1 here to be aligned with the canlogger
relevant_data["PedalForce"] = relevant_data["PedalForce"] / 100
can_log_data_filled.loc[phase_start:phase_end, :] = relevant_data
return can_log_data_filled
def phase_204(phase_data: pd.DataFrame) -> pd.DataFrame:
"""
Neither CAN dump nor CAN logger did work during the first 90s of phase 1, scenario rural;
hence we cut this part out.
"""
start_loc = phase_data.columns.get_loc("start")
index_loc = phase_data[
(phase_data["phase"] == 1) & (phase_data["scenario"] == "rural")
].index.item()
phase_data.iloc[index_loc, start_loc] = pd.to_datetime(
"2020-10-22 10:33:14.340000+0200"
)
return phase_data
def phase_219(phase_data: pd.DataFrame) -> pd.DataFrame:
"""
Phase 1.1: U-turn directly before starting, need to remove from phase (true start: 10:03:56)
"""
phase_data.loc[1, "start"] = pd.to_datetime("2021-04-29 10:03:54.780+0200")
return phase_data
def phase_220(phase_data: pd.DataFrame) -> pd.DataFrame:
"""
Neither CAN dump nor CAN logger did work during the last 180s of phase 3, scenario rural;
hence we cut this part out.
"""
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 3) & (phase_data["scenario"] == "rural")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2021-04-30 11:25:00.040000+0200"
)
return phase_data
def phase_222(phase_data: pd.DataFrame) -> pd.DataFrame:
"""
Participant 222 took the wrong turn, therefore we cut the driving data off at this point.
"""
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 2) & (phase_data["scenario"] == "city")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2021-05-18 10:52:06.000000+0200"
)
return phase_data
def intervention_201(gps_data, phase_data, subject):
"""
GPS data of phase 2 missing, set new end point of phase 2 city (intervention scenario) manually.
"""
phase_data = remove_intervention(gps_data, phase_data, subject)
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 2) & (phase_data["scenario"] == "city")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2020-10-09 11:58:12.000000+0200"
)
return phase_data
def intervention_204(gps_data, phase_data, subject):
"""
GPS data of phase 1 missing, set new end point of phase 1 highway (intervention scenario) manually.
"""
phase_data = remove_intervention(gps_data, phase_data, subject)
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 1) & (phase_data["scenario"] == "highway")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2020-10-22 10:50:05.000000+0200"
)
return phase_data
def intervention_205(gps_data, phase_data, subject):
"""
GPS data of phase 4 missing, set new end point of phase 4 highway (intervention scenario) manually.
"""
phase_data = remove_intervention(gps_data, phase_data, subject)
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 4) & (phase_data["scenario"] == "highway")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2020-10-23 13:24:45.000000+0200"
)
return phase_data
def intervention_224(gps_data, phase_data, subject):
"""
GPS data of phase 2 missing, set new end point of phase 2 highway (intervention scenario) manually.
"""
phase_data = remove_intervention(gps_data, phase_data, subject)
end_loc = phase_data.columns.get_loc("end")
index_loc = phase_data[
(phase_data["phase"] == 2) & (phase_data["scenario"] == "highway")
].index.item()
phase_data.iloc[index_loc, end_loc] = pd.to_datetime(
"2021-05-21 11:03:27.000000+0200"
)
return phase_data
CUSTOM_CAN = {
202: can_202,
211: can_211,
214: can_214,
}
CUSTOM_PHASES = {204: phase_204, 219: phase_219, 220: phase_220, 222: phase_222}
CUSTOM_INTERVENTION = {
201: intervention_201,
204: intervention_204,
205: intervention_205,
224: intervention_224,
}