Repository navigation
MRG+1: check info against compensation chans - #5133
Conversation
There was a problem hiding this comment.
The main difference from what we discussed is that I removed the check_comp flags, as that mechanism seemed like something that could start to be abused and would lead to headaches later.
Agreed. A different option would be not to add it to pick_info but rather have pick_info wrap immediately to a new private _pick_info where it is exposed. Then it will only be used by us internally e.g. in _pick_drop_channels.
But this is (probably?) functionally equivalent to the stuff you have now, so either way is fine with me.
| from ..io.compensator import get_current_comp | ||
| from ..io.constants import FIFF | ||
| from ..io.meas_info import anonymize_info, Info | ||
| from ..io.meas_info import anonymize_info, Info, _bad_chans_comp |
There was a problem hiding this comment.
you will have a flake error here, it's unused
| # should this be thought of as a bug? | ||
| return False, [] | ||
|
|
||
| # only include compensation channels that would effect selected channels |
| comp_names.extend(comp['data']['col_names']) | ||
| comp_names = sorted(set(comp_names)) | ||
|
|
||
| missing_ch_names = list(set(comp_names).difference(ch_names)) |
There was a problem hiding this comment.
sorted here is a bit better than list
| assert ret == expected_result | ||
| assert len(missing) == 17 | ||
| if comp != 0: | ||
| pytest.raises(RuntimeError, Epochs, raw, events, None, -0.2, 0.2, |
There was a problem hiding this comment.
FYI the "modern" way to do this is:
with pytest.raises(RuntimeError, match='something from the error string'):
Epochs(...)
It is a bit more precise than using pytest.raises as a drop-in for assert_raises
| raw_ctf = read_raw_fif(fname_ctf_raw) | ||
| raw_ctf.apply_gradient_compensation(2) | ||
| infos.append(raw_ctf.info) | ||
| # infos.append(raw_ctf.info) |
There was a problem hiding this comment.
Revert or remove? Probably remove based on the "directly" comment below
| this_ch['ch_name'] = clean_ch_names[ii] | ||
| info['bads'] = _clean_names(info['bads']) | ||
| for comp in info['comps']: | ||
| comp['data']['col_names'] = _clean_names(comp['data']['col_names']) |
There was a problem hiding this comment.
This will modify the object inplace. Do we know that it has been copied already everywhere _prepare_topo_plot is used?
Is there some new test that shows why this is necessary (i.e., fails on master but passes here)?
There was a problem hiding this comment.
info is a copy see https://github.com/mne-tools/mne-python/blob/master/mne/viz/topomap.py#L37
mne/viz/tests/test_topomap.py::test_ctf_plotting currently fails without this line.
I can probably eliminate this line if we drop the comp matrices from the info before calling _prepare_topo_plot. I imagine this is also a problem from the other topomap plotting functions- currently only plot_evoked_topomap drops the comp matrices. I'll look at others
|
Skipping the check ( WIth this model, if a developer is confident that the comp mats aren't going to be used they should remove them from the info. |
Codecov Report
@@ Coverage Diff @@
## master #5133 +/- ##
==========================================
+ Coverage 88.18% 88.21% +0.03%
==========================================
Files 358 358
Lines 65944 66143 +199
Branches 11210 11245 +35
==========================================
+ Hits 58150 58347 +197
- Misses 4958 4963 +5
+ Partials 2836 2833 -3 |
|
What do I need to do to run tests that depend on MNE-C locally? And is it possible to look at my old travis results? they disappeared with my [ci skip] commit? |
download and install/configure MNE-C
Yes, scroll up to your previous commit on GitHub and you'll see a red X, click on it and it will show the CI statuses including Travis. You can also go to the Travis site and navigate to it. But here is the link: |
|
I have 2 questions regarding the last commit (0c67672 )
Base on this information i remove the comp matrices from the info object so that it is self consistent. |
|
ignoring my poor spelling.... I'll see if it works on a different setup during the week. FYI locally The mne command that executes is
This command completes creating |
|
I pushed a commit that fixes the problem. It was pretty straightforward, feel free to look. But now that we have several places where we need this |
You can provide a
It looks like |
|
All better except the spelling :) |
| compchs = pick_info(info, picks)['chs'] | ||
| comp_info = info.copy() | ||
| comp_info['comps'] = [] | ||
| compchs = pick_info(comp_info, picks)['chs'] |
There was a problem hiding this comment.
I don't think these changes are needed. that pick_info should be fine. ref_meg=True so unless the compensation matrices depend on non reference channels this should pass.
| comps, info['comps'] = info['comps'], [] # avoid picking errors | ||
| info = pick_info(info, pick_types(info, meg=meg, eeg=eeg, ref_meg=False, | ||
| exclude=[])) | ||
| info['comps'] = comps |
There was a problem hiding this comment.
I'm torn here.
I like backward compatibility and comps have been retained in fwd.info till now.
However, this code will create an info object that will not pass info._check_consistency() in comp matrices are present causing potential issues on reading/writing or other serializations.
There was a problem hiding this comment.
It depends on when _check_consistency gets called, and it must not for forward, otherwise tests would not pass (assuming we do an I/O test for CTF, which I hope we do!).
If we add a consistency check we can make an exception like ._check_consistency(check_comps=False)
There was a problem hiding this comment.
Nope no I/O tests for CTF forwards with compensation mats.
|
I think the core question is this: Do we think that in order for a meas_info object that has compensation matrices ( If the answer is no then we can remove the hard check from If the answer is yes (and I fall into this group) we could add a just my 2cnts, I'll be happy to implement either path. |
|
But there is also an API issue here: if you put it in How about we combine 1 and 2. Add |
|
I think the problem with so for forward objects this means using An alternative is to only create infos that pass |
|
Hmm. It does seem simplest to drop |
|
Additionally, I'm now concerned about testing coverage with CTF data. For instance if we had merged following 3b5f4b0 (which had a green dashboard) we wouldn't have been able call |
|
+1 for gradually increasing coverage, feel free to do it here if you want |
|
@bloyl is this good to go from your end? FYI we are finally really pushing to release, hopefully this week. |
|
if there is no what's new update needed +1 for MRG |
|
I think it fits in with what we have already done for |
|
This currently breaks ICA for CTF data. I'll have a commit in a few minutes. |
|
With the latest commits - this PR makes it through on of my analysis scripts |
| write_forward_solution(fname_temp, fwd_py) | ||
| fwd_py2 = read_forward_solution(fname_temp) | ||
| _compare_forwards(fwd_py, fwd_py2, 274, n_src) | ||
| fwd_py.__repr__() # __repr__ also calls _check_consistency |
There was a problem hiding this comment.
For future reference repr(...) is a slightly cleaner way to do this (but don't need to change here)
larsoner
left a comment
There was a problem hiding this comment.
Can you write an update to the API section, since we are technically changing the behavior of forward such that both new computations and round-trip I/O with old CTF forward (at least with my suggestion/comment below) will now be different?
| raise RuntimeError('No MEG or EEG channels found.') | ||
|
|
||
| # pick out final info | ||
| info['comps'] = [] |
There was a problem hiding this comment.
I suspect you'll also need one of these in read_forward_solution. If in <= 0.15 someone computed a CTF forward, it will have this field, so I think they will get an error on your assert( statement in inverse.py. Right?
There was a problem hiding this comment.
I can certainly add it. I don't think it will ever be needed as from looking at
_read_forward_meas_info https://github.com/mne-tools/mne-python/blob/master/mne/forward/forward.py#L249 It doesn't look like comps is ever set from anything.
do you want it to be silent or issue a RuntimeWarning
There was a problem hiding this comment.
You're right -- I just tried creating a CTF forward on master, writing to disk, reading it, and fwd['info']['comps'] does indeed go away. So nothing to do here, I think.
There was a problem hiding this comment.
what do you think of the following at https://github.com/mne-tools/mne-python/blob/master/mne/forward/forward.py#L501
# remove comp matrcies.
if fwd['info']['comps']:
fwd['info']['comps'] = []
warn('Removing compensation matrices found in measurement info ' +
'of forward operator', UserWarning)
There was a problem hiding this comment.
you do not need the + and I would make it a bit more explanatory "..., this should not affect application of the forward model but it will change the model if it's written back to disk" or so, to give people a sense of what the consequences are
There was a problem hiding this comment.
Ok no change.
Do you still want an update to the api section?
There was a problem hiding this comment.
I think that we don't need it anymore
| with pytest.raises(RuntimeError, match='Compensation grade of ICA'): | ||
| ica.apply(inst) | ||
| with pytest.raises(RuntimeError, match='Compensation grade of ICA'): | ||
| ica.get_sources(inst) |
There was a problem hiding this comment.
How long does this take to run? Can we set the number of iterations to some tiny number (1 or 2) to make it faster, since we don't actually care if the decomp is any good?
There was a problem hiding this comment.
test_ica::test_ica_ctf takes 6 seconds with max_iter=2 and 12 seconds without the default max_iter
There was a problem hiding this comment.
That's a good speedup, let's add it
|
Examples look reasonable to me, feel free to revert the CircleCI-related changes if you want |
|
|
||
| # make sure comps matrices was not removed from raw | ||
| if not raw.info['comps']: | ||
| raise RuntimeError('Comps matrices removed') |
There was a problem hiding this comment.
This can just be assert raw.info['comps']
|
@bloyl what else is left to do here? There are only a few PRs blocking release, it would be good to include this one. |
|
i'm done. The things I have run have worked. So I think its good to go. |
| # | ||
| fwd['info'] = _read_forward_meas_info(tree, fid) | ||
|
|
||
| # remove comp matrcies. |
| write_forward_solution(fname_temp, fwd_py) | ||
| fwd_py2 = read_forward_solution(fname_temp) | ||
| _compare_forwards(fwd_py, fwd_py2, 274, n_src) | ||
| fwd_py.__repr__() # __repr__ also calls _check_consistency |
| raise ValueError('Data input must be of Raw, Epochs or Evoked ' | ||
| 'type') | ||
|
|
||
| # confirm ica.compensation_grade == inst.compensation_Grade |
| raise ValueError('Data input must be of Raw, Epochs or Evoked ' | ||
| 'type') | ||
|
|
||
| # confirm ica.compensation_grade == inst.compensation_Grade |
| raise ValueError('Data input must be of Raw, Epochs or Evoked ' | ||
| 'type') | ||
|
|
||
| # confirm ica.compensation_grade == inst.compensation_Grade |
There was a problem hiding this comment.
Small g
You should avoid the code dupes below
|
@bloyl can you address my comments asap so we can release in the next few days? thx heaps |
|
thx @bloyl |
* ensure comp channels are retained through picked etc. * make compenstation checks mandatory on all infos * move error message to pick_info * add read/write ctf fwd test with compchans * FIX ica and comp_chans * Fix: prepare_forward + prepare_cov
| in zip(n_epochs, data_mean)] | ||
|
|
||
| if info['comps']: | ||
| info['comps'] = [] |
There was a problem hiding this comment.
this does an in-place modification of the info. Is this desired? compute_covariance should not change epochs.info IMO. It led me to a long debugging session with a colleague ...
There was a problem hiding this comment.
That does sound like a bug, possibly avoidable with info = info.copy() in a line above
@larsoner, take a look at this.
The main difference from what we discussed is that I removed the check_comp flags, as that mechanism seemed like something that could start to be abused and would lead to headaches later.
Closes #5127