Example #1
0
 def test_empty_nx(self):
     expected = 0, []
     results = _nx_to_edge_list(
         graph=nx.Graph(),
         identifier=_IdentityMapper(),
         is_weighted=None,
         weight_attribute="weight",
         weight_default=1.0,
     )
     self.assertEqual(expected, results)
     with self.assertRaises(ValueError):
         _nx_to_edge_list(
             graph=nx.DiGraph(),
             identifier=_IdentityMapper(),
             is_weighted=None,
             weight_attribute="weight",
             weight_default=1.0,
         )
     with self.assertRaises(ValueError):
         _nx_to_edge_list(
             graph=nx.MultiGraph(),
             identifier=_IdentityMapper(),
             is_weighted=None,
             weight_attribute="weight",
             weight_default=1.0,
         )
     with self.assertRaises(ValueError):
         _nx_to_edge_list(
             graph=nx.MultiDiGraph(),
             identifier=_IdentityMapper(),
             is_weighted=None,
             weight_attribute="weight",
             weight_default=1.0,
         )
Example #2
0
    def test_assert_wrong_types_in_tuples(self):
        edges = [(True, 4, "sandwich")]
        with self.assertRaises(BeartypeCallHintParamViolation):
            _edge_list_to_edge_list(
                edges=edges,
                identifier=_IdentityMapper(),
            )

        edges = [(True, False, 3.2)]
        _nodes, results = _edge_list_to_edge_list(
            edges=edges,
            identifier=_IdentityMapper(),
        )
        self.assertEqual([("True", "False", 3.2)], results)
Example #3
0
 def test_assert_list_contains_misshapen_tuple(self):
     edges = [(1, 2, 1.0, 1.0)]
     with self.assertRaises(BeartypeCallHintParamViolation):
         _edge_list_to_edge_list(
             edges=edges,
             identifier=_IdentityMapper(),
         )
Example #4
0
 def test_assert_list_does_not_contain_tuples(self):
     edges = ["invalid"]
     with self.assertRaises(BeartypeCallHintParamViolation):
         _edge_list_to_edge_list(
             edges=edges,
             identifier=_IdentityMapper(),
         )
Example #5
0
 def test_empty_edge_list(self):
     edges = []
     results = _edge_list_to_edge_list(
         edges=edges,
         identifier=_IdentityMapper(),
     )
     self.assertEqual([], results[1])
Example #6
0
    def test_matrices(self):
        graph = add_edges_to_graph(nx.Graph())
        di_graph = add_edges_to_graph(nx.DiGraph())

        dense_undirected = nx.to_numpy_array(graph)
        dense_directed = nx.to_numpy_array(di_graph)

        sparse_undirected = nx.to_scipy_sparse_matrix(graph)
        sparse_directed = nx.to_scipy_sparse_matrix(di_graph)

        expected = [("0", "1", 2.2), ("1", "2", 0.001)]
        _, edges = _adjacency_matrix_to_edge_list(
            matrix=dense_undirected,
            identifier=_IdentityMapper(),
            check_directed=True,
            is_weighted=True,
            weight_default=1.0,
        )
        self.assertEqual(expected, edges)
        _, edges = _adjacency_matrix_to_edge_list(
            matrix=sparse_undirected,
            identifier=_IdentityMapper(),
            check_directed=True,
            is_weighted=True,
            weight_default=1.0,
        )
        self.assertEqual(expected, edges)

        with self.assertRaises(ValueError):
            _adjacency_matrix_to_edge_list(
                matrix=dense_directed,
                identifier=_IdentityMapper(),
                check_directed=True,
                is_weighted=True,
                weight_default=1.0,
            )

        with self.assertRaises(ValueError):
            _adjacency_matrix_to_edge_list(
                matrix=sparse_directed,
                identifier=_IdentityMapper(),
                check_directed=True,
                is_weighted=True,
                weight_default=1.0,
            )
Example #7
0
 def test_misshapen_matrices(self):
     data = [[3, 2, 0], [2, 0, 1]]  # this is utter gibberish
     with self.assertRaises(ValueError):
         _adjacency_matrix_to_edge_list(
             matrix=np.array(data),
             identifier=_IdentityMapper(),
             check_directed=True,
             is_weighted=True,
             weight_default=1.0,
         )
     with self.assertRaises(ValueError):
         _adjacency_matrix_to_edge_list(
             matrix=scipy.sparse.csr_matrix(data),
             identifier=_IdentityMapper(),
             check_directed=True,
             is_weighted=True,
             weight_default=1.0,
         )
Example #8
0
    def test_unweighted_nx(self):
        graph = nx.Graph()
        graph.add_edge("dwayne", "nick")
        graph.add_edge("nick", "ben")

        with self.assertRaises(TypeError):
            _, edges = _nx_to_edge_list(
                graph=graph,
                identifier=_IdentityMapper(),
                is_weighted=True,
                weight_attribute="weight",
                weight_default=1.0,
            )

        _, edges = _nx_to_edge_list(
            graph=graph,
            identifier=_IdentityMapper(),
            is_weighted=False,
            weight_attribute="weight",
            weight_default=3.33333,
        )
        self.assertEqual(
            [("dwayne", "nick", 3.33333), ("nick", "ben", 3.33333)],
            edges,
        )

        graph.add_edge("salad", "sandwich", weight=100)
        _, edges = _nx_to_edge_list(
            graph=graph,
            identifier=_IdentityMapper(),
            is_weighted=False,
            weight_attribute="weight",
            weight_default=3.33333,
        )
        self.assertEqual(
            [
                ("dwayne", "nick", 3.33333),
                ("nick", "ben", 3.33333),
                ("salad", "sandwich", 100),
            ],
            edges,
        )
Example #9
0
 def test_nx_graph_node_str_collision(self):
     graph = nx.Graph()
     graph.add_edge("1", 1, weight=1.0)
     with self.assertRaisesRegex(ValueError, "collision"):
         _nx_to_edge_list(
             graph=graph,
             identifier=_IdentityMapper(),
             is_weighted=True,
             weight_attribute="weight",
             weight_default=1.0,
         )
Example #10
0
    def test_empty_adj_matrices(self):
        dense = np.array([])
        with self.assertRaises(ValueError):
            _adjacency_matrix_to_edge_list(
                matrix=dense,
                identifier=_IdentityMapper(),
                check_directed=True,
                is_weighted=True,
                weight_default=1.0,
            )

        sparse = scipy.sparse.csr_matrix([])
        with self.assertRaises(ValueError):
            _adjacency_matrix_to_edge_list(
                matrix=sparse,
                identifier=_IdentityMapper(),
                check_directed=True,
                is_weighted=True,
                weight_default=1.0,
            )
Example #11
0
 def test_valid_nx(self):
     graph = add_edges_to_graph(nx.Graph())
     expected = [("nick", "dwayne", 2.2), ("dwayne", "ben", 0.001)]
     _, edges = _nx_to_edge_list(
         graph=graph,
         identifier=_IdentityMapper(),
         is_weighted=None,
         weight_attribute="weight",
         weight_default=1.0,
     )
     self.assertEqual(expected, edges)
Example #12
0
 def test_edgelist_node_str_collision(self):
     with self.assertRaisesRegex(ValueError, "collision"):
         _edge_list_to_edge_list(edges=[("1", 1, 1.0)],
                                 identifier=_IdentityMapper())