Skip to content

MNE usage with sleep data #5684

Closed
Closed

Description

@agramfort

let me share here some thoughts of how working with EEG sleep data could be simplified once we have the annotations API in place. Given let's say 2 files (one raw data and one annotations) it should be as easy as doing:

import mne
raw = mne.io.read_raw_edf('data.edf')
annotations = mne.read_annotations('sleep_annots.edf')
raw.set_annotations(annotations)
events, event_id = mne.events_from_annotations(raw)
epochs = mne.Epochs(raw, events, event_id, tmin=0., tmax=30., baseline=None)
epochs['N1']  # to get all epochs of N1 type etc.

cc @massich @Slasnista

it would be great to have this in place for the release. It's pretty much done but it needs some ironing.

Activity

  1. massich commented on Nov 1, 2018

    @massich
    Contributor
  2. massich commented on Nov 7, 2018

    @massich
    Contributor

    Is there any sleeping data already in MNE-python?

  3. dengemann commented on Nov 7, 2018

    @dengemann
    Member
  4. dengemann commented on Nov 7, 2018

    @dengemann
    Member
  5. jona-sassenhagen commented on Nov 7, 2018

    @jona-sassenhagen
    Contributor

    API wise it looks good.

  6. Slasnista commented on Nov 8, 2018

    @Slasnista
    Contributor

    Ok, I'll go over it and make you a proposal.

    Just a question, can multiple process access to the same edf record at the same time ? It would be great to use it in a batch generator for deep learning.

  7. agramfort commented on Nov 8, 2018

    @agramfort
    SponsorMemberAuthor
  8. jasmainak commented on Nov 8, 2018

    @jasmainak
    Member

    @agramfort I would argue that you shouldn't need:

    events, event_id = mne.events_from_annotations(raw)

    since the annotations already contain the duration. So specifying tmin and tmax will run into the danger of overshooting your duration of annotation.

  9. massich commented on Nov 8, 2018

    @massich
    Contributor
  10. jasmainak commented on Nov 8, 2018

    @jasmainak
    Member

    So, if I do:

    epochs = mne.Epochs(raw, events, event_id, tmin=-2, tmax=raw.annot.duration.max() + 0.1, baseline=None)

    I will get an error?

  11. massich commented on Nov 8, 2018

    @massich
    Contributor
  12. agramfort commented on Nov 9, 2018

    @agramfort
    SponsorMemberAuthor

    @jasmainak I am not sure I get your concern here. Many formats use annotations to mark stim onsets like events. We just allow this with event and event_id. If the durations are not 0 or fixed for some annotations then we'll need to invent something new. But we'll need a good use case.

  13. jasmainak commented on Nov 9, 2018

    @jasmainak
    Member

    I'm concerned about the use case you shared in your code snippet above. The duration is not 0 there and the annotations are (probably) contiguous. That is to say, right after N1 ends, another sleep stage starts. If you allow the user to specify arbitrary tmin or tmax in the Epochs constructor, their epochs could contain two (or more) sleep stages instead of one.

    I think adding a check inside the Epochs constructor that makes sure your epochs don't contain more than one annotation should be fine.

  14. massich commented on Nov 9, 2018

    @massich
    Contributor
  15. jasmainak commented on Nov 9, 2018

    @jasmainak
    Member

    If I'm trying to do a classification task between sleep stages, I don't want my data to be contaminated by other classes. I'm worried that people will unknowingly use the wrong tmax (e.g., by copy-paste from example) when they did not intend to.

    Of course maybe people want to do onset - 0.1 to duration + 0.1 but I'm not aware of such use cases. Maybe there are valid reasons to do this too ...

  16. 4 remaining items

  17. Slasnista commented on Nov 13, 2018

    @Slasnista
    Contributor

    yes the data are on our dropbox and the script is called check_mne_reader.py

  18. massich commented on Nov 13, 2018

    @massich
    Contributor

    The error was due to fact that events_from_annotations was called without specifying event_id. Therefor it was None and all the events are selected.

    events, event_id = mne.events_from_annotations(raw, event_id=None)

    passing event_id solves the problem

    my_event_id = { 'Sleep stage ?':1,
                    'Sleep stage W':2,
                    'Sleep stage 1':3,
                    'Sleep stage 2':4,
                    'Sleep stage 3':5,
                    'Sleep stage R':6 }
    events, event_id = mne.events_from_annotations(raw, event_id=my_event_id)
  19. agramfort commented on Nov 13, 2018

    @agramfort
    SponsorMemberAuthor
  20. massich commented on Nov 13, 2018

    @massich
    Contributor

    We should. @Slasnista said he would draft an example with public data. I'll try to integrate it to MNE

    @bdyetton pointed us to some publicly available sleep data in #4494 (comment)

  21. mmagnuski commented on Nov 13, 2018

    @mmagnuski
    Member

    Just a reminder that it won't be as easy with eeglab data: currently the eeglab event reader does more steps than just events, event_id = mne.events_from_annotations(raw) to be backward compatible (for example event names that contain digits are turned to integers).

  22. massich commented on Nov 13, 2018

    @massich
    Contributor

    I think that this was addressed. Or available through event_id as a callable. Do you have an example that is not working?

  23. mmagnuski commented on Nov 13, 2018

    @mmagnuski
    Member

    No, I'll check, I'd be happy to be wrong on this. :)
    What I meant is that before annotations one could just read the eeglab file and then use mne.find_events(), while currently you need to create a function that parses event names and pass it to events_from_annotations, right? But I'll check that later in hope that it is as easy as it was before. :)

  24. mmagnuski commented on Nov 13, 2018

    @mmagnuski
    Member

    Currently the eeglab reader does this internally:

    def _event_id_func(trigger, event_id, event_id_func, dropped):
        """Mimic old behavior to be used with events_from_annotations."""
        if event_id is not None and trigger in event_id:
            return event_id[trigger]
        if event_id_func == 'strip_to_integer':
            trigger_new = "".join([x for x in trigger if x.isdigit()])
            if trigger_new.isdigit():
                return int(trigger_new)
            else:
                dropped.append(trigger)
                return None
        elif event_id_func is not None:
            return event_id_func(trigger)
    
    # ...
            # create event_ch from annotations
            annot = read_annotations(input_fname)
            self.set_annotations(annot)
    
            _check_boundary(annot, event_id)
    
            latencies = np.round(annot.onset * self.info['sfreq'])
            _check_latencies(latencies)
    
            dropped_desc = []  # use to collect dropped descriptions
            event_id_ = partial(_event_id_func,
                                event_id=event_id,
                                event_id_func=event_id_func,
                                dropped=dropped_desc)
            events, _ = events_from_annotations(self, event_id=event_id_)

    and then many additional checks. So currently it does not seem possible to drop what eeglab reader does internally and unify with the future default of events_from_annotations.
    But sorry for littering this discussion - this is not specific to sleep data, but relates to whether eeglab file reading could be made easy with using events_from_annotations and still produce the same events as before. But this is a separate discussion.

  25. agramfort commented on Nov 13, 2018

    @agramfort
    SponsorMemberAuthor

    @mmagnuski we are aiming to avoid having some custom handling of annotations across our readers. What was (is still) done by eeglab raw reader should be deprecated although it should still be simple to do with the new unified Annotations API. As I don't use eeglab I don't have a clear vision. What are your thoughts on this? Can you live with the new API?

    btw @massich did we already deprecate all the stim channel generation for eeg readers as planned? it should be done before the release otherwise it will delay the transition by 6 months. Do we all agree on this? cc @cbrnr @jona-sassenhagen

  26. jona-sassenhagen commented on Nov 13, 2018

    @jona-sassenhagen
    Contributor

    I don't use EEGLAB data myself these days, so I might not be the best advocate. But remember it's a secondary format - it is for data recorded in another format, and preprocessed in EEGLAB. So it might not be a top-priority format.

    That said, deprecation is fine with me.

  27. mmagnuski commented on Nov 13, 2018

    @mmagnuski
    Member

    @agramfort Let's continue this conversation in the issue regarding backwards compatibility of eeglab reading, ok? I didn't want to sidetrack the conversation here so much.

  28. massich commented on Nov 13, 2018

    @massich
    Contributor

    So this should warn?

    import os.path as op
    import mne
    edf_path = op.join(op.dirname(op.dirname(mne.__file__)), 'mne/io/edf/tests/data/test.edf')
    raw_py = read_raw_edf(edf_path, misc=range(-4, 0), stim_channel=139, preload=True)
  29. bphanikrishna commented on Jan 15, 2019

    @bphanikrishna

    let me share here some thoughts of how working with EEG sleep data could be simplified once we have the annotations API in place. Given let's say 2 files (one raw data and one annotations) it should be as easy as doing:

    import mne
    raw = mne.io.read_raw_edf('data.edf')
    annotations = mne.read_annotations('sleep_annots.edf')
    raw.set_annotations(annotations)
    events, event_id = mne.events_from_annotations(raw)
    epochs = mne.Epochs(raw, events, event_id, tmin=0., tmax=30., baseline=None)
    epochs['N1']  # to get all epochs of N1 type etc.
    

    cc @massich @Slasnista

    it would be great to have this in place for the release. It's pretty much done but it needs some ironing.

    Myself KRISHNA B, I'm a student from India. My research is on EEG signals using a wireless sensor network to detect drowsiness of a person.
    I have questions about the details of the procedure described for extracting wakeful state and drowsiness stage from freely available physionet sleep EEG data. That is available at:
    https://physionet.org/physiobank/database/sleep-edfx/sleep-cassette/

    I am stuck, and I want to get out of it. May I request you to spare some time to clarify my doubts. If you clarify my doubts, It would be a very big support for my research work. Any time of your convenience is okay for me.

  30. agramfort commented on Jan 15, 2019

    @agramfort
    SponsorMemberAuthor

    closed by #5718

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions