Skip to content

Commit 4a449e0

Browse files
authored
rename spikegadgets (#440)
1 parent 7092508 commit 4a449e0

3 files changed

Lines changed: 96 additions & 3 deletions

File tree

src/probeinterface/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
read_BIDS_probe,
1616
write_BIDS_probe,
1717
read_spikegadgets,
18+
read_spikegadgets_neuropixels,
19+
has_spikegadgets_neuropixels_probes,
1820
read_mearec,
1921
read_nwb,
2022
read_maxwell,

src/probeinterface/io.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,17 @@ def write_csv(file, probe):
733733
raise NotImplementedError
734734

735735

736-
def read_spikegadgets(file: str | Path, raise_error: bool = True) -> ProbeGroup:
736+
def read_spikegadgets_neuropixels(file: str | Path, raise_error: bool = True) -> ProbeGroup:
737737
"""
738738
Find active channels of the given Neuropixels probe from a SpikeGadgets .rec file.
739739
SpikeGadgets headstages support up to three Neuropixels 1.0 probes (as of March 28, 2024),
740740
and information for all probes will be returned in a ProbeGroup object.
741741
742+
This function only supports Neuropixels probes recorded with SpikeGadgets
743+
headstages (``HardwareConfiguration`` entries with ``name == "NeuroPixels1"``).
744+
It does not handle tetrodes or other probe types that SpikeGadgets can
745+
record. Use :func:`has_spikegadgets_neuropixels_probes` to check whether a
746+
``.rec`` file contains Neuropixels probe geometry before calling this reader.
742747
743748
Parameters
744749
----------
@@ -834,6 +839,67 @@ def read_spikegadgets(file: str | Path, raise_error: bool = True) -> ProbeGroup:
834839
return probe_group
835840

836841

842+
def read_spikegadgets(*args, **kwargs) -> ProbeGroup:
843+
"""
844+
Deprecated alias for :func:`read_spikegadgets_neuropixels`.
845+
846+
The name ``read_spikegadgets`` is misleading because the function only reads
847+
Neuropixels probe geometry, not arbitrary SpikeGadgets ``.rec`` recordings.
848+
Use :func:`read_spikegadgets_neuropixels` instead, and
849+
:func:`has_spikegadgets_neuropixels_probes` to check whether a ``.rec`` file
850+
has Neuropixels geometry before calling it.
851+
"""
852+
warnings.warn(
853+
"read_spikegadgets is deprecated and will be removed in a future release. "
854+
"Use read_spikegadgets_neuropixels instead.",
855+
category=DeprecationWarning,
856+
stacklevel=2,
857+
)
858+
return read_spikegadgets_neuropixels(*args, **kwargs)
859+
860+
861+
def has_spikegadgets_neuropixels_probes(file: str | Path) -> bool:
862+
"""
863+
Return True if the SpikeGadgets ``.rec`` file describes at least one
864+
Neuropixels probe.
865+
866+
Detection scans the ``HardwareConfiguration`` block of the ``.rec`` XML
867+
header for ``Device`` entries whose ``name`` attribute matches a known
868+
Neuropixels source name (currently ``"NeuroPixels1"``). The presence of
869+
any such entry is the ground-truth signal that the file contains
870+
Neuropixels probe geometry, independent of what other hardware the
871+
headstage is also streaming.
872+
873+
Intended use: callers that route heterogeneous SpikeGadgets recordings
874+
(mixing tetrodes, Neuropixels, etc.) can gate the call to
875+
:func:`read_spikegadgets_neuropixels` on this helper and skip probe
876+
attachment for non-Neuropixels recordings.
877+
878+
Parameters
879+
----------
880+
file : str or Path
881+
Path to the SpikeGadgets ``.rec`` file.
882+
883+
Returns
884+
-------
885+
bool
886+
"""
887+
try:
888+
header_txt = parse_spikegadgets_header(file)
889+
root = ElementTree.fromstring(header_txt)
890+
except Exception:
891+
return False
892+
893+
hconf = root.find("HardwareConfiguration")
894+
if hconf is None:
895+
return False
896+
897+
for device in hconf:
898+
if device.attrib.get("name") == "NeuroPixels1":
899+
return True
900+
return False
901+
902+
837903
def parse_spikegadgets_header(file: str | Path) -> str:
838904
"""
839905
Parse file (SpikeGadgets .rec format) into a string until "</Configuration>",

tests/test_io/test_spikegadgets.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from pathlib import Path
22
from xml.etree import ElementTree
33

4-
from probeinterface import read_spikegadgets
4+
import pytest
5+
6+
from probeinterface import (
7+
read_spikegadgets,
8+
read_spikegadgets_neuropixels,
9+
has_spikegadgets_neuropixels_probes,
10+
)
511
from probeinterface.io import parse_spikegadgets_header
612
from probeinterface.testing import validate_probe_dict
713

@@ -18,7 +24,7 @@ def test_parse_meta():
1824

1925

2026
def test_neuropixels_1_reader():
21-
probe_group = read_spikegadgets(data_path / test_file, raise_error=False)
27+
probe_group = read_spikegadgets_neuropixels(data_path / test_file, raise_error=False)
2228
assert len(probe_group.probes) == 2
2329
for probe in probe_group.probes:
2430
probe_dict = probe.to_dict(array_as_list=True)
@@ -29,6 +35,25 @@ def test_neuropixels_1_reader():
2935
assert probe_group.get_contact_count() == 768
3036

3137

38+
def test_read_spikegadgets_deprecation_warning():
39+
# Old read_spikegadgets name must still work but emit DeprecationWarning pointing at the new name.
40+
with pytest.warns(DeprecationWarning, match="read_spikegadgets_neuropixels"):
41+
read_spikegadgets(data_path / test_file, raise_error=False)
42+
43+
44+
def test_has_spikegadgets_neuropixels_probes_positive():
45+
# A real Neuropixels .rec header should report True.
46+
assert has_spikegadgets_neuropixels_probes(data_path / test_file) is True
47+
48+
49+
def test_has_spikegadgets_neuropixels_probes_missing_file():
50+
# Unreadable / nonexistent files return False rather than raising.
51+
assert has_spikegadgets_neuropixels_probes(data_path / "does_not_exist.rec") is False
52+
53+
3254
if __name__ == "__main__":
3355
test_parse_meta()
3456
test_neuropixels_1_reader()
57+
test_read_spikegadgets_deprecation_warning()
58+
test_has_spikegadgets_neuropixels_probes_positive()
59+
test_has_spikegadgets_neuropixels_probes_missing_file()

0 commit comments

Comments
 (0)