Skip to content

Commit d8c77aa

Browse files
committed
profile
1 parent 7b974b7 commit d8c77aa

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/probeinterface/probe.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@
88
_possible_contact_shapes = ["circle", "square", "rect"]
99

1010

11+
def _raise_non_unique_positions(positions):
12+
"""
13+
Check for duplicate positions and raise ValueError with detailed information.
14+
15+
Parameters
16+
----------
17+
positions : array
18+
Array of positions to check for duplicates.
19+
20+
Raises
21+
------
22+
ValueError
23+
If duplicate positions are found, with detailed information about duplicates.
24+
"""
25+
duplicates = {}
26+
for index, pos in enumerate(positions):
27+
pos_key = tuple(pos)
28+
if pos_key in duplicates:
29+
duplicates[pos_key].append(index)
30+
else:
31+
duplicates[pos_key] = [index]
32+
33+
duplicate_groups = {pos: indices for pos, indices in duplicates.items() if len(indices) > 1}
34+
duplicate_info = []
35+
for pos, indices in duplicate_groups.items():
36+
pos_str = f"({', '.join(map(str, pos))})"
37+
indices_str = f"[{', '.join(map(str, indices))}]"
38+
duplicate_info.append(f"Position {pos_str} appears at indices {indices_str}")
39+
40+
raise ValueError(f"Contact positions must be unique within a probe. Found {len(duplicate_groups)} duplicate(s): {'; '.join(duplicate_info)}")
41+
42+
1143
class Probe:
1244
"""
1345
Class to handle the geometry of one probe.
@@ -281,24 +313,10 @@ def set_contacts(
281313

282314
# Check for duplicate positions
283315
unique_positions = np.unique(positions, axis=0)
284-
if len(unique_positions) != len(positions):
285-
# Find and report duplicates
286-
duplicates = {}
287-
for index, pos in enumerate(positions):
288-
pos_key = tuple(pos)
289-
if pos_key in duplicates:
290-
duplicates[pos_key].append(index)
291-
else:
292-
duplicates[pos_key] = [index]
293-
294-
duplicate_groups = {pos: indices for pos, indices in duplicates.items() if len(indices) > 1}
295-
duplicate_info = []
296-
for pos, indices in duplicate_groups.items():
297-
pos_str = f"({', '.join(map(str, pos))})"
298-
indices_str = f"[{', '.join(map(str, indices))}]"
299-
duplicate_info.append(f"Position {pos_str} appears at indices {indices_str}")
300-
301-
raise ValueError(f"Contact positions must be unique within a probe. Found {len(duplicate_groups)} duplicate(s): {'; '.join(duplicate_info)}")
316+
are_positions_unique = unique_positions.shape[0] == positions.shape[0]
317+
318+
if not are_positions_unique:
319+
_raise_non_unique_positions(positions)
302320

303321
self._contact_positions = positions
304322
n = positions.shape[0]

0 commit comments

Comments
 (0)