Skip to content
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

Make ch_type reading more robust & support RESP #482

Merged
merged 7 commits into from Jul 22, 2020

Conversation

hoechenberger
Copy link
Member

PR Description

If mapping of channel types from BIDS to MNE fails, retry with all-uppercase channel types. Closes GH-481.
I anticipate this will only be a temporary thing and therefore didn't add a dedicated test for this. – However, it does come with a breaking change: we're not using a defaultdict for the mapping anymore.

This commit also adds support for mapping respiratory channels between BIDS and MNE.

Merge checklist

Maintainer, please confirm the following before merging:

  • All comments resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • PR description includes phrase "closes <#issue-number>"
  • Commit history does not contain any merge commits

If mapping of channel types from BIDS to MNE fails, retry with
all-uppercase channel types. Closes mne-toolsGH-481.
I anticipate this will only be a temporary thing and therefore didn't
add a dedicated test for this.

This commit also adds support for mapping respiratory channels
between BIDS and MNE.
@codecov-commenter
Copy link

codecov-commenter commented Jul 21, 2020

Codecov Report

Merging #482 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #482   +/-   ##
=======================================
  Coverage   93.70%   93.70%           
=======================================
  Files          11       11           
  Lines        1762     1763    +1     
=======================================
+ Hits         1651     1652    +1     
  Misses        111      111           
Impacted Files Coverage Δ
mne_bids/read.py 95.40% <100.00%> (+0.08%) ⬆️
mne_bids/utils.py 95.66% <100.00%> (-0.04%) ⬇️

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 e2798b3...81dd026. Read the comment docs.

@jasmainak
Copy link
Member

Why did you remove defaultdict?

@hoechenberger
Copy link
Member Author

hoechenberger commented Jul 21, 2020

@jasmainak

Why did you remove defaultdict?

I thought we had discussed that in #481 in the top comment, but actually I only posted a followup comment and talked about this with @agramfort on Slack.

From #481:

The ds001810 dataset, however, uses lowercase channel types, and hence read_raw_bids() assigns the type misc to all of them (and, later, somehow, they all beome eeg, but I'm not sure yet where this occurs).

Quoting my followup comment:

Forgot to add:
I'm not sure using a defaultdict() is the best idea here:

mne-bids/mne_bids/utils.py

Lines 460 to 464 in e2798b3

# Make it a defaultdict to prevent key errors
ch_type_mapping = defaultdict(lambda: default_value)
ch_type_mapping.update(map_chs)
return ch_type_mapping

I only noticed something must be wrong with my channels when trying to plot topomaps, and MNE complained about missing location info for some of the channels. I would have preferred to get an error message early on if a channel type cannot be mapped correctly from BIDS to MNE.

So what happened in my was that I somehow, silently, ended up with incorrect channel assignments due to the default dict (how exactly this happened I would still need to investigate)

@sappelhoff @adam2392 I would love to hear your view on this too :)

@sappelhoff
Copy link
Member

mmmh yes, it sounds like defaultdict is a dangerous code practice here. We should handle our cases explicitly and fail/warn early.

@@ -33,6 +33,8 @@ Changelog
- :func:`write_raw_bids` now adds citations to the README, by `Alex Rockhill`_ (`#463 <https://github.com/mne-tools/mne-bids/pull/463>`_)
- :func:`make_dataset_description` now has an additional parameter ``dataset_type`` to set the recommended field ``DatasetType`` (defaults to ``"raw"``), by `Stefan Appelhoff`_ (`#472 <https://github.com/mne-tools/mne-bids/pull/472>`_)
- :func:`mne_bids.copyfiles.copyfile_brainvision` now has an ``anonymize`` parameter to control anonymization, by `Stefan Appelhoff`_ (`#475 <https://github.com/mne-tools/mne-bids/pull/475>`_)
- :func:`mne_bids.read_raw_bids` and :func:`mne_bids.write_raw_bids` now map respiratory (``RESP``) channel types, by `Richard Höchenberger` (`#482 <https://github.com/mne-tools/mne-bids/pull/482>`_)
- :func:`mne_bids.read_raw_bids` now tries to correctly map channel types that are not in all-uppercase letters. Since this is in violation of BIDS, a warning is raised, by `Richard Höchenberger` (`#482 <https://github.com/mne-tools/mne-bids/pull/482>`_)
Copy link
Member

Choose a reason for hiding this comment

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

not sure if I would say this in the changelog ... i'd rather treat it silently. --> but let's see, we are not done discussing anyway, with Mainak's point that BIDS is agnostic at best, ambiguous at worst about casing

Copy link
Member Author

Choose a reason for hiding this comment

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

Another (temporary?) solution for MNE-BIDS could also be to always convert to uppercase when reading BIDS channel types, i.e.,

    for ch_name, ch_type in zip(ch_names_tsv, ch_types_json):
        updated_ch_type = bids_to_mne_ch_types.get(ch_type.upper(), None)  # <-- !!!

        if updated_ch_type is not None:
            channel_type_dict[ch_name] = updated_ch_type

Copy link
Member Author

Choose a reason for hiding this comment

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

@jasmainak WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

@sappelhoff
I've dropped this changelog entry.

Copy link
Member

Choose a reason for hiding this comment

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

I think we need to decide: 1) how specification treats the cases, 2) whether the validator is an accurate reflection of the specification, and 3) what we do in MNE-BIDS about it?

From a user perspective, I think it's nicer if the cases are taken care of in MNE-BIDS without complaining. E.g., through blah.upper() or blah.lower(). You have a similar situation with email addresses: you expect people to use the same casing but actually it doesn't matter. I would still have the validator be more whiny so that we are more forgiving to downstream applications that are not as careful as MNE-BIDS.

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.

merge when you're happy here.

I am really happy to see that actually using more and more BIDS dataset to do automatic processing we find all these issues !

@agramfort
Copy link
Member

@sappelhoff or @jasmainak feel free to merge

@jasmainak jasmainak merged commit 023b25d into mne-tools:master Jul 22, 2020
@jasmainak
Copy link
Member

thanks a ton @hoechenberger !

@hoechenberger hoechenberger deleted the ch_types branch July 22, 2020 17:41
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.

Should _get_ch_type_mapping() ignore case of input data?
5 participants