-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlecell-continue.py
189 lines (166 loc) · 7.83 KB
/
singlecell-continue.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
#!/usr/bin/env python
from config import Configurable, quantity, quantity_list
import brian as b
import logging
import numpy as np
from singlecell import SingleCellModel
logger = logging.getLogger('single-cell-continue')
class SingleCellModelContinuedRecorder(Configurable):
def __init__(self, input_data, model):
config = input_data.getNodeAttr('/', 'config')
Configurable.__init__(self, config['recording'])
self._add_config_value('recording_duration', quantity)
self._add_config_value('rate_bin_size', quantity)
self._add_config_value('store_times', quantity_list)
self._add_config_value('current_timestep', int)
self._add_config_value('weights_timestep', int)
self.input_data = input_data
self.dt = quantity(config['dt'])
self.model = model
self.m_spikes = b.SpikeMonitor(model.neuron)
self.m_rates = b.PopulationRateMonitor(model.neuron, self.rate_bin_size)
self.m_exc_syn_currents = b.RecentStateMonitor(
model.neuron, 'I_exc', self.recording_duration,
timestep=self.current_timestep)
self.m_inh_syn_currents = b.RecentStateMonitor(
model.neuron, 'I_inh', self.recording_duration,
timestep=self.current_timestep)
self.m_exc_weights = b.StateMonitor(
model.exc_synapses, 'w', record=True,
timestep=self.weights_timestep)
self.m_inh_weights = b.StateMonitor(
model.inh_synapses, 'w', record=True,
timestep=self.weights_timestep)
self.model.add(
self.m_spikes, self.m_rates, self.m_exc_syn_currents,
self.m_inh_syn_currents, self.m_exc_weights, self.m_inh_weights)
def record(self, outfile, time):
self._store_group_memberships(outfile)
outfile.flush()
time_passed = input_data.root.weights.inhibitory.times[-1]
logger.info('Running time interval of duration %ds up to %ds' % (
time, time / b.second + time_passed))
self.model.run(time, report='text')
logger.info('Storing data')
self._store_rates(outfile)
self._store_spikes(outfile)
self._store_weights(outfile)
outfile.flush()
@staticmethod
def _store_array_with_unit(
outfile, where, name, array, unit, *args, **kwargs):
node = outfile.createArray(where, name, array, *args, **kwargs)
node.attrs.unit = unit
return node
def _store_rates(self, outfile):
group = outfile.createGroup('/', 'rates', "Firing rates.")
self._store_array_with_unit(
outfile, group, 'rates',
np.hstack((self.input_data.root.rates.rates,
self.m_rates.rate / b.hertz)),
'hertz')
self._store_array_with_unit(
outfile, group, 'times',
np.hstack((self.input_data.root.rates.times,
self.input_data.root.rates.times[-1] +
(self.rate_bin_size + self.m_rates.times) / b.second)),
'second', "Times of the firing rate estimation bins.")
def _store_group_memberships(self, outfile):
group = outfile.createGroup('/', 'group_memberships')
outfile.createArray(
group, 'inhibitory', self.model.input_groups.inh_group_membership)
outfile.createArray(
group, 'excitatory', self.model.input_groups.exc_group_membership)
def _store_recent_currents(self, outfile, interval_index):
from numpy.testing import assert_allclose
assert_allclose(
self.m_exc_syn_currents.times, self.m_inh_syn_currents.times)
group = outfile.createGroup(
'/currents', 't' + str(interval_index), createparents=True)
self._store_array_with_unit(
outfile, group, 'times',
self.input_data.root.currents.times[-1] +
(self.dt * self.current_timestep + self.m_exc_syn_currents.times) /
b.second,
'second')
self._store_array_with_unit(
outfile, group, 'excitatory',
self.m_exc_syn_currents.values / b.amp, 'amp')
self._store_array_with_unit(
outfile, group, 'inhibitory',
self.m_inh_syn_currents.values / b.amp, 'amp')
def _store_spikes(self, outfile):
self._store_array_with_unit(
outfile, '/', 'spikes',
np.hstack((self.input_data.root.spikes,
self.input_data.root.weights.inhibitory.times[-1] +
self.dt * self.weights_timestep / b.second +
self.m_spikes[0])),
'second', "Spike times of model neuron.")
def _store_weights(self, outfile):
weight_group = outfile.createGroup('/', 'weights', "Synaptic weights.")
group = outfile.createGroup(weight_group, 'inhibitory')
self._store_array_with_unit(
outfile, group, 'weights',
np.hstack((self.input_data.root.weights.inhibitory.weights,
self.m_inh_weights.values / b.siemens)),
'siemens')
self._store_array_with_unit(
outfile, group, 'times',
np.hstack((self.input_data.root.weights.inhibitory.times,
self.input_data.root.weights.inhibitory.times[-1] +
(self.dt * self.weights_timestep +
self.m_inh_weights.times) / b.second)),
'second', "Times of the recorded synaptic weights.")
group = outfile.createGroup(weight_group, 'excitatory')
self._store_array_with_unit(
outfile, group, 'weights',
np.hstack((self.input_data.root.weights.excitatory.weights,
self.m_exc_weights.values / b.siemens)),
'siemens')
self._store_array_with_unit(
outfile, group, 'times',
np.hstack((self.input_data.root.weights.excitatory.times,
self.input_data.root.weights.excitatory.times[-1] +
(self.dt * self.weights_timestep +
self.m_exc_weights.times) / b.second)),
'second', "Times of the recorded synaptic weights.")
if __name__ == '__main__':
import argparse
import os.path
import tables
from brian.globalprefs import set_global_preferences
set_global_preferences(useweave=True)
logging.basicConfig()
logger.setLevel(logging.INFO)
parser = argparse.ArgumentParser(
description="Run the Vogels et al. 2011 single cell model.")
parser.add_argument(
'-i', '--input', type=str, nargs=1, required=True,
help="Path to the input file for which to continue the simulation.")
parser.add_argument(
'-t', '--time', type=float, nargs=1, required=True,
help="Additional time to simulate in seconds.")
parser.add_argument(
'output', nargs=1, type=str,
help="Filename of the HDF5 output file.")
parser.add_argument(
'label', nargs='?', type=str,
help="Label for the simulation. Will create a directory with the same "
+ "to store the produced data.")
args = parser.parse_args()
outpath = 'Data'
if args.label is not None:
outpath = os.path.join(outpath, args.label)
with tables.openFile(args.input[0], 'r') as input_data:
config = input_data.getNodeAttr('/', 'config')
b.defaultclock.dt = quantity(config['dt'])
model = SingleCellModel(config)
model.exc_synapses.w[:, :] = \
input_data.root.weights.excitatory.weights[:, -1]
model.inh_synapses.w[:, :] = \
input_data.root.weights.inhibitory.weights[:, -1]
recorder = SingleCellModelContinuedRecorder(input_data, model)
with tables.openFile(os.path.join(outpath, args.output[0]), 'w') as outfile:
outfile.setNodeAttr('/', 'config', config)
recorder.record(outfile, args.time[0] * b.second)