24"""Classes for visualising classifier knowledge representations."""
26from __future__
import annotations
32 """! Visualises a GP tree with graphviz."""
38 note: str |
None =
None,
39 feature_names: list[str] |
None =
None,
42 Plots a tree with graphviz, saving to a file.
47 List of strings representing a GP tree.
49 Name of the output file to save the drawn tree.
51 Optional string to be added as a note/caption.
52 feature_names : list[str], optional
53 Optional list of feature names.
55 self.feature_names: list[str] |
None = feature_names
56 self.tree: list[str] = tree
59 self.
gviz = graphviz.Graph(
"G", filename=filename +
".gv")
62 self.
gviz.attr(label=note)
65 def label(self, symbol: str) -> str:
66 """Returns the node label for a symbol."""
67 if self.feature_names
is not None and isinstance(symbol, str):
68 start, end = symbol.split(
"_")
if "_" in symbol
else (symbol,
"")
69 if start ==
"feature" and int(end) < len(self.feature_names):
70 return self.feature_names[int(end)]
71 elif isinstance(symbol, float):
72 return f
"{symbol:.5f}"
76 """Parses functions."""
78 symbol: str = self.tree[self.pos]
79 if symbol
in (
"+",
"-",
"*",
"/"):
84 self.
gviz.edge(str(self.
cnt), expr2)
90 """Parses sub-expressions."""
91 symbol: str = self.tree[self.pos]
103 """! Visualises a DGP graph with graphviz."""
109 note: str |
None =
None,
110 feature_names: list[str] |
None =
None,
113 Plots a DGP graph with graphviz, saving to a file.
118 Dictionary representing a DGP graph.
120 Name of the output file to save the drawn graph.
122 Optional string to be added as a note/caption.
123 feature_names : list[str], optional
124 Optional list of feature names.
127 self.
n: int = graph[
"n"]
128 self.
n_inputs: int = graph[
"n_inputs"]
129 self.
functions: list[str] = graph[
"functions"]
130 self.connectivity: list[int] = graph[
"connectivity"]
131 self.
k: int =
int(len(self.connectivity) / self.
n)
134 label: str =
"" if note
is None else note
135 label +=
"\nN = {graph['n']}\n"
136 label += f
"T = {graph['t']}\n"
137 label +=
"match node shaded\n"
141 def label(self, symbol: str) -> str:
142 """Returns the node label for a symbol."""
144 start, end =
symbol.split(
"_")
if "_" in symbol
else (symbol,
"")
148 return f
"{symbol:.5f}"
152 """Plots the nodes and edges in the graph."""
154 style: str =
"filled" if i == 0
else ""
156 n_inputs: int = 1
if self.
functions[i] ==
"Fuzzy NOT" else self.
k
157 for j
in range(n_inputs):
158 src = self.connectivity[(i * self.
k) + j]
160 feature = f
"feature_{src}"
161 self.
gviz.
node(feature, label=self.
label(feature), shape=
"square")
Visualises a DGP graph with graphviz.
None __init__(self, dict graph, str filename, str|None note=None, list[str]|None feature_names=None)
str label(self, str symbol)
Visualises a GP tree with graphviz.
str label(self, str symbol)
None __init__(self, list[str] tree, str filename, str|None note=None, list[str]|None feature_names=None)