-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkde_decoder.m
1550 lines (1299 loc) · 72.5 KB
/
kde_decoder.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
classdef kde_decoder < handle
%KDE_DECODER class for generalized bayesian decoding
%
% obj=KDE_DECODER(time,stimulus,spiketimes,spikestimulus) constructs a new
% kde_decoder object. Time is an ordered vector of times at which the
% stimulus (e.g. position, or head direction) is sampled. The stimulus
% argument is a 2D array in which rows represent time and columns
% represent the stimulus dimensions. The spiketimes argument is a cell
% array with in each cell a vector of spike times from a single source
% (such as a cluster or a tetrode). The spikestimulus argument is a cell
% array with the corresponding stimulus for each of the sources.
%
% obj=KDE_DECODER(time,stimulus,spiketimes,spikestimulus,spikeresponse)
% also specifies the responses (e.g. spike amplitude) for each of the
% sources. The spikeresponse argument should be a cell array with in each
% cell a 2D array of responses.
%
% obj=KDE_DECODER(...,parm1,val1,...) initialize parameters using optional
% keyword/value pairs. Valid parameters are:
% encoding_segments - nx2 list of time epochs. Only stimulus data and
% spike data recorded during these epochs is used to
% construct the kernel density estimate. (default:
% epoch defined by start and end time of the time
% vector provided during object construction)
% stimulus_variable_type - string or cell array of strings with for each
% stimulus dimension the type of variable, which
% can be one of: 'linear', 'circular' or
% 'categorical'. (Default: 'linear')
% stimulus_grid - cell array with for each stimulus dimension a vector of
% stimulus values at which the kde is evaluated.
% (default: [])
% stimulus_grid_validity - logical matrix the same size as final PDF
% matrix in which all true elements are deemed
% invalid and are not computed.
% stimulus_kernel - string or cell string array with for each stimulus
% dimension the type of kernel to use. Choose from:
% 'gaussian', 'epanechnikov', 'vonmises', 'kronecker'.
% Linear variables only support gaussian, epanechnikov
% and kronecker delta kernels; circular variables only
% support von Mises kernels; categorical variables only
% support kronecker delta kernels with a fixed
% bandwidth of 0.5. (Default: gaussian)
% stimulus_bandwidth - scalar or vector of kernel bandwidths for all
% stimulus dimensions. For the gaussian kernel, the
% bandwidth specifies the standard deviation; for
% the von Mises kernel the bandwidth specifies the
% concentration parameter kappa; for the
% epanechnikov and kronecker delta kernels, the
% bandwidth specifies the half width of the total
% kernel; for categorical variables using the
% kronecker delta kernel, the bandwidth is fixed at
% 0.5. (Default: 1)
% response_variable_type - string or cell array of strings with for each
% response dimension the type of variable, which
% can be one of: 'linear', 'circular' or
% 'categorical'. (Default: 'linear')
% response_kernel - string or cell string array with for each response
% dimension the type of kernel to use. Choose from:
% 'gaussian', 'epanechnikov', 'vonmises' or
% 'kronecker'. (Default: gaussian)
% response_bandwidth - scalar, vector or cell array with vectors
% specifying kernel bandwidths for all response
% dimensions. In case of cell array, cells
% specify bandwidths for each source.
% source_selection - a vector with for each source 1 or 0, indicating
% whether or not the source should be included in the
% computation. (Default: all 1)
% response_filter - handle to a function that takes the response matrix
% and the source index as input arguments and returns a
% vector with for each response (i.e. each row) true or
% false, to indicate whether or not it should be
% included in the computation. This can for example be
% used to select on spike amplitude or width.
% (Default: @(x,src) true(size(x,1),1) )
% response_filter_post -
% response_transformation - handle to a function that takes the response
% matrix and the source index as input
% arguments and returns a same-size matrix with
% transformed responses. (Default: @(x,src) x)
% response_selection - scalar, vector or cell array of vectors specifying
% which of the response dimensions will be used in
% the computation. In case of a cell array, cells
% specify the response selection for each source.
% (Default: 1)
% response_randomization - true/false. If true, spike response are
% randomized before computing the kde.
% distance - cell array with for each stimulus dimension either en empty
% array (standard eucledian distance will be used) or a square
% matrix with distance between sampled stimulus values. If for
% a given stimulus dimension a distance matrix is provided,
% then corresponding columns in the stimulus and spikestimulus
% arrays should contain indices of the sampled stimulus value.
% For stimulus dimensions with a discrete kernel, the distance
% matrix is ignored.
% (Default: [])
% rate_modulation - rate modulation factor (Default: 1)
% rate_offset - offset for rate functions (Default: 0.01Hz)
% parallel - true/false, parallelize computation. In some case this may
% speed up the computation if you have configured matlab to
% use multiple cores, in other cases the overhead of using
% parallelization could slow down the computation (in
% particular if you are computing the posterior very often
% with small test data sets). (Default: true)
%
properties (Constant=true)
kernel_types = {'gaussian', 'epanechnikov', 'vonmises', 'kronecker'};
variable_types = {'linear', 'circular', 'categorical'};
end
properties (SetAccess=protected)
training_time
training_stimulus % Z x Q
spike_time
spike_stimulus % cell array, Ni x Q
spike_response % cell array, Ni x Di
distance_lut % length Q cell array with square matrices
stimulus_variable_type
response_variable_type
state = struct( 'stimulus_kernel', [], ...
'response_kernel', {{}}, ...
'stimulus_bandwidth', [], ...
'response_bandwidth', {{}}, ...
'source_selection', [], ...
'response_selection', {{}}, ...
'stimulus_grid', [], ...
'stimulus_grid_validity', [], ...
'response_randomization', false, ...
'response_transformation', @(x) x, ...
'response_filter', @(x) true(size(x,1),1), ...
'response_filter_post', @(x) true(size(x,1),1), ...
'encoding_segments', zeros(0,2), ...
'rate_modulation', 1, ...
'rate_offset', 0.01)
stimulus_grid_expanded
end
properties (Access=protected, Hidden=true)
initialized = false;
index_cache = struct( 'state', struct('encoding_segments', NaN), 'training', NaN, 'spike', NaN )
response_filter_cache = struct( 'state', struct('response_filter', NaN), 'data', NaN )
spike_stimulus_cache = struct( 'state', struct('stimulus_kernel', NaN, 'stimulus_bandwidth', NaN, 'encoding_segments', NaN, 'response_filter', NaN), 'data', NaN );
spike_response_cache = struct( 'state', struct('response_transformation', NaN, 'response_kernel', NaN, 'response_bandwidth', NaN, 'encoding_segments', NaN, 'response_filter', NaN), 'data', NaN );
distance_cache = struct( 'state', struct('stimulus_kernel', NaN, 'stimulus_bandwidth', NaN), 'data', NaN );
marginal_cache = struct( 'state', struct('stimulus_kernel', NaN, 'stimulus_bandwidth', NaN), 'stimulus', NaN, 'spike_stimulus', NaN );
flag_index_cache = true;
flag_response_filter_cache = true;
flag_stimulus_cache = true;
flag_response_cache = true;
flag_distance_cache = true;
flag_marginal_cache = true;
end
properties (Dependent=true)
response_randomization
response_transformation
response_filter
response_filter_post
stimulus_kernel
response_kernel
stimulus_bandwidth
response_bandwidth
source_selection
response_selection
stimulus_grid
stimulus_grid_validity
encoding_segments
rate_modulation
rate_offset
end
properties (Dependent=true, SetAccess=protected)
ndim_stimulus
ndim_response
nsources
nspikes
mean_rate
stimulus_grid_size
current_state
training_duration
stimulus_marginal
spike_stimulus_marginal
end
properties
parallel = true
end
methods
%CONSTRUCTOR
function obj=kde_decoder( varargin )
obj.initialized = false;
%parse keyword/value arguments
options = struct( 'encoding_segments', [-Inf Inf], 'distance', [], 'stimulus_kernel', 'gaussian', 'stimulus_bandwidth', 1, 'response_kernel', 'gaussian', 'response_bandwidth', 1, ...
'source_selection', true, 'response_selection', true, 'response_randomization', false, 'response_transformation', [], 'response_filter', [], 'response_filter_post', [], ...
'stimulus_grid', [], 'stimulus_grid_validity', [], 'parallel', true, 'rate_modulation', 1, 'rate_offset', 0.01, 'stimulus_variable_type', 'linear', 'response_variable_type', 'linear' );
[options,other] = parseArgs( varargin, options );
%check non-keyword, immutable arguments
if numel(other)<4
err = MException('kde_decoder:kde_decoder:invalidArgument', 'Need at least 4 arguments');
throw(err);
end
if ~isnumeric(other{1}) || ~isvector(other{1}) || isempty(other{1}) || ~issorted(other{1})
err = MException('kde_decoder:kde_decoder:invalidArgument','Training time should be a vector');
throw(err);
else
obj.training_time = other{1}(:);
end
if ~isnumeric(other{2}) || ndims(other{2})~=2 || size(other{2},1)~=numel(obj.training_time)
err = MException('kde_decoder:kde_decoder:invalidArgument','Training stimulus should be a 2D array');
throw(err);
else
obj.training_stimulus = other{2};
end
ndim_stim = size(obj.training_stimulus,2);
if ~iscell(other{3}) || ~isvector(other{3}) || any( cellfun( @(x) ~isnumeric(x) || ~isvector(x) || isempty(x) || ~issorted(x), other{3} ) )
err = MException('kde_decoder:kde_decoder:invalidArgument','Spike time should be a cell array with vectors');
throw(err);
else
obj.spike_time = other{3}(:);
end
nsrc = numel(obj.spike_time);
if ~iscell(other{4}) || ~isvector(other{4}) || numel(other{4})~=nsrc || any( cellfun( @(x) ~isnumeric(x) || ndims(x)~=2 || isempty(x) || size(x,2)~=ndim_stim, other{4} ) )
err = MException('kde_decoder:kde_decoder:invalidArgument','Spike stimulus should be a cell array with 2D arrays');
throw(err);
else
obj.spike_stimulus = other{4}(:);
end
if numel(other)<5 || isempty(other{5})
obj.spike_response = cell( nsrc, 1 );
for j=1:nsrc
obj.spike_response{j} = zeros( size(obj.spike_stimulus{j},1) , 0 );
end
elseif ~iscell(other{5}) || ~isvector(other{5}) || numel(other{5})~=nsrc || any( cellfun( @(x,y) ~isnumeric(x) || ndims(x)~=2 || size(x,1)~=size(y,1), other{5}(:), obj.spike_stimulus ) )
err = MException('kde_decoder:kde_decoder:invalidArgument','Spike response should be a cell array with 2D arrays');
throw(err);
else
obj.spike_response = other{5}(:);
end
%set keyword parameters
obj.encoding_segments = options.encoding_segments;
%variable types are immutable, so let's check it here
if isempty(options.stimulus_variable_type)
obj.stimulus_variable_type = ones(1,obj.ndim_stimulus);
elseif ischar( options.stimulus_variable_type ) || (iscellstr(options.stimulus_variable_type) && numel(options.stimulus_variable_type)==obj.ndim_stimulus)
val = obj.validate_vartypes( options.stimulus_variable_type );
obj.stimulus_variable_type = ones(1,obj.ndim_stimulus).*val(:)';
else
err = MException('kde_decoder:kde_decoder:invalidArgument','Variable type should be specified as a string or cell array of strings');
throw(err);
end
%let's round any categorical stimulus dimensions to integers
idx = obj.stimulus_variable_type==3;
obj.training_stimulus(:,idx) = round( obj.training_stimulus(:,idx) );
for j=1:nsrc
obj.spike_stimulus{j}(:,idx) = round( obj.spike_stimulus{j}(:,idx) );
end
val = options.response_variable_type;
if ischar(val) || isempty(val)
val = obj.validate_vartypes( val );
for j=1:nsrc
obj.response_variable_type{j,1} = ones(1,obj.ndim_response(j)).*val;
end
elseif iscellstr(val) && isequal( size(val), [1 obj.ndim_response(1)]) && isscalar( unique( obj.ndim_response ) )
val = obj.validate_vartypes( val );
obj.response_variable_type = cell(nsrc,1);
[obj.response_variable_type{1:nsrc,1}] = deal(val);
elseif iscellstr(val) && isequal( size(val), [nsrc obj.ndim_response(1)]) && isscalar( unique( obj.ndim_response ) )
val = obj.validate_vartypes( val );
for j=1:nsrc
obj.response_variable_type{j,1} = val(j,:);
end
elseif iscell(val) && isequal( size(val), [nsrc 1] )
for j=1:nsrc
tmp = obj.validate_vartypes( val{j} );
if ~isscalar(tmp) && numel(tmp)~=obj.ndim_response(j)
err = MException('kde_decoder:kde_decoder:invalidArgument','Invalid response variable type');
throw(err);
end
obj.response_variable_type{ j,1 } = ones(1,obj.ndim_response(j)).*tmp(:)';
end
else
err = MException('kde_decoder:kde_decoder:invalidArgument','Invalid response variable type');
throw(err)
end
%let's round any categorical response dimensions to integers
for j=1:nsrc
idx = obj.response_variable_type{j}==3;
obj.spike_response{j}(:,idx) = round( obj.spike_response{j}(:,idx) );
end
%distance LUT is immutable, hence we check it here
if isempty(options.distance)
obj.distance_lut = cell(1,ndim_stim);
elseif ~iscell(options.distance) || ~isvector(options.distance) || numel(options.distance)~=ndim_stim || any( cellfun(@(x) (ndims(x)~=2 || size(x,1)~=size(x,2)) && ~isempty(x), options.distance ) )
err = MException('kde_decoder:kde_decoder:invalidArgument', 'Invalid distance lookup tables');
throw(err);
else
ni = cellfun( @(x) size(x,1), options.distance(:)' );
%categorical variable cannot have a distance matrix
idx = obj.stimulus_variable_type==3 & ni>0;
if sum(idx)>0
warning('kde_decoder:kde_decoder:invalidOption', 'Distance matrix is not supported for categorical stimulus dimensions')
[options.distance{idx}] = deal([]);
ni(idx) = 0;
end
%check validity of training_stimulus and spike_stimulus
%these should be indices into the distance LUTs
idx = ni>0;
if sum(idx)>0
%round to nearest integer
obj.training_stimulus(:,idx) = round( obj.training_stimulus(:,idx) );
for k=1:ndim_stim
obj.spike_stimulus{k}(:,idx) = round( obj.spike_stimulus{k}(:,idx) );
end
%check validity of indices
if any( any( bsxfun( @ge, obj.training_stimulus(:,idx), ni(idx) ) | obj.training_stimulus(:,idx)<0 ) ) || ...
any( cellfun( @(x) any( any( bsxfun( @ge, x(:,idx), ni(idx) ) | x(:,idx)<0 ) ), obj.spike_stimulus ) )
err = MException('kde_decoder:kde_decoder:invalidArgument', 'Index out of range for stimulus dimensions with a distance lookup table');
throw(err);
end
end
obj.distance_lut = options.distance;
end
obj.response_randomization = options.response_randomization;
obj.response_transformation = options.response_transformation;
obj.response_filter = options.response_filter;
obj.response_filter_post = options.response_filter_post;
obj.stimulus_kernel = options.stimulus_kernel;
obj.response_kernel = options.response_kernel;
obj.stimulus_bandwidth = options.stimulus_bandwidth;
obj.response_bandwidth = options.response_bandwidth;
obj.source_selection = options.source_selection;
obj.response_selection = options.response_selection;
obj.stimulus_grid = options.stimulus_grid;
obj.stimulus_grid_validity = options.stimulus_grid_validity;
obj.parallel = options.parallel;
obj.rate_modulation = options.rate_modulation;
obj.rate_offset = options.rate_offset;
end
%INFORMATION METHODS
function display(obj)
s = sprintf( 'kde decoder object with %d source%s', obj.nsources, plural(obj.nsources) );
disp(s)
s = sprintf( 'with %d stimulus dimension%s, evaluated on a %s size grid', obj.ndim_stimulus, plural(obj.ndim_stimulus), mat2str(obj.stimulus_grid_size) );
disp(s)
end
function info(obj)
disp('kde decoder object')
fprintf(' total data time = %0.2f seconds\n', diff( obj.training_time([1 end]) ) );
fprintf(' duration of encoding segments = %0.2f seconds (%0.1f%%) across %d segment%s\n', obj.training_duration, 100*obj.training_duration./diff( obj.training_time([1 end]) ), size(obj.encoding_segments,1), plural(size(obj.encoding_segments,1) ) );
disp(' ')
% # sources, number of spikes, mean rate, source selection
fprintf(' contains %d source%s\n', obj.nsources, plural(obj.nsources) );
fprintf(' selected sources = %s\n', mat2str( find( obj.source_selection(:)' ) ) );
fprintf(' # spikes per source = %s\n', mat2str( obj.nspikes(:)', 3 ) );
fprintf(' mean rate per source = %s\n', mat2str( obj.mean_rate(:)', 3 ) );
% rate modulation
tmp = unique( obj.rate_modulation );
if isscalar(tmp)
fprintf( ' rate modulation = %0.2f\n', tmp )
else
fprintf( ' rate modulation = %s\n', mat2str(obj.rate_modulation(:)',3) )
end
disp(' ')
%stimulus
% # dimensions, variable types, kernels, bandwidths, distance LUTs
fprintf(' stimulus has %d dimension%s\n', obj.ndim_stimulus, plural(obj.ndim_stimulus) );
fprintf(' %3s %12s %10s %10s %12s\n', 'dim', 'var. type', 'kernel', 'bandwidth', 'distance LUT' );
for k=1:obj.ndim_stimulus
fprintf(' %3d %12s %10s %10.3f %12s\n', k, obj.variable_types{ obj.stimulus_variable_type(k) }, obj.kernel_types{ obj.stimulus_kernel(k) }, obj.stimulus_bandwidth(k), onoff( isempty(obj.distance_lut{k}), { 'no' sprintf('yes (size=%d)', size(obj.distance_lut{k},1) ) } ) );
end
disp(' ')
% stimulus grid
fprintf( ' stimulus grid size = %s\n', mat2str(obj.stimulus_grid_size) );
disp(' ')
%response
% # dimensions, variable types, kernels, bandwidths
% response_selection
if all(obj.ndim_response==0)
fprintf(' no responses\n')
else
tmp = cat(2,obj.response_selection, obj.response_variable_type, obj.response_kernel, obj.response_bandwidth );
tmp_str = cellfun( @(x)(mat2str(x)), tmp, 'UniformOutput',false);
tmp_str2 = cell(obj.nsources,1);
for s=1:obj.nsources
tmp_str2{s} = [ tmp_str{s,:} ];
end
[~, jj, ii] = unique( tmp_str2 );
tmp = tmp(jj,:);
fprintf(' responses\n' );
for s=1:size(tmp,1)
fprintf(' source%s %s\n', plural(sum(ii==s)), mat2str(find(ii(:)'==s)));
fprintf(' %3s %12s %10s %10s\n', 'dim', 'var. type', 'kernel', 'bandwidth' );
for k=1:numel(tmp{s,1})
fprintf(' %s%2d %12s %10s %6.3f\n', onoff(tmp{s,1}(k), {'*', ' '}), k, obj.variable_types{ tmp{s,2}(k) }, obj.kernel_types{ tmp{s,3}(k) }, tmp{s,4}(k) );
end
fprintf('\n');
end
end
disp(' ')
fprintf(' randomization of responses = %s\n', onoff( obj.response_randomization ) );
fprintf(' response transformation function = %s\n', func2str( obj.response_transformation ) );
fprintf(' response filter function = %s\n', func2str( obj.response_filter ) );
fprintf(' response filter post function = %s\n', func2str( obj.response_filter_post ) );
disp(' ')
%rate offset
fprintf( ' rate offset = %0.3f\n', obj.rate_offset );
%functions used
s = cell(obj.nsources,1);
for j=1:obj.nsources
[~,s{j}] = kde_decoder.get_func( obj.stimulus_kernel, obj.response_kernel{j} );
end
s = unique(s);
fprintf( ' mex function%s used = %s\n', plural(numel(s)), sprintf( '%s ', s{:}))
%parallel
fprintf( ' parallelization = %s\n', onoff( obj.parallel && matlabpool('size')>1 ) )
disp(' ')
end
%COMPUTATION METHODS
function [P,E,info] = compute( obj, bins, varargin )
%check arguments
options = struct( 'use_marginal', false );
[options,other,remainder] = parseArgs( varargin, options );
%compute log(rate) and marginal rate for all sources
[P,M,info] = obj.compute_sources( bins, other{:}, remainder{:} );
%compute posterior distribution
P = obj.compute_posterior( P, M, diff( bins, [], 2 ) );
if nargout>1
%compute decoding performamce
if numel(other)==0
true_stim = interp1( obj.training_time, obj.training_stimulus, mean( bins, 2 ), 'nearest' );
E = obj.compute_performance( true_stim, P, options.use_marginal );
else
%user provided own spike times and responses, the true
%stimulus is (probably) unknown, so issue a warning
E = [];
warning('kde_decoder:compute:cannotCompute', 'Unable to compute performance if custom spike times/responses are provided')
end
end
end
function [P,M,info] = compute_sources(obj,bins,timestamp,testresponse)
%get which sources to compute
currentstate = obj.state(end);
src_idx = find( currentstate.source_selection );
%get stimulus grid and mask invalid elements
stimgrid = obj.stimulus_grid_expanded;
if ~isempty(currentstate.stimulus_grid_validity)
stimgrid( currentstate.stimulus_grid_validity,:) = NaN;
end
%get spike stimulus and response
sp_stimulus = get_spike_stimulus_cache(obj);
sp_stimulus = sp_stimulus( src_idx );
sp_response = get_spike_response_cache(obj);
sp_response = sp_response( src_idx );
%get distance matrices
dist_lut = get_distance_cache(obj);
nsrc = numel(src_idx);
if nargin==2
%use spike times and responses stored in object
timestamp = obj.spike_time( src_idx );
testresponse = obj.spike_response( src_idx );
flt_idx = obj.get_response_filter_cache();
flt_idx = flt_idx(src_idx);
else
%use spike times and responses provided in arguments
flt_idx = cell(nsrc,1);
end
%pre-allocate arrays
P = cell( nsrc, 1 );
M = cell( nsrc, 1 );
nspikes = zeros( nsrc, 1 );
ntestspikes = zeros( nsrc, 1 );
nrespdim = zeros( nsrc, 1 );
%get stimulus marginal
stim_marginal = obj.get_stimulus_marginal_cache();
training_duration = obj.training_duration;
%compute log(rate) and marginal rate for all sources
if obj.parallel && matlabpool('size')>1
parfor src = 1:numel(src_idx)
[P{src},M{src},nspikes(src),ntestspikes(src),nrespdim(src)] = compute_source(src_idx(src), bins, timestamp{src}, testresponse{src}, sp_stimulus{src}, sp_response{src}, stimgrid, currentstate, dist_lut, stim_marginal, training_duration, flt_idx{src});
end
else
for src = 1:numel(src_idx)
[P{src},M{src},nspikes(src),ntestspikes(src),nrespdim(src)] = compute_source(src_idx(src), bins, timestamp{src}, testresponse{src}, sp_stimulus{src}, sp_response{src}, stimgrid, currentstate, dist_lut, stim_marginal, training_duration, flt_idx{src});
end
end
info = struct( 'nspikes', nspikes, 'ntestspikes', ntestspikes, 'nrespdim', nrespdim );
end
function P = compute_posterior( obj, P, M, delta, rate_modulation )
%COMPUTE_POSTERIOR compute posterior distribution
if nargin<5 || isempty(rate_modulation)
rate_modulation = obj.rate_modulation( obj.source_selection );
elseif isscalar(rate_modulation)
rate_modulation = rate_modulation.*ones(numel(P),1);
elseif numel(rate_modulation)~=numel(P)
error('kde_decoder:invalidArgument', 'Invalid rate modulation vector')
end
Psum = 0;
Msum = 0;
%sum log(rates) and marginal rates
if iscell( P ) && iscell(M)
for k=1:numel(P)
Psum = Psum + P{k};
Msum = Msum + rate_modulation(k) .* M{k};
end
else
Psum = P;
Msum = M;
end
%compute likelihood
P = bsxfun( @minus, Psum, bsxfun( @times, delta(:), Msum ) );
%normalize
P = exp( bsxfun( @minus, P, nanmax(P,[],2) ) );
P = bsxfun( @rdivide, P, nansum(P,2) );
%reshape to match grid size
P = shiftdim( reshape( P, [size(P,1) obj.stimulus_grid_size] ), 1 );
end
function M = compute_source_tuning( obj )
%get which sources to compute
currentstate = obj.state(end);
src_idx = find( currentstate.source_selection );
%get stimulus grid and mask invalid elements
stimgrid = obj.stimulus_grid_expanded;
if ~isempty(currentstate.stimulus_grid_validity)
stimgrid( currentstate.stimulus_grid_validity,:) = NaN;
end
%get spike stimulus and response
sp_stimulus = get_spike_stimulus_cache(obj);
sp_stimulus = sp_stimulus( src_idx );
sp_response = get_spike_response_cache(obj);
sp_response = sp_response( src_idx );
%get distance matrices
dist_lut = get_distance_cache(obj);
nsrc = numel(src_idx);
%pre-allocate arrays
M = cell( nsrc, 1 );
%get stimulus marginal
stim_marginal = obj.get_stimulus_marginal_cache();
training_duration = obj.training_duration;
%compute rates and marginal rates for all sources
if obj.parallel && matlabpool('size')>1
parfor src = 1:numel(src_idx)
M{src} = compute_source_tuning(src_idx(src), sp_stimulus{src}, sp_response{src}, stimgrid, currentstate, dist_lut, stim_marginal, training_duration);
end
else
for src = 1:numel(src_idx)
M{src} = compute_source_tuning(src_idx(src), sp_stimulus{src}, sp_response{src}, stimgrid, currentstate, dist_lut, stim_marginal, training_duration);
end
end
%reshape to match grid size
M = vertcat( M{:} );
M = shiftdim( reshape( M, [size(M,1) obj.stimulus_grid_size] ), 1 );
end
function s = compute_performance(obj,true_stim,estimate,use_marginal)
if nargin<3
err = MException( 'kde_decoder:compute_performance:invalidArguments', 'Need at least two input arguments');
throw(err);
end
%check if we should use the marginal of the posterior to find
%the posterior mode
if nargin<4 || isempty(use_marginal)
use_marginal = false;
else
use_marginal = isequal( use_marginal, true );
end
%find the posterior mode
[estimate_mode{1:obj.ndim_stimulus}] = posterior_mode( estimate, obj.ndim_stimulus, 'marginal', use_marginal );
estimate_mode = cellfun( @(x,y) reshape(x(y),numel(y),1), obj.stimulus_grid, estimate_mode, 'UniformOutput', false );
estimate_mode = horzcat( estimate_mode{:} );
%compute error per dimension
estimate_error = zeros(size(estimate_mode));
for k=1:obj.ndim_stimulus
if ~isempty( obj.distance_lut{k} )
%use distance matrix
estimate_error(:,k) = obj.distance_lut{k}( sub2ind( size(obj.distance_lut{k}), true_stim(:,k)+1, estimate_mode(:,k)+1 ) );
elseif strcmp( obj.variable_types{ obj.stimulus_variable_type(k) }, 'circular' )
%use circular difference
estimate_error(:,k) = circ_diff( true_stim(:,k), estimate_mode(:,k) );
elseif strcmp( obj.variable_types{ obj.stimulus_variable_type(k) }, 'categorical' )
%use binary difference
estimate_error(:,k) = true_stim(:,k)~=estimate_mode(:,k);
else
%use algebraic difference
estimate_error(:,k) = abs( true_stim(:,k) - estimate_mode(:,k) );
end
end
%compute summary error
%use mean for categorical variables (result is fraction correct)
%use median for other variable types
idx = strcmp( obj.variable_types( obj.stimulus_variable_type ), 'categorical' );
summary_error = NaN(1,obj.ndim_stimulus);
summary_error(1, idx ) = mean( estimate_error(:,idx) );
summary_error(1,~idx ) = median( estimate_error(:,~idx) );
%compute bootstrap confidence intervals for summary error
summary_error_ci(:, idx) = bootci( 1000, @mean, estimate_error(:, idx) );
summary_error_ci(:,~idx) = bootci( 1000, @median, estimate_error(:,~idx) );
%pre-allocate matrices
conf = cell(1,obj.ndim_stimulus);
muinfo = zeros(1,obj.ndim_stimulus);
ientro = zeros(1,obj.ndim_stimulus);
muinfo_unbiased = zeros(1,obj.ndim_stimulus);
ientro_unbiased = zeros(1,obj.ndim_stimulus);
gridsize = obj.stimulus_grid_size;
%compute confusion matrix, mutual information and entropy for
%all dimensions
for k=1:obj.ndim_stimulus
%compute indices into grid for true and estimated stimulus values
true_stim(:,k) = reshape( nearestpoint( true_stim(:,k), obj.stimulus_grid{k}(:) ), size(true_stim,1), 1 );
estimate_mode(:,k) = reshape( nearestpoint( estimate_mode(:,k), obj.stimulus_grid{k}(:) ), size(estimate_mode,1), 1 );
%compute confusion matrix (no normalization)
conf{k} = accumarray( [true_stim(:,k) estimate_mode(:,k)], 1, [gridsize(k) gridsize(k)]);
%compute mutual information
muinfo(1,k) = mutualinfo( conf{k} );
%compute entropy of true stimulus
ientro(1,k) = ientropy( sum( conf{k}, 2 ) );
%perform jackknife bias correction for mutual information
%and entropy
jj = jackknife( @(x,y) local_jackknife(x,y,gridsize(k)), true_stim(:,k), estimate_mode(:,k) );
unbiased = size(true_stim,1).*[muinfo(1,k) ientro(1,k)] - (size(true_stim,1)-1).*mean(jj);
muinfo_unbiased(1,k) = unbiased(1);
ientro_unbiased(1,k) = unbiased(2);
end
%compute confusion matrix for all dimensions combined
conf_all = accumarray( [true_stim estimate_mode], 1, [gridsize gridsize]);
%construct output
s = struct( 'estimate_type', onoff(use_marginal, {'marginal', 'max a posteriori'}), 'estimation_error', estimate_error, 'summary_error', summary_error, 'summary_error_ci', summary_error_ci, 'mutual_info', muinfo, 'mutual_info_unbiased', muinfo_unbiased, 'entropy', ientro, 'entropy_unbiased', ientro_unbiased, 'mutual_info_unbiased_normalized', muinfo_unbiased./ientro_unbiased, 'confusion', {conf}, 'confusion_all', conf_all );
%inline function for jackknifing
function output = local_jackknife(jx,jy,gg)
cc = accumarray( [jx jy], 1, [gg gg] );
output = [ mutualinfo( cc ) ientropy( sum(cc,2) ) ];
end
end
end
methods
%PUSH/POP STATE
function push_state(obj)
obj.state(end+1) = obj.state(end);
end
function pop_state(obj)
if numel(obj.state)>1
obj.state(end) = [];
obj.flag_distance_cache = true;
obj.flag_stimulus_cache = true;
obj.flag_response_cache = true;
obj.flag_index_cache = true;
obj.flag_response_filter_cache = true;
obj.flag_marginal_cache = true;
end
end
%GET (DEPENDENT PROPS)
function val = get.ndim_stimulus(obj)
val = size(obj.training_stimulus,2);
end
function val = get.ndim_response(obj)
val = cellfun( @(x) size(x,2), obj.spike_response );
end
function val = get.nsources(obj)
val = numel(obj.spike_stimulus);
end
function val = get.nspikes(obj)
val = cellfun( @(x) size(x,1), obj.spike_stimulus );
end
function val = get.mean_rate(obj)
val = obj.nspikes ./ obj.training_duration;
end
function val = get.stimulus_grid_size(obj)
current_state = numel(obj.state);
val = cellfun('prodofsize',obj.state(current_state).stimulus_grid);
end
function val = get.current_state(obj)
val = obj.state(end);
end
function val = get.training_duration(obj)
val = sum( diff( obj.state(end).encoding_segments, [], 2) );
end
function val = get.stimulus_marginal(obj)
update_marginal_cache(obj);
val = obj.marginal_cache.stimulus;
end
%GET/SET
function val=get.rate_modulation(obj)
val = obj.state(end).rate_modulation;
end
function set.rate_modulation(obj,val)
if ~isnumeric(val) || ndims(val)~=2 || ( ~isscalar(val) && numel(val)~=obj.nsources ) || any(val<=0)
error('kde_decoder:setrate_modulation:invalidValue', 'Invalid value')
end
obj.state(end).rate_modulation = val(:).*ones(obj.nsources,1);
end
function val=get.rate_offset(obj)
val = obj.state(end).rate_offset;
end
function set.rate_offset(obj,val)
if ~isnumeric(val) || ~isscalar(val) || val<0
error('kde_decoder:setrate_offset:invalidValue', 'Invalid value')
end
obj.state(end).rate_offset = val;
end
function set.parallel(obj,val)
obj.parallel = isequal( val, true );
end
function val=get.response_randomization(obj)
val = obj.state(end).response_randomization;
end
function set.response_randomization(obj,val)
if isa(val, 'function_handle')
obj.state(end).response_randomization = val;
elseif ~isscalar(val) || (~isnumeric(val) && ~islogical(val))
err = MException('kde_decoder:set_response_randomization:invalidArgument','Invalid value');
throw(err);
else
obj.state(end).response_randomization = (val~=0);
end
end
function val=get.response_transformation(obj)
val = obj.state(end).response_transformation;
end
function set.response_transformation(obj,val)
if isempty(val)
val = @(x,src) x;
elseif ~isa(val,'function_handle')
err = MException('kde_decoder:set_response_randomization:invalidArgument','Invalid value');
throw(err);
end
obj.state(end).response_transformation = val;
obj.flag_response_cache = true;
end
function val=get.response_filter(obj)
val = obj.state(end).response_filter;
end
function set.response_filter(obj,val)
if isempty(val)
val = @(x,src) true(size(x,1),1);
elseif ~isa(val,'function_handle')
err = MException('kde_decoder:set_response_filter:invalidArgument','Invalid value');
throw(err);
end
obj.state(end).response_filter = val;
obj.flag_stimulus_cache = true;
obj.flag_response_cache = true;
obj.flag_response_filter_cache = true;
obj.flag_marginal_cache = true;
end
function val=get.response_filter_post(obj)
val = obj.state(end).response_filter_post;
end
function set.response_filter_post(obj,val)
if isempty(val)
val = @(x,src) true(size(x,1),1);
elseif ~isa(val,'function_handle')
err = MException('kde_decoder:set_response_filter:invalidArgument','Invalid value');
throw(err);
end
obj.state(end).response_filter_post = val;
end
function val=get.stimulus_kernel(obj)
val = obj.state(end).stimulus_kernel;
end
function set.stimulus_kernel(obj,val)
if nargin<2 || isempty(val)
return
elseif ischar( val ) || (iscellstr(val) && numel(val)==obj.ndim_stimulus)
val = obj.validate_kernels( val );
val = ones(1,obj.ndim_stimulus).*val(:)';
val = kde_decoder.cross_check_kernel( val, obj.stimulus_variable_type );
obj.state(end).stimulus_kernel = ones(1,obj.ndim_stimulus).*val(:)';
else
err = MException('kde_decoder:set_stimulus_kernel:invalidArgument','Kernel should be specified as a string or cell array of strings');
throw(err);
end
obj.flag_distance_cache = true;
obj.flag_stimulus_cache = true;
obj.flag_marginal_cache = true;
end
function val=get.stimulus_bandwidth(obj)
val = obj.state(end).stimulus_bandwidth;
end
function set.stimulus_bandwidth( obj, w )
if nargin<2 || isempty(w)
return
elseif ~isnumeric(w) || ~isvector(w) || (~isscalar(w) && numel(w)~=obj.ndim_stimulus) || any(w<=0)
err = MException('kde_decoder:set_stimulus_bandwidth:invalidArgument','Invalid stimulus kernel bandwidth');
throw(err);
else
w = kde_decoder.cross_check_bandwidth(ones(1,obj.ndim_stimulus).*w(:)', obj.stimulus_variable_type );
obj.state(end).stimulus_bandwidth = w;
end
obj.flag_distance_cache = true;
obj.flag_stimulus_cache = true;
obj.flag_marginal_cache = true;
end
function val=get.response_kernel(obj)
val = obj.state(end).response_kernel;
end
function set.response_kernel(obj,val)
obj.set_response_kernel( val );
end
function set_response_kernel( obj, arg1, arg2 )
if nargin<2
return
elseif nargin<3
idx = (1:obj.nsources)';
k = arg1;
else
idx = arg1;
k = arg2;
end
if isempty(idx) || ~isnumeric(idx) || ~isvector(idx) || any( idx<1 | idx>obj.nsources )
err = MException('kde_decoder:set_response_kernel:invalidArgument','Invalid source indices');
throw(err);
end
idx = unique(idx);
if ischar(k) || isempty(k)
k = obj.validate_kernels( k );
for j=1:numel(idx)
obj.state(end).response_kernel{idx(j),1} = kde_decoder.cross_check_kernel( ones(1,obj.ndim_response(idx(j))).*k, obj.response_variable_type{idx(j)});
end
elseif iscellstr(k) && isequal( size(k), [1 obj.ndim_response(idx(1))]) && isscalar( unique( obj.ndim_response(idx) ) )
k = obj.validate_kernels( k );
for j=1:numel(idx)
obj.state(end).response_kernel{idx(j),1} = kde_decoder.cross_check_kernel( k, obj.response_variable_type{idx(j)} );
end
elseif iscellstr(k) && isequal( size(k), [numel(idx) obj.ndim_response(idx(1))]) && isscalar( unique( obj.ndim_response(idx) ) )
k = obj.validate_kernels( k );
for j=1:numel(idx)
obj.state(end).response_kernel{idx(j),1} = kde_decoder.cross_check_kernel( k(j,:), obj.response_variable_type{idx(j)} );
end
elseif iscell(k) && isequal( size(k), [numel(idx) 1] )
for j=1:numel(idx)
tmp = obj.validate_kernels( k{j} );
if ~isscalar(tmp) && numel(tmp)~=obj.ndim_response(idx(j))
err = MException('kde_decoder:set_response_kernel:invalidArgument','Invalid response kernel');
throw(err);
end
obj.state(end).response_kernels{ idx(j),1 } = kde_decoder.cross_check_kernel( ones(1,obj.ndim_response(idx(j))).*tmp(:)', obj.response_variable_type{idx(j)} );
end
else
err = MException('kde_decoder:set_response_kernel:invalidArgument','Invalid response kernel');
throw(err)
end
obj.flag_response_cache = true;
end
function val=get.response_bandwidth(obj)
val = obj.state(end).response_bandwidth;
end
function set.response_bandwidth(obj,val)
obj.set_response_bandwidth( val );
end