Skip to content

ENH: Add description filter parameter to Raw.crop_by_annotations() #13743

@aman-coder03

Description

@aman-coder03

Describe the new feature or enhancement

Raw.crop_by_annotations()currently crops the raw data for every annotation with no way to filter by description. If a recording has multiple annotation types like "stimulus","bad","response"...there is no way to crop only a specific type without a messy workaround

it would be much cleaner to do...

raws = raw.crop_by_annotations(description="stimulus")
#or multiple types
raws = raw.crop_by_annotations(description=["stimulus", "response"])

Describe your proposed implementation

adding a description parameter to crop_by_annotations() in mne/io/base.py. When description=None (the default), the method behaves exactly as it does today, so there is no API breakage.

when a description is provided, annotations are filtered before cropping...

if description is not None:
    if isinstance(description, str):
        description = [description]
    mask = np.isin(annotations.description, description)
    annotations = annotations[mask]

mne/annotations.py already has a private function_select_annotations_based_on_description() used internally by
events_from_annotations() that does exactly this kind of filtering, so the pattern is already established in the codebase

Describe possible alternatives

users can already work around this manually..

mask = raw.annotations.description == "stimulus"
raw.crop_by_annotations(annotations=raw.annotations[mask])

But this is verbose and inconsistent with how other mne methods handle description filtering. Adding the parameter directly to the method is cleaner and more ergonomic for the common use case

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions