def test_from_adjacency_matrix(self): input_array = np.array( [[0.0, 4.0, 0.0], [4.0, 0.0, 4.0], [0.0, 4.0, 0.0]], dtype=np.float64) graph = retworkx.PyDiGraph.from_adjacency_matrix(input_array) out_array = retworkx.digraph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(input_array, out_array))
def test_float_cast_weight_func(self): dag = retworkx.PyDAG() node_a = dag.add_node("a") dag.add_child(node_a, "b", 7.0) res = retworkx.digraph_adjacency_matrix(dag, lambda x: float(x)) self.assertIsInstance(res, np.ndarray) self.assertTrue(np.array_equal(np.array([[0.0, 7.0], [0.0, 0.0]]), res))
def test_random_graph_different_dtype_astype_no_copy(self): input_matrix = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=np.int64) graph = retworkx.PyDiGraph.from_adjacency_matrix( input_matrix.astype(np.float64, copy=False)) adj_matrix = retworkx.digraph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix))
def test_digraph_with_index_holes(self): dag = retworkx.PyDAG() node_a = dag.add_node('a') node_b = dag.add_child(node_a, 'b', 1) dag.add_child(node_a, 'c', 1) dag.remove_node(node_b) res = retworkx.digraph_adjacency_matrix(dag, lambda x: 1) self.assertIsInstance(res, np.ndarray) self.assertTrue(np.array_equal(np.array([[0, 1], [0, 0]]), res))
def test_multigraph_sum_cast_weight_func(self): dag = retworkx.PyDAG() node_a = dag.add_node('a') node_b = dag.add_child(node_a, 'b', 7.0) dag.add_edge(node_a, node_b, 0.5) res = retworkx.digraph_adjacency_matrix(dag, lambda x: float(x)) self.assertIsInstance(res, np.ndarray) self.assertTrue(np.array_equal(np.array([[0.0, 7.5], [0.0, 0.0]]), res))
def test_default_weight(self): dag = retworkx.PyDAG() node_a = dag.add_node('a') dag.add_child(node_a, 'b', {'a': 1}) dag.add_child(node_a, 'c', {'a': 2}) res = retworkx.digraph_adjacency_matrix(dag, default_weight=4) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array([[0.0, 4.0, 4.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float64), res))
def test_single_neighbor(self): dag = retworkx.PyDAG() node_a = dag.add_node('a') dag.add_child(node_a, 'b', {'a': 1}) dag.add_child(node_a, 'c', {'a': 2}) res = retworkx.digraph_adjacency_matrix(dag, lambda x: 1) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array([[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float64), res))
def test_negative_weight(self): input_matrix = np.array( [[0, 1, 0], [-1, 0, -1], [0, 1, 0]], dtype=float ) graph = retworkx.PyDiGraph.from_adjacency_matrix(input_matrix) adj_matrix = retworkx.digraph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix)) self.assertEqual( [(0, 1, 1), (1, 0, -1), (1, 2, -1), (2, 1, 1)], graph.weighted_edge_list(), )
def test_no_weight_fn(self): dag = retworkx.PyDAG() node_a = dag.add_node("a") dag.add_child(node_a, "b", {"a": 1}) dag.add_child(node_a, "c", {"a": 2}) res = retworkx.digraph_adjacency_matrix(dag) self.assertIsInstance(res, np.ndarray) self.assertTrue( np.array_equal( np.array( [[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float64, ), res, ) )
def test_no_edge_digraph_adjacency_matrix(self): dag = retworkx.PyDAG() for i in range(50): dag.add_node(i) res = retworkx.digraph_adjacency_matrix(dag, lambda x: 1) self.assertTrue(np.array_equal(np.zeros([50, 50]), res))
import tempfile from typing import Sized from PIL import Image import pydot import retworkx as rx import igraph as ig import numpy as np G: rx.PyDiGraph = rx.directed_gnm_random_graph(1000, 2000, None) components: list = rx.strongly_connected_components(G) largest_component: Sized = max(components, key=len) H: rx.PyGraph = G.subgraph(largest_component) # have to fix pydot error """ dot = pydot.graph_from_dot_data(G.to_dot())[0] with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, 'dag.png') dot.write_png(tmp_path) image = Image.open(tmp_path) os.remove(tmp_path) image """ n1: np.ndarray = rx.digraph_adjacency_matrix(G, None) g: ig.Graph = ig.Graph.Weighted_Adjacency(n1.tolist()) ig.plot(g)
def test_random_graph_float_dtype(self): input_matrix = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=float) graph = retworkx.PyDiGraph.from_adjacency_matrix(input_matrix) adj_matrix = retworkx.digraph_adjacency_matrix(graph, lambda x: x) self.assertTrue(np.array_equal(adj_matrix, input_matrix))
def test_random_graph_full_path(self): graph = retworkx.directed_gnp_random_graph(100, 0.95, seed=42) adjacency_matrix = retworkx.digraph_adjacency_matrix(graph) new_graph = retworkx.PyDiGraph.from_adjacency_matrix(adjacency_matrix) new_adjacency_matrix = retworkx.digraph_adjacency_matrix(new_graph) self.assertTrue(np.array_equal(adjacency_matrix, new_adjacency_matrix))