Skip to content

[WIP] Split PrepPipeline into separate methods, make final interpolation optional - #99

Closed
a-hurst wants to merge 12 commits into
sappelhoff:mainfrom
a-hurst:split_pipeline_methods
Closed

a-hurst wants to merge 12 commits into
sappelhoff:mainfrom
a-hurst:split_pipeline_methods

Conversation

@a-hurst

@a-hurst a-hurst commented Jul 3, 2021

Copy link
Copy Markdown
Collaborator

PR Description

(Eventually) closes #73. Currently, this PR:

  • Adds separate remove_line_noise and robust_reference methods to PrepPipeline.
  • Makes final bad channel interpolation optional.
  • Removes a bunch of unused matplotlib-based MATLAB comparison code in the PrepPipeline unit tests.
  • Removes support for notch filter types other than spectrum_fit, since you can now just filter line noise however you want and then run PrepPipeline.robust_reference if you want to use another kind. This vastly simplifies the demands on the filter_kwargs API and should make the PrepSettings filtering options much easier to document.

There's still definitely more cleanup to do, but I figured it'd be good to get the core of this up for review sooner than later!

Merge Checklist

  • the PR has been reviewed and all comments are resolved
  • all CI checks pass
  • (if applicable): the PR description includes the phrase closes #<issue-number> to automatically close an issue
  • (if applicable): bug fixes, new features, or API changes are documented in whats_new.rst

@a-hurst

a-hurst commented Jul 3, 2021

Copy link
Copy Markdown
Collaborator Author

Whoops, looks like one of the examples relies on an undocumented attribute I removed for the sake of RAM (self.EEG_new). Will address that tomorrow.

@codecov-commenter

codecov-commenter commented Jul 3, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #99 (0688a5c) into master (ec44384) will decrease coverage by 0.35%.
The diff coverage is 95.34%.

❗ Current head 0688a5c differs from pull request most recent head 6ff2755. Consider uploading reports for the commit 6ff2755 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
- Coverage   99.04%   98.68%   -0.36%     
==========================================
  Files           7        7              
  Lines         734      762      +28     
