Skip to content

Commit 867dc16

Browse files
committed
Merge branch 'main' into further_refactor
2 parents d80b8b0 + c631fbb commit 867dc16

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
@@ -1568,44 +1568,41 @@ def read_mearec(file: Union[str, Path]) -> Probe:
15681568

15691569
f = h5py.File(file, "r")
15701570
positions = f["channel_positions"][()]
1571-
elinfo = f["info"]["electrodes"]
1572-
elinfo_keys = elinfo.keys()
1571+
electrodes_info = f["info"]["electrodes"]
1572+
electrodes_info_keys = electrodes_info.keys()
15731573

15741574
mearec_description = None
15751575
mearec_name = None
1576-
if "description" in elinfo_keys:
1577-
description = elinfo["description"][()]
1578-
mearec_description = description.decode("utf-8") if isinstance(description, bytes) else description
1579-
if "electrode_name" in elinfo_keys:
1580-
mearec_name = elinfo["electrode_name"][()]
1576+
if "electrode_name" in electrodes_info_keys:
1577+
mearec_name = electrodes_info["electrode_name"][()]
15811578
mearec_name = mearec_name.decode("utf-8") if isinstance(mearec_name, bytes) else mearec_name
15821579

1583-
probe = Probe(ndim=2, si_units="um")
1580+
if "description" in electrodes_info_keys:
1581+
description = electrodes_info["description"][()]
1582+
mearec_description = description.decode("utf-8") if isinstance(description, bytes) else description
15841583

1585-
if "plane" in elinfo_keys:
1586-
plane = elinfo["plane"]
1587-
else:
1588-
plane = "yz" # default
1584+
probe = Probe(ndim=2, si_units="um")
15891585

1590-
if plane == "xy":
1591-
positions_2d = positions[()][:, :2]
1592-
elif plane == "xz":
1593-
positions_2d = positions[()][:, [0, 2]]
1594-
else:
1595-
positions_2d = positions[()][:, 1:]
1586+
plane = "yz" # default
1587+
if "plane" in electrodes_info_keys:
1588+
plane = electrodes_info["plane"][()]
1589+
plane = plane.decode("utf-8") if isinstance(plane, bytes) else plane
1590+
1591+
plane_to_columns = {"xy": [0, 1], "xz": [0, 2], "yz": [1, 2]}
1592+
columns = plane_to_columns[plane]
1593+
positions_2d = positions[()][:, columns]
15961594

15971595
shape = None
1598-
if "shape" in elinfo_keys:
1599-
shape = elinfo["shape"][()]
1600-
if isinstance(shape, bytes):
1601-
shape = shape.decode()
1596+
if "shape" in electrodes_info_keys:
1597+
shape = electrodes_info["shape"][()]
1598+
shape = shape.decode("utf-8") if isinstance(shape, bytes) else shape
16021599

16031600
size = None
1604-
if "shape" in elinfo_keys:
1605-
size = elinfo["size"][()]
1601+
if "shape" in electrodes_info_keys:
1602+
size = electrodes_info["size"][()]
16061603

16071604
shape_params = {}
1608-
if shape is not None:
1605+
if shape is not None and size is not None:
16091606
if shape == "circle":
16101607
shape_params = {"radius": size}
16111608
elif shape == "square":
@@ -1617,14 +1614,12 @@ def read_mearec(file: Union[str, Path]) -> Probe:
16171614
probe.set_contacts(positions_2d, shapes=shape, shape_params=shape_params)
16181615

16191616
# add MEArec annotations
1620-
if mearec_name is not None:
1621-
probe.annotate(mearec_name=mearec_name)
1622-
if mearec_description is not None:
1623-
probe.annotate(mearec_description=mearec_description)
1617+
annotations = dict(mearec_name=mearec_name, mearec_description=mearec_description)
1618+
probe.annotate(**annotations)
16241619

16251620
# set device indices
1626-
if elinfo["sortlist"][()] not in (b"null", "null"):
1627-
channel_indices = elinfo["sortlist"][()]
1621+
if electrodes_info["sortlist"][()] not in (b"null", "null"):
1622+
channel_indices = electrodes_info["sortlist"][()]
16281623
else:
16291624
channel_indices = np.arange(positions.shape[0], dtype="int64")
16301625
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)