Skip to content

Commit 932b939

Browse files
committed
TST, FIX: make tests pass again
Actually we are now catching a case that we didn't before, which is good! That is why I adjusted test_multi_block_misc_channels, to make sure we continue to warn when the eye being tracked changes
1 parent 1c4cb15 commit 932b939

2 files changed

Lines changed: 47 additions & 19 deletions

File tree

mne/io/eyelink/_utils.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,7 @@ def _validate_data(data_blocks: list):
202202
)
203203
# Monocular tracking but switched between left/right eye
204204
elif len(set(eyes)) > 1:
205-
warn(
206-
"The eye being tracked changed during the"
207-
" recording. The channel names will reflect"
208-
" the eye that was tracked at the start of"
209-
" the recording."
210-
)
205+
warn("The eye being tracked changed during the recording.")
211206

212207

213208
def _get_recording_datetime(fname):
@@ -246,7 +241,13 @@ def _get_metadata(data_block: dict):
246241
meta_data = dict()
247242
rec_info = data_block["events"]["SAMPLES"][0]
248243
meta_data["unit"] = rec_info[0]
249-
meta_data["pupil_unit"] = data_block["events"]["PUPIL"][0][0]
244+
245+
# If the file doesn't have pupil data, i'm not sure if there will be any PUPIL info?
246+
if not data_block["events"]["PUPIL"]:
247+
ps_unit = None
248+
else:
249+
ps_unit = data_block["events"]["PUPIL"][0][0]
250+
meta_data["pupil_unit"] = ps_unit
250251
if ("LEFT" in rec_info) and ("RIGHT" in rec_info):
251252
meta_data["tracking_mode"] = "binocular"
252253
meta_data["eye"] = "both"
@@ -363,8 +364,7 @@ def _create_dataframes_for_block(block, apply_offsets):
363364
df_dict[label] = pd.DataFrame(block["events"][event])
364365
else:
365366
# Changed this from info to debug level to avoid spamming the log
366-
block_idx = block["info"]["block_idx"]
367-
logger.debug(f"No {label} events found in block {block_idx}")
367+
logger.debug(f"No {label} events found in block")
368368

369369
# make dataframe for experiment messages in this block
370370
if block["events"]["MSG"]:
@@ -464,19 +464,24 @@ def _combine_block_dataframes(processed_blocks: list[dict]):
464464

465465
# Determine unified column structure by collecting all unique column names
466466
# across all acquisition blocks
467-
all_ch_names = set()
467+
all_ch_names = []
468468
all_samples_cols = set()
469469
all_df_types = set()
470470

471471
for block in processed_blocks:
472-
all_ch_names.update(block["ch_names"])
472+
# The tests assume a certain order of channel names.
473+
# so we can't use a set like we do for the columns.
474+
# bc it randomly orders the channel names.
475+
for ch_name in block["ch_names"]:
476+
if ch_name not in all_ch_names:
477+
all_ch_names.append(ch_name)
478+
# all_ch_names.update(block["ch_names"])
473479
if "samples" in block["dfs"]:
474480
all_samples_cols.update(block["dfs"]["samples"].columns)
475481
all_df_types.update(block["dfs"].keys())
476482

477-
# Convert to sorted lists for consistent ordering
478-
unified_ch_names = sorted(all_ch_names)
479-
unified_samples_cols = sorted(all_samples_cols)
483+
# The sets randomly ordered the column names.
484+
all_samples_cols = sorted(all_samples_cols)
480485

481486
# Combine dataframes by type
482487
combined_dfs = {}
@@ -492,12 +497,12 @@ def _combine_block_dataframes(processed_blocks: list[dict]):
492497
# For samples dataframes, ensure all have the same columns
493498
if df_type == "samples":
494499
# Add missing columns with NaN
495-
for col in unified_samples_cols:
500+
for col in all_samples_cols:
496501
if col not in block_df.columns:
497502
block_df[col] = np.nan
498503

499504
# Reorder columns to match unified structure
500-
block_df = block_df[unified_samples_cols]
505+
block_df = block_df[all_samples_cols]
501506

502507
block_dfs.append(block_df)
503508

@@ -515,7 +520,7 @@ def _combine_block_dataframes(processed_blocks: list[dict]):
515520
blocks_data, columns=["time", "end_time", "block"]
516521
)
517522

518-
return combined_dfs, unified_ch_names
523+
return combined_dfs, all_ch_names
519524

520525

521526
def _drop_status_col(samples_df):

mne/io/eyelink/tests/test_eyelink.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,19 @@ def test_bino_to_mono(tmp_path, fname):
262262
lines += second_block
263263
with out_file.open("w") as file:
264264
file.writelines(lines)
265-
assert read_raw_eyelink(out_file)
265+
with pytest.warns(
266+
RuntimeWarning, match="Acquisition changed between monocular and"
267+
):
268+
raw = read_raw_eyelink(out_file)
269+
want_channels = [
270+
"xpos_left",
271+
"ypos_left",
272+
"pupil_left",
273+
"xpos_right",
274+
"ypos_right",
275+
"pupil_right",
276+
]
277+
assert len(set(raw.info["ch_names"]).difference(set(want_channels))) == 0
266278

267279

268280
def _simulate_eye_tracking_data(in_file, out_file):
@@ -323,13 +335,19 @@ def _simulate_eye_tracking_data(in_file, out_file):
323335
@requires_testing_data
324336
@pytest.mark.parametrize("fname", [fname_href])
325337
def test_multi_block_misc_channels(fname, tmp_path):
326-
"""Test an eyelink file with multiple blocks and additional misc channels."""
338+
"""Test a file with many edge casses.
339+
340+
This file has multiple acquisition blocks, each tracking a different eye.
341+
The coordinates are in raw units (not pixels or radians).
342+
It has some misc channels (head position, saccade velocity, etc.)
343+
"""
327344
out_file = tmp_path / "tmp_eyelink.asc"
328345
_simulate_eye_tracking_data(fname, out_file)
329346

330347
with (
331348
_record_warnings(),
332349
pytest.warns(RuntimeWarning, match="Raw eyegaze coordinates"),
350+
pytest.warns(RuntimeWarning, match="The eye being tracked changed"),
333351
):
334352
raw = read_raw_eyelink(out_file, apply_offsets=True)
335353

@@ -345,6 +363,11 @@ def test_multi_block_misc_channels(fname, tmp_path):
345363
"x_head",
346364
"y_head",
347365
"distance",
366+
"xpos_left",
367+
"ypos_left",
368+
"pupil_left",
369+
"xvel_left",
370+
"yvel_left",
348371
]
349372

350373
assert raw.ch_names == chs_in_file

0 commit comments

Comments
 (0)