Repository navigation
Description
Describe the new feature or enhancement
After briefly discussing this with @drammock, opening this to evaluate if there is interest in MNE-community to add support for reading the data from the Neuralynx acquisition systems (https://neuralynx.com/).
These electrophysiology time series are typically recorded from depth electrodes at high sampling rates (32KHz) to detect single-neuron spiking activity in humans. To my familiarity, a researcher typically will not interact with the raw dataset (due to large file sizes), but with a downsampled version of the continuous recording (.ncs) usually in combination with event files (.nev).
Information on the type of data files in a typical Neuralynx dataset:
- Neuralynx file specification: https://support.neuralynx.com/hc/en-us/articles/360040444811-TechTip-Neuralynx-Data-File-Formats
- FieldTrip docs: https://www.fieldtriptoolbox.org/getting_started/neuralynx/
Example dataset/research paper:
- A NWB-based dataset and processing pipeline of human single-neuron activity during a declarative memory task (organized following the Neurodata Without Borders structure)
Describe your proposed implementation
In brief, Neuralynx datasets are stored as a single .ncs (continuously sampled) files for each channel. A dataset directory containing only .ncs channel files could like something like this:
>>> ls ./neuralynx_dataset
LACC1.ncs LAM5.ncs LPCC1.ncs LPHG5.ncs
LACC2.ncs LAM6.ncs LPCC2.ncs LPHG6.ncs
LACC3.ncs LAM7.ncs LPCC3.ncs LPHG7.ncs
LACC4.ncs LAM8.ncs LPCC4.ncs LPHG8.ncs
It seems to me that on the user end this could look something like:
raw = mne.io.read_raw_neuralynx("./neuralynx_dataset") # read all channel files and header informationor possibly for individual channel files:
raw_ch1 = mne.io.read_raw_neuralynx_ncs("./neuralynx_dataset/LACC1.ncs") # read single channel file and header informationDescribe possible alternatives
The Neo python package has a NeuralynxIO class intended to be used as interface to read Neuralynx files in python, but offers no functionality to analyze and visualize the data.
Additional context
I have not analyzed such data before, so I don't have a sample dataset at hand nor can give examples a typical workflow (but might in the coming months). I've only ever had to read in the header information of a Neuralynx dataset for purposes of documenting it and noticed this was not possible with MNE-Python.
Activity
welcome commentedon Aug 10, 2023
Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴
larsoner commentedon Aug 10, 2023
To clarify this way of doing things -- I think it shouldn't read all the data (this should only be done when
preload=True) but rather it should figure out how much data there are on disk, and read later. (This stuff is built into the BaseRaw class -- theread_raw_neuralynx"just" needs to know how many samples there actually are and how to read an arbitrary segment of data for a set of channel picks.)I don't think we need to add this variant. If you want a single channel you would just use the directory version and pick as usual like:
or so. And the data is only loaded from disk when calling
load_dataorget_dataor usingpreload=Trueetc.An optional dependency on
neoseems okay to me. As long as that class does not immediately read all data and makes it easy to do on-demand reads and extract metadata, then we should be good to use it internally I think. Then the class becomes hopefully simple like the following (neo use is wrong here but just to give an idea):It will be more complicated than this but hopefully this conveys the gist.
A good template to follow would be https://github.com/mne-tools/mne-python/blob/main/mne/io/eximia/eximia.py -- it's easy to see the two methods that need to be implemented (
__init__.pyand_read_segment_file. Once those are implemented you get all the plotting/preload/picking etc. for free from the base class.@KristijanArmeni any interest in giving it a shot?
KristijanArmeni commentedon Aug 10, 2023
Thanks @larsoner for the pointers and extra explanation! I'm happy to give this a shot (over the coming weeks).
I think I've enough information to get started. Would be my first attempt at contribution, so will have to be baby steps, but will ping here in case I need further steering.
larsoner commentedon Aug 10, 2023
Awesome!
One other thing to think about is testing. We add files here (see instructions):
https://github.com/mne-tools/mne-testing-data
For this we'd want a file as small as possible from the system (e.g., ~1 sec and < 1 MB if possible) with known values, e.g., read using other software (like FieldTrip) and saved to
.mat. Then at the MNE-Python end you can use your newly implemented reader and check theraw.get_data()values against theread_mat(fieldtrip_mat)['data']values usingassert_allclose.KristijanArmeni commentedon Sep 6, 2023
Just a small update on this. I started implementing
RawNeuralynx(BaseRaw). Need some pointers for next steps below.Summary
Currently,
RawNeuralynx(BaseRaw)usesNeuralynxIO()class from the Neo package to get basic file information from file headers, without loading data into memory (in my local install, neo required no additional dependencies on top of those included in mnedev). This seems to work ok (see screenshots in Additional info below)Next steps
To read data into memory, I need to make something like this work:
raw.pick(["LACC1", "LACC2"]).load_data()I need some help with:
BaseRaw()._read_segment_filemethod, but if I see correctly that one assumes that data on disk is stored inshape = (channel, samples)array? For Neuralynx, each file one disk is a single channel._read_segments_file(following Eximia template) though after updating my local MNE copy, I’m getting deprecation warnings (FutureWarning: mne.io.utils._read_segments_file is deprecated will be removed in 1.6, use documented public API instead. If no appropriate public API exists, please open an issue on GitHub.). I get deprecation warning formne.io.meas_info.create_infoas well.Additional info
For example, for a neuralynx folder with channel files:
The basic bookkeeping seems fine:
drammock commentedon Sep 6, 2023
we already require
neofor our doc building (this example) so it's not a huge deal to add it as a dependency for a neuralynx reader, IMO.are you getting that warning within MNE's source code? or are you testing it out in a separate script for now? (Maybe time to open the WIP pull request so I could just look at the code and see what you're doing)
This has moved, access it at
mne._fiff.meas_info.create_infoin package code (or atmne.create_infoin user code)maybe worth asking on the forum if anyone out there has a neuralynx system and would be willing to record us a short segment. Otherwise, maybe we could find a way to truncate and anonymize some of your non-sharable files? Are you able to share them privately (e.g., to me and/or @larsoner) for that purpose, or are they 100% non-sharable?
larsoner commentedon Sep 6, 2023
Or if you have access to one, the data can come from a junk recording without the system hooked up to any subject/patient. As long as there are some non-zero non-NaN values in there we can work with it!
Not exactly. The beauty of
_read_segment_fileis that you can read data from whatever file or files (plural) you need to read from to put the data in an array of shape(n_channels, n_samples). So for your data format this would mean you probably need to openn_channelsnumber of files instead of just one.There are more details in there -- like you don't typically need to read all
n_channelsnumber of channels and there's a variable that tells you which to read -- but that's the general idea. If you can start by coding it to read all(n_channels, n_samples)worth of data within the_read_segments_filemethod and thensubselecting the correct set of data using something likeall_data[idx, start:stop]then that's already a good start, and we can figure out how to make it efficient by only reading what needs to be read, etc.Agreed, our comments/feedback will probably be easier to make inline moving forward!
KristijanArmeni commentedon Sep 7, 2023
Great, thanks for the input, all makes sense! Good call -- I will create a WIP pull request and we can take it from there w.r.t. the issues raised above.
KristijanArmeni commentedon Sep 19, 2023
Update: we have a small Neuralynx test dataset!
It’s a small, 3.4 sec long (< 1MB) unconnected recording with 10 continuously sampled channels (
.ncs), 10 spike files (.nse, likely empty though) and an event file (.nev). CLI print below.In terms of moving forward:
Additional info
Dataset contents:
mscheltienne commentedon Sep 19, 2023
Great! Yes, the next step is to add the file to the testing dataset.
Antho2422 commentedon Nov 17, 2023
@KristijanArmeni Hello ! Thank you for trying to add Neuralynx support in MNE. Any update on how this process is going ? I would be really interested !
Cheers,
Anthony
larsoner commentedon Nov 17, 2023
Support was added in #11969, should be out in 1.6 which should be released soon