Visitar URL original
Do not convert a probe's annotations to lists in Probe.to_dict (#477) · SpikeInterface/probeinterface@354e844 · GitHub
Skip to content

Commit 354e844

Browse files
authored
Do not convert a probe's annotations to lists in Probe.to_dict (#477)
1 parent 46e06ca commit 354e844

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎src/probeinterface/probe.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,8 @@ def to_dict(self, array_as_list: bool = False) -> dict:
933933
for k in self._dump_attr_names:
934934
v = getattr(self, k, None)
935935
if array_as_list and isinstance(v, dict):
936-
for kk, vv in v.items():
937-
if isinstance(vv, np.ndarray):
938-
v[kk] = vv.tolist()
936+
# build a new dict so the probe's own annotations are not converted in place
937+
v = {kk: vv.tolist() if isinstance(vv, np.ndarray) else vv for kk, vv in v.items()}
939938
if array_as_list and v is not None and isinstance(v, np.ndarray):
940939
v = v.tolist()
941940
if v is not None:

‎tests/test_probe.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ def test_copy_is_independent():
335335
assert probe.contact_ids[0] == "c0"
336336

337337

338+
def test_to_dict_array_as_list_does_not_modify_probe():
339+
probe = _annotated_probe()
340+
341+
d = probe.to_dict(array_as_list=True)
342+
343+
assert isinstance(d["contact_annotations"]["impedance"], list)
344+
assert isinstance(probe.contact_annotations["impedance"], np.ndarray)
345+
# slicing relies on contact annotations staying numpy arrays
346+
sliced = probe.get_slice(np.arange(2))
347+
np.testing.assert_array_equal(sliced.contact_annotations["impedance"], probe.contact_annotations["impedance"][:2])
348+
349+
338350
def _probe_3d_for_rotation():
339351
probe = Probe(ndim=2)
340352
positions = np.array([[0.0, 0.0], [10.0, 0.0], [0.0, 20.0], [10.0, 20.0]])

0 commit comments

Comments
 (0)