/
bindings.py
182 lines (135 loc) · 5.48 KB
/
bindings.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
import io
import math
import pdb
import copy
from types import MethodType
import numpy as np
import quantities as pq
import matplotlib.pyplot as plt
from elephant.spike_train_generation import threshold_detection
from neo import AnalogSignal
try:
from pyNN.neuron import HH_cond_exp
from pyNN.neuron import EIF_cond_exp_isfa_ista
import pyNN.neuron as pn
from pyNN.neuron import setup as setup
from pyNN.neuron import DCSource
pyNN_NEURON = True
except (ImportError, AttributeError):
print("Error loading pyNN.neuron")
pyNN_NEURON = False
from sciunit.utils import redirect_stdout
import neuronunit.capabilities.spike_functions as sf
import neuronunit.capabilities as cap
def bind_NU_interface(model):
def load_model(self):
neuron = None
from pyNN import neuron
self.hhcell = neuron.create(EIF_cond_exp_isfa_ista())
pn.setup(timestep=self.dt, min_delay=1.0)
def init_backend(self, attrs = None, cell_name= 'HH_cond_exp', current_src_name = 'hannah', DTC = None, dt=0.01):
backend = 'HHpyNN'
self.current_src_name = current_src_name
self.cell_name = cell_name
self.adexp = True
self.DCSource = DCSource
self.setup = setup
#self.model_path = None
self.related_data = {}
self.lookup = {}
self.attrs = {}
self.neuron = pn
self.model._backend = self
self.backend = self
self.model.attrs = {}
#self.orig_lems_file_path = 'satisfying'
self.model._backend.use_memory_cache = False
#self.model.unpicklable += ['h','ns','_backend']
self.dt = dt
if type(DTC) is not type(None):
if type(DTC.attrs) is not type(None):
self.set_attrs(**DTC.attrs)
assert len(self.model.attrs.keys()) > 0
if hasattr(DTC,'current_src_name'):
self._current_src_name = DTC.current_src_name
if hasattr(DTC,'cell_name'):
self.cell_name = DTC.cell_name
def _local_run(self):
'''
pyNN lazy array demands a minimum population size of 3. Why is that.
'''
results = {}
DURATION = 1000.0
if self.celltype == 'HH_cond_exp':
self.hhcell.record('spikes','v')
else:
self.neuron.record_v(self.hhcell, "Results/HH_cond_exp_%s.v" % str(pn))
#self.neuron.record_gsyn(self.hhcell, "Results/HH_cond_exp_%s.gsyn" % str(neuron))
self.neuron.run(DURATION)
data = self.hhcell.get_data().segments[0]
volts = data.filter(name="v")[0]#/10.0
#data_block = all_cells.get_data()
vm = AnalogSignal(volts,
units = pq.mV,
sampling_period = self.dt*pq.ms)
results['vm'] = vm
results['t'] = vm.times # self.times
results['run_number'] = results.get('run_number',0) + 1
return results
def get_membrane_potential(self):
"""Must return a neo.core.AnalogSignal.
And must destroy the hoc vectors that comprise it.
"""
data = self.hhcell.get_data().segments[0]
volts = data.filter(name="v")[0]
vm = AnalogSignal(volts,
units = pq.mV,
sampling_period = self.dt*pq.ms)
return vm
def set_attrs(self,**attrs):
self.init_backend()
self.model.attrs.update(attrs)
assert type(self.model.attrs) is not type(None)
self.hhcell[0].set_parameters(**attrs)
return self
def inject_square_current(self,current):
attrs = copy.copy(self.model.attrs)
self.init_backend()
self.set_attrs(**attrs)
c = copy.copy(current)
if 'injected_square_current' in c.keys():
c = current['injected_square_current']
stop = float(c['delay'])+float(c['duration'])
duration = float(c['duration'])
start = float(c['delay'])
amplitude = float(c['amplitude'])
electrode = self.neuron.DCSource(start=start, stop=stop, amplitude=amplitude)
electrode.inject_into(self.hhcell)
self.results = self._local_run()
self.vm = self.results['vm']
def get_APs(self,vm):
vm = self.get_membrane_potential()
waveforms = sf.get_spike_waveforms(vm,threshold=-45.0*pq.mV)
return waveforms
def get_spike_train(self,**run_params):
vm = self.get_membrane_potential()
spike_train = threshold_detection(vm,threshold=-45.0*pq.mV)
return spike_train
def get_spike_count(self,**run_params):
vm = self.get_membrane_potential()
return len(threshold_detection(vm,threshold=-45.0*pq.mV))
model.init_backend = MethodType(init_backend,model)
model.get_spike_count = MethodType(get_spike_count,model)
model.get_APs = MethodType(get_APs,model)
model.get_spike_train = MethodType(get_spike_train,model)
model.set_attrs = MethodType(set_attrs, model) # Bind to the score.
model.inject_square_current = MethodType(inject_square_current, model) # Bind to the score.
model.set_attrs = MethodType(set_attrs, model) # Bind to the score.
model.get_membrane_potential = MethodType(get_membrane_potential,model)
model.load_model = MethodType(load_model, model) # Bind to the score.
model._local_run = MethodType(_local_run,model)
model.init_backend(model)
#model.load_model() #= MethodType(load_model, model) # Bind to the score.
return model
if pyNN_NEURON:
HH_cond_exp = bind_NU_interface(HH_cond_exp)