-
Notifications
You must be signed in to change notification settings - Fork 124
/
standardmodels.py
306 lines (248 loc) · 9.52 KB
/
standardmodels.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
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
"""
Standard cells for the Arbor module.
:copyright: Copyright 2006-2024 by the PyNN team, see AUTHORS.
:license: CeCILL, see LICENSE for details.
"""
import logging
from copy import deepcopy
import arbor
from ..standardmodels import cells, ion_channels, synapses, electrodes, receptors, build_translations
from ..parameters import ParameterSpace, IonicSpecies
from ..morphology import Morphology, NeuriteDistribution, LocationGenerator
from .cells import CellDescriptionBuilder
from .simulator import state
from .morphology import LabelledLocations
logger = logging.getLogger("PyNN")
class SpikeSourcePoisson(cells.SpikeSourcePoisson):
__doc__ = cells.SpikeSourcePoisson.__doc__
translations = build_translations(
('start', 'tstart'),
('rate', 'freq'),
('duration', 'tstop', "start + duration", "tstop - tstart"),
)
# todo: manage "seed"
arbor_cell_kind = arbor.cell_kind.spike_source
arbor_schedule = arbor.poisson_schedule
class SpikeSourceArray(cells.SpikeSourceArray):
__doc__ = cells.SpikeSourceArray.__doc__
translations = build_translations(
('spike_times', 'times'),
)
arbor_cell_kind = arbor.cell_kind.spike_source
arbor_schedule = arbor.explicit_schedule
class BaseCurrentSource(object):
pass
class DCSource(BaseCurrentSource, electrodes.DCSource):
__doc__ = electrodes.DCSource.__doc__
translations = build_translations(
('amplitude', 'current'),
('start', 'tstart'),
('stop', 'duration', "stop - start", "tstart + duration")
)
def inject_into(self, cells, location=None): # rename to `locations` ?
if hasattr(cells, "parent"):
cell_descr = cells.parent._arbor_cell_description.base_value
index = cells.parent.id_to_index(cells.all_cells.astype(int))
else:
cell_descr = cells._arbor_cell_description.base_value
index = cells.id_to_index(cells.all_cells.astype(int))
self.parameter_space.shape = (1,)
if location is None:
raise NotImplementedError
elif isinstance(location, str):
location = LabelledLocations(location)
elif isinstance(location, LocationGenerator):
# morphology = cells._arbor_cell_description.base_value.parameters["morphology"].base_value # todo: evaluate lazyarray
# locations = location.generate_locations(morphology, label="dc_current_source")
# assert len(locations) == 1
# locset = locations[0]
pass
else:
raise TypeError("location must be a string or a LocationGenerator")
cell_descr.add_current_source(
model_name="iclamp",
location_generator=location,
index=index,
parameters=self.native_parameters
)
class StepCurrentSource(BaseCurrentSource, electrodes.StepCurrentSource):
__doc__ = electrodes.StepCurrentSource.__doc__
translations = build_translations(
('amplitudes', 'amplitudes'),
('times', 'times')
)
class ACSource(BaseCurrentSource, electrodes.ACSource):
__doc__ = electrodes.ACSource.__doc__
translations = build_translations(
('amplitude', 'amplitude'),
('start', 'start'),
('stop', 'stop'),
('frequency', 'frequency'),
('offset', 'offset'),
('phase', 'phase')
)
class NoisyCurrentSource(BaseCurrentSource, electrodes.NoisyCurrentSource):
translations = build_translations(
('mean', 'mean'),
('start', 'start'),
('stop', 'stop'),
('stdev', 'stdev'),
('dt', 'dt')
)
class StaticSynapse(synapses.StaticSynapse):
__doc__ = synapses.StaticSynapse.__doc__
translations = build_translations(
('weight', 'weight'),
('delay', 'delay'),
)
def _get_minimum_delay(self):
d = state.min_delay
if d == 'auto':
d = state.dt
return d
class NaChannel(ion_channels.NaChannel):
translations = build_translations(
('conductance_density', 'gnabar'),
('e_rev', 'ena'),
)
variable_translations = {
'm': ('na', 'm'),
'h': ('na', 'h')
}
model = "na"
conductance_density_parameter = 'gnabar'
def get_model(self, parameters=None):
return "na"
class KdrChannel(ion_channels.KdrChannel):
translations = build_translations(
('conductance_density', 'gkbar'),
('e_rev', 'ek'),
)
variable_translations = {
'n': ('kdr', 'n')
}
conductance_density_parameter = 'gkbar'
def get_model(self, parameters=None):
return "kdr"
class PassiveLeak(ion_channels.PassiveLeak):
translations = build_translations(
('conductance_density', 'g'),
('e_rev', 'e'),
)
conductance_density_parameter = 'g'
global_parameters = ['e']
def get_model(self, parameters=None):
if parameters:
assert parameters._evaluated
param_entries = []
for name, value in parameters.items():
if name in self.global_parameters:
param_entries.append(f"{name}={value}")
param_str = ",".join(param_entries)
for name in self.global_parameters:
parameters.pop(name, None)
return f"pas/{param_str}"
else:
return "pas"
class PassiveLeakHH(ion_channels.PassiveLeak):
translations = build_translations(
('conductance_density', 'gl'),
('e_rev', 'el'),
)
conductance_density_parameter = 'gl'
global_parameters = ['el']
def get_model(self, parameters=None):
if parameters:
assert parameters._evaluated
param_entries = []
for name, value in parameters.items():
if name in self.global_parameters:
param_entries.append(f"{name}={value}")
param_str = ",".join(param_entries)
for name in self.global_parameters:
parameters.pop(name, None)
return f"leak/{param_str}"
else:
return "leak"
class MultiCompartmentNeuron(cells.MultiCompartmentNeuron):
"""
"""
default_initial_values = {}
ion_channels = {}
post_synaptic_entities = {}
arbor_cell_kind = arbor.cell_kind.cable
variable_map = {"v": "Vm"}
def __init__(self, **parameters):
# replace ion channel classes with instantiated ion channel objects
for name, ion_channel in self.ion_channels.items():
self.ion_channels[name] = ion_channel(**parameters.pop(name, {}))
# ditto for post synaptic responses
for name, pse in self.post_synaptic_entities.items():
self.post_synaptic_entities[name] = pse(**parameters.pop(name, {}))
super(MultiCompartmentNeuron, self).__init__(**parameters)
for name, ion_channel in self.ion_channels.items():
self.parameter_space[name] = ion_channel.parameter_space
for name, pse in self.post_synaptic_entities.items():
self.parameter_space[name] = pse.parameter_space
self.extra_parameters = {}
self.spike_source = None
def get_schema(self):
schema = {
"morphology": Morphology,
"cm": NeuriteDistribution,
"Ra": float,
"ionic_species": {
"na": IonicSpecies,
"k": IonicSpecies,
"ca": IonicSpecies,
"cl": IonicSpecies
}
}
#for name, ion_channel in self.ion_channels.items():
# schema[name] = ion_channel.get_schema()
return schema
def translate(self, parameters, copy=True):
"""Translate standardized model parameters to simulator-specific parameters."""
if copy:
_parameters = deepcopy(parameters)
else:
_parameters = parameters
cls = self.__class__
if parameters.schema != self.get_schema():
# should replace this with a PyNN-specific exception type
raise Exception(f"Schemas do not match: {parameters.schema} != {self.get_schema()}")
# translate ion channel
arbor_description = CellDescriptionBuilder(_parameters, self.ion_channels, self.post_synaptic_entities)
native_parameters = {"cell_description": arbor_description}
return ParameterSpace(native_parameters, schema=None, shape=_parameters.shape)
def reverse_translate(self, native_parameters):
raise NotImplementedError
@property # can you have a classmethod-like property?
def default_parameters(self):
return {}
@property
def segment_names(self): # rename to section_names?
return [seg.name for seg in self.morphology.segments]
def has_parameter(self, name):
"""Does this model have a parameter with the given name?"""
return False # todo: implement this
def get_parameter_names(self):
"""Return the names of the parameters of this model."""
raise NotImplementedError
@property
def recordable(self):
raise NotImplementedError
def can_record(self, variable, location=None):
return True # todo: implement this properly
@property
def receptor_types(self):
return self.post_synaptic_entities.keys()
class CondExpPostSynapticResponse(receptors.CondExpPostSynapticResponse):
translations = build_translations(
('locations', 'locations'),
('e_syn', 'e'),
('tau_syn', 'tau')
)
model = "expsyn"
recordable = ["gsyn"]
variable_map = {"gsyn": "g"}