-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
115 lines (90 loc) · 4.22 KB
/
visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (c) 2024 Jordan Barrett & Aleksander Wojnarowicz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Optional
from abcd_graph.api.abcd_models import Model
from abcd_graph.callbacks.abstract import (
ABCDCallback,
BuildContext,
)
from abcd_graph.core.abcd_objects.abcd_graph import ABCDGraph
from abcd_graph.core.abcd_objects.community import Community
from abcd_graph.core.exporter import GraphExporter
from abcd_graph.core.utils import get_community_color_map
from abcd_graph.utils import require
class Visualizer(ABCDCallback):
def __init__(self) -> None:
self._communities: list[Community] = []
self._model_used: Optional[Model] = None
self._exporter: Optional[GraphExporter] = None
self._graph: Optional[ABCDGraph] = None
def after_build(self, graph: ABCDGraph, context: BuildContext, exporter: GraphExporter) -> None:
self._communities = graph.communities
self._model_used = context.model_used
self._exporter = exporter
self._graph = graph
@require("matplotlib")
def draw_community_cdf(self) -> None:
import matplotlib.pyplot as plt # type: ignore[import]
assert self._graph is not None
actual_cdf = self._graph.actual_community_cdf
expected_cdf = self._graph.expected_community_cdf
x_actual = list(actual_cdf.keys())
x_expected = list(expected_cdf.keys())
y_actual = list(actual_cdf.values())
y_expected = list(expected_cdf.values())
plt.plot(x_actual, y_actual, label="Actual")
plt.plot(x_expected, y_expected, label="Expected")
plt.xlabel("Community Size")
plt.ylabel("CDF")
plt.legend()
plt.title("Community size CDF")
plt.show()
@require("matplotlib")
def draw_degree_cdf(self) -> None:
import matplotlib.pyplot as plt
assert self._graph is not None
actual_cdf = self._graph.actual_degree_cdf
expected_cdf = self._graph.expected_degree_cdf
x_actual = list(actual_cdf.keys())
x_expected = list(expected_cdf.keys())
y_actual = list(actual_cdf.values())
y_expected = list(expected_cdf.values())
plt.plot(x_actual, y_actual, label="Actual")
plt.plot(x_expected, y_expected, label="Expected")
plt.xlabel("Degree")
plt.ylabel("CDF")
plt.legend()
plt.title("Degree CDF")
plt.show()
@require("networkx")
@require("matplotlib")
def draw_communities(self) -> None:
assert self._graph is not None
if len(self._graph.deg_b) > 100:
raise ValueError("Drawing communities is only supported for graphs with at most 100 vertices")
if self._model_used is not None and self._model_used.__name__ != "configuration_model":
raise NotImplementedError("Drawing communities is only supported for the configuration model")
import networkx as nx # type: ignore[import]
from matplotlib import pyplot as plt
assert self._exporter is not None
nx_g = self._exporter.to_networkx()
color_map = get_community_color_map(communities=self._communities)
nx.draw(nx_g, node_color=color_map, with_labels=True, font_weight="bold")
plt.show()