-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpupilparser.py
executable file
·171 lines (133 loc) · 5.93 KB
/
pupilparser.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
#!/usr/bin/python
import msgpack
import sys
import os
import math
import csv
import numpy as np
import peakutils
import json
import matplotlib.pyplot as plt
def main():
blink_filter_length = 0.2
blink_confindence_threshold = 0.5
blink_resolution = 20.0
fixation_resolution = .1
blinks = []
blink_freq = []
pupil_diameter_x = [] # time
pupil_diameter_y = [] # diameter
pupil_error_x = [] # time
pupil_error_y = []
start_time = sys.maxint
end_time = -sys.maxint
last_blink_onset = -1
if len(sys.argv) < 3:
print "Usage: ./pupilparser.py <pupil_data file> <output folder>"
exit()
try:
pupil_data_file = open(sys.argv[1], 'rb')
pupil_data = pupil_data_file.read()
except:
print "Could not open pupil_data file"
exit()
finally:
pupil_data_file.close()
pupil_data_object = msgpack.unpackb(pupil_data, use_list=False, raw=False)
# Get start and end timestamps
for pupil_position in pupil_data_object["pupil_positions"]:
if pupil_position["timestamp"] < start_time:
start_time = pupil_position["timestamp"]
if pupil_position["timestamp"] > end_time:
end_time = pupil_position["timestamp"]
print "Recording is " + str(end_time-start_time) + " seconds long"
# Get blink times
for blink in pupil_data_object["blinks"]:
if blink["confidence"] > blink_confindence_threshold:
if blink["type"] == "onset":
last_blink_onset = blink["timestamp"]
elif blink["timestamp"] - last_blink_onset < blink_filter_length:
blinks.append(last_blink_onset)
print str(len(blinks)) + " blinks"
# Get blink freq
for i in range(int(blink_resolution), int(end_time-start_time)):
blink_count = 0
for blink in blinks:
if blink > (i - blink_resolution) + start_time and blink < i + start_time:
blink_count += 1
blink_freq.append(blink_count/blink_resolution)
# -------
# Pupil Diameter
last_diameter = -1
last_timestamp = -1
for pupil_position in pupil_data_object["pupil_positions"]:
pupil_error_x.append(pupil_position["timestamp"] - start_time)
pupil_error_y.append(pupil_position["confidence"])
if pupil_position["confidence"] > 0.6:
if last_timestamp == -1 or last_diameter == -1 or abs(last_diameter - pupil_position["diameter_3d"])/abs(pupil_position["timestamp"]-last_timestamp) < 1:
pupil_diameter_x.append(pupil_position["timestamp"] - start_time)
pupil_diameter_y.append(pupil_position["diameter_3d"])
last_timestamp = pupil_position["timestamp"]
last_diameter = pupil_position["diameter_3d"]
# -----
# Duration
fixations = [0] * int(((1.0/fixation_resolution) * (end_time - start_time)))
for fixation in pupil_data_object["fixations"]:
fixation_start = int((fixation["timestamp"] - start_time) * 1./fixation_resolution)
fixation_end = fixation_start + int(fixation["duration"] / 1000. * 1./fixation_resolution)
for i in range(fixation_start, fixation_end):
if i < len(fixations):
fixations[i] = 1
if not os.path.exists(sys.argv[2]):
os.makedirs(sys.argv[2])
blinkPlt = plt.figure(0)
plt.plot(blink_freq)
plt.ylabel('Blink Frequency')
plt.xlabel('time [s]')
plt.savefig(os.path.join(sys.argv[2], 'blink.png'))
#blinkPlt.show()
diameterPlt = plt.figure(1)
plt.plot(pupil_diameter_x, pupil_diameter_y)
plt.ylabel("diameter [mm]")
plt.xlabel("time [s]")
plt.savefig(os.path.join(sys.argv[2], 'diameter.png'))
error = plt.figure(2)
plt.plot(pupil_error_x, pupil_error_y)
plt.ylabel("confidence")
plt.xlabel("time [s]")
plt.savefig(os.path.join(sys.argv[2], 'confidence.png'))
with open(os.path.join(sys.argv[2], 'diameter.csv'), 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(pupil_diameter_x)
writer.writerow(pupil_diameter_y)
with open(os.path.join(sys.argv[2], 'blink.csv'), 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(blink_freq)
with open(os.path.join(sys.argv[2], 'confidence.csv'), 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(pupil_error_x)
writer.writerow(pupil_error_y)
summary = {}
summary["blink_freq"] = float(sum(blink_freq))/float(len(blink_freq))
summary["blink_freq_calibration"] = float(sum(blink_freq[:120]))/float(len(blink_freq[:120]))
summary["blink_freq_execution"] = float(sum(blink_freq[120:]))/float(len(blink_freq[120:]))
summary["blink_freq_diff"] = summary["blink_freq_execution"] - summary["blink_freq_calibration"]
summary["blink_freq_std"] = np.std(blink_freq)
summary["blink_freq_calibration_std"] = np.std(blink_freq[:120])
summary["blink_freq_execution_std"] = np.std(blink_freq[120:])
summary["blink_freq_peaks"] = len(peakutils.indexes(np.array(blink_freq), thres=0.15, min_dist=10))
summary["blink_freq_calibration_peaks"] = len(peakutils.indexes(np.array(blink_freq[:120]), thres=0.15, min_dist=10))
summary["blink_freq_execution_peaks"] = len(peakutils.indexes(np.array(blink_freq[120:]), thres=0.15, min_dist=10))
summary["blink_freq_calibration_max"] = max(np.array(blink_freq[:120]))
summary["blink_freq_execution_max"] = max(np.array(blink_freq[120:]))
summary["blink_freq_max_diff"] = summary["blink_freq_execution_max"] - summary["blink_freq_calibration_max"]
with open(os.path.join(sys.argv[2], 'summary.json'), 'w') as file:
file.write(json.dumps(summary, indent=4))
#fixationPlt = plt.figure(2)
#plt.plot(fixations)
#plt.bar(range(len(fixations)), fixations, 1)
#plt.ylabel("fixating [bool]")
#plt.xlabel("time [s*10]")
#plt.show()
if __name__ == "__main__":
main()