-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataUtils.m
788 lines (701 loc) · 24.1 KB
/
DataUtils.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
classdef DataUtils < handle
%DATAUTILS contains data-utilities functions
methods (Static)
function output = divide_to_subvectors(input, l, d)
% Splits input vector to subvectors
%
% Parameters
% ----------
% - input : double vector
% input vector
% - l : int
% length of sub-vector
% - d : int
% delta between two sub-vectors
%
% Returns
% -------
% - output : 2d double array
% array of splited sub-vectors(each row).
%
% Examples
% --------
% 1.
% >>> input = [1, 2, 3, 4, 5];
% >>> l = 3;
% >>> d = 2;
% >>> divide_to_subvectors(input, l, d)
% [
% [1, 2, 3];
% [3, 4, 5]
% ]
% let 'm' is the number of splited sub-vectors and 'n' is the
% length of 'input', so
% (m - 1)d + l = n
% m = floor((n - l) / d) + 1
n = length(input);
m = floor((n - l) / d) + 1;
output = zeros(m, l);
for i = 1 : m
begin_index = (i - 1) * d + 1;
end_index = begin_index + l - 1;
output(i, :) = input(begin_index : end_index);
end
% output = [];
% i = 1;
% while (i + l - 1) <= length(input)
% output(end + 1, :) = input(i : i + l - 1);
%
% i = i + d;
% end
end
function output = divide_timeseries(input, dt_sec, l_sec, d_sec)
%DIVIDE_TIMESERIES splits input signal to sub-signals
%
% Parameters
% ----------
% - input : double vector
% input vector
% - dt_sec : double
% time resolution in seconds
% - l_sec : double
% length of sub-signal in seconds
% - d_sec : double
% delta between two sub-signlas in secondss
%
% Returns
% -------
% - output : 2d double array
% array of splited sub-vectors(each row)
%
% Examples
% --------
% 1.
% >>> input = [1, 2, 3, 4, 5];
% >>> dt_sec = 0.001;
% >>> l_sec = 0.002;
% >>> d_sec = 0.001;
% >>> divide_timeseries(input, dt_sec, l_sec, d_sec)
% [
% [1, 2],
% [2, 3],
% [3, 4],
% [4, 5]
% ]
% default values
switch nargin
case 1
dt_sec = 0.001;
l_sec = 0.001;
d_sec = l_sec;
case 2
l_sec = 0.001;
d_sec = l_sec;
case 3
d_sec = l_sec;
end
l = floor(l_sec / dt_sec);
d = floor(d_sec / dt_sec);
output = DataUtils.divide_to_subvectors(input, l, d);
end
function output = divide_to_samelength_subvectors(input, m)
%DIVIDE_TO_SAMELENGTH_SUBVECTORS splits input vector to same-legnth subvectors
%
% Parameters
% ----------
% - input : double vector
% input vector
% - m : int
% number of sub-vectors
%
% Returns
% -------
% - output : 2d double array
% array of splited sub-vectors(each row)
%
% Examples
% --------
% 1.
% >>> input = [1, 2, 3, 4, 5];
% >>> m = 2;
% >>> divide_to_samelength_subvectors(input, l, d)
% [
% [1, 2],
% [3, 4]
% ]
% length of each sub-vector
l = floor(length(input) / m);
% remove residual elements(mod(lenght(input), m) == 0)
input = input(1 : m * l);
% divide
output = DataUtils.divide_to_subvectors(input, l, l);
end
function output = downsample_vector(input, m)
%DOWNSAMPLE_VECTOR donwsample 'input' to 'output' with length 'm'
%
% Parameters
% ----------
% - input : double vector
% input vector
% - m : int
% length of output
%
% Returns
% -------
% - output : double vector
% downsampled vector
%
% Examples
% 1.
% >>> input = [1, 2, 3, 4, 5];
% >>> m = 3;
% >>> downsample_vector(input, m)
% [1, 3, 5]
% let 'n' is a length of 'input' and 'd' is the sampling rate, so
% (m - 1)d + 1 = n
% d = floor((n - 1) / (m - 1))
n = length(input);
d = floor((n - 1) / (m - 1));
% remove residual elements(mod(n, m) == 0)
n = m * d;
input = input(1 : n);
output = input(1 : d : n);
end
function output = resize(input, output_size, method)
%RESIZE resizes 'input' to 'output' with length 'm'
%
% Parameters
% ----------
% - input : double array
% input vector
% - output_size : int array
% size of output
% - method : char vector (default is 'bicubic')
% method of interpolation such as 'nearest', 'bilinear'
% and 'bicubic'
%
% Returns
% -------
% - output : double array
% resized vector
%
% Examples
% 1.
% >>> input = [1, 2, 3, 4];
% >>> output_size = [2, 1]
% >>> method = 'bilinear';
% >>> resize(input, output_size, method)
% [1.5, 3.5]
% default values
if nargin < 3
method = 'bicubic';
end
output = imresize(...
input, output_size, ...
method, ...
'Antialiasing', false);
end
end
% Make Random Data
methods (Static)
function make_data(n, l, filename, generator)
% Make random `data` file
%
% Parameters
% ----------
% - n : int
% number of samples
% - l : int
% length of each sample
% - filename: char vector
% filename of saved file
% - generator : handle function (default is @randn)
% generator function such as `randn`, `rand`, ...
% default generator
if ~exist('generator', 'var')
generator = @randn;
end
% db
data = struct();
data.x = cell(n, 1);
data.y = cell(n, 1);
% - x, y
for i = 1:n
data.x{i} = generator([l, 1]);
data.y{i} = generator([l, 1]);
end
% - save
save(fullfile(Path.DATA_DIR, filename), '-struct', 'data');
clear('data');
end
function transformAndSaveRealData(inFilename)
% parameters
% - output directory
outDirData = './assets/data';
outDirParams = './assets/ground-truth';
% inputField = 'input_PSTHsmooth';
% outputField = 'output_PSTHmoresmooth';
% inputField = 'input_spksmooth';
% outputField = 'output_spkmoresmooth';
% inputField = 'input_ce';
% outputField = 'output_ce';
inputField = 'vstim';
outputField = 'spk';
% - length of input sample
li = 50;
% - length of output sample
lo = 1;
% - offset between two consecutive data
d = 1;
% d = li;
% - begin index of data
begin = 1;
% - scale of output
scale = 1;
% - max(abs(sample)) must be greater thatn threshold
th = 0;
% make data
data = load(inFilename);
% divide input
input = data.(inputField)(begin:end);
% - make zero mean, unit variance
input = (input - mean(input)) / std(input);
input = DataUtils.divide_to_subvectors(input, li, d);
x = num2cell(input', 1)';
% divide output
output = data.(outputField)(begin:end);
% output = DataUtils.divide_to_subvectors(output, li, d) * scale;
output = output(li:end)' * scale;
y = num2cell(output', 1)';
y = cellfun(@(s) s(1:lo), y, 'UniformOutput', false);
% filter x, y
v = cellfun(@(s) max(abs(s)), y);
i = find(v >= th);
x = x(i);
y = y(i);
% save db
[~, name, ~] = fileparts(inFilename);
outFilename = fullfile(outDirData, name);
save(outFilename, 'x', 'y');
% make parameters
% b_A
b_A = 0;
% b_B
b_B = 0;
% b_G
b_G = 0;
% w_A
try
w_A = data.FA_ds(:);
w_A = w_A(end:-1:1);
catch
w_A = randn(li, 1);
end
% w_B
try
w_B = data.FB_ds(:);
w_B = w_B(end:-1:1);
catch
w_B = randn(li, 1);
end
% w_G
w_G = 1;
% - save
outFilename = fullfile(outDirParams, name);
save(outFilename, 'b_A', 'b_B', 'b_G', 'w_A', 'w_B', 'w_G');
end
function transformAndSaveRealData2(inFilename)
% parameters
% - output directory
outDirData = './assets/data';
outDirParams = './assets/ground-truth';
inputField = 'input_PSTHsmooth';
outputField = 'output_PSTHmoresmooth';
% inputField = 'input_spksmooth';
% outputField = 'output_spkmoresmooth';
% - length of input samples
li = [4000, 1000, 1000];
% - length of output sample
w = 25;
lo = li - w + 1;
% - begin index of data
begin = 1;
% - scale of output
scale = 0.1;
% make data
data = load(inFilename);
% input, output
input = data.(inputField)(begin:end);
output = data.(outputField)(begin:end);
% - make zero mean, unit variance
input = (input - mean(input)) / std(input);
% x, y
n = length(li);
x = cell(n, 1);
y = cell(n, 1);
startIndex = 1;
for i = 1:n
endIndex = startIndex + li(i) - 1;
x{i} = input(startIndex:endIndex)';
y{i} = output(startIndex:endIndex)';
y{i} = y{i}(1:lo(i));
y{i} = scale * y{i};
startIndex = endIndex + 1;
end
% save db
[~, name, ~] = fileparts(inFilename);
outFilename = fullfile(outDirData, name);
save(outFilename, 'x', 'y');
% make parameters
% b_A
b_A = 0;
% b_B
b_B = 0;
% b_G
b_G = 0;
% w_A
w_A = data.FA_ds(:);
w_A = w_A(end:-1:1);
% w_B
w_B = data.FB_ds(:);
w_B = w_B(end:-1:1);
% w_G
w_G = 1;
% - save
outFilename = fullfile(outDirParams, name);
save(outFilename, 'b_A', 'b_B', 'b_G', 'w_A', 'w_B', 'w_G');
end
function B = model1(w_B, b_B)
% B
% - linear
BL = @(x) conv(x, w_B, 'valid') + b_B;
% - nonlinear
BN = @(x) max(0, x);
% - LN
B = @(x) BN(BL(x));
end
function G = model2(w_B, b_B, w_A, w_G, b_G)
% B
% - linear
BL = @(x) conv(x, w_B, 'valid') + b_B;
% - nonlinear
BN = @(x) max(0, x);
% - LN
B = @(x) BN(BL(x));
% A
% - linear
AL = @(x) conv(x, w_A, 'valid');
% - nonlinear
AN = @(x) x;
% - LN
A = @(x) AN(AL(x));
% G
% - linear
GL = @(x) w_G * (B(x) - A(x)) + b_G;
% - nonlinear
GN = @(x) max(0, x);
% - LN
G = @(x) GN(GL(x));
end
function G = model3(w_B, w_A, b_A, w_G, b_G)
% B
% - linear
BL = @(x) conv(x, w_B, 'valid');
% - nonlinear
BN = @(x) x;
% - LN
B = @(x) BN(BL(x));
% A
% - linear
AL = @(x) conv(x, w_A, 'valid') + b_A;
% - nonlinear
AN = @(x) logsig(x);
% - LN
A = @(x) AN(AL(x));
% G
% - linear
GL = @(x) w_G * (B(x) .* A(x)) + b_G;
% - nonlinear
GN = @(x) max(0, x);
% - LN
G = @(x) GN(GL(x));
end
function G = model4(w_B, b_B, w_A, b_A, w_G, b_G)
% B
% - linear
BL = @(x) conv(x, w_B, 'valid') + b_B;
% - nonlinear
BN = @(x) max(0, x);
% - LN
B = @(x) BN(BL(x));
% A
% - linear
AL = @(x) conv(x, w_A, 'valid') + b_A;
% - nonlinear
AN = @(x) logsig(x);
% - LN
A = @(x) AN(AL(x));
% G
% - linear
GL = @(x) w_G * (B(x) .* A(x)) + b_G;
% - nonlinear
GN = @(x) max(0, x);
% - LN
G = @(x) GN(GL(x));
end
function makeAndSaveExpectedOutputs(dataFilename, paramsFilename, outFilename)
% data
data = load(dataFilename);
x = data.x;
y = data.y;
% parameters
params = load(paramsFilename);
w_B = params.w_B;
b_B = params.b_B;
w_A = params.w_A;
b_A = params.b_A;
w_G = params.w_G;
b_G = params.b_G;
% models
f1 = DataUtils.model1(w_B, b_B);
f2 = DataUtils.model2(w_B, b_B, w_A, w_G, b_G);
f3 = DataUtils.model3(w_B, w_A, b_A, w_G, b_G);
f4 = DataUtils.model4(w_B, b_B, w_A, b_A, w_G, b_G);
% expected outputs
y1 = out(f1);
y2 = out(f2);
y3 = out(f3);
y4 = out(f4);
% save
save(outFilename, 'x', 'y', 'y1', 'y2', 'y3', 'y4');
% Local Functions
function y = out(f)
y = cellfun(f, x, 'UniformOutput', false);
end
end
end
% Make Random Parameters
methods (Static)
function make_params(l, filename)
% Make random `data` file
%
% Parameters
% ----------
% - l : int vector
% length of each sample
% - filename: char vector
% filename of saved file
params = struct();
% b_A
params.b_A = 0;
% b_B
params.b_B = 0;
% b_G
params.b_G = 0;
x = linspace(0, 2 * pi, l)';
% w_A
params.w_A = -sin(x);
% w_B
params.w_B = sin(x);
% w_G
params.w_G = cos(x);
% - save
save(fullfile(Path.GROUND_TRUTH_DIR, filename), '-struct', 'params');
clear('params');
end
end
% Error
methods (Static)
function e = error(x, y, f, d)
% Compute averaged error of a model
% Parameters
% ----------
% - x: number[][]
% Input
% - y: number[][]
% Output
% - f: (x: number[]) => number[]
% Model
% - d: (y: number[], y_: number[]) => number
% Distance
if ~exist('d', 'var')
d = @(u, v) norm(u - v);
end
e = mean(...
arrayfun(...
@(i) d(y{i}, f(x{i})), ...
1:length(x)...
)...
);
end
end
methods (Static)
function kernel = make_gaussian_kernel( n, sigma )
%MAKE_GAUSSIAN_KERNEL makes 1d gaussian kernel
%
% Parameters
% ----------
% - n : int
% length of output kernel is 2*n+1
% - sigma : double
% std of gaussian kernel
%
% Returns
% -------
% - kernel : double array
% sum of kernel must be 1
x = -n:n;
kernel = exp(-((x ./ sigma) .^ 2));
kernel = kernel ./ sum(kernel);
end
function psth = spks_to_psth( spks, window_size_sec, kernel_size_sec, dt_sec )
%SPKS_TO_PSTH make psth (peri-stimulus time histogram) from input spike trains
%
% Parameters
% ----------
% - spks : 2d double array
% spikes
% - window_size_sec : double (default is 0.001)
% window of computing psth. unit is second.
% - kernel_size_sec : double (default is 0.001)
% size of kernel of gaussian smoothing. unit is second.
% - dt_sec : double (default is 0.001)
% time resolution. unit is second;
%
% Examples
% --------
% 1.
% >>> spks = [[1, 2]; [3, 4]];
% >>> spks_to_psth(spks)
% [2000, 3000]
% default values
switch nargin
case 1
window_size_sec = 0.001;
kernel_size_sec = 0.001;
dt_sec = 0.001;
case 2
kernel_size_sec = 0.001;
dt_sec = 0.001;
case 3
dt_sec = 0.001;
end
% number of trials
m = size(spks, 1);
% length of each trial
n = size(spks, 2);
% number of samples for computing firing rate in each trial
window_size = floor(window_size_sec / dt_sec);
% window_size_sec = window_size * dt_sec;
% mod(n, window-size) must be equals 0
residual = mod(n, window_size);
if residual ~= 0
spks = [spks, zeros(m, window_size - residual)];
end
% number of elements in each window in all trials
batch_size = m * window_size;
batch_size_sec = batch_size * dt_sec;
% make psth
psth = zeros(1, n);
% - i is index of psth
i = 1;
% - j is index of spks
j = 1;
while i <= n
psth(i : i + window_size - 1) = ...
sum(spks(j : j + batch_size - 1)) / batch_size_sec;
i = i + window_size;
j = j + batch_size;
end
% smooth psth
% - length of kernel
kernel_size = floor(kernel_size_sec / dt_sec);
% - 3 * sigma is enough (kernel_size = 6 * sigma)
sigma = kernel_size / 6;
psth = conv(...
psth, ...
fspecial('gaussian', [kernel_size, 1], sigma), ...
'same' ...
);
end
function mkdata(opts)
%MKDATA make db from continuous 'spk' and 'vstim'.
%[block diagram](./mkdata.vsdx)
%
% Parameters
% ----------
% - opts : struct
% path of schema is './mkdata_schema.json'
% inputs
% - spk
spk = getfield(load(opts.inputs.path), 'spk');
% - vstim
vstim = getfield(load(opts.inputs.path), 'vstim');
% divide
% - spks
spks = DataUtils.divide_timeseries(...
spk, ...
opts.params.dt, ...
opts.params.divide.trial_length ...
);
% - vstims
vstims = DataUtils.divide_timeseries(...
vstim, ...
opts.params.dt, ...
opts.params.divide.trial_length ...
);
% psth
psth = DataUtils.spks_to_psth(...
spks, ...
opts.params.psth.window_size, ...
opts.params.psth.kernel_size, ...
opts.params.dt ...
);
figure();
plot(psth);
grid('on');
title(...
sprintf(...
'PSTH (window: %.3f, kernel: %.3f)', ...
opts.params.psth.window_size, ...
opts.params.psth.kernel_size ...
) ...
);
% vstim
vstim = vstims(1, :);
figure();
plot(vstim);
grid('on');
title('VSTIM');
% db
% - length of each output sample
sample_size = floor(opts.params.db.output_size / opts.params.dt);
% - delta between two output samples
delta_size = floor(...
(opts.params.db.output_size - opts.params.db.output_intersection) / ...
opts.params.dt ...
);
% - divide psth
psths = DataUtils.divide_to_subvectors(...
psth, ...
sample_size, ...
delta_size ...
);
% - divide vstim
vstims = DataUtils.divide_to_subvectors(...
vstim, ...
sample_size, ...
delta_size ...
);
% - make db
db.x = num2cell(vstims', 1)';
db.y = num2cell(psths', 1)';
% - save db
save(opts.outputs.path, 'db');
end
function mkdata_test()
opts_path = './mkdata.json';
DataUtils.mkdata(jsondecode(fileread(opts_path)));
end
end
end