modularitypruning 1.5.0__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,7 @@
1
1
  # this makes prune_to_stable_partitions and prune_to_multilayer_stable_partitions importable from the top-level package
2
- from .parameter_estimation_utilities import prune_to_stable_partitions, prune_to_multilayer_stable_partitions # noqa
2
+ from .parameter_estimation_utilities import (
3
+ prune_to_multilayer_stable_partitions as prune_to_multilayer_stable_partitions,
4
+ prune_to_stable_partitions as prune_to_stable_partitions,
5
+ )
6
+
7
+ __all__ = ["prune_to_stable_partitions", "prune_to_multilayer_stable_partitions"]
@@ -1,3 +1,7 @@
1
+ from typing import TYPE_CHECKING
2
+ if TYPE_CHECKING:
3
+ import igraph
4
+ import multiprocessing.pool
1
5
  import warnings
2
6
  from collections import defaultdict
3
7
  from math import floor
@@ -9,11 +13,15 @@ from scipy.linalg import LinAlgWarning
9
13
  from scipy.optimize import linprog, OptimizeWarning
10
14
  from scipy.spatial import HalfspaceIntersection
11
15
 
12
- from .partition_utilities import all_degrees, in_degrees, out_degrees, membership_to_communities, \
13
- membership_to_layered_communities
16
+ from .partition_utilities import (all_strengths, in_strengths, out_strengths,
17
+ membership_to_communities, membership_to_layered_communities)
18
+ from .types import (Domain, FloatArray, GammaRange, LayerVector, Membership,
19
+ Partitions, PartitionCoefficients2D, PartitionCoefficients3D)
14
20
 
15
21
 
16
- def get_interior_point(halfspaces, initial_num_sampled=50, full_retry_limit=10):
22
+ def get_interior_point(
23
+ halfspaces: FloatArray, initial_num_sampled: int = 50, full_retry_limit: int = 10
24
+ ) -> FloatArray:
17
25
  """
18
26
  Find interior point of halfspaces (needed to perform halfspace intersection)
19
27
 
@@ -41,19 +49,21 @@ def get_interior_point(halfspaces, initial_num_sampled=50, full_retry_limit=10):
41
49
  else:
42
50
  raise ValueError(f"get_interior_point received unhandled dimension {normals.shape[1]}!")
43
51
 
44
- num_retries = 0
52
+ num_retries: int = 0
45
53
 
46
54
  while True: # retry until success
47
55
  # randomly sample some of the halfspaces
48
- sample_len = min(initial_num_sampled, len(interior_hs)) # len(interior_hs)
49
- sampled_hs = np.vstack((interior_hs[choice(interior_hs.shape[0], sample_len, replace=False)], boundaries))
56
+ sample_len: int = min(initial_num_sampled, len(interior_hs)) # len(interior_hs)
57
+ sampled_hs: FloatArray = np.vstack((
58
+ interior_hs[choice(interior_hs.shape[0], sample_len, replace=False)], boundaries))
50
59
 
51
60
  # compute the Chebyshev center of the sampled halfspaces' intersection
52
- norm_vector = np.reshape(np.linalg.norm(sampled_hs[:, :-1], axis=1), (sampled_hs.shape[0], 1))
53
- c = np.zeros((sampled_hs.shape[1],))
61
+ norm_vector: FloatArray = np.reshape(
62
+ np.linalg.norm(sampled_hs[:, :-1], axis=1), (sampled_hs.shape[0], 1))
63
+ c: FloatArray = np.zeros((sampled_hs.shape[1],))
54
64
  c[-1] = -1
55
- A = np.hstack((sampled_hs[:, :-1], norm_vector))
56
- b = -sampled_hs[:, -1:]
65
+ A: FloatArray = np.hstack((sampled_hs[:, :-1], norm_vector))
66
+ b: FloatArray = -sampled_hs[:, -1:]
57
67
 
58
68
  res = linprog(c, A_ub=A, b_ub=b, bounds=(-np.inf, np.inf), method='highs-ipm')
59
69
 
@@ -65,7 +75,10 @@ def get_interior_point(halfspaces, initial_num_sampled=50, full_retry_limit=10):
65
75
  # 3 : Problem appears to be unbounded.
66
76
  # 4 : Numerical difficulties encountered.
67
77
  if res.success or res.status in {0, 1, 4}:
68
- intpt = res.x[:-1] # res.x contains [interior_point, distance to enclosing polyhedron]
78
+ if res.x is None:
79
+ # satisfying mypy, this should never occur
80
+ raise ValueError(f"Scipy's linprog claimed success but returned no solution! {res}")
81
+ intpt: FloatArray = res.x[:-1] # res.x contains [interior_point, distance to enclosing polyhedron]
69
82
 
70
83
  # ensure that the computed point is actually interior to all halfspaces
71
84
  if (np.dot(normals, intpt) + np.transpose(offsets) < 0).all():
@@ -86,15 +99,20 @@ def get_interior_point(halfspaces, initial_num_sampled=50, full_retry_limit=10):
86
99
  return intpt
87
100
 
88
101
 
89
- def CHAMP_2D(G, all_parts, gamma_0, gamma_f, single_threaded=False):
102
+ def CHAMP_2D(
103
+ G: "igraph.Graph", all_parts: Partitions, gamma_0: float, gamma_f: float,
104
+ single_threaded: bool = False
105
+ ) -> list[GammaRange]:
90
106
  r"""Calculates the pruned set of partitions from CHAMP on ``gamma_0`` :math:`\leq \gamma \leq` ``gamma_f``.
