Skip to content

Support STIM channels in EDF export #9883

Closed

Description

@cbrnr

I found some issues with our recently added EDF export that we might want to fix before the next release:

  1. The error if EDFlib-Python is not installed is misleading, because it says RuntimeError: For exporting to EDF to work, the EDFlib module is needed, but it could not be imported. However, although the import name is EDFlib, the PyPI name is actually EDFlib-Python and EDFlib is a different PyPI package (which requires Cython etc.).
  2. EDFlib-Python is super slow compared to pyEDFlib (which uses the C implementation). I tested exporting with one of our BDF files from our lab (425MB), and it took EDFlib-Python 1 minute and 52 seconds to export. For comparison, pyEDFlib took only 6 seconds.
  3. Analog stim channels are not exported. This is essential in BDF files, because Biosemi amplifiers do not have discrete triggers and instead always write an analog stim channel. There is no technical reason to not export stim channels in addition to EEG (other than carefully setting the scaling, unit, and physical min/max). I've fixed this in my EDF/BDF export implementation in MNELAB recently (today) in Fix EDF/BDF export of data containing STIM channels cbrnr/mnelab#230.

The first issue is probably easy to fix. The second issue is pretty serious, we can't expect that users wait almost 2 minutes for something that can be done in 6 seconds. I'd consider switching to pyEDFlib instead (they have binary wheels and AFAIR the restriction to use pure Python applies only to our project but not external dependencies). The third issue requires some work, but this is important for BDF files.

