Skip to content

[MRG] FIX/MAINT/ENH Covlove clip rank - #4637

Closed
dengemann wants to merge 25 commits into
mne-tools:masterfrom
dengemann:covlove_clip_rank
Closed

dengemann wants to merge 25 commits into
mne-tools:masterfrom
dengemann:covlove_clip_rank

Conversation

@dengemann

Copy link
Copy Markdown
Member

Work in progress & to be fine tuned.

But here is what I propose (pooling over the last months of discussion):

  1. detect the rank prior to cov estimation and clip the cov if the rank is < n_sensors.
    This should then stabilize the performance of plot_white
  2. compute SSSed covariance in a low dimensional space to save time and improve regularization.

cc @agramfort @larsoner

@codecov-io

codecov-io commented Oct 3, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #4637 into master will decrease coverage by 0.1%.
The diff coverage is 54.09%.

@@            Coverage Diff             @@
##           master    #4637      +/-   ##
==========================================
- Coverage   87.68%   87.57%   -0.11%     
==========================================
  Files         351      351              
  Lines       66613    66765     +152     
  Branches    11550    11597      +47     
==========================================
+ Hits        58407    58469      +62     
- Misses       5254     5328      +74     
- Partials     2952     2968      +16

@larsoner

larsoner commented Oct 3, 2017

Copy link
Copy Markdown
Member

These changes look too extensive for 0.15. I think we'll want to test for some months before unleashing on the public. 0.16 okay with you?

@larsoner larsoner added this to the 0.16 milestone Oct 3, 2017
@agramfort

agramfort commented Oct 3, 2017 via email

Copy link
Copy Markdown
Member

Comment thread mne/io/tests/test_proc_history.py Outdated
from mne.io import read_info, read_raw_fif
from mne.io.constants import FIFF
from mne.io.proc_history import _get_sss_rank
from mne import _get_rank_sss

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we should put this at the root level, it seems better buried in io.proc_history

@larsoner larsoner modified the milestones: 0.16, 0.15 Oct 3, 2017
@dengemann

dengemann commented Oct 3, 2017 via email

Copy link
Copy Markdown
Member Author

@larsoner

larsoner commented Oct 3, 2017

Copy link
Copy Markdown
Member

I think this solves a circrular import issue.

Probably better to solve it with a nested import

@dengemann

dengemann commented Oct 3, 2017 via email

Copy link
Copy Markdown
Member Author

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Computing covariance matrix
===========================

Many methods in MNE, including e.g. source estimation and some classification

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This broke CircleCI

ValueError: Example docstring should have a header for the example title and at least a paragraph explaining what the example is about. Please check the example file:
 plot_compute_covariance.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

wow, oops. this was not intended.

@dengemann dengemann changed the title [WIP][FIX/MAINT/ENH] Covlove clip rank [FIX/MAINT/ENH] Covlove clip rank Oct 5, 2017
@dengemann

Copy link
Copy Markdown
Member Author

@larsoner @larsoner @jasmainak this is good for review.

Comment thread mne/cov.py
picks_list_ = picks_list

for key, this_picks in picks_list_:
this_info = pick_info(info.copy(), this_picks)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pick_info has a copy param and I think it defaults to True

Comment thread mne/cov.py
if key == 'meg' and has_sss:
rank = min(rank, _get_rank_sss(info))
n_ssp = sum(pp['active'] for pp in info['projs'] if
_match_proj_type(pp, this_info['ch_names']))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think it depends on active IIRC we always apply proj at some point in the pipeline before applying the inverse

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

what do you mean @larsoner? This is what I needed to get it right IIRC. Any alternative suggestion?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm trying to figure out if you should just do sum(_match_proj_type(pp, this_info['ch_names'])). Think about the proj='delayed' use case or even epochs=Epochs(proj=False) (but will be applied later during inversion) will this code do the right thing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with @larsoner we always end up applying all proj in the inverse to me:

sum(_match_proj_type(pp, this_info['ch_names']))

seems like the right fix

Comment thread mne/cov.py Outdated

elif this_method == 'pca':
estimator_cov_info.append([sc, sc.covariance_, _info])
elif this_method == 'pca': # XXX this is now nonsensical

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what does this comment mean?

Comment thread mne/cov.py Outdated
estimator_cov_info.append([pca, pca.get_covariance(), _info])

