-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedfinfo.py
More file actions
executable file
·326 lines (263 loc) · 10.7 KB
/
edfinfo.py
File metadata and controls
executable file
·326 lines (263 loc) · 10.7 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
#!/usr/bin/env python3
"""edfinfo provides some basic information about edf files"""
import re
import os.path
import shutil
import subprocess
import tempfile
import pathlib
from typing import List
_PROGRAM_NAME = "edfinfo"
_DESCRIPTION = """edfinfo provides some helpful information about SR-Reseach/Eyelink
edf files (.edf)"""
_EDF2ASC = "edf2asc"
_HAVE_EDF2ASC = shutil.which(_EDF2ASC) is not None
MSG = "MSG"
def is_edf(fn: str):
"""Returns whether or not the file is a edf file"""
return os.path.splitext(fn)[1] == ".edf"
def is_asc(fn: str):
"""Returns whether or not the file is a asc file"""
return os.path.splitext(fn)[1] == ".asc"
def is_eytracker_fn(fn: str):
"""Returns True if edfinfo should think the file is a valid eyetracker
file False otherwise.
"""
return is_asc(fn) or is_edf(fn)
class NotAnEyetrackerFile(Exception):
"""Raised by EdfInfo when it thinks it is parsing an invalid file"""
class EyeFileInfo:
"""
Obtains general info about a eyelink data file (.edf file).
"""
RE_START = r"^(\*\* )?"
RE_MSG_START = r"^(MSG\s+\d+\s+)"
RE_DATE = re.compile(RE_START + r"DATE: (.*)")
RE_TYPE = re.compile(RE_START + r"TYPE: (.*)")
RE_VERSION = re.compile(RE_START + r"VERSION: (.*)")
RE_SOURCE = re.compile(RE_START + r"SOURCE: (.*)")
RE_EYELINK = re.compile(RE_START + r"(EYELINK .*)")
RE_CAMERA = re.compile(RE_START + r"CAMERA: (.*)")
RE_SERIAL = re.compile(RE_START + r"SERIAL NUMBER: (.*)")
RE_CAMERA_CONF = re.compile(RE_START + r"CAMERA_CONFIG: (.*)")
RE_RECORDED_BY = re.compile(RE_START + r"RECORDED BY: (.*)")
RE_EXPERIMENT = re.compile(RE_START + r"EXPERIMENT: (.*)")
RE_RESEARCHER = re.compile(RE_START + r"RESEARCHER: (.*)")
RE_PARTICIPANT = re.compile(RE_START + r"PARTICIPANT: (.*)")
RE_SESSION = re.compile(RE_START + r"SESSION: (.*)")
RE_LIST = re.compile(RE_START + r"LIST: (.*)")
RE_RECORDING = re.compile(RE_START + r"RECORDING: (.*)")
# These are added because Zep-2 output this info in messages instead of preamble
RE_M_RECORDED_BY = re.compile(RE_MSG_START + r"RECORDED BY:(.*)")
RE_M_EXPERIMENT = re.compile(RE_MSG_START + r"EXPERIMENT:(.*)")
RE_M_RESEARCHER = re.compile(RE_MSG_START + r"RESEARCHER:(.*)")
RE_M_PARTICIPANT = re.compile(RE_MSG_START + r"PARTICIPANT:(.*)")
RE_M_SESSION = re.compile(RE_MSG_START + r"SESSION:(.*)")
RE_M_LIST = re.compile(RE_MSG_START + r"LIST:(.*)")
RE_M_RECORDING = re.compile(RE_MSG_START + r"RECORDING:(.*)")
# If the next two regexes match we've parsing of eyefileinfo
# should be completed.
RE_MSG = re.compile(r"^MSG")
RE_ENDP = re.compile(r"^ENDP:.*")
def __init__(self):
self.date = ""
self.type = ""
self.version = ""
self.source = ""
self.eyelink = ""
self.camera = ""
self.serial = ""
self.camera_conf = ""
self.recorded_by = ""
self.experiment = ""
self.researcher = ""
self.participant = ""
self.session = ""
self.list = ""
self.recording = ""
def is_complete(self):
"""Determines whether all attributes are set to their final
value"""
for key, value in self.__dict__.items():
if not value:
return False
return True
def _parse_preamble(self, lines: List[str]):
"""Parses the preable of and edf file and fills out
The required attributes on self
Note in Zep-2 some of these variables have been moved
from the preable to the MSG's in the actual data.
hence, some deeper parsing is neccessary. Used
EdfInfo.is_complete() to check whether additional info
should be obtained from the file.
"""
for line in lines:
if obj := self.RE_DATE.match(line):
self.date = obj.group(2)
continue
if obj := self.RE_TYPE.match(line):
self.type = obj.group(2)
continue
if obj := self.RE_VERSION.match(line):
self.version = obj.group(2)
continue
if obj := self.RE_SOURCE.match(line):
self.source = obj.group(2)
continue
if obj := self.RE_EYELINK.match(line):
self.eyelink = obj.group(2)
continue
if obj := self.RE_CAMERA.match(line):
self.camera = obj.group(2)
continue
if obj := self.RE_SERIAL.match(line):
self.serial = obj.group(2)
continue
if obj := self.RE_CAMERA_CONF.match(line):
self.camera_conf = obj.group(2)
continue
if obj := self.RE_RECORDED_BY.match(line):
self.recorded_by = obj.group(2)
continue
if obj := self.RE_EXPERIMENT.match(line):
self.experiment = obj.group(2)
continue
if obj := self.RE_RESEARCHER.match(line):
self.researcher = obj.group(2)
continue
if obj := self.RE_PARTICIPANT.match(line):
self.participant = obj.group(2)
continue
if obj := self.RE_SESSION.match(line):
self.session = obj.group(2)
continue
if obj := self.RE_LIST.match(line):
self.list = obj.group(2)
continue
if obj := self.RE_RECORDING.match(line):
self.recording = obj.group(2)
continue
def _parse_msg_lines(self, lines: List[str]):
"""Parses the messages to collect info
about the experiment, researcher, participant,
session, list and recording info.
If information was already found in the preamble
it's overwritten, hence the messages are treated
as leading.
"""
for line in lines:
if obj := self.RE_M_RECORDED_BY.match(line):
self.recording = obj.group(2)
if obj := self.RE_M_EXPERIMENT.match(line):
self.experiment = obj.group(2)
if obj := self.RE_M_RESEARCHER.match(line):
self.researcher = obj.group(2)
if obj := self.RE_M_PARTICIPANT.match(line):
self.participant = obj.group(2)
if obj := self.RE_M_SESSION.match(line):
self.session = obj.group(2)
if obj := self.RE_M_LIST.match(line):
self.list = obj.group(2)
if obj := self.RE_M_RECORDING.match(line):
self.recording = obj.group(2)
def parse_file(self, fn: str):
"""Parses the file fn
@fn a valid eyetracker file
Raises NotAnEyetrackerFile when we think it is not an eyetracker
file.
"""
if not is_eytracker_fn(fn):
raise NotAnEyetrackerFile()
lines = []
with open(fn, "rb") as f:
# If these match, than we should have everything we need.
for l in f:
line = l.decode("utf8")
obj = self.RE_MSG.match(line)
if obj:
break
obj = self.RE_ENDP.match(line)
if obj:
break
lines.append(line)
self._parse_preamble(lines)
if not self.is_complete():
if _HAVE_EDF2ASC:
self.deep_parse(fn)
def deep_parse(self, fn: str):
"""Extracts the .edf file to .asc and inspects whether the eyelink MSG
can fill out the missing values"""
if not is_eytracker_fn(fn):
raise ValueError(f'Not a valid filename: "${fn}"')
tempname = os.path.join(
tempfile.gettempdir(), os.path.splitext(os.path.basename(fn))[0] + ".asc"
)
if is_edf(fn):
if not _HAVE_EDF2ASC:
raise RuntimeError("the SR research edf2asc program wasn't found")
# Create a temporary output file in .asc format
# edf2asc The SR research edf -> asc converter
# -y : overwrite .asc if exists
# -ns : no samples
subprocess.run(
[_EDF2ASC, "-y", "-ns", fn, tempname], stdout=subprocess.DEVNULL
)
else:
shutil.copy(fn, tempname)
msg_lines = []
with open(tempname) as myfile:
msg_lines = [line.strip() for line in myfile if line[: len(MSG)] == MSG]
self._parse_msg_lines(msg_lines)
# Cleanup after use
temppath = pathlib.Path(tempname)
temppath.unlink()
def __str__(self):
"""Return a string representation of self compatible with the
older edfinfo perl script.
"""
s = ""
if self.date:
s += " date:\t\t\t{}".format(self.date) + os.linesep
if self.type:
s += " type:\t\t\t{}".format(self.type) + os.linesep
if self.version:
s += " version:\t\t{}".format(self.version) + os.linesep
if self.source:
s += " source:\t\t{}".format(self.source) + os.linesep
if self.eyelink:
s += " eyelink:\t\t{}".format(self.eyelink) + os.linesep
if self.camera:
s += " camera:\t\t{}".format(self.camera) + os.linesep
if self.serial:
s += " serial number:\t{}".format(self.serial) + os.linesep
if self.camera_conf:
s += " camera config:\t{}".format(self.camera_conf) + os.linesep
if self.recorded_by:
s += " recorded by:\t\t{}".format(self.recorded_by) + os.linesep
if self.experiment:
s += " experiment:\t\t{}".format(self.experiment) + os.linesep
if self.researcher:
s += " researcher:\t\t{}".format(self.researcher) + os.linesep
if self.participant:
s += " participant:\t\t{}".format(self.participant) + os.linesep
if self.session:
s += " session:\t\t{}".format(self.session) + os.linesep
if self.list:
s += " list:\t\t\t{}".format(self.list) + os.linesep
if self.recording:
s += " recording:\t\t{}".format(self.recording)
return s
if __name__ == "__main__":
import sys
import argparse as ap
parser = ap.ArgumentParser(_PROGRAM_NAME, description=_DESCRIPTION)
parser.add_argument("input_files", nargs="+", help="The input .edf file's")
args = parser.parse_args()
for fn in args.input_files:
if is_eytracker_fn(fn) and os.path.exists(fn):
info = EyeFileInfo()
info.parse_file(fn)
print("{}:".format(fn))
print(str(info))
else:
print('Skipping "{}" (not an edf or asc file).'.format(fn), file=sys.stderr)