-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualization.py
213 lines (195 loc) · 9.6 KB
/
Visualization.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import matplotlib.pylab as plt
import matplotlib.patches as patches
import bokeh.plotting as bk
import networkx as nx
import numpy as np
import warnings
import igraph as ig
#import cairo
class Visualization:
def __init__(self,brain):
self._Brain = brain
self._NumberOfNeurons = self._Brain._NumberOfNeurons
self._Neurons = list(range(self._NumberOfNeurons))
self._FigNum = 0
self.ConstructNetwork()
self.SortNeurons()
def ConstructNetwork(self):
nodeData = [(0,{'color':'#db203c','pos':self._Brain._NeuronPosition[0]})]+[(n,{'color':'#1941d3','pos':self._Brain._NeuronPosition[n]}) for n in range(1,self._NumberOfNeurons)]
edgeData=[(*p,{'weight':self._Brain._SynapseWeight[p]/self._Brain._SynapseStrengthLimit}) for p in self._Brain._SynapseWeight.keys() if self._Brain._SynapseWeight[p]>0]
self._Network= nx.DiGraph()
self._Network.add_nodes_from(nodeData)
self._Network.add_edges_from(edgeData)
def ConstructNetwork_igraph(self, linewidth):
ymax = max([self._Brain._NeuronPosition[n][1] for n in range(0,self._NumberOfNeurons)])
self._g = ig.Graph(directed=True)
self._g.add_vertices(self._NumberOfNeurons)
self._g.vs["label"] = list(range(self._NumberOfNeurons))
self._g.vs['x'] = [self._Brain._NeuronPosition[n][0] for n in range(0,self._NumberOfNeurons)]
self._g.vs['y'] = [ymax-self._Brain._NeuronPosition[n][1] for n in range(0,self._NumberOfNeurons)]
nodeData = [p for p in self._Brain._SynapseWeight.keys() if self._Brain._SynapseWeight[p]>0]
self._g.add_edges(nodeData)
self._g.es["weight"] = [self._Brain._SynapseWeight[p]/self._Brain._SynapseStrengthLimit for p in self._Brain._SynapseWeight.keys() if self._Brain._SynapseWeight[p]>0]
self._vis_style = {}
outdegree = self._g.outdegree()
self._vis_style["vertex_size"] = [x/max(outdegree)*10+5 for x in outdegree]
self._vis_style["edge_curved"] = False
self._vis_style["weights"] = self._g.es["weight"]
self._vis_style["edge_width"] = [linewidth*e for e in self._g.es["weight"]]
self._vis_style["arrow_size"] = [e for e in self._g.es["weight"]]
self._vis_style["edge_curved"] = False
self._vis_style['vertex_color'] = 'blue'
self._vis_style["vertex_label_color"] = 'white'
def PlotState(self,neurons=[0]):
self._FigNum += 1
plt.figure(self._FigNum)
for idx, n in enumerate(neurons):
plt.subplot(len(neurons),1,idx+1)
p = plt.plot(self._Brain._Time,self._Brain._VV[:,n])
plt.ylim( (-90, 80) )
plt.setp(p, 'Color', [0.7,0.3,0.3], 'linewidth', 3)
plt.grid(True)
plt.xlabel("Time")
plt.ylabel("Neuron {}".format(n))
plt.show()
def DrawNetwork(self, edgelabels=False):
warnings.filterwarnings("ignore")
fig1 = plt.figure(2) #self._FigureNumber)
#self._FigureNumber += 1
ax = fig1.add_subplot(111, aspect='equal')
plt.xlim((0,80))
plt.ylim((0,80))
#for n in self._Neurons:
# self._NodeLabels[n]=n._ID
# if n._ActiveQ and n._ID != 0:
# nx.set_node_attributes(self._Network, 'color', {n:'#918b21'})
pos = nx.get_node_attributes(self._Network,'pos')
weightcap = max(self._Brain._SynapseWeight.values())
weights = [self._Network[u][v]['weight'] for u,v in self._Network.edges()]
nodecolors = list(nx.get_node_attributes(self._Network, 'color').values())
nx.draw(self._Network, pos, node_size=110, width=weights)
nx.draw_networkx_nodes(self._Network, pos, node_size=110,node_color=nodecolors)
nx.draw_networkx_labels(self._Network, pos, labels={n:n for n in self._Neurons},font_color=[1,1,1],font_family='Times',font_size=8)
nx.draw_networkx_edges(self._Network, pos, edge_color='#2c8c7d', width=weights,arrows=True)
# Background color to represent the experiment dish.
ax.add_patch(patches.Rectangle((10, 0),60,10,facecolor=[0.1,0.1,0.6],alpha=0.2))
ax.add_patch(patches.Rectangle((70, 10),10,60,facecolor=[0.1,0.1,0.6],alpha=0.2))
ax.add_patch(patches.Rectangle((10, 70),60,10,facecolor=[0.1,0.1,0.6],alpha=0.2))
ax.add_patch(patches.Rectangle((0, 10),10,60,facecolor=[0.1,0.1,0.6],alpha=0.2))
ax.add_patch(patches.Rectangle((10, 10),60,60,facecolor=[0.1,0.1,0.6],alpha=0.2))
plt.show()
def SortNeurons(self):
d = []
for n in range(self._NumberOfNeurons):
d.append((n,self._Brain._NeuronPosition[n][0]**2+self._Brain._NeuronPosition[n][1]**2))
sl = sorted(d, key=lambda x: x[1])
self._SortedNeurons = [x[0] for x in sl]
def PlotTimeFrequencyDot(self,cutoff=20,radius=2,plot_height=100,plot_width=300, sorted=False):
time = self._Brain._Time
if sorted:
#neurons = [row[0] for row in self._SortedNeurons]
neurons = self._SortedNeurons
title = "Firing Pattern of Neurons Sorted by Distance"
factors = [str(n) for n in self._SortedNeurons]
else:
neurons = self._Neurons
title = "Firing Pattern of Neurons"
factors = [str(n) for n in self._Neurons]
datax = np.array([])
datay = np.array([])
for i,n in enumerate(neurons):
V = self._Brain._VV[:,n]
d = time[V>cutoff]
datax=np.append(datax, d)
datay=np.append(datay, (i+1)*np.ones_like(d))
p = bk.figure(title=title, y_range=factors, x_axis_label='Time', y_axis_label='Neurons',x_range=(0,time[-1]),plot_height=plot_height,plot_width=plot_width)
p.circle(datax,datay,radius=radius, fill_color='darkblue', line_color='darkblue')
bk.show(p)
def PlotTimeFrequency(self):
self._FigNum += 1
plt.figure(self._FigNum)
time = self._Brain._Time
data = np.zeros((self._NumberOfNeurons,len(time)))
for i,n in enumerate(self._Neurons):
for j,t in enumerate(time):
data[i,j] = self._Brain._VV[j,n]
ax = plt.subplot(1,1,1)
p=ax.pcolorfast(time,range(len(self._Neurons)),data)
plt.colorbar(p)
plt.xlabel('Time (Seconds)')
plt.ylabel('Neurons')
plt.show()
def PlotAdjacencyMatrix(self):
plt.figure(self._FigNum)
self._FigNum += 1
M = nx.to_numpy_matrix(self._Network)
ax1 = plt.subplot(131)
p=ax1.pcolorfast(M,cmap='Blues')
plt.colorbar(p)
ax2 = plt.subplot(132)
p=ax2.pcolorfast(self.ComputeSortedAdjacencyMatrix(),cmap='Blues')
ax2.set_xticks(self._SortedNeurons)
ax2.set_yticks(self._SortedNeurons)
plt.colorbar(p)
ax3 = plt.subplot(133)
H=np.array(np.ndarray.flatten(M))
plt.hist(H[0],20)
plt.show()
def ComputeSortedAdjacencyMatrix(self):
M = np.array([np.array([self._Brain._SynapseWeight[(n1,n2)]/self._Brain._SynapseStrengthLimit for n2 in self._SortedNeurons]) for n1 in self._SortedNeurons])
return M
def PlotConnectivityProperties(self):
#self._AverageConnectivity.append(nx.average_node_connectivity(self._Network))
self._FigNum += 1
plt.figure(self._FigNum)
plt.subplot(131)
self._DegreeDistribution = sorted(nx.degree(self._Network).values(),reverse=True)
p=plt.loglog(self._DegreeDistribution,'-',marker='o')
plt.setp(p,color='darkblue')
plt.title("Degree rank plot")
plt.ylabel("degree")
plt.xlabel("rank")
plt.subplot(132)
#self.GetSynapseCount()
plt.hist(self._Brain._SynapseCount, 20,facecolor='lightblue', alpha=0.75, edgecolor='darkblue')
plt.title('Distribution of number of synapses formed')
plt.xlabel('Number of synapses')
plt.ylabel('Count')
plt.xlim( (0, self._Brain._SynapseLimit) )
plt.subplot(133)
weights = list(self._Brain._SynapseWeight.values())#[self._Network.get_edge_data(n1,n2,default=0)['weight'] for n1,n2 in self._Network.edges()]
plt.hist(weights, 20, facecolor='lightblue', alpha=0.75, edgecolor='darkblue')
plt.title('Distribution of edge weights')
plt.xlabel('Edge weight')
plt.ylabel('Count')
plt.xlim( (0, self._Brain._SynapseStrengthLimit) )
plt.show()
def PlotDegreeDistribution(self):
idegree = list(self._Network.in_degree().values())
odegree = list(self._Network.out_degree().values())
try:
self._FigNum += 1
plt.figure(self._FigNum)
plt.subplot(131)
plt.hist(idegree, int(max(idegree)/2), facecolor='lightblue', alpha=0.75, edgecolor='darkblue')
plt.title('in-degree')
plt.xlabel('degree')
plt.ylabel('count')
plt.subplot(132)
plt.hist(odegree, int(max(odegree)/2), facecolor='lightblue', alpha=0.75, edgecolor='darkblue')
plt.title('out-degree')
plt.xlabel('degree')
plt.ylabel('count')
plt.subplot(133)
plt.hist(self._DegreeDistribution, int(max(self._DegreeDistribution)/2), facecolor='lightblue', alpha=0.75, edgecolor='darkblue')
plt.title('total degree distribution')
plt.xlabel('degree')
plt.ylabel('count')
plt.show()
except:
print("No connections at all")
def GetSynapseCount(self):
self._SynapseCount = []
for n in self._Neurons:
self._Brain_SynapseCount
self._SynapseCount.append(sum([self._Brain._SynapseWeight[(n,i)] for i in self._Neurons]))