-
Notifications
You must be signed in to change notification settings - Fork 1
/
AplysiaNet.py
211 lines (168 loc) · 7.54 KB
/
AplysiaNet.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
from Interface import Interface
from Neuron import Neuron
from Synapse import Synapse, GSynapse
from tkinter import *
import csv
import time
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
class AplysiaNet:
def __init__(self, duration, step_size):
# Initializes the interface and runs it until the user enters valid inputs and clicks "run."
root = Tk()
interface = Interface(root)
while interface.get_status() == 'running':
root.update()
self.start_time = time.time()
# The input from the interface is stored here
self.input = interface.get_input()
# The current from the user input is stored here
self.current = interface.get_current()
# The model is set up to run for duration seconds
self.duration = duration
# The frequency will be evaluated every step_size seconds
self.step_size = step_size
# The model starts at time = 0 seconds
self.t = 0
# This will store the neurons in the circuit
self.neurons = []
# This will store the synapses in the circuit
self.synapses = []
self.neurons_to_output = []
self.outputs = []
def set_neurons(self, neurons):
self.neurons = neurons
def set_synapses(self, synapses):
self.synapses = synapses
def set_neurons_to_output(self, neurons_to_output):
self.neurons_to_output = neurons_to_output
def set_synapses_to_output(self, synapses_to_output):
self.synapses_to_output = synapses_to_output
def run(self):
# Creates a .csv file for the data
data_file = open('AplysiaData.csv', 'w')
writer = csv.writer(data_file, lineterminator= '\n')
# Creates a .csv file for the synapse data
syn_file = open('SlugInput2.txt', 'w')
syn_writer = csv.writer(syn_file, delimiter='\t', lineterminator='\n')
syn_writer.writerow(['Time', 'PC', 'RC', 'CC', 'HC'])
# At each time step, update each neuron and run the output through the synapses to become the next inputs
for index in range(0, (int)((1 / self.step_size)*(self.duration))):
g_values = [self.t]
write_g = 0
for neuron in self.neurons:
neuron.update(self.t)
for synapse in synapses:
synapse.update(self.t)
for synapse in self.synapses_to_output:
g_values.append(synapse.get_g())
if synapse.write_g():
write_g = 1
if write_g:
syn_writer.writerow(g_values)
self.t += self.step_size
# Handle the neuron outputs
for neuron in self.neurons_to_output:
# The following line can be uncommented to print a subset of the neuron voltages
#self.selective_print(neuron.get_output())
self.outputs.append(neuron.get_output())
# This handles the output data to be written
int_arr = list(range((int)(self.duration/self.step_size)))
self.time_arr = [i * self.step_size for i in int_arr]
output_arr = [self.time_arr] + self.outputs
output_arr = zip(*output_arr)
# This writes every 10th time step to a file if uncommented.
#for i, val in enumerate(output_arr):
#if i % 10 == 0:
#writer.writerow(val)
# Computes and prints the total run time of the simulation minus graphing
end_time = time.time()
total_time = end_time - self.start_time
print('Total time = %s seconds' % total_time)
# Closes the data files
data_file.close()
syn_file.close()
# Creates the graph and displays it
self.graph()
# Generates an array of chemical or mechanical input to a neuron given stimulus start and end times
def handle_inputs(self, start, end, storage, type):
if type == 'chem':
current = self.current[0]
elif type == 'mech':
current = self.current[1]
else:
raise ValueError('Input type must be either chemical (chem) or mechanical (mech).')
if start == end:
return
for index in range(0, (int)((1 / self.step_size)*(self.duration))):
if index*self.step_size <= start or index*self.step_size > end:
storage[index] = 0
else:
storage[index] = current
# Prints out an array of every 0.25 seconds; this is for testing purposes
def selective_print(self, arr):
arr_str = []
index = 0
while index < len(arr):
arr_str.append(arr[index])
index = index + int(0.25 / self.step_size)
print(arr_str)
# This graphs the membrane potential vs time of all of the neurons to output
def graph(self):
start_time = 0
end_time = 8.5 # Normally 8.5. For IK replication, 0.23636
resolution = 1 # Normally 1. For IK replication, 0.01
# Initializes the graph
figure = plt.figure(figsize=(18, 18))
timeticks = np.arange(start_time, end_time, resolution)
grid = gridspec.GridSpec(self.neurons_to_output.__len__(), 1)
index = 0
# Adds a new plot for each neuron
for neuron in self.neurons_to_output:
graph = figure.add_subplot(grid[index, 0])
potentialticks = np.arange(-90, 40, 10)
neuron_data = neuron.get_output()
row_count = neuron_data.__len__()
graph.plot(self.time_arr[0:row_count], neuron_data[0:row_count])
graph.set_title('Membrane Potential of %s' % neuron.get_name())
graph.set_xlabel('Time (s)')
graph.set_ylabel('Membrane Potential (mV)')
graph.axis([start_time, end_time, -90, 40])
graph.set_xticks(timeticks)
graph.set_yticks(potentialticks)
graph.grid(True)
index += 1
# Displays the graph
plt.show()
step_size = 0.00001 # original step: 0.000008
# Creates the network.
network = AplysiaNet(8.5, step_size)
# This code gets the input from the user and translates into an input array for the network.
inputs = network.input
chem_input = [0]*((int)((1 / network.step_size)*(network.duration)) + 1)
mech_input = [0]*((int)((1 / network.step_size)*(network.duration)) + 1)
pc_input = [0]*((int)((1 / network.step_size)*(network.duration)) + 1)
rc_input = [0]*((int)((1 / network.step_size)*(network.duration)) + 1)
network.handle_inputs(inputs[0], inputs[1], chem_input, 'chem')
network.handle_inputs(inputs[2], inputs[3], mech_input, 'mech')
# This code builds the circuit.
chem = Neuron(0.02, 0.2, -65, 8, -70, chem_input, step_size, 'chem')
mech = Neuron(0.02, 0.2, -65, 8, -70, mech_input, step_size, 'mech')
PC = Neuron(0.0005, 0.3, -65, 0.1, -70, pc_input, step_size, 'PC')
RC = Neuron(0.0005, 0.3, -65, 0.1, -70, rc_input, step_size, 'RC')
hco1 = Synapse(PC, RC, 1.5, -80, 10, 21, 20, step_size)
hco2 = Synapse(RC, PC, 1.5, -80, 10, 21, 20, step_size)
out1 = GSynapse(PC, 0.2, 40, 10, 20, 20, step_size)
out2 = GSynapse(RC, 0.2, 40, 10, 20, 20, step_size)
chemin = Synapse(chem, RC, 0.01, -90, 10, 20, 20, step_size)
mechin = Synapse(mech, PC, 0.035, -90, 10, 20, 20, step_size)
neurons = [chem, mech, PC, RC]
synapses = [hco1, hco2, out1, out2, chemin, mechin]
gsynapses = [out1, out2, out2, out2]
# The neurons and synapses are given to the network, and the simulation runs.
network.set_neurons(neurons)
network.set_synapses(synapses)
network.set_neurons_to_output(neurons)
network.set_synapses_to_output(gsynapses)
network.run()