funpreprocess = function(workdir, FDMax = 1000, FDMin = 80) { #preprocess and put out FTtotal.csv, FTtotalA.csv, FTtoalAS.csv and FTtotalASR.csv #workdir - woking directory where the Sub*.csv files were setwd(workdir) cat('Now preprocessing is done...\n') Sys.sleep(1) csvfilename = dir(pattern = "Sub*") sub0 = c() item0 = c() cond0 = c() xcoor0 = c() ycoor0 = c() Tstart0 = c() Tend0 = c() for(i in csvfilename) { tempsubfile = read.csv(i, sep = ' ', header = F) for(j in 1:nrow(tempsubfile)) { templine = tempsubfile[j,] templine = templine[,!is.na(templine)] if(ncol(templine) > 8 | templine[[3]] > 99) { if(templine[[3]] < 99) { numberfixation = (ncol(templine)-8)/4 for(k in 1:numberfixation) { sub0 = c(sub0, substr(i,1,nchar(i)-4)) cond0 = c(cond0, templine[[2]]) item0 = c(item0, templine[[3]]) xcoor0 = c(xcoor0, templine[[4*k+5]]) ycoor0 = c(ycoor0, templine[[4*k+6]]) Tstart0 = c(Tstart0, templine[[4*k+7]]) Tend0 = c(Tend0, templine[[4*k+8]]) } } else { numberfixation = ncol(templine)/4 for(k in 1:numberfixation) { sub0 = c(sub0, substr(i,1,nchar(i)-4)) cond0 = c(cond0, templine[j-1,][[2]]) item0 = c(item0, tempsubfile[j-1,][[3]]) xcoor0 = c(xcoor0, templine[[4*k-3]]) ycoor0 = c(ycoor0, templine[[4*k-2]]) Tstart0 = c(Tstart0, templine[[4*k-1]]) Tend0 = c(Tend0, templine[[4*k]]) } } } } cat("\015") cat(i,' has been done!!',"\n") } FTtotal = data.frame(sub0,cond0,item0,xcoor0,ycoor0,Tstart0,Tend0, stringsAsFactors = F) naposition = which(is.na(FTtotal$cond0)) for(i in naposition) { FTtotal$cond0[i] = FTtotal$cond0[i-1] } largeitemp = which(FTtotal$item0 > 99) for(i in largeitemp) { FTtotal$item0[i] = FTtotal$item0[i-1] } write.csv(FTtotal,'FTtotal.csv',row.names = F, quote = F) FTtotal = read.csv('FTtotal.csv', stringsAsFactors = F) cat('FTtotal.csv has been produced','\n') Sys.sleep(1) FTtotal = within(FTtotal,{ finalcoor = NA finalcoor = xcoor0+ycoor0*160 finalcoor = as.integer(finalcoor) }) subindex = unique(FTtotal$sub0) ffd0 = c() FFT = c() ROI0 = c() sacdir = c() checkffd = c() library(rio) ROI = import('ROI.xlsx') ROI[is.na(ROI),is.na(ROI)]=-1 for(i in subindex) { ffd1 = length(ffd0) #cat(i,' is doing...............','\n') tempsub = FTtotal[FTtotal$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { ffd1 = length(ffd0) # n == 1 tempitem = tempsub[tempsub$item0 %in% j,] xmax = 0 if(nrow(tempitem) > 1) { templine1 = tempitem[1,] templine2 = tempitem[2,] xmax = templine1$finalcoor[[1]] #ffd0 ffd0 = c(ffd0, T) #FFT FFT = c(FFT, templine1$Tend0[[1]] - templine1$Tstart0[[1]]) #sacdir x1 = templine1$finalcoor[[1]] x2 = templine2$finalcoor[[1]] sacdir = c(sacdir, ifelse(x2 > x1, 'fore','back')) #ROI0 temproi = ROI[ROI$item == templine1$item0 & ROI$condition == templine1$cond0,][3:ncol(ROI)] ROI0 = c(ROI0, ifelse(x1 %in% temproi, T, F)) #2 - n fnumber = nrow(tempitem) if(fnumber>1) { for(k in 2:fnumber) { templinen = tempitem[k,] xn = templinen$finalcoor[[1]] #ffd0 if(xn > xmax) { ffd0 = c(ffd0,T) xmax = xn } else { ffd0 = c(ffd0,F) } #FFT FFT = c(FFT, templinen$Tend0[[1]] - templinen$Tstart0[[1]]) #sacdir if(k < fnumber) { templinenp1 = tempitem[k+1,] xp1 = templinenp1$finalcoor[[1]] sacdir = c(sacdir, ifelse(xp1 > xn, 'fore', 'back')) } else { sacdir = c(sacdir, "NO") } #ROI temproi = ROI[ROI$item == templinen$item0 & ROI$condition == templinen$cond0,][3:ncol(ROI)] ROI0 = c(ROI0, ifelse(xn %in% temproi, T, F)) } } } checkffd = c(checkffd, length(ffd0) - ffd1) } cat(i,' has been done!!!!','\n') } FTtotalA = cbind(FTtotal, ffd0, FFT, ROI0, sacdir) write.csv(FTtotalA, 'FTtotalA.csv', row.names = F, quote = F) cat('FTtoalA.csv has been produced','\n') Sys.sleep(1) FTtotalAS = FTtotalA[FTtotalA$FFT %in% FDMax:FDMin,] write.csv(FTtotalAS, 'FTtotalAS.csv',row.names = F, quote = F) cat('FTtoalAS.csv has been produced','\n') Sys.sleep(1) FTtotalASR = FTtotalAS[FTtotalAS$ROI0 == T,] write.csv(FTtotalASR, 'FTtotalASR.csv', row.names = F, quote = F) cat('FTtoalASR.csv has been produced','\n') cat('Preprocessing has been done','\n\n') Sys.sleep(1) } funFTROInum = function(workdir) { #calculate the fixation numbers and its proportion in ROI for each trial on each prticipant #based on FTtotalAS.csv #workdir - directory where the FTtotal.csv file was setwd(workdir) cat('Calculating the number of fixation potint in ROI...\n') Sys.sleep(1) FTtotalAS = read.csv('FTtotalAS.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() ROIFixationnum = c() ROIFixationprop = c() subindex = unique(FTtotalAS$sub) for(i in subindex) { tempsub = FTtotalAS[FTtotalAS$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) tempitem2 = tempitem[tempitem$ROI0 == T,] ROIFixationnum = c(ROIFixationnum, nrow(tempitem2)) ROIFixationprop = c(ROIFixationprop, nrow(tempitem2)/nrow(tempitem)) } cat(i,' has been done','\n') } FTROInum = data.frame(Sub, Item, Cond, ROIFixationnum, ROIFixationprop) write.csv(FTROInum,'ROIFTnum.csv',row.names = F,quote = F) cat('FTROInum.csv has been produced','\n\n') Sys.sleep(1) } funROITTFFD = function(workdir) { #calculate the first fixation duration in ROI on each trial in each subject #based on FTtoalASR.csv #workdir - directory where the FTtotalASR.csv was setwd(workdir) cat('Calculating the first fixation duration and total time...\n') Sys.sleep(1) FTtotalASR = read.csv('FTtotalASR.csv', stringsAsFactors = F) sub0 = c() item0 = c() cond0 = c() totaltime0 = c() fft0 = c() subindex = unique(FTtotalASR$sub) for(i in subindex) { tempsub = FTtotalASR[FTtotalASR$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] sub0 = c(sub0, i) item0 = c(item0, j) cond0 = c(cond0, unique(tempitem$cond0)[[1]]) totaltime0 = c(totaltime0, sum(tempitem$FFT)) fft0 = c(fft0, ifelse(is.null(tempitem$FFT[[1]]),0,tempitem$FFT[[1]])) } cat(i,' has been done!!!!','\n') } totaltime_fft = data.frame(Sub = sub0, Item = item0, Cond = cond0, TotalTime = totaltime0, FFD = fft0, stringsAsFactors = F) write.csv(totaltime_fft, 'ROItotaltime&fft.csv', row.names = F, quote = F) cat('Totaltime and first fixation duration have been done','\n\n') Sys.sleep(1) } funROIRpt = function(workdir) { #calculate the pass times on ROI and cbind it to FTtotalAS.csv and then put out FTtotalASRpt.csv file #based on FTtotalAS.csv #workdir - directory where the FTtoalAS.csv file was setwd(workdir) cat('Calculating the ROI passing times...\n') Sys.sleep(1) FTtotalAS = read.csv('FTtotalAS.csv', stringsAsFactors = F) FTtotalASRpt = within(FTtotalAS,{ passtimes = NA passtimes[ROI0 == F] = 0 }) subindex = unique(FTtotalAS$sub) passtimes = c() for(i in subindex) { tempsub = FTtotalASRpt[FTtotalASRpt$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] NAposition = which(is.na(tempitem$passtimes)) if(length(NAposition) >0 ) { passtimes = c(passtimes,1) count = 1 if(length(NAposition) >1) { for(k in 2:length(NAposition)) { if(NAposition[k]-NAposition[k-1] == 1) { passtimes = c(passtimes,count) } else { count = count+1 passtimes = c(passtimes, count) } } } } } cat(i,' has been done!!!','\n') } FTtotalASRpt$passtimes[which(is.na(FTtotalASRpt$passtimes))] = passtimes write.csv(FTtotalASRpt, 'FTtotalASRpt.csv', quote = F, row.names = F) cat('FTtotalASRpt.csv has been done','\n\n') Sys.sleep(1) } funROIRptReg = function(workdir) { #calculate the last fixation before the first fixation landing in ROI each time was on the right or left to the ROI #based on FTtotalASRpe.csv and put out FTtoalASRepReg.csv file #workdir - directory where the FTtotalASRep.csv file was setwd(workdir) cat('Calculating whether regression was existed...\n') Sys.sleep(1) FTtotalASRpt = read.csv('FTtotalASRpt.csv', stringsAsFactors = F) FTtotalASRptReg = within(FTtotalASRpt, { regressionfrom = NA regressionfrom[passtimes %in% 0] = 'None' }) regression = c() subindex = unique(FTtotalASRpt$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] NAposition = which(is.na(tempitem$regre)) if(length(NAposition) > 0) { for(k in NAposition) { if(k==1) { regression = c(regression,'None') } else { if(tempitem$sacdir[[k-1]] %in% 'fore' & tempitem$ROI0[[k-1]] == F) { regression = c(regression,'Left') } else if(tempitem$sacdir[[k-1]] %in% 'back' & tempitem$ROI0[[k-1]] == F) { regression = c(regression,'Right') } else { regression = c(regression,regression[length(regression)]) } } } } } cat(i,' has been done!!!','\n') } FTtotalASRptReg$regressionfrom[is.na(FTtotalASRptReg$regressionfrom)] = regression write.csv(FTtotalASRptReg, 'FTtotalASRptReg.csv', row.names = F, quote = F) cat('FTtotalASRptReg.csv has been done','\n\n') Sys.sleep(1) } funROIregressionin = function(workdir) { #calculate whether the ROI received regression and whether the first time landing in ROI was regression #based on FTtotalASRepReg.csv #workdir - the directory where the FTtotalASRptReg.csv was setwd(workdir) cat('Calculating the ROI regression in...\n') Sys.sleep(1) FTtotalASRptReg = read.csv('FTtotalASRptReg.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() ReginRight = c() ReginRightFF = c() subindex = unique(FTtotalASRptReg$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) if(sum(tempitem$regressionfrom[tempitem$passtimes != 0] %in% 'Right') != 0) { ReginRight = c(ReginRight, 1) } else { ReginRight = c(ReginRight, 0) } if(sum(tempitem$regressionfrom[tempitem$passtimes == 1] %in% 'Right') !=0) { ReginRightFF = c(ReginRightFF, 1) } else { ReginRightFF = c(ReginRightFF,0) } } cat(i,' has been done!!!','\n') } ROIrightregressionin = data.frame(Sub, Item, Cond, ReginRight, ReginRightFF, stringsAsFactors = F) write.csv(ROIrightregressionin, 'ROIrightregressionIn.csv', row.names = F, quote = F) cat('ROI regression has been done','\n\n') Sys.sleep(1) } funROIsecondFT = function(workdir) { #calculate the total time of passing ROI second time #based on FTtotalASRepReg.csv #workdir - directory where the FTtotalASRptReg.csv file was setwd(workdir) cat('Calculating the second pass time...\n') Sys.sleep(1) FTtotalASRptReg = read.csv('FTtotalASRptReg.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() SecondFT2 = c() SecondFT3 = c() SecondFT4 = c() subindex = unique(FTtotalASRptReg$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) tempsecFT = tempitem[tempitem$passtimes %in% 2,] if(is.na(tempsecFT$FFT[1])) { SecondFT2 = c(SecondFT2,0) } else { SecondFT2 = c(SecondFT2, sum(tempsecFT$FFT)) } tempsecFT = tempitem[tempitem$passtimes %in% 3,] if(is.na(tempsecFT$FFT[1])) { SecondFT3 = c(SecondFT3,0) } else { SecondFT3 = c(SecondFT3, sum(tempsecFT$FFT)) } tempsecFT = tempitem[tempitem$passtimes %in% 4,] if(is.na(tempsecFT$FFT[1])) { SecondFT4 = c(SecondFT4,0) } else { SecondFT4 = c(SecondFT4, sum(tempsecFT$FFT)) } } cat(i,' has been done!!!','\n') } ROIsecondFT = data.frame(Sub, Item, Cond, SecondFT2, SecondFT3, SecondFT4, stringsAsFactors = F) write.csv(ROIsecondFT, 'ROIsecondFT.csv', row.names = F, quote = F) cat('ROI second fixation duration has been done','\n\n') Sys.sleep(1) } funROIgazeduration = function(workdir) { #calcualte the gaze duration on ROI #based on FTtotalASRptReg.csv file #workdir - drectory where the FTtotalASRptReg.csv file was setwd(workdir) cat('Calculating the first pass time...\n') Sys.sleep(1) FTtotalASRptReg = read.csv('FTtotalASRptReg.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() GazeDuration = c() subindex = unique(FTtotalASRptReg$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) tempsecFT = tempitem[tempitem$passtimes %in% 1,] if(is.na(tempsecFT$FFT[1])) { GazeDuration = c(GazeDuration,0) } else { GazeDuration = c(GazeDuration, sum(tempsecFT$FFT)) } } cat(i,' has been done!!!','\n') } ROIgazeduration = data.frame(Sub, Item, Cond, GazeDuration, stringsAsFactors = F) write.csv(ROIgazeduration, 'ROIgazeduration.csv', row.names = F, quote = F) cat('ROI gaze duration has been done','\n\n') Sys.sleep(1) } funROIregressionout = function(workdir) { #calculate whether any regression was given out from ROI and whether any regression was given out from ROI first time passing #based on FTtotalASRptReg.csv file #workdir - directory where FTtotalASRptReg.csv file was setwd(workdir) cat('Calculating the ROI regression out...\n') Sys.sleep(1) FTtotalASRptReg = read.csv('FTtotalASRptReg.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() Regressionout = c() FFregressionout = c() subindex = unique(FTtotalASRptReg$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) NAposition = which(tempitem$passtimes != 0) regout = 0 if(length(NAposition)>0) { for(k in NAposition) { if(k < nrow(tempitem) & tempitem$passtimes[k+1] == 0 & tempitem$sacdir[k] %in% 'back') { regout = 1 } } } Regressionout = c(Regressionout, regout) NAposition = which(tempitem$passtimes == 1) FTregout = 0 if(length(NAposition) > 0) { for(k in NAposition) { if(k < nrow(tempitem) & tempitem$passtimes[k+1] == 0 & tempitem$sacdir[k] %in% 'back') { FTregout = 1 } } } FFregressionout = c(FFregressionout, FTregout) } cat(i,'has been done!!!','\n') } ROIregressionout = data.frame(Sub, Item,Cond,Regressionout, FPregressionout = FFregressionout, stringsAsFactors = F) write.csv(ROIregressionout, 'ROIregressionout.csv', row.names = F, quote = F) cat('ROI regression out has been done','\n\n') Sys.sleep(1) } funROIfixationprop = function(workdir) { #calculate whether the ROI was focused when subject passed it first time #based on FTtotalASRptReg.csv file #workdir - directory where FTtotalASRptReg.csv file was setwd(workdir) cat('Calculating the fixation proportion...\n') Sys.sleep(1) FTtotalASRptReg = read.csv('FTtotalASRptReg.csv', stringsAsFactors = F) Sub = c() Item = c() Cond = c() FixationProp = c() subindex = unique(FTtotalASRptReg$sub) for(i in subindex) { tempsub = FTtotalASRptReg[FTtotalASRptReg$sub0 %in% i,] itemindex = unique(tempsub$item0) for(j in itemindex) { tempitem = tempsub[tempsub$item0 %in% j,] Sub=c(Sub,i) Item = c(Item,j) Cond = c(Cond, unique(tempitem$cond0)[[1]]) FFTposition = which(tempitem$passtimes == 1)[1] if(is.na(FFTposition)) { FixationProp = c(FixationProp,0) } else { if(max(tempitem$finalcoor[1:(FFTposition-1)]) < tempitem$finalcoor[FFTposition]) { FixationProp = c(FixationProp,1) } else { FixationProp = c(FixationProp,0) } } } cat(i,'has been done!!!','\n') } ROIfixationprop = data.frame(Sub,Item,Cond,FixationProp, stringsAsFactors = F) write.csv(ROIfixationprop, 'ROIfixationprop.csv', row.names = F, quote = F) cat('ROI fixation proportion has been done','\n\n') Sys.sleep(1) } funIntegrate = function(workdir = getwd(), FDMax = 1000, FDMin = 80, preprocess = T, analysis = T, Integrate = T) { #check whether the rio package has been downloaded if(sum(unique(installed.packages()[,c('Package')] %in% 'rio')) == 0) {install.packages('rio')} library(rio) #integrate the processing from preprocess to extracting all measures if(preprocess == T) {funpreprocess(workdir, FDMax = FDMax, FDMin = FDMin)} if(analysis == T) { funROIRpt(workdir) funROIRptReg(workdir) funFTROInum(workdir) funROITTFFD(workdir) funROIregressionin(workdir) funROIsecondFT(workdir) funROIgazeduration(workdir) funROIregressionout(workdir) funROIfixationprop(workdir) if(Integrate == T) {ROI1 = import("ROItotaltime&fft.csv") ROI2 = import("ROIfixationprop.csv") ROI3 = import("ROIFTnum.csv") ROI4 = import("ROIgazeduration.csv") ROI5 = import('ROIregressionout.csv') ROI6 = import('ROIrightregressionIn.csv') ROI7 = import('ROIsecondFT.csv') ROItotal = merge(ROI1, ROI2, by = c('Sub','Item','Cond')) ROItotal = merge(ROItotal, ROI3, by = c('Sub','Item','Cond')) ROItotal = merge(ROItotal, ROI4, by = c('Sub','Item','Cond')) ROItotal = merge(ROItotal, ROI5, by = c('Sub','Item','Cond')) ROItotal = merge(ROItotal, ROI6, by = c('Sub','Item','Cond')) ROItotal = merge(ROItotal, ROI7, by = c('Sub','Item','Cond')) export(ROItotal, 'ROItotal.csv') cat('Data integrating is done!\n\n')} cat('Analysis is done!\n') } } #check whether the fgui package has been down loaded if(sum(unique(installed.packages()[,c('Package')] %in% 'fgui')) == 0) {install.packages('fgui')} library(fgui) res = gui(funIntegrate, argOption = list(preprocess = c('T','F'), analysis = c('T','F'), Integrate = c('T','F')), argEdit = list(FDMax = NULL, FDMin = NULL), title = 'Data preprocessing and analysis for eye movements')