Skip to content

Commit

Permalink
Merge branch 'main' into dict_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
qian-chu authored Jan 16, 2024
2 parents 1bce965 + 8583885 commit f42d5fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12357.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix faulty indexing in :func:`mne.io.read_raw_neuralynx` when picking a single channel, by `Kristijan Armeni`_.
18 changes: 12 additions & 6 deletions mne/io/neuralynx/neuralynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnalogSignalGap:
Parameters
----------
signal : array-like
Array of shape (n_channels, n_samples) containing the data.
Array of shape (n_samples, n_chans) containing the data.
units : str
Units of the data. (e.g., 'uV')
sampling_rate : quantity
Expand All @@ -39,13 +39,20 @@ def __init__(self, signal, units, sampling_rate):
self.units = units
self.sampling_rate = sampling_rate

def load(self, channel_indexes):
def load(self, **kwargs):
"""Return AnalogSignal object."""
_soft_import("neo", "Reading NeuralynxIO files", strict=True)
from neo import AnalogSignal

# `kwargs` is a dummy argument to mirror the
# AnalogSignalProxy.load() call signature which
# accepts `channel_indexes`` argument; but here we don't need
# any extra data selection arguments since
# self.signal array is already in the correct shape
# (channel dimension is based on `idx` variable)

sig = AnalogSignal(
signal=self.signal[:, channel_indexes],
signal=self.signal,
units=self.units,
sampling_rate=self.sampling_rate,
)
Expand Down Expand Up @@ -141,8 +148,7 @@ def __init__(
sfreq=nlx_reader.get_signal_sampling_rate(),
)

# find total number of samples per .ncs file (`channel`) by summing
# the sample sizes of all segments
# Neo reads only valid contiguous .ncs samples grouped as segments
n_segments = nlx_reader.header["nb_segment"][0]
block_id = 0 # assumes there's only one block of recording

Expand All @@ -160,7 +166,7 @@ def __init__(
seg_diffs = next_start_times - previous_stop_times

# mark as discontinuous any two segments that have
# start/stop delta larger than sampling period (1/sampling_rate)
# start/stop delta larger than sampling period (1.5/sampling_rate)
logger.info("Checking for temporal discontinuities in Neo data segments.")
delta = 1.5 / info["sfreq"]
gaps = seg_diffs > delta
Expand Down
11 changes: 11 additions & 0 deletions mne/io/neuralynx/tests/test_neuralynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,14 @@ def test_neuralynx_gaps():
assert_allclose(
mne_y, mat_y, rtol=1e-6, err_msg="MNE and Nlx2MatCSC.m not all close"
)

# test that channel selection works
raw = read_raw_neuralynx(
fname=testing_path,
preload=False,
exclude_fname_patterns=ignored_ncs_files,
)

raw.pick("LAHC2")
assert raw.ch_names == ["LAHC2"]
raw.load_data() # before gh-12357 this would fail

0 comments on commit f42d5fb

Please sign in to comment.