|
8 | 8 | _possible_contact_shapes = ["circle", "square", "rect"] |
9 | 9 |
|
10 | 10 |
|
| 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 | + |
11 | 43 | class Probe: |
12 | 44 | """ |
13 | 45 | Class to handle the geometry of one probe. |
@@ -281,24 +313,10 @@ def set_contacts( |
281 | 313 |
|
282 | 314 | # Check for duplicate positions |
283 | 315 | 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) |
302 | 320 |
|
303 | 321 | self._contact_positions = positions |
304 | 322 | n = positions.shape[0] |
|
0 commit comments