-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processing.R
More file actions
349 lines (316 loc) · 9.46 KB
/
Copy pathdata_processing.R
File metadata and controls
349 lines (316 loc) · 9.46 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
library(tidyverse)
library(magrittr)
library(readxl)
library(saccades)
library(arrow)
library(data.table)
library(this.path)
library(qs2)
library(glue)
# library(runner)
### Read in DMS data
# Load in data
setwd(this.dir())
non_aug <- read_feather("./data/non_aug_data.feather")
# Replace repeated button-press observations with zeros
non_aug %<>%
group_by(rleid(Task_Available)) %>%
mutate(
# Correct button press is first frame pressed within task window
Correct_Left_Button_Press = case_when(
Left_Button_Press == 1 &
!duplicated(Left_Button_Press) &
Task_Available %in% c("Left", "Right") ~ 1,
TRUE ~ 0
),
# Left button press is any press that is not holding an ongoing press
Left_Button_Press = case_when(
(Left_Button_Press == lag(Left_Button_Press)) &
(Left_Button_Press == 1) |
(Left_Button_Press == 2) ~ 0,
TRUE ~ Left_Button_Press
)
) %>%
ungroup()
non_aug$`rleid(Task_Available)` <- NULL
# Replace coded 99 values from python script with NA values
non_aug[non_aug == 99] <- NA
# Use 1-base indexing for X and Frame_Num (match row number)
non_aug %<>%
mutate(X = 1:nrow(non_aug), .before = 1) %>%
group_by(DaqName) %>%
mutate(Frame_Num = row_number(), .after = X) %>%
ungroup()
# Add average pupil size of both eyes
non_aug$Avg_Pupil_Diameter <- (non_aug$RPupil_Diameter +
non_aug$LPupil_Diameter) / 2
# Rolling SD of vehicle lateral deviation
# non_aug %<>%
# group_by(DaqName) %>%
# mutate(Vehicle_Lat_Dev_rolling_sd3 = runner(
# Vehicle_Lat_Dev, sd,
# k = 180, na_pad = FALSE, na.rm = TRUE
# )) %>%
# ungroup()
# Get `ST_*` columns
output <- read_excel("./data/output_IIHS_withDMS.xls") %>%
rename("ST_Right_N" = "ST_Rights_N") %>%
select(matches("ST_(Right|Left).*"), DaqName, Subject, Drive) %>%
relocate(DaqName, .after = Drive) %>%
drop_na()
output$DaqName <- output$DaqName %>% str_sub(0, -5)
# Add counted hits from data set
output <- output %>% left_join(
non_aug %>%
filter(Task_Available %in% c("Left", "Right")) %>%
group_by(DaqName, Task_Available) %>%
summarize(
Counted_Hits = sum(Correct_Left_Button_Press),
.groups = "drop"
) %>%
pivot_wider(
names_from = Task_Available,
values_from = Counted_Hits,
names_prefix = "Counted_Hits_",
values_fill = 0
),
by = "DaqName"
)
### Add and modify predictors
# Fully blinking
non_aug %<>% mutate(Blinking = Left_Eyelid_Closed & Right_Eyelid_Closed)
# Eyes 80% or more closed
non_aug %<>%
group_by(Subject) %>%
mutate(
Eyes_Closed_80pct = (
Eye_Opening <
0.2 * max(
Eye_Opening,
na.rm = TRUE
)
)
)
# Get saccades, fixations, and blinks
non_saccades <- detect.fixations(
non_aug %>%
select(
DaqName,
Gaze_Yaw,
Gaze_Pitch,
X
) %>%
rename(
trial = DaqName,
x = Gaze_Yaw,
y = Gaze_Pitch,
time = X
),
lambda = 15,
smooth.coordinates = TRUE
)
# Add column to `non_aug` for eye event type
non_aug$Eye_Event <- factor(
"saccade",
levels = c("saccade", "blink", "fixation", "too short")
)
indices <- mapply(seq, non_saccades$start, non_saccades$end)
flattened_indices <- unlist(indices)
non_aug$Eye_Event[flattened_indices] <- rep(
as.character(non_saccades$event),
times = lengths(indices)
)
non_aug[is.na(non_aug$Eye_Event), "Eye_Event"] <- "saccade"
rm(non_saccades, indices, flattened_indices)
# First frame of task availability
non_aug %<>%
group_by(consecutive_id(Task_Available)) %>%
mutate(Task_Start_X = first(X)) %>%
ungroup()
non_aug$`consecutive_id(Task_Available)` <- NULL
# Filter to rural straight, gravel, and dark segments
non_aug %<>% filter(EventName %in% c(
"RuralStraight",
"Gravel",
"Dark"
))
### Aggregate matrix
# Sensitivity of lateral deviation (Road width / sens is the threshold)
lat_dev_sens <- 4 # 12 / 4 = 3 foot threshold
# Cleaned button press frames
press_events <- non_aug %>%
filter(Correct_Left_Button_Press == 1) %>%
select(
DaqName,
Subject,
Drive,
X,
Frame_Num,
Left_Button_Press,
Task_Start_X,
)
# Metrics for which to generate a paired difference
metrics <- c(
"Deviation_Frames_prop",
"Avg_Pupil_Diameter_mean",
"RPupil_Diameter_mean",
"LPupil_Diameter_mean",
"Gaze_Yaw_mean",
"Gaze_Pitch_mean",
"Lat_Dev_SD",
"Speed_SD",
"Braking_Events",
"Per_Close",
"Per_Close_80",
"Brake_Force_mean"
)
for (frame_length in c(180, 300, 600)) {
# Window of frames around each press
task_windows <- press_events %>%
rowwise() %>%
reframe(
DaqName = DaqName,
Subject = Subject,
Drive = Drive,
# Want the frame number within the run of the simulator
Press_Frame = Frame_Num,
Frame_Index = X,
Frame = Task_Start_X:(X + frame_length - 1),
Phase = rep("Task", X - Task_Start_X + frame_length)
)
# Find the boundaries of every task block per Daq run
task_boundaries <- task_windows %>%
group_by(DaqName, Subject, Drive, Press_Frame, Frame_Index) %>%
summarize(
Task_Start_X = min(Frame),
Segment_Length = n(),
.groups = "drop"
) %>%
arrange(DaqName, Task_Start_X) %>%
group_by(DaqName) %>%
mutate(
Control_Start = Task_Start_X - Segment_Length,
Control_End = Task_Start_X - 1,
Task_Start = Task_Start_X,
Task_End = Task_Start_X + Segment_Length - 1,
) %>%
ungroup() %>%
filter(!is.na(Control_Start) & Control_End >= Control_Start)
non_aug$Task_Start_X <- NULL
# Expand these boundaries into actual individual frame rows
control_windows <- task_boundaries %>%
rowwise() %>%
reframe(
DaqName = DaqName,
Subject = Subject,
Drive = Drive,
Press_Frame = Press_Frame, # Linking it to the upcoming press
Frame_Index = Frame_Index,
Frame = Control_Start:Control_End,
Phase = "Control"
)
task_boundaries %<>% select(matches("^(Control|Task)_(Start|End)$"))
# Combine with original Pre/Post task windows with the new control windows
all_windows <- bind_rows(task_windows, control_windows) %>%
arrange(DaqName, Frame) %>%
mutate(Phase = factor(Phase, levels = c(
"Control",
"Task"
)))
rm(control_windows, task_windows)
# Pull in raw eye data for ALL windows (Tasks and Controls)
task_windows_with_control <- all_windows %>%
inner_join(
select(non_aug, -DaqName),
by = c("Subject", "Drive", "Frame" = "X")
)
rm(all_windows)
# Aggregate into one row per window with the control phases
task_matrix <- task_windows_with_control %>%
group_by(DaqName, Subject, Drive, Press_Frame, Frame_Index, Phase) %>%
summarize(
LPupil_Diameter_mean = mean(LPupil_Diameter, na.rm = TRUE),
RPupil_Diameter_mean = mean(RPupil_Diameter, na.rm = TRUE),
Avg_Pupil_Diameter_mean = mean(Avg_Pupil_Diameter, na.rm = TRUE),
Gaze_Pitch_mean = mean(Gaze_Pitch, na.rm = TRUE),
Gaze_Yaw_mean = mean(Gaze_Yaw, na.rm = TRUE),
Lane_Departure = any(abs(Vehicle_Lat_Dev) > Road_Width / lat_dev_sens),
Deviation_Frames_prop = sum(
abs(Vehicle_Lat_Dev) > Road_Width / lat_dev_sens
) / n(),
BAC = Start_BAC[1],
KSS = KSS_Score[1],
Blink_Count = max(Blink_Counter) - min(Blink_Counter),
# Per close is the proportion that **both eyes are closed simultaneously**
Per_Close = mean(Blinking, na.rm = TRUE),
Per_Close_80 = mean(Eyes_Closed_80pct, na.rm = TRUE),
# Initial lateral deviation affects SDLP since they expect to correct
Lat_Dev_abs_init = Vehicle_Lat_Dev[1],
Lat_Dev_SD = sd(Vehicle_Lat_Dev, na.rm = TRUE),
Speed_SD = sd(Vehicle_Speed, na.rm = TRUE),
Speed_mean = mean(Vehicle_Speed, na.rm = TRUE),
Braking_Events = sum(lag(Brake_Pedal_Force) == 0 & Brake_Pedal_Force > 0,
na.rm = TRUE
),
Brake_Force_mean = mean(Brake_Pedal_Force, na.rm = TRUE),
Saccade_Count = sum(Eye_Event == "saccade" & lag(Eye_Event) != "saccade",
na.rm = TRUE
),
Saccade_Prop = mean(Eye_Event == "saccade", na.rm = TRUE),
Road_Surface = ifelse(names(which.max(table(EventName))) != "Gravel",
"Paved",
"Gravel"
),
Frame_Count = n(),
.groups = "drop"
) %>%
mutate(
BAC_cent = BAC - median(BAC, na.rm = TRUE),
KSS_cent = KSS - median(KSS, na.rm = TRUE)
) %>%
group_by(Frame_Index) %>%
mutate(Reaction_Frames = Frame_Count[1] - frame_length) %>%
ungroup()
rm(task_windows_with_control)
write.csv(task_matrix, "./data/task_matrix.csv", row.names = FALSE)
# Make wide-form version for paired differences
wideform_task_matrix <- task_matrix %>%
pivot_wider(
names_from = Phase,
values_from = setdiff(
tail(colnames(task_matrix), -5),
c(
"BAC",
"KSS",
"BAC_cent",
"KSS_cent",
"Reaction_Frames",
"Road_Surface"
)
)
)
for (metric in metrics) {
wideform_task_matrix <- wideform_task_matrix %>%
mutate(
"{metric}_diff" := .data[[glue("{metric}_Task")]] -
.data[[glue("{metric}_Control")]]
)
}
# Assign to frame length-specific variables
for (var in c("task_boundaries", "task_matrix", "wideform_task_matrix")) {
assign(glue("{var}_{frame_length}"), get(var))
}
}
rm(
press_events,
metrics,
metric,
frame_length,
lat_dev_sens,
task_boundaries,
task_matrix,
wideform_task_matrix,
var
)
# Write environment
qs_save(as.list(environment()), "./data/processed_data.qs")