-
Notifications
You must be signed in to change notification settings - Fork 53
/
task.py
1615 lines (1270 loc) · 59.7 KB
/
task.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
"""Collections of tasks."""
from __future__ import division
import six
import numpy as np
# all rules
rules_dict = \
{'all' : ['fdgo', 'reactgo', 'delaygo', 'fdanti', 'reactanti', 'delayanti',
'dm1', 'dm2', 'contextdm1', 'contextdm2', 'multidm',
'delaydm1', 'delaydm2', 'contextdelaydm1', 'contextdelaydm2', 'multidelaydm',
'dmsgo', 'dmsnogo', 'dmcgo', 'dmcnogo'],
'mante' : ['contextdm1', 'contextdm2'],
'oicdmc' : ['oic', 'dmc']}
# Store indices of rules
rule_index_map = dict()
for ruleset, rules in rules_dict.items():
rule_index_map[ruleset] = dict()
for ind, rule in enumerate(rules):
rule_index_map[ruleset][rule] = ind
def get_num_ring(ruleset):
'''get number of stimulus rings'''
return 3 if ruleset=='oicdmc' else 2
def get_num_rule(ruleset):
'''get number of rules'''
return len(rules_dict[ruleset])
def get_rule_index(rule, config):
'''get the input index for the given rule'''
return rule_index_map[config['ruleset']][rule]+config['rule_start']
def get_dist(original_dist):
'''Get the distance in periodic boundary conditions'''
return np.minimum(abs(original_dist),2*np.pi-abs(original_dist))
class Trial(object):
"""Class representing a batch of trials."""
def __init__(self, config, tdim, batch_size):
"""A batch of trials.
Args:
config: dictionary of configurations
tdim: int, number of time steps
batch_size: int, batch size
"""
self.float_type = 'float32' # This should be the default
self.config = config
self.dt = self.config['dt']
self.n_eachring = self.config['n_eachring']
self.n_input = self.config['n_input']
self.n_output = self.config['n_output']
self.pref = np.arange(0,2*np.pi,2*np.pi/self.n_eachring) # preferences
self.batch_size = batch_size
self.tdim = tdim
self.x = np.zeros((tdim, batch_size, self.n_input), dtype=self.float_type)
self.y = np.zeros((tdim, batch_size, self.n_output), dtype=self.float_type)
if self.config['loss_type'] == 'lsq':
self.y[:,:,:] = 0.05
# y_loc is the stimulus location of the output, -1 for fixation, (0,2 pi) for response
self.y_loc = -np.ones((tdim, batch_size) , dtype=self.float_type)
self._sigma_x = config['sigma_x']*np.sqrt(2/config['alpha'])
def expand(self, var):
"""Expand an int/float to list."""
if not hasattr(var, '__iter__'):
var = [var] * self.batch_size
return var
def add(self, loc_type, locs=None, ons=None, offs=None, strengths=1, mods=None):
"""Add an input or stimulus output.
Args:
loc_type: str (fix_in, stim, fix_out, out), type of information to be added
locs: array of list of float (batch_size,), locations to be added, only for loc_type=stim or out
ons: int or list, index of onset time
offs: int or list, index of offset time
strengths: float or list, strength of input or target output
mods: int or list, modalities of input or target output
"""
ons = self.expand(ons)
offs = self.expand(offs)
strengths = self.expand(strengths)
mods = self.expand(mods)
for i in range(self.batch_size):
if loc_type == 'fix_in':
self.x[ons[i]: offs[i], i, 0] = 1
elif loc_type == 'stim':
# Assuming that mods[i] starts from 1
self.x[ons[i]: offs[i], i, 1+(mods[i]-1)*self.n_eachring:1+mods[i]*self.n_eachring] \
+= self.add_x_loc(locs[i])*strengths[i]
elif loc_type == 'fix_out':
# Notice this shouldn't be set at 1, because the output is logistic and saturates at 1
if self.config['loss_type'] == 'lsq':
self.y[ons[i]: offs[i], i, 0] = 0.8
else:
self.y[ons[i]: offs[i], i, 0] = 1.0
elif loc_type == 'out':
if self.config['loss_type'] == 'lsq':
self.y[ons[i]: offs[i], i, 1:] += self.add_y_loc(locs[i])*strengths[i]
else:
y_tmp = self.add_y_loc(locs[i])
y_tmp /= np.sum(y_tmp)
self.y[ons[i]: offs[i], i, 1:] += y_tmp
self.y_loc[ons[i]: offs[i], i] = locs[i]
else:
raise ValueError('Unknown loc_type')
def add_x_noise(self):
"""Add input noise."""
self.x += self.config['rng'].randn(*self.x.shape)*self._sigma_x
def add_c_mask(self, pre_offs, post_ons):
"""Add a cost mask.
Usually there are two periods, pre and post response
Scale the mask weight for the post period so in total it's as important
as the pre period
"""
pre_on = int(100/self.dt) # never check the first 100ms
pre_offs = self.expand(pre_offs)
post_ons = self.expand(post_ons)
if self.config['loss_type'] == 'lsq':
c_mask = np.zeros((self.tdim, self.batch_size, self.n_output), dtype=self.float_type)
for i in range(self.batch_size):
# Post response periods usually have the same length across tasks
c_mask[post_ons[i]:, i, :] = 5.
# Pre-response periods usually have different lengths across tasks
# To keep cost comparable across tasks
# Scale the cost mask of the pre-response period by a factor
c_mask[pre_on:pre_offs[i], i, :] = 1.
# self.c_mask[:, :, 0] *= self.n_eachring # Fixation is important
c_mask[:, :, 0] *= 2. # Fixation is important
self.c_mask = c_mask.reshape((self.tdim*self.batch_size, self.n_output))
else:
c_mask = np.zeros((self.tdim, self.batch_size), dtype=self.float_type)
for i in range(self.batch_size):
# Post response periods usually have the same length across tasks
# Having it larger than 1 encourages the network to achieve higher performance
c_mask[post_ons[i]:, i] = 5.
# Pre-response periods usually have different lengths across tasks
# To keep cost comparable across tasks
# Scale the cost mask of the pre-response period by a factor
c_mask[pre_on:pre_offs[i], i] = 1.
self.c_mask = c_mask.reshape((self.tdim*self.batch_size,))
self.c_mask /= self.c_mask.mean()
def add_rule(self, rule, on=None, off=None, strength=1.):
"""Add rule input."""
if isinstance(rule, int):
self.x[on:off, :, self.config['rule_start']+rule] = strength
else:
ind_rule = get_rule_index(rule, self.config)
self.x[on:off, :, ind_rule] = strength
def add_x_loc(self, x_loc):
"""Input activity given location."""
dist = get_dist(x_loc-self.pref) # periodic boundary
dist /= np.pi/8
return 0.8*np.exp(-dist**2/2)
def add_y_loc(self, y_loc):
"""Target response given location."""
dist = get_dist(y_loc-self.pref) # periodic boundary
if self.config['loss_type'] == 'lsq':
dist /= np.pi/8
y = 0.8*np.exp(-dist**2/2)
else:
# One-hot output
y = np.zeros_like(dist)
ind = np.argmin(dist)
y[ind] = 1.
return y
def test_init(config, mode, **kwargs):
'''
Test initialization of model. mode is not actually used
Fixation is on then off.
'''
dt = config['dt']
tdim = int(10000/dt)
fix_offs = [int(800/dt)]
batch_size = 1
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
return trial
def delaygo_(config, mode, anti_response, **kwargs):
'''
Fixate whenever fixation point is shown,
saccade to the location of the previously shown stimulus
whenever the fixation point is off
Generate one batch of trials
The fixation is shown between (0, fix_off)
The stimulus is shown between (stim_on, stim_off)
The output should be fixation location for (0, fix_off)
and the stimulus location for (fix_off, T)
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# A list of locations of stimuluss and on/off time
stim_locs = rng.rand(batch_size)*2*np.pi
# stim_ons = int(500/dt)
stim_ons = int(rng.choice([300, 500, 700])/dt)
# stim_offs = stim_ons + int(200/dt)
stim_offs = stim_ons + int(rng.choice([200, 400, 600])/dt)
fix_offs = stim_offs + int(rng.choice([200, 400, 800, 1600])/dt)
# fix_offs = stim_offs + int(rng.choice([1600])/dt)
tdim = fix_offs + int(500/dt)
stim_mod = rng.choice([1,2])
elif mode == 'test':
tdim = int(2500/dt)
n_stim_loc, n_stim_mod = batch_shape = 20, 2
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim_mod = np.unravel_index(range(batch_size),batch_shape)
fix_offs = int(2000/dt)
stim_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim_ons = int(500/dt)
stim_mod = ind_stim_mod + 1
stim_offs = int(1000/dt)
elif mode == 'psychometric':
p = kwargs['params']
stim_locs = p['stim_locs']
# Time of stimuluss on/off
stim_ons = int(p['stim_ons']/dt)
stim_offs = int(p['stim_offs']/dt)
delay_time = int(p['delay_time']/dt)
fix_offs = stim_offs + delay_time
tdim = int(400/dt) + fix_offs
stim_mod = 1
batch_size = len(stim_locs)
else:
raise ValueError('Unknown mode: ' + str(mode))
check_ons= fix_offs + int(100/dt)
# Response locations
stim_locs = np.array(stim_locs)
if not anti_response:
response_locs = stim_locs
else:
response_locs = (stim_locs+np.pi)%(2*np.pi)
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
trial.add('stim', stim_locs, ons=stim_ons, offs=stim_offs, mods=stim_mod)
trial.add('fix_out', offs=fix_offs)
trial.add('out', response_locs, ons=fix_offs)
trial.add_c_mask(pre_offs=fix_offs, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim_ons),
'stim1' : (stim_ons, stim_offs),
'delay1' : (stim_offs, fix_offs),
'go1' : (fix_offs, None)}
return trial
def delaygo(config, mode, **kwargs):
return delaygo_(config, mode, False, **kwargs)
def contextdm_genstim(batch_size, rng, stim_coh_range=None):
stim_mean = rng.uniform(0.8, 1.2, (batch_size,))
if stim_coh_range is None:
stim_coh_range = np.array([0.16, 0.32, 0.64])*1.0
stim_coh = rng.choice(stim_coh_range, (batch_size,))
stim_sign = rng.choice([+1, -1], (batch_size,))
stim1_strengths = stim_mean + stim_coh*stim_sign
stim2_strengths = stim_mean - stim_coh*stim_sign
return stim1_strengths, stim2_strengths
def _contextdm(config, mode, attend_mod, **kwargs):
'''
Fixate whenever fixation point is shown.
Two stimuluss are shown in each ring,
Saccade to the one with higher intensity for the attended ring
Generate one batch of trials
The fixation is shown between (0, fix_off)
The two stimuluss is shown between (0,T)
The output should be fixation location for (0, fix_off)
Otherwise the location of the stronger stimulus
In this task, if the model's strategy is to ignore context, and integrate both,
then the maximum performance is 75%. So we need to make the highest correct performance
much higher than that.
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# A list of locations of stimuluss, same locations for both modalities
stim_dist = rng.uniform(0.5*np.pi, 1.5*np.pi,(batch_size,))*rng.choice([-1,1],(batch_size,))
stim1_locs = rng.uniform(0, 2*np.pi, (batch_size,))
stim2_locs = (stim1_locs+stim_dist)%(2*np.pi)
stim_coh_range = np.array([0.01, 0.02, 0.04, 0.08])
if ('easy_task' in config) and config['easy_task']:
# stim_coh_range = np.array([0.1, 0.2, 0.4, 0.8])
stim_coh_range *= 10
if (attend_mod == 1) or (attend_mod == 2):
stim1_mod1_strengths, stim2_mod1_strengths = contextdm_genstim(batch_size, rng, stim_coh_range)
stim1_mod2_strengths, stim2_mod2_strengths = contextdm_genstim(batch_size, rng, stim_coh_range)
if attend_mod == 1:
stim1_strengths, stim2_strengths = stim1_mod1_strengths, stim2_mod1_strengths
else:
stim1_strengths, stim2_strengths = stim1_mod2_strengths, stim2_mod2_strengths
else:
stim1_strengths, stim2_strengths = contextdm_genstim(batch_size, rng, stim_coh_range)
stim1_mod12_diff = stim1_strengths * \
np.random.uniform(0.2, 0.8, (batch_size,)) * \
np.random.choice([+1, -1], (batch_size,))
stim1_mod1_strengths = stim1_strengths + stim1_mod12_diff/2
stim1_mod2_strengths = stim1_strengths - stim1_mod12_diff/2
stim2_mod12_diff = stim2_strengths * \
np.random.uniform(0.2, 0.8, (batch_size,)) * \
np.random.choice([+1, -1], (batch_size,))
stim2_mod1_strengths = stim2_strengths + stim2_mod12_diff/2
stim2_mod2_strengths = stim2_strengths - stim2_mod12_diff/2
# Time of stimuluss on/off
stim_on = int(rng.uniform(100,400)/dt)
stim_ons = (np.ones(batch_size)*stim_on).astype(int)
stim_dur = int(rng.choice([400, 800, 1600])/dt)
# stim_dur = rng.choice((np.array([200, 400, 800, 1600])/dt).astype(int)) # Current setting
# stim_dur = int(rng.uniform(500, 1000)/dt) # Current setting
# stim_dur = int(800/dt)
stim_offs = stim_ons+stim_dur
# delay_dur = rng.choice((np.array([200, 400, 800])/dt).astype(int)) # Current setting
delay_dur = 0
fix_offs = stim_offs + delay_dur
# each batch consists of sequences of equal length
tdim = stim_on+stim_dur+delay_dur+int(500/dt)
elif mode == 'test':
tdim = int(2000/dt)
n_stim_loc, n_stim_mod1_strength, n_stim_mod2_strength = batch_shape = 20, 5, 5
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim_mod1_strength, ind_stim_mod2_strength = np.unravel_index(range(batch_size),batch_shape)
fix_offs = int(1500/dt)
stim1_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim2_locs = (stim1_locs+np.pi)%(2*np.pi)
stim1_mod1_strengths = 0.4*ind_stim_mod1_strength/n_stim_mod1_strength+0.8
stim2_mod1_strengths = 2 - stim1_mod1_strengths
stim1_mod2_strengths = 0.4*ind_stim_mod2_strength/n_stim_mod2_strength+0.8
stim2_mod2_strengths = 2 - stim1_mod2_strengths
stim_ons = int(500/dt)
stim_offs = int(1500/dt)
elif mode == 'psychometric':
p = kwargs['params']
stim1_locs = p['stim1_locs']
stim2_locs = p['stim2_locs']
stim1_mod1_strengths = p['stim1_mod1_strengths']
stim2_mod1_strengths = p['stim2_mod1_strengths']
stim1_mod2_strengths = p['stim1_mod2_strengths']
stim2_mod2_strengths = p['stim2_mod2_strengths']
stim_time = int(p['stim_time']/dt)
batch_size = len(stim1_locs)
# Time of stimuluss on/off
stim_ons = int(500/dt)
stim_offs = stim_ons + stim_time
fix_offs = stim_offs
tdim = int(500/dt) + fix_offs
else:
raise ValueError('Unknown mode: ' + str(mode))
# time to check the saccade location
check_ons = fix_offs + int(100/dt)
if attend_mod == 1:
stim1_strengths, stim2_strengths = stim1_mod1_strengths, stim2_mod1_strengths
elif attend_mod == 2:
stim1_strengths, stim2_strengths = stim1_mod2_strengths, stim2_mod2_strengths
elif attend_mod == 'both':
stim1_strengths = stim1_mod1_strengths + stim1_mod2_strengths
stim2_strengths = stim2_mod1_strengths + stim2_mod2_strengths
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
trial.add('stim', stim1_locs, ons=stim_ons, offs=stim_offs, strengths=stim1_mod1_strengths, mods=1)
trial.add('stim', stim2_locs, ons=stim_ons, offs=stim_offs, strengths=stim2_mod1_strengths, mods=1)
trial.add('stim', stim1_locs, ons=stim_ons, offs=stim_offs, strengths=stim1_mod2_strengths, mods=2)
trial.add('stim', stim2_locs, ons=stim_ons, offs=stim_offs, strengths=stim2_mod2_strengths, mods=2)
trial.add('fix_out', offs=fix_offs)
stim_locs = [stim1_locs[i] if (stim1_strengths[i]>stim2_strengths[i])
else stim2_locs[i] for i in range(batch_size)]
trial.add('out', stim_locs, ons=fix_offs)
trial.add_c_mask(pre_offs=fix_offs, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim_ons),
'stim1' : (stim_ons, stim_offs),
# 'delay1' : (stim_offs, fix_offs),
'go1' : (fix_offs, None)}
return trial
def contextdm1(config, mode, **kwargs):
return _contextdm(config, mode, 1, **kwargs)
def contextdm2(config, mode, **kwargs):
return _contextdm(config, mode, 2, **kwargs)
def multidm(config, mode, **kwargs):
return _contextdm(config, mode, 'both', **kwargs)
def reactgo_(config, mode, anti_response, **kwargs):
'''
Fixate when fixation point is shown,
A stimulus will be shown, and the output should saccade to the stimulus location
Generate one batch of trials
The fixation is shown between (0, T)
The stimulus is shown between (fix_off,T)
The output should be fixation location for (0, fix_off)
Otherwise should be the stimulus location
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# each batch consists of sequences of equal length
# A list of locations of fixation points and fixation off time
stim_ons = int(rng.uniform(500,2500)/dt)
tdim = int(500/dt) + stim_ons
# A list of locations of stimuluss (they are always on)
stim_locs = rng.uniform(0, 2*np.pi, (batch_size,))
stim_mod = rng.choice([1,2])
elif mode == 'test':
tdim = int(2500/dt)
n_stim_loc, n_stim_mod = batch_shape = 20, 2
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim_mod = np.unravel_index(range(batch_size),batch_shape)
stim_ons = int(2000/dt)
stim_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim_mod = ind_stim_mod + 1
elif mode == 'psychometric':
p = kwargs['params']
stim_locs = p['stim_locs']
batch_size = len(stim_locs)
# Time of stimuluss on/off
stim_ons = int(1000/dt)
tdim = int(400/dt) + stim_ons
stim_mod = 1
else:
raise ValueError('Unknown mode: ' + str(mode))
# time to check the saccade location
check_ons = stim_ons + int(100/dt)
# Response locations
stim_locs = np.array(stim_locs)
if not anti_response:
response_locs = stim_locs
else:
response_locs = (stim_locs+np.pi)%(2*np.pi)
trial = Trial(config, tdim, batch_size)
trial.add('fix_in')
trial.add('stim', stim_locs, ons=stim_ons, mods=stim_mod)
trial.add('fix_out', offs=stim_ons)
trial.add('out', response_locs, ons=stim_ons)
trial.add_c_mask(pre_offs=stim_ons, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim_ons),
'go1' : (stim_ons, None)}
return trial
def reactgo(config, mode, **kwargs):
return reactgo_(config, mode, False, **kwargs)
def reactanti(config, mode, **kwargs):
return reactgo_(config, mode, True, **kwargs)
def fdgo_(config, mode, anti_response, **kwargs):
'''
Go with inhibitory control. Important difference with Go task is that
the stimulus is presented from the beginning.
Fixate whenever fixation point is shown,
A stimulus will be shown from the beginning
And output should saccade to the stimulus location
Generate one batch of trials
The fixation is shown between (0, fix_off)
The stimulus is shown between (0,T)
The output should be fixation location for (0, fix_off)
Otherwise should be the stimulus location
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# each batch consists of sequences of equal length
# A list of locations of fixation points and fixation off time
# A list of locations of stimulus (they are always on)
stim_locs = rng.rand(batch_size)*2*np.pi
stim_mod = rng.choice([1,2])
stim_ons = int(rng.uniform(300,700)/dt)
fix_offs = stim_ons + int(rng.uniform(500,1500)/dt)
tdim = int(500/dt) + fix_offs
elif mode == 'test':
tdim = int(2000/dt)
n_stim_loc, n_stim_mod = batch_shape = 20, 2
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim_mod = np.unravel_index(range(batch_size),batch_shape)
stim_ons = int(500/dt)
fix_offs = int(1500/dt)
stim_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim_mod = ind_stim_mod + 1
elif mode == 'psychometric':
p = kwargs['params']
stim_locs = p['stim_locs']
stim_time = int(p['stim_time']/dt)
batch_size = len(stim_locs)
# Time of stimuluss on/off
stim_ons = int(300/dt)
fix_offs = stim_ons + stim_time
tdim = int(400/dt) + fix_offs
stim_mod = 1
else:
raise ValueError('Unknown mode: ' + str(mode))
# time to check the saccade location
check_ons = fix_offs + int(100/dt)
# Response locations
stim_locs = np.array(stim_locs)
if not anti_response:
response_locs = stim_locs
else:
response_locs = (stim_locs+np.pi)%(2*np.pi)
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
trial.add('stim', stim_locs, ons=stim_ons, mods=stim_mod)
trial.add('fix_out', offs=fix_offs)
trial.add('out', response_locs, ons=fix_offs)
trial.add_c_mask(pre_offs=fix_offs, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim_ons),
'stim1' : (stim_ons, fix_offs),
'go1' : (fix_offs, None)}
return trial
def fdgo(config, mode, **kwargs):
return fdgo_(config, mode, False, **kwargs)
def fdanti(config, mode, **kwargs):
return fdgo_(config, mode, True, **kwargs)
def delayanti(config, mode, **kwargs):
return delaygo_(config, mode, True, **kwargs)
def _dm(config, mode, stim_mod, **kwargs):
'''
Fixate whenever fixation point is shown.
Two stimuluss are shown, saccade to the one with higher intensity
Generate one batch of trials
The fixation is shown between (0, fix_off)
The two stimuluss is shown between (0,T)
The output should be fixation location for (0, fix_off)
Otherwise the location of the stronger stimulus
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# A list of locations of stimuluss (they are always on)
stim_dist = rng.uniform(0.5*np.pi,1.5*np.pi,(batch_size,))*rng.choice([-1,1],(batch_size,))
stim1_locs = rng.uniform(0, 2*np.pi, (batch_size,))
stim2_locs = (stim1_locs+stim_dist)%(2*np.pi)
# Target strengths
stims_mean = rng.uniform(0.8,1.2,(batch_size,))
# stims_diff = rng.uniform(0.01,0.2,(batch_size,))
# stims_diff = rng.choice([0.02, 0.04, 0.08], (batch_size,)) # Encourage integration
# stims_coh = rng.choice([0.16, 0.32, 0.64], (batch_size,))
stim_coh_range = np.array([0.01, 0.02, 0.04, 0.08])
if ('easy_task' in config) and config['easy_task']:
# stim_coh_range = np.array([0.1, 0.2, 0.4, 0.8])
stim_coh_range *= 10
stims_coh = rng.choice(stim_coh_range, (batch_size,))
stims_sign = rng.choice([1,-1], (batch_size,))
stim1_strengths = stims_mean + stims_coh*stims_sign
stim2_strengths = stims_mean - stims_coh*stims_sign
# Time of stimuluss on/off
stim_on = int(rng.uniform(100,400)/dt)
stim_ons = (np.ones(batch_size)*stim_on).astype(int)
# stim_dur = int(rng.uniform(300,1500)/dt)
stim_dur = int(rng.choice([400, 800, 1600])/dt)
fix_offs = (stim_ons+stim_dur).astype(int)
# each batch consists of sequences of equal length
tdim = stim_on+stim_dur+int(500/dt)
elif mode == 'test':
# Dense coverage of the stimulus space
tdim = int(2500/dt)
n_stim_loc, n_stim1_strength = batch_shape = 20, 5
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim1_strength = np.unravel_index(range(batch_size),batch_shape)
fix_offs = int(2000/dt)
stim1_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim2_locs = (stim1_locs+np.pi)%(2*np.pi)
stim1_strengths = 0.4*ind_stim1_strength/n_stim1_strength+0.8
stim2_strengths = 2 - stim1_strengths
stim_ons = int(500/dt)
elif mode == 'psychometric':
p = kwargs['params']
stim1_locs = p['stim1_locs']
stim2_locs = p['stim2_locs']
stim1_strengths = p['stim1_strengths']
stim2_strengths = p['stim2_strengths']
stim_time = int(p['stim_time']/dt)
batch_size = len(stim1_locs)
# Time of stimuluss on/off
stim_ons = int(300/dt)
fix_offs = int(300/dt) + stim_time
tdim = int(400/dt) + fix_offs
else:
raise ValueError('Unknown mode: ' + str(mode))
# time to check the saccade location
check_ons = fix_offs + int(100/dt)
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
trial.add('stim', stim1_locs, ons=stim_ons, offs=fix_offs, strengths=stim1_strengths, mods=stim_mod)
trial.add('stim', stim2_locs, ons=stim_ons, offs=fix_offs, strengths=stim2_strengths, mods=stim_mod)
trial.add('fix_out', offs=fix_offs)
stim_locs = [stim1_locs[i] if (stim1_strengths[i]>stim2_strengths[i])
else stim2_locs[i] for i in range(batch_size)]
trial.add('out', stim_locs, ons=fix_offs)
trial.add_c_mask(pre_offs=fix_offs, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim_ons),
'stim1' : (stim_ons, fix_offs),
'go1' : (fix_offs, None)}
return trial
def dm1(config, mode, **kwargs):
return _dm(config, mode, 1, **kwargs)
def dm2(config, mode, **kwargs):
return _dm(config, mode, 2, **kwargs)
def _delaydm(config, mode, stim_mod, **kwargs):
'''
Fixate whenever fixation point is shown.
Two stimuluss are shown at different time, with different intensities
The fixation is shown between (0, fix_off)
The two stimuluss is shown between (0,T)
The output should be fixation location for (0, fix_off)
Otherwise the location of the stronger stimulus
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# A list of locations of stimuluss (they are always on)
stim_dist = rng.uniform(0.5*np.pi, 1.5*np.pi,(batch_size,))*rng.choice([-1,1],(batch_size,))
stim1_locs = rng.uniform(0, 2*np.pi, (batch_size,))
stim2_locs = (stim1_locs+stim_dist)%(2*np.pi)
stims_mean = rng.uniform(0.8,1.2,(batch_size,))
# stims_diff = rng.choice([0.32,0.64,1.28],(batch_size,))
stim_coh_range = np.array([0.08,0.16,0.32])
if ('easy_task' in config) and config['easy_task']:
# stim_coh_range = np.array([0.16,0.32,0.64])
stim_coh_range *= 2
stims_coh = rng.choice(stim_coh_range,(batch_size,))
stims_sign = rng.choice([1,-1], (batch_size,))
stim1_strengths = stims_mean + stims_coh*stims_sign
stim2_strengths = stims_mean - stims_coh*stims_sign
# stim1_strengths = rng.uniform(0.25,1.75,(batch_size,))
# stim2_strengths = rng.uniform(0.25,1.75,(batch_size,))
# Time of stimuluss on/off
stim1_ons = int(rng.choice([200, 400, 600])/dt)
stim1_offs = stim1_ons + int(rng.choice([200, 400, 600])/dt)
stim2_ons = stim1_offs + int(rng.choice([200, 400, 800, 1600])/dt)
stim2_offs = stim2_ons + int(rng.choice([200, 400, 600])/dt)
fix_offs = stim2_offs + int(rng.uniform(100,300)/dt)
# stim2_ons = (np.ones(batch_size)*rng.choice([400,500,600,700,1400])/dt).astype(int)
# stim2_ons = (np.ones(batch_size)*rng.choice([400,600,1000,1400,2000])/dt).astype(int)
# stim2_ons = (np.ones(batch_size)*rng.uniform(2800,3200)/dt).astype(int)
# each batch consists of sequences of equal length
tdim = fix_offs + int(500/dt) # longest trial
elif mode == 'test':
tdim = int(3000/dt)
n_stim_loc, n_stim1_strength = batch_shape = 20, 5
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim1_strength = np.unravel_index(range(batch_size),batch_shape)
fix_offs = int(2700/dt)
stim1_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim2_locs = (stim1_locs+np.pi)%(2*np.pi)
stim1_strengths = 1.0*ind_stim1_strength/n_stim1_strength+0.5
stim2_strengths = 2 - stim1_strengths
stim1_ons = int(500/dt)
stim1_offs = int(1000/dt)
stim2_ons = int(2000/dt)
stim2_offs = int(2500/dt)
elif mode == 'psychometric':
p = kwargs['params']
stim1_locs = p['stim1_locs']
stim2_locs = p['stim2_locs']
stim1_strengths = p['stim1_strengths']
stim2_strengths = p['stim2_strengths']
stim1_ons = int(p['stim1_ons']/dt)
stim1_offs = int(p['stim1_offs']/dt)
stim2_ons = int(p['stim2_ons']/dt)
stim2_offs = int(p['stim2_offs']/dt)
batch_size = len(stim1_locs)
fix_offs = int(200/dt) + stim2_offs
tdim = int(300/dt) + fix_offs
else:
raise ValueError('Unknown mode: ' + str(mode))
# time to check the saccade location
check_ons = fix_offs + int(100/dt)
trial = Trial(config, tdim, batch_size)
trial.add('fix_in', offs=fix_offs)
trial.add('stim', stim1_locs, ons=stim1_ons, offs=stim1_offs, strengths=stim1_strengths, mods=stim_mod)
trial.add('stim', stim2_locs, ons=stim2_ons, offs=stim2_offs, strengths=stim2_strengths, mods=stim_mod)
trial.add('fix_out', offs=fix_offs)
stim_locs = [stim1_locs[i] if (stim1_strengths[i]>stim2_strengths[i])
else stim2_locs[i] for i in range(batch_size)]
trial.add('out', stim_locs, ons=fix_offs)
trial.add_c_mask(pre_offs=fix_offs, post_ons=check_ons)
trial.epochs = {'fix1' : (None, stim1_ons),
'stim1' : (stim1_ons, stim1_offs),
'delay1' : (stim1_offs, stim2_ons),
'stim2' : (stim2_ons, stim2_offs),
'delay2' : (stim2_offs, fix_offs),
'go1' : (fix_offs, None)}
return trial
def delaydm1(config, mode, **kwargs):
return _delaydm(config, mode, 1, **kwargs)
def delaydm2(config, mode, **kwargs):
return _delaydm(config, mode, 2, **kwargs)
def _contextdelaydm(config, mode, attend_mod, **kwargs):
'''
Fixate whenever fixation point is shown.
Two stimuluss are shown in each ring,
Saccade to the one with higher intensity for the attended ring
Generate one batch of trials
The fixation is shown between (0, fix_off)
The two stimuluss is shown between (0,T)
The output should be fixation location for (0, fix_off)
Otherwise the location of the stronger stimulus
In this task, if the model's strategy is to ignore context, and integrate both,
then the maximum performance is 75%. So we need to make the highest correct performance
much higher than that.
:param mode: the mode of generating. Options: 'random', 'explicit'...
Optional parameters:
:param batch_size: Batch size (required for mode=='random')
:param tdim: dimension of time (required for mode=='sample')
:param param: a dictionary of parameters (required for mode=='explicit')
:return: 2 Tensor3 data array (Time, Batchsize, Units)
'''
dt = config['dt']
rng = config['rng']
if mode == 'random': # Randomly generate parameters
batch_size = kwargs['batch_size']
# A list of locations of stimuluss, same locations for both modalities
stim_dist = rng.uniform(0.5*np.pi,1.5*np.pi,(batch_size,))*rng.choice([-1,1],(batch_size,))
stim1_locs = rng.uniform(0, 2*np.pi, (batch_size,))
stim2_locs = (stim1_locs+stim_dist)%(2*np.pi)
stim_coh_range = np.array([0.08,0.16,0.32])
if ('easy_task' in config) and config['easy_task']:
# stim_coh_range = np.array([0.16, 0.32, 0.64])
stim_coh_range *= 2
if (attend_mod == 1) or (attend_mod == 2):
stim1_mod1_strengths, stim2_mod1_strengths = \
contextdm_genstim(batch_size, rng, stim_coh_range)
stim1_mod2_strengths, stim2_mod2_strengths = \
contextdm_genstim(batch_size, rng, stim_coh_range)
if attend_mod == 1:
stim1_strengths, stim2_strengths = stim1_mod1_strengths, stim2_mod1_strengths
else:
stim1_strengths, stim2_strengths = stim1_mod2_strengths, stim2_mod2_strengths
else:
stim1_strengths, stim2_strengths = \
contextdm_genstim(batch_size, rng, stim_coh_range)
stim1_mod12_diff = stim1_strengths * \
np.random.uniform(0.2, 0.8, (batch_size,)) * \
np.random.choice([+1, -1], (batch_size,))
stim1_mod1_strengths = stim1_strengths + stim1_mod12_diff/2
stim1_mod2_strengths = stim1_strengths - stim1_mod12_diff/2
stim2_mod12_diff = stim2_strengths * \
np.random.uniform(0.2, 0.8, (batch_size,)) * \
np.random.choice([+1, -1], (batch_size,))
stim2_mod1_strengths = stim2_strengths + stim2_mod12_diff/2
stim2_mod2_strengths = stim2_strengths - stim2_mod12_diff/2
# Time of stimuluss on/off
stim1_ons = int(rng.choice([200, 400, 600])/dt)
stim1_offs = stim1_ons + int(rng.choice([200, 400, 600])/dt)
stim2_ons = stim1_offs + int(rng.choice([200, 400, 800, 1600])/dt)
stim2_offs = stim2_ons + int(rng.choice([200, 400, 600])/dt)
fix_offs = stim2_offs + int(rng.uniform(100,300)/dt)
# each batch consists of sequences of equal length
tdim = fix_offs + int(500/dt) # longest trial
elif mode == 'test':
n_stim_loc, n_stim_mod1_strength, n_stim_mod2_strength = batch_shape = 20, 5, 5
batch_size = np.prod(batch_shape)
ind_stim_loc, ind_stim_mod1_strength, ind_stim_mod2_strength = np.unravel_index(range(batch_size),batch_shape)
stim1_locs = 2*np.pi*ind_stim_loc/n_stim_loc
stim2_locs = (stim1_locs+np.pi)%(2*np.pi)
stim1_mod1_strengths = 0.4*ind_stim_mod1_strength/n_stim_mod1_strength+0.8
stim2_mod1_strengths = 2 - stim1_mod1_strengths
stim1_mod2_strengths = 0.4*ind_stim_mod2_strength/n_stim_mod2_strength+0.8
stim2_mod2_strengths = 2 - stim1_mod2_strengths
stim1_ons = int(500/dt)
stim1_offs = int(1000/dt)
stim2_ons = int(2000/dt)
stim2_offs = int(2500/dt)
fix_offs = int(3000/dt)
tdim = int(3500/dt)
elif mode == 'psychometric':
p = kwargs['params']
stim1_locs = p['stim1_locs']
stim2_locs = p['stim2_locs']
stim1_mod1_strengths = p['stim1_mod1_strengths']
stim2_mod1_strengths = p['stim2_mod1_strengths']