Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eager dtype conversion of value column #1353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions mne_bids/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ def _handle_events_reading(events_fname, raw):
logger.info(f"Reading events from {events_fname}.")
events_dict = _from_tsv(events_fname)

# drop events where onset is n/a
# drop events where onset is n/a; we can't annotate them and thus don't need entries
# for them in event_id either
events_dict = _drop(events_dict, "n/a", "onset")

# Get event descriptions. Use `trial_type` column if available.
Expand Down Expand Up @@ -574,12 +575,23 @@ def _handle_events_reading(events_fname, raw):
new_name = f"{trial_type}/{value}"
logger.info(f" Renaming event: {trial_type} -> {new_name}")
trial_types[ii] = new_name
# drop rows where `value` is `n/a` & convert remaining `value` to int (only
# when making our `event_id` dict; `value = n/a` doesn't prevent annotation)
# make a copy with rows dropped where `value` is `n/a` (only for making our
# `event_id` dict; `value = n/a` doesn't prevent making annotations).
culled = _drop(events_dict, "n/a", "value")
event_id = dict(
zip(culled[trial_type_col_name], np.asarray(culled["value"], dtype=int))
)
# Often (but not always!) the `value` column was written by MNE-BIDS and
# represents integer event IDs (as would be found in MNE-Python events
# arrays / event_id dicts). But in case not, let's be defensive:
culled_vals = culled["value"]
try:
culled_vals = np.asarray(culled_vals, dtype=float)
except ValueError:
pass
Copy link
Contributor

@scott-huberty scott-huberty Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pass
# If the values are not numeric, just use the row index as the event ID
culled_vals = np.arange(len(culled_vals))

else:
try:
culled_vals = culled_vals.astype(int)
except ValueError:
pass
event_id = dict(zip(culled[trial_type_col_name], culled_vals))
else:
event_id = dict(zip(trial_types, np.arange(len(trial_types))))
descrs = np.asarray(trial_types, dtype=str)
Expand Down
Loading