-
Notifications
You must be signed in to change notification settings - Fork 70
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
WIP: ICA clean raw data #296
Closed
Closed
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f146494
add supports for autoreject and rest sessions
80a2cb4
add few suggested modifications
b4eed0d
tiny fixes
agramfort 5cc2c15
Merge branch 'main' into rest_support
agramfort e7ff1f5
few modifications
2f08f07
little oversight
af1c120
Merge branch 'rest_support' of https://github.com/apmellot/mne-bids-p…
MerlinDumeur 8bf7eea
Added possibility to clean raw data using ICA
MerlinDumeur 59907cb
removed autoreject code
MerlinDumeur abf6d05
Fixed config
MerlinDumeur 1c70e09
Recreate epochs for ICA, fix logging
MerlinDumeur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,17 +45,34 @@ def apply_ica(subject, session): | |
root=config.deriv_root, | ||
check=False) | ||
|
||
fname_epo_in = bids_basename.copy().update(suffix='epo', extension='.fif') | ||
fname_epo_out = bids_basename.copy().update( | ||
suffix='epo', processing='clean', extension='.fif') | ||
if config.no_epoching: | ||
|
||
fname_in = { | ||
run: | ||
bids_basename.copy().update(run=run, processing='filt', | ||
suffix='raw', check=False, | ||
extension='.fif') | ||
for run in config.get_runs() | ||
} | ||
|
||
fname_out = { | ||
run: | ||
bids_basename.copy().update(run=run, processing='clean', | ||
suffix='raw', check=False, | ||
extension='.fif') | ||
for run in config.get_runs() | ||
} | ||
|
||
else: | ||
fname_in = bids_basename.copy().update(suffix='epo', extension='.fif') | ||
fname_out = bids_basename.copy().update( | ||
suffix='epo', processing='clean', extension='.fif') | ||
|
||
fname_ica = bids_basename.copy().update(suffix='ica', extension='.fif') | ||
fname_ica_components = bids_basename.copy().update( | ||
processing='ica', suffix='components', extension='.tsv') | ||
|
||
# Load epochs to reject ICA components. | ||
epochs = mne.read_epochs(fname_epo_in, preload=True) | ||
|
||
msg = f'Input: {fname_epo_in}, Output: {fname_epo_out}' | ||
msg = f'Input: {fname_in}, Output: {fname_out}' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
session=session)) | ||
|
||
|
@@ -76,41 +93,69 @@ def apply_ica(subject, session): | |
.loc[tsv_data['status'] == 'bad', 'component'] | ||
.to_list()) | ||
|
||
# Compare ERP/ERF before and after ICA artifact rejection. The evoked | ||
# response is calculated across ALL epochs, just like ICA was run on | ||
# all epochs, regardless of their respective experimental condition. | ||
# | ||
# Note that up until now, we haven't actually rejected any ICs from the | ||
# epochs. | ||
evoked = epochs.average() | ||
|
||
# Plot source time course | ||
fig = ica.plot_sources(evoked, show=config.interactive) | ||
report.add_figs_to_section(figs=fig, | ||
captions='All ICs - Source time course') | ||
|
||
# Plot original & corrected data | ||
fig = ica.plot_overlay(evoked, show=config.interactive) | ||
report.add_figs_to_section(figs=fig, | ||
captions=f'Evoked response (across all epochs) ' | ||
f'before and after cleaning via ICA ' | ||
f'({len(ica.exclude)} ICs removed)') | ||
report.save(report_fname, overwrite=True, open_browser=False) | ||
|
||
# Now actually reject the components. | ||
msg = f'Rejecting ICs: {ica.exclude}' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
session=session)) | ||
epochs_cleaned = ica.apply(epochs.copy()) # Copy b/c works in-place! | ||
epochs_cleaned.apply_baseline(config.baseline) | ||
if config.no_epoching: | ||
|
||
msg = 'Saving cleaned epochs.' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
session=session)) | ||
epochs_cleaned.save(fname_epo_out, overwrite=True) | ||
msg = f'Rejecting ICs: {ica.exclude}' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be logged for both cases, i.e. should be outside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep that's what I intended to do, will fix |
||
session=session)) | ||
|
||
# Load and apply ICA to each run separately | ||
for run in fname_in: | ||
|
||
raw = mne.io.read_raw_fif(fname_in[run], preload=True) | ||
raw_cleaned = ica.apply(raw.copy()) # Not sure if copy needed | ||
|
||
msg = 'Saving cleaned raw.' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
session=session)) | ||
raw_cleaned.save(fname_out[run], overwrite=True) | ||
|
||
fig = ica.plot_sources(raw, show=config.interactive) | ||
report.add_figs_to_section(figs=fig, captions='All ICs - run ' | ||
f'{run}') | ||
|
||
# Does it make sense to plot_overlay for raw data? I don't think so | ||
|
||
report.save(report_fname, overwrite=True, open_browser=False) | ||
|
||
else: | ||
|
||
# Load epochs to reject ICA components. | ||
epochs = mne.read_epochs(fname_in, preload=True) | ||
|
||
# Compare ERP/ERF before and after ICA artifact rejection. The evoked | ||
# response is calculated across ALL epochs, just like ICA was run on | ||
# all epochs, regardless of their respective experimental condition. | ||
# | ||
# Note that up until now, we haven't actually rejected any ICs from the | ||
# epochs. | ||
evoked = epochs.average() | ||
|
||
# Plot source time course | ||
fig = ica.plot_sources(evoked, show=config.interactive) | ||
report.add_figs_to_section(figs=fig, | ||
captions='All ICs - Source time course') | ||
|
||
# Plot original & corrected data | ||
fig = ica.plot_overlay(evoked, show=config.interactive) | ||
report.add_figs_to_section(figs=fig, | ||
captions=f'Evoked response (across all ' | ||
f'epochs) before and after ' | ||
f'cleaning via ICA ' | ||
f'({len(ica.exclude)} ICs removed)' | ||
) | ||
report.save(report_fname, overwrite=True, open_browser=False) | ||
|
||
epochs_cleaned = ica.apply(epochs.copy()) # Copy b/c works in-place! | ||
epochs_cleaned.apply_baseline(config.baseline) | ||
|
||
msg = 'Saving cleaned epochs.' | ||
logger.info(gen_log_message(message=msg, step=5, subject=subject, | ||
session=session)) | ||
epochs_cleaned.save(fname_out, overwrite=True) | ||
|
||
if config.interactive: | ||
epochs_cleaned.plot_image(combine='gfp', sigma=2., cmap="YlGnBu_r") | ||
if config.interactive: | ||
epochs_cleaned.plot_image(combine='gfp', sigma=2., cmap="YlGnBu_r") | ||
|
||
|
||
def main(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The base code recreates an epoch object, but I don't understand why. Here I just reuse the epochs created last step, not sure if that's okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we filter the raw data again with a high-pass filter suitable for ICA. We then create new epochs from the newly-filtered data. You'd have to do the same for the "no epoching" case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely missed that, thanks