def test_distance_matrix_as_undirected(self): if self.class_type == "PyGraph": with self.assertRaises(TypeError): retworkx.distance_matrix(self.graph, as_undirected=True) else: res = retworkx.distance_matrix(self.graph, as_undirected=True) self.assertIsInstance(res, numpy.ndarray)
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 test_digraph_distance_matrix_non_zero_null(self): graph = retworkx.PyDiGraph() graph.add_nodes_from(list(range(7))) graph.add_edges_from_no_data([(0, 1), (0, 6), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]) graph.add_node(7) dist = retworkx.distance_matrix(graph, as_undirected=True, null_value=np.nan) expected = np.array([ [0.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, np.nan], [1.0, 0.0, 1.0, 2.0, 3.0, 3.0, 2.0, np.nan], [2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 3.0, np.nan], [3.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, np.nan], [3.0, 3.0, 2.0, 1.0, 0.0, 1.0, 2.0, np.nan], [2.0, 3.0, 3.0, 2.0, 1.0, 0.0, 1.0, np.nan], [1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 0.0], ]) self.assertTrue(np.array_equal(dist, expected, equal_nan=True))
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
def test_distance_matrix(self): res = retworkx.distance_matrix(self.graph) self.assertIsInstance(res, numpy.ndarray)
def get_distance_matrix(net: Network) -> np.ndarray: dm: np.ndarray = rx.distance_matrix(net.R).copy() # type: ignore dm[dm == 0] = np.inf for u in range(len(dm)): dm[u, u] = 0 return dm