def __init__(self, graph: Union[PyGraph, "nx.Graph"]) -> None: """ Args: graph: Input graph for Lattice. Can be provided as ``retworkx.PyGraph``, which is used internally, or, for convenience, as ``networkx.Graph``. The graph cannot be a multigraph. Raises: ValueError: If the input graph is a multigraph. ValueError: If the graph edges are non-numeric. """ if not isinstance(graph, PyGraph): _optionals.HAS_NETWORKX.require_now( "Lattice construction from networkx.Graph") graph = networkx_converter(graph) if graph.multigraph: raise ValueError( f"Invalid `graph.multigraph` {graph.multigraph} is given. " "`graph.multigraph` must be `False`.") # validate the edge weights for edge_index, edge in graph.edge_index_map().items(): weight = edge[2] if weight is None or weight == {}: # None or {} is updated to be 1 graph.update_edge_by_index(edge_index, 1) elif not isinstance(weight, numbers.Number): raise ValueError( f"Unsupported weight {weight} on edge with index {edge_index}." ) self._graph = graph self.pos: Optional[dict] = None
def test_random_k_out_graph(self): g = networkx.random_k_out_graph(100, 50, 3.14159, True, 42) out_graph = retworkx.networkx_converter(g) self.assertIsInstance(out_graph, retworkx.PyDiGraph) self.assertEqual(out_graph.nodes(), list(g.nodes)) self.assertEqual(out_graph.weighted_edge_list(), list(g.edges(data=True))) self.assertEqual(out_graph.multigraph, g.is_multigraph())
def test_cubical_multigraph(self): g = networkx.cubical_graph(networkx.MultiGraph) out_graph = retworkx.networkx_converter(g) self.assertIsInstance(out_graph, retworkx.PyGraph) self.assertEqual(out_graph.nodes(), list(g.nodes)) self.assertEqual(out_graph.weighted_edge_list(), list(g.edges(data=True))) self.assertEqual(out_graph.multigraph, g.is_multigraph())
def test_empty_directed_graph(self): g = networkx.DiGraph() out_graph = retworkx.networkx_converter(g) self.assertIsInstance(out_graph, retworkx.PyDiGraph) self.assertEqual(out_graph.nodes(), list(g.nodes)) self.assertEqual(out_graph.weighted_edge_list(), list(g.edges(data=True))) self.assertEqual(out_graph.multigraph, g.is_multigraph())
def test_undirected_gnm_graph(self): g = networkx.gnm_random_graph(10, 10, seed=42) out_graph = retworkx.networkx_converter(g) self.assertIsInstance(out_graph, retworkx.PyGraph) self.assertEqual(out_graph.nodes(), list(g.nodes)) self.assertEqual(out_graph.weighted_edge_list(), list(g.edges(data=True))) self.assertEqual(out_graph.multigraph, g.is_multigraph())
def __call__(self, D: np.ndarray, M: np.ndarray, time_step: int, sir: np.ndarray) -> np.ndarray: pressured_nodes = self._pressure_handler(sir) D = self._call(D, M, time_step, pressured_nodes) R = M - D # Collect Data self._last_pressured_nodes = pressured_nodes self._last_removed_edges = R G = nx.from_numpy_array(D) self._last_comps = tuple(nx.connected_components(G)) self._last_diameter = np.max( rx.distance_matrix(rx.networkx_converter(G))) self._last_perc_edges_removed = np.sum(R, axis=0) / np.sum(M, axis=0) return D
def __call__(self, D: np.ndarray, M: np.ndarray, time_step: int, sir: np.ndarray) -> np.ndarray: final_ND = np.zeros(D.shape, dtype=bool) final_pressured_nodes = np.zeros(D.shape[0], dtype=bool) for behavior in self._behaviors: pressured_nodes = behavior._pressure_handler(sir) ND = behavior._call(D, M, time_step, pressured_nodes) final_ND = final_ND | (ND > 0) final_pressured_nodes = final_pressured_nodes | pressured_nodes # Collect Data R = M - final_ND self._last_pressured_nodes = final_pressured_nodes self._last_removed_edges = R G = nx.from_numpy_array(final_ND) self._last_comps = tuple(nx.connected_components(G)) self._last_diameter = np.max( rx.distance_matrix(rx.networkx_converter(G))) self._last_perc_edges_removed = np.sum(R, axis=0) / np.sum(M, axis=0) return ND