Skip to content

Commit 14d4cf2

Browse files
authored
Merge pull request #401 from h-mayorquin/fix_wrong_error
Fix missing `raise` in `Probe.set_contact_ids()` size validation
2 parents 55fbcb7 + 4b10297 commit 14d4cf2

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/probeinterface/probe.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,15 @@ def set_contact_ids(self, contact_ids: np.array | list):
570570
self._contact_ids = None
571571
return
572572

573-
assert np.unique(contact_ids).size == contact_ids.size, "Contact ids have to be unique within a Probe"
574-
575573
if contact_ids.size != self.get_contact_count():
576-
ValueError(
574+
raise ValueError(
577575
f"contact_ids {contact_ids.size} do not have the same size "
578576
f"as number of contacts {self.get_contact_count()}"
579577
)
580578

579+
if np.unique(contact_ids).size != contact_ids.size:
580+
raise ValueError("contact_ids must be unique within a Probe")
581+
581582
if contact_ids.dtype.kind != "U":
582583
contact_ids = contact_ids.astype("U")
583584

tests/test_probegroup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ def test_probegroup_allows_duplicate_positions_across_probes():
9292
assert len(group.probes) == 2
9393

9494

95+
def test_set_contact_ids_rejects_within_probe_duplicates():
96+
"""Setting duplicate contact_ids within a single probe raises ValueError."""
97+
from probeinterface import Probe
98+
99+
positions = np.array([[0, 0], [10, 10]])
100+
probe = Probe(ndim=2, si_units="um")
101+
probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5})
102+
103+
with pytest.raises(ValueError, match="unique within a Probe"):
104+
probe.set_contact_ids(["a", "a"])
105+
106+
107+
def test_set_contact_ids_rejects_wrong_size():
108+
"""Setting contact_ids with wrong count raises ValueError."""
109+
from probeinterface import Probe
110+
111+
positions = np.array([[0, 0], [10, 10]])
112+
probe = Probe(ndim=2, si_units="um")
113+
probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5})
114+
115+
with pytest.raises(ValueError, match="do not have the same size"):
116+
probe.set_contact_ids(["a", "b", "c"])
117+
118+
95119
if __name__ == "__main__":
96120
test_probegroup()
97121
# ~ test_probegroup_3d()

0 commit comments

Comments
 (0)