-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
81 lines (65 loc) · 4.22 KB
/
main.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
import os
import matplotlib.pyplot as plt
import numpy as np
import readers
import calculators
import plotters
import functions
import run_detection
from pathlib import Path
savedata = False # whether or not the gaze events and their measures are saved in .csv files
showfig = True # whether or not the plot figures are shown after detection
savefig = False # whether or not the plot figures are saved after detection
debugdetection = False # show runtime info about the detection in the console
printresults = True # show results of the detection in the console
# Import csv files --------------------------------------------------------------------------------------------------
datapath = os.getcwd() + "/testdata/" # put the full path to your data here
participants = 2 # number of participants
trials = 2 # trials per participant
filename = 'varjo_gaze_output' # looks for files with this string in the name
for participant in range(1, participants + 1):
print(), print(), print('Analyisis results for participant {}'.format(participant))
#start plot
fig, axs = plt.subplots(trials, figsize=[25.60, 7.20*trials])
fig.suptitle('Detection per trial for participant {}'.format(participant))
for trial in range(1, trials + 1):
trialpath = datapath + '{}/{}/'.format(participant,trial)
print(), print('Trial ' + str(trial))
csvdata = readers.file_reader(trialpath, participant, trial, filename)
gazedata = readers.gaze_arff(csvdata)
# classify gaze events ----------------------------------------------------------------------------------------------
classifiedgazedata = run_detection.DetectGazeEvents(gazedata, debugdetection)
t = classifiedgazedata['data']['time'] / 1000 # [s]
x = classifiedgazedata['data']['x'] # [deg]
y = classifiedgazedata['data']['y'] # [deg]
v = classifiedgazedata['data']['v'] # [deg/s]
e = classifiedgazedata['data']['EYE_MOVEMENT_TYPE'] # ('UNKNOWN', 'FIX', 'SACCADE', 'SP', 'NOISE', 'BLINK', 'NOISE_CLUSTER', 'PSO')
hz = 1000 / np.mean(np.diff(classifiedgazedata['data']['time']))
print("Gaze data recorded at: {} Hz".format(hz))
# Analyzing gaze event measures --------------------------------------------------------------------------------------
Fixations = calculators.fixation(x, y, t, e, printresults)
Saccades = calculators.saccade(x, y, v, t, e, printresults)
Pursuits = calculators.pursuit(x, y, v, t, e, printresults)
Blinks = calculators.blink(t, e, printresults)
# Saving gaze event data ---------------------------------------------------------------------------------------------
if savedata:
outputpath = trialpath + 'detection'
Path(outputpath).mkdir(parents=True, exist_ok=True)
# save detections per even type with their measures
functions.save_events(Fixations, 'fixations.csv', outputpath)
functions.save_events(Saccades, 'saccades.csv', outputpath)
functions.save_events(Pursuits, 'pursuits.csv', outputpath)
functions.save_events(Blinks, 'blinks.csv', outputpath)
# add gaze_event classification column to raw data and save copy
csvdata["gaze_event"] = classifiedgazedata['data']['EYE_MOVEMENT_TYPE']
csvdata.to_csv(outputpath + "/classified_data.csv")
# Plotting and saving------------------------------------------------------------------------------------------------
plotters.detection(x, y, t, v, Fixations, Saccades, Pursuits, Blinks, trials, trial, axs, hz)
plotters.calculation(Fixations, Saccades, Pursuits, Blinks, trial, participant)
outputpath = trialpath + "calculation-p{}-t{}.png".format(participant, trial, participant, trial)
if savefig: plt.savefig(outputpath, bbox_inches='tight')
plt.figure(1)
outputpath = datapath + '{}/detection-p{}.png'.format(participant, participant)
if savefig: plt.savefig(outputpath, bbox_inches='tight')
if showfig: plt.show()
if savefig: plt.close('all')