-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstimulus.py
1648 lines (1346 loc) · 77.4 KB
/
stimulus.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, sys, gc, copy, time
import numpy as np
from scipy.interpolate import Rbf
from scipy.ndimage import gaussian_filter
from collections import defaultdict, ChainMap, namedtuple
from dentate.utils import get_module_logger, object, range, str, Struct, gauss2d, gaussian, viewitems, mpi_op_set_union
from dentate.stgen import get_inhom_poisson_spike_times_by_thinning
from neuroh5.io import read_cell_attributes, append_cell_attributes, NeuroH5CellAttrGen, scatter_read_cell_attribute_selection
from mpi4py import MPI
import h5py
## This logger will inherit its setting from its root logger, dentate,
## which is created in module env
logger = get_module_logger(__name__)
PhaseModConfig = namedtuple('PhaseModConfig',
['phase_range',
'phase_pref',
'phase_offset',
'mod_depth',
'frequency'])
class InputSelectivityConfig(object):
def __init__(self, stimulus_config, local_random):
"""
We assume that "stimulus input cells" from MEC and LEC have grid spacing and spatial field widths that are
topographically organized septo-temporally. Cells are assigned to one of ten discrete modules with distinct
grid spacing and field width probabilistically according to septo-temporal position. The grid spacing across
modules increases exponentially from 40 cm to 8 m. We then assume that "proxy input cells" from GC, MC, and
CA3c neurons have place fields that result from sampling input from multiple discrete modules, and therefore
have field widths that vary continuously with septo-temporal position, rather than clustering into discrete
modules. Features are imposed on these "proxy input cells" during microcircuit clamp or network clamp
simulations.
:param stimulus_config: dict
:param local_random: :class:'np.random.RandomState
"""
self.num_modules = stimulus_config['Number Modules']
self.module_ids = list(range(stimulus_config['Number Modules']))
self.module_probability_width = stimulus_config['Selectivity Module Parameters']['width']
self.module_probability_displacement = stimulus_config['Selectivity Module Parameters']['displacement']
self.module_probability_offsets = \
np.linspace(-self.module_probability_displacement, 1. + self.module_probability_displacement,
self.num_modules)
self.get_module_probability = \
np.vectorize(lambda distance, offset:
np.exp(
-(((distance - offset) / (self.module_probability_width / 3. / np.sqrt(2.)))) ** 2.),
excluded=['offset'])
self.get_grid_module_spacing = \
lambda distance: stimulus_config['Grid Spacing Parameters']['offset'] + \
stimulus_config['Grid Spacing Parameters']['slope'] * \
(np.exp(distance / stimulus_config['Grid Spacing Parameters']['tau']) - 1.)
self.grid_module_spacing = \
[self.get_grid_module_spacing(distance) for distance in np.linspace(0., 1., self.num_modules)]
self.grid_spacing_sigma = stimulus_config['Grid Spacing Variance'] / 6.
self.grid_field_width_concentration_factor = stimulus_config['Field Width Concentration Factor']['grid']
self.grid_module_orientation = [local_random.uniform(0., np.pi / 3.) for i in range(self.num_modules)]
self.grid_orientation_sigma = np.deg2rad(stimulus_config['Grid Orientation Variance'] / 6.)
self.place_field_width_concentration_factor = stimulus_config['Field Width Concentration Factor']['place']
self.place_module_field_widths = np.multiply(self.grid_module_spacing,
self.place_field_width_concentration_factor)
self.place_module_field_width_sigma = stimulus_config['Modular Place Field Width Variance'] / 6
self.non_modular_place_field_width_sigma = stimulus_config['Non-modular Place Field Width Variance'] / 6.
def get_module_probabilities(self, distance):
p_modules = []
for offset in self.module_probability_offsets:
p_modules.append(self.get_module_probability(distance, offset))
p_modules = np.array(p_modules, dtype=np.float32)
p_sum = np.sum(p_modules, axis=0)
if p_sum == 0.:
raise RuntimeError('SelectivityModuleConfig: get_module_probabilities: problem computing selectivity module'
'identity probabilities for normalized distance: %.4f' % distance)
p_density = np.divide(p_modules, p_sum)
return p_density
def plot_module_probabilities(self, **kwargs):
import matplotlib.pyplot as plt
from dentate.plot import clean_axes, default_fig_options, save_figure
fig_options = copy.copy(default_fig_options)
fig_options.update(kwargs)
distances = np.linspace(0., 1., 1000)
p_modules = [self.get_module_probability(distances, offset) for offset in self.module_probability_offsets]
p_modules = np.array(p_modules)
p_sum = np.sum(p_modules, axis=0)
p_density = np.divide(p_modules, p_sum)
fig, axes = plt.subplots(1, 2, figsize=(10., 4.8))
for i in range(len(p_modules)):
axes[0].plot(distances, p_density[i, :], label='Module %i' % i)
axes[0].set_title('Selectivity module assignment probabilities', fontsize=fig_options.fontSize)
axes[0].set_xlabel('Normalized cell position', fontsize=fig_options.fontSize)
axes[0].set_ylabel('Probability', fontsize=fig_options.fontSize)
expected_field_widths = np.matmul(self.place_module_field_widths, p_density)
axes[1].plot(distances, expected_field_widths, c='k')
axes[1].set_title('Expected place field width', fontsize=fig_options.fontSize)
axes[1].set_xlabel('Normalized cell position', fontsize=fig_options.fontSize)
axes[1].set_ylabel('Place field width (cm)', fontsize=fig_options.fontSize)
clean_axes(axes)
fig.tight_layout()
if fig_options.saveFig is not None:
save_figure('%s selectivity module probabilities' % str(fig_options.saveFig), **fig_options())
if fig_options.showFig:
fig.show()
def get_expected_place_field_width(self, p_modules):
"""
While feedforward inputs to the DG (MPP and LPP) exhibit modular spatial selectivity, the populations in the
hippocampus receive convergent input from multiple discrete modules. Their place fields are, therefore,
"non-modular", but their widths will vary with position along the septo-temporal axis of the hippocampus.
This method computes the expected width of a place field as a weighted mean of the input field widths. The
provided probabilities (p_modules) should be pre-computed with get_module_probabilities(distance).
:param p_modules: array
:return: float
"""
return np.average(self.place_module_field_widths, weights=p_modules)
class GridInputCellConfig(object):
def __init__(self, selectivity_type=None, arena=None, selectivity_config=None,
peak_rate=None, distance=None, local_random=None, selectivity_attr_dict=None,
phase_mod_config=None, noise_gen_dict=None, comm=None):
"""
:param selectivity_type: int
:param arena: namedtuple
:param selectivity_config: :class:'InputSelectivityConfig'
:param peak_rate: float
:param distance: float; u arc distance normalized to reference layer
:param local_random: :class:'np.random.RandomState'
:param selectivity_attr_dict: dict
:param phase_mod_config: dict with phase modulation configuration
:param noise_gen_dict: dictionary with per-module noise generator objects
:param comm: MPI communicator
"""
self.phase_mod_function = None
if phase_mod_config is not None:
phase_range = phase_mod_config.phase_range
phase_pref = phase_mod_config.phase_pref
phase_offset = phase_mod_config.phase_offset
mod_depth = phase_mod_config.mod_depth
freq = phase_mod_config.frequency
self.phase_mod_function = lambda x, y, velocity, field_width, initial_phase=0: spatial2d_phase_mod(x, y, velocity, field_width, phase_range, phase_pref, phase_offset+initial_phase, mod_depth, freq)
if selectivity_attr_dict is not None:
self.init_from_attr_dict(selectivity_attr_dict)
elif any([arg is None for arg in [selectivity_type, selectivity_config, peak_rate, distance]]):
raise RuntimeError('GridInputCellConfig: missing argument(s) required for object construction')
else:
if local_random is None:
local_random = np.random.RandomState()
self.selectivity_type = selectivity_type
self.peak_rate = peak_rate
p_modules = selectivity_config.get_module_probabilities(distance)
self.module_id = local_random.choice(selectivity_config.module_ids, p=p_modules)
self.grid_spacing = selectivity_config.grid_module_spacing[self.module_id]
if arena is None:
arena_x_bounds, arena_y_bounds = (-self.grid_spacing, self.grid_spacing)
else:
arena_x_bounds, arena_y_bounds = get_2D_arena_bounds(arena, margin=self.grid_spacing / 2.)
if selectivity_config.grid_spacing_sigma > 0.:
delta_grid_spacing_factor = local_random.normal(0., selectivity_config.grid_spacing_sigma)
self.grid_spacing += self.grid_spacing * delta_grid_spacing_factor
self.grid_orientation = selectivity_config.grid_module_orientation[self.module_id]
if selectivity_config.grid_orientation_sigma > 0.:
delta_grid_orientation = local_random.normal(0., selectivity_config.grid_orientation_sigma)
self.grid_orientation += delta_grid_orientation
if noise_gen_dict is not None:
# Use allreduce to determine the set of modules on each rank and determine which noise_gens to call
all_module_ids = comm.allreduce(set([self.module_id]), op=mpi_op_set_union)
for this_module_id in all_module_ids:
this_noise_gen = noise_gen_dict[this_module_id]
global_num_fields = this_noise_gen.sync(1 if this_module_id == self.module_id else 0)
for i in range(global_num_fields):
if this_module_id == self.module_id and i < 1:
p = this_noise_gen.next()
self.x0, self.y0 = p[0]
this_noise_gen.add(p, lambda p, g: get_grid_rate_map(p[0], p[1], self.grid_spacing,
self.grid_orientation, g[0], g[1],
a=selectivity_config.grid_field_width_concentration_factor))
else:
this_noise_gen.add(np.empty( shape=(0, 0), dtype=np.float32 ), None)
else:
self.x0 = local_random.uniform(*arena_x_bounds)
self.y0 = local_random.uniform(*arena_y_bounds)
self.grid_field_width_concentration_factor = selectivity_config.grid_field_width_concentration_factor
def init_from_attr_dict(self, selectivity_attr_dict):
self.selectivity_type = selectivity_attr_dict['Selectivity Type'][0]
self.peak_rate = selectivity_attr_dict['Peak Rate'][0]
self.module_id = selectivity_attr_dict['Module ID'][0]
self.grid_spacing = selectivity_attr_dict['Grid Spacing'][0]
self.grid_orientation = selectivity_attr_dict['Grid Orientation'][0]
self.x0 = selectivity_attr_dict['X Offset'][0]
self.y0 = selectivity_attr_dict['Y Offset'][0]
self.grid_field_width_concentration_factor = selectivity_attr_dict['Field Width Concentration Factor'][0]
def gather_attributes(self):
"""
Select a subset of selectivity attributes to gather across a population. Cell attributes have one value per
cell, component attributes have variable length per cell. A count is returned for the length of component
attribute lists.
:return: dict(val), count, None
"""
gathered_cell_attr_dict = dict()
gathered_cell_attr_dict['Module ID'] = self.module_id
gathered_cell_attr_dict['Grid Spacing'] = self.grid_spacing
gathered_cell_attr_dict['Grid Orientation'] = self.grid_orientation
return gathered_cell_attr_dict, 0, None
def get_selectivity_attr_dict(self):
return {'Selectivity Type': np.array([self.selectivity_type], dtype=np.uint8),
'Peak Rate': np.array([self.peak_rate], dtype=np.float32),
'Module ID': np.array([self.module_id], dtype=np.uint8),
'Grid Spacing': np.array([self.grid_spacing], dtype=np.float32),
'Grid Orientation': np.array([self.grid_orientation], dtype=np.float32),
'X Offset': np.array([self.x0], dtype=np.float32),
'Y Offset': np.array([self.y0], dtype=np.float32),
'Field Width Concentration Factor':
np.array([self.grid_field_width_concentration_factor], dtype=np.float32)
}
def get_rate_map(self, x, y, velocity=1.0, scale=1.0, initial_phase=0.):
"""
:param x: array
:param y: array
:return: array
"""
rate_array = np.multiply(get_grid_rate_map(self.x0, self.y0, scale * self.grid_spacing, self.grid_orientation, x, y,
a=self.grid_field_width_concentration_factor), self.peak_rate)
mean_rate = np.mean(rate_array)
if self.phase_mod_function is not None:
mx = x - self.x0
my = y - self.y0
rate_array *= self.phase_mod_function(mx, my, velocity, self.grid_spacing, initial_phase=initial_phase)
mean_rate_mod = np.mean(rate_array)
if mean_rate_mod > 0.:
rate_array *= mean_rate / mean_rate_mod
return rate_array
class PlaceInputCellConfig(object):
def __init__(self, selectivity_type=None, arena=None, normalize_scale=True, selectivity_config=None,
peak_rate=None, distance=None, modular=None, num_place_field_probabilities=None, field_width=None,
local_random=None, selectivity_attr_dict=None, phase_mod_config=None, noise_gen_dict=None, comm=None):
"""
:param selectivity_type: int
:param arena: namedtuple
:param normalize_scale: bool; whether to interpret the scale of the num_place_field_probabilities distribution
as normalized to the scale of the mean place field width
:param selectivity_config: :class:'SelectivityModuleConfig'
:param peak_rate: float
:param distance: float; u arc distance normalized to reference layer
:param modular: bool
:param num_place_field_probabilities: dict
:param field_width: float; option to enforce field_width rather than choose from distance-dependent distribution
:param local_random: :class:'np.random.RandomState'
:param selectivity_attr_dict: dict
:param phase_mod_config: dict with phase modulation configuration
:param noise_gen_dict: dictionary with per-module noise generator objects
:param comm: MPI communicator
"""
self.phase_mod_function = None
if phase_mod_config is not None:
phase_range = phase_mod_config.phase_range
phase_pref = phase_mod_config.phase_pref
phase_offset = phase_mod_config.phase_offset
mod_depth = phase_mod_config.mod_depth
freq = phase_mod_config.frequency
self.phase_mod_function = lambda x, y, velocity, field_width, initial_phase=0.: spatial2d_phase_mod(x, y, velocity, field_width, phase_range, phase_pref, phase_offset+initial_phase, mod_depth, freq)
if selectivity_attr_dict is not None:
self.init_from_attr_dict(selectivity_attr_dict)
elif any([arg is None for arg in [selectivity_type, selectivity_config, peak_rate, arena,
num_place_field_probabilities]]):
raise RuntimeError('PlaceInputCellConfig: missing argument(s) required for object construction')
else:
self.rate_map_residual_sum = None
if local_random is None:
local_random = np.random.RandomState()
self.selectivity_type = selectivity_type
self.peak_rate = peak_rate
if field_width is not None:
self.mean_field_width = field_width
self.module_id = -1
self.modular = False
elif distance is None or modular is None:
raise RuntimeError('PlaceInputCellConfig: missing argument(s) required for object construction')
else:
p_modules = selectivity_config.get_module_probabilities(distance)
if modular:
self.module_id = local_random.choice(selectivity_config.module_ids, p=p_modules)
self.mean_field_width = selectivity_config.place_module_field_widths[self.module_id]
else:
self.module_id = -1
self.mean_field_width = selectivity_config.get_expected_place_field_width(p_modules)
self.num_fields = get_num_place_fields(num_place_field_probabilities, self.mean_field_width, arena=arena,
normalize_scale=normalize_scale, local_random=local_random)
arena_x_bounds, arena_y_bounds = get_2D_arena_bounds(arena=arena, margin=self.mean_field_width / 2.)
self.field_width = []
self.x0 = []
self.y0 = []
for i in range(self.num_fields):
this_field_width = self.mean_field_width
if modular:
if selectivity_config.place_module_field_width_sigma > 0.:
delta_field_width_factor = \
local_random.normal(0., selectivity_config.place_module_field_width_sigma)
this_field_width += self.mean_field_width * delta_field_width_factor
else:
if selectivity_config.non_modular_place_field_width_sigma > 0.:
delta_field_width_factor = \
local_random.normal(0., selectivity_config.non_modular_place_field_width_sigma)
this_field_width += self.mean_field_width * delta_field_width_factor
self.field_width.append(this_field_width)
if noise_gen_dict is not None:
# Use allreduce to determine the set of modules on each rank and determine which noise_gens to call
all_module_ids = [-1]
if modular:
all_module_ids = comm.allreduce(set([self.module_id]), op=mpi_op_set_union)
for this_module_id in all_module_ids:
this_noise_gen = noise_gen_dict[this_module_id]
# Use allreduce to determine the total number of fields required on all ranks
global_num_fields = this_noise_gen.sync(self.num_fields if this_module_id == self.module_id else 0)
for i in range(global_num_fields):
if this_module_id == self.module_id and i < self.num_fields:
p = this_noise_gen.next()
this_x0, this_y0 = p[0]
self.x0.append(this_x0)
self.y0.append(this_y0)
this_noise_gen.add(p, lambda p, g, width: get_place_rate_map(p[0], p[1], width, g[0], g[1]),
energy_kwargs=({'width': self.field_width[i]},))
else:
this_noise_gen.add(np.empty( shape=(0, 0), dtype=np.float32 ), None)
else:
for i in range(self.num_fields):
this_x0 = local_random.uniform(*arena_x_bounds)
this_y0 = local_random.uniform(*arena_y_bounds)
self.x0.append(this_x0)
self.y0.append(this_y0)
def init_from_attr_dict(self, selectivity_attr_dict):
self.rate_map_residual_sum = None
if 'Rate Map Residual Sum' in selectivity_attr_dict:
self.rate_map_residual_sum = selectivity_attr_dict['Rate Map Residual Sum'][0]
self.selectivity_type = selectivity_attr_dict['Selectivity Type'][0]
self.peak_rate = selectivity_attr_dict['Peak Rate'][0]
self.module_id = selectivity_attr_dict.get('Module ID', [0])[0]
self.num_fields = selectivity_attr_dict['Num Fields'][0]
self.field_width = selectivity_attr_dict['Field Width']
self.x0 = selectivity_attr_dict['X Offset']
self.y0 = selectivity_attr_dict['Y Offset']
def gather_attributes(self):
"""
Select a subset of selectivity attributes to gather across a population. Cell attributes have one value per
cell, component attributes have variable length per cell. A count is returned for the length of component
attribute lists.
:return: dict(val), count, dict(list)
"""
gathered_cell_attr_dict = dict()
gathered_comp_attr_dict = dict()
gathered_cell_attr_dict['Module ID'] = self.module_id
gathered_cell_attr_dict['Num Fields'] = self.num_fields
if self.rate_map_residual_sum is not None:
gathered_cell_attr_dict['Rate Map Residual Sum'] = self.rate_map_residual_sum
count = len(self.field_width)
gathered_comp_attr_dict['Field Width'] = self.field_width
return gathered_cell_attr_dict, count, gathered_comp_attr_dict
def get_selectivity_attr_dict(self):
return {'Selectivity Type': np.array([self.selectivity_type], dtype=np.uint8),
'Peak Rate': np.array([self.peak_rate], dtype=np.float32),
'Module ID': np.array([self.module_id], dtype=np.int8),
'Num Fields': np.array([self.num_fields], dtype=np.uint8),
'Field Width': np.asarray(self.field_width, dtype=np.float32),
'X Offset': np.asarray(self.x0, dtype=np.float32),
'Y Offset': np.asarray(self.y0, dtype=np.float32)
}
def get_rate_map(self, x, y, velocity=None, scale=1.0, initial_phase=0.0):
"""
:param x: array
:param y: array
:return: array
"""
if (velocity is None) and (self.phase_mod_function is not None):
raise RuntimeError("PlaceInputCellConfig.get_rate_map: when phase modulation is provided, velocity must be provided to get_rate_map")
rate_array = np.zeros_like(x, dtype=np.float32)
for i in range(self.num_fields):
rate_array = np.maximum(rate_array, get_place_rate_map(self.x0[i], self.y0[i], self.field_width[i] * scale, x, y))
rate_array *= self.peak_rate
rate_array = gaussian_filter(rate_array, sigma=1)
mean_rate = np.mean(rate_array)
if (self.num_fields > 0) and (self.phase_mod_function is not None):
mx = x - self.x0[0]
my = y - self.y0[0]
rate_array *= self.phase_mod_function(mx, my, velocity, self.field_width[0], initial_phase=initial_phase)
mean_rate_mod = np.mean(rate_array)
if mean_rate_mod > 0.:
rate_array *= mean_rate / mean_rate_mod
return rate_array
class ConstantInputCellConfig(object):
def __init__(self, selectivity_type=None, arena=None, selectivity_config=None,
peak_rate=None, local_random=None, selectivity_attr_dict=None, phase_mod_config=None):
"""
:param selectivity_type: int
:param arena: namedtuple
:param selectivity_config: :class:'SelectivityModuleConfig'
:param peak_rate: float
:param local_random: :class:'np.random.RandomState'
:param selectivity_attr_dict: dict
"""
self.phase_mod_function = None
if phase_mod_config is not None:
phase_range = phase_mod_config.phase_range
phase_pref = phase_mod_config.phase_pref
phase_offset = phase_mod_config.phase_offset
mod_depth = phase_mod_config.mod_depth
freq = phase_mod_config.frequency
self.phase_mod_function = lambda t, initial_phase=0.: stationary_phase_mod(t, phase_range, phase_pref, phase_offset+initial_phase, mod_depth, freq)
if selectivity_attr_dict is not None:
self.init_from_attr_dict(selectivity_attr_dict)
elif any([arg is None for arg in [selectivity_type, selectivity_config, peak_rate, arena]]):
raise RuntimeError('ConstantInputCellConfig: missing argument(s) required for object construction')
else:
if local_random is None:
local_random = np.random.RandomState()
self.selectivity_type = selectivity_type
self.peak_rate = peak_rate
def init_from_attr_dict(self, selectivity_attr_dict):
self.selectivity_type = selectivity_attr_dict['Selectivity Type'][0]
self.peak_rate = selectivity_attr_dict['Peak Rate'][0]
def get_selectivity_attr_dict(self):
return {'Selectivity Type': np.array([self.selectivity_type], dtype=np.uint8),
'Peak Rate': np.array([self.peak_rate], dtype=np.float32),
}
def get_rate_map(self, x, y, velocity=None, initial_phase=0.0):
"""
:param x: array
:param y: array
:return: array
"""
if (velocity is None) and (self.phase_mod_function is not None):
raise RuntimeError("ConstantInputCellConfig.get_rate_map: when phase config is provided, get_rate_map must be provided with velocity")
rate_array = np.ones_like(x, dtype=np.float32) * self.peak_rate
mean_rate = np.mean(rate_array)
if (velocity is not None) and (self.phase_mod_function is not None):
d = np.insert(np.cumsum(np.sqrt((np.diff(x) ** 2. + np.diff(y) ** 2.))), 0, 0.)
t = d/velocity
rate_array *= self.phase_mod_function(t, initial_phase=initial_phase)
mean_rate_mod = np.mean(rate_array)
if mean_rate_mod > 0.:
rate_array *= mean_rate / mean_rate_mod
return rate_array
def get_place_rate_map(x0, y0, width, x, y):
"""
:param x0: float
:param y0: float
:param width: float
:param x: array
:param y: array
:return: array
"""
fw = 2. * np.sqrt(2. * np.log(100.))
return gauss2d(x=x, y=y, mx=x0, my=y0, sx=width / fw, sy=width / fw)
def get_grid_rate_map(x0, y0, spacing, orientation, x, y, a=0.7):
"""
:param x0: float
:param y0: float
:param spacing: float
:param orientation: float
:param x: array
:param y: array
:param a: concentrates field width relative to grid spacing
:return: array
"""
b = -1.5
theta_k = [np.deg2rad(-30.), np.deg2rad(30.), np.deg2rad(90.)]
inner_sum = np.zeros_like(x)
for theta in theta_k:
inner_sum += np.cos(((4. * np.pi) / (np.sqrt(3.) * spacing)) *
(np.cos(theta - orientation) * (x - x0) +
np.sin(theta - orientation) * (y - y0)))
transfer = lambda z: np.exp(a * (z - b)) - 1.
max_rate = transfer(3.)
rate_map = transfer(inner_sum) / max_rate
return rate_map
def get_input_cell_config(selectivity_type, selectivity_type_names, population=None, stimulus_config=None,
arena=None, selectivity_config=None, distance=None, local_random=None,
selectivity_attr_dict=None, phase_mod_config=None, noise_gen_dict=None, comm=None):
"""
:param selectivity_type: int
:param selectivity_type_names: dict: {int: str}
:param population: str
:param stimulus_config: dict
:param arena: namedtuple
:param selectivity_config: :class:'InputSelectivityConfig'
:param distance: float; u arc distance normalized to reference layer
:param local_random: :class:'np.random.RandomState'
:param selectivity_attr_dict: dict
:param phase_mod_config: dict; oscillatory phase modulation configuration
:return: instance of one of various InputCell classes
"""
if selectivity_type not in selectivity_type_names:
raise RuntimeError('get_input_cell_config: enumerated selectivity type: %i not recognized' % selectivity_type)
selectivity_type_name = selectivity_type_names[selectivity_type]
if selectivity_attr_dict is not None:
if selectivity_type_name == 'grid':
input_cell_config = GridInputCellConfig(selectivity_attr_dict=selectivity_attr_dict,
phase_mod_config=phase_mod_config)
elif selectivity_type_name == 'place':
input_cell_config = PlaceInputCellConfig(selectivity_attr_dict=selectivity_attr_dict,
phase_mod_config=phase_mod_config)
elif selectivity_type_name == 'constant':
input_cell_config = ConstantInputCellConfig(selectivity_attr_dict=selectivity_attr_dict,
phase_mod_config=phase_mod_config)
else:
RuntimeError('get_input_cell_config: selectivity type %s is not supported' % selectivity_type_name)
elif any([arg is None for arg in [population, stimulus_config, arena]]):
raise RuntimeError(f'get_input_cell_config: missing argument(s) required to construct {selectivity_type_name} cell config object: population: {population} arena: {arena} stimulus_config: {stimulus_config}')
else:
if population not in stimulus_config['Peak Rate'] or \
selectivity_type not in stimulus_config['Peak Rate'][population]:
raise RuntimeError('get_input_cell_config: peak rate not specified for population: %s, selectivity type: '
'%s' % (population, selectivity_type_name))
peak_rate = stimulus_config['Peak Rate'][population][selectivity_type]
if selectivity_type_name in ['grid', 'place']:
if selectivity_config is None:
raise RuntimeError('get_input_cell_config: missing required argument: selectivity_config')
if distance is None:
raise RuntimeError('get_input_cell_config: missing required argument: distance')
if local_random is None:
local_random = np.random.RandomState()
logger.warning('get_input_cell_config: local_random argument not provided - randomness will not be '
'reproducible')
if selectivity_type_name == 'grid':
input_cell_config = \
GridInputCellConfig(selectivity_type=selectivity_type, arena=arena,
selectivity_config=selectivity_config, peak_rate=peak_rate, distance=distance,
local_random=local_random, phase_mod_config=phase_mod_config,
noise_gen_dict=noise_gen_dict, comm=comm)
elif selectivity_type_name == 'place':
if population in stimulus_config['Non-modular Place Selectivity Populations']:
modular = False
else:
modular = True
if population not in stimulus_config['Num Place Field Probabilities']:
raise RuntimeError('get_input_cell_config: probabilities for number of place fields not specified for '
f'population: {population}')
num_place_field_probabilities = stimulus_config['Num Place Field Probabilities'][population]
input_cell_config = \
PlaceInputCellConfig(selectivity_type=selectivity_type, arena=arena,
selectivity_config=selectivity_config, peak_rate=peak_rate, distance=distance,
modular=modular, num_place_field_probabilities=num_place_field_probabilities,
local_random=local_random, phase_mod_config=phase_mod_config,
noise_gen_dict=noise_gen_dict, comm=comm)
elif selectivity_type_name == 'constant':
input_cell_config = ConstantInputCellConfig(selectivity_type=selectivity_type, arena=arena,
selectivity_config=selectivity_config, peak_rate=peak_rate,
phase_mod_config=phase_mod_config)
else:
RuntimeError(f'get_input_cell_config: selectivity type: {selectivity_type_name} not implemented')
return input_cell_config
def get_equilibration(env):
if 'Equilibration Duration' in env.stimulus_config and env.stimulus_config['Equilibration Duration'] > 0.:
equilibrate_filter_type = env.stimulus_config.get('Equlibration Filter', 'hann')
equilibrate_len = int(env.stimulus_config['Equilibration Duration'] /
env.stimulus_config['Temporal Resolution'])
from scipy.signal import hann, tukey
if equilibrate_filter_type == 'hann':
equilibrate_filter = hann(2 * equilibrate_len)[:equilibrate_len]
elif equilibrate_filter_type == 'tukey':
equilibrate_filter = tukey(2 * equilibrate_len)[:equilibrate_len]
else:
raise RuntimeError(f"Unknown equilibration filter type {equilibrate_filter_type}")
equilibrate = (equilibrate_filter, equilibrate_len)
else:
equilibrate = None
return equilibrate
def choose_input_selectivity_type(p, local_random):
"""
:param p: dict: {str: float}
:param local_random: :class:'np.random.RandomState'
:return: str
"""
if len(p) == 1:
return list(p.keys())[0]
return local_random.choice(list(p.keys()), p=list(p.values()))
def get_active_cell_matrix(pop_activity, threshold=2.):
active_cell_matrix = np.zeros_like(pop_activity, dtype=np.float32)
active_indexes = np.where(pop_activity >= threshold)
active_cell_matrix[active_indexes] = 1.
return active_cell_matrix
def get_num_place_fields(num_place_field_probabilities, field_width, arena=None, normalize_scale=True,
local_random=None):
"""
Probability distributions for the number of place fields per cell are defined relative to the area of a standard
arena size with dimension length = 2. * field_width. Given an arena with arbitrary dimensions, the number of place
fields to distribute within the area of the arena is equivalent to a series of biased dice rolls.
:param num_place_field_probabilities: dict: {int: float}
:param field_width: float
:param arena: namedtuple
:param normalize_scale: bool
:param local_random: :class:'np.random.RandomState'
:return: int
"""
num_fields_array, p_num_fields = normalize_num_place_field_probabilities(num_place_field_probabilities,
return_item_arrays=True)
if not normalize_scale:
scale = 1.
else:
calibration_x_bounds = calibration_y_bounds = (-field_width, field_width)
calibration_area = abs(calibration_x_bounds[1] - calibration_x_bounds[0]) * \
abs(calibration_y_bounds[1] - calibration_y_bounds[0])
arena_x_bounds, arena_y_bounds = get_2D_arena_bounds(arena, margin=field_width / 2.)
arena_area = abs(arena_x_bounds[1] - arena_x_bounds[0]) * abs(arena_y_bounds[1] - arena_y_bounds[0])
scale = arena_area / calibration_area
num_fields = 0
while scale > 0.:
if scale >= 1.:
scale -= 1.
else:
num_fields_array, p_num_fields = \
rescale_non_zero_num_place_field_probabilities(num_place_field_probabilities, scale,
return_item_arrays=True)
scale = 0.
num_fields += local_random.choice(num_fields_array, p=p_num_fields)
return num_fields
def normalize_num_place_field_probabilities(num_place_field_probabilities, return_item_arrays=False):
"""
Normalizes provided probability distribution for the number of place fields per cell to sum to one. Can return
either a dictionary or a tuple of array.
:param num_place_field_probabilities: dict: {int: float}
:param return_item_arrays: bool
:return: dict or tuple of array
"""
num_fields_array = np.arange(len(num_place_field_probabilities))
p_num_fields = np.array([num_place_field_probabilities[i] for i in num_fields_array])
p_num_fields_sum = np.sum(p_num_fields)
if p_num_fields_sum <= 0.:
raise RuntimeError('normalize_num_place_field_probabilities: invalid num_place_field_probabilities')
p_num_fields /= p_num_fields_sum
if return_item_arrays:
return num_fields_array, p_num_fields
return {i: p_num_fields[i] for i in range(len(p_num_fields))}
def rescale_non_zero_num_place_field_probabilities(num_place_field_probabilities, scale, return_item_arrays=False):
"""
Modify a probability distribution for the number of place fields per cell by scaling the probabilities of one or
greater fields, and compensating the probability of zero fields. Normalizes the modified probability distribution to
sum to one. Can return either a dictionary or a tuple of array.
:param num_place_field_probabilities: dict: {int: float}
:param scale: float
:param return_item_arrays: bool
:return: dict or tuple of array
"""
num_fields_array, p_num_fields = normalize_num_place_field_probabilities(num_place_field_probabilities,
return_item_arrays=True)
if scale <= 0.:
raise RuntimeError('rescale_non_zero_num_place_field_probabilities: specified scale is invalid: %.3f' % scale)
p_num_fields *= scale
non_zero_p_num_fields_sum = np.sum(p_num_fields[1:])
if non_zero_p_num_fields_sum > 1.:
raise RuntimeError('rescale_non_zero_place_field_num_probabilities: the provided scale factor would generate '
'an invalid distribution of place_field_num_probabilities')
p_num_fields[0] = 1. - non_zero_p_num_fields_sum
if return_item_arrays:
return num_fields_array, p_num_fields
return {i: p_num_fields[i] for i in range(len(p_num_fields))}
def calibrate_num_place_field_probabilities(num_place_field_probabilities, field_width, peak_rate=20., threshold=2.,
target_fraction_active=None, pop_size=10000, bins=100, selectivity_type=1,
arena=None, normalize_scale=True, selectivity_config=None, random_seed=0,
plot=False):
"""
Given a probability distribution of the number of place fields per cell and a 2D arena, this method can either
report the fraction of the population that will be active per unit area, or re-scale the probability distribution to
achieve a target fraction active. The argument "normalize_scale" interprets the probability distribution as being
defined for an intrinsic area that scales with field_width. When "normalize_scale" is set to False, this method will
instead interpret the probability distribution as being defined over the area of the provided arena, buffered by
margins that scale with field_width. If a target_fraction_active is provided, the distribution is modified by
scaling the probabilities of one or greater fields, and compensating the probability of zero fields. Resulting
modified probability distribution sums to one. Returns a dictionary.
:param num_place_field_probabilities: dict: {int: float}
:param field_width: float
:param peak_rate: float
:param threshold: float
:param target_fraction_active: float
:param pop_size: int
:param bins: int
:param selectivity_type: int
:param arena: namedtuple
:param normalize_scale: bool
:param selectivity_config: :class:'InputSelectivityConfig'
:param random_seed: int
:param plot: bool
:return: dict: {int: float}
"""
if arena is None:
inner_arena_x_bounds, inner_arena_y_bounds = (-field_width / 2., field_width / 2.)
outer_arena_x_bounds, outer_arena_y_bounds = (-field_width, field_width)
else:
inner_arena_x_bounds, inner_arena_y_bounds = get_2D_arena_bounds(arena)
outer_arena_x_bounds, outer_arena_y_bounds = get_2D_arena_bounds(arena, margin=field_width / 2.)
x = np.linspace(inner_arena_x_bounds[0], inner_arena_x_bounds[1], bins)
y = np.linspace(inner_arena_y_bounds[0], inner_arena_y_bounds[1], bins)
x_mesh, y_mesh = np.meshgrid(x, y, indexing='ij')
arena_area = abs(outer_arena_x_bounds[1] - outer_arena_x_bounds[0]) * \
abs(outer_arena_y_bounds[1] - outer_arena_y_bounds[0])
local_random = np.random.RandomState()
iteration_label = ' before:' if target_fraction_active is not None else ''
for iteration in range(2):
local_random.seed(random_seed)
population_num_fields = []
pop_activity = np.zeros((pop_size, len(x), len(y)))
for i in range(pop_size):
input_cell_config = \
PlaceInputCellConfig(selectivity_type=selectivity_type, arena=arena, normalize_scale=normalize_scale,
selectivity_config=selectivity_config, peak_rate=peak_rate,
num_place_field_probabilities=num_place_field_probabilities,
field_width=field_width, local_random=local_random)
num_fields = input_cell_config.num_fields
population_num_fields.append(num_fields)
if num_fields > 0:
rate_map = input_cell_config.get_rate_map(x_mesh, y_mesh)
pop_activity[i, :, :] = rate_map
active_cell_matrix = get_active_cell_matrix(pop_activity, threshold)
fraction_active_array = np.mean(active_cell_matrix, axis=0)
fraction_active_mean = np.mean(fraction_active_array)
fraction_active_variance = np.var(fraction_active_array)
field_density = np.mean(population_num_fields) / arena_area
print('calibrate_num_place_field_probabilities:%s field_width: %.2f, fraction active: mean: %.4f, var: %.4f; '
'field_density: %.4E' % (iteration_label, field_width, fraction_active_mean, fraction_active_variance,
field_density))
sys.stdout.flush()
if target_fraction_active is None:
break
if iteration == 0:
scale = target_fraction_active / fraction_active_mean
num_place_field_probabilities = \
rescale_non_zero_num_place_field_probabilities(num_place_field_probabilities, scale)
iteration_label = ' after:'
pop_activity_sum = np.sum(pop_activity, axis=0)
if plot:
import matplotlib.pyplot as plt
import math
from dentate.plot import clean_axes
fig, axes = plt.subplots(2, 3, figsize=(9., 6.))
for count, i in enumerate(range(0, pop_size, int(math.ceil(pop_size / 6.)))):
axes[count // 3][count % 3].pcolor(x_mesh, y_mesh, pop_activity[i])
clean_axes(axes)
fig.suptitle('Field width: %.2f; Fraction active: %.4f' % (field_width, fraction_active_mean))
fig.tight_layout()
fig.subplots_adjust(top=0.9)
fig.show()
fig, axes = plt.subplots()
bins = max(population_num_fields) + 1
hist, edges = np.histogram(population_num_fields, bins=bins, range=(-0.5, bins - 0.5), density=True)
axes.bar(edges[1:] - 0.5, hist)
fig.suptitle('Number of place fields\nField width: %.2f' % field_width)
clean_axes(axes)
fig.subplots_adjust(top=0.85)
fig.show()
fig, axes = plt.subplots()
pc = axes.pcolor(x_mesh, y_mesh, pop_activity_sum, vmin=0.)
cb = fig.colorbar(pc, ax=axes)
cb.set_label('Firing rate (Hz)', rotation=270., labelpad=20.)
fig.suptitle('Summed population activity\nField width: %.2f' % field_width)
clean_axes(axes)
fig.subplots_adjust(top=0.85)
fig.show()
fig, axes = plt.subplots()
pc = axes.pcolor(x_mesh, y_mesh, fraction_active_array, vmin=0.)
cb = fig.colorbar(pc, ax=axes)
cb.set_label('Fraction active', rotation=270., labelpad=20.)
fig.suptitle('Fraction active\nField width: %.2f' % field_width)
clean_axes(axes)
fig.subplots_adjust(top=0.85)
fig.show()
return num_place_field_probabilities
def get_2D_arena_bounds(arena, margin=0., margin_fraction=None):
"""
:param arena: namedtuple
:return: tuple of (tuple of float)
"""
vertices_x = np.asarray([v[0] for v in arena.domain.vertices], dtype=np.float32)
vertices_y = np.asarray([v[1] for v in arena.domain.vertices], dtype=np.float32)
if margin_fraction is not None:
extent_x = np.abs(np.max(vertices_x) - np.min(vertices_x))
extent_y = np.abs(np.max(vertices_y) - np.min(vertices_y))
margin = max(margin_fraction*extent_x, margin_fraction*extent_y)
arena_x_bounds = (np.min(vertices_x) - margin, np.max(vertices_x) + margin)
arena_y_bounds = (np.min(vertices_y) - margin, np.max(vertices_y) + margin)
return arena_x_bounds, arena_y_bounds
def get_2D_arena_extents(arena):
"""
:param arena: namedtuple
:return: tuple of (tuple of float)
"""
vertices_x = np.asarray([v[0] for v in arena.domain.vertices], dtype=np.float32)
vertices_y = np.asarray([v[1] for v in arena.domain.vertices], dtype=np.float32)
extent_x = np.abs(np.max(vertices_x) - np.min(vertices_x))
extent_y = np.abs(np.max(vertices_y) - np.min(vertices_y))
return extent_x, extent_y
def get_2D_arena_spatial_mesh(arena, spatial_resolution=5., margin=0., indexing='ij'):
"""
:param arena: namedtuple
:param spatial_resolution: float (cm)
:param margin: float
:return: tuple of array
"""
arena_x_bounds, arena_y_bounds = get_2D_arena_bounds(arena=arena, margin=margin)
arena_x = np.arange(arena_x_bounds[0], arena_x_bounds[1] + spatial_resolution / 2., spatial_resolution)
arena_y = np.arange(arena_y_bounds[0], arena_y_bounds[1] + spatial_resolution / 2., spatial_resolution)
return np.meshgrid(arena_x, arena_y, indexing=indexing)
def get_2D_arena_grid(arena, spatial_resolution=5., margin=0., indexing='ij'):
"""
:param arena: namedtuple
:param spatial_resolution: float (cm)
:param margin: float
:return: tuple of array
"""
arena_x_bounds, arena_y_bounds = get_2D_arena_bounds(arena=arena, margin=margin)
arena_x = np.arange(arena_x_bounds[0], arena_x_bounds[1] + spatial_resolution / 2., spatial_resolution)
arena_y = np.arange(arena_y_bounds[0], arena_y_bounds[1] + spatial_resolution / 2., spatial_resolution)
return arena_x, arena_y
def generate_linear_trajectory(trajectory, temporal_resolution=1., equilibration_duration=None):
"""
Construct coordinate arrays for a spatial trajectory, considering run velocity to interpolate at the specified
temporal resolution. Optionally, the trajectory can be prepended with extra distance traveled for a specified
network equilibration time, with the intention that the user discards spikes generated during this period before
analysis.
:param trajectory: namedtuple
:param temporal_resolution: float (ms)
:param equilibration_duration: float (ms)
:return: tuple of array
"""
velocity = trajectory.velocity # (cm / s)
spatial_resolution = velocity / 1000. * temporal_resolution
x = trajectory.path[:, 0]
y = trajectory.path[:, 1]
if equilibration_duration is not None:
equilibration_distance = velocity / 1000. * equilibration_duration
x = np.insert(x, 0, x[0] - equilibration_distance)
y = np.insert(y, 0, y[0])
else:
equilibration_duration = 0.
equilibration_distance = 0.
segment_lengths = np.sqrt((np.diff(x) ** 2. + np.diff(y) ** 2.))
distance = np.insert(np.cumsum(segment_lengths), 0, 0.)
interp_distance = np.arange(distance.min(), distance.max() + spatial_resolution / 2., spatial_resolution)
interp_x = np.interp(interp_distance, distance, x)
interp_y = np.interp(interp_distance, distance, y)
t = interp_distance / (velocity / 1000.) # ms
t = np.subtract(t, equilibration_duration)
interp_distance -= equilibration_distance
return t, interp_x, interp_y, interp_distance
def generate_input_selectivity_features(env, population, arena, arena_x, arena_y,
gid, norm_distances,
selectivity_config, selectivity_type_names,
selectivity_type_namespaces, noise_gen_dict=None,
rate_map_sum=None, debug=False):
"""
Generates input selectivity features for the given population and
returns the selectivity type-specific dictionary provided through
argument selectivity_type_namespaces. The set of selectivity
attributes is determined by procedure get_selectivity_attr_dict in
the respective input cell configuration class
(e.g. PlaceInputCellConfig or GridInputCellConfig).
:param env
:param population: str
:param arena: str
:param gid: int