-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormattingFixCountForMachineLearning
More file actions
290 lines (227 loc) · 11.6 KB
/
FormattingFixCountForMachineLearning
File metadata and controls
290 lines (227 loc) · 11.6 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
#Load Packages
library("broom", lib.loc="~/R/R-3.3.1/library")
library("tidyr", lib.loc="~/R/R-3.3.1/library")
library("ggplot2", lib.loc="~/R/R-3.3.1/library")
library("gridExtra", lib.loc="~/R/R-3.3.1/library")
library("dplyr", lib.loc="~/R/R-3.3.1/library")
library("tidyr", lib.loc="~/R/R-3.3.2/library")
library("plotly", lib.loc="~/R/R-3.3.2/library")
library(plyr)
library(data.table)
library(reshape2)
library(readr)
library(jpeg)
#Goals of this code
#1. Import all fixation and saccade reports
#2. Combine them
#3. Figure out how to add them to a folder based on column "SubjectID_imageID"
sdif = 0
y =37
#Some vestigal bits of code bc changed purpose halfway through when talking to Pradeep.
#Loading Sample data####
#note, 9a and 9b were combined in advance to this. 9a block 11 crashed part way through.
#part of this loop subsets to only include -1 in test confidence column. The purpose of this is some glitchy trials appear in the study period with only 1 sample from a test image which comes up as a new image but it's fake).
mylistnew <- list()
for (i in 1:y){
i = i + sdif #this is to adjust what subject you want to import
data_dir = 'C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/fixationreports'
filename = i
fileextension = ".csv"
filename = paste0(filename, fileextension)
filepath = paste(data_dir, '/', filename, sep = ''); filepath
tosubset <- fread(filepath)
newdf <- tosubset
#getting rid of "Trial: " so trial number can be numeric (mainly to match the df.merged_wdp)
newdf$TRIAL_LABEL <- gsub("Trial: ", "", newdf$TRIAL_LABEL)
newdf$TRIAL_LABEL <- as.numeric(newdf$TRIAL_LABEL)
i = i -sdif #(part 2 to not have a list 9 items long for subj 7-9)
mylistnew[[i]] <- newdf
}
#Load data 2####
#Loading in data from other script (Objfam_qualitychecks_accuracy_betweenSubj_april11) If it's still named that.
df.merged_wdp <- fread('C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/dp_and_subqAcc.csv')
# #1.5 Fixing Studyphase/repnum####
# #selecting only study
# df.merged_wdp <- ifelse(df.merged_wdp)
#
# df.merged_wdp <- subset(df.merged_wdp, subset = df.merged_wdp$task == 'study')
# df.merged_wdp$repnum <- with(df.merged_wdp, ave(RECORDING_SESSION_LABEL, RECORDING_SESSION_LABEL, image_filename, FUN = seq_along))
#
# #does repnum equal studyphase for the subjects that had it?
# df.merged_wdp$
#
# df.merged_wdp$errorcheck <- ifelse(df.merged_wdp$studyphase==df.merged_wdp$repnum, "good", "maybeerror")
#
#2. Label each row as hit/miss/FA/CR and dprime
#practice not in a loop
#making a vector of relevant columns I want from df.merged_wdp
df.merged_wdp$imgname <- substr(df.merged_wdp$image_filename, 1,7)
df.merged_wdp$TRIAL_LABEL <- gsub("Trial: ", "", df.merged_wdp$TRIAL_LABEL)
df.merged_wdp$TRIAL_LABEL <- as.numeric(df.merged_wdp$TRIAL_LABEL)
df.sorted <- df.merged_wdp[order(SID, imgname, TRIAL_LABEL),]
df.sorted <- subset(df.sorted, subset = df.sorted$task == 'study')
df.sorted$IID_SID <- paste0(df.sorted$imgname,"_._", df.sorted$SID)
RelCol <- c("IID_SID","SID", "RECORDING_SESSION_LABEL", "X__Reaction_Time__1", "block", "imgname", "studyphase", "oldnearorfar", "test_confidence.x", "TRIAL_LABEL", "SubsequentAccuracy", "SubsequentMorph", "test_confidence.y", "dprime_nearonly_noguess", "dprime_faronly_noguess")
#Loop to merge d' and hit/miss (and many other rows)
mylist2 <- list(seq(1:y))
j = 1
for (j in 1:y) {
mylist2[[j]] <- merge(mylistnew[[j]], df.sorted[ , RelCol, with = F], by = c('RECORDING_SESSION_LABEL', 'TRIAL_LABEL'), all = F)
}
#2.5. set it to show only the used eye's location
#in loop for all subj
mylist3 <- list(seq(1:y))
k = 1
for (k in 1:y) {
#Setting list number to df named visualizermerged
mylist2[[k]] -> visualizerMerged
#Doing nothing bc not relevant for fixation and saccade reports
visualizerMerged -> mylist3[[k]]
}
#3. Loop to save with everything in its own folder by trial...
#Encoding####
#saving individual trial to it's own folder.
for (m in 1:y){
mylist3[[m]] -> alltrials
for (n in 1:length(unique(alltrials$TRIAL_LABEL))) {
trial1 <- subset(alltrials, subset = alltrials$TRIAL_LABEL == n)
s1 <- trial1[1, 'IID_SID']
i1 <- trial1[1,"imgname"]
r1 <- trial1[1, "studyphase.x"]
# mainDir <- ('C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
mainDir <- ('C:/Users/kgeier/Desktop/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
subDira <- paste0("IID_SID_", s1)
subDird2 <- paste0("repnum_", r1)
#Slowly building directory structure
path <- file.path(mainDir, subDira)
dir.create(path, showWarnings = F)
path <- file.path(path, subDird2)
dir.create(path, showWarnings = F)
#selecting only the few columns Pradeep wants for Machine learning
OneTrial <- trial1[, c("SID","TRIAL_LABEL","CURRENT_FIX_BLINK_AROUND",'CURRENT_FIX_DURATION', "studyphase.x",
"CURRENT_FIX_START","CURRENT_FIX_END","CURRENT_FIX_INDEX",'CURRENT_FIX_MSG_TEXT_1','CURRENT_FIX_PUPIL',
"CURRENT_FIX_X", "CURRENT_FIX_Y", 'IP_START_TIME', "IP_END_TIME",
'TRIAL_FIXATION_TOTAL',"Response","__Reaction_Time__1", "block.x",'image_filename',
"SubsequentAccuracy", "test_confidence.y")]
pathfinal <- file.path(mainDir,subDira,subDird2)
write.csv(x=OneTrial, file= paste0(pathfinal,"/","fixationreport.csv"), quote = F, sep="\t", row.names = FALSE)
}
}
#Recall####
#saving individual trial to it's own folder.
for (m in 1:y){
mylist3[[m]] -> alltrials
for (n in 1:length(unique(alltrials$TRIAL_LABEL))) {
trial1 <- subset(alltrials, subset = alltrials$TRIAL_LABEL == n)
s1 <- trial1[1, 'IID_SID']
sa1 <- trial1[1,"SubsequentAccuracy"]
#mainDir <- ('C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
mainDir <- ('C:/Users/kgeier/Desktop/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
subDira <- paste0("IID_SID_", s1)
#Slowly building directory structure
path <- file.path(mainDir, subDira)
dir.create(path, showWarnings = F)
pathfinal <- file.path(mainDir,subDira)
write.table(x=sa1, file= paste0(pathfinal,"/","subsequentaccuracy.txt"), quote = F, col.names = F, row.names = F, sep="\t")
}
}
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
#SACCADE REPORT####
sdif=0
y =37
mylistnew <- list()
for (i in 1:y){
i = i + sdif #this is to adjust what subject you want to import
data_dir = 'C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/saccadereports'
filename = i
fileextension = ".csv"
filename = paste0(filename, fileextension)
filepath = paste(data_dir, '/', filename, sep = ''); filepath
tosubset <- fread(filepath)
newdf <- tosubset
#getting rid of "Trial: " so trial number can be numeric (mainly to match the df.merged_wdp)
newdf$TRIAL_LABEL <- gsub("Trial: ", "", newdf$TRIAL_LABEL)
newdf$TRIAL_LABEL <- as.numeric(newdf$TRIAL_LABEL)
i = i -sdif #(part 2 to not have a list 9 items long for subj 7-9)
mylistnew[[i]] <- newdf
}
RelCol <- c("IID_SID","SID", "RECORDING_SESSION_LABEL", "X__Reaction_Time__1", "block", "imgname", "studyphase", "oldnearorfar", "test_confidence.x", "TRIAL_LABEL", "SubsequentAccuracy", "SubsequentMorph", "test_confidence.y", "dprime_nearonly_noguess", "dprime_faronly_noguess")
#Loop to merge d' and hit/miss (and many other rows)
mylist2 <- list(seq(1:y))
j = 1
for (j in 1:y) {
mylist2[[j]] <- merge(mylistnew[[j]], df.sorted[ , RelCol, with = F], by = c('RECORDING_SESSION_LABEL', 'TRIAL_LABEL'), all = F)
}
#2.5. set it to show only the used eye's location
#in loop for all subj
mylist3 <- list(seq(1:y))
k = 1
for (k in 1:y) {
#Setting list number to df named visualizermerged
mylist2[[k]] -> visualizerMerged
#Doing nothing bc not relevant for fixation and saccade reports
visualizerMerged -> mylist3[[k]]
}
#Final Check to see if anything went wrong with the repnum
check.list <- list(seq(1:y))
p = 1
for (p in 1:y) {
mylist3[[p]] -> df.tc
df.er <- subset(df.tc, subset = df.tc$errorcheck != "good")
check.list[[p]] <- df.er
}
repcheckall <- do.call("rbind", check.list)
#3. Loop to save with everything in its own folder by trial...
#Encoding####
#saving individual trial to it's own folder.
for (m in 1:y){
mylist3[[m]] -> alltrials
for (n in 1:length(unique(alltrials$TRIAL_LABEL))) {
trial1 <- subset(alltrials, subset = alltrials$TRIAL_LABEL == n)
s1 <- trial1[1, 'IID_SID']
i1 <- trial1[1,"imgname"]
r1 <- trial1[1, "studyphase.x"]
# mainDir <- ('C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
mainDir <- ('C:/Users/kgeier/Desktop/MACHINE_LEARNING_codeForPradeep/OutputFolder2')
subDira <- paste0("IID_SID_", s1)
subDird2 <- paste0("repnum_", r1)
#Slowly building directory structure
path <- file.path(mainDir, subDira)
dir.create(path, showWarnings = F)
path <- file.path(path, subDird2)
dir.create(path, showWarnings = F)
#selecting only the few columns Pradeep wants for Machine learning
OneTrial <- trial1[, c("RECORDING_SESSION_LABEL",'IID_SID',"SID","TRIAL_LABEL", "studyphase.x",
"CURRENT_SAC_AMPLITUDE",'CURRENT_SAC_ANGLE',"CURRENT_SAC_AVG_VELOCITY",
"CURRENT_SAC_CONTAINS_BLINK","CURRENT_SAC_DIRECTION",'CURRENT_SAC_DURATION',
'CURRENT_SAC_END_TIME', "CURRENT_SAC_END_TIME", "CURRENT_SAC_START_X", "CURRENT_SAC_START_Y",
"CURRENT_SAC_END_X", "CURRENT_SAC_END_Y", "CURRENT_SAC_INDEX","CURRENT_SAC_MSG_TEXT_1",
"CURRENT_SAC_MSG_TIME_1","CURRENT_SAC_PEAK_VELOCITY","CURRENT_SAC_START_TIME",'IP_START_TIME', "IP_END_TIME",
"Response","__Reaction_Time__1", "block.x",'image_filename',
"test_confidence.y")]
pathfinal <- file.path(mainDir,subDira,subDird2)
write.csv(x=OneTrial, file= paste0(pathfinal,"/","saccadereport.csv"), quote = F, sep="\t", row.names = FALSE)
}
}
# data_dir = 'C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/saccadereports'
# filename = 1
# filename2 = '1_repnum'
# fileextension = ".csv"
# filename = paste0(filename, fileextension)
# filename2 = paste0(filename2, fileextension)
# filepath = paste(data_dir, '/', filename, sep = ''); filepath
# filepath2 = paste(data_dir, '/', filename2, sep = ''); filepath2
#
# tosubset <- fread(filepath)
# withrepnum <-fread(filepath2)
#
#
# a <- merge(tosubset, withrepnum[ ,c('RECORDING_SESSION_LABEL', 'TRIAL_LABEL',"V29"), with = F], by = c('RECORDING_SESSION_LABEL', 'TRIAL_LABEL'), all = F)
# a$studyphase <- a$V29
# b <- a[,1:26]
#
# pathfinal = 'C:/Users/kgeier/Google Drive/OlsenLab_cloud/All Experiments/EyeTracking/ObjFamETStudy/MACHINE_LEARNING_codeForPradeep/saccadereports'
# write.csv(x=b, file= paste0(pathfinal,"/","1.csv"), quote = F, sep="\t", row.names = FALSE)