-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathattracting.py
More file actions
35 lines (27 loc) · 1.34 KB
/
attracting.py
File metadata and controls
35 lines (27 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from networkx import attracting_components
from joblib import Parallel, delayed
import nx_parallel as nxp
__all__ = ["number_attracting_components"]
@nxp._configure_if_nx_active(should_run=nxp.should_skip_parallel)
def number_attracting_components(G, get_chunks="chunks"):
"""The parallel computation is implemented by dividing the list
of attracting components into chunks and then finding the length
of each chunk in parallel and then adding all the lengths at the end.
networkx.number_attracting_components : https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.number_attracting_components.html
Parameters
----------
get_chunks : str, function (default = "chunks")
A function that takes in a list of attracting components as input and returns
an iterable `component_chunks`. The default chunking is done by slicing the
list of attracting components into `n_jobs` number of chunks.
"""
if hasattr(G, "graph_object"):
G = G.graph_object
n_jobs = nxp.get_n_jobs()
ac = list(attracting_components(G))
if get_chunks == "chunks":
component_chunks = nxp.chunks(ac, n_jobs)
else:
component_chunks = get_chunks(ac)
results = Parallel()(delayed(len)(chunk) for chunk in component_chunks)
return sum(results)