Skip to content

Commit 41a12cb

Browse files
Wouter KrootWouter Kroot
authored andcommitted
Formatting time sort _utils for buttons
1 parent 8178b0f commit 41a12cb

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

mne/io/eyelink/_utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,23 @@ def _make_eyelink_annots(df_dict, create_annots, apply_offsets):
740740
this_annot = Annotations(
741741
onset=onsets, duration=durations, description=descriptions
742742
)
743-
elif (key in ["buttons"]) and (key in descs):
743+
elif (key == "buttons") and (key in descs):
744+
required_cols = {"time", "button_id", "button_pressed"}
745+
if not required_cols.issubset(df.columns):
746+
raise ValueError(f"Missing required columns for 'buttons': {required_cols - set(df.columns)}")
747+
748+
def get_button_description(row):
749+
try:
750+
button_id = int(row["button_id"])
751+
action = "press" if row["button_pressed"] == 1 else "release"
752+
return f"button_{button_id}_{action}"
753+
except Exception as e:
754+
raise ValueError(f"Invalid row for button description: {row}") from e
755+
df = df.sort_values("time") # Add this line
744756
onsets = df["time"]
745757
durations = np.zeros_like(onsets)
746-
descriptions = df.apply(
747-
lambda row: (
748-
f"button_{int(row['button_id'])}_"
749-
f"{'press' if row['button_pressed'] == 1 else 'release'}"
750-
),
751-
axis=1,
752-
)
758+
descriptions = df.apply(get_button_description, axis=1)
759+
753760
this_annot = Annotations(
754761
onset=onsets, duration=durations, description=descriptions
755762
)

0 commit comments

Comments
 (0)