Source code for nx_parallel.interface

import networkx as nx
from nx_parallel import algorithms

__all__ = ["BackendInterface", "ParallelGraph"]


ALGORITHMS = [
    # Bipartite
    "node_redundancy",
    # Isolates
    "number_of_isolates",
    # Vitality
    "closeness_vitality",
    # Tournament
    "is_reachable",
    "tournament_is_strongly_connected",
    # Link Prediction
    "resource_allocation_index",
    "jaccard_coefficient",
    "adamic_adar_index",
    "preferential_attachment",
    "common_neighbor_centrality",
    "cn_soundarajan_hopcroft",
    "ra_index_soundarajan_hopcroft",
    "within_inter_cluster",
    # Centrality
    "betweenness_centrality",
    "edge_betweenness_centrality",
    "harmonic_centrality",
    # Components : attracting
    "number_attracting_components",
    # Components : connected
    "number_connected_components",
    # Components : strongly connected
    "number_strongly_connected_components",
    # Components : weakly connected
    "number_weakly_connected_components",
    # Dag
    "colliders",
    "v_structures",
    # Efficiency
    "local_efficiency",
    # Shortest Paths : generic
    "all_pairs_all_shortest_paths",
    # Shortest Paths : weighted graphs
    "all_pairs_dijkstra",
    "all_pairs_dijkstra_path_length",
    "all_pairs_dijkstra_path",
    "all_pairs_bellman_ford_path_length",
    "all_pairs_bellman_ford_path",
    "johnson",
    # Clustering
    "square_clustering",
    "triangles",
    "clustering",
    "average_clustering",
    # Shortest Paths : unweighted graphs
    "all_pairs_shortest_path",
    "all_pairs_shortest_path_length",
    # Approximation
    "approximate_all_pairs_node_connectivity",
    # Assortativity
    "average_neighbor_degree",
    # Connectivity
    "all_pairs_node_connectivity",
    # Maximal Independent Set
    "maximal_independent_set",
]


[docs] class ParallelGraph: """A wrapper class for networkx.Graph, networkx.DiGraph, networkx.MultiGraph, and networkx.MultiDiGraph. """ __networkx_backend__ = "parallel" def __init__(self, graph_object=None): if graph_object is None: self.graph_object = nx.Graph() elif isinstance(graph_object, nx.Graph): self.graph_object = graph_object else: self.graph_object = nx.Graph(graph_object)
[docs] def is_multigraph(self): return self.graph_object.is_multigraph()
[docs] def is_directed(self): return self.graph_object.is_directed()
def __str__(self): return f"Parallel{self.graph_object}"
[docs] class BackendInterface: """BackendInterface class for parallel algorithms."""
[docs] @staticmethod def convert_from_nx(graph, *args, **kwargs): """Convert a networkx.Graph, networkx.DiGraph, networkx.MultiGraph, or networkx.MultiDiGraph to a ParallelGraph. """ if isinstance(graph, ParallelGraph): return graph return ParallelGraph(graph)
[docs] @staticmethod def convert_to_nx(result, *, name=None): """Convert a ParallelGraph to a networkx.Graph, networkx.DiGraph, networkx.MultiGraph, or networkx.MultiDiGraph. """ if isinstance(result, ParallelGraph): return result.graph_object return result
[docs] @classmethod def should_run(cls, name, args, kwargs): """Determine whether this backend should run the specified algorithm with the given arguments. Parameters ---------- cls : type `BackendInterface` class name : str Name of the target algorithm args : tuple Positional arguments passed to the algorithm's `should_run`. kwargs : dict Keyword arguments passed to the algorithm's `should_run`. Returns ------- bool or str If the algorithm should run, returns True. Otherwise, returns a string explaining why parallel execution is skipped. """ return getattr(cls, name).should_run(*args, **kwargs)
[docs] @staticmethod def on_start_tests(items): """Modify pytest items after tests have been collected. This is called during pytest_collection_modifyitems phase. Mark tests that have different valid behavior in parallel backend. """ try: import pytest except ModuleNotFoundError: return xfail_tests = { "test_random_seed": ( "test_mis.py", "Parallel MIS produces different valid ordering than sequential", ), "test_K5[graph0]": ( "test_mis.py", "Return type changed from list to set in NetworkX main", ), "test_K5[graph1]": ( "test_mis.py", "Return type changed from list to set in NetworkX main", ), } for item in items: for test_name, (filename, reason) in xfail_tests.items(): if item.name == test_name and filename in str(item.fspath): item.add_marker(pytest.mark.xfail(reason=reason))
for attr in ALGORITHMS: setattr(BackendInterface, attr, getattr(algorithms, attr))