elif this_method == 'factor_analysis':
elif this_method == 'factor_analysis': # XXX SSS case + EEG weird?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and this one? is it exposed by some test? do we need a doc update?

Comment thread mne/cov.py Outdated
# XXX we need a rank dict here and clip conditional upon
# low rank config.
if has_sss:
# XXX the logic is broken here for EEG

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks a bit like something we need to raise an error or warning for, or fix before release

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I should remove the XXX now....

Comment thread mne/evoked.py Outdated
time_label=time_label, n_jobs=n_jobs)

def plot_white(self, noise_cov, show=True):
def plot_white(self, noise_cov, rank=None, show=True):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be after show for backward compat

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do we really support API positional arguments?
I feel something like show should be the last one. And people should anyways not use positional arguments. The order of the purely positional arguments should be of course preserved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we really support API positional arguments?

Yes, at least for the first few inputs (say maybe 10?). In this case there were only two so I think it's clear.

I feel something like show should be the last one.

We'd need to do this in a separate PR

And people should anyways not use positional arguments.

I think it's reasonable to use positional arguments and expect API stability there. Imagine someone has code:

show = True  # can be toggled to false in iteration

...
evoked.plot_white(cov, show)

This seems fine to me, and this change will break it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't share this view as I always recommend to pass by keyword, but if you insist it's ok with me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes please

Comment thread mne/tests/test_cov.py Outdated

# but the rest is the same
assert_array_equal(
assert_array_almost_equal( # XXX make sure this is ok.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does this comment mean?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That assert_array_equal does not work here and that I don't know if this is expected or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd use assert_allclose with proper rtol and atol set based on the data of interest

noise_cov = mne.compute_raw_covariance(
raw_empty_room, tmin=0, tmax=None, method='diagonal_fixed')
noise_cov.nfree
###############################################################################

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

leftover from debug?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes

Comment thread tutorials/plot_compute_covariance.py Outdated

noise_cov = mne.compute_raw_covariance(
raw_empty_room, tmin=0, tmax=None, method='diagonal_fixed')
noise_cov.nfree

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

some explanation for diagonal_fixed choice would be good

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it was just debugging. I will remove it.

Comment thread tutorials/plot_compute_covariance.py Outdated
# file with :func:`mne.write_cov`. Later you can read it back to a python
# object using :func:`mne.read_cov`.
# Now that you the covariance matrix in an MNE-Python object you can save it
# to a file with :func:`mne.write_cov`. Later you can read it back

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Now that you have

@jasmainak

Copy link
Copy Markdown
Member

@dengemann I am sorry I need one more day before I can try it on biomag data ...

@agramfort

Copy link
Copy Markdown
Member

@dengemann you have some undesired commits in this PR (stuff already in master)

@agramfort

Copy link
Copy Markdown
Member

@dengemann you need to rebase and clean history...

@dengemann

dengemann commented Oct 9, 2017 via email

Copy link
Copy Markdown
Member Author

@dengemann dengemann changed the title [FIX/MAINT/ENH] Covlove clip rank [MRG] FIX/MAINT/ENH Covlove clip rank Oct 12, 2017
@larsoner

Copy link
Copy Markdown
Member

Travis failure is the unrelated OSX failure currently in master.

Can you add to whats_new.rst to summarize the change? Maybe toward the top of BUG so it's clear what's fixed. Otherwise LGTM!

@cbrnr

cbrnr commented Oct 13, 2017

Copy link
Copy Markdown
Contributor

Slightly OT, but do you think this can be merged ASAP to finalize 0.15 (if possible, please consider #4662, which is ready to go, but I'd argue for no further major delays)?

@larsoner

Copy link
Copy Markdown
Member

but do you think this can be merged ASAP to finalize 0.15

+1 yes I think this is all we are waiting for currently

raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(raw_fname)
raw.set_eeg_reference('average', projection=True)
raw.set_eeg_reference()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be reverted

@dengemann dengemann closed this Oct 16, 2017
@dengemann

Copy link
Copy Markdown
Member Author

Closing to split into mergeable and more-dev-to-do part.

@larsoner larsoner mentioned this pull request Oct 17, 2017
2 of 5 tasks
@dengemann dengemann mentioned this pull request Nov 6, 2018
12 tasks done
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.

6 participants