Activity

  1. hoechenberger commented on Oct 22, 2021

    @hoechenberger
    Member

    @cbrnr Would you have time to work on this next week?

  2. cbrnr commented on Oct 22, 2021

    @cbrnr
    ContributorAuthor

    Probably not next week, maybe the week after. I have to finish a paper revision and some other stuff. But we should decide if we want to replace EDFlib-Python with pyEDFlib – what is everyone's opinion?

  3. larsoner commented on Oct 22, 2021

    @larsoner
    Member

    It might make sense to do a quick profile and look at EDFlib-Python's code to see if it can easily be improved for example by using NumPy data writing functions rather than struct or anything like that. But I guess it also doesn't matter too much which lib we use, so I'm also fine with just switching to pyEDFlib if it's easy enough.

  4. hoechenberger commented on Oct 22, 2021

    @hoechenberger
    Member

    But we should decide if we want to replace EDFlib-Python with pyEDFlib – what is everyone's opinion?

    I'd pick whichever seems to work best at the moment … if you believe pyEDFlib is a good choice, let's choose this. It's on conda-forge too, so installation shouldn't be a problem.

  5. cbrnr commented on Oct 23, 2021

    @cbrnr
    ContributorAuthor

    I think the slowness comes from our own loop here:

    https://github.com/mne-tools/mne-python/blob/main/mne/export/_edf.py#L240-L268

    pyEDFlib doesn't require users to loop over blocks, because it accepts a list of NumPy arrays (one element per channel). I'm pretty sure this can even be simplified to passing the whole 2D array in one go.

  6. agramfort commented on Oct 23, 2021

    @agramfort
    SponsorMember
  7. cbrnr commented on Oct 23, 2021

    @cbrnr
    ContributorAuthor

    But it's an external package with wheels and an optional dependency. NumPy has tons of compiled code and we're not thinking about a pure Python alternative.

  8. hoechenberger commented on Oct 24, 2021

    @hoechenberger
    Member

    And since it's an optional dependency, we can simply replace it if / once it turns out to become problematic. It won't affect our core functionality in any way...

  9. cbrnr commented on Oct 24, 2021

    @cbrnr
    ContributorAuthor

    Exactly. I think in this case the speedup might be worth the tradeoff.

  10. agramfort commented on Oct 24, 2021

    @agramfort
    SponsorMember
  11. adam2392 commented on Oct 26, 2021

    @adam2392
    Member

    My only objection with pyedflib writing is that the end file never opened for me in EDFBrowser, which is my primary reason for working in EDF format. My export costs only occur once per file occasionally, so I"m okay with slow exports as long as they facilitate a downstream data analysis.

    My export code at the time could've been incorrect tho.

    Although maybe with the super fast data browser I've been peeking at, this could be eventually be irrelevant.

  12. cbrnr commented on Oct 26, 2021

    @cbrnr
    ContributorAuthor

    Exports with pyEDFlib worked fine when I tried it, all files opened in EDFbrowser with no problems.

  13. adam2392 commented on Oct 26, 2021

    @adam2392
    Member

    Do you have a code snippet I can try?

  14. larsoner commented on Oct 26, 2021

    @larsoner
    Member

    This takes 30 sec, if I vectorize the loop over channels it takes 3 sec but produces an incorrect result (at least for my first attempt) -- probably need a new method to write multiple channels simultaneously

    import mne
    import numpy as np
    info = mne.create_info(64, 1000., 'eeg')
    data = np.random.RandomState(0).randn(64, 1000000)
    raw = mne.io.RawArray(data, info)
    raw.export('test.edf')
    
  15. 10 remaining items

  16. modified the milestones: 1.1, 1.2 on Jun 4, 2022
  17. modified the milestones: 1.2, 1.3 on Sep 13, 2022
  18. removed this from the 1.3 milestone on Dec 8, 2022
  19. withmywoessner commented on Sep 30, 2023

    @withmywoessner
    Contributor

    @cbrnr Do you still have your function? I could try adapting the tests to it.

  20. cbrnr commented on Oct 1, 2023

    @cbrnr
    ContributorAuthor

    Yes, it's here: https://github.com/cbrnr/mnelab/blob/main/mnelab/io/writers.py#L57

    Since it's been 1.5 years since I last tried to work on that, it might be worth checking how much functionality has been added to EDF export in MNE which is not present in my implementation.

  21. withmywoessner commented on Oct 4, 2023

    @withmywoessner
    Contributor

    @cbrnr I wrote something that works using pyedflib. One problem is that the final output differs more than the test allows (test is 1e-5):

    absolute tolerance: 0.01
    
    Not equal to tolerance rtol=0, atol=0.01
    
    Mismatched elements: 8540 / 14400 (59.3%)
    Max absolute difference: 0.02457271
    Max relative difference: 0.00102393
     x: array([0.000000e+00, 1.664960e-03, 3.329920e-03, ..., 2.397043e+01,
           2.397210e+01, 2.397376e+01])
     y: array([0.000000e+00, 1.666667e-03, 3.333333e-03, ..., 2.399500e+01,
           2.399667e+01, 2.399833e+01])
    
    absolute tolerance: 0.1
    Success!
    

    Is a tolerance of .1 enough? I am not sure why this is happening. It might have to do with the fact that the testing data has a non integer sampling rate and the physical maxes and mins are of high precision (pyedflib only accepts up to 4 decimal places).

  22. cbrnr commented on Oct 5, 2023

    @cbrnr
    ContributorAuthor

    Which test is this? It depends on the scale of the data, but even if that's µV, I think a tolerance of 0.1 is not ... tolerable 😄.

  23. withmywoessner commented on Oct 5, 2023

    @withmywoessner
    Contributor

    It is test_export_raw_edf. I saw in your code you used writeSamples. In mine, I replace the current write function with writePhysicalSamples. I am not sure why there is such a large difference in values...
    In my own testing using pyedflib.highlevel.write_edf. I also got values within .1 uV. Is it all right if I make a pull request?

  24. cbrnr commented on Oct 5, 2023

    @cbrnr
    ContributorAuthor

    Yes, please submit a PR and we can discuss the tolerance issue there!

  25. withmywoessner commented on Oct 9, 2023

    @withmywoessner
    Contributor

    @cbrnr I made a PR whenever you have a chance to look :)

  26. changed the title [-]Some issues with EDF export[/-] [+]Support STIM channels in EDF export[/+] on Jan 3, 2024
  27. cbrnr commented on Jan 3, 2024

    @cbrnr
    ContributorAuthor

    The first two issues have been fixed by switching to edfio. Now there's only one issue left, namely exporting STIM channels (I think MISC channels are already supported).

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions