Skip to content

Commit 06cf520

Browse files
authored
Add sparse option (#22)
Merges #22
1 parent 8c809c1 commit 06cf520

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

xswap/prior.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def compute_xswap_occurrence_matrix(edge_list: List[Tuple[int, int]],
1212
shape: Tuple[int, int],
1313
allow_self_loops: bool = False,
1414
allow_antiparallel: bool = False,
15+
sparse: bool = True,
1516
swap_multiplier: float = 10,
1617
initial_seed: int = 0,
1718
max_malloc: int = 4000000000):
@@ -46,6 +47,10 @@ def compute_xswap_occurrence_matrix(edge_list: List[Tuple[int, int]],
4647
of bipartite graphs, these edges represent two connections between four
4748
distinct nodes, while for other graphs, these may be connections between
4849
the same two nodes.
50+
sparse : bool
51+
Whether to use a sparse matrix when adding up edge occurrences across
52+
permutations. If large changes in sparsity are expected, a dense
53+
array may be preferable.
4954
swap_multiplier : float
5055
The number of edge swap attempts is determined by the product of the
5156
number of existing edges and multiplier. For example, if five edges are
@@ -79,23 +84,26 @@ def compute_xswap_occurrence_matrix(edge_list: List[Tuple[int, int]],
7984

8085
max_id = max(map(max, edge_list))
8186

82-
edge_counter = scipy.sparse.csc_matrix(shape, dtype=int)
87+
if sparse:
88+
edge_counter = scipy.sparse.csc_matrix(shape, dtype=int)
89+
else:
90+
edge_counter = numpy.zeros(shape, dtype=int)
8391

8492
for i in range(n_permutations):
8593
permuted_edges, stats = xswap._xswap_backend._xswap(
8694
edge_list, [], max_id, allow_self_loops, allow_antiparallel,
8795
num_swaps, initial_seed + i, max_malloc)
8896
permuted_matrix = xswap.network_formats.edges_to_matrix(
8997
permuted_edges, add_reverse_edges=(not allow_antiparallel),
90-
shape=shape, dtype=int, sparse=True)
98+
shape=shape, dtype=int, sparse=sparse)
9199
edge_counter += permuted_matrix
92100

93101
return edge_counter
94102

95103

96104
def compute_xswap_priors(edge_list: List[Tuple[int, int]], n_permutations: int,
97105
shape: Tuple[int, int], allow_self_loops: bool = False,
98-
allow_antiparallel: bool = False,
106+
allow_antiparallel: bool = False, sparse: bool = True,
99107
swap_multiplier: int = 10, initial_seed: int = 0,
100108
max_malloc: int = 4000000000,
101109
dtypes = {'id': numpy.uint16, 'degree': numpy.uint16,
@@ -134,6 +142,10 @@ def compute_xswap_priors(edge_list: List[Tuple[int, int]], n_permutations: int,
134142
of bipartite graphs, these edges represent two connections between four
135143
distinct nodes, while for other graphs, these may be connections between
136144
the same two nodes.
145+
sparse : bool
146+
Whether to use a sparse matrix when adding up edge occurrences across
147+
permutations. If large changes in sparsity are expected, a dense
148+
array may be preferable.
137149
swap_multiplier : float
138150
The number of edge swap attempts is determined by the product of the
139151
number of existing edges and multiplier. For example, if five edges are
@@ -193,7 +205,7 @@ def compute_xswap_priors(edge_list: List[Tuple[int, int]], n_permutations: int,
193205
edge_counter = compute_xswap_occurrence_matrix(
194206
edge_list=edge_list, n_permutations=n_permutations, shape=shape,
195207
allow_self_loops=allow_self_loops, allow_antiparallel=allow_antiparallel,
196-
swap_multiplier=swap_multiplier, initial_seed=initial_seed,
208+
sparse=sparse, swap_multiplier=swap_multiplier, initial_seed=initial_seed,
197209
max_malloc=max_malloc)
198210

199211
prior_df['num_permuted_edges'] = edge_counter.toarray().flatten()

0 commit comments

Comments
 (0)