24 """Classes for visualising classifier knowledge representations."""
26 from __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.
gvizgviz = graphviz.Graph(
"G", filename=filename +
".gv")
62 self.
gvizgviz.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 "%.5f" % symbol
76 """Parses functions."""
78 symbol: str = self.tree[self.pos]
79 if symbol
in (
"+",
"-",
"*",
"/"):
83 self.
gvizgviz.edge(str(self.cnt), expr1)
84 self.
gvizgviz.edge(str(self.cnt), expr2)
85 self.
gvizgviz.node(str(self.cnt), label=self.
labellabel(symbol))
90 """Parses sub-expressions."""
91 symbol: str = self.tree[self.pos]
98 self.
gvizgviz.node(str(self.cnt), label=self.
labellabel(symbol))
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.
126 self.feature_names: list[str] |
None = 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)
132 self.
gvizgviz = graphviz.Digraph(
"G", filename=filename +
".gv")
134 label: str =
"" if note
is None else note
135 label +=
"\nN = %d\n" % graph[
"n"]
136 label +=
"T = %d\n" % graph[
"t"]
137 label +=
"match node shaded\n"
138 self.
gvizgviz.attr(label=label)
141 def label(self, symbol: str) -> str:
142 """Returns the node label for a symbol."""
143 if self.feature_names
is not None and isinstance(symbol, str):
144 start, end = symbol.split(
"_")
if "_" in symbol
else (symbol,
"")
145 if start ==
"feature" and int(end) < len(self.feature_names):
146 return self.feature_names[int(end)]
147 elif isinstance(symbol, float):
148 return "%.5f" % symbol
152 """Plots the nodes and edges in the graph."""
153 for i
in range(self.n):
154 style: str =
"filled" if i == 0
else ""
155 self.
gvizgviz.node(str(i), label=self.functions[i], style=style)
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]
159 if src < self.n_inputs:
160 feature =
"feature_%d" % src
161 self.
gvizgviz.node(feature, label=self.
labellabel(feature), shape=
"square")
162 self.
gvizgviz.edge(feature, str(i))
164 self.
gvizgviz.edge(str(src - self.n_inputs), str(i))
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)