forked from emackev/HVCModelCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HVCModelCode.m
1573 lines (1414 loc) · 54 KB
/
HVCModelCode.m
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
function HVCModelCode
% Script to reproduce figure 5 a-d and extended data figure 10 a-d from
% Okubo et al.
%
% This file will be posted as supplemental info for Okubo et al, so is
% formatted as one script to reproduce figure 5 a-d and extended data
% figure 10 a-d. More useable code (separated into different .m files),
% including code for the other figures and supplemental movies, can be
% found here: https://github.com/emackev/HVCModelCode
%
% Emily Mackevicius 7/18/2015, based on Hannah Payne's code
% which builds off Ila Fiete's model, with help from Michale Fee and Tatsuo
% Okubo.
%% *Alternating differentiation*
% From subsong through protosyllable stage through splitting,
% to generate figure 5 a-d
% Calls HVCIter to step through one iteration of the model
%% Alternating differentiation: network parameters
% fixed parameters
seed = 9038;
p.seed = seed; % seed random number generator
p.n = 100; % n neurons
p.trainint = 10; % Time interval between inputs
p.nsteps = 100; % time-steps to simulate -- each time-step is 1
% burst duration.
nstepsSubsong = 1000; % time-steps to simulate for subsong stage
p.pin = .01; % probability of external stimulation of at least
% one neuron at any time
k = 10; % number of training neurons
p.trainingInd = 1:k; % index of training neurons
p.beta = .115; % strength of feedforward inhibition
p.alpha = 30; % strength of neural adaptation
p.eta = .025; % learning rate parameter
p.epsilon = .2; % relative strength of heterosynaptic LTD
p.tau = 4; % time constant of adaptation
gammaStart= .01; % strength of recurrent inhibition
gammaSplit =.18; % increased strength of recurrent inhibition to
% induce splitting
wmaxStart = 1; % single synapse hard bound
wmaxSplit = 2; % single synapse hard bound to induce splitting
% (increased to encourage fewer stronger synapses)
mStart = 10; % desired number of synapses per neuron
% (wmax = Wmax/m)
Wmax = mStart*wmaxStart;% soft bound for weights of each neuron
mSplit = Wmax/wmaxSplit;% keep Wmax constant, change m & wmax to induce
% fewer stronger synapses
HowClamped = 10; % give training neurons higher threshold
HowOn = 10; % higher inputs to training neurons
% how many iterations to run before plotting
nIterProto = 500; % end of protosyllable stage
nIterPlotSplit1 = 492; % number of splitting iterations before plotting
% intermediate splitting phase
nIterPlotSplit2 = 2000; % total number of splitting iterations
% parameters that change over development
protosyllableStage = [true(1,nIterProto) false(1,nIterPlotSplit2)];
splittingStage = [false(1,nIterProto) true(1,nIterPlotSplit2)];
gammas(protosyllableStage) = gammaStart;
gammas(splittingStage) = gammaSplit*sigmf(1:nIterPlotSplit2,[1/200 500]);
wmaxs(protosyllableStage) = wmaxStart;
wmaxs(splittingStage) = wmaxSplit;
ms(protosyllableStage) = mStart;
ms(splittingStage) = mSplit;
%Subsong Inputs
rng(seed)
isOnset = rand(1,nstepsSubsong)>.9;
Input =-HowClamped*ones(k, nstepsSubsong); % clamp training neurons
% (effectively giving them higher threshold)
Input(:,isOnset) = HowOn;
bdyn = double(rand(p.n,nstepsSubsong)>=(1-p.pin)); % Random activation
bdyn(1:k,:) = Input;
subsongInput = bdyn;
%Protosyllable inputs
PsylInput = -HowClamped*ones(k, p.nsteps); %clamp training neurons
% (effectively giving them higher threshold)
PsylInput(:,mod(1:p.nsteps,p.trainint)==1) = HowOn; % rhythmic activation
% of training neurons
%Alternating Inputs
AltInput =-HowClamped*ones(k, p.nsteps);
AltInput(1:k/2,mod(1:p.nsteps,2*p.trainint)==1) = HowOn;
AltInput((k/2+1):k,mod(1:p.nsteps,2*p.trainint)==p.trainint+1) = HowOn;
% alternating rhythmic activation of training neurons
%% Alternating differentiation: run simulation
% random initial weights
rng(seed);
w0 = 2*rand(p.n)*Wmax/p.n;
% subsong stage
pSubsong = p;
pSubsong.gamma = gammas(1);
pSubsong.wmax = wmaxs(1);
pSubsong.m = ms(1);
pSubsong.eta = 0;
pSubsong.epsilon = 0;
pSubsong.nsteps = nstepsSubsong;
pSubsong.w = w0;
pSubsong.input = subsongInput;
% Run subsong network
[wSubsong xdynSubsong] = HVCIter(pSubsong);
w = wSubsong;
% learning stages
for t = 1:(nIterProto+nIterPlotSplit2)
p.w = w;
% set parameters that change over development
p.gamma = gammas(t);
p.wmax = wmaxs(t);
p.m = ms(t);
% Construct input
bdyn = double(rand(p.n,p.nsteps)>=(1-p.pin)); % Random activation
bdyn(1:k,:) = protosyllableStage(t)*PsylInput+ ...
splittingStage(t)*AltInput; % drive to seed neurons
p.input = bdyn;
% run one iteration
[w xdyn] = HVCIter(p);
% save certain iterations for plotting later
switch t
case nIterProto;
wProto = w;
xdynProto = xdyn;
case nIterProto + nIterPlotSplit1;
wSplit1 = w;
xdynSplit1 = xdyn;
case nIterProto + nIterPlotSplit2;
wSplit2 = w;
xdynSplit2 = xdyn;
end
end
%% Alternating differentiation: plotting parameters
figure(1)
isEPS = 0;
clf
set(gcf, 'color', ones(1,3));
if isEPS
PlottingParams.msize = 8;
PlottingParams.linewidth = .25;
set(0,'defaultAxesFontName', 'Arial')
set(0,'defaultTextFontName', 'Arial')
PlottingParams.labelFontSize = 7;
set(gcf, 'units','centimeters', 'position', [5 5 13.5 6])
else
PlottingParams.msize = 10;
PlottingParams.linewidth = .25;
PlottingParams.labelFontSize = 7;
end
PlottingParams.SeedColor = [.95 .5 1];
PlottingParams.Syl1Color = [1 0 0];
PlottingParams.Syl2Color = [0 0 1];
PlottingParams.ProtoSylColor = [0 0 0];
PlottingParams.ProtoSylBarColor = [.5 .5 .5];
PlottingParams.SubsongSylColor = [0 0 0];
PlottingParams.SubsongBarColor = [1 1 1];
PlottingParams.numFontSize = 5;
PlottingParams.wplotmin = 0;
PlottingParams.wplotmax = 2; % this should be wmaxSplit
PlottingParams.wprctile = 0; % plot all weights above this percentile.
% If nonzero, ignores wplotmin, wplotmax
PlottingParams.wperneuron = 6; % max outgoing weights plotted
PlottingParams.wperneuronIn = 9; % min incoming weights plotted
PlottingParams.totalPanels = 4;
nplots = 4;
bottom = .1;
height = .45;
scale = .005;
spacing = .75/(2*nplots);
%% Alternating differentiation: plotting subsong
trainingNeuronsSubsong{1}.nIDs = 1:k;
trainingNeuronsSubsong{1}.tind = find(isOnset);
trainingNeuronsSubsong{1}.candLat = 1:2*p.trainint;
trainingNeuronsSubsong{1}.thres = 12; % criteria for participation during
% subsong (thres from testLatSig --
% must fire at consistent latency
% more than 12 times in the bout of
% ~100 syllables to count as
% participating)
PlottingParams.thisPanel = 1;
PlottingParams.Hor = 1;
plotSubsong(wSubsong, xdynSubsong, trainingNeuronsSubsong, PlottingParams)
%% Alternating differentiation: plotting protosylable
trainingNeuronsPsyl{1}.nIDs = 1:k;
trainingNeuronsPsyl{2}.nIDs = 1:k;
trainingNeuronsPsyl{1}.tind = find(mod(1:p.nsteps, p.trainint)==1);
trainingNeuronsPsyl{2}.tind = find(mod(1:p.nsteps, p.trainint)==1);
trainingNeuronsPsyl{1}.candLat = 1:p.trainint;
trainingNeuronsPsyl{2}.candLat = 1:p.trainint;
trainingNeuronsPsyl{1}.thres = 4; % criteria for participation during
% protosyllable stage (thres from
% testLatSig -- must fire at consistent
% latency more than 4 times in the bout
% of 10 syllables to count as
% participating)
trainingNeuronsPsyl{2}.thres = 4;
PlottingParams.thisPanel = 2;
subplot('position', ...
[PlottingParams.thisPanel/nplots-.9/nplots, .6, .9/nplots, .35])
plotHVCnet(wProto,xdynProto,p.trainint,trainingNeuronsPsyl,PlottingParams)
set(gca, 'color', 'none');
PlottingParams.axesPosition = ...
[PlottingParams.thisPanel/nplots-2*spacing, bottom, 40*scale, height];
plotAlternating(wProto, xdynProto, p.trainint, ...
trainingNeuronsPsyl, PlottingParams)
set(gca, 'color', 'none')
%% Alternating differentiation: plotting splitting stages
trainingNeuronsAlt{1}.nIDs = 1:k/2;
trainingNeuronsAlt{2}.nIDs = (k/2+1):k;
trainingNeuronsAlt{1}.tind = find(mod(1:p.nsteps, 2*p.trainint)==1);
trainingNeuronsAlt{2}.tind = find(mod(1:p.nsteps, ...
2*p.trainint)==p.trainint+1);
trainingNeuronsAlt{1}.candLat = 1:p.trainint;
trainingNeuronsAlt{2}.candLat = 1:p.trainint;
trainingNeuronsAlt{1}.thres = 2; % criteria for participation during
% splitting stage (thres from testLatSig
% -- must fire at consistent latency
% more than 2 times in the bout of 5
% syllables (of each type) to count
% as participating)
trainingNeuronsAlt{2}.thres = 2;
PlottingParams.thisPanel = 3;
subplot('position', ...
[PlottingParams.thisPanel/nplots-.9/nplots, .6, .9/nplots, .35])
plotHVCnet(wSplit1,xdynSplit1,p.trainint,trainingNeuronsAlt,PlottingParams)
set(gca, 'color', 'none');
PlottingParams.axesPosition = ...
[PlottingParams.thisPanel/nplots-2*spacing, bottom, 40*scale, height];
plotAlternating(wSplit1, xdynSplit1, p.trainint, ...
trainingNeuronsAlt, PlottingParams)
set(gca, 'color', 'none')
PlottingParams.thisPanel = 4;
subplot('position', ...
[PlottingParams.thisPanel/nplots-.9/nplots, .6, .9/nplots, .35])
plotHVCnet(wSplit2,xdynSplit2,p.trainint,trainingNeuronsAlt,PlottingParams)
set(gca, 'color', 'none');
PlottingParams.axesPosition = ...
[PlottingParams.thisPanel/nplots-2*spacing, bottom, 40*scale, height];
plotAlternating(wSplit2, xdynSplit2, p.trainint, ...
trainingNeuronsAlt, PlottingParams)
set(gca, 'color', 'none')
%% Alternating differentiation: exporting
if isEPS
cd('Z:\Fee_lab\Papers\HVC_differentiation\Figures\EPS_files');
export_fig(1,'Figure5a.eps','-transparent','-eps','-painters');
else
%figure parameters, exporting
figw = 6;
figh = 2;
set(gcf, 'color', [1 1 1],...
'papersize', [figw figh], 'paperposition', [0 0 figw*.9 figh])
% print -dmeta -r150
end
%% *Bout-onset differentiation*
% Code to generate figure 5 j-m, which shows bout onset differentiation
clear all;
%% Bout-onset differentiation: network parameters
% fixed parameters
seed = 1009;
p.seed = seed; % seed random number generator
p.n = 100; % n neurons
p.trainint = 10; % Time interval between inputs
p.nsteps = 500; % time-steps to simulate --
% each time-step is 1 burst duration.
p.pin = .01; % probability of external stimulation
% of at least one neuron at any time
k = 10; % number of training neurons
p.trainingInd = 1:k; % index of training neurons
p.beta = .13; % strength of feedforward inhibition
p.alpha = 30; % strength of neural adaptation
p.eta = .05; % learning rate parameter
p.epsilon = .14; % relative strength of heterosynaptic LTD
p.tau = 4; % time constant of adaptation
gammaStart = .01; % strength of recurrent inhibition
gammaSplit = .04; % increased strength of recurrent inhibition
% to induce splitting
wmaxStart = 1; % single synapse hard bound
wmaxSplit = 2; % single synapse hard bound to induce splitting
% (increased to encourage fewer stronger synapses)
mStart = 5; % desired number of synapses per neuron
% (wmax = Wmax/m)
Wmax = mStart*wmaxStart;% soft bound for weights of each neuron
mSplit = Wmax/wmaxSplit;% keep Wmax constant, change m & wmax
% to induce fewer stronger synapses
HowClamped = 10; % give training neurons higher threshold
HowOn = 10; % higher inputs to bout onset training neurons
HowOnPsylStart = HowOn; % inputs to protosyllable training neurons
HowOnPsylSplit = 1; % decrease input to protosyllable training neurons
% during splitting
% how many iterations to run before plotting
nIterEarly = 5; % early protosyllable stage
nIterProto = 100; % end of protosyllable stage
nIterPlotSplit1 = 30; % number of splitting iterations before plotting
% intermediate splitting phase
nIterPlotSplit2 = 500; % total number of splitting iterations
% parameters that change over development
protosyllableStage = [true(1,nIterProto) false(1,nIterPlotSplit2)];
splittingStage = [false(1,nIterProto) true(1,nIterPlotSplit2)];
gammas(protosyllableStage) = gammaStart;
gammas(splittingStage) = gammaSplit * sigmf(1:nIterPlotSplit2,[1/200 250]);
wmaxs(protosyllableStage) = wmaxStart;
wmaxs(splittingStage) = wmaxSplit;
ms(protosyllableStage) = mStart;
ms(splittingStage) = mSplit;
HowOnPsyl(protosyllableStage) = HowOnPsylStart;
HowOnPsyl(splittingStage) = HowOnPsylSplit;
% params for training inputs
CyclesPerBout = 5;
bOnOffset = 3;
%% Bout-onset differentiation: run simulation
% random initial weights
rng(seed);
w = 2*rand(p.n)*Wmax/p.n;
bOnOffsetVar = [1 randperm(20)]; % variable inter-bout-interval
% learning stages
for t = 1:(nIterProto+nIterPlotSplit2)
p.w = w;
% set parameters that change over development
p.gamma = gammas(t);
p.wmax = wmaxs(t);
p.m = ms(t);
% Construct input
Input = -HowClamped*ones(k, p.nsteps); % clamp training neurons
bOnOffsetVar = [1 randperm(20)]; % variable inter-bout-interval
% initializing
indPsyl = []; indBstart = []; indOff = []; prevPsylEnd = 1;
for i = 1:(p.nsteps/CyclesPerBout/p.trainint)
istart = (i-1)*CyclesPerBout*p.trainint+1+bOnOffsetVar(i)+bOnOffset;
indBstart = [indBstart istart-bOnOffset]; % bout onset times
indPsyl = [indPsyl ...
istart istart+p.trainint istart+2*p.trainint]; % 3psyls/bout
indOff = [indOff ...
prevPsylEnd:(istart-bOnOffset-1)]; % will clamp all neurons
% between bouts
prevPsylEnd = istart+3*p.trainint; % keep track of when bout ends,
% to clamp neurons between bouts
end
indPsyl = indPsyl(indPsyl<=p.nsteps);
indBstart = indBstart(indBstart<=p.nsteps);
Input(1:k/2,indBstart) = HowOn; % input to bout onset neurons
Input((k/2+1):k,indPsyl) = HowOnPsyl(t); % input to psyl neurons
bdyn = double(rand(p.n,p.nsteps)>=(1-p.pin)); % Random activation
bdyn(1:k,:) = Input;
bdyn(:,indOff) = -HowClamped; % clamp all neurons between bouts
p.input = bdyn;
% run one iteration
[w xdyn] = HVCIter(p);
% save certain iterations for plotting later
switch t
case nIterEarly
wEarly = w;
xdynEarly = xdyn;
trainingNeuronsEarly{1}.tind = indBstart+bOnOffset;
trainingNeuronsEarly{2}.tind = ...
setdiff(indPsyl, indBstart+bOnOffset);
case nIterProto;
wProto = w;
xdynProto = xdyn;
trainingNeuronsProto{1}.tind = indBstart+bOnOffset;
trainingNeuronsProto{2}.tind = ...
setdiff(indPsyl, indBstart+bOnOffset);
case nIterProto + nIterPlotSplit1;
wSplit1 = w;
xdynSplit1 = xdyn;
trainingNeuronsSplit1{1}.tind = indBstart+bOnOffset;
trainingNeuronsSplit1{2}.tind = ...
setdiff(indPsyl, indBstart+bOnOffset);
case nIterProto + nIterPlotSplit2;
wSplit2 = w;
xdynSplit2 = xdyn;
trainingNeuronsSplit2{1}.tind = indBstart+bOnOffset;
trainingNeuronsSplit2{2}.tind = ...
setdiff(indPsyl, indBstart+bOnOffset);
end
end
%% Bout-onset differentiation: plotting parameters
figure(2)
isEPS = 0;
clf
set(gcf, 'color', ones(1,3));
if isEPS
PlottingParams.msize = 8; % change to what is best for EPS figure
PlottingParams.linewidth = .25;
set(0,'defaultAxesFontName', 'Arial')
set(0,'defaultTextFontName', 'Arial')
PlottingParams.labelFontSize = 7;
set(gcf, 'units','centimeters', 'position', [5 5 13.5 6])
else
PlottingParams.msize = 10;
PlottingParams.linewidth = .25;
PlottingParams.labelFontSize = 7;
end
PlottingParams.SeedColor = [.95 .5 1];
PlottingParams.Syl1Color = [0 0 1];
PlottingParams.Syl2Color = [1 0 0];
PlottingParams.Syl1BarColor = [0 0 1];
PlottingParams.Syl2BarColor = [1 0 0];
PlottingParams.numFontSize = 5;
PlottingParams.wplotmin = 0;
PlottingParams.wplotmax = 2; % this should be wmaxSplit
PlottingParams.wprctile = 0; % plot all weights above this percentile.
PlottingParams.totalPanels = 4;
PlottingParams.thisPanel = 1;
PlottingParams.sortby = 'weightMatrix';
%% Bout-onset differentiation: plotting early network activity
trainingNeuronsEarly{1}.nIDs = 1:k/2;
trainingNeuronsEarly{2}.nIDs = (k/2+1):k;
trainingNeuronsEarly{1}.candLat = (-bOnOffset+1):p.trainint;
trainingNeuronsEarly{2}.candLat = 1:p.trainint;
trainingNeuronsEarly{1}.thres = 4;
trainingNeuronsEarly{2}.thres = 6;
PlottingParams.thisPanel = 1;
PlottingParams.Hor = 0;
pp1 = PlottingParams;
pp1.Syl1BarColor = [1 1 1];
pp1.Syl2BarColor = [.5 .5 .5];
plotHVCnet_boutOnset(wEarly, xdynEarly, trainingNeuronsEarly, pp1)
PlottingParams.Hor = 1;
%% Bout-onset differentiation: plotting protosyllable
trainingNeuronsProto{1}.nIDs = 1:k/2;
trainingNeuronsProto{2}.nIDs = (k/2+1):k;
trainingNeuronsProto{1}.candLat = (-bOnOffset+1):p.trainint;
trainingNeuronsProto{2}.candLat = 1:p.trainint;
trainingNeuronsProto{1}.thres = 4;
trainingNeuronsProto{2}.thres = 6;
PlottingParams.thisPanel = 2;
plotHVCnet_boutOnset(wProto, xdynProto, ...
trainingNeuronsProto, PlottingParams)
%% Bout-onset differentiation: plotting splitting stages
trainingNeuronsSplit1{1}.nIDs = 1:k/2;
trainingNeuronsSplit1{2}.nIDs = (k/2+1):k;
trainingNeuronsSplit1{1}.candLat = (-bOnOffset+1):p.trainint;
trainingNeuronsSplit1{2}.candLat = 1:p.trainint;
trainingNeuronsSplit1{1}.thres = 4;
trainingNeuronsSplit1{2}.thres = 6;
PlottingParams.thisPanel = 3;
plotHVCnet_boutOnset(wSplit1, xdynSplit1, ...
trainingNeuronsSplit1, PlottingParams)
trainingNeuronsSplit2{1}.nIDs = 1:k/2;
trainingNeuronsSplit2{2}.nIDs = (k/2+1):k;
trainingNeuronsSplit2{1}.candLat = (-bOnOffset+1):p.trainint;
trainingNeuronsSplit2{2}.candLat = 1:p.trainint;
trainingNeuronsSplit2{1}.thres = 4;
trainingNeuronsSplit2{2}.thres = 6;
PlottingParams.thisPanel = 4;
plotHVCnet_boutOnset(wSplit2, xdynSplit2, ...
trainingNeuronsSplit2, PlottingParams)
%% Bout-onset differentiation: exporting
if isEPS
cd('Z:\Fee_lab\Papers\HVC_differentiation\Figures\EPS_files');
export_fig(2,'Figure5h.eps','-transparent','-eps','-painters');
else
figw = 6;
figh = 4;
set(gcf, 'color', [1 1 1],...
'papersize', [figw figh], 'paperposition', [0 0 figw*.9 figh])
%print -dmeta -r150
end
end
%% *HVCIter function*
function [w xdyn] = HVCIter(p)
% Runs one iteration of the simulation. p is a structure of parameters.
% redefining params that are used often outside the for loop
nsteps = p.nsteps;
n = p.n;
m = p.m;
w = p.w;
wmax = p.wmax;
Wmax = wmax*m;
eta = p.eta;
bdyn=p.input;
% initializing variables
xdyn=zeros(n,nsteps);
oldx = zeros(n,1);
oldy = zeros(n,1);
for t = 1:nsteps
% Adaptation
y = oldy + 1/p.tau*(-oldy+oldx);
Aadapt = p.alpha * y; % adaptation
% Net feedforward input.
B = bdyn(:,t); % external input
AE = w*oldx; % excitatory input
AIff = p.beta * sum(oldx); % feed forward inhibition
Anet = AE - AIff - Aadapt + B; % net feed forward input
Anet(Anet < 0) = 0; % rectify
% recurrent inhibition
AIrec = p.gamma * sum(Anet);
% binary output
x = (Anet - AIrec) > 0;
% STDP rule (Fiete et al 2010)
dw_STDP = eta.*(x*(oldx)'-(oldx)*x');
% Hetersynaptic penalty (Fiete et al 2010)
dw_hLTDpre = ...
eta*ones(n,1)*max(0, sum(w+dw_STDP,1)-Wmax); % Weights leaving
% cells (pre)
dw_hLTDpost = ...
eta*max(0, sum(w+dw_STDP,2)-Wmax)*ones(1,n); % Weights onto
% cells (post)
% Update weights
if eta>0
dwtotal = dw_STDP-p.epsilon*(dw_hLTDpre+dw_hLTDpost);
w = w + dwtotal;
w(w > wmax) = wmax; % hard limit on strength of a single synapse
w(w < 0) = 0; % weights cannot be negative
w = w.*(~eye(p.n)); % clamp diagonal
end
oldx = double(x);
oldy = y;
xdyn(:,t)=x;
end
end
%% Plotting functions: *findLatency*
function Latency = findLatency(xsort, trainingNeurons)
% Calculates the mode latency of each neuron
% w: weight matrix
% xdyn: activity of network
% m: duration of one syllable, in timesteps
% trainingNeurons: cell array of structures containing
% neuron and time indices for each syllable type
% PlottingParams: sets linewidth, etc.
nsteps = size(xsort,2);
n = size(xsort,1);
% iterate over candidate latencies
for syli = 1:length(trainingNeurons)
nFired = zeros(n,length(trainingNeurons{syli}.candLat));
for lati = 1:length(trainingNeurons{syli}.candLat);
tInds = zeros(1,nsteps);
tInds(min(nsteps, ...
trainingNeurons{syli}.tind + ...
trainingNeurons{syli}.candLat(lati))-1) = 1;
nFired(:,lati) = ...
sum(bsxfun(@times, xsort, tInds),2); % number of times each
% neuron fired at this
% latency
end
for ni = 1:n
[~, Latency{syli}.mode(ni)] = max(nFired(ni,:));
Latency{syli}.mode(ni) = ...
trainingNeurons{syli}.candLat(Latency{syli}.mode(ni));
Latency{syli}.FireDur(ni) = ...
max(nFired(ni,:))>trainingNeurons{syli}.thres;
end
end
end
%% Plotting functions: *plotAlternating*
function plotAlternating(w, xsort, m, trainingNeurons, PlottingParams)
% Makes network activity plot, called by AlternatingDifferentiation
% w: weight matrix
% xsort: activity of network
% m: duration of one syllable, in timesteps
% trainingNeurons: cell array of structures containing
% neuron and time indices for each syllable type
% PlottingParams: sets linewidth, etc.
Syl1Color = PlottingParams.Syl1Color;
Syl2Color = PlottingParams.Syl2Color;
ProtoSylColor = PlottingParams.ProtoSylColor;
numFontSize = PlottingParams.numFontSize;
labelFontSize = PlottingParams.labelFontSize;
Latency = findLatency(xsort, trainingNeurons);
% plotting the mode latency for each syll type
xplot = zeros(size(w,1),2*m);
for ni = 1:size(w,1)
if Latency{1}.FireDur(ni) & ~isnan(Latency{1}.mode(ni))
xplot(ni,Latency{1}.mode(ni)) = 1;
end
if Latency{2}.FireDur(ni) & ~isnan(Latency{2}.mode(ni))
xplot(ni,Latency{2}.mode(ni)+m) = 1;
end
end
% keep track of which neurons participated in each syllable
FireDur1 = Latency{1}.FireDur;
FireDur2= Latency{2}.FireDur;
% classify neurons as specific or shared
Specific1 = FireDur1&~FireDur2;
Specific2 = FireDur2&~FireDur1;
Shared = (FireDur1&FireDur2);
indshared = find(Shared);
Red = trainingNeurons{1}.nIDs;
Green = trainingNeurons{2}.nIDs;
if issame(Red,Green)
IsProto = zeros(1,length(xplot)); IsProto(Red) = 1;
Red = [];
Green = [];
else
IsProto = zeros(1,length(xplot));
end
IsTrain1 = zeros(1,length(xplot)); IsTrain1(Red) = 1;
IsTrain2 = zeros(1,length(xplot)); IsTrain2(Green) = 1;
if length(Green)==0
tmp= xplot>0;
tmpXplot = tmp(:,1:(size(xplot,2)/2))+tmp(:,(size(xplot,2)/2+1):end);
else
tmpXplot = xplot>0;
end
[~,sortind] = sortrows(tmpXplot);
xplot = xplot(flipud(sortind),:); % from early (top) to late (bottom)
w = w(flipud(sortind),flipud(sortind));
IsTrain1 = IsTrain1(flipud(sortind));
IsTrain2 = IsTrain2(flipud(sortind));
Specific1 = Specific1(flipud(sortind));
Specific2 = Specific2(flipud(sortind));
IsProto = IsProto(flipud(sortind));
% if differentiated, sort shared neurons first, then specific neurons
if length(Green)>0
sharedind = (sum(xplot,2)>=2)&(sum(xplot,2)<8);
else
sharedind = zeros(1,size(xplot,1));
end
rest = find(~sharedind);
xplotall = xplot([find(sharedind); (rest)],:);
IsTrain1 = IsTrain1([find(sharedind); (rest)]);
IsTrain2 = IsTrain2([find(sharedind); (rest)]);
Specific1 = Specific1([find(sharedind); (rest)]);
Specific2 = Specific2([find(sharedind); (rest)]);
IsProto = IsProto([find(sharedind); (rest)]);
% plotting the activity for the two syll types
for i = 1:2
axesPos = PlottingParams.axesPosition;
axesPos(1) = axesPos(1)+(i-1)*axesPos(3)/2;
axesPos(3) = axesPos(3)*.3;
subplot('position', axesPos);
xplot = ...
xplotall(:,(1:(size(xplotall,2)/2))+(i-1)*(size(xplotall,2)/2));
tplot = (1:(size(xplot,2)))*10; % assuming each bin is 10ms
tOffset = 0;
for j=1:size(xplot,2) % for all the time steps
Idx = ...
find(xplot(1:end-1,j)>0); % find the indices of active neurons
if ~isempty(Idx)
for k=1:length(Idx) % for all the active neurons
Color = IsProto(Idx(k))*PlottingParams.ProtoSylColor + ...
Specific1(Idx(k))*PlottingParams.Syl1Color + ...
Specific2(Idx(k))*PlottingParams.Syl2Color;
h = patch(10*([j-1,j,j,j-1]+tOffset),...
[Idx(k)-1,Idx(k)-1,Idx(k),Idx(k)],...
Color,'edgecolor','none');
end
end
end
hold on
% plot line between each syl type
if length(Green)>0
train2 = find(IsTrain2); train2 = train2(1)-1;
plot([0 size(xplot,2)*10], [train2 train2], 'k', ...
'linewidth', PlottingParams.linewidth)
end
% plot line between shared and specific neurons
if length(rest)>0 & sum(sharedind)>0 & length(Green)>0
plot([0 size(xplot,2)*10], [sum(sharedind) sum(sharedind)],...
'k', 'linewidth', PlottingParams.linewidth)
end
% plot colored bars above each syllable
if length(Green) == 0
patch([0 90 90 0],[-4 -4 -2 -2],PlottingParams.ProtoSylBarColor);
text(40,-10,'\alpha','fontsize',7);
elseif i == 1
patch([0 90 90 0],[-4 -4 -2 -2],Syl1Color);
text(40,-10,'\beta','fontsize',7)
else
patch([0 90 90 0],[-4 -4 -2 -2],Syl2Color);
text(40,-10,'\gamma','fontsize',7);
end
% plotting parameters
ylim([-5 size(xplot,1)-1]);
box off
set(gca, 'ydir', 'reverse','tickdir','out',...
'ticklength',[0.015 0.015], 'color', 'none', ...
'xtick', 0:50:100,'fontsize', numFontSize,'tickdir','out');
xlim([-2 100]);
if PlottingParams.thisPanel==1
ylabel('Neuron #', 'fontsize', labelFontSize,'fontname','arial');
set(gca,'ytick',0:20:100,'fontsize',numFontSize)
else
set(gca,'ytick',0:20:100,'yticklabel', {})
end
end
end
%% Plotting functions: *plotHVCnet*
function plotHVCnet(w, xdyn, m, trainingNeurons, PlottingParams)
% Makes network diagram for alternating differentiation
% w: weight matrix
% xdyn: activity of network
% m: duration of one syllable, in timesteps
% trainingNeurons: cell array of structures containing
% neuron and time indices for each training neuron type
% PlottingParams: sets linewidth, etc.
msize = PlottingParams.msize;
linewidth = PlottingParams.linewidth;
Syl1Color = PlottingParams.Syl1Color;
Syl2Color = PlottingParams.Syl2Color;
ProtoSylColor = PlottingParams.ProtoSylColor;
Latency = findLatency(xdyn, trainingNeurons);
% first exclude all neurons that don't fire at a consistent phase
cla; hold on
x = zeros(1,size(w,1));
y = zeros(1,size(w,1));
for ni = 1:size(w,1)
% if it fired during either syll
if Latency{1}.FireDur(ni)|Latency{2}.FireDur(ni)
% if it fired during both sylls
if (Latency{1}.FireDur(ni)&Latency{2}.FireDur(ni))
% if fired during both sylls at same phase
if (Latency{1}.mode(ni)==Latency{2}.mode(ni))
x(ni) = Latency{1}.mode(ni);
else % exclude from plot if different phases for both sylls
x(ni) = NaN;
end
elseif Latency{1}.FireDur(ni) % if it fired during syll 1
x(ni) = Latency{1}.mode(ni);
else % fired during syll 2 only
x(ni) = Latency{2}.mode(ni);
end
else % if it fired during neither syll
x(ni) = NaN;
end
end
indkeep = ~isnan(x);
y = y(indkeep);
w = w(indkeep,indkeep);
xdyn = xdyn(indkeep,:);
x = x(indkeep);
ux = unique(x);
% keep track of training neuron and syl time indices
trainingset1 = trainingNeurons{1}.nIDs;
trainingset2 = trainingNeurons{2}.nIDs;
x(trainingset1) = 1;
x(trainingset2) = 1;
tind1 = (trainingNeurons{1}.tind);
tind2 = (trainingNeurons{2}.tind);
% keep track of which neurons participated in each syllable
FireDur1 = Latency{1}.FireDur(indkeep);
FireDur2= Latency{2}.FireDur(indkeep);
% classify neurons as specific or shared
Specific1 = FireDur1&~FireDur2;
Specific2 = FireDur2&~FireDur1;
Shared = (FireDur1&FireDur2);
indshared = find(Shared);
% calculate the incoming weights from specific neurons of each type, to
% determine sorting in y axis and color
c1 = zeros(1,length(x));
c2 = zeros(1,length(x));
for ni = 1:size(w,1)
tmp = find(xdyn(ni,:));
if sum(w(ni,:))>0
c1(ni) = sum(w(ni,Specific1))/sum(w(ni,:));
c2(ni) = sum(w(ni,Specific2))/sum(w(ni,:));
end
y(ni) = c1(ni)-c2(ni);
end
% for each latency (x), sort along y, with small gap between shared and
% specific neurons
y1 = zeros(1,size(w,1));
for ui = 1:length(ux)
indshared = (x==ux(ui))&Shared;
ind1 = (x==ux(ui))&Specific1;
ind2 = (x==ux(ui))&Specific2;
[~,y1(indshared)] = sort(y(indshared));
tocentershared = 1+(numel(find(indshared))-1)/2;
y1(indshared) = y1(indshared)-tocentershared;
[~,y1(ind1)] = sort(y(ind1));
y1(ind1) = y1(ind1) + (numel(find(indshared)))/2;
[~,y1(ind2)] = sort(y(ind2));
y1(ind2) = y1(ind2) -numel(find(ind2))-1- (numel(find(indshared)))/2;
end
cla; hold on
% keep only feedforward part of weight matrix
wplot = w;
n = size(wplot,1);
for i = 1:n
for j = 1:n
ff = x(i)<x(j);
longrange = abs(x(i)-x(j))>2;
if (~ff) | longrange
wplot(j,i) = 0;
end
end
end
% Color weights white to black between wplotmin and wplotmax
wplot = wplot-PlottingParams.wplotmin;
wplot(wplot<0) = 0;
wplot = wplot/(PlottingParams.wplotmax-PlottingParams.wplotmin);
wplot(wplot<prctile(wplot(:), PlottingParams.wprctile)) = 0;
wplotold = wplot;
n = size(wplot,1);
for i = 1:n
[~,ind] = sort(wplot(:,i), 'descend');
indplot = zeros(n,1);
indplot(ind(1:min(PlottingParams.wperneuron,length(ind)))) = 1;
wplot(~indplot,i) = 0;
end
for i = 1:n
if sum(wplot(i,:)>0)<PlottingParams.wperneuronIn
[~,ind] = sort(wplotold(i,:), 'descend');
wplot(i,ind(1:min(PlottingParams.wperneuron,length(ind)))) = ...
wplotold(i,ind(1:min(PlottingParams.wperneuron,length(ind))));
end
end
% jitter a little in x and y, so it doesn't look like a grid, but
% don't jitter seed neurons
jitter = .1;
Seed0 = randn(1,300);
indJitter = setdiff(1:length(x), union(trainingset1, trainingset2));
x(indJitter)= x(indJitter)+jitter*Seed0(1:length(x(indJitter)));
y1(indJitter) = ...
y1(indJitter)+...
jitter*Seed0((length(x(indJitter))+1):(2*length(x(indJitter))));
% plot w in order from weakest to strongest, so darker lines are on top
js = repmat((1:n)',1,n);
is = repmat((1:n),n,1);
isVec = is(:);
jsVec = js(:);
wVec = wplot(:);
[wSort,indSort] = sort(wVec, 'ascend');
nplotted = zeros(1,n);
for k = 1:length(wSort)
i = isVec(indSort(k));
j = jsVec(indSort(k));
if wplot(j,i)>0
ff = x(i)<=x(j);
longrange = abs(x(i)-x(j))>2;
loopback = (round(x(i))==round(max(x)))&...
(round(x(j))==round(min(x)));
if (ff & ~longrange)%|loopback
C = ones(1,3)-wplot(j,i)*ones(1,3);
plot([x(i), x(j)], [y1(i),y1(j)], ...
'color', C, 'linewidth', linewidth)
end
end
end
% color each neuron based on its relative input from each syllable type
for pli = 1:length(x)
tmpC = c1(pli)'/(max(c1)+eps)*Syl1Color+c2(pli)'/(max(c2)+eps)*Syl2Color;
tmpC = tmpC/(max(tmpC)+eps); % normalize so colors are bright
if Shared(pli)
tmpC = zeros(1,3);
end
if Specific1(pli)
tmpC = Syl1Color;
end
if Specific2(pli)
tmpC = Syl2Color;
end
plot(x(pli),y1(pli), 'marker', '.', 'color', tmpC, 'markersize', msize)
end
% plot training neurons in given colors
if sum(Specific1)>0
plot(x(trainingset1),y1(trainingset1), ...
'.', 'markersize', msize, 'color', Syl1Color)
plot(x(trainingset2),y1(trainingset2), ...
'.', 'markersize', msize, 'color', Syl2Color)
else
plot(x([trainingset1 trainingset2]),y1([trainingset1 trainingset2]),...
'.', 'markersize', msize, 'color', ProtoSylColor)
end
% plot rectangle for syl1 seed neurons
rx = 1-.5;
ry = min(y1(trainingset1))-.5;
rw = 1;
rh = max(y1(trainingset1)) - min(y1(trainingset1))+1;
rectangle('Position', [rx ry rw rh], ...
'FaceColor', 'none',...
'LineStyle', '-', 'LineWidth', .5, ...
'EdgeColor', PlottingParams.SeedColor, ...
'curvature', [.98 .1])
% plot rectangle for syl2 seed neurons
rx = 1-.5;
ry = min(y1(trainingset2))-.5;
rw = 1;
rh = max(y1(trainingset2)) - min(y1(trainingset2))+1;
rectangle('Position', [rx ry rw rh], ...
'FaceColor', 'none',...
'LineStyle', '-', 'LineWidth', .5, ...
'EdgeColor', PlottingParams.SeedColor, ...
'curvature', [.98 .1])
axis tight; axis off;
xlim([-.5 m+.5]);
ylim([min(y1)-1 max(y1)+1])
set(gca, 'color', 'none')
end
%% Plotting functions: *plotHVCnet_boutOnset*
function plotHVCnet_boutOnset(w, xdyn, trainingNeurons, PlottingParams)
% Makes network diagram and raster plots, called by RunHVC_boutOnset_net
% w: weight matrix
% xdyn: activity of network
% m: duration of one syllable, in timesteps
% trainingNeurons: cell array of structures containing
% neuron and time indices for each syllable type
% PlottingParams: sets linewidth, etc. See RunHVC_split
%plotting parameters%
msize = PlottingParams.msize;
linewidth = PlottingParams.linewidth;
Syl1Color = PlottingParams.Syl1Color;
Syl2Color = PlottingParams.Syl2Color;
numFontSize = PlottingParams.numFontSize;
labelFontSize = PlottingParams.labelFontSize;
nplots = PlottingParams.totalPanels;
ploti = PlottingParams.thisPanel;
%Network diagram%
subplot('position', [ploti/nplots-.9/nplots, .56, .9/nplots, .44])