-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterned_ogen.py
525 lines (479 loc) · 18.4 KB
/
patterned_ogen.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
from collections.abc import Iterable
from hdmf.utils import docval, popargs_to_dict, get_docval
from pynwb import register_class
from pynwb.core import DynamicTableRegion
from pynwb.device import Device
from pynwb.file import LabMetaData, TimeIntervals
from pynwb.ogen import OptogeneticStimulusSite
namespace = "ndx-patterned-ogen"
@register_class("SpatialLightModulator3D", namespace)
class SpatialLightModulator3D(Device):
"""
Spatial light modulator used in the experiment.
"""
__nwbfields__ = ("model", "spatial_resolution")
@docval(
{"name": "name", "type": str, "doc": "Name of SpatialLightModulator3D object."},
*get_docval(Device.__init__, "description", "manufacturer"),
{
"name": "model",
"type": str,
"doc": "The model specification of the spatial light modulator (e.g. 'NeuraLight 3D Ultra', from Bruker).",
},
{
"name": "spatial_resolution",
"type": Iterable,
"doc": "Resolution of spatial light modulator (in pixels), formatted as [width, height, depth].",
"default": None,
"shape": (3,),
},
)
def __init__(self, **kwargs):
keys_to_set = ("model", "spatial_resolution")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("SpatialLightModulator2D", namespace)
class SpatialLightModulator2D(Device):
"""
Spatial light modulator used in the experiment.
"""
__nwbfields__ = ("model", "spatial_resolution")
@docval(
{"name": "name", "type": str, "doc": "Name of SpatialLightModulator3D object. "},
*get_docval(Device.__init__, "description", "manufacturer"),
{
"name": "model",
"type": str,
"doc": "The model specification of the spatial light modulator (e.g. 'X15213 series', from Hamamatsu).",
},
{
"name": "spatial_resolution",
"type": Iterable,
"doc": "Resolution of spatial light modulator (in pixels), formatted as [width, height].",
"default": None,
"shape": (2,),
},
)
def __init__(self, **kwargs):
keys_to_set = ("model", "spatial_resolution")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("LightSource", namespace)
class LightSource(Device):
"""
Light source used in the experiment.
"""
__nwbfields__ = (
"model",
"stimulation_wavelength",
"peak_power",
"filter_descriptionpeak_pulse_energy",
"intensity",
"pulse_rate",
"exposure_time",
)
@docval(
{"name": "name", "type": str, "doc": "Name of LightSource object."},
*get_docval(Device.__init__, "description", "manufacturer"),
{"name": "model", "type": str, "doc": "Model of light source device."},
{
"name": "stimulation_wavelength",
"type": (int, float),
"doc": "Excitation wavelength of stimulation light (nanometers).",
"default": None,
},
{
"name": "peak_power",
"type": (int, float),
"doc": "Incident power of stimulation device (in Watts).",
"default": None,
},
{
"name": "filter_description",
"type": str,
"doc": (
"Filter used to obtain the excitation wavelength of stimulation light, e.g. 'Short pass at 1040 nm'."
),
"default": None,
},
{
"name": "peak_pulse_energy",
"type": (int, float),
"doc": "If device is pulsed light source, pulse energy (in Joules).",
"default": None,
},
{
"name": "intensity",
"type": (int, float),
"doc": "Intensity of the excitation in W/m^2, if known.",
"default": None,
},
{
"name": "pulse_rate",
"type": (int, float),
"doc": "If device is pulsed light source, pulse rate (in Hz) used for stimulation.",
"default": None,
},
{"name": "exposure_time", "type": (int, float), "doc": "Exposure time of the sample (in sec)", "default": None},
)
def __init__(self, **kwargs):
keys_to_set = (
"model",
"stimulation_wavelength",
"peak_power",
"filter_description",
"peak_pulse_energy",
"intensity",
"pulse_rate",
"exposure_time",
)
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("PatternedOptogeneticStimulusSite", namespace)
class PatternedOptogeneticStimulusSite(OptogeneticStimulusSite):
"""
Patterned optogenetic stimulus site.
"""
__nwbfields__ = ("effector", "spatial_light_modulator", "light_source")
@docval(
*get_docval(OptogeneticStimulusSite.__init__, "name", "description", "device", "location", "excitation_lambda"),
{
"name": "effector",
"type": str,
"doc": "Light-activated effector protein expressed by the targeted cell (eg. ChR2).",
"default": None,
},
{
"name": "spatial_light_modulator",
"type": (SpatialLightModulator3D, SpatialLightModulator2D),
"doc": "Spatial light modulator used to generate photostimulation pattern.",
},
{"name": "light_source", "type": LightSource, "doc": "Light source used to apply photostimulation."},
)
def __init__(self, **kwargs):
keys_to_set = ("effector", "spatial_light_modulator", "light_source")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@docval(
{
"name": "spatial_light_modulator",
"type": (SpatialLightModulator3D, SpatialLightModulator2D),
"doc": "Spatial light modulator used to generate photostimulation pattern. ",
}
)
def add_spatial_light_modulator(self, spatial_light_modulator):
"""
Add a spatial light modulator to the photostimulation method.
"""
if self.spatial_light_modulator is not None:
raise ValueError("SpatialLightModulator already exists in this PatternedOptogeneticStimulusSite container.")
else:
self.spatial_light_modulator = spatial_light_modulator
@docval({"name": "light_source", "type": LightSource, "doc": "Light source used to apply photostimulation."})
def add_light_source(self, light_source):
"""
Add a light source to the photostimulation method.
"""
if self.light_source is not None:
raise ValueError("LightSource already exists in this PatternedOptogeneticStimulusSite container.")
else:
self.light_source = light_source
@register_class("OptogeneticStimulusTarget", namespace)
class OptogeneticStimulusTarget(LabMetaData):
"""
Container to store the targated rois in a photostimulation experiment.
"""
__nwbfields__ = (
{"name": "segmented_rois", "child": True},
{"name": "targeted_rois", "child": True},
)
@docval(
*get_docval(LabMetaData.__init__, "name"),
{
"name": "segmented_rois",
"type": DynamicTableRegion,
"doc": (
"A table region referencing a PlaneSegmentation object storing segmented ROIs that receive"
" photostimulation."
),
},
{
"name": "targeted_rois",
"type": DynamicTableRegion,
"doc": "A table region referencing a PlaneSegmentation object storing targeted ROIs.",
},
)
def __init__(self, **kwargs):
keys_to_set = ("segmented_rois", "targeted_rois")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("OptogeneticStimulus2DPattern", namespace)
class OptogeneticStimulus2DPattern(LabMetaData):
"""
Container to store the information about a generic 2D stimulus pattern (spatial information).
"""
__nwbfields__ = ("description", "sweep_size", "sweep_mask")
@docval(
*get_docval(LabMetaData.__init__, "name"),
{
"name": "description",
"type": str,
"doc": (
"Description of the scanning or scanless method for shaping optogenetic light. Examples include"
" diffraction limited points, 3D shot, disks, etc."
),
},
{
"name": "sweep_size",
"type": (int, float, Iterable),
"doc": (
"Size of the scanning sweep pattern in micrometers. If a scalar is provided, the sweep pattern is"
" assumed to be a circle (for 2D patterns) with diameter 'sweep_size'."
" If 'sweep_size' is a two dimensional array, the the sweep pattern is assumed to be a"
" rectangle, with dimensions [width, height]."
),
"default": None,
},
{
"name": "sweep_mask",
"type": Iterable,
"doc": (
"Scanning sweep pattern designated using a mask of size [width, height] for 2D stimulation,"
" where for a given pixel a value of 1 indicates stimulation, and a"
" value of 0 indicates no stimulation."
),
"default": None,
},
)
def __init__(self, **kwargs):
keys_to_set = ("description", "sweep_size", "sweep_mask")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("OptogeneticStimulus3DPattern", namespace)
class OptogeneticStimulus3DPattern(LabMetaData):
"""
Container to store the information about a generic 3D stimulus pattern (spatial information).
"""
__nwbfields__ = ("description", "sweep_size", "sweep_mask")
@docval(
*get_docval(LabMetaData.__init__, "name"),
{
"name": "description",
"type": str,
"doc": (
"Description of the scanning or scanless method for shaping optogenetic light. Examples include"
" diffraction limited points, 3D shot, disks, etc."
),
},
{
"name": "sweep_size",
"type": (int, float, Iterable),
"doc": (
"Size of the scanning sweep pattern in micrometers. If a scalar is provided, the sweep pattern is"
" assumed to be a cylinder (for 3D patterns), with diameter 'sweep_size'."
" If 'sweep_size' is a three dimensional array, the the sweep pattern is assumed to be a"
" cuboid, with dimensions [width, height, depth]."
),
"default": None,
},
{
"name": "sweep_mask",
"type": Iterable,
"doc": (
"Scanning sweep pattern designated using a mask of size [width, height, depth] for 3D stimulation,"
" where for a given pixel a value of 1 indicates stimulation, and a"
" value of 0 indicates no stimulation."
),
"default": None,
},
)
def __init__(self, **kwargs):
keys_to_set = ("description", "sweep_size", "sweep_mask")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("TemporalFocusing", namespace)
class TemporalFocusing(LabMetaData):
"""
Container to store the parameters defining a temporal focusing beam-shaping
"""
__nwbfields__ = ("description", "lateral_point_spread_function", "axial_point_spread_function")
@docval(
*get_docval(LabMetaData.__init__, "name"),
{
"name": "description",
"type": str,
"doc": "Describe any additional details about the pattern.",
"default": None,
},
{
"name": "lateral_point_spread_function",
"type": str,
"doc": "Estimated lateral spatial profile or point spread function, expressed as mean [um] ± s.d [um].",
"default": None,
},
{
"name": "axial_point_spread_function",
"type": str,
"doc": "Estimated axial spatial profile or point spread function, expressed as mean [um] ± s.d [um]",
"default": None,
},
)
def __init__(self, **kwargs):
keys_to_set = ("description", "lateral_point_spread_function", "axial_point_spread_function")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("SpiralScanning", namespace)
class SpiralScanning(LabMetaData):
"""
Container to store the parameters defining a spiral scanning pattern.
"""
__nwbfields__ = ("description", "diameter", "height", "number_of_revolutions")
@docval(
*get_docval(LabMetaData.__init__, "name"),
{
"name": "description",
"type": str,
"doc": "Describe any additional details about the pattern.",
"default": None,
},
{
"name": "diameter",
"type": (int, float),
"doc": "Spiral diameter (in micrometers).",
"default": None,
},
{
"name": "height",
"type": (int, float),
"doc": "Spiral height of each sweep (in micrometers).",
"default": None,
},
{
"name": "number_of_revolutions",
"type": int,
"doc": "Number of turns within a spiral.",
"default": None,
},
)
def __init__(self, **kwargs):
keys_to_set = ("description", "diameter", "height", "number_of_revolutions")
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@register_class("PatternedOptogeneticStimulusTable", namespace)
class PatternedOptogeneticStimulusTable(TimeIntervals):
"""
Parameters corresponding to events of patterned optogenetic stimulation with indicated targeted rois.
"""
__fields__ = ()
__columns__ = (
{"name": "start_time", "description": "Start time of stimulation, in seconds", "required": True},
{"name": "stop_time", "description": "Stop time of stimulation, in seconds", "required": True},
{
"name": "power",
"description": "Power (in Watts) applied to each target during patterned photostimulation.",
"required": True,
},
{
"name": "frequency",
"description": "Frequency of stimulation if the stimulus delivered is pulsed (in Hz).",
"required": False,
},
{
"name": "pulse_width",
"description": "Pulse width of stimulation if the stimulus delivered is pulsed, in seconds/phase.",
"required": False,
},
{"name": "targets", "description": "Targeted rois for the stimulus onset.", "required": True},
{
"name": "stimulus_pattern",
"description": "Link to the stimulus pattern.",
"required": True,
},
{
"name": "stimulus_site",
"description": "Link to the stimulus site.",
"required": True,
},
)
@docval(
{
"name": "name",
"type": str,
"doc": "Name of this PatternedOptogeneticStimulusTable",
"default": "PatternedOptogeneticStimulusTable",
},
{
"name": "description",
"type": str,
"doc": "Description of what is in this PatternedOptogeneticStimulusTable",
"default": "stimulation parameters",
},
*get_docval(TimeIntervals.__init__, "id", "columns", "colnames"),
)
def __init__(self, **kwargs):
keys_to_set = ()
args_to_set = popargs_to_dict(keys_to_set, kwargs)
super().__init__(**kwargs)
for key, val in args_to_set.items():
setattr(self, key, val)
@docval(
{"name": "start_time", "doc": "Start time of stimulation, in seconds.", "type": float},
{"name": "stop_time", "doc": "Stop time of stimulation, in seconds.", "type": float},
{
"name": "power",
"doc": "Power (in Watts) applied to each target during patterned photostimulation.",
"type": (int, float, Iterable),
"default": 0.0,
},
{
"name": "frequency",
"doc": "Frequency of stimulation if the stimulus delivered is pulsed (in Hz).",
"type": (int, float, Iterable),
"default": 0.0,
},
{
"name": "pulse_width",
"doc": "Pulse width of stimulation if the stimulus delivered is pulsed, in seconds/phase.",
"type": (int, float, Iterable),
"default": 0.0,
},
{
"name": "targets",
"doc": "Targeted rois for the stimulus onset.",
"type": OptogeneticStimulusTarget,
},
{
"name": "stimulus_pattern",
"doc": "Link to the stimulus pattern.",
"type": (OptogeneticStimulus3DPattern,OptogeneticStimulus2DPattern, TemporalFocusing, SpiralScanning),
},
{
"name": "stimulus_site",
"doc": "Link to the stimulus site.",
"type": PatternedOptogeneticStimulusSite,
},
allow_extra=True,
)
def add_interval(self, **kwargs):
"""
Add a stimulation parameters for a specific stimulus onset.
"""
super(PatternedOptogeneticStimulusTable, self).add_interval(**kwargs)