Skip to content

Conversation

@massich
Copy link
Contributor

@massich massich commented Jan 17, 2019

This PR allows to use stim_channel again when calling mne.io.read_raw_edf.

Reference issue

Fixes #5738

What does this implement/fix?

  • Allows to use stim_channel
  • Updates the deprecation warnings
  • Updates docstrings
More
  • What to do with ints
  • Allow for multiple stim_channels
  • Warn 'auto' in channel names

Additional information

The test file has been created using this code.
Most probably we want to review all the files we are shipping and modify them to do the testing. Even some of the test files
we are not using them.

@massich massich changed the title EDF stim_channels [WIP] EDF stim_channels Jan 17, 2019
@massich massich added the backport-candidate on-merge: backport to maint/1.11 label Jan 17, 2019
@massich
Copy link
Contributor Author

massich commented Jan 17, 2019

After writting the thest I think that stim_channel should take a list of strings rather than string.
It has 2 advangages:
1 - If I accept a string, then I've to differenciate 'auto' from 'auto' as a really poor ch_name choice.
2 - If we load a bunch of files, they can have different stim names and I can load them with a single call.

and stim_channel=['my name'] is not that bad.

@codecov
Copy link

codecov bot commented Jan 17, 2019

Codecov Report

Merging #5841 into master will increase coverage by 0.01%.
The diff coverage is 86.88%.

@@            Coverage Diff             @@
##           master    #5841      +/-   ##
==========================================
+ Coverage   88.63%   88.64%   +0.01%     
==========================================
  Files         373      373              
  Lines       69317    69468     +151     
  Branches    11665    11727      +62     
==========================================
+ Hits        61436    61580     +144     
- Misses       5030     5038       +8     
+ Partials     2851     2850       -1

@cbrnr
Copy link
Contributor

cbrnr commented Jan 17, 2019

I'm -1 on having to supply a list, because at least in my use cases I have only one stim channel. But since we also accept a list, we could resolve this ambiguity by issuing a warning if the user passes 'auto', saying that if the channel is really called 'auto' it must be passed in a list (i.e. ['auto']).

@larsoner
Copy link
Member

I wouldn't worry about the 'auto' channel use case for now (YAGNI) and let's allow str->list of str automatically, we do this many other places

@agramfort
Copy link
Member

agramfort commented Jan 17, 2019 via email

@larsoner
Copy link
Member

eog, misc etc. can be lists so stim_channel could be a list and it would make explicit the default valid options that get mapped automatically to stim channels.

If we do stim_channel=('STATUS', 'Status', ...) default (what I think you are proposing?) there are a couple of drawbacks

  • we lose the ability to raise an error if something in stim_channel is not actually a channel, unless we special-case the default itself before checking for existence
  • the signature would become quite long

Neither 'auto' nor ('STATUS', 'Status', ...) default is perfect, and I'm fine with either. I lean +0.5 for 'auto' based on the relative drawbacks of both and what we've tended to do elsewhere. But in either case, I think we should treat stim_channel='foo' the same way as stim_channel=['foo'], i.e. have a if isinstance(stim_channel, str): stim_channel = [stim_channel] in there.

@agramfort
Copy link
Member

agramfort commented Jan 17, 2019 via email

@cbrnr
Copy link
Contributor

cbrnr commented Jan 18, 2019

I agree that we should treat a string the same way as a list with a single string. But we need to make sure to issue a warning if there is a channel called "auto" in the data. In that case, the only way to specify that channel as the stim channel would be to use its index in the array (i.e. an int), right?

@agramfort
Copy link
Member

agramfort commented Jan 18, 2019 via email

@cbrnr
Copy link
Contributor

cbrnr commented Jan 18, 2019

That's what I was suggesting but @larsoner wanted to have consistent behavior for this case. I'm fine with anything as long as I can provide stim_channel='auto' (a simple string), which should also be the default behavior.

@larsoner
Copy link
Member

I'm fine with the probably-never-happening corner case 'auto' channel name needing to be provided as ['auto']

@massich massich changed the title [WIP] EDF stim_channels [MRG] EDF stim_channels Jan 21, 2019
@massich
Copy link
Contributor Author

massich commented Jan 21, 2019

This should be green, and do what we said.

At this point, the warning of 'auto' when 'auto' is a channel name is not tested. I don't think its worth covering it. But I'll do it if someone asks for it.

passing a list of integers stim_channel=[0, 3, -1] is not allowed either. I'll put it in if you ask for it.