91
107
 
92
108
  See https://doi.org/10.3390/a10030093 for more details.
93
109
 
110
+ Directed, undirected, weighted, and unweighted graphs are supported.
111
+
94
112
  :param G: graph of interest
95
113
  :type G: igraph.Graph
96
114
  :param all_parts: partitions to prune
97
- :type all_parts: list[tuple] or set[tuple]
115
+ :type all_parts: Sequence[tuple[int, ...]] | set[tuple[int, ...]]
98
116
  :param gamma_0: starting gamma value for CHAMP
99
117
  :type gamma_0: float
100
118
  :param gamma_f: ending gamma value for CHAMP
@@ -106,41 +124,42 @@ def CHAMP_2D(G, all_parts, gamma_0, gamma_f, single_threaded=False):
106
124
  - starting gamma value for the partition's domain of optimality
107
125
  - ending gamma value for the partition's domain of optimality
108
126
  - community membership tuple for the partition
109
- :rtype: list of tuple[float, float, tuple[int]]
127
+ :rtype: list of tuple[float, float, tuple[int, ...]]
110
128
  """
111
129
 
112
130
  if len(all_parts) == 0:
113
131
  return []
114
132
 
115
133
  all_parts = list(all_parts)
116
- num_partitions = len(all_parts)
134
+ num_partitions: int = len(all_parts)
117
135
 
118
- partition_coefficients = partition_coefficients_2D(G, all_parts, single_threaded=single_threaded)
136
+ partition_coefficients: PartitionCoefficients2D = partition_coefficients_2D(
137
+ G, all_parts, single_threaded=single_threaded)
119
138
  A_hats, P_hats = partition_coefficients
120
139
 
121
140
  # add on boundaries: bottom < Q < top, gamma_0 < gamma < gamma_f
122
- top = max(A_hats - P_hats * gamma_0) # Could potentially be optimized
123
- bottom = min(A_hats - P_hats * gamma_f) # Could potentially be optimized
124
- halfspaces = np.vstack((halfspaces_from_coefficients_2D(*partition_coefficients),
125
- np.array([[0, 1, -top], [0, -1, bottom],
126
- [1, 0, -gamma_f], [-1, 0, gamma_0]])))
141
+ top: np.float64 = max(A_hats - P_hats * gamma_0) # Could potentially be optimized
142
+ bottom: np.float64 = min(A_hats - P_hats * gamma_f) # Could potentially be optimized
143
+ halfspaces: FloatArray = np.vstack((halfspaces_from_coefficients_2D(*partition_coefficients),
144
+ np.array([[0, 1, -top], [0, -1, bottom],
145
+ [1, 0, -gamma_f], [-1, 0, gamma_0]])))
127
146
 
128
147
  # normalize halfspaces to try to improve ill-conditioned linear programs
129
148
  halfspaces = halfspaces / np.linalg.norm(halfspaces, axis=1, keepdims=True)
130
149
 
131
150
  # Could potentially scale axes so Chebyshev center is better for problem
132
- interior_point = get_interior_point(halfspaces)
133
- hs = HalfspaceIntersection(halfspaces, interior_point)
151
+ interior_point: FloatArray = get_interior_point(halfspaces)
152
+ hs: HalfspaceIntersection = HalfspaceIntersection(halfspaces, interior_point)
134
153
 
135
154
  # scipy does not support facets by halfspace directly, so we must compute them
136
- facets_by_halfspace = defaultdict(list)
155
+ facets_by_halfspace: defaultdict[int, list[FloatArray]] = defaultdict(list)
137
156
  for v, idx in zip(hs.intersections, hs.dual_facets):
138
157
  assert np.isfinite(v).all()
139
158
  for i in idx:
140
159
  if i < num_partitions:
141
160
  facets_by_halfspace[i].append(v)
142
161
 
143
- ranges = []
162
+ ranges: list[GammaRange] = []
144
163
  for i, intersections in facets_by_halfspace.items():
145
164
  x1, x2 = intersections[0][0], intersections[1][0]
146
165
  if x1 > x2:
@@ -150,12 +169,17 @@ def CHAMP_2D(G, all_parts, gamma_0, gamma_f, single_threaded=False):
150
169
  return sorted(ranges, key=lambda x: x[0])
151
170
 
152
171
 
153
- def CHAMP_3D(G_intralayer, G_interlayer, layer_vec, all_parts, gamma_0, gamma_f, omega_0, omega_f):
172
+ def CHAMP_3D(
173
+ G_intralayer: "igraph.Graph", G_interlayer: "igraph.Graph", layer_vec: LayerVector,
174
+ all_parts: Partitions, gamma_0: float, gamma_f: float, omega_0: float, omega_f: float
175
+ ) -> list[Domain]:
154
176
  r"""Calculates the pruned set of partitions from CHAMP on ``gamma_0`` :math:`\leq \gamma \leq` ``gamma_f`` and
