-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonkey_task_comparison_EMG.py
More file actions
1158 lines (1058 loc) · 49.8 KB
/
Monkey_task_comparison_EMG.py
File metadata and controls
1158 lines (1058 loc) · 49.8 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
#!/usr/bin/env python3
"""
Full validation pipeline for EMG decoders.
This pipeline:
1. Loads raw data, maps EMG channels, and groups data into scenarios.
2. For a selected training scenario, builds a continuous dataset.
If a 'split_ratio' is provided, it splits the dataset into a training partition (for PCA & model training) and an internal holdout.
3. Fits a PCA model on the training partition.
4. Trains each of the decoders (GRU, LSTM, Linear, LiGRU) on the training partition.
5. Runs external validation over all test scenarios using different alignment modes.
6. Saves the validation metrics into a DataFrame for later plotting.
A PCA caching mechanism is provided.
"""
import os, sys, random, datetime
import numpy as np
import pandas as pd
import torch, torch.nn as nn, torch.optim as optim
import time
from torch.utils.data import TensorDataset, DataLoader
from sklearn.decomposition import PCA
from numpy.linalg import pinv
from collections import defaultdict
from scipy.ndimage import gaussian_filter1d
from scipy.signal import iirnotch, filtfilt
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
# ---------------- Global Parameters ----------------
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(SEED)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# File paths
COMBINED_PICKLE_FILE = "output.pkl" # Update this to your file location
SAVE_RESULTS_PATH = "df_results_emg_validation_GRU.pkl"
# Data are already binned:
BIN_SIZE = 0.02 # seconds per sample
BIN_FACTOR = 1 # no downsampling
SMOOTHING_LENGTH = 0.05 # for spikes
LAG_BINS = 0
# PCA and decoder parameters
GRU_N_PCA = 14
LSTM_N_PCA = 14
LINEAR_N_PCA = 18
LIGRU_N_PCA = 14
GRU_HIDDEN_DIM = 5
GRU_K_LAG = 16
LSTM_HIDDEN_DIM = 16
LSTM_K_LAG = 16
LINEAR_HIDDEN_DIM = 64
LINEAR_K_LAG = 16
LIGRU_HIDDEN_DIM = 5
LIGRU_K_LAG = 16
NUM_EPOCHS = 300
BATCH_SIZE = 64
LEARNING_RATE = 0.001
DECODERS_TO_RUN = {"GRU"}
# EMG mapping
TARGET_MUSCLES = {"FCR", "FDS", "FDP", "FCU", "ECR", "EDC", "ECU"}
GLOBAL_MUSCLE_MAP = {
'ECR_1': 'ECR', 'ECR_2': 'ECR',
'EDC_1': 'EDC', 'EDC_2': 'EDC',
'FCR_1': 'FCR',
'FCU_1': 'FCU',
'FDS_1': 'FDS', 'FDS_2': 'FDS',
'FDP_1': 'FDP', 'FDP_2': 'FDP',
'ECU_1': 'ECU',
}
# Options for PCA and alignment
RECALC_PCA_EACH_DAY = True
APPLY_ZSCORE = False
REALIGN_PCA_TO_DAY0 = True
# ---------------- Preprocessing Functions ----------------
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def smooth_spike_data(x_2d, bin_size=BIN_SIZE, smoothing_length=SMOOTHING_LENGTH):
sigma = (smoothing_length / bin_size) / 2
out = np.zeros_like(x_2d, dtype=float)
for ch in range(x_2d.shape[1]):
out[:, ch] = gaussian_filter1d(x_2d[:, ch].astype(float), sigma=sigma)
return out
def smooth_emg(emg_array, fs=1000):
rect = np.abs(emg_array)
b_notch, a_notch = iirnotch(60, 30, fs)
rect = filtfilt(b_notch, a_notch, rect, axis=0)
b_lp, a_lp = butter(4, 20/(fs/2), 'low')
return filtfilt(b_lp, a_lp, rect, axis=0)
def map_emg_labels(emg_df):
new_data = {}
count_for_muscle = defaultdict(int)
for col in emg_df.columns:
raw = col.strip().upper()
tmp = raw
while len(tmp) > 0 and (tmp[-1].isdigit() or tmp[-1].isalpha()):
if tmp in GLOBAL_MUSCLE_MAP:
break
tmp = tmp[:-1]
if tmp == "":
tmp = raw
base = GLOBAL_MUSCLE_MAP.get(tmp, None)
if base is None or base not in TARGET_MUSCLES:
continue
count_for_muscle[base] += 1
new_label = f"{base}_{count_for_muscle[base]}"
new_data[new_label] = emg_df[col]
return pd.DataFrame(new_data)
def filter_and_map_emg(df):
new_rows = []
all_cols = set()
for _, row in df.iterrows():
emg_df = row.get("EMG")
if isinstance(emg_df, pd.DataFrame) and not emg_df.empty:
mapped = map_emg_labels(emg_df)
row["EMG"] = mapped
all_cols.update(mapped.columns)
new_rows.append(row)
df_new = pd.DataFrame(new_rows)
sorted_cols = sorted(list(all_cols))
for idx, row in df_new.iterrows():
emg_df = row.get("EMG")
if isinstance(emg_df, pd.DataFrame):
row["EMG"] = emg_df.reindex(columns=sorted_cols, fill_value=0)
return df_new, sorted_cols
def build_continuous_dataset_preprocessed(df, reference_emg_cols=None):
X_list = []
Y_list = []
expected_neurons = [f"neuron{i}" for i in range(1, 97)] # Always expect neurons 1 to 96.
for idx, row in df.iterrows():
spike_df = row["spike_counts"]
emg_val = row["EMG"]
# Ensure spike_df is a DataFrame and not empty.
if not isinstance(spike_df, pd.DataFrame) or spike_df.empty:
continue
# Check which expected columns are missing in the original data.
original_missing = set(expected_neurons) - set(spike_df.columns)
if original_missing:
print(f"[DEBUG] Padding will be applied for row {idx}: missing neurons {sorted(original_missing)}")
# Reindex spike_df so it always contains all expected neuron columns, filling missing with zeros.
spike_df = spike_df.reindex(columns=expected_neurons, fill_value=0)
if emg_val is None:
continue
X_smoothed = smooth_spike_data(spike_df.values, bin_size=BIN_SIZE, smoothing_length=SMOOTHING_LENGTH)
if isinstance(emg_val, pd.DataFrame):
emg_df = emg_val
if reference_emg_cols is not None:
emg_df = emg_df.reindex(reference_emg_cols, axis=1, fill_value=0)
emg_array = emg_df.values
else:
emg_array = np.asarray(emg_val)
Y_smoothed = smooth_emg(emg_array, fs=1000)
X_list.append(X_smoothed)
Y_list.append(emg_array)
if len(X_list) == 0:
return np.empty((0,)), np.empty((0,))
X_big = np.concatenate(X_list, axis=0)
Y_big = np.concatenate(Y_list, axis=0)
return X_big, Y_big
def parse_test_task(tname):
tasks = ['iso', 'iso8', 'wm', 'spr', 'mgpt', 'ball']
tname = tname.lower() # ensure lowercase matching
for t in tasks:
if t in tname:
return t
return "unknown"
def build_scenarios(df):
"""
We'll define:
1) Within monkey: Jango, JacB, Jaco, Theo
2) Across monkey same tasks
3) Cross monkey CROSS tasks => iso/wm/spr vs mg-pt/ball, etc.
"""
def f_jango_iso(r): return (r['monkey']=='Jango') and (r['task']=='iso')
def f_jango_wm(r): return (r['monkey']=='Jango') and (r['task']=='wm')
def f_jango_spr(r): return (r['monkey']=='Jango') and (r['task']=='spr')
def f_jacb_iso(r): return (r['monkey']=='JacB') and (r['task']=='iso')
def f_jacb_wm(r): return (r['monkey']=='JacB') and (r['task']=='wm')
def f_jacb_spr(r): return (r['monkey']=='JacB') and (r['task']=='spr')
def f_jaco_mgpt(r): return (r['monkey']=='Jaco') and (r['task'] in ['mg-pt','mgpt'])
def f_jaco_ball(r): return (r['monkey']=='Jaco') and (r['task']=='ball')
def f_theo_mgpt(r): return (r['monkey']=='Theo') and (r['task'] in ['mg-pt','mgpt'])
def f_theo_ball(r): return (r['monkey']=='Theo') and (r['task']=='ball')
scenarios = []
# -------------------------------
# WITHIN-MONKEY
# Jango
# scenarios.append({
# 'name': 'Jango_iso',
# 'train_filter': f_jango_iso,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jango_wm, 'name': 'jango_wm'},
# {'test_filter': f_jango_spr, 'name': 'jango_spr'}
# ],
# 'force_same_day': True
# })
# scenarios.append({
# 'name': 'Jango_wm',
# 'train_filter': f_jango_wm,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jango_iso, 'name': 'jango_iso'},
# {'test_filter': f_jango_spr, 'name': 'jango_spr'}
# ],
# 'force_same_day': True
# })
# scenarios.append({
# 'name': 'Jango_spr',
# 'train_filter': f_jango_spr,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jango_iso, 'name': 'jango_iso'},
# {'test_filter': f_jango_wm, 'name': 'jango_wm'}
# ],
# 'force_same_day': True
# })
# # JacB
# scenarios.append({
# 'name': 'JacB_iso',
# 'train_filter': f_jacb_iso,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jacb_wm, 'name': 'jacB_wm'},
# {'test_filter': f_jacb_spr, 'name': 'jacB_spr'}
# ],
# 'force_same_day': True
# })
# scenarios.append({
# 'name': 'JacB_wm',
# 'train_filter': f_jacb_wm,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jacb_iso, 'name': 'jacB_iso'},
# {'test_filter': f_jacb_spr, 'name': 'jacB_spr'}
# ],
# 'force_same_day': True
# })
# scenarios.append({
# 'name': 'JacB_spr',
# 'train_filter': f_jacb_spr,
# 'split_ratio': 0.25,
# 'tests': [
# {'test_filter': f_jacb_iso, 'name': 'jacB_iso'},
# {'test_filter': f_jacb_wm, 'name': 'jacB_wm'}
# ],
# 'force_same_day': True
# })
# Jaco
scenarios.append({
'name': 'Jaco_mgpt',
'train_filter': f_jaco_mgpt,
'split_ratio': 0.25,
'tests': [
{'test_filter': f_jaco_ball, 'name': 'jaco_ball'}
],
'force_same_day': True
})
scenarios.append({
'name': 'Jaco_ball',
'train_filter': f_jaco_ball,
'split_ratio': 0.25,
'tests': [
{'test_filter': f_jaco_mgpt, 'name': 'jaco_mgpt'}
],
'force_same_day': True
})
# Theo
scenarios.append({
'name': 'Theo_mgpt',
'train_filter': f_theo_mgpt,
'split_ratio': 0.25,
'tests': [
{'test_filter': f_theo_ball, 'name': 'theo_ball'}
],
'force_same_day': True
})
scenarios.append({
'name': 'Theo_ball',
'train_filter': f_theo_ball,
'split_ratio': 0.25,
'tests': [
{'test_filter': f_theo_mgpt, 'name': 'theo_mgpt'}
],
'force_same_day': True
})
# -------------------------------
# ACROSS-MONKEY (SAME TASK)
# Jango <-> JacB (these monkeys likely never share the same recording day)
# scenarios.append({
# 'name': 'MC_JangoIso2JacBIso',
# 'train_filter': f_jango_iso,
# 'tests': [
# {'test_filter': f_jacb_iso, 'name': 'jacB_iso'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JangoWm2JacBWm',
# 'train_filter': f_jango_wm,
# 'tests': [
# {'test_filter': f_jacb_wm, 'name': 'jacB_wm'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JangoSpr2JacBSpr',
# 'train_filter': f_jango_spr,
# 'tests': [
# {'test_filter': f_jacb_spr, 'name': 'jacB_spr'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JacBIso2JangoIso',
# 'train_filter': f_jacb_iso,
# 'tests': [
# {'test_filter': f_jango_iso, 'name': 'jango_iso'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JacBWm2JangoWm',
# 'train_filter': f_jacb_wm,
# 'tests': [
# {'test_filter': f_jango_wm, 'name': 'jango_wm'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JacBSpr2JangoSpr',
# 'train_filter': f_jacb_spr,
# 'tests': [
# {'test_filter': f_jango_spr, 'name': 'jango_spr'}
# ],
# 'force_same_day': False
# })
# # Jaco <-> Theo (same tasks)
# scenarios.append({
# 'name': 'MC_JacoMgpt2TheoMgpt',
# 'train_filter': f_jaco_mgpt,
# 'tests': [
# {'test_filter': f_theo_mgpt, 'name': 'theo_mgpt'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_JacoBall2TheoBall',
# 'train_filter': f_jaco_ball,
# 'tests': [
# {'test_filter': f_theo_ball, 'name': 'theo_ball'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_TheoMgpt2JacoMgpt',
# 'train_filter': f_theo_mgpt,
# 'tests': [
# {'test_filter': f_jaco_mgpt, 'name': 'jaco_mgpt'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'MC_TheoBall2JacoBall',
# 'train_filter': f_theo_ball,
# 'tests': [
# {'test_filter': f_jaco_ball, 'name': 'jaco_ball'}
# ],
# 'force_same_day': False
# })
# -------------------------------
# ACROSS-MONKEY CROSS-TASK
# (Comparisons where recordings never align by date)
# scenarios.append({
# 'name': 'Cross_JangoIso_2_JacoMgpt',
# 'train_filter': f_jango_iso,
# 'tests': [
# {'test_filter': f_jaco_mgpt, 'name': 'jaco_mgpt'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'Cross_JangoSpr_2_TheoBall',
# 'train_filter': f_jango_spr,
# 'tests': [
# {'test_filter': f_theo_ball, 'name': 'theo_ball'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'Cross_JacBWm_2_JacoBall',
# 'train_filter': f_jacb_wm,
# 'tests': [
# {'test_filter': f_jaco_ball, 'name': 'jaco_ball'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'Cross_JacBIso_2_TheoMgpt',
# 'train_filter': f_jacb_iso,
# 'tests': [
# {'test_filter': f_theo_mgpt, 'name': 'theo_mgpt'}
# ],
# 'force_same_day': False
# })
# scenarios.append({
# 'name': 'Cross_JacoMgpt_2_JangoSpr',
# 'train_filter': f_jaco_mgpt,
# 'tests': [
# {'test_filter': f_jango_spr, 'name': 'jango_spr'}
# ],
# 'force_same_day': False
# })
return scenarios
# ---------------- Sequence Building Functions ----------------
def create_rnn_dataset_continuous(X_arr, Y_arr, seq_len):
if X_arr.shape[0] <= seq_len + LAG_BINS:
return np.empty((0, seq_len, X_arr.shape[1])), np.empty((0, Y_arr.shape[1]))
X_out, Y_out = [], []
T = X_arr.shape[0]
for t in range(seq_len + LAG_BINS, T):
X_out.append(X_arr[t-seq_len-LAG_BINS : t-LAG_BINS])
Y_out.append(Y_arr[t, :]) # ← keeps the channel dimension
return np.asarray(X_out, np.float32), np.asarray(Y_out, np.float32)
def create_linear_dataset_continuous(X_arr, Y_arr, seq_len):
if X_arr.shape[0] <= seq_len + LAG_BINS:
return np.empty((0, seq_len*X_arr.shape[1])), np.empty((0, Y_arr.shape[1]))
X_out, Y_out = [], []
T = X_arr.shape[0]
for t in range(seq_len + LAG_BINS, T):
window = X_arr[t-seq_len-LAG_BINS : t-LAG_BINS].reshape(-1)
X_out.append(window)
Y_out.append(Y_arr[t, :])
return np.asarray(X_out, np.float32), np.asarray(Y_out, np.float32)
def build_dayX_decoder_data(df, day_pca_model, n_pca, seq_len, is_linear=False, reference_emg_cols=None):
X_big, Y_big = build_continuous_dataset_preprocessed(df, reference_emg_cols=reference_emg_cols)
if X_big.shape[0] == 0:
return np.empty((0, )), np.empty((0, ))
if day_pca_model is not None:
z_full = day_pca_model.transform(X_big)
else:
z_full = X_big
X_pca = z_full[:, :n_pca]
if not is_linear:
X_arr, Y_arr = create_rnn_dataset_continuous(X_pca, Y_big, seq_len)
else:
X_arr, Y_arr = create_linear_dataset_continuous(X_pca, Y_big, seq_len)
return X_arr, Y_arr
# ---------------- PCA Caching ----------------
def get_day_pca(df, monkey, date, task, n_components):
X_day, _ = build_continuous_dataset_preprocessed(df)
if X_day.shape[0] == 0:
return None
pca_model = PCA(n_components=n_components, random_state=SEED)
pca_model.fit(X_day)
return pca_model
# ---------------- Validation Functions ----------------
def append_holdout_results(decoder_name, model, X_hold, Y_hold, scenario_name, df_train_day, sc):
preds_hold, vaf_ch_hold, mean_vaf_hold = evaluate_decoder(model, X_hold, Y_hold)
date_val = df_train_day.iloc[0]["date"] if "date" in df_train_day.columns else None
train_task = sc.get("task", "Unknown")
# List the external alignment modes for which you want a diagonal entry.
align_modes = ["bland", "recalculated", "realign"]
results = []
for mode in align_modes:
result = {
"scenario_name": scenario_name,
"train_monkey": sc.get("monkey", "Unknown"),
"test_monkey": sc.get("monkey", "Unknown"),
"train_task": train_task,
"test_task": train_task,
# Use the training task as the test name for the diagonal.
"test_name": train_task,
"decoder_type": decoder_name,
"alignment_mode": mode,
"mean_VAF": mean_vaf_hold,
"VAF_ch": vaf_ch_hold,
"timestamp": datetime.datetime.now(),
"train_date": date_val,
"test_date": date_val,
"holdout_type": "internal",
"date": date_val
}
results.append(result)
return results
def compute_vaf_1d(y_true, y_pred):
var_true = np.var(y_true)
if var_true < 1e-12:
return np.nan
var_resid = np.var(y_true - y_pred)
return 1.0 - (var_resid / var_true)
def compute_multichannel_vaf(y_true, y_pred):
n_ch = y_true.shape[1]
return np.array([compute_vaf_1d(y_true[:, ch], y_pred[:, ch]) for ch in range(n_ch)])
def evaluate_decoder(model, X_val, Y_val, context=""):
print(f"[DEBUG]{context} - Evaluating decoder {model.__class__.__name__}: X_val shape: {X_val.shape}, Y_val shape: {Y_val.shape}")
model.eval()
preds = []
with torch.no_grad():
for i in range(0, len(X_val), BATCH_SIZE):
batch_X = torch.tensor(X_val[i:i+BATCH_SIZE], dtype=torch.float32).to(DEVICE)
out = model(batch_X)
preds.append(out.cpu().numpy())
if preds:
preds = np.concatenate(preds, axis=0)
else:
preds = np.empty((0, ))
vaf_ch = compute_multichannel_vaf(Y_val, preds)
mean_vaf = np.nanmean(vaf_ch)
return preds, vaf_ch, mean_vaf
def validate_scenario(train_scenario, test_scenario, model, n_pca, seq_len, is_linear=False,
alignment_mode="bland", reference_emg_cols=None, force_external=False):
# If not forcing external, and the test DataFrame equals the training DataFrame,
# run the internal holdout branch.
if not force_external and train_scenario["df"].equals(test_scenario["df"]):
split_ratio = train_scenario.get('split_ratio', None)
if split_ratio is not None and 0 < split_ratio < 1:
X_train_raw, Y_train_raw = build_continuous_dataset_preprocessed(train_scenario["df"],
reference_emg_cols=reference_emg_cols)
if X_train_raw.shape[0] == 0:
print("Training data empty for internal holdout validation.")
return None, None, None
z_train = train_scenario["pca_model"].transform(X_train_raw)
X_train_proj = z_train[:, :n_pca]
if not is_linear:
X_seq, Y_seq = create_rnn_dataset_continuous(X_train_proj, Y_train_raw, seq_len)
else:
X_seq, Y_seq = create_linear_dataset_continuous(X_train_proj, Y_train_raw, seq_len)
X_tr, X_val, Y_tr, Y_val = train_test_split(X_seq, Y_seq, test_size=split_ratio, random_state=SEED, shuffle=False)
preds, vaf_ch, mean_vaf = evaluate_decoder(model, X_val, Y_val)
print("Internal Holdout Validation:")
print(f"Mean VAF: {mean_vaf:.4f}")
return preds, vaf_ch, mean_vaf
# --- External Validation Branch ---
X_test_raw, Y_test_raw = build_continuous_dataset_preprocessed(test_scenario["df"],
reference_emg_cols=reference_emg_cols)
if X_test_raw.shape[0] == 0:
print("Test data empty.")
return None, None, None
if alignment_mode == "bland":
print("Alignment mode: bland")
transform_matrix = train_scenario["pca_model"].components_.T
X_test_proj = X_test_raw @ transform_matrix
X_test_proj = X_test_proj[:, :n_pca]
elif alignment_mode == "recalculated":
print("Alignment mode: recalculated")
test_date = test_scenario["df"].iloc[0]["date"] if "date" in test_scenario["df"].columns else None
test_pca = get_day_pca(test_scenario["df"],
monkey=test_scenario["monkey"],
date=test_date,
task=test_scenario["task"],
n_components=n_pca)
if test_pca is None:
print("Failed to compute test PCA; defaulting to bland.")
transform_matrix = train_scenario["pca_model"].components_.T
X_test_proj = X_test_raw @ transform_matrix
X_test_proj = X_test_proj[:, :n_pca]
else:
X_test_proj = test_pca.transform(X_test_raw)[:, :n_pca]
elif alignment_mode == "realign":
print("Alignment mode: realign")
test_date = test_scenario["df"].iloc[0]["date"] if "date" in test_scenario["df"].columns else None
test_pca = get_day_pca(test_scenario["df"],
monkey=test_scenario["monkey"],
date=test_date,
task=test_scenario["task"],
n_components=n_pca)
if test_pca is None:
print("Failed to compute test PCA; defaulting to bland.")
transform_matrix = train_scenario["pca_model"].components_.T
X_test_proj = X_test_raw @ transform_matrix
X_test_proj = X_test_proj[:, :n_pca]
else:
V_train = train_scenario["pca_model"].components_.T[:, :n_pca]
V_test = test_pca.components_.T[:, :n_pca]
R = pinv(V_test) @ V_train
z_test_local = test_pca.transform(X_test_raw)
X_test_proj = (z_test_local[:, :n_pca]) @ R
else:
print(f"Unknown alignment_mode: {alignment_mode}; defaulting to bland.")
transform_matrix = train_scenario["pca_model"].components_.T
X_test_proj = X_test_raw @ transform_matrix
X_test_proj = X_test_proj[:, :n_pca]
if not is_linear:
X_val, Y_val = create_rnn_dataset_continuous(X_test_proj, Y_test_raw, seq_len)
else:
X_val, Y_val = create_linear_dataset_continuous(X_test_proj, Y_test_raw, seq_len)
if X_val.shape[0] == 0:
print("Insufficient test samples after sequence construction.")
return None, None, None
preds, vaf_ch, mean_vaf = evaluate_decoder(model, X_val, Y_val)
return preds, vaf_ch, mean_vaf
# Example of how to loop through scenarios and decoders:
# def run_validation(scenarios, trained_models, reference_emg_cols):
# results = []
# for scenario in scenarios:
# train_df = scenario["df"]
# if "pca_model" not in scenario:
# X_train_tmp, _ = build_continuous_dataset_preprocessed(train_df, reference_emg_cols=reference_emg_cols)
# if X_train_tmp.shape[0] > 0:
# max_dim = max(GRU_N_PCA, LSTM_N_PCA, LINEAR_N_PCA, LIGRU_N_PCA)
# pca_model = PCA(n_components=max_dim, random_state=SEED)
# pca_model.fit(X_train_tmp)
# scenario["pca_model"] = pca_model
# else:
# continue
# if "tests" in scenario:
# for test_def in scenario["tests"]:
# test_scenario = {
# "scenario_name": f"{scenario['monkey']}_{test_def['name']}",
# "monkey": scenario["monkey"],
# "task": test_def["name"],
# "df": scenario["df"] # Modify if you want a different test df.
# }
# for decoder_name, model in trained_models.items():
# if decoder_name == "GRU":
# n_pca_val = GRU_N_PCA
# seq_len = GRU_K_LAG
# elif decoder_name == "LSTM":
# n_pca_val = LSTM_N_PCA
# seq_len = LSTM_K_LAG
# elif decoder_name == "Linear":
# n_pca_val = LINEAR_N_PCA
# seq_len = LINEAR_K_LAG
# elif decoder_name == "LiGRU":
# n_pca_val = LIGRU_N_PCA
# seq_len = LIGRU_K_LAG
# else:
# continue
# for align_mode in ["bland", "recalculated", "realign"]:
# preds, vaf_ch, mean_vaf = validate_scenario(
# train_scenario=scenario,
# test_scenario=test_scenario,
# model=model,
# n_pca=n_pca_val,
# seq_len=seq_len,
# is_linear=(decoder_name=="Linear"),
# alignment_mode=align_mode,
# reference_emg_cols=reference_emg_cols
# )
# result = {
# "scenario_name": scenario["scenario_name"],
# "train_monkey": scenario["monkey"],
# "train_task": scenario["task"],
# "test_name": test_def["name"],
# "decoder_type": decoder_name,
# "alignment_mode": align_mode,
# "mean_VAF": mean_vaf,
# "VAF_ch": vaf_ch,
# "timestamp": datetime.datetime.now(),
# "date": train_df.iloc[0]["date"] if "date" in train_df.columns else None
# }
# results.append(result)
# print(f"[RESULT] {result}")
# df_results = pd.DataFrame(results)
# df_results.to_pickle(SAVE_RESULTS_PATH)
# return df_results
# ---------------- Model Definitions ----------------
class GRUDecoder(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.gru = nn.GRU(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, _ = self.gru(x)
out = out[:, -1, :]
return self.fc(out)
class LSTMDecoder(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
out, _ = self.lstm(x)
out = out[:, -1, :]
return self.fc(out)
class LinearLagDecoder(nn.Module):
def __init__(self, input_dim, hidden_dim, output_size):
super().__init__()
self.lin1 = nn.Linear(input_dim, hidden_dim)
self.act = nn.ReLU()
self.lin2 = nn.Linear(hidden_dim, output_size)
def forward(self, x):
x = self.lin1(x)
x = self.act(x)
return self.lin2(x)
class LiGRUCell(nn.Module):
def __init__(self, input_size, hidden_size):
super().__init__()
self.x2z = nn.Linear(input_size, hidden_size)
self.h2z = nn.Linear(hidden_size, hidden_size, bias=False)
self.x2h = nn.Linear(input_size, hidden_size)
self.h2h = nn.Linear(hidden_size, hidden_size, bias=False)
def forward(self, x, h):
z = torch.sigmoid(self.x2z(x) + self.h2z(h))
h_candidate = torch.relu(self.x2h(x) + self.h2h(h))
return (1 - z) * h + z * h_candidate
class LiGRUDecoder(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.hidden_size = hidden_size
self.cell = LiGRUCell(input_size, hidden_size)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
batch_size, seq_len, _ = x.size()
h = torch.zeros(batch_size, self.hidden_size, device=x.device)
for t in range(seq_len):
h = self.cell(x[:, t, :], h)
return self.fc(h)
# ---------------- Training Function ----------------
def train_model(model, X_train, Y_train, num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr=LEARNING_RATE):
dataset = TensorDataset(torch.tensor(X_train, dtype=torch.float32),
torch.tensor(Y_train, dtype=torch.float32))
loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
optimizer = optim.Adam(model.parameters(), lr=lr)
criterion = nn.MSELoss()
for ep in range(1, num_epochs + 1):
model.train()
total_loss = 0.0
for xb, yb in loader:
xb, yb = xb.to(DEVICE), yb.to(DEVICE)
optimizer.zero_grad()
pred = model(xb)
loss = criterion(pred, yb)
loss.backward()
optimizer.step()
total_loss += loss.item()
if ep % 10 == 0:
print(f"Epoch {ep}/{num_epochs}: Loss = {total_loss/len(loader):.4f}")
return model
# ---------------- Main Pipeline ----------------
def main():
set_seed(SEED)
print(f"[INFO] Using device: {DEVICE}")
# 1. Load raw data and preprocess EMG.
df_raw = pd.read_pickle(COMBINED_PICKLE_FILE)
try:
df_raw["date"] = pd.to_datetime(df_raw["date"])
except Exception as e:
print("Error converting date column:", e)
df_raw["date"] = pd.to_datetime(df_raw["date"], errors="coerce")
df_proc, train_emg_cols = filter_and_map_emg(df_raw)
print(f"[INFO] Processed df shape: {df_proc.shape}")
all_results = []
training_times = {}
# 2. Build scenarios.
scenarios = build_scenarios(df_proc)
print(f"[INFO] Found {len(scenarios)} scenarios.")
# 3. Process each scenario.
for sc in scenarios:
scenario_name = sc.get("name", "UnnamedScenario")
print(f"\n[INFO] Processing scenario: {scenario_name}")
# Set default metadata if not present.
if "monkey" not in sc:
sc["monkey"] = scenario_name.split("_")[0]
if "task" not in sc:
sc["task"] = scenario_name.split("_")[-1]
# Apply the train_filter to obtain training data.
df_train = df_proc[df_proc.apply(sc['train_filter'], axis=1)].copy()
if df_train.empty:
print(f"[WARNING] Scenario {scenario_name} has no training data; skipping.")
continue
# Always split training data by day.
if "date" in df_train.columns:
unique_train_days = sorted(df_train["date"].unique())
else:
unique_train_days = [None]
if scenario_name not in training_times:
training_times[scenario_name] = {}
# Loop over each training day.
for train_day in unique_train_days:
if train_day is not None:
df_train_day = df_train[df_train["date"] == train_day].copy()
day_train_str = train_day.strftime("%Y%m%d") if hasattr(train_day, "strftime") else str(train_day)
else:
df_train_day = df_train.copy()
day_train_str = "all"
if df_train_day.empty:
print(f"[WARNING] Scenario {scenario_name} - Train Day {day_train_str}: no training data; skipping.")
continue
print(f"[INFO] Scenario {scenario_name} - Train Day {day_train_str}: {df_train_day.shape[0]} samples")
# Update the scenario with this day-specific training DataFrame.
sc["df"] = df_train_day
# Build continuous training dataset.
X_train_raw, Y_train_raw = build_continuous_dataset_preprocessed(df_train_day, reference_emg_cols=train_emg_cols)
if X_train_raw.shape[0] == 0:
print(f"[WARNING] Scenario {scenario_name} - Train Day {day_train_str}: no raw training data; skipping.")
continue
# Fit PCA on training data.
max_dim = max(GRU_N_PCA, LSTM_N_PCA, LINEAR_N_PCA, LIGRU_N_PCA)
pca_train = PCA(n_components=max_dim, random_state=SEED)
pca_train.fit(X_train_raw)
sc["pca_model"] = pca_train
print(f"[DEBUG] Scenario {scenario_name} - Train Day {day_train_str}: PCA components shape: {pca_train.components_.shape}")
# Build training sequences.
X_gru_train, Y_gru_train = build_dayX_decoder_data(df_train_day, pca_train, GRU_N_PCA, GRU_K_LAG, is_linear=False, reference_emg_cols=train_emg_cols)
X_lstm_train, Y_lstm_train = build_dayX_decoder_data(df_train_day, pca_train, LSTM_N_PCA, LSTM_K_LAG, is_linear=False, reference_emg_cols=train_emg_cols)
X_lin_train, Y_lin_train = build_dayX_decoder_data(df_train_day, pca_train, LINEAR_N_PCA, LINEAR_K_LAG, is_linear=True, reference_emg_cols=train_emg_cols)
X_ligru_train, Y_ligru_train = build_dayX_decoder_data(df_train_day, pca_train, LIGRU_N_PCA, LIGRU_K_LAG, is_linear=False, reference_emg_cols=train_emg_cols)
print("============= INPUT SHAPE =============")
print(f" GRU : X: {X_gru_train.shape} , Y: {Y_gru_train.shape}")
print(f" LSTM : X: {X_lstm_train.shape} , Y: {Y_lstm_train.shape}")
print(f" Linear : X: {X_lin_train.shape} , Y: {Y_lin_train.shape}")
print(f" LiGRU : X: {X_ligru_train.shape} , Y: {Y_ligru_train.shape}")
n_emg_channels = Y_train_raw.shape[1]
# --- Internal Holdout Split ---
split_ratio = sc.get("split_ratio", None)
if split_ratio is not None and 0 < split_ratio < 1:
print(f"[INFO] Scenario {scenario_name} - Train Day {day_train_str}: Applying internal holdout split (ratio={split_ratio}).")
X_gru_tr, X_gru_hold, Y_gru_tr, Y_gru_hold = train_test_split(X_gru_train, Y_gru_train, test_size=split_ratio, random_state=SEED, shuffle=False)
X_lstm_tr, X_lstm_hold, Y_lstm_tr, Y_lstm_hold = train_test_split(X_lstm_train, Y_lstm_train, test_size=split_ratio, random_state=SEED, shuffle=False)
X_lin_tr, X_lin_hold, Y_lin_tr, Y_lin_hold = train_test_split(X_lin_train, Y_lin_train, test_size=split_ratio, random_state=SEED, shuffle=False)
X_ligru_tr, X_ligru_hold, Y_ligru_tr, Y_ligru_hold = train_test_split(X_ligru_train, Y_ligru_train, test_size=split_ratio, random_state=SEED, shuffle=False)
else:
X_gru_tr, X_gru_hold, Y_gru_tr, Y_gru_hold = X_gru_train, None, Y_gru_train, None
X_lstm_tr, X_lstm_hold, Y_lstm_tr, Y_lstm_hold = X_lstm_train, None, Y_lstm_train, None
X_lin_tr, X_lin_hold, Y_lin_tr, Y_lin_hold = X_lin_train, None, Y_lin_train, None
X_ligru_tr, X_ligru_hold, Y_ligru_tr, Y_ligru_hold = X_ligru_train, None, Y_ligru_train, None
# --- Initialize Models ---
trained_models = {}
if "GRU" in DECODERS_TO_RUN:
gru_model = GRUDecoder(GRU_N_PCA, GRU_HIDDEN_DIM, n_emg_channels).to(DEVICE)
trained_models["GRU"] = gru_model
if "LSTM" in DECODERS_TO_RUN:
lstm_model = LSTMDecoder(LSTM_N_PCA, LSTM_HIDDEN_DIM, n_emg_channels).to(DEVICE)
trained_models["LSTM"] = lstm_model
if "Linear" in DECODERS_TO_RUN:
linear_model = LinearLagDecoder(LINEAR_N_PCA * LINEAR_K_LAG,
LINEAR_HIDDEN_DIM, n_emg_channels).to(DEVICE)
trained_models["Linear"] = linear_model
if "LiGRU" in DECODERS_TO_RUN:
ligru_model = LiGRUDecoder(LIGRU_N_PCA, LIGRU_HIDDEN_DIM, n_emg_channels).to(DEVICE)
trained_models["LiGRU"] = ligru_model
# --- Train Each Model on the Training Partition ---
print(f"[INFO] Scenario {scenario_name} - Train Day {day_train_str}: Training Models...")
if "GRU" in DECODERS_TO_RUN:
print("Training GRU...")
start_time = time.time()
gru_model = train_model(gru_model, X_gru_tr, Y_gru_tr,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr=LEARNING_RATE)
elapse = time.time() - start_time
print(f"GRU training completed in {elapse} s.")
if "LSTM" in DECODERS_TO_RUN:
print("Training LSTM...")
start_time = time.time()
lstm_model = train_model(lstm_model, X_lstm_tr, Y_lstm_tr,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr=LEARNING_RATE)
elapse = time.time() - start_time
print(f"LSTM training completed in {elapse} s.")
if "Linear" in DECODERS_TO_RUN:
print("Training Linear...")
start_time = time.time()
linear_model = train_model(linear_model, X_lin_tr, Y_lin_tr,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr=LEARNING_RATE)
elapse = time.time() - start_time
print(f"Linear training completed in {elapse} s.")
if "LiGRU" in DECODERS_TO_RUN:
print("Training LiGRU...")
start_time = time.time()
ligru_model = train_model(ligru_model, X_ligru_tr, Y_ligru_tr,
num_epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr=LEARNING_RATE)
elapse = time.time() - start_time
print(f"LiGRU training completed in {elapse} s.")
has_holdout = (
("GRU" in DECODERS_TO_RUN and X_gru_hold is not None) or
("LSTM" in DECODERS_TO_RUN and X_lstm_hold is not None) or
("Linear" in DECODERS_TO_RUN and X_lin_hold is not None) or
("LiGRU" in DECODERS_TO_RUN and X_ligru_hold is not None)
)
# --- Internal Holdout Evaluation ---
if has_holdout:
if "GRU" in DECODERS_TO_RUN:
all_results.extend(append_holdout_results("GRU", gru_model,
X_gru_hold, Y_gru_hold,
scenario_name, df_train_day, sc))
if "LSTM" in DECODERS_TO_RUN:
all_results.extend(append_holdout_results("LSTM", lstm_model,
X_lstm_hold, Y_lstm_hold,
scenario_name, df_train_day, sc))
if "Linear" in DECODERS_TO_RUN:
all_results.extend(append_holdout_results("Linear", linear_model,
X_lin_hold, Y_lin_hold,
scenario_name, df_train_day, sc))
if "LiGRU" in DECODERS_TO_RUN:
all_results.extend(append_holdout_results("LiGRU", ligru_model,
X_ligru_hold, Y_ligru_hold,
scenario_name, df_train_day, sc))
# --- External Validation ---
if sc.get("force_same_day", True):
rec_date = df_train_day.iloc[0]["date"]
monkey_val = sc.get("monkey", "Unknown")
# Use global data for this day & monkey.
df_test_day = df_proc[(df_proc["date"] == rec_date) & (df_proc["monkey"] == monkey_val)].copy()
day_test_str = rec_date.strftime("%Y%m%d") if hasattr(rec_date, "strftime") else str(rec_date)
# print(f"[DEBUG] Global data for date {day_test_str} (monkey {monkey_val}):")
if "task" in df_test_day.columns:
print(df_test_day["task"].value_counts())
test_filters = sc.get("tests", [])
for test_def in test_filters:
test_name = test_def["name"]
train_task = sc.get("task", "Unknown")