Coverage for src/abcd_graph/exporter.py: 100%
40 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-12-04 21:31 +0100
« prev ^ index » next coverage.py v7.5.3, created at 2024-12-04 21:31 +0100
1# Copyright (c) 2024 Jordan Barrett & Aleksander Wojnarowicz
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in all
11# copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
21from typing import TYPE_CHECKING
23import numpy as np
24from numpy.typing import NDArray
26from abcd_graph.graph.core.abcd_objects import GraphImpl
27from abcd_graph.graph.core.exceptions import MalformedGraphException
28from abcd_graph.utils import require
30if TYPE_CHECKING: # pragma: no cover
31 from igraph import Graph as IGraph # type: ignore[import]
32 from networkx import Graph as NetworkXGraph # type: ignore[import]
33 from scipy.sparse import csr_matrix # type: ignore[import]
36class GraphExporter:
37 def __init__(self, graph: GraphImpl) -> None:
38 self._graph: GraphImpl = graph
40 @property
41 def is_proper_abcd(self) -> bool:
42 return self._graph.is_proper_abcd
44 def to_adjacency_matrix(self) -> NDArray[np.bool_]:
45 if not self.is_proper_abcd:
46 raise MalformedGraphException("Graph is not proper ABCD so the adjacency matrix cannot be built")
48 assert self._graph is not None
49 return self._graph.to_adj_matrix()
51 @require("scipy")
52 def to_sparse_adjacency_matrix(self) -> "csr_matrix": # type: ignore[no-any-unimported]
53 from scipy.sparse import csr_matrix
55 if not self.is_proper_abcd:
56 raise MalformedGraphException("Graph is not proper ABCD so the adjacency matrix cannot be built")
58 assert self._graph is not None
59 return csr_matrix(self.to_adjacency_matrix())
61 @require("igraph")
62 def to_igraph(self) -> "IGraph": # type: ignore[no-any-unimported]
63 import igraph
65 graph = igraph.Graph(self._graph.edges)
67 graph.vs["ground_truth_community"] = self._graph.membership_list
69 return graph
71 @require("networkx")
72 def to_networkx(self) -> "NetworkXGraph": # type: ignore[no-any-unimported]
73 import networkx as nx
75 graph = nx.Graph()
77 graph.add_nodes_from(range(self._graph._params.vcount))
78 graph.add_edges_from(self._graph.edges)
80 m_list = self._graph.membership_list
82 for node in graph.nodes:
83 graph.nodes[node]["ground_truth_community"] = m_list[node]
85 return graph