==========================================
+ Hits          727      752      +25     
- Misses          7       10       +3     
Impacted Files Coverage Δ
pyprep/reference.py 97.88% <90.00%> (-1.30%) ⬇️
pyprep/prep_pipeline.py 98.59% <100.00%> (-1.41%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ec44384...6ff2755. Read the comment docs.

@sappelhoff sappelhoff left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

thanks, it's great that we don't need the matplotlib stuff in the tests anymore. Also reminds me of #15

@sappelhoff

Copy link
Copy Markdown
Owner

It is possible that I introduced these conflicts 😖 it's because I worked on dbe2062 before pulling master ... the pulled master, rebased, and force pushed. That was a bad idea - I hope nothing serious got destroyed.

@a-hurst

a-hurst commented Jul 17, 2021

Copy link
Copy Markdown
Collaborator Author

It is possible that I introduced these conflicts 😖 it's because I worked on dbe2062 before pulling master ... the pulled master, rebased, and force pushed. That was a bad idea - I hope nothing serious got destroyed.

No worries, I'll rebase before I push any more work! I've set this aside for a few days while tackling some other projects, but I'll try and get this wrapped up within the next week or so :)

@sappelhoff sappelhoff mentioned this pull request Aug 2, 2021
@a-hurst
a-hurst force-pushed the split_pipeline_methods branch from 3e67ad7 to 0688a5c Compare August 2, 2021 22:42
@a-hurst
a-hurst force-pushed the split_pipeline_methods branch from 0688a5c to c4bb806 Compare August 4, 2021 21:37
@a-hurst

a-hurst commented Aug 4, 2021

Copy link
Copy Markdown
Collaborator Author

Okay @sappelhoff, I've gotten the PrepPipeline API rewrite to a point I think I'm happy with (though I'd love to hear your thoughts and @yjmantilla's). Some of the attributes aren't documented yet and much of it is untested, but I figured it'd be best to handle that side of things once I was sure we were all happy with the changes.

My main changes here are:

  • Renamed EEG_before_interpolation to EEG_post_reference and reference_before_interpolation to robust_reference_signal
  • Added a getter method current_reference_signal that returns reference_before_interpolation if interpolation hasn't been done, or the post-interpolation average reference (previously reference_after_interpolation) if it has (and None if neither have been performed, though that could be changed).
  • Moved all the loose noisy_channels_original, noisy_channels_before_interpolation, bad_before_interpolation, still_noisy_channels, etc. attributes into two dicts: noisy_info (which contains the full noisy channel dicts) and bad_channels (which just contains the bad channel names), each with the keys 'original', 'post-reference', and 'post-interpolation'. This makes things easier to document and has the advantage of a more consistent/informative naming scheme.
  • Added current_noisy_info and remaining_bad_channels attributes that get the current noisy info and bad channel names for the pipeline (will retrieve post-interpolation values if interpolation was used, otherwise returns the pre-interpolation values). These allow for people to try enabling/disabling interpolation for their data without having to rewrite their code to access different attributes.
  • Added a new get_raw method, which is a more flexible version of the current raw getter that allows retrieving the full mne.io.Raw object with the EEG data from any given stage in the pipeline.

Let me know what you think!

@sappelhoff
sappelhoff marked this pull request as ready for review August 5, 2021 08:19
Comment thread examples/run_full_prep.py Resolved
@sappelhoff

Copy link
Copy Markdown
Owner

I didn't take a look at the diff because I am not sure what diff will stay after #99 (comment) is resolved, but I still have concerns over these two points (I am commenting only based on your summary comment):

returns reference_before_interpolation if interpolation hasn't been done, or the post-interpolation average reference (previously reference_after_interpolation) if it has

and

(will retrieve post-interpolation values if interpolation was used, otherwise returns the pre-interpolation values

Could we not just return a dict in both cases, with the keys pre_interpolation and post_interpolation ... and the values for these keys being None or empty dicts if these values have not yet been computed? I think that'd be clearer. An alternative would be to send a log message upon the getter call that says something like: "you are getting the post_interpolation values". The minimum would be to have an attribute that can clearly tell you whether the prep object is "pre" or "post" interpolation, but we might already have something like that, not sure right now.

What do you think?

@a-hurst

a-hurst commented Aug 5, 2021

Copy link
Copy Markdown
Collaborator Author

Could we not just return a dict in both cases, with the keys pre_interpolation and post_interpolation ... and the values for these keys being None or empty dicts if these values have not yet been computed? I think that'd be clearer. An alternative would be to send a log message upon the getter call that says something like: "you are getting the post_interpolation values". The minimum would be to have an attribute that can clearly tell you whether the prep object is "pre" or "post" interpolation, but we might already have something like that, not sure right now.

That could work, though I still think it'd be useful to have an attribute like current_reference_signal so there's a consistent way to get the final reference signal regardless if interpolation was enabled or disabled. For the last project we did with PyPREP we tried the analysis pipeline both with and without final bad channel interpolation, so I think that having an API where you can toggle interpolation on and off without needing to change any attributes you access afterwards would be handy.

Instead of attributes, maybe methods in the style of the new get_raw function would be clearer? For example, a get_noisy_info method that returns the most recent noisy info available by default (like current_noisy_info does now), but also has an argument that lets you specify explicitly whether you want the original, post-reference, or post-interpolation noisy info.

Keep in mind that for the PrepPipeline API I decided against exposing interpolation as a separate method and instead made it an optional flag to robust_reference(), so it should hopefully be clear to users based on the settings they chose (and the documentation) whether the "current reference signal" reflects interpolated data or not.

@sappelhoff sappelhoff left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Instead of attributes, maybe methods in the style of the new get_raw function would be clearer? For example, a get_noisy_info method that returns the most recent noisy info available by default (like current_noisy_info does now), but also has an argument that lets you specify explicitly whether you want the original, post-reference, or post-interpolation noisy info.

yes! That sounds good to me, I like the new get_raw 👍 But I think the default should be "current", not "None", to be more explicit.

I approve of the remaining changes, but I am slowly losing my grasp on the potential workflows we can have with our different classes and their methods 😇 it's been too long that I actually worked with this code, I think.

Comment thread pyprep/prep_pipeline.py

Parameters
----------
stage : str, optional

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think I'd do something like this:

Suggested change
stage : str, optional
stage : {"unprocessed", "filtered", "post-reference", "post-interpolation"}, optional

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That causes the line to go beyond 88 characters, is line wrap for argument types something that's supported by Numpy docstyle?

Comment thread pyprep/prep_pipeline.py
Comment on lines +250 to +251
"Could not retrieve {stage} data, as that stage of the pipeline "
"has not yet been performed."

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
"Could not retrieve {stage} data, as that stage of the pipeline "
"has not yet been performed."
f"Could not retrieve {stage} data, as that stage of the pipeline "
"has not yet been performed."

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Whoops, nice catch!

@a-hurst

a-hurst commented Aug 7, 2021

Copy link
Copy Markdown
Collaborator Author

yes! That sounds good to me, I like the new get_raw 👍 But I think the default should be "current", not "None", to be more explicit.

Great! I'll get to work on this, as well as proper tests and docs for everything new. In that case, for the sake of simplicity I'm going to leave the internal noisy info and reference signal attributes undocumented so that the new get_x methods are the one clear official way for users to get at that data.

I approve of the remaining changes, but I am slowly losing my grasp on the potential workflows we can have with our different classes and their methods 😇 it's been too long that I actually worked with this code, I think.

Hopefully cleaning up the example scripts for 0.4 will refresh us all on that front!

@sappelhoff
sappelhoff changed the base branch from master to main October 27, 2023 09:55
sappelhoff added a commit that referenced this pull request Jun 13, 2026
* Make interpolation a separate Reference method

(cherry picked from commit 8ba133b)

* Add new separate methods for prep stages

(cherry picked from commit 5191d85)

* Keep staged PREP methods fully backward compatible

Adapt the staged-pipeline methods cherry-picked from Austin Hurst's
work (gh #99) so that no existing public behavior changes:

- remove_line_noise: restore the EEG_new/EEG_clean/EEG attributes and
  the original filter_kwargs semantics (None -> spectrum_fit defaults,
  otherwise use the kwargs verbatim, so e.g. method="fir" still works).
  line_freqs now defaults to prep_params["line_freqs"].
- robust_reference: max_iterations defaults to prep_params; warn with a
  UserWarning if line-noise removal was not performed first. fit() marks
  the line-noise stage as done even when line_freqs is empty, so the
  default pipeline never emits the warning.
- Reference.interpolate_bads: use `is None`/`is not None` sentinels in the
  guards so that finding zero bad channels (an empty list) no longer
  raises during the default interpolating path.

Co-authored-by: Austin Hurst <[email protected]>

* Test staged PREP methods and backward compatibility

- test_prep_pipeline_stages_match_fit: manual remove_line_noise +
  robust_reference is identical to fit().
- test_prep_pipeline_skip_interpolation: interpolate_bads=False leaves the
  post-interpolation outputs uncomputed while still detecting bads.
- test_robust_reference_warns_without_line_noise / _without_line_freqs: the
  out-of-order UserWarning fires when expected and never during fit().
- test_interpolate_bads_guards: Reference.interpolate_bads raises before
  referencing and on a repeated call.

Co-authored-by: Austin Hurst <[email protected]>

* Apply ruff formatting

Normalize formatting of the cherry-picked and newly added code to satisfy
the project's ruff-format gate.

Co-authored-by: Austin Hurst <[email protected]>

* Add staged-PREP example and changelog entry

Add an examples/run_prep_in_stages.py gallery example demonstrating the new
remove_line_noise / robust_reference staged API and skipping interpolation
via interpolate_bads=False, plus a changelog entry under 0.7.0.

Co-authored-by: Austin Hurst <[email protected]>

* Reference the PR number in the changelog entry

The changelog convention cites the PR number; the staged-PREP entry
mistakenly cited the issue number (#73) instead of the PR (#191).

Co-authored-by: Austin Hurst <[email protected]>

---------

Co-authored-by: Austin Hurst <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend PrepPipeline to allow performing PREP in stages

3 participants