Skip to content

Add Support for NWB #10427

Open
Open

Description

@nmarkowitz

Hi,

I've been working on creating a new subclass of BaseRaw that can read from a NWB file. These are organized hdf files that can store multiple types of data streams including those with different sampling rates, sizes from different sources (such as eye-tracking, seeg, scalp eeg, audio etc.). Because they're hdf files they have lazy loading. It would be nice to have a RawNWB object and/or a read_raw_nwb() function so that MNE can work with these types of files. Attached is a link to a gist I made with some example code for reading/writing NWB files (Part 1) and some code snippets I've made to get started with creating a RawNWB object (Part 2). The thing I mostly struggling with is creating the read_segment_file() function. Any insight or help would be greatly appreciated.

https://gist.github.com/sportnoah14/b74f1226c09bd79aa7f7ec7b11af80c9

pynwb docs

Activity

  1. welcome commented on Mar 10, 2022

    @welcome

    Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

  2. rob-luke commented on Mar 10, 2022

    @rob-luke
    Member

    There is also an extension for NWB-NIRS that would be nice to support if this is implemented.
    See https://github.com/agencyenterprise/ndx-nirs
    Also ping @dsleiter

  3. nmarkowitz commented on Mar 10, 2022

    @nmarkowitz
    ContributorAuthor

    @rob-luke if that extension is a subclass of a TimeSeries then it should work the same. It would be hard to encompass all the extensions. I think just having a single reader for a TimeSeries container is a great start

  4. dsleiter commented on Mar 11, 2022

    @dsleiter
    Contributor

    ndx-nirs stores NIRS data in a class that inherits from TimeSeries, so it should be possible to read the raw data. However, to read channel metadata, I think RawNWB might need to know about specific types of timeseries (eg. ElectricalSeries, SpikeEventSeries, NIRSSeries, etc).

    I'm not familiar with the usual implementation details of _read_segment_file for other Raw classes, but all pynwb timeseries objects should have a .data attribute for access to the raw data.
    https://nwb-schema.readthedocs.io/en/latest/format.html#timeseries

    Would the following help in extracting the desired data?

    timeseries = nwb.get_acquisition('timeseries_name')
    timeseries.data[start:stop+1, idx]

    Or was the difficulty with something else about the nwb files?

  5. nmarkowitz commented on Mar 11, 2022

    @nmarkowitz
    ContributorAuthor

    @dsleiter I was just meant that support for all TimeSeries subclasses may be difficult. Not impossible though. I've already worked out a way to extract channel information for an ElectricalSeries object. RawNWB may just an up being an object with multiple if statements in its declaration (or a function it calls in its declaration). The biggest issue for this may be if there's significant differences (such as attributes added, deprecated or changed) between versions of NWB. But we can figure that out after getting an initial version running.

  6. dsleiter commented on Mar 16, 2022

    @dsleiter
    Contributor

    Yeah, I think that separate logic might be needed for each TimeSeries subclass in order to get channel information. That sounds good to start with ElectricalSeries and add support for others later. There probably aren't too many cases that would need to be implemented in order to support the majority of use cases for mne users.

    I believe the NWB team has been pretty good about ensuring backwards compatibility within major versions, so I'd expect the pyNWB 2.0 API to be pretty stable at least until version 3.0. It might be worth adding a check in the code to verify the major version of the installed pynwb library and raise a warning or error if != 2.

    Due to NWB's flexibility, other challenges might be handling situations with multiple acquisitions within a single file or ensuring a consistent label for ElectricalSeries channels across different user-created files (looks like you explicitly add a 'label' column, which might not exist for most NWB files).

  7. nmarkowitz commented on Mar 16, 2022

    @nmarkowitz
    ContributorAuthor

    @dsleiter I agree, we would probably need separate logic for each case. If it's restricted to the TimeSeries subclasses that are part of primary pynwb package and the major extensions then that should be fine.

    Beecause there can be many acquisitions, my thinking is that the RawNWB object would interact with a single TimeSeries as opposed to the whole file. So the function would work more like this:

    from pynwb import NWBHDF5IO
    io = NWBHDF5IO('myfile.nwb')
    nwb = io.read()
    raw = RawNWB(nwb.get_acquisition('eeg'))
    

    For other specifications maybe a bunch of kwargs can be added such as ch_names as an argument that points to the column in the ElectrodeTable object that specifies a name for a channel. Otherwise it can simply name channels as "chn1, chn2, chn3" like it would for another TimeSeries object.

    Before any of this though we need to get the _read_segment_file() function working to support lazy loading. This is the part that I haven't figured out yet and am still working on. Once this, the most essential, aspect is done then we can go crazy with supporting all the subclasses of TimeSeries

  8. larsoner commented on Aug 31, 2022

    @larsoner
    Member

    @sportnoah14 still interested in this?

    Before any of this though we need to get the _read_segment_file() function working to support lazy loading. This is the part that I haven't figured out yet and am still working on. Once this, the most essential, aspect is done then we can go crazy with supporting all the subclasses of TimeSeries

    FWIW a first version doesn't absolutely have to have this. You can just always preload data (then _read_segment_file does not need to be defined at all). It's not ideal but it's a good start, and might be good enough forever if these files are never expected to be very large...

  9. nmarkowitz commented on Sep 3, 2022

    @nmarkowitz
    ContributorAuthor

    @larsoner im still interested in this. I could start with having data preloaded but ideally I, and I think everyone using NWB files, would like to have lazy loading as these files can get very large. I'm currently on holiday and in the midst of moving but I'll be resuming this once I settle a bit more. Hopefully this coming week or the week after.

    @dsleiter and @rob-luke I'd love your help with extensions once I get an initial version for preloaded data working for TimeSeries objects. Maybe some of the core NWB developers would be interested in helping too.

  10. yarikoptic commented on May 6, 2025

    @yarikoptic
    Contributor

    May be @rly and @stephprince could provide some guidance here if @nmarkowitz or anyone else is still interested to implement support for NWB input?

  11. nmarkowitz commented on May 8, 2025

    @nmarkowitz
    ContributorAuthor

    @yarikoptic I have a general function that reads in the data and makes it into a RawArray object. What is still needed is defining the function _read_segment_file()

  12. yarikoptic commented on May 8, 2025

    @yarikoptic
    Contributor

    Well, it depends what mne-python could benefit from reading?! ie. what does it read from other file formats?

    E.g. likely for ERPs etc, event onsets, which could come from some behavioral data present, see https://pynwb.readthedocs.io/en/dev/tutorials/domain/plot_behavior.html . May be paper like https://www.nature.com/articles/s41597-020-0415-9 (although for other type of neuroephys recordings ;) ) could be of inspiration/details?

    Also, there also could be ndx-events almost finalized, see https://github.com/rly/ndx-events , would be yet another location how events could be specified. Also, since could contain HED descriptors, relates to

    thus attn @VisLab

  13. nmarkowitz commented on May 8, 2025

    @nmarkowitz
    ContributorAuthor

    If we'd want to make this feature available for more people, which may be useful because MNE-Python is a popular package, then this could possibly go into another repo. Maybe this could be its own repo that defines a custom object(s) and handles all the minutia of NWB objects. Would be a bit of an extra pain to make sure you have it but it's possible

  14. yarikoptic commented on May 8, 2025

    @yarikoptic
    Contributor

    what happens with other formats MNE can consume -- any of them is as expressive as nwb to include events info? or where from MNE could load events info? (only BIDS _events.tsv ?)

  15. added this to the 1.13 milestone on May 4, 2026
  16. bruAristimunha commented on Jun 17, 2026

    @bruAristimunha
    Contributor

    As discussed during the OHBM with @yarikoptic, I now have a better view of how to implement the NWB reader, and the suggestion was to use some ieeg dataset from the DANDI dataset, which may be BIDS-ish: https://dandiarchive.org/dandiset/search?search=ieeg

    I will push some minimal PR in this sense.

  17. removed this from the 1.13 milestone on Sep 9, 2026
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