From 561af3126f3dea61bbff6ca99e9b0c3a5c98238c Mon Sep 17 00:00:00 2001 From: aaronkanzer Date: Mon, 9 Nov 2026 16:39:45 -0500 Subject: [PATCH] Do not convert a probe's annotations to lists in Probe.to_dict --- src/probeinterface/probe.py | 5 ++--- tests/test_probe.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/probeinterface/probe.py b/src/probeinterface/probe.py index 25debf23..0d7f0d0c 100644 --- a/src/probeinterface/probe.py +++ b/src/probeinterface/probe.py @@ -933,9 +933,8 @@ def to_dict(self, array_as_list: bool = False) -> dict: for k in self._dump_attr_names: v = getattr(self, k, None) if array_as_list and isinstance(v, dict): - for kk, vv in v.items(): - if isinstance(vv, np.ndarray): - v[kk] = vv.tolist() + # build a new dict so the probe's own annotations are not converted in place + v = {kk: vv.tolist() if isinstance(vv, np.ndarray) else vv for kk, vv in v.items()} if array_as_list and v is not None and isinstance(v, np.ndarray): v = v.tolist() if v is not None: diff --git a/tests/test_probe.py b/tests/test_probe.py index 129c62fb..4d045290 100644 --- a/tests/test_probe.py +++ b/tests/test_probe.py @@ -335,6 +335,18 @@ def test_copy_is_independent(): assert probe.contact_ids[0] == "c0" +def test_to_dict_array_as_list_does_not_modify_probe(): + probe = _annotated_probe() + + d = probe.to_dict(array_as_list=True) + + assert isinstance(d["contact_annotations"]["impedance"], list) + assert isinstance(probe.contact_annotations["impedance"], np.ndarray) + # slicing relies on contact annotations staying numpy arrays + sliced = probe.get_slice(np.arange(2)) + np.testing.assert_array_equal(sliced.contact_annotations["impedance"], probe.contact_annotations["impedance"][:2]) + + def _probe_3d_for_rotation(): probe = Probe(ndim=2) positions = np.array([[0.0, 0.0], [10.0, 0.0], [0.0, 20.0], [10.0, 20.0]])