Coverage for src/abcd_graph/exporter.py: 100%

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.

20

21 from typing import TYPE_CHECKING

22

23 import numpy as np

24 from numpy.typing import NDArray

25

26 from abcd_graph.graph.core.abcd_objects import GraphImpl

27 from abcd_graph.graph.core.exceptions import MalformedGraphException

28 from abcd_graph.utils import require

29

30 if 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]

34

35

36 class GraphExporter:

37 def __init__(self, graph: GraphImpl) -> None:

38 self._graph: GraphImpl = graph

39

40 @property

41 def is_proper_abcd(self) -> bool:

42 return self._graph.is_proper_abcd

43

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")

47

48 assert self._graph is not None

49 return self._graph.to_adj_matrix()

50

51 @require("scipy")

52 def to_sparse_adjacency_matrix(self) -> "csr_matrix": # type: ignore[no-any-unimported]

53 from scipy.sparse import csr_matrix

54

55 if not self.is_proper_abcd:

56 raise MalformedGraphException("Graph is not proper ABCD so the adjacency matrix cannot be built")

57

58 assert self._graph is not None

59 return csr_matrix(self.to_adjacency_matrix())

60

61 @require("igraph")

62 def to_igraph(self) -> "IGraph": # type: ignore[no-any-unimported]

63 import igraph

64

65 graph = igraph.Graph(self._graph.edges)

66

67 graph.vs["ground_truth_community"] = self._graph.membership_list

68

69 return graph

70

71 @require("networkx")

72 def to_networkx(self) -> "NetworkXGraph": # type: ignore[no-any-unimported]

73 import networkx as nx

74

75 graph = nx.Graph()

76

77 graph.add_nodes_from(range(self._graph._params.vcount))

78 graph.add_edges_from(self._graph.edges)

79

80 m_list = self._graph.membership_list

81

82 for node in graph.nodes:

83 graph.nodes[node]["ground_truth_community"] = m_list[node]

84

85 return graph