-
Notifications
You must be signed in to change notification settings - Fork 0
/
hop_styles.py
69 lines (60 loc) · 2.13 KB
/
hop_styles.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
"""
This module contains the class HopStyles, which is used to style the graph of the Hopfield network.
"""
ACTIVE_COLOR = "yellow"
IDLE_COLOR = "darkblue"
class HopStyles:
def __init__(self, hopfield):
self.hopfield = hopfield
self.N = hopfield.N
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def get_edges_style(self):
"""
strong synaptics are in stronger green, weak synaptics are in lighter green / grey
"""
colors = []
widths = []
for i in range(self.N):
for j in range(i + 1, self.N):
weight = self.hopfield.weights[i][j]
# make many shades of green
if weight > 0:
colors.append((weight, 0, weight, weight))
else:
colors.append((0.5, 0.5, 0.5, abs(weight)))
widths.append(abs(weight) * 100)
# normalize widths, if very big graph make the edges smaller
max_width = max(widths)
if max_width != 0:
widths = [50*width / max_width for width in widths]
if len(widths) >= 100:
widths = [width / 20 for width in widths]
widths = [width if width > 0.1 else 0.1 for width in widths]
return colors, widths
def get_nodes_colors(self, neurons=None):
"""
Return the color of the nodes
"""
if neurons is None:
neurons = self.hopfield.neurons
return [
ACTIVE_COLOR if neuron == 1 else IDLE_COLOR for neuron in list(neurons)
]
def get_nodes_sizes(self):
"""
Return the size of the nones based on ther energy
"""
nodes_sizes = []
for i in range(self.N):
energy = self.hopfield.getLocalField(i)
nodes_sizes.append(abs(energy) * 100)
# normalize the sizes
max_size = max(nodes_sizes)
if max_size != 0:
nodes_sizes = [size / max_size * 1000 for size in nodes_sizes]
else:
nodes_sizes = [100 for _ in nodes_sizes]
return nodes_sizes