Skip to content

Commit 9d0ab84

Browse files
FIX, TST: Better file handling practices suggested by Eric
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
1 parent 598390a commit 9d0ab84

1 file changed

Lines changed: 82 additions & 83 deletions

File tree

mne/io/eyelink/tests/test_eyelink.py

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -201,89 +201,88 @@ def test_bino_to_mono(tmp_path, fname):
201201
out_file = tmp_path / "tmp_eyelink.asc"
202202
in_file = Path(fname)
203203

204-
with in_file.open() as file:
205-
# We'll also add some binocular velocity data to increase our testing coverage.
206-
lines = file.readlines()
207-
start_idx = [li for li, line in enumerate(lines) if line.startswith("START")][0]
208-
for li, line in enumerate(lines[start_idx:-2], start=start_idx):
209-
tokens = line.split("\t")
210-
event_type = tokens[0]
211-
if event_type == "SAMPLES":
212-
tokens.insert(3, "VEL")
213-
lines[li] = "\t".join(tokens)
214-
elif event_type.isnumeric():
215-
# fake velocity values for x/y left/right
216-
tokens[4:4] = ["999.1", "999.2", "999.3", "999.4"]
217-
lines[li] = "\t".join(tokens)
218-
end_line = lines[-2]
219-
end_ts = int(end_line.split("\t")[1])
220-
# Now only left eye data
221-
second_block = []
222-
new_ts = end_ts + 1
223-
info = [
224-
"GAZE",
225-
"LEFT",
226-
"VEL",
227-
"RATE",
228-
"500.00",
229-
"TRACKING",
230-
"CR",
231-
"FILTER",
232-
"2",
233-
]
234-
start = ["START", f"{new_ts}", "LEFT", "SAMPLES", "EVENTS"]
235-
pupil = ["PUPIL", "DIAMETER"]
236-
samples = ["SAMPLES"] + info
237-
events = ["EVENTS"] + info
238-
second_block.append("\t".join(start) + "\n")
239-
second_block.append("\t".join(pupil) + "\n")
240-
second_block.append("\t".join(samples) + "\n")
241-
second_block.append("\t".join(events) + "\n")
242-
# Some fake data.. # x, y, pupil, velicty x/y status
243-
left = ["960", "540", "0.0", "999.1", "999.2", "..."]
244-
NUM_FAKE_SAMPLES = 4000
245-
for ii in range(NUM_FAKE_SAMPLES):
246-
ts = new_ts + ii
247-
tokens = [f"{ts}"] + left
248-
second_block.append("\t".join(tokens) + "\n")
249-
# interleave some events into the second block
250-
duration = 500
251-
blink_ts = new_ts + 500
252-
end_blink = ["EBLINK", "L", f"{blink_ts}", f"{blink_ts + 50}", "106"]
253-
fix_ts = new_ts + 1500
254-
end_fix = [
255-
"EFIX",
256-
"L",
257-
f"{fix_ts}",
258-
f"{fix_ts + duration}",
259-
"1616",
260-
"1025.1",
261-
"580.9",
262-
"1289",
263-
]
264-
sacc_ts = new_ts + 2500
265-
end_sacc = [
266-
"ESACC",
267-
"L",
268-
f"{sacc_ts}",
269-
f"{sacc_ts + duration}",
270-
"52",
271-
"1029.6",
272-
"582.3",
273-
"581.7",
274-
"292.5",
275-
"10.30",
276-
"387",
277-
]
278-
second_block.append("\t".join(end_blink) + "\n")
279-
second_block.append("\t".join(end_fix) + "\n")
280-
second_block.append("\t".join(end_sacc) + "\n")
281-
end_ts = ts + 1
282-
end_block = ["END", f"{end_ts}", "SAMPLES", "EVENTS", "RES", "45", "45"]
283-
second_block.append("\t".join(end_block))
284-
lines += second_block
285-
with out_file.open("w") as file:
286-
file.writelines(lines)
204+
lines = in_file.read_text("utf-8").splitlines()
205+
# We'll also add some binocular velocity data to increase our testing coverage.
206+
start_idx = [li for li, line in enumerate(lines) if line.startswith("START")][0]
207+
for li, line in enumerate(lines[start_idx:-2], start=start_idx):
208+
tokens = line.split("\t")
209+
event_type = tokens[0]
210+
if event_type == "SAMPLES":
211+
tokens.insert(3, "VEL")
212+
lines[li] = "\t".join(tokens)
213+
elif event_type.isnumeric():
214+
# fake velocity values for x/y left/right
215+
tokens[4:4] = ["999.1", "999.2", "999.3", "999.4"]
216+
lines[li] = "\t".join(tokens)
217+
end_line = lines[-2]
218+
end_ts = int(end_line.split("\t")[1])
219+
# Now only left eye data
220+
second_block = []
221+
new_ts = end_ts + 1
222+
info = [
223+
"GAZE",
224+
"LEFT",
225+
"VEL",
226+
"RATE",
227+
"500.00",
228+
"TRACKING",
229+
"CR",
230+
"FILTER",
231+
"2",
232+
]
233+
start = ["START", f"{new_ts}", "LEFT", "SAMPLES", "EVENTS"]
234+
pupil = ["PUPIL", "DIAMETER"]
235+
samples = ["SAMPLES"] + info
236+
events = ["EVENTS"] + info
237+
second_block.append("\t".join(start) + "\n")
238+
second_block.append("\t".join(pupil) + "\n")
239+
second_block.append("\t".join(samples) + "\n")
240+
second_block.append("\t".join(events) + "\n")
241+
# Some fake data.. # x, y, pupil, velicty x/y status
242+
left = ["960", "540", "0.0", "999.1", "999.2", "..."]
243+
NUM_FAKE_SAMPLES = 4000
244+
for ii in range(NUM_FAKE_SAMPLES):
245+
ts = new_ts + ii
246+
tokens = [f"{ts}"] + left
247+
second_block.append("\t".join(tokens) + "\n")
248+
# interleave some events into the second block
249+
duration = 500
250+
blink_ts = new_ts + 500
251+
end_blink = ["EBLINK", "L", f"{blink_ts}", f"{blink_ts + 50}", "106"]
252+
fix_ts = new_ts + 1500
253+
end_fix = [
254+
"EFIX",
255+
"L",
256+
f"{fix_ts}",
257+
f"{fix_ts + duration}",
258+
"1616",
259+
"1025.1",
260+
"580.9",
261+
"1289",
262+
]
263+
sacc_ts = new_ts + 2500
264+
end_sacc = [
265+
"ESACC",
266+
"L",
267+
f"{sacc_ts}",
268+
f"{sacc_ts + duration}",
269+
"52",
270+
"1029.6",
271+
"582.3",
272+
"581.7",
273+
"292.5",
274+
"10.30",
275+
"387",
276+
]
277+
second_block.append("\t".join(end_blink) + "\n")
278+
second_block.append("\t".join(end_fix) + "\n")
279+
second_block.append("\t".join(end_sacc) + "\n")
280+
end_ts = ts + 1
281+
end_block = ["END", f"{end_ts}", "SAMPLES", "EVENTS", "RES", "45", "45"]
282+
second_block.append("\t".join(end_block))
283+
lines += second_block
284+
out_file.write_text("\n".join(lines), encoding="utf-8")
285+
287286
with pytest.warns(
288287
RuntimeWarning, match="Acquisition changed between monocular and"
289288
):

0 commit comments

Comments
 (0)