-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclamps.py
More file actions
206 lines (183 loc) · 7.71 KB
/
clamps.py
File metadata and controls
206 lines (183 loc) · 7.71 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
# ========================================================================
# DEFINES ELECTRODES FOR EVALUATING NEURONAL PROPERTIES
#
# ========================================================================
import numpy as np
from netpyne import specs, sim
import random
class VClamp(object):
def __init__(self,
cell,
delay=100,
duration=400,
T=600,
dt=0.025,
record_step=0.1,
verbose=False):
""" Defines a voltage clamp object for stimulating and recording at the soma
Arguments:
cell: `dict`. Cellular properties specified in NetPyNE dictionary syntax
delay: `float`. Delay until voltage step is taken
duration: `float`. Duration of current injection [ms]
T: `float`. Total duration of simulation [ms]
dt: `float`. Integration timestep [ms]
record_step: `float`. Step size at which to save data [mS]
TODO:
- [ ] Allow for changing the stimulation/recording location
"""
self.cell = cell
self.delay = delay
self.duration = duration
self.T = T
self.dt = dt
self.record_step = record_step
self.verbose = verbose
self.netparams = specs.NetParams()
self._set_netparams_neuron()
self._set_netparams_stim()
self._set_simparams()
def _set_netparams_neuron(self):
self.netparams.cellParams['neuron'] = self.cell
self.netparams.popParams['pop'] = {'cellType': 'neuron', 'numCells': 1}
def _set_netparams_stim(self):
self.netparams.stimSourceParams['vclamp'] = {
'type': 'VClamp',
'dur': [
self.delay,
self.duration,
self.T - (self.delay + self.duration),
],
}
self.netparams.stimTargetParams['vclamp->neuron'] = {
'source': 'vclamp',
'sec': 'soma',
'loc': 0.5,
'conds': {'pop': 'pop', 'cellList': [0]},
}
def _set_simparams(self):
"""
TODO:
- [ ] Make it so that you can record many other types of currents
"""
self.simconfig = specs.SimConfig()
self.simconfig.duration = self.T
self.simconfig.dt = self.dt
self.simconfig.verbose = self.verbose
self.simconfig.recordCells = ["all"]
self.simconfig.recordTraces = {
'i_na': {'sec': 'soma', 'loc': 0.5, 'var': 'ina'},
'i_k': {'sec': 'soma', 'loc': 0.5, 'var': 'ik'},
}
self.simconfig.recordStep = self.record_step
def __call__(self, amp):
"""
Arguments:
amp: `list` of `float`. Voltage at which membrane is to be maintained [mV]
Returns:
`dict`. Simulation data with the following key-value pairs
- `t`: List representing time [ms]
- `i_na`: array of sodium currents
- `i_k`: array of potassium currents
"""
self.netparams.stimSourceParams['vclamp']['amp'] = amp
sim.createSimulateAnalyze(self.netparams, self.simconfig)
results = {
't': sim.allSimData['t'],
'i_na': np.array(sim.allSimData['i_na']['cell_0']),
'i_k': np.array(sim.allSimData['i_k']['cell_0']),
}
return results
class IClamp(object):
def __init__(self, cell, noise = False, delay=100, duration=200, T=400, dt=0.025,
record_step=0.1, verbose=False):
""" Runs a current-clamp experiment stimulating and recording at the soma
Arguments:
cell: `dict`. Cellular properties specified in NetPyNE dictionary syntax
delay: `float`. Time after which current starts [ms]
noise: 'bool' Option to include background noise to simulation.
duration: `float`. Duration of current injection [ms]
T: `float`. Total duration of simulation [ms]
dt: `float`. Integration timestep [ms]
record_step: `float`. Step size at which to save data [mS]
TODO:
- [ ] Allow for changing the stimulation/recording location
"""
self.cell = cell
self.delay = delay
self.noise = noise
self.duration = duration
self.T = T
self.dt = dt
self.record_step = record_step
self.verbose = verbose
self.netparams = specs.NetParams()
self._set_netparams_neuron()
self._set_netparams_stim()
self._set_simparams()
def _set_netparams_neuron(self):
self.netparams.cellParams['neuron'] = self.cell
self.netparams.popParams['pop'] = {'cellType': 'neuron', 'numCells': 1}
def _set_netparams_synmech(self):
self.netparams.synMechParams['exc'] = {'mod': 'Exp2Syn',
'tau1': 0.1,
'tau2': 5.0,
'e': 0}
def _set_netparams_stim(self):
self.netparams.stimSourceParams['iclamp'] = {'type': 'IClamp',
'del': self.delay,
'dur': self.duration}
self.netparams.stimTargetParams['iclamp->neuron'] = {
'source': 'iclamp',
'sec': 'soma',
'loc': 0.5,
'conds': {'pop': 'pop', 'cellList': [0]},
}
if self.noise:
self.netparams.stimSourceParams['bkg'] = {'type': 'NetStim',
'rate': 10}
self.netparams.stimTargetParams['bkg->neuron'] = {
'source': 'bkg',
'sec': 'soma',
'loc': 0.5,
'conds': {'pop': 'pop', 'cellList': [0]},
'delay': 5,
'synMech': 'exc'
}
def _set_simparams(self):
self.simconfig = specs.SimConfig()
self.simconfig.duration = self.T
self.simconfig.dt = self.dt
self.simconfig.verbose = self.verbose
self.simconfig.recordCells = ["all"]
self.simconfig.recordTraces = {
'V_soma': {'sec': 'soma', 'loc': 0.5, 'var': 'v'},
'iclamp': {'sec': 'soma', 'loc': 0.5, 'stim': 'iclamp->neuron', 'var': 'i'}
}
self.simconfig.recordStep = self.record_step
def __call__(self, amp, noise=random.uniform(0.8, 1), weight=0.0001):
"""
Arguments:
amp: `float`. Current to be injected [nA]
Returns:
`dict`. Simulation data with the following key-value pairs
- `t`: List representing time [ms]
- `V`: List representing membrane voltage [mV]
- `spkt`: List of spike times [ms]
- `avg_rate`: Average firing rate across whole recording [Hz]
- `rate`: Firing rate only during current injection [Hz]
"""
self.netparams.stimSourceParams['iclamp']['amp'] = amp
if self.noise:
self.netparams.stimSourceParams['bkg']['noise'] = noise
self.netparams.stimTargetParams['bkg->neuron']['weight'] = weight
sim.createSimulateAnalyze(self.netparams, self.simconfig)
print(list(sim.allSimData.keys()))
results = {
't': np.array(sim.allSimData['t']),
'V': np.array(sim.allSimData['V_soma']['cell_0']),
'spkt': np.array(sim.allSimData['spkt']),
'avg_rate': sim.allSimData['avgRate'],
'rate': len(sim.allSimData['spkt']) / (self.duration / 1000),
'i': sim.allSimData['iclamp']['cell_0']
}
return results