Repository navigation
Expand file tree
/
Copy pathplot_utils.py
More file actions
70 lines (62 loc) · 2.14 KB
/
Copy pathplot_utils.py
File metadata and controls
70 lines (62 loc) · 2.14 KB
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
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import importlib
import imp
import os
def plotTrends(logbook,name,folder=None):
gen = logbook.select("gen")
fit_min = logbook.select("min")
fit_max = logbook.select("max")
fit_avg = logbook.select("avg")
fit_std = logbook.select("std")
fig = plt.figure("Genetic Programming (fitness trend)")
ax1 = fig.add_subplot(111)
line1 = ax1.plot(gen, fit_min, label="Min")
line2 = ax1.plot(gen, fit_max, label="Max")
line3 = ax1.errorbar(gen, fit_avg, yerr=fit_std, label="Avg")
ax1.set_xlabel("Generation")
ax1.set_ylabel("Fitness")
ax1.set_xlim(0,len(gen)-1)
ax1.legend()
plt.savefig(folder+'/'+'trends_'+name+'.png')
plt.show()
def plotTree(nodes,edges,labels,name,folder=None):
if folder is not None and not os.path.exists(folder):
os.makedirs(folder)
print("ciao")
if importModule('pygraphviz'):
import pygraphviz as pgv
g = pgv.AGraph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
g.layout(prog='dot')
for i in nodes:
n = g.get_node(i)
print(i)
print(labels)
print(labels[i].name)
n.attr['label'] = labels[i].name
g.draw(folder+'/'+'tree_'+name+'.pdf')
if importModule('networkx'):
import networkx as nx
plt.figure("GP (best tree)")
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
pos = nx.nx_agraph.graphviz_layout(g, prog='dot')
"""nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos, labels)"""
node_labels = {i: labels[i].name for i in nodes} # Crea un dizionario con il nome come etichetta
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos, labels=node_labels)
plt.savefig(folder+'/'+'tree_'+name+'.png')
def importModule(module):
try:
imp.find_module(module)
found = True
importlib.import_module(module)
except ImportError:
found = False
return found