Skip to content

Conversation

@moritz-gerster
Copy link
Contributor

@moritz-gerster moritz-gerster commented Feb 23, 2023

Reference issue

Fixes #11501.

What does this implement/fix?

The order of bad channels no longer matters.

Note, that this is already correctly implemented in _ensure_infos_match:

mne-python/mne/io/meas_info.py

Lines 2903 to 2904 in 603a96d

if set(info1['bads']) != set(info2['bads']):
raise ValueError(f'{name}.info[\'bads\'] must match')

It's only wrong in _check_raw_compatibility(raw):

mne-python/mne/io/base.py

Lines 2501 to 2513 in 603a96d

def _check_raw_compatibility(raw):
"""Ensure all instances of Raw have compatible parameters."""
for ri in range(1, len(raw)):
if not isinstance(raw[ri], type(raw[0])):
raise ValueError(f'raw[{ri}] type must match')
for key in ('nchan', 'bads', 'sfreq'):
a, b = raw[ri].info[key], raw[0].info[key]
if a != b:
raise ValueError(
f'raw[{ri}].info[{key}] must match:\n'
f'{repr(a)} != {repr(b)}')
if not set(raw[ri].info['ch_names']) == set(raw[0].info['ch_names']):
raise ValueError('raw[%d][\'info\'][\'ch_names\'] must match' % ri)

moritz-gerster and others added 2 commits February 24, 2023 07:47
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
@moritz-gerster
Copy link
Contributor Author

moritz-gerster commented Feb 24, 2023

Shit I got confused by git. I hope I didn't mess everything up. I think some files were moved to the cloud during a commit, that might be why there are so many file changes. How can I fix this?

@larsoner
Copy link
Member

Yikes, a ton of files deleted :)

I looked through your commits and saw which one did this (bff61e8), and then did:

git revert bff61e8c0bb03b9385b7ebad548f66023cb650a9
git checkout bff61e8c0bb03b9385b7ebad548f66023cb650a9 mne/io/base.py
git commit -am 'FIX: Try this'
git push

Looks like the diff is now +24 −13 so probably closer to correct at least. Locally if you git pull origin compare_bads you should get to the same state, then you can fix whatever is still broken/missing!

mne/io/base.py Outdated
Comment on lines 2514 to 2520
bads_mismatch = bads0.symmetric_difference(badsi)
if bads_mismatch:
raise ValueError(f'raw[{i}][\'info\'][\'bads\'] do not match: '
f'{sorted(bads_mismatch)}')
chs0 = set(raw[0].info['ch_names'])
chsi = set(raw[i].info['ch_names'])
ch_mismatch = chs0.symmetric_difference(chsi)
Copy link
Member

Choose a reason for hiding this comment

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

This code also could be more DRY -- it should exist in a loop like

for kind in ('bads', 'ch_names'):

... but this also now does a symmetric diff on the channel names, which I don't think we've discussed yet. I actually don't think this will work -- I'd expect the user to need to do a minual reorder_channels before being able to concat successfully/correctly. So for ch_names the order does actually matter...

Copy link
Member

Choose a reason for hiding this comment

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

@moritz-gerster I think this comment has not been addressed. I do not think you should use set with channels like you do (now) for bads, so I think this chs part should be reverted / go back to old behavior, ideally with a test that fails if the channel orders do not match (it should IIRC!)

Copy link
Member

Choose a reason for hiding this comment

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

@moritz-gerster I think this comment has not been addressed. I'll un-resolve it.

I noticed on this read through that it's a set check on main so you can ignore that part -- but I think it could still be more DRY by using a loop, right?

@moritz-gerster
Copy link
Contributor Author

Looks like the diff is now +24 −13 so probably closer to correct at least. Locally if you git pull origin compare_bads you should get to the same state, then you can fix whatever is still broken/missing!

Thank you for your help! ☺️

@moritz-gerster
Copy link
Contributor Author

Dear @larsoner, are the changes good to go now? :)

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

diff looks good @moritz-gerster

any chance you can update or add a test for this?

@moritz-gerster
Copy link
Contributor Author

any chance you can update or add a test for this?

@agramfort: Check ✅

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

@moritz-gerster there is a already a place where concatenate_raws is tested. https://github.com/mne-tools/mne-python/blob/main/mne/io/fiff/tests/test_raw_fiff.py#L366

I know it's not the ideal place and it's there cause back in the days when this was added we were only supporting FIF files. Can you move your testing there to be consistent? Also rename your test test_concatenate_raws_bads_order

thanks

@moritz-gerster
Copy link
Contributor Author

Can you move your testing there to be consistent? Also rename your test test_concatenate_raws_bads_order

@agramfort done :)

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

Marking as requesting changes so we don't forget to undo the chs-as-set check

@moritz-gerster
Copy link
Contributor Author

@larsoner is there more I should do? :)

@agramfort
Copy link
Member

@moritz-gerster you need to fix conflicts

@moritz-gerster
Copy link
Contributor Author

@moritz-gerster you need to fix conflicts

@agramfort done ✅

@moritz-gerster moritz-gerster requested review from larsoner and removed request for sappelhoff March 16, 2023 08:02
Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

@larsoner I let you merge

@moritz-gerster
Copy link
Contributor Author

moritz-gerster commented Mar 19, 2023

@larsoner good to go now?

Some tests are failing but they don't seem to be related to my changes. They only failed after I synchronized the code with the main branch

@agramfort
Copy link
Member

@moritz-gerster lets wait for main to be green and lets merge this after its green again

@larsoner larsoner merged commit 0e305ee into mne-tools:main Mar 19, 2023
@larsoner
Copy link
Member

Thanks @moritz-gerster !

larsoner added a commit to larsoner/mne-python that referenced this pull request Mar 20, 2023
* upstream/main:
  Fix bug in new `mne.sys_info()` (mne-tools#11577)
  Remove legacy plot_psd* funcs/methods in our docs and codebase (mne-tools#11563)
  The order of raw1.info["bads"] should not matter when concatenating with raw0.info["bads"] mne-tools#11501 (mne-tools#11502)
  MAINT: Fixes for matplotlib and pandas (mne-tools#11574)
  Fix Circle [circle deploy]
  Fix export to EDF format with a set physical range smaller than the data range (mne-tools#11569)
  Slightly rework `mne.sys_info()` (mne-tools#11568)
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.

The order of raw1.info["bads"] should not matter when concatenating with raw0.info["bads"]

3 participants