Skip to content

Commit 6108921

Browse files
authored
Merge pull request #176 from h-mayorquin/add_mearec_test_data
Add mearec test data
2 parents 7708184 + ccdd5d0 commit 6108921

File tree

3 files changed

+68
-31
lines changed

3 files changed

+68
-31
lines changed

probeinterface/io.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,44 +1542,41 @@ def read_mearec(file: Union[str, Path]) -> Probe:
15421542

15431543
f = h5py.File(file, "r")
15441544
positions = f["channel_positions"][()]
1545-
elinfo = f["info"]["electrodes"]
1546-
elinfo_keys = elinfo.keys()
1545+
electrodes_info = f["info"]["electrodes"]
1546+
electrodes_info_keys = electrodes_info.keys()
15471547

15481548
mearec_description = None
15491549
mearec_name = None
1550-
if "description" in elinfo_keys:
1551-
description = elinfo["description"][()]
1552-
mearec_description = description.decode("utf-8") if isinstance(description, bytes) else description
1553-
if "electrode_name" in elinfo_keys:
1554-
mearec_name = elinfo["electrode_name"][()]
1550+
if "electrode_name" in electrodes_info_keys:
1551+
mearec_name = electrodes_info["electrode_name"][()]
15551552
mearec_name = mearec_name.decode("utf-8") if isinstance(mearec_name, bytes) else mearec_name
15561553

1557-
probe = Probe(ndim=2, si_units="um")
1554+
if "description" in electrodes_info_keys:
1555+
description = electrodes_info["description"][()]
1556+
mearec_description = description.decode("utf-8") if isinstance(description, bytes) else description
15581557

1559-
if "plane" in elinfo_keys:
1560-
plane = elinfo["plane"]
1561-
else:
1562-
plane = "yz" # default
1558+
probe = Probe(ndim=2, si_units="um")
15631559

1564-
if plane == "xy":
1565-
positions_2d = positions[()][:, :2]
1566-
elif plane == "xz":
1567-
positions_2d = positions[()][:, [0, 2]]
1568-
else:
1569-
positions_2d = positions[()][:, 1:]
1560+
plane = "yz" # default
1561+
if "plane" in electrodes_info_keys:
1562+
plane = electrodes_info["plane"][()]
1563+
plane = plane.decode("utf-8") if isinstance(plane, bytes) else plane
1564+
1565+
plane_to_columns = {"xy": [0, 1], "xz": [0, 2], "yz": [1, 2]}
1566+
columns = plane_to_columns[plane]
1567+
positions_2d = positions[()][:, columns]
15701568

15711569
shape = None
1572-
if "shape" in elinfo_keys:
1573-
shape = elinfo["shape"][()]
1574-
if isinstance(shape, bytes):
1575-
shape = shape.decode()
1570+
if "shape" in electrodes_info_keys:
1571+
shape = electrodes_info["shape"][()]
1572+
shape = shape.decode("utf-8") if isinstance(shape, bytes) else shape
15761573

15771574
size = None
1578-
if "shape" in elinfo_keys:
1579-
size = elinfo["size"][()]
1575+
if "shape" in electrodes_info_keys:
1576+
size = electrodes_info["size"][()]
15801577

15811578
shape_params = {}
1582-
if shape is not None:
1579+
if shape is not None and size is not None:
15831580
if shape == "circle":
15841581
shape_params = {"radius": size}
15851582
elif shape == "square":
@@ -1591,14 +1588,12 @@ def read_mearec(file: Union[str, Path]) -> Probe:
15911588
probe.set_contacts(positions_2d, shapes=shape, shape_params=shape_params)
15921589

15931590
# add MEArec annotations
1594-
if mearec_name is not None:
1595-
probe.annotate(mearec_name=mearec_name)
1596-
if mearec_description is not None:
1597-
probe.annotate(mearec_description=mearec_description)
1591+
annotations = dict(mearec_name=mearec_name, mearec_description=mearec_description)
1592+
probe.annotate(**annotations)
15981593

15991594
# set device indices
1600-
if elinfo["sortlist"][()] not in (b"null", "null"):
1601-
channel_indices = elinfo["sortlist"][()]
1595+
if electrodes_info["sortlist"][()] not in (b"null", "null"):
1596+
channel_indices = electrodes_info["sortlist"][()]
16021597
else:
16031598
channel_indices = np.arange(positions.shape[0], dtype="int64")
16041599
probe.set_device_channel_indices(channel_indices)

tests/data/mearec_smaller_data.h5

31.6 KB
Binary file not shown.

tests/test_io/test_mearec.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
import numpy as np
5+
6+
from probeinterface.io import read_mearec
7+
8+
data_path = Path(__file__).absolute().parent.parent / "data"
9+
10+
11+
def test_mearec_small_file():
12+
"""
13+
Small file generated by Alessio and Heberto for testing purposes
14+
15+
The file was then reduced using the following script:
16+
https://gist.github.com/h-mayorquin/aec90a67ccf68c152e845f350a2a8230
17+
18+
"""
19+
probe = read_mearec(data_path / "mearec_smaller_data.h5")
20+
21+
assert probe.annotations["mearec_name"] == "Neuronexus-32"
22+
assert (
23+
probe.annotations["mearec_description"]
24+
== "Neuronexus A1x32-Poly3-5mm-25s-177-CM32 probe. 32 circular contacts in 3 staggered columns."
25+
)
26+
27+
assert probe.ndim == 2
28+
assert probe.get_shank_count() == 1
29+
assert probe.get_contact_count() == 32
30+
31+
# Test contact geometry
32+
contact_radius = 7.5
33+
contact_shape = "circle"
34+
35+
assert np.all(probe.contact_shape_params == {"radius": contact_radius})
36+
assert np.all(probe.contact_shapes == contact_shape)
37+
38+
contact_positions = probe.contact_positions
39+
x = contact_positions[:, 0]
40+
41+
set_of_x_values_as_int = {int(x) for x in x}
42+
assert set_of_x_values_as_int == {-18, 0, 18} # Three columns in this positions

0 commit comments

Comments
 (0)