# the type of channel it is.
stim_name_ends_with_annotations = [ch for ch in valid_stim_ch_names
if ch.endswith('annotations')]
if len(stim_name_ends_with_annotations):
Copy link
Contributor

Choose a reason for hiding this comment

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

When does this evaluate to True? If someone has a channel ending with "annotations"? If so, shouldn't this channel be treated as a stim channel (i.e. set its type to stim and don't synthesize)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is true when someone passes 'EDF Annotations' or 'BDF Annotations', in that case the user should take it out from the stim_channel. Therefore the ValueError.

Then, if an annotations channel is present in the file it gets loaded as annotations, and there is no stim channel with such name.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, of course, you are right!

Copy link
Contributor

Choose a reason for hiding this comment

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

However, this also includes other channels ending with "annotations", which is probably not what we want. I'd exclude only these 2 particular names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, the right thing to do would be to actually get a list of TAL channels when _read_edf_header and raise the error based on that. But at this point, I think that we can do that in a different PR (if we ever get to it).

But sure, we can check for those specific names only.

if stim_channel == 'auto':
valid_stim_ch_names = DEFAULT_STIM_CH_NAMES
if 'auto' in ch_names:
warn(RuntimeWarning, 'Using `stim_channel=\'auto\'` when auto'
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use a raw string here to avoid the backslashes. Also, the comma in the first sentence should be removed, and there should be a period after the last sentence.

@larsoner larsoner added this to the 0.18 milestone Jan 22, 2019
@larsoner
Copy link
Member

@cbrnr let me know when you are happy, and I'll take a look and merge

@cbrnr
Copy link
Contributor

cbrnr commented Jan 23, 2019

What about passing an int (or a list of ints)? Previously, I liked to do stim_channel=-1, because the stim channel is usually the last one. Does this work again?

@massich
Copy link
Contributor Author

massich commented Jan 23, 2019 via email

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.

@cbrnr feel free to merge if you're happy

@massich
Copy link
Contributor Author

massich commented Jan 23, 2019

I fixed a pep8 error that was there. The rest of the CI errors in travis are network timeouts when fetching physionet data.

@cbrnr cbrnr merged commit 42cf687 into mne-tools:master Jan 24, 2019
@cbrnr
Copy link
Contributor

cbrnr commented Jan 24, 2019

Thanks @massich!

@agramfort
Copy link
Member

cool thanks @massich

can you now backport this? maybe it's a simple as removing the deprecation warning on 0.17 branch and see how what's new needs to be adjusted?

thx

@massich massich deleted the edf_stim branch January 24, 2019 09:33
massich pushed a commit that referenced this pull request Jan 24, 2019
@larsoner
Copy link
Member

This appears to have broken CircleCI:

...
  File "/home/circleci/project/tutorials/plot_sleep.py", line 90, in <module>
    raw_train.plot(duration=60, scalings='auto')
  File "/home/circleci/project/mne/io/base.py", line 1873, in plot
    event_id=event_id)
  File "/home/circleci/project/mne/viz/raw.py", line 265, in plot_raw
    scalings = _compute_scalings(scalings, raw)
  File "/home/circleci/project/mne/viz/utils.py", line 1634, in _compute_scalings
    data = inst._read_segment(tmin, tmax)
  File "/home/circleci/project/mne/io/base.py", line 561, in _read_segment
    cals, mult)
  File "<string>", line 2, in _read_segment_file
  File "/home/circleci/project/mne/utils.py", line 953, in verbose
    return function(*args, **kwargs)
  File "/home/circleci/project/mne/io/edf/edf.py", line 284, in _read_segment_file
    if ci in stim_channel:
TypeError: argument of type 'NoneType' is not iterable

Can you look into it? It seems like there is some bug, and some use case not caught by our tests (only by an example).

It should be fixed before backporting in #5861

@massich
Copy link
Contributor Author

massich commented Jan 24, 2019

@larsoner yes I just saw it as well, what I don't understand is why circle CI was not red. I guess that we don't build the entire documentation.

@massich massich mentioned this pull request Jan 24, 2019
larsoner pushed a commit that referenced this pull request Jan 25, 2019
* [Backport 5841] Modify docstring + add test

* Remove stim_channel deprecation warning

* update whatsnew
@larsoner larsoner removed the backport-candidate on-merge: backport to maint/1.11 label Apr 23, 2020
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.

FIX: Remove EDF stim code

4 participants