From bf5b98097160ea76f0eaa50d10062b1f07f1cfd3 Mon Sep 17 00:00:00 2001 From: Wouter Kroot Date: Mon, 19 May 2025 12:20:18 +0200 Subject: [PATCH 1/2] Added button event parsing in _utils.py --- mne/io/eyelink/_utils.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/mne/io/eyelink/_utils.py b/mne/io/eyelink/_utils.py index 7ac03e088fd..457785c7a68 100644 --- a/mne/io/eyelink/_utils.py +++ b/mne/io/eyelink/_utils.py @@ -324,7 +324,20 @@ def _create_dataframes(raw_extras, apply_offsets): cols = ["time", "end_time", "block"] df_dict["recording_blocks"] = pd.DataFrame(blocks, columns=cols) - # TODO: Make dataframes for other eyelink events (Buttons) + # make dataframes for other button events + if raw_extras["event_lines"]["BUTTON"]: + button_events = raw_extras["event_lines"]["BUTTON"] + parsed = [] + for entry in button_events: + parsed.append({ + "onset": float(entry[0]), + "button_id": int(entry[1]), + "button_pressed": int(entry[2]) # 1 = press, 0 = release + }) + df_dict["buttons"] = pd.DataFrame(parsed) + else: + logger.info("No button events found in this file.") + return df_dict @@ -416,6 +429,10 @@ def _assign_col_names(col_names, df_dict): elif key == "messages": cols = ["time", "offset", "event_msg"] df.columns = cols + #added for buttons + elif key == "buttons": + cols = ["time", "button_id", "button_pressed"] + df.columns = cols return df_dict @@ -677,7 +694,7 @@ def _make_eyelink_annots(df_dict, create_annots, apply_offsets): "pupil_right", ), } - valid_descs = ["blinks", "saccades", "fixations", "messages"] + valid_descs = ["blinks", "saccades", "fixations", "buttons", "messages" ] msg = ( "create_annotations must be True or a list containing one or" f" more of {valid_descs}." @@ -720,9 +737,17 @@ def _make_eyelink_annots(df_dict, create_annots, apply_offsets): descriptions = df["event_msg"] this_annot = Annotations( onset=onsets, duration=durations, description=descriptions - ) + ) + elif (key in ["buttons"]) and (key in descs): + onsets = df["time"] + durations = np.zeros_like(onsets) + descriptions = df.apply( + lambda row: f"button_{int(row['button_id'])}_{'press' if row['button_pressed'] == 1 else 'release'}", axis=1) + this_annot = Annotations( + onset=onsets, duration= durations, description=descriptions + ) else: - continue # TODO make df and annotations for Buttons + continue if not annots: annots = this_annot elif annots: From fb9d5c80f5267e6e34322a8784489e5f459d618e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 19 May 2025 11:32:08 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/io/eyelink/_utils.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/mne/io/eyelink/_utils.py b/mne/io/eyelink/_utils.py index 457785c7a68..75c89a25c50 100644 --- a/mne/io/eyelink/_utils.py +++ b/mne/io/eyelink/_utils.py @@ -325,19 +325,21 @@ def _create_dataframes(raw_extras, apply_offsets): df_dict["recording_blocks"] = pd.DataFrame(blocks, columns=cols) # make dataframes for other button events - if raw_extras["event_lines"]["BUTTON"]: + if raw_extras["event_lines"]["BUTTON"]: button_events = raw_extras["event_lines"]["BUTTON"] parsed = [] for entry in button_events: - parsed.append({ - "onset": float(entry[0]), - "button_id": int(entry[1]), - "button_pressed": int(entry[2]) # 1 = press, 0 = release - }) + parsed.append( + { + "onset": float(entry[0]), + "button_id": int(entry[1]), + "button_pressed": int(entry[2]), # 1 = press, 0 = release + } + ) df_dict["buttons"] = pd.DataFrame(parsed) else: logger.info("No button events found in this file.") - + return df_dict @@ -429,7 +431,7 @@ def _assign_col_names(col_names, df_dict): elif key == "messages": cols = ["time", "offset", "event_msg"] df.columns = cols - #added for buttons + # added for buttons elif key == "buttons": cols = ["time", "button_id", "button_pressed"] df.columns = cols @@ -694,7 +696,7 @@ def _make_eyelink_annots(df_dict, create_annots, apply_offsets): "pupil_right", ), } - valid_descs = ["blinks", "saccades", "fixations", "buttons", "messages" ] + valid_descs = ["blinks", "saccades", "fixations", "buttons", "messages"] msg = ( "create_annotations must be True or a list containing one or" f" more of {valid_descs}." @@ -737,17 +739,19 @@ def _make_eyelink_annots(df_dict, create_annots, apply_offsets): descriptions = df["event_msg"] this_annot = Annotations( onset=onsets, duration=durations, description=descriptions - ) - elif (key in ["buttons"]) and (key in descs): + ) + elif (key in ["buttons"]) and (key in descs): onsets = df["time"] durations = np.zeros_like(onsets) descriptions = df.apply( - lambda row: f"button_{int(row['button_id'])}_{'press' if row['button_pressed'] == 1 else 'release'}", axis=1) + lambda row: f"button_{int(row['button_id'])}_{'press' if row['button_pressed'] == 1 else 'release'}", + axis=1, + ) this_annot = Annotations( - onset=onsets, duration= durations, description=descriptions - ) + onset=onsets, duration=durations, description=descriptions + ) else: - continue + continue if not annots: annots = this_annot elif annots: