sappelhoff / pyprep

PyPREP: A Python implementation of the Preprocessing Pipeline (PREP) for EEG data
https://pyprep.readthedocs.io/en/latest/
MIT License
136 stars 33 forks source link

add channels in raw.info['bads'] to unusable channels in Reference class #146

Closed john-veillette closed 2 weeks ago

john-veillette commented 4 months ago

Currently, PREP assumes you have no prior information about which channels are bad. This isn't necessarily a bad design choice if it's philosophically important that all bad channel identification be automatic, but currently it will result in an error if you have any channels marked as bad in raw.info['bads'] before running PREP.

The culprit re: the error is in Reference.__init__:

self.raw.pick_types(eeg=True, eog=False, meg=False)

Raw.pick_types will not include bad channels in its output by default, and since Reference inherits self.prep_params["ref_chs"] = self.ch_names_eeg from the PrepPipeline class that initializes it (where channels manually marked as bad haven't been removed yet), then the self.raw.get_data(picks=self.reference_channels) in Reference.robust_reference will throw a missing value error.

So a quick solution would be to change the offending line to

self.raw.pick_types(eeg=True, eog=False, meg=False, exclude=[])

so that manually marked bad channels are not unintentionally removed. But they should probably also be added to Reference.unusable_channels, so users have the ability to manually tell PREP to leave out certain channels (e.g. if they know the channel is broken in their hardware).

In the meantime, users can circumvent the problem by just setting raw.info['bads'] = [] before running PREP. Hopefully that saves someone some trouble :)

sappelhoff commented 4 months ago

So a quick solution would be to change the offending line to

self.raw.pick_types(eeg=True, eog=False, meg=False, exclude=[])

so that manually marked bad channels are not unintentionally removed. But they should probably also be added to Reference.unusable_channels, so users have the ability to manually tell PREP to leave out certain channels (e.g. if they know the channel is broken in their hardware).

Thanks for the report, I'd be happy to receive a PR for this.

john-veillette commented 4 months ago

Great, I'll submit a pull request as a soon as I have time!

sappelhoff commented 1 month ago

@john-veillette any news on your expected timeline for this?

john-veillette commented 1 month ago

Sorry @sappelhoff this slipped my mind, but I have time to work on it today so this week or next seems reasonable.

john-veillette commented 1 month ago

Okay, that didn't take long once I got to it. I've now opened PR #156 but some CI tests are failing.

Update: was just a formatting thing, fixed now!