-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoders.py
More file actions
1151 lines (907 loc) · 48.9 KB
/
decoders.py
File metadata and controls
1151 lines (907 loc) · 48.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import scipy
from scipy import signal
import pdb
from copy import deepcopy
import pandas as pd
import PSID
from sklearn.multioutput import MultiOutputRegressor
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.model_selection import KFold
from sklearn.metrics import r2_score, hamming_loss, log_loss
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.decomposition import PCA
from sklearn.svm import SVR
from numpy.lib.stride_tricks import as_strided
from sklearn.utils import check_random_state
def form_lag_matrix(X, T, stride=1, stride_tricks=True, rng=None, writeable=False):
"""Form the data matrix with `T` lags.
Parameters
----------
X : ndarray (n_time, N)
Timeseries with no lags.
T : int
Number of lags.
stride : int or float
If stride is an `int`, it defines the stride between lagged samples used
to estimate the cross covariance matrix. Setting stride > 1 can speed up the
calculation, but may lead to a loss in accuracy. Setting stride to a `float`
greater than 0 and less than 1 will random subselect samples.
rng : NumPy random state
Only used if `stride` is a float.
stride_tricks : bool
Whether to use numpy stride tricks to form the lagged matrix or create
a new array. Using numpy stride tricks can can lower memory usage, especially for
large `T`. If `False`, a new array is created.
writeable : bool
For testing. You should not need to set this to True. This function uses stride tricks
to form the lag matrix which means writing to the array will have confusing behavior.
If `stride_tricks` is `False`, this flag does nothing.
Returns
-------
X_with_lags : ndarray (n_lagged_time, N * T)
Timeseries with lags.
"""
if not isinstance(stride, int) or stride < 1:
if not isinstance(stride, float) or stride <= 0. or stride >= 1.:
raise ValueError('stride should be an int and greater than or equal to 1 or a float ' +
'between 0 and 1.')
N = X.shape[1]
frac = None
if isinstance(stride, float):
frac = stride
stride = 1
n_lagged_samples = (len(X) - T) // stride + 1
if n_lagged_samples < 1:
raise ValueError('T is too long for a timeseries of length {}.'.format(len(X)))
if stride_tricks:
X = np.asarray(X, dtype=float, order='C')
shape = (n_lagged_samples, N * T)
strides = (X.strides[0] * stride,) + (X.strides[-1],)
X_with_lags = as_strided(X, shape=shape, strides=strides, writeable=writeable)
else:
X_with_lags = np.zeros((n_lagged_samples, T * N))
for i in range(n_lagged_samples):
X_with_lags[i, :] = X[i * stride:i * stride + T, :].flatten()
if frac is not None:
rng = check_random_state(rng)
idxs = np.sort(rng.choice(n_lagged_samples, size=int(np.ceil(n_lagged_samples * frac)),
replace=False))
X_with_lags = X_with_lags[idxs]
return X_with_lags
def decimate_(X, q):
Xdecimated = []
for i in range(X.shape[1]):
Xdecimated.append(signal.decimate(X[:, i], q))
return np.array(Xdecimated).T
# If X has trial structure, need to seperately normalize each trial
def standardize(X):
scaler = StandardScaler()
if type(X) == list:
#Xstd = [scaler.fit_transform(x) for x in X]
Xstd = [scaler.fit_transform(np.asarray(x)) if len(x) > 0 else np.array([]) for x in X]
elif np.ndim(X) == 3:
Xstd = np.array([scaler.fit_transform(X[idx, ...]) for idx in range(X.shape[0])])
else:
Xstd = scaler.fit_transform(X)
return Xstd
# Turn position into velocity and acceleration with finite differences
def expand_state_space(Z, X, include_vel=True, include_acc=True):
concat_state_space = []
for i, z in enumerate(Z):
if include_vel and include_acc:
pos = z[2:, :]
vel = np.diff(z, 1, axis=0)[1:, :]
acc = np.diff(z, 2, axis=0)
# Trim off 2 samples from the neural data to match lengths
X[i] = X[i][2:, :]
concat_state_space.append(np.concatenate((pos, vel, acc), axis=-1))
elif include_vel:
pos = z[1:, :]
vel = np.diff(z, 1, axis=0)
# Trim off only one sample in this case
X[i] = X[i][1:, :]
concat_state_space.append(np.concatenate((pos, vel), axis=-1))
else:
concat_state_space.append(z)
return concat_state_space, X
def lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity, include_acc):
# If no trial structure is present, convert to a list for easy coding
if np.ndim(Xtrain) == 2:
Xtrain = [Xtrain]
Xtest = [Xtest]
Ztrain = [Ztrain]
Ztest = [Ztest]
Ztrain = standardize(Ztrain)
Xtrain = standardize(Xtrain)
Ztest = standardize(Ztest)
Xtest = standardize(Xtest)
# Apply train lag
if trainlag > 0:
Xtrain = [x[:-trainlag, :] for x in Xtrain]
Ztrain = [z[trainlag:, :] for z in Ztrain]
elif trainlag < 0:
Xtrain = [x[-trainlag:, :] for x in Xtrain]
Ztrain = [z[:trainlag, :] for z in Ztrain]
# Apply test lag
if testlag > 0:
Xtest = [x[:-trainlag, :] for x in Xtest]
Ztest = [z[trainlag:, :] for z in Ztest]
elif testlag < 0:
Xtest = [x[-trainlag:, :] for x in Xtest]
Ztest = [z[:trainlag, :] for z in Ztest]
# Apply decoding window
Xtrain = [form_lag_matrix(x, decoding_window) for x in Xtrain]
Xtest = [form_lag_matrix(x, decoding_window) for x in Xtest]
Ztrain = [z[decoding_window//2:, :] for z in Ztrain]
Ztrain = [z[:x.shape[0], :] for z, x in zip(Ztrain, Xtrain)]
Ztest = [z[decoding_window//2:, :] for z in Ztest]
Ztest = [z[:x.shape[0], :] for z, x in zip(Ztest, Xtest)]
# Expand state space to include velocity and acceleration
if np.any([include_velocity, include_acc]):
Ztrain, Xtrain = expand_state_space(Ztrain, Xtrain, include_velocity, include_acc)
Ztest, Xtest = expand_state_space(Ztest, Xtest, include_velocity, include_acc)
# Flatten trial structure as regression will not care about it
Xtrain = np.concatenate(Xtrain)
Xtest = np.concatenate(Xtest)
Ztrain = np.concatenate(Ztrain)
Ztest = np.concatenate(Ztest)
return Xtest, Xtrain, Ztest, Ztrain
# Sticking with consistent nomenclature, Z is the behavioral data and X is the neural data
def lr_encoder(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window=1, include_velocity=True, include_acc=False):
# By default, we look only at pos and vel
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity, include_acc)
# Apply the decoding window to the behavioral data
# Ztrain, _ = form_lag_matrix(Ztrain, decoding_window)
# Ztest, _ = form_lag_matrix(Ztest, decoding_window)
# Xtrain = Xtrain[decoding_window//2:, :]
# Xtest = Xtest[:Ztest.shape[1], :]
encodingregressor = LinearRegression(fit_intercept=True)
# Throw away acceleration
# Ztest = Ztest[:, 0:4]
# Ztrain = Ztrain[:, 0:4]
encodingregressor.fit(Ztrain, Xtrain)
Xpred = encodingregressor.predict(Ztest)
r2 = r2_score(Xtest, Xpred)
return r2, encodingregressor
def svm_decoder(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window=1, include_velocity=True, include_acc=True):
# Do not fit if the sample size is too large
if Xtrain.shape[0] > 20000:
return np.nan, np.nan, np.nan, None, np.nan, np.nan, np.nan
# Only fit every other dimension
# Load arg files and check if dimensionality is *every other*
dimvals = np.arange(1, 31, 2)
if Xtrain.shape[-1] not in dimvals:
return np.nan, np.nan, np.nan, None, np.nan, np.nan, np.nan
behavior_dim = Ztrain[0].shape[-1]
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity, include_acc)
svr = SVR()
decodingregressor = MultiOutputRegressor(svr)
decodingregressor.fit(Xtrain, Ztrain)
Zpred = decodingregressor.predict(Xtest)
# Calculate log likelihood of the training fit
if include_velocity and include_acc:
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
lr_r2_acc = r2_score(Ztest[..., 2*behavior_dim:], Zpred[..., 2*behavior_dim:])
return lr_r2_pos, lr_r2_vel, lr_r2_acc, decodingregressor, np.nan, np.nan, np.nan
elif include_velocity:
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
return lr_r2_pos, lr_r2_vel, decodingregressor, np.nan, np.nan
else:
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_pos = r2_score(Ztest, Zpred)
return {'lr_r2_pos':lr_r2_pos, 'decodingregressor':decodingregressor}
def psid_decoder(Xtest, Xtrain, Ztest, Ztrain, lag, include_velocity=True, include_acc=True, state_dim=None):
behavior_dim = Ztrain[0].shape[-1]
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, 0, 0, 1, include_velocity, include_acc)
if state_dim is None:
try:
state_dim = Xtrain.shape[-1]
except:
state_dim = Xtrain[0].shape[-1]
if np.isscalar(state_dim):
# Was a single state dimension passed in?
idsys = PSID.PSID(Xtrain, Ztrain, nx=state_dim,
n1=min(state_dim, lag * behavior_dim), i=lag)
Zpred, _, _ = idsys.predict(Xtest)
try:
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
lr_r2_acc = r2_score(Ztest[..., 2*behavior_dim:], Zpred[..., 2*behavior_dim:])
except:
# SVD likely has failed to converge
print('SVD failed to converge!')
lr_r2_pos = np.nan
lr_r2_vel = np.nan
lr_r2_acc = np.nan
# r2 = evalPrediction(Ztest, zpred, 'R2')
#return lr_r2_pos, lr_r2_vel, lr_r2_acc, None, None, None, None
return {'lr_r2_pos':lr_r2_pos, 'lr_r2_vel':lr_r2_vel, 'lr_r2_acc':lr_r2_acc}
else:
# An array of state dimensions was passed in that we should loop over
r2_pos = np.zeros(len(state_dim))
r2_vel = np.zeros(len(state_dim))
r2_acc = np.zeros(len(state_dim))
for i, sdim in enumerate(state_dim):
idsys = PSID.PSID(Xtrain, Ztrain, nx=sdim,
n1=min(sdim, lag * behavior_dim), i=lag)
Zpred, _, _ = idsys.predict(Xtest)
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
lr_r2_acc = r2_score(Ztest[..., 2*behavior_dim:], Zpred[..., 2*behavior_dim:])
r2_pos[i] = lr_r2_pos
r2_vel[i] = lr_r2_vel
r2_acc[i] = lr_r2_acc
#return r2_pos, r2_vel, r2_acc
return {'r2_pos': r2_pos, 'r2_vel':r2_vel, 'r2_acc':r2_acc}
def rrlr_decoder(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, ranks, decoding_window=1, include_velocity=True, include_acc=True):
# Don't have the trailized version currently
if isinstance(Xtrain, list) or np.ndim(Xtrain) == 3:
raise NotImplementedError
behavior_dim = Ztrain[0].shape[-1]
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity, include_acc)
# Following the exposition in
# Sparse Reduced-Rank Regression for Simultaneous Dimension Reduction and Variable Selection
# Form the matrices Sxx and Sxy
n = Xtrain.shape[0]
Sxx = 1/n * Xtrain.T @ Xtrain
Sxxinv = np.linalg.pinv(Sxx)
Sxy = 1/n * Xtrain.T @ Ztrain
eig, V = np.linalg.eig(Sxy.T @ Sxxinv @ Sxy)
eigorder = np.argsort(eig)[::-1]
V = V[:, eigorder]
r2_pos = np.zeros(ranks.size)
r2_vel = np.zeros(ranks.size)
r2_acc = np.zeros(ranks.size)
for i, rank in enumerate(ranks):
Vr = V[:, 0:rank]
coef = Sxxinv @ Sxy @ Vr @ Vr.T
# Predict
Zpred = Xtest @ coef
r2_pos[i] = r2_score(Ztest[:, 0:behavior_dim], Zpred[:, 0:behavior_dim])
if include_velocity:
r2_vel[i] = r2_score(Ztest[..., behavior_dim:2*behavior_dim],
Zpred[..., behavior_dim:2*behavior_dim])
if include_acc:
r2_acc[i] = r2_score(Ztest[..., 2*behavior_dim:],
Zpred[..., 2*behavior_dim:])
#return r2_pos, r2_vel, r2_acc, None, None, None, None
return {'r2_pos':r2_pos, 'r2_vel':r2_vel, 'r2_acc':r2_acc}
def lr_decoder(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window=1, include_velocity=True, include_acc=True):
behavior_dim = Ztrain[0].shape[-1]
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity, include_acc)
decodingregressor = LinearRegression(fit_intercept=True)
decodingregressor.fit(Xtrain, Ztrain)
Zpred = decodingregressor.predict(Xtest)
# Calculate log likelihood of the training fit
Zpred_train = decodingregressor.predict(Xtrain)
if include_velocity and include_acc:
#logll_pos = log_likelihood_glm('normal', Ztrain[..., 0:behavior_dim], Zpred_train[..., 0:behavior_dim])
#logll_vel = log_likelihood_glm('normal', Ztrain[..., behavior_dim:2*behavior_dim], Zpred_train[..., behavior_dim:2*behavior_dim])
#logll_acc = log_likelihood_glm('normal', Ztrain[..., 2*behavior_dim:], Zpred_train[..., 2*behavior_dim:])
logll_pos = np.nan
logll_vel = np.nan
logll_acc = np.nan
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
lr_r2_acc = r2_score(Ztest[..., 2*behavior_dim:], Zpred[..., 2*behavior_dim:])
#return lr_r2_pos, lr_r2_vel, lr_r2_acc, decodingregressor, logll_pos, logll_vel, logll_acc
return {'lr_r2_pos':lr_r2_pos, 'lr_r2_vel':lr_r2_vel, 'lr_r2_acc':lr_r2_acc, 'decodingregressor':decodingregressor, 'logll_pos':logll_pos, 'logll_vel':logll_vel, 'logll_acc':logll_acc}
elif include_velocity:
logll_pos = log_likelihood_glm('normal', Ztrain[..., 0:behavior_dim], Zpred_train[..., 0:behavior_dim])
logll_vel = log_likelihood_glm('normal', Ztrain[..., behavior_dim:2*behavior_dim], Zpred_train[..., behavior_dim:2*behavior_dim])
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
return #lr_r2_pos, lr_r2_vel, decodingregressor, logll_pos, logll_vel
return {'lr_r2_pos':lr_r2_pos, 'lr_r2_vel':lr_r2_vel, 'decodingregressor':decodingregressor, 'logll_pos':logll_pos, 'logll_vel':logll_vel}
else:
logll_pos = log_likelihood_glm('normal', Ztrain[..., 0:behavior_dim], Zpred_train[..., 0:behavior_dim])
logll_vel = log_likelihood_glm('normal', Ztrain[..., behavior_dim:2*behavior_dim], Zpred_train[..., behavior_dim:2*behavior_dim])
lr_r2_pos = r2_score(Ztest[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_pos = r2_score(Ztest, Zpred)
#return lr_r2_pos, decodingregressor, logll_pos
return {'lr_r2_pos':lr_r2_pos, 'decodingregressor':decodingregressor, 'logll_pos':logll_pos}
def _draw_bootstrap_sample(rng, X, y):
sample_indices = np.arange(X.shape[0])
bootstrap_indices = rng.choice(
sample_indices, size=sample_indices.shape[0], replace=True
)
return X[bootstrap_indices], y[bootstrap_indices]
def lr_bias_variance(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window=1, n_boots=200, random_seed=None):
if random_seed is None:
rand = np.random
else:
rand = np.random.RandomState(random_seed)
# To bootstrap, we need to preprocess and flatten the data
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window, include_velocity=True, include_acc=True)
# Run lr_decoder over bootstrapped samples of xtrain and xtest. Use this to calculate bias and variance of the estimator
zpred_boot = []
for k in range(n_boots):
xboot, zboot = _draw_bootstrap_sample(rand, Xtrain, Ztrain)
decodingregressor = LinearRegression(fit_intercept=True)
decodingregressor.fit(xboot, zboot)
zpred = decodingregressor.predict(Xtest)
zpred_boot.append(zpred)
zpred_boot = np.array(zpred_boot)
assert(np.allclose((zpred_boot - Ztest).shape, zpred_boot.shape))
# Bias/Variance/MSE
mse = np.mean(np.mean(np.power(zpred_boot - Ztest, 2), axis=1), axis=0)
Ezpred = np.mean(zpred_boot, axis=0)
bias = np.sum((Ezpred - Ztest)**2, axis=0)/Ztest.shape[0]
var = np.mean(np.mean(np.power(zpred_boot - Ezpred, 2), axis=1), axis=0)
return mse, bias, var
#*** Assumes that Z already has dimension 6**** (hence includevelocity/acc is set to FALSE)#
# This also means that the transition times and pkassign indices correspond to Z having been passed through expnad state space #
def lr_bv_windowed(X, Z, lag, train_windows, test_windows, transition_times, train_idxs, test_idxs, pkassign=None, apply_pk_to_train=False,
decoding_window=1, n_boots=200, random_seed=None, offsets=None, norm=False):
if random_seed is None:
rand = np.random
else:
rand = np.random.RandomState(random_seed)
win_min = train_windows[0][0]
if win_min >= 0:
win_min = 0
# Filter out by transitions that lie within the train idxs, and stay clear of the start and end
tt_train = [(t, idx) for idx, t in enumerate(transition_times)
if idx in train_idxs and t[0] > (lag + np.abs(win_min)) and t[1] < (Z.shape[0] - lag - np.abs(win_min))]
# Re-assign train idxs removing those reaches that were outside the start/end region
train_idxs = [x[1] for x in tt_train]
tt_train = [x[0] for x in tt_train]
if offsets is not None:
offsets_train = offsets[train_idxs]
else:
offsets_train = None
# Get trialized, windowed data
if pkassign is not None and apply_pk_to_train:
assert(np.all([s.size == np.arange(t[0], t[1]).size for (s, t) in zip(pkassign[train_idxs], tt_train)]))
subset_selection = [np.argwhere(np.array(s) == 0).squeeze() for s in pkassign[train_idxs]]
Xtrain, Ztrain, _, _ = apply_window(X, Z, lag, train_windows, tt_train, decoding_window, False, False, subset_selection, offsets=offsets_train)
else:
Xtrain, Ztrain, _, _ = apply_window(X, Z, lag, train_windows, tt_train, decoding_window, False, False, offsets=offsets_train)
# Filter out by transitions that lie within the test idxs, and stay clear of the start and end
tt_test = [(t, idx) for idx, t in enumerate(transition_times)
if idx in test_idxs and t[0] > (lag + np.abs(win_min)) and t[1] < (Z.shape[0] - lag - np.abs(win_min))]
# Re-assign test idxs removing those reaches that were outside the start/end region
test_idxs = [x[1] for x in tt_test]
tt_test = [x[0] for x in tt_test]
if offsets is not None:
offsets_test = offsets[test_idxs]
else:
offsets_test = None
if pkassign is not None:
assert(np.all([s.size == np.arange(t[0], t[1]).size for (s, t) in zip(pkassign[test_idxs], tt_test)]))
subset_selection = [np.argwhere(np.array(s) != 0).squeeze() for s in pkassign[test_idxs]]
Xtest, Ztest, _, _ = apply_window(X, Z, lag, test_windows, tt_test, decoding_window, False, False, subset_selection, offsets=offsets_test)
else:
Xtest, Ztest, _, _ = apply_window(X, Z, lag, test_windows, tt_test, decoding_window, False, False, offsets=offsets_test)
num_test_reaches = len(Xtest)
# verify dimensionalities
if len(Xtrain) > 0:
Xtrain = np.concatenate(Xtrain)
Ztrain = np.concatenate(Ztrain)
else:
return np.nan, np.nan, np.nan, num_test_reaches
if len(Xtest) > 0:
Xtest = np.concatenate(Xtest)
Ztest = np.concatenate(Ztest)
Xtrain = StandardScaler().fit_transform(Xtrain)
#Ztrain = StandardScaler().fit_transform(Ztrain)
Xtest = StandardScaler().fit_transform(Xtest)
#Ztest = StandardScaler().fit_transform(Ztest)
# Run lr_decoder over bootstrapped samples of xtrain and xtest. Use this to calculate bias and variance of the estimator
zpred_boot = []
for k in range(n_boots):
xboot, zboot = _draw_bootstrap_sample(rand, Xtrain, Ztrain)
decodingregressor = LinearRegression(fit_intercept=True)
decodingregressor.fit(xboot, zboot)
zpred = decodingregressor.predict(Xtest)
zpred_boot.append(zpred)
zpred_boot = np.array(zpred_boot)
assert(np.allclose((zpred_boot - Ztest).shape, zpred_boot.shape))
# Bias/Variance/MSE
mse = np.mean(np.mean(np.power(zpred_boot - Ztest, 2), axis=1), axis=0)
Ezpred = np.mean(zpred_boot, axis=0)
bias = np.sum((Ezpred - Ztest)**2, axis=0)/Ztest.shape[0]
var = np.mean(np.mean(np.power(zpred_boot - Ezpred, 2), axis=1), axis=0)
if norm:
norm_ = np.mean(np.power(Ztest, 2), axis=0)
return np.divide(mse, norm_), np.divide(bias, norm_), np.divide(var, norm_), num_test_reaches
else:
return mse, bias, var, num_test_reaches
else:
return np.nan, np.nan, np.nan, num_test_reaches
def apply_window(X, Z, lag, window, transition_times, decoding_window, include_velocity, include_acc,
subset_selection=None, offsets=None, enforce_full_indices=False):
# Update 12/15: We allow for multiple windows for each transition time, so we can train the decoder across pooled sections
# of the reach
# subset_selection: set of indices of the same length as transition_times that indicate whether a subset of the transition
# is to be included. This is used when we enforce peak membership in decoding.
# Apply decoding window
X = form_lag_matrix(X, decoding_window)
Z = Z[decoding_window//2:, :]
Z = Z[:X.shape[0], :]
# Expand state space to include velocity and acceleration
if np.any([include_velocity, include_acc]):
Z, X = expand_state_space([Z], [X], include_velocity, include_acc)
# Flatten list structure imposed by expand_state_space
Z = Z[0]
X= X[0]
# This *also* requires shifting the transition times, as behavior will have been affected
# There is a shift due to the formation of the lag matrix *and* the expansion of the state sapce,
# which will cut off the first 2 sample points, if include_acc is set to true
if decoding_window > 1:
transition_times = [(t[0] - decoding_window//2, t[1] - decoding_window//2) for t in transition_times]
try:
assert(X.shape[0] == Z.shape[0])
except:
pdb.set_trace()
if include_acc:
transition_times = [(t[0] - 2, t[1] - 2) for t in transition_times]
elif include_velocity:
transition_times = [(t[0] - 1, t[1] - 1) for t in transition_times]
# We assume that the subset indices have already been shifted (this is the case in biasvariance_vst)
# Segment the time series with respect to the transition times (including lag)
xx = []
zz = []
valid_idxs = []
# Which reaches had no truncation due to start of the next reach?
full_idxs = []
# If given a single window, duplicate it across all transition times
if len(window) != len(transition_times):
window = [window for _ in range(len(transition_times))]
# If no offsets provided, let it be 0 for all transition times
if offsets is None:
offsets = np.zeros(len(transition_times))
for i, (t0, t1) in enumerate(transition_times):
for j, win in enumerate(window[i]):
window_indices = np.arange(t0 + win[0] + offsets[i], t0 + win[1] + offsets[i])
if subset_selection is not None:
# Select only indices that do not belong to the first velocity peak
subset_indices = np.arange(t0, t1)[subset_selection[i]]
window_indices = np.intersect1d(window_indices, subset_indices)
# No matter what, we should remove segments that overlap with the next transition
if i < len(transition_times) - 1:
l1 = len(window_indices)
window_indices = window_indices[window_indices < transition_times[i + 1][0]]
if len(window_indices) == l1:
full_idxs.append(i)
else:
# Or else make sure that we don't exceed the length of the time series
window_indices = window_indices[window_indices < Z.shape[0]]
window_indices = window_indices.astype(int)
zz_ = Z[window_indices]
# Shift x indices by lag
window_indices -= lag
xx_ = X[window_indices]
if len(xx_) > 0:
assert(xx_.shape[0] == zz_.shape[0])
if enforce_full_indices:
if i in full_idxs:
xx.append(xx_)
zz.append(zz_)
else:
xx.append(xx_)
zz.append(zz_)
valid_idxs.append(i)
# try:
# assert(full_idxs[-1] in valid_idxs or i == len(transition_times) - 1)
# except:
# pdb.set_trace()
return xx, zz, valid_idxs, full_idxs
def lr_decode_windowed(X, Z, lag, train_windows, test_windows, transition_times, train_idxs, test_idxs=None,
decoding_window=1, include_velocity=True, include_acc=True,
pkassign=None, apply_pk_to_train=False, offsets=None):
behavior_dim = Z.shape[-1]
# We have been given a list (of list) of windows for each transition
win_min = train_windows[0][0]
if win_min >= 0:
win_min = 0
# Filter out by transitions that lie within the train idxs, and stay clear of the start and end
tt_train = [(t, idx) for idx, t in enumerate(transition_times)
if idx in train_idxs and t[0] > (lag + np.abs(win_min)) and t[1] < (Z.shape[0] - lag - np.abs(win_min))]
# Re-assign train idxs removing those reaches that were outside the start/end region
train_idxs = [x[1] for x in tt_train]
tt_train = [x[0] for x in tt_train]
if offsets is not None:
offsets_train = offsets[train_idxs]
else:
offsets_train = None
if apply_pk_to_train:
# Train on the first velocity peak only
assert(np.all([s.size == np.arange(t[0], t[1]).size for (s, t) in zip(pkassign[train_idxs], tt_train)]))
subset_selection = [np.argwhere(np.array(s) == 0).squeeze() for s in pkassign[train_idxs]]
Xtrain, Ztrain, vi1, fi1 = apply_window(X, Z, lag, train_windows, tt_train, decoding_window, include_velocity, include_acc, subset_selection, offsets=offsets_train,
enforce_full_indices=True)
else:
Xtrain, Ztrain, vi1, fi1 = apply_window(X, Z, lag, train_windows, tt_train, decoding_window, include_velocity, include_acc, offsets=offsets_train,
enforce_full_indices=True)
if Xtrain is None:
return np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, None, 0
else:
n = len(Xtrain)
if test_idxs is not None:
# Filter out by transitions that lie within the test idxs, and stay clear of the start and end
tt_test = [(t, idx) for idx, t in enumerate(transition_times)
if idx in test_idxs and t[0] > (lag + np.abs(win_min)) and t[1] < (Z.shape[0] - lag - np.abs(win_min))]
# Re-assign test idxs removing those reaches that were outside the start/end region
test_idxs = [x[1] for x in tt_test]
tt_test = [x[0] for x in tt_test]
if offsets is not None:
offsets_test = offsets[test_idxs]
else:
offsets_test = None
if pkassign is not None:
assert(np.all([s.size == np.arange(t[0], t[1]).size for (s, t) in zip(pkassign[test_idxs], tt_test)]))
subset_selection = [np.argwhere(np.array(s) != 0).squeeze() for s in pkassign[test_idxs]]
Xtest, Ztest, _, fi2 = apply_window(X, Z, lag, test_windows, tt_test, decoding_window, include_velocity, include_acc, subset_selection, offsets=offsets_test,
enforce_full_indices=True)
else:
Xtest, Ztest, _, fi2 = apply_window(X, Z, lag, test_windows, tt_test, decoding_window, include_velocity, include_acc, offsets=offsets_test,
enforce_full_indices=True)
else:
Xtest = None
Ztest = None
# Standardize
# X = StandardScaler().fit_transform(X)
# Z = StandardScaler().fit_transform(Z)
decodingregressor = LinearRegression(fit_intercept=True)
# Fit and score
if len(Xtrain) == 0:
return tuple([np.nan] * 9) + (0,)
decodingregressor.fit(np.concatenate(Xtrain), np.concatenate(Ztrain))
Zpred = decodingregressor.predict(np.concatenate(Xtrain))
# Re-segment Zpred
idx = 0
Zpred_segmented = []
for i, z in enumerate(Ztrain):
Zpred_segmented.append(Zpred[idx:idx+z.shape[0]])
idx += z.shape[0]
assert(np.all([z1.shape[0] == z2.shape[0] for (z1, z2) in zip(Zpred_segmented, Ztrain)]))
#Ztrain = np.concatenate(Ztrain)
if Xtest is not None:
if len(Xtest) > 0:
num_test_reaches = len(Xtest)
Zpred_test = decodingregressor.predict(np.concatenate(Xtest))
idx = 0
Zpred_test_segmented = []
for i, z in enumerate(Ztest):
Zpred_test_segmented.append(Zpred_test[idx:idx+z.shape[0]])
idx += z.shape[0]
assert(np.all([z1.shape[0] == z2.shape[0] for (z1, z2) in zip(Zpred_test_segmented, Ztest)]))
else:
Xtest = None
Ztest = None
num_test_reaches = 0
if include_velocity and include_acc:
# Additionally calculate the individual MSE. Do not average over data points
mse_train = [(z1 - z2)**2 for (z1, z2) in zip(Zpred_segmented, Ztrain)]
Ztrain = np.concatenate(Ztrain)
lr_r2_pos = r2_score(Ztrain[..., 0:behavior_dim], Zpred[..., 0:behavior_dim])
lr_r2_vel = r2_score(Ztrain[..., behavior_dim:2*behavior_dim], Zpred[..., behavior_dim:2*behavior_dim])
lr_r2_acc = r2_score(Ztrain[..., 2*behavior_dim:], Zpred[..., 2*behavior_dim:])
if Xtest is not None:
mse_test = [(z1 - z2)**2 for (z1, z2) in zip(Zpred_test_segmented, Ztest)]
Ztest = np.concatenate(Ztest)
lr_r2_post = r2_score(Ztest[..., 0:behavior_dim], Zpred_test[..., 0:behavior_dim])
lr_r2_velt = r2_score(Ztest[..., behavior_dim:2*behavior_dim], Zpred_test[..., behavior_dim:2*behavior_dim])
lr_r2_acct = r2_score(Ztest[..., 2*behavior_dim:], Zpred_test[..., 2*behavior_dim:])
else:
mse_test = np.nan
lr_r2_post = np.nan
lr_r2_velt = np.nan
lr_r2_acct = np.nan
#return lr_r2_pos, lr_r2_vel, lr_r2_acc, lr_r2_post, lr_r2_velt, lr_r2_acct, decodingregressor, num_test_reaches, fi1, fi2, mse_train, mse_test
return {'lr_r2_pos':lr_r2_pos, 'lr_r2_vel':lr_r2_vel, 'lr_r2_acc':lr_r2_acc, 'lr_r2_post':lr_r2_post, 'lr_r2_velt':lr_r2_velt, 'lr_r2_acct':lr_r2_acct, 'decodingregressor':decodingregressor, 'num_test_reaches':num_test_reaches, 'fi1':fi1, 'fi2':fi2, 'mse_train':mse_train, 'mse_test':mse_test }
elif include_velocity:
raise NotImplementedError
else:
raise
############################### Residual decoding #########################################
def decorrelate(Z, decorrelation='entire', embed=True, transition_times=None, window_indices=None):
print('Decorrelating!')
if decorrelation == 'entire':
pos = Z[:, 0:2]
vel = Z[:, 2:4]
acc = Z[:, 4:]
posdecodingregressor = LinearRegression(fit_intercept=True)
pos_vel = np.hstack([pos, vel])
posdecodingregressor.fit(pos, vel)
# Extract the residuals
vel_residuals = vel - posdecodingregressor.predict(pos)
posveldecodingregressor = LinearRegression(fit_intercept=True)
posveldecodingregressor.fit(pos_vel, acc)
# Extract the residuals
acc_residuals = acc - posveldecodingregressor.predict(pos_vel)
elif decorrelation == 'trialized':
# Segment Z by transition times
Z_segmented = [Z[t[0]:t[1]] for t in transition_times]
# Concatenate, predict, and re-segment
vel_residuals, acc_residuals = decorrelate(np.concatenate(Z_segmented), decorrelation='entire')
transition_lengths = np.cumsum([t[1] - t[0] for t in transition_times])
transition_lengths = np.concatenate([[0], transition_lengths]).astype(int)
vel_segmented = [vel_residuals[transition_lengths[i]:transition_lengths[i + 1]]
for i in range(len(transition_lengths) - 1)]
acc_segmented = [acc_residuals[transition_lengths[i]:transition_lengths[i + 1]]
for i in range(len(transition_lengths) - 1)]
# assert(np.all([v.shape[0] == t[1] - t[0] for (v, t) in zip(vel_segmented, transition_times)]))
if embed:
vel_residuals = deepcopy(Z)[:, 2:4]
acc_residuals = deepcopy(Z)[:, 4:]
for i, t in enumerate(transition_times):
vel_residuals[t[0]:t[1]] = vel_segmented[i]
acc_residuals[t[0]:t[1]] = acc_segmented[i]
else:
vel_residuals = vel_segmented
acc_residuals = acc_segmented
elif decorrelation == 'trialized_windowed':
# In this case, the segmentation and windowing has already been done for us
Z = np.concatenate(Z)
vel_residuals, acc_residuals = decorrelate(Z, decorrelation='entire')
# Need to re-apply the windowing/segmentation
# For windows later in the transition period, we will have some window indices that only contain
# a single element
transition_lengths = np.cumsum([len(w) for w in window_indices])
transition_lengths = np.concatenate([[0], transition_lengths]).astype(int)
vel_residuals = [vel_residuals[transition_lengths[i]:transition_lengths[i + 1]]
for i in range(len(transition_lengths) - 1)]
acc_residuals = [acc_residuals[transition_lengths[i]:transition_lengths[i + 1]]
for i in range(len(transition_lengths) - 1)]
return vel_residuals, acc_residuals
# Sequentually regress position onto velocity, predict the reisudals, and then regress position and
# acceleration onto acceleration, and then predict the residuals
def lr_residual_decoder(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag, decoding_window=1):
behavior_dim = Ztrain[0].shape[-1]
Xtest, Xtrain, Ztest, Ztrain = lr_preprocess(Xtest, Xtrain, Ztest, Ztrain, trainlag, testlag,
decoding_window, include_velocity=True, include_acc=True)
# No train test split here
vel_residuals, acc_residuals = decorrelate(np.hstack([Ztrain, Ztest]), decorrelation='entire')
velresidual_decoder = LinearRegression(fit_intercept=True)
velresidual_decoder.fit(Xtrain, vel_residuals[:Xtrain.shape[0], :])
vel_residuals_pred = velresidual_decoder.predict(Xtest)
lr_r2_vel = r2_score(vel_residuals[Xtrain.shape[0]:, :], vel_residuals_pred)
accresidual_decoder = LinearRegression(fit_intercept=True)
accresidual_decoder.fit(Xtrain, acc_residuals[:Xtrain.shape[0], :])
acc_residuals_pred = accresidual_decoder.predict(Xtest)
lr_r2_acc = r2_score(acc_residuals[Xtrain.shape[0]:, :], acc_residuals_pred)
return np.nan, lr_r2_vel, lr_r2_acc, np.nan, np.nan, np.nan, np.nan
def apply_window_residual(X, Z, lag, window, transition_times, decoding_window, include_velocity, include_acc,
subset_selection=None, offsets=None, enforce_full_indices=False, decorrelation='entire'):
include_acc = True
# Apply decoding window
X = form_lag_matrix(X, decoding_window)
Z = Z[decoding_window//2:, :]
Z = Z[:X.shape[0], :]
# Expand state space to include velocity and acceleration
if np.any([include_velocity, include_acc]):
Z, X = expand_state_space([Z], [X], include_velocity, include_acc)
# Flatten list structure imposed by expand_state_space
Z = Z[0]
X= X[0]
# This *also* requires shifting the transition times, as behavior will have been affected
# There is a shift due to the formation of the lag matrix *and* the expansion of the state sapce,
# which will cut off the first 2 sample points, if include_acc is set to true
if decoding_window > 1:
transition_times = [(t[0] - decoding_window//2, t[1] - decoding_window//2) for t in transition_times]
try:
assert(X.shape[0] == Z.shape[0])
except:
pdb.set_trace()
if include_acc:
transition_times = [(t[0] - 2, t[1] - 2) for t in transition_times]
elif include_velocity:
transition_times = [(t[0] - 1, t[1] - 1) for t in transition_times]
# We assume that the subset indices have already been shifted (this is the case in biasvariance_vst)
# Decorrelate the time series according to the desired decorrelation method
# Windowed decorrelation is done after windowing
if decorrelation in ['entire', 'trialized']:
vel_residuals, acc_residuals = decorrelate(Z, decorrelation=decorrelation, transition_times=transition_times)
Z = np.hstack([vel_residuals, acc_residuals])
# Segment the time series with respect to the transition times (including lag)
xx = []
zz = []
valid_idxs = []
# Which reaches had no truncation due to start of the next reach?
full_idxs = []
# If given a single window, duplicate it across all transition times
if len(window) != len(transition_times):
window = [window for _ in range(len(transition_times))]
# If no offsets provided, let it be 0 for all transition times
if offsets is None:
offsets = np.zeros(len(transition_times))
# Needed for decorrelation
window_indices_all = []
for i, (t0, t1) in enumerate(transition_times):
for j, win in enumerate(window[i]):
window_indices = np.arange(t0 + win[0] + offsets[i], t0 + win[1] + offsets[i])
if subset_selection is not None:
# Select only indices that do not belong to the first velocity peak
subset_indices = np.arange(t0, t1)[subset_selection[i]]
window_indices = np.intersect1d(window_indices, subset_indices)
# No matter what, we should remove segments that overlap with the next transition
if i < len(transition_times) - 1:
l1 = len(window_indices)
window_indices = window_indices[window_indices < transition_times[i + 1][0]]
if len(window_indices) == l1:
full_idxs.append(i)
else:
# Or else make sure that we don't exceed the length of the time series
window_indices = window_indices[window_indices < Z.shape[0]]
window_indices = window_indices.astype(int)
zz_ = Z[window_indices]
# Shift x indices by lag
window_indices -= lag
xx_ = X[window_indices]
if len(xx_) > 0:
assert(xx_.shape[0] == zz_.shape[0])
if enforce_full_indices:
if i in full_idxs:
xx.append(xx_)
zz.append(zz_)
else:
xx.append(xx_)
zz.append(zz_)
window_indices_all.append(window_indices)
# try:
# assert(full_idxs[-1] in valid_idxs or i == len(transition_times) - 1)
# except:
# pdb.set_trace()
if decorrelation == 'trialized_windowed':
vel_residuals, acc_residuals = decorrelate(zz, decorrelation='trialized_windowed',
window_indices=window_indices_all)
zz = [np.hstack([v, a]) for (v, a) in zip(vel_residuals, acc_residuals)]
return xx, zz, valid_idxs, full_idxs
def lr_residual_decode_windowed(X, Z, lag, train_windows, test_windows, transition_times,
train_idxs, test_idxs=None,
decoding_window=1, include_velocity=True, include_acc=True,
pkassign=None, apply_pk_to_train=False, offsets=None,
decorrelation='entire'):
# There are 3 versions of this - decorrelation of the entire time series...
# Decorrelation of the trialized time series
# Decorrelation of each windowed segment
behavior_dim = Z.shape[-1]
# We have been given a list (of list) of windows for each transition
win_min = train_windows[0][0]
if win_min >= 0:
win_min = 0
# Filter out by transitions that lie within the train idxs, and stay clear of the start and end
tt_train = [(t, idx) for idx, t in enumerate(transition_times)
if idx in train_idxs and t[0] > (lag + np.abs(win_min)) and t[1] < (Z.shape[0] - lag - np.abs(win_min))]
# Re-assign train idxs removing those reaches that were outside the start/end region
train_idxs = [x[1] for x in tt_train]
tt_train = [x[0] for x in tt_train]
if offsets is not None:
offsets_train = offsets[train_idxs]
else:
offsets_train = None
if apply_pk_to_train:
# Train on the first velocity peak only
assert(np.all([s.size == np.arange(t[0], t[1]).size for (s, t) in zip(pkassign[train_idxs], tt_train)]))
subset_selection = [np.argwhere(np.array(s) == 0).squeeze() for s in pkassign[train_idxs]]
Xtrain, Ztrain, vi1, fi1 = apply_window_residual(X, Z, lag, train_windows, tt_train, decoding_window, include_velocity, include_acc, subset_selection, offsets=offsets_train,
enforce_full_indices=True, decorrelation=decorrelation)
else:
Xtrain, Ztrain, vi1, fi1 = apply_window_residual(X, Z, lag, train_windows, tt_train, decoding_window, include_velocity, include_acc, offsets=offsets_train,
enforce_full_indices=True, decorrelation=decorrelation)
if Xtrain is None:
return np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, None, 0
else:
n = len(Xtrain)