-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhello_worm.py
More file actions
423 lines (362 loc) · 12.2 KB
/
hello_worm.py
File metadata and controls
423 lines (362 loc) · 12.2 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import csv
import pandas
import random
import pickle
import analysis as al
from math import log
try:
import pygraphviz
from networkx.drawing.nx_agraph import graphviz_layout
except ImportError:
try:
import pydotplus
from networkx.drawing.nx_pydot import graphviz_layout
except ImportError:
raise ImportError("This example needs Graphviz and either "
"PyGraphviz or PyDotPlus")
#import Graph Data and set layout position
G = nx.read_graphml("data/c.elegans.herm_pharynx_1.graphml")
pos = graphviz_layout(G, prog='sfdp', args='')
#assign neurodata type to nodes
def assign_neuro_type():
colnames = ['NAME', 'GROUP', 'TYPE']
data = pandas.read_csv('data/neurogroup.csv', names=colnames)
names = data.NAME.tolist()
groups = data.GROUP.tolist()
types = data.TYPE.tolist()
for n,nbrs in G.adjacency_iter():
for i in range(len(names)):
if G.node[n]['cell_name'] == names[i]:
G.node[n]['cell_type'] = types[i]
#determine if a neuron is excitory or inhibitory
def exin():
i = 1.0
for n,nbrs in G.adjacency_iter():
NT_types = ['Ach', 'DA', 'GABA', '5-HT']
if G.node[n]['neurotransmitters'] == NT_types[0]:
G.node[n]['exin'] = 1
elif G.node[n]['neurotransmitters'] == NT_types[1]:
G.node[n]['exin'] = 1
elif G.node[n]['neurotransmitters'] == NT_types[2]:
#10% are labeled inhibitory
G.node[n]['exin'] = -1
i += 1.0
else:
#63% of nodes are unassigned
G.node[n]['exin'] = 1
ratio_inhibitory = i / 270.0
return ratio_inhibitory
#initialise all perimeter nodes to have the parameter activity
def init_activity_perimeter():
init_active_nodes = 0
for n,nbrs in G.adjacency_iter():
#node is inactive when degree of node is smaller than 12 (~19% node activation)
if len(nbrs) < 12:
G.node[n]['activity'] = 100
init_active_nodes += 1
#node is set to initialise as active when the degree of node is greater or equals to 10
else:
G.node[n]['activity'] = 0
#calculate the percentage of active nodes
percentage_init_active = float(init_active_nodes) / G.number_of_nodes()
print(percentage_init_active)
return percentage_init_active
#initialise all hub nodes to have the parameter activity
def init_activity_hub():
init_active_nodes = 0
for n,nbrs in G.adjacency_iter():
#node is inactive when degree of node is larger than 30 (~18.6 percent node activation)
if len(nbrs) > 30:
G.node[n]['activity'] = 100
init_active_nodes += 1
#node is set to initialise as active when the degree of node is greater or equals to 10
else:
G.node[n]['activity'] = 0
#calculate the percentage of active nodes
percentage_init_active = float(init_active_nodes) / G.number_of_nodes()
print(percentage_init_active)
return percentage_init_active
#initialise all nodes to have the parameter activity
def init_activity_random():
init_active_nodes = 0
for n,nbrs in G.adjacency_iter():
#randomly activate roughly 20% of nodes
if random.random() > 0.20:
G.node[n]['activity'] = 0
else:
G.node[n]['activity'] = 100
init_active_nodes += 1
#calculate the percentage of active nodes
percentage_init_active = float(init_active_nodes) / G.number_of_nodes()
print(percentage_init_active)
return percentage_init_active
#initialize refractory period, all
def init_refractory():
init_active_nodes = 0
for n,nbrs in G.adjacency_iter():
G.node[n]['refractory'] = 0
def init_activationCount(iterations,activationData):
for i in range(iterations):
activationData[i] = {}
for n,nbrs in G.adjacency_iter():
activationData[i][n] = 0
#pull function to get the current activity of nodes used to visualize color of nodes in graph
def get_activity():
activity_array = {}
for n,nbrs in G.adjacency_iter():
activity_array[n] = G.node[n]['activity']
#print activity_array
return activity_array
def get_activity_int():
activity_array = [0] * G.number_of_nodes()
i = 0
for n,nbrs in G.adjacency_iter():
activity_array[i] = G.node[n]['activity']
i += 1
return activity_array
def null_activity_int():
activity_array = [0] * G.number_of_nodes()
i = 0
for n,nbrs in G.adjacency_iter():
activity_array[i] = 0
i += 1
return activity_array
#create an array of the degree of each node which can be used in visualization for node size
def node_size_map():
size_array = [0] * G.number_of_nodes()
i = 0
for n,nbrs in G.adjacency_iter():
size_array[i] = G.degree(n) * 5
i += 1
return size_array
def normalize_synapse_weight():
#find maximum weights for each types of synapses
max_e_weight = 1
max_c_weight = 1
for n,nbrs in G.adjacency_iter():
for nbr,eattr in nbrs.items():
for attr, data in eattr.items():
if data['synapse_type'] == 'E':
if data['weight'] > max_e_weight:
max_e_weight = data['weight']
if data['synapse_type'] == 'C':
if data['weight'] > max_c_weight:
max_c_weight = data['weight']
#normalize for each synapse
for n,nbrs in G.adjacency_iter():
for nbr,eattr in nbrs.items():
for attr, data in eattr.items():
if data['synapse_type'] == 'E':
data['normal_weight'] = data['weight'] / max_e_weight
if data['synapse_type'] == 'C':
data['normal_weight'] = data['weight'] / max_c_weight
#interate over all nodes to propogate neural activity
def single_time_step(node_sizes,iteration,refractory):
integral= [0] * G.number_of_nodes()
m = 0
for n,nbrs in G.adjacency_iter():
#check if the node is active
#decay of activity of activated neuron in 2 time steps
if G.node[n]['activity'] > 0:
#an activated node will be activated for 2 timesteps
G.node[n]['activity'] -= 50
current_activity = G.node[n]['activity']
#set refractory period if activity of the node just ended
if current_activity == 0:
#refractory period takes 3 time steps to end
G.node[n]['refractory'] = refractory
#if the node is in the refractory period reduce its count
elif G.node[n]['refractory'] > 0:
G.node[n]['refractory'] -= 1
#else determine the sum of all activities of its neighbouring nodes and decide if the integral is sufficient for firing
else:
#initialize integral
for nbr,eattr in nbrs.items():
for attr, data in eattr.items():
#'E' for electrical synapse
if data['synapse_type'] == 'C':
#summing the activity input into a node and store integral into a list
integral[m] += G.node[nbr]['exin'] * G.node[nbr]['activity'] * data['normal_weight']
#this threshold activation limit is chosen based on the proportion of neuron action potential
if integral[m] > 2:
G.node[n]['activity'] = 100
activationData[iteration][n] += 1
#for tracking the integral list
m += 1
"""
#print current activities and integral
print get_activity()
print integral
"""
#main function for time iteration that contain all smaller functions
def time_itr(time,iteration,refractory):
assign_neuro_type()
exin()
percentageActivation = init_activity_random()
init_refractory()
normalize_synapse_weight()
node_sizes = node_size_map()
activations = {}
for i in range(time):
if sum(get_activity_int()) == 0:
dieDownTime[iteration] = i
died[iteration] = 1
break
'''
for t in range(i,timesteps):
activitydata[iteration][time] = null_activity_int()
break
'''
#figure perimeter set up
#pos=nx.spring_layout(G,iterations=100,scale=2.0)
#n_colors=range(279)
#e_colors=range(3225)
#draw graphs so propogation can be seen in real time
#nx.draw_spectral(G)
#nx.draw_circular(G, node_color=get_activity(), node_size=node_sizes, width=1, style='dotted', arrows=False, cmap=plt.cm.Blues)
#plt.savefig("img/step_cr3_" + str(i) + ".png")
#plt.show()
activitydata[iteration][i] = get_activity()
'''
plt.figure(figsize=(5,5))
nx.draw(G, pos, node_color = get_activity_int(), node_size=node_sizes, width=1, style='dotted', arrows=False, cmap=plt.cm.Blues)
font = {'fontname' : 'Helvetica',
'color' : 'k',
'fontweight' : 'bold',
'fontsize' : 11}
plt.title("C.Elegans Neural Activity", font)
# change font and write text (using data coordinates)
font = {'fontname' : 'Helvetica',
'color' : 'r',
'fontweight' : 'bold',
'fontsize' : 11}
#type of activation
plt.text(0.97, 0.97, "Initial node activation method = Random",
horizontalalignment='right',
transform=plt.gca().transAxes)
#activation period to refractory period ratio
plt.text(0.97, 0.94, "AR = " + str(10.0/refractory),
horizontalalignment='right',
transform=plt.gca().transAxes)
#percentage of initial activation
plt.text(0.97, 0.91, "Percentage of node activated at t0 = " + "{0:.2f}".format(percentageActivation),
horizontalalignment='right',
transform=plt.gca().transAxes)
#iteration
plt.text(0.97, 0.88, "Simulation number = " + str(iteration),
horizontalalignment='right',
transform=plt.gca().transAxes)
#time
plt.text(0.97, 0.85, "t = " + str(i),
horizontalalignment='right',
transform=plt.gca().transAxes)
plt.savefig("img/refractory10test/" + str(refractory) + "_" + str(iteration) + "step_n1_" + str(i) + ".jpg")
plt.close()
'''
single_time_step(node_sizes, iteration, refractory)
return activations
#importing the wormNet data from graphml file
#if __name__ == "__main__":
#a list that stores all the data from
timesteps = 500
simulation_no = 100
activitydata = {}
dieDownTime = {}
activationData = {}
died = {}
#initialize activation data and set all nodes to 0
init_activationCount(simulation_no, activationData)
#define path lengths
hopcountdata = nx.all_pairs_shortest_path_length(G)
#inputing range for refractory
r = 1
for i in range(simulation_no):
activitydata[i] = {}
time_itr(timesteps,i,r)
#save data in file
with open('data/randomResults/dieDownTime.txt', 'wb') as f:
pickle.dump(dieDownTime, f)
with open('data/randomResults/died.txt', 'wb') as f:
pickle.dump(died, f)
with open('data/randomResults/activityData.txt', 'wb') as f:
pickle.dump(activitydata, f)
with open('data/randomResults/hopcountData.txt', 'wb') as f:
pickle.dump(hopcountdata, f)
def frequencyCalcuation(G,timesteps, iteration, activations):
frequency = {}
for i in range(iteration):
activationTotal = 0
activeNodes = 0
for n,nbrs in G.adjacency_iter():
if activations[i][n] > 5:
activationTotal += activations[i][n]
activeNodes += 1
if activeNodes > 0 and activationTotal > 0:
frequency[i] = float(1)/(float(activationTotal)/float(activeNodes)/float(timesteps))
print frequency
return frequency
frequencies = frequencyCalcuation(G,timesteps, simulation_no, activationData)
with open('data/randomResults/frequencies.txt', 'wb') as f:
pickle.dump(frequencies, f)
with open('data/randomResults/frequencies.txt', 'rb') as f:
pickle.load(f)
#load list from pickle
"""
with open('data/randomResults/test', 'rb') as f:
mylist = pickle.load(f)
"""
# figure setup
#time iterate through the network
#time_itr(5)
"""
def node_activity_map():
activity_array = range(G.number_of_nodes())
i = 0
for n,nbrs in G.adjacency_iter():
size_array[i] = G.degree(n) * 5
i += 1
return size_array
"""
"""
G=nx.star_graph(4)
pos=nx.spring_layout(G)
colors=range(4)
nx.draw(G,pos,node_color=['#A0CBE2',#EE1BE2',#EE1BE2',#EE1BE2'])
plt.figure(figsize=(12,12))
#pos=nx.spring_layout(G,iterations=100,scale=2.0)
n_colors=range(279)
e_colors=range(3225)
pos = graphviz_layout(G, prog='sfdp', args='')
nx.draw(G,pos,node_color=n_colors, node_cmap=plt.cm.Blues, edge_color=e_colors, edge_cmap=plt.cm.Reds, width=1, style='solid')
#nx.draw_spectral(G)
plt.savefig("test.png")
plt.show()
"""
"""
#test
for n,nbrs in G.adjacency_iter():
#check if the node is active
for nbr,eattr in nbrs.items():
for attr, data in eattr.items():
weight = data['weight']
synapse = data['synapse_type']
if synapse == 'E':
print ('(%s, %s, %s, %d)' %(n, nbr, synapse, weight))
"""
"""
with open("data/neurogroup.csv") as f:
c = csv.reader(f, delimiter=' ', skipinitialspace=True)
for line in c:
print line[0]
"""
"""
for i in G.nodes(data=True):
data = i[1]
NT_types = ['Ach', 'DA', 'GABA', '5-HT']
if data['neurotransmitters'] == 'Ach':
G.
elif
"""