Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit a8f75e4

Browse files
authored
Fixing the iter_connection with unique_node_ids (#57)
* Fixing the iter_connection with unique_node_ids * fixing the function * adding comments and explanation in docstrings * fixing the pylint in tox due to the multiprocessing error pylint-dev/pylint#3524 * Change node_id to node ID in doc
1 parent da7389e commit a8f75e4

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

bluepysnap/edges.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
class EdgeStorage(object):
3636
"""Edge storage access."""
37+
3738
def __init__(self, config, circuit):
3839
"""Initializes a EdgeStorage object from a edge config and a Circuit.
3940
@@ -369,6 +370,7 @@ def _optimal_direction():
369370
if target_node_ids is None:
370371
return 'source'
371372
else:
373+
# Checking the indexing 'direction'. One direction has contiguous indices.
372374
range_size_source = _estimate_range_size(
373375
self._population.efferent_edges, source_node_ids
374376
)
@@ -399,6 +401,7 @@ def _optimal_direction():
399401

400402
for key_node_id in primary_node_ids:
401403
connected_node_ids = get_connected_node_ids(key_node_id, unique=False)
404+
# [[secondary_node_id, count], ...]
402405
connected_node_ids_with_count = np.stack(
403406
np.unique(connected_node_ids, return_counts=True)
404407
).transpose()
@@ -410,7 +413,10 @@ def _optimal_direction():
410413
connected_node_ids_with_count = connected_node_ids_with_count[mask]
411414
if shuffle:
412415
np.random.shuffle(connected_node_ids_with_count)
416+
413417
for conn_node_id, edge_count in connected_node_ids_with_count:
418+
if unique_node_ids and (conn_node_id in secondary_node_ids_used):
419+
continue
414420
if direction == 'target':
415421
yield conn_node_id, key_node_id, edge_count
416422
else:
@@ -428,7 +434,9 @@ def iter_connections(
428434
Args:
429435
source: source node group
430436
target: target node group
431-
unique_node_ids: if True, no node ID would be used more than once
437+
unique_node_ids: if True, no node ID will be used more than once as source or
438+
target for edges. Careful, this flag does not provide unique (source, target)
439+
pairs but unique node IDs.
432440
shuffle: if True, result order would be (somewhat) randomized
433441
return_edge_count: if True, edge count is added to yield result
434442
return_edge_ids: if True, edge ID list is added to yield result

tests/data/edges_complete_graph.h5

58.6 KB
Binary file not shown.

tests/test_edges.py

+24
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,27 @@ def test_iter_connections_10(self):
420420
self.test_obj.iter_connections(
421421
[0, 2], [1], return_edge_ids=True, return_edge_count=True
422422
)
423+
424+
def test_iter_connection_unique(self):
425+
test_obj = TestEdgePopulation.create_population(
426+
str(TEST_DATA_DIR / "edges_complete_graph.h5"), 'default')
427+
it = test_obj.iter_connections([0, 1, 2], [0, 1, 2])
428+
assert sorted(it) == [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
429+
430+
it = test_obj.iter_connections([0, 1, 2], [0, 1, 2], unique_node_ids=True)
431+
assert sorted(it) == [(0, 1), (1, 0)]
432+
433+
it = test_obj.iter_connections([0, 1, 2], [0, 2], unique_node_ids=True)
434+
assert sorted(it) == [(0, 2), (1, 0)]
435+
436+
it = test_obj.iter_connections([0, 2], [0, 2], unique_node_ids=True)
437+
assert sorted(it) == [(0, 2), (2, 0)]
438+
439+
it = test_obj.iter_connections([0, 1, 2], [0, 2, 1], unique_node_ids=True)
440+
assert sorted(it) == [(0, 1), (1, 0)]
441+
442+
it = test_obj.iter_connections([1, 2], [0, 1, 2], unique_node_ids=True)
443+
assert sorted(it) == [(1, 0), (2, 1)]
444+
445+
it = test_obj.iter_connections([0, 1, 2], [1, 2], unique_node_ids=True)
446+
assert sorted(it) == [(0, 1), (1, 2)]

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ basepython=python3.6
1919
deps =
2020
pycodestyle
2121
pydocstyle
22-
pylint
22+
pylint==2.4.4 # see https://github.com/PyCQA/pylint/issues/3524
2323
commands =
2424
pycodestyle {[base]name}
2525
pydocstyle {[base]name}

0 commit comments

Comments
 (0)