Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/dev/14423.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix MNE-Python readers for Neuralynx recordings with gaps, following changes in `Neo 0.14.6 <https://github.com/NeuralEnsemble/python-neo/blob/0.14.6/doc/source/releases/0.14.6.rst>`_ by `Scott Huberty`_
21 changes: 16 additions & 5 deletions mne/io/neuralynx/neuralynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ def read_raw_neuralynx(


# Helper for neo change of exclude_filename -> exclude_filenames in 0.13.2
def _exclude_kwarg(exclude_fnames):
def _neo_kwargs(exclude_fnames):
from neo.io import NeuralynxIO

kwargs = {}
key = "exclude_filename"
if "exclude_filenames" in inspect.getfullargspec(NeuralynxIO).args:
params = inspect.signature(NeuralynxIO)
if "exclude_filenames" in params.parameters:
key += "s"
return {key: exclude_fnames}
kwargs[key] = exclude_fnames

# Introduced in neo version 0.14.6
if "gap_tolerance_ms" in params.parameters:
# As recommended by neo API documentation. This restores the old behavior
kwargs.update({"gap_tolerance_ms": 0.0})
return kwargs


@fill_doc_static()
Expand Down Expand Up @@ -146,9 +154,11 @@ def __init__(

logger.info("Ignoring .ncs files:\n" + "\n".join(exclude_fnames))

# Gap handling and filename excluding
neo_kwargs = _neo_kwargs(exclude_fnames)
# get basic file info from header, throw Error if NeuralynxIO can't parse
try:
nlx_reader = NeuralynxIO(dirname=fname, **_exclude_kwarg(exclude_fnames))
nlx_reader = NeuralynxIO(dirname=fname, **neo_kwargs)
except ValueError as e:
# give a more informative error message and what the user can do about it
if "Incompatible section structures across streams" in str(e):
Expand Down Expand Up @@ -341,9 +351,10 @@ def _read_segment_file(self, data, idx, fi, start, stop, cals, mult):
# quantities is a dependency of neo so we are guaranteed it exists
from quantities import Hz

neo_kwargs = _neo_kwargs(self._raw_extras[0]["exclude_fnames"])
nlx_reader = NeuralynxIO(
dirname=self.filenames[fi],
**_exclude_kwarg(self._raw_extras[0]["exclude_fnames"]),
**neo_kwargs,
)
neo_block = nlx_reader.read(lazy=True)

Expand Down
4 changes: 2 additions & 2 deletions mne/io/neuralynx/tests/test_neuralynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from mne.datasets.testing import data_path, requires_testing_data
from mne.io import read_raw_neuralynx
from mne.io.neuralynx.neuralynx import _exclude_kwarg
from mne.io.neuralynx.neuralynx import _neo_kwargs
from mne.io.tests.test_raw import _test_raw_reader

testing_path = data_path(download=False) / "neuralynx"
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_neuralynx():
mne_y = raw.get_data() # in V

# ==== NeuralynxIO ==== #
nlx_reader = NeuralynxIO(dirname=testing_path, **_exclude_kwarg(excluded_ncs_files))
nlx_reader = NeuralynxIO(dirname=testing_path, **_neo_kwargs(excluded_ncs_files))
bl = nlx_reader.read(
lazy=False
) # read a single block which contains the data split in segments
Expand Down
Loading