155
177
  ``omega_0`` :math:`\leq \omega \leq` ``omega_f``.
156
178
 
157
179
  See https://doi.org/10.3390/a10030093 for more details.
158
180
 
181
+ Both the intralayer and interlayer graphs may be directed or undirected, weighted or unweighted.
182
+
159
183
  :param G_intralayer: intralayer graph of interest
160
184
  :type G_intralayer: igraph.Graph
161
185
  :param G_interlayer: interlayer graph of interest
@@ -163,7 +187,7 @@ def CHAMP_3D(G_intralayer, G_interlayer, layer_vec, all_parts, gamma_0, gamma_f,
163
187
  :param layer_vec: list of each vertex's layer membership
164
188
  :type layer_vec: list[int]
165
189
  :param all_parts: partitions to prune
166
- :type all_parts: iterable[tuple]
190
+ :type all_parts: Sequence[tuple[int, ...]] | set[tuple[int, ...]]
167
191
  :param gamma_0: starting gamma value for CHAMP
168
192
  :type gamma_0: float
169
193
  :param gamma_f: ending gamma value for CHAMP
@@ -176,100 +200,106 @@ def CHAMP_3D(G_intralayer, G_interlayer, layer_vec, all_parts, gamma_0, gamma_f,
176
200
 
177
201
  - list of polygon vertices in (gamma, omega) plane for the partition's domain of optimality
178
202
  - community membership tuple for the partition
179
- :rtype: list of tuple[list[float], tuple[int]]
203
+ :rtype: list of tuple[list[numpy.ndarray], tuple[int, ...]]
180
204
  """
181
205
 
182
206
  if len(all_parts) == 0:
183
207
  return []
184
208
 
185
209
  all_parts = list(all_parts)
186
- num_partitions = len(all_parts)
210
+ num_partitions: int = len(all_parts)
187
211
 
188
- partition_coefficients = partition_coefficients_3D(G_intralayer, G_interlayer, layer_vec, all_parts)
212
+ partition_coefficients: PartitionCoefficients3D = partition_coefficients_3D(
213
+ G_intralayer, G_interlayer, layer_vec, all_parts)
189
214
  A_hats, P_hats, C_hats = partition_coefficients
190
215
 
191
216
  # add on boundaries: bottom < Q < top, gamma_0 < gamma < gamma_f, omega_0 < omega < omega_f
192
- top = max(A_hats - P_hats * gamma_0 + C_hats * omega_f) # Could potentially be optimized
193
- bottom = min(A_hats - P_hats * gamma_f + C_hats * omega_0) # Could potentially be optimized
194
- halfspaces = np.vstack((halfspaces_from_coefficients_3D(*partition_coefficients),
195
- (np.array([[0, 0, 1, -top], [0, 0, -1, bottom],
196
- [1, 0, 0, -gamma_f], [-1, 0, 0, gamma_0],
197
- [0, 1, 0, -omega_f], [0, -1, 0, omega_0]]))))
217
+ top: np.float64 = max(A_hats - P_hats * gamma_0 + C_hats * omega_f) # Could potentially be optimized
218
+ bottom: np.float64 = min(A_hats - P_hats * gamma_f + C_hats * omega_0) # Could potentially be optimized
219
+ halfspaces: FloatArray = np.vstack((halfspaces_from_coefficients_3D(*partition_coefficients),
220
+ (np.array([[0, 0, 1, -top], [0, 0, -1, bottom],
221
+ [1, 0, 0, -gamma_f], [-1, 0, 0, gamma_0],
222
+ [0, 1, 0, -omega_f], [0, -1, 0, omega_0]]))))
198
223
 
199
224
  # normalize halfspaces to try to improve ill-conditioned linear programs
200
225
  halfspaces = halfspaces / np.linalg.norm(halfspaces, axis=1, keepdims=True)
201
226
 
202
227
  # Could potentially scale axes so Chebyshev center is better for problem
203
- interior_point = get_interior_point(halfspaces)
204
- hs = HalfspaceIntersection(halfspaces, interior_point)
228
+ interior_point: FloatArray = get_interior_point(halfspaces)
229
+ hs: HalfspaceIntersection = HalfspaceIntersection(halfspaces, interior_point)
205
230
 
206
231
  # scipy does not support facets by halfspace directly, so we must compute them
207
- facets_by_halfspace = defaultdict(list)
232
+ facets_by_halfspace: defaultdict[int, list[FloatArray]] = defaultdict(list)
208
233
  for v, idx in zip(hs.intersections, hs.dual_facets):
209
234
  assert np.isfinite(v).all()
210
235
  for i in idx:
211
236
  if i < num_partitions:
212
237
  facets_by_halfspace[i].append(v)
213
238
 
214
- domains = []
239
+ domains: list[Domain] = []
215
240
  for i, intersections in facets_by_halfspace.items():
216
241
  # drop quality dimension / project into (gamma, omega) plane
217
242
  intersections = [x[:2] for x in intersections]
218
243
  # sort the domain's points counter-clockwise around the centroid
219
- centroid = np.mean(intersections, axis=0)
244
+ centroid: FloatArray = np.mean(intersections, axis=0)
220
245
  intersections.sort(key=lambda x: np.arctan2(x[0] - centroid[0], x[1] - centroid[1]))
221
246
  domains.append((intersections, all_parts[i]))
222
247
 
223
248
  return domains
224
249
 
225
250
 
226
- def partition_coefficients_2D_serial(G, partitions):
227
- """Computes A_hat and P_hat for partitions of :G:
228
-
229
- TODO: support edge weights"""
251
+ def partition_coefficients_2D_serial(G: "igraph.Graph", partitions: Partitions
252
+ ) -> PartitionCoefficients2D:
253
+ """Computes A_hat and P_hat for partitions of :G:"""
230
254
 
231
- all_edges = [(e.source, e.target) for e in G.es]
255
+ weights = G.es['weight'] if G.is_weighted() else [1.0] * G.ecount()
256
+ total_weight = float(sum(weights))
257
+ all_edges = [(e.source, e.target, w) for e, w in zip(G.es, weights)]
232
258
 
233
259
  # multiply by 2 only if undirected here
234
260
  if G.is_directed():
235
- A_hats = np.array([sum([membership[u] == membership[v] for u, v in all_edges])
236
- for membership in partitions])
261
+ A_hats = np.array([sum(w for u, v, w in all_edges if membership[u] == membership[v])
262
+ for membership in partitions], dtype=np.float64)
237
263
  else:
238
- A_hats = np.array([2 * sum([membership[u] == membership[v] for u, v in all_edges])
239
- for membership in partitions])
264
+ A_hats = np.array([2 * sum(w for u, v, w in all_edges if membership[u] == membership[v])
265
+ for membership in partitions], dtype=np.float64)
240
266
 
241
267
  if G.is_directed():
242
268
  # directed modularity of Leicht and Newman is actually
243
269
  # (1/m) sum_{ij} [A_{ij} - k_i^{in} * k_j^{out} / m] delta(c_i, c_j)
244
- in_degree = in_degrees(G)
245
- out_degree = out_degrees(G)
246
- P_hats = np.array([sum(sum(in_degree[v] for v in vs) * sum(out_degree[v] for v in vs)
270
+ in_strength = in_strengths(G)
271
+ out_strength = out_strengths(G)
272
+ P_hats = np.array([sum(sum(in_strength[v] for v in vs) * sum(out_strength[v] for v in vs)
247
273
  for vs in membership_to_communities(membership).values())
248
- for membership in partitions]) / G.ecount()
274
+ for membership in partitions]) / total_weight
249
275
  else:
250
- twom = 2 * G.ecount()
251
- degree = all_degrees(G)
252
- P_hats = np.array([sum(sum(degree[v] for v in vs) ** 2 for vs in membership_to_communities(membership).values())
276
+ twom = 2 * total_weight
277
+ strength = all_strengths(G)
278
+ P_hats = np.array([sum(sum(strength[v] for v in vs) ** 2
279
+ for vs in membership_to_communities(membership).values())
253
280
  for membership in partitions]) / twom
254
281
 
255
282
  return A_hats, P_hats
256
283
 
257
284
 
258
- def partition_coefficients_2D(G, partitions, single_threaded=False):
285
+ def partition_coefficients_2D(
286
+ G: "igraph.Graph", partitions: Partitions, single_threaded: bool = False
287
+ ) -> PartitionCoefficients2D:
259
288
  """Computes partitions coefficients in parallel by calling partition_coefficients_2D_serial"""
260
289
  partitions = list(partitions)
261
290
 
262
291
  if single_threaded:
263
292
  A_hats, P_hats = partition_coefficients_2D_serial(G, partitions)
264
293
  else:
265
- partition_chunks = [
294
+ partition_chunks: list[list[Membership]] = [
266
295
  partitions[floor(i * len(partitions) / cpu_count()):floor((i + 1) * len(partitions) / cpu_count())]
267
296
  for i in range(cpu_count())
268
297
  ]
269
298
 
270
- pool = Pool(processes=cpu_count())
271
- results = pool.starmap(partition_coefficients_2D_serial,
272
- [(G, partition_chunk) for partition_chunk in partition_chunks])
299
+ pool: "multiprocessing.pool.Pool" = Pool(processes=cpu_count())
300
+ results: list[PartitionCoefficients2D] = \
301
+ pool.starmap(partition_coefficients_2D_serial,
302
+ [(G, partition_chunk) for partition_chunk in partition_chunks])
273
303
  pool.close()
274
304
 
275
305
  A_hats = np.array([v for A_hats, P_hats in results for v in A_hats])
@@ -280,92 +310,101 @@ def partition_coefficients_2D(G, partitions, single_threaded=False):
280
310
  return A_hats, P_hats
281
311
 
282
312
 
283
- def halfspaces_from_coefficients_2D(A_hats, P_hats):
313
+ def halfspaces_from_coefficients_2D(
314
+ A_hats: FloatArray, P_hats: FloatArray
315
+ ) -> FloatArray:
284
316
  """Converts partitions' coefficients to halfspace normal, offset
285
317
 
286
318
  Q >= -P_hat*gamma + A_hat
287
319
  -Q - P_hat*gamma + A_hat <= 0
288
320
  (-P_hat, -1) * (gamma, Q) + A_hat <= 0
289
321
  """
290
- return np.vstack((-P_hats, -np.ones_like(P_hats), A_hats)).T
322
+ return np.vstack((-P_hats, -np.ones_like(P_hats), A_hats)).T.astype(np.float64)
291
323
 
292
324
 
293
- def halfspaces_from_coefficients_3D(A_hats, P_hats, C_hats):
325
+ def halfspaces_from_coefficients_3D(
326
+ A_hats: FloatArray, P_hats: FloatArray, C_hats: FloatArray
327
+ ) -> FloatArray:
294
328
  """Converts partitions' coefficients to halfspace normal, offset
295
329
 
296
330
  Q >= C_hat*omega - P_hat*gamma + A_hat
297
331
  -Q + C_hat*omega - P_hat*gamma + A_hat <= 0
298
332
  (-P_hat, C_hat, -1) * (gamma, omega, Q) + A_hat <= 0
299
333
  """
300
- return np.vstack((-P_hats, C_hats, -np.ones_like(P_hats), A_hats)).T
334
+ return np.vstack((-P_hats, C_hats, -np.ones_like(P_hats), A_hats)).T.astype(np.float64)
301
335
 
302
336
 
303
- def partition_coefficients_3D_serial(G_intralayer, G_interlayer, layer_vec, partitions):
337
+ def partition_coefficients_3D_serial(G_intralayer: "igraph.Graph", G_interlayer: "igraph.Graph",
338
+ layer_vec: LayerVector,
339
+ partitions: list[Membership]) -> PartitionCoefficients3D:
304
340
  """Computes A_hat, P_hat, C_hat for partitions of a graph with intralayer edges given in :G_intralayer:,
305
- interlayer edges given in :G_interlayer:, and layer membership :layer_vec:
306
-
307
- TODO: support edge weights"""
341
+ interlayer edges given in :G_interlayer:, and layer membership :layer_vec:"""
308
342
 
309
- all_intralayer_edges = [(e.source, e.target) for e in G_intralayer.es]
310
- all_interlayer_edges = [(e.source, e.target) for e in G_interlayer.es]
343
+ intra_weights = G_intralayer.es['weight'] if G_intralayer.is_weighted() else [1.0] * G_intralayer.ecount()
344
+ inter_weights = G_interlayer.es['weight'] if G_interlayer.is_weighted() else [1.0] * G_interlayer.ecount()
345
+ all_intralayer_edges = [(e.source, e.target, w) for e, w in zip(G_intralayer.es, intra_weights)]
346
+ all_interlayer_edges = [(e.source, e.target, w) for e, w in zip(G_interlayer.es, inter_weights)]
311
347
 
312
348
  # multiply by 2 only if undirected here
313
349
  if G_intralayer.is_directed():
314
- A_hats = np.array([sum([membership[u] == membership[v] for u, v in all_intralayer_edges])
315
- for membership in partitions])
350
+ A_hats = np.array([sum(w for u, v, w in all_intralayer_edges if membership[u] == membership[v])
351
+ for membership in partitions], dtype=np.float64)
316
352
  else:
317
- A_hats = np.array([2 * sum([membership[u] == membership[v] for u, v in all_intralayer_edges])
318
- for membership in partitions])
353
+ A_hats = np.array([2 * sum(w for u, v, w in all_intralayer_edges if membership[u] == membership[v])
354
+ for membership in partitions], dtype=np.float64)
319
355
 
320
356
  num_layers = max(layer_vec) + 1
321
- ecount_per_layer = np.zeros(num_layers)
322
- for e in G_intralayer.es:
323
- ecount_per_layer[layer_vec[e.source]] += 1
357
+ weight_per_layer = np.zeros(num_layers)
358
+ for e, w in zip(G_intralayer.es, intra_weights):
359
+ weight_per_layer[layer_vec[e.source]] += w
324
360
 
325
361
  if G_intralayer.is_directed():
326
- in_degree = in_degrees(G_intralayer)
327
- out_degree = out_degrees(G_intralayer)
362
+ in_strength = in_strengths(G_intralayer)
363
+ out_strength = out_strengths(G_intralayer)
328
364
  P_hats = np.array([
329
- sum(sum(in_degree[v] for v in vs) * sum(out_degree[v] for v in vs) / ecount_per_layer[layer]
365
+ sum(sum(in_strength[v] for v in vs) * sum(out_strength[v] for v in vs) / weight_per_layer[layer]
330
366
  for (_, layer), vs in membership_to_layered_communities(membership, layer_vec).items())
331
367
  for membership in partitions
332
368
  ])
333
369
  else:
334
- degree = all_degrees(G_intralayer)
370
+ strength = all_strengths(G_intralayer)
335
371
  P_hats = np.array([
336
- sum(sum(degree[v] for v in vs) ** 2 / (2 * ecount_per_layer[layer])
372
+ sum(sum(strength[v] for v in vs) ** 2 / (2 * weight_per_layer[layer])
337
373
  for (_, layer), vs in membership_to_layered_communities(membership, layer_vec).items())
338
374
  for membership in partitions
339
375
  ])
340
376
 
341
377
  # multiply by 2 only if undirected here
342
378
  if G_interlayer.is_directed():
343
- C_hats = np.array([sum([membership[u] == membership[v] for u, v in all_interlayer_edges])
344
- for membership in partitions])
379
+ C_hats = np.array([sum(w for u, v, w in all_interlayer_edges if membership[u] == membership[v])
380
+ for membership in partitions], dtype=np.float64)
345
381
  else:
346
- C_hats = np.array([2 * sum([membership[u] == membership[v] for u, v in all_interlayer_edges])
347
- for membership in partitions])
382
+ C_hats = np.array([2 * sum(w for u, v, w in all_interlayer_edges if membership[u] == membership[v])
383
+ for membership in partitions], dtype=np.float64)
348
384
 
349
385
  return A_hats, P_hats, C_hats
350
386
 
351
387
 
352
- def partition_coefficients_3D(G_intralayer, G_interlayer, layer_vec, partitions):
388
+ def partition_coefficients_3D(
389
+ G_intralayer: "igraph.Graph", G_interlayer: "igraph.Graph", layer_vec: LayerVector,
390
+ partitions: Partitions
391
+ ) -> PartitionCoefficients3D:
353
392
  """Computes partitions coefficients in parallel by calling partition_coefficients_3D_serial"""
354
393
  partitions = list(partitions)
355
- partition_chunks = [
394
+ partition_chunks: list[list[Membership]] = [
356
395
  partitions[floor(i * len(partitions) / cpu_count()):floor((i + 1) * len(partitions) / cpu_count())]
357
396
  for i in range(cpu_count())
358
397
  ]
359
398
 
360
- pool = Pool(processes=cpu_count())
361
- results = pool.starmap(partition_coefficients_3D_serial,
362
- [(G_intralayer, G_interlayer, layer_vec, partition_chunk)
363
- for partition_chunk in partition_chunks])
399
+ pool: "multiprocessing.pool.Pool" = Pool(processes=cpu_count())
400
+ results: list[PartitionCoefficients3D] = pool.starmap(
401
+ partition_coefficients_3D_serial, [(G_intralayer, G_interlayer, layer_vec, partition_chunk)
402
+ for partition_chunk in partition_chunks])
364
403
  pool.close()
365
404
 
366
- A_hats = np.array([v for A_hats, P_hats, C_hats in results for v in A_hats])
367
- P_hats = np.array([v for A_hats, P_hats, C_hats in results for v in P_hats])
368
- C_hats = np.array([v for A_hats, P_hats, C_hats in results for v in C_hats])
405
+ A_hats: FloatArray = np.array([v for A_hats, P_hats, C_hats in results for v in A_hats])
406
+ P_hats: FloatArray = np.array([v for A_hats, P_hats, C_hats in results for v in P_hats])
407
+ C_hats: FloatArray = np.array([v for A_hats, P_hats, C_hats in results for v in C_hats])
369
408
 
370
409
  assert len(A_hats) == len(P_hats) == len(C_hats) == len(partitions)
371
410