-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxgboost_analysis.py
1675 lines (1491 loc) · 82.8 KB
/
xgboost_analysis.py
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 matplotlib.pyplot as plt
import numpy as np
import os
import sys
import time
import scipy
import h5py
import pandas as pd
from itertools import combinations
from scipy.stats.mstats import zscore
from scipy import ndimage
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import copy
import seaborn as sns
from matplotlib import interactive
import utils_cabmi as uc
import xgboost
import shap
from matplotlib.ticker import LogitLocator
interactive(True)
def basic_entry(folder, animal, day, df_e2, speed=False):
'''
to generate an entry to the pd dataframe with the basic features
Needs to have a folder with all_processed together
'''
file_template = "full_{}_{}__data.hdf5"
file_name = os.path.join(folder, animal, file_template.format(animal, day))
folder_proc = os.path.join(folder, 'processed')
f = h5py.File(file_name, 'r')
com = np.asarray(f['com'])
nerden = np.asarray(f['nerden'])
ens_neur = np.asarray(f['ens_neur'])
e2_neur = np.asarray(df_e2[animal][day])
e1_neur = copy.deepcopy(ens_neur)
online_data = np.asarray(f['online_data'])[:, 2:]
dff = np.asarray(f['dff'])
cursor = np.asarray(f['cursor'])
startBMI = np.asarray(f['trial_start'])[0]
f.close()
if np.isnan(np.sum(ens_neur)):
print('Only ' + str(4 - np.sum(np.isnan(ens_neur))) + ' ensemble neurons')
if np.isnan(np.sum(e2_neur)):
print('Only ' + str(2 - np.sum(np.isnan(e2_neur))) + ' e2 neurons')
if np.nansum(e2_neur) > 0:
for i in np.arange(len(e2_neur)):
e1_neur[np.where(ens_neur == e2_neur[i])[0]] = np.nan
e1_neur = np.int16(e1_neur[~np.isnan(e1_neur)])
ens_neur = np.int16(ens_neur[~np.isnan(ens_neur)])
e2_neur = np.int16(e2_neur[~np.isnan(e2_neur)])
if len(ens_neur) > 0:
com_ens = com[ens_neur, :]
com_e2 = com[e2_neur, :]
com_e1 = com[e1_neur, :]
dff_ens = dff[ens_neur, :]
# xyz position
depth_mean = np.nanmean(com_ens[:, 2])
depth_max = np.nanmax(com_ens[:, 2])
depth_min = np.nanmin(com_ens[:, 2])
# if len(e2_neur) > 0:
# depth_mean_e2 = np.nanmean(com_e2[:,2])
# depth_max_e2 = np.nanmax(com_e2[:,2])
# else:
# depth_mean_e2 = np.nan
# depth_max_e2 = np.nan
# distance
if ens_neur.shape[0] > 1:
if (e1_neur.shape[0] > 0) & (e2_neur.shape[0] > 0):
auxdist = []
for nne2 in np.arange(e2_neur.shape[0]):
for nne1 in np.arange(e1_neur.shape[0]):
auxdist.append(scipy.spatial.distance.euclidean(com_e2[nne2, :], com_e1[nne1, :]))
dist_mean = np.nanmean(auxdist)
dist_max = np.nanmax(auxdist)
dist_min = np.nanmin(auxdist)
else:
dist_mean = np.nan
dist_max = np.nan
dist_min = np.nan
# diff of depth
auxddepth = []
for nn in np.arange(ens_neur.shape[0]):
for nns in np.arange(nn + 1, ens_neur.shape[0]):
auxddepth.append(scipy.spatial.distance.euclidean(com_ens[nn, 2], com_ens[nns, 2]))
diffdepth_mean = np.nanmean(auxddepth)
diffdepth_max = np.nanmax(auxddepth)
diffdepth_min = np.nanmin(auxddepth)
# diff in xy
auxdxy = []
for nn in np.arange(ens_neur.shape[0]):
for nns in np.arange(nn + 1, ens_neur.shape[0]):
auxdxy.append(scipy.spatial.distance.euclidean(com_ens[nn, :2], com_ens[nns, :2]))
diffxy_mean = np.nanmean(auxdxy)
diffxy_max = np.nanmax(auxdxy)
diffxy_min = np.nanmin(auxdxy)
else:
dist_mean = np.nan
dist_max = np.nan
dist_min = np.nan
diffdepth_mean = np.nan
diffdepth_max = np.nan
diffdepth_min = np.nan
diffxy_mean = np.nan
diffxy_max = np.nan
diffxy_min = np.nan
# dynamic range
# there are some dff that are crazy weird, to avoid them for deteriorating the dataset any std>1 will be ignored
auxpostwhostd = []
for nn in np.arange(dff_ens.shape[0]):
aux_std = np.nanstd(dff_ens[nn, startBMI:])
if aux_std < 1:
auxpostwhostd.append(aux_std)
post_whole_std_mean = np.nanmean(auxpostwhostd)
post_whole_std_max = np.nanmax(auxpostwhostd)
post_whole_std_min = np.nanmin(auxpostwhostd)
auxpostbasestd = []
for nn in np.arange(dff_ens.shape[0]):
aux_std = np.nanstd(dff_ens[nn, :startBMI])
if aux_std < 1:
auxpostbasestd.append(aux_std)
post_base_std_mean = np.nanmean(auxpostbasestd)
post_base_std_max = np.nanmax(auxpostbasestd)
post_base_std_min = np.nanmin(auxpostbasestd)
else:
depth_mean = np.nan
depth_max = np.nan
depth_min = np.nan
x_mean = np.nan
x_max = np.nan
x_min = np.nan
y_mean = np.nan
y_max = np.nan
y_min = np.nan
dist_mean = np.nan
dist_max = np.nan
dist_min = np.nan
diffdepth_mean = np.nan
diffdepth_max = np.nan
diffdepth_min = np.nan
diffxy_mean = np.nan
diffxy_max = np.nan
diffxy_min = np.nan
post_whole_std_mean = np.nan
post_whole_std_max = np.nan
post_whole_std_min = np.nan
post_base_std_mean = np.nan
post_base_std_max = np.nan
post_base_std_min = np.nan
auxonstd = []
for nn in np.arange(online_data.shape[1]):
pseudo_dff = (online_data[:, nn] - np.nanmean(online_data[:, nn])) / np.nanmean(online_data[:, nn])
auxonstd.append(np.nanstd(pseudo_dff))
onstd_mean = np.nanmean(auxonstd)
onstd_max = np.nanmax(auxonstd)
onstd_min = np.nanmin(auxonstd)
auxcursorstd = []
cursor_std = np.nanstd(cursor)
row_entry = np.asarray([depth_mean, depth_max, depth_min, \
dist_mean, dist_max, dist_min, diffdepth_mean, diffdepth_max, \
diffdepth_min, diffxy_mean, diffxy_max, diffxy_min, \
onstd_mean, onstd_max, onstd_min, post_whole_std_mean, post_whole_std_max,
post_whole_std_min, \
post_base_std_mean, post_base_std_max, post_base_std_min, cursor_std])
return row_entry, ens_neur, e2_neur, e1_neur
def plot_results(folder_plots, df_aux, first_ind=0, single_animal=True, mode='basic'):
'''
Function to plot all the basic results in terms of linear regression
'''
columns = df_aux.columns.tolist()
# depth mean
columns_ler = columns[4:10]
if mode == 'basic':
columns_aux = columns[10:32] ## basic
figsiz = (12, 20)
sbx = 5
sby = 5
elif mode == 'SNR':
columns_aux = columns[32:35] ## SNR
figsiz = (8, 4)
sbx = 1
sby = 3
elif mode == 'ce':
columns_aux = [columns[35]] ## SNR
figsiz = (4, 4)
sbx = 1
sby = 1
elif mode == 'GC':
columns_aux = columns[36:] ## SNR
figsiz = (8, 8)
sbx = 4
sby = 4
for cc, col in enumerate(columns_ler):
fig1 = plt.figure(figsize=figsiz)
for tt, btest in enumerate(columns_aux):
ax = fig1.add_subplot(sbx, sby, tt + 1)
ax = sns.regplot(x=btest, y=col, data=df_aux)
ax.set_xticks([])
if single_animal:
plotname = os.path.join(folder_plots, 'per_animal', df_aux.loc[first_ind][0] + '_' + mode + '_' + col)
else:
plotname = os.path.join(folder_plots, mode + '_' + col)
fig1.savefig(plotname + '.png', bbox_inches="tight")
fig1.savefig(plotname + '.eps', bbox_inches="tight")
plt.close('all')
def create_dataframe(folder_main, file_csv, to_plot=True):
'''
create pandas dataframe to include all the results together and save it
folder_main = main folder where everything will be stored in subfolders
file_csv = excel file where the learning results were stored
to_plot = boolean to plot or not
saves df
'''
# ineficient way to create the dataframe, but I want to be f* sure that each entry is correct.
folder = os.path.join(folder_main, 'processed')
folder_plots = os.path.join(folder_main, 'plots', 'learning_regressions')
if not os.path.exists(folder_plots):
os.makedirs(folder_plots)
folder_snr = os.path.join(folder_main, 'onlineSNR')
to_save_df = os.path.join(folder_main, 'df_all.hdf5')
to_load_pick = os.path.join(folder_main, 'cursor_engagement.p')
to_load_e2 = os.path.join(folder_main, 'e2_neurs.p')
animals = os.listdir(folder)
df_results = pd.read_csv(file_csv)
df_ce = pd.read_pickle(to_load_pick)
df_e2 = pd.read_pickle(to_load_e2)
columns_res = df_results.columns.tolist()
columns_basic = ['depth_mean', 'depth_max', 'depth_min', \
'dist_mean', 'dist_max', 'dist_min', 'diffdepth_mean', 'diffdepth_max', \
'diffdepth_min', 'diffxy_mean', 'diffxy_max', 'diffxy_min', \
'onstd_mean', 'onstd_max', 'onstd_min', 'post_whole_std_mean', 'post_whole_std_max',
'post_whole_std_min',
'post_base_std_mean', 'post_base_std_max', 'post_base_std_min', 'cursor_std']
columns = columns_res + columns_basic
columns.insert(3, 'ITPTlabel')
df = pd.DataFrame(columns=columns)
# obtain basic features
print('obtaining basic features!')
mat_ens_ind = np.zeros((len(animals), 25, 4)) + np.nan
mat_e2_ind = np.zeros((len(animals), 25, 2)) + np.nan
mat_e1_ind = np.zeros((len(animals), 25, 2)) + np.nan
for aa, animal in enumerate(animals):
folder_path = os.path.join(folder, animal)
filenames = os.listdir(folder_path)
mat_animal = np.zeros((len(filenames), len(columns_basic))) + np.nan
for dd, filename in enumerate(filenames):
day = filename[-17:-11]
print('Analyzing animal: ' + animal + ' day: ' + day)
try:
mat_animal[dd, :], ens_neur, e2_neur, e1_neur = basic_entry(folder, animal, day, df_e2)
mat_ens_ind[aa, dd, :len(ens_neur)] = ens_neur
mat_e2_ind[aa, dd, :len(e2_neur)] = e2_neur
mat_e1_ind[aa, dd, :len(e1_neur)] = e1_neur
except OSError:
print('day: ' + day + ' not obtained. ERRRRROOOOOOOOOOOOOOR')
break
df_res_animal = df_results.loc[df_results['animal'] == animal]
if len(filenames) != len(df_res_animal):
print('Experiment number missmatch!!. STOPING')
break
if animal[:2] == 'IT':
aux_label = 0
elif animal[:2] == 'PT':
aux_label = 1
df_res_animal.insert(3, 'ITPTlabel', np.repeat([aux_label], len(df_res_animal)))
df_basic = pd.DataFrame(mat_animal, columns=columns_basic, index=df_res_animal.index)
df_animal = df_res_animal.join(df_basic)
if to_plot:
plot_results(folder_plots, df_animal, df_res_animal.index[0])
df = df.append(df_animal, ignore_index=True)
if to_plot:
plot_results(folder_plots, df, single_animal=False)
df['ITPTlabel'] = pd.to_numeric(df['ITPTlabel'])
# obtain snr features
print('obtaining snrs')
snr_vector_mean = [] # np.zeros(len(df))
snr_vector_max = [] # np.zeros(len(df))
snr_vector_min = [] # np.zeros(len(df))
number_snrs = np.zeros(len(animals))
for aa, animal in enumerate(animals):
folder_path = os.path.join(folder_snr, animal)
filenames = os.listdir(folder_path)
number_snrs[aa] = len(filenames)
for dd, filename in enumerate(filenames):
print('Analyzing animal: ' + animal + ' day: ' + filename)
f = h5py.File(os.path.join(folder_path, filename), 'r')
aux_snr = np.asarray(f['SNR_ens'])
f.close()
snr_vector_mean.append(np.nanmean(aux_snr))
snr_vector_max.append(np.nanmax(aux_snr))
snr_vector_min.append(np.nanmin(aux_snr))
try:
df['onlineSNRmean'] = snr_vector_mean
df['onlineSNRmax'] = snr_vector_max
df['onlineSNRmin'] = snr_vector_min
except ValueError:
print('BIG ERROR!!! sizes dont match for SNR and basics')
if to_plot:
plot_results(folder_plots, df, single_animal=False, mode='SNR')
# obtain cursor engagement
print('obtaining cursor engagement')
df['cursor_eng'] = np.nan
for aa, animal in enumerate(animals):
folder_path = os.path.join(folder, animal)
filenames = os.listdir(folder_path)
andf = df_ce.loc[df_ce['Animal'] == animal]
for dd, filename in enumerate(filenames):
day = filename[-17:-11]
auxr2 = andf[andf['Date'] == day]['R2 Values'].values
if len(auxr2) > 0:
dfind = df.index[(df['animal'] == animal) & (df['day'] == int(day))]
df.loc[dfind, 'cursor_eng'] = auxr2
if to_plot:
plot_results(folder_plots, df, single_animal=False, mode='ce')
# obtain connectivity values
df['GC_raw_ens_ens'] = np.nan
df['GC_per_ens_ens'] = np.nan
df['GC_raw_e2_e1'] = np.nan
df['GC_per_e2_e1'] = np.nan
df['GC_raw_ratio_ens_x'] = np.nan
df['GC_raw_ratio_x_ens'] = np.nan
df['GC_per_ratio_ens_x'] = np.nan
df['GC_per_ratio_x_ens'] = np.nan
df['GC_raw_ens_red'] = np.nan
df['GC_raw_red_ens'] = np.nan
df['GC_per_ens_red'] = np.nan
df['GC_per_red_ens'] = np.nan
df['GC_raw_ens_ind'] = np.nan
df['GC_raw_ind_ens'] = np.nan
df['GC_per_ens_ind'] = np.nan
df['GC_per_ind_ens'] = np.nan
for aa, animal in enumerate(animals):
folder_path = os.path.join(folder, animal)
filenames = os.listdir(folder_path)
for dd, filename in enumerate(filenames):
day = filename[-17:-11]
dfind = df.index[(df['animal'] == animal) & (df['day'] == int(day))]
to_load_FC = os.path.join(folder_main, 'FC', 'statsmodel', animal, day,
'baseline_red_ens-indirect_dff_order_auto.p')
try:
df_FC = pd.read_pickle(to_load_FC)
except FileNotFoundError:
print('Animal: ' + animal + ' or day: ' + day + ' doesnt exist on the Granger causality analysis')
break
# extract the granger causality values
FC_red = df_FC['FC_red']
FC_pval_red = df_FC['FC_pval_red']
ind_red = df_FC['indices_red']
FC_ens_indirect = df_FC['FC_ens-indirect']
FC_indirect_ens = df_FC['FC_indirect-ens']
FC_pval_ens_indirect = df_FC['FC_pval_ens-indirect']
FC_pval_indirect_ens = df_FC['FC_pval_indirect-ens']
ind_ens_ind = df_FC['indices_ens-indirect']
ind_ind_ens = df_FC['indices_indirect-ens']
ens_neur = mat_ens_ind[aa, dd, ~np.isnan(mat_ens_ind[aa, dd, :])].astype(int)
e2_neur = mat_e2_ind[aa, dd, ~np.isnan(mat_e2_ind[aa, dd, :])].astype(int)
e1_neur = mat_e1_ind[aa, dd, ~np.isnan(mat_e1_ind[aa, dd, :])].astype(int)
ind_mat_red = np.zeros(ens_neur.shape[0], dtype=np.int16)
ind_mat_e2 = np.zeros(e2_neur.shape[0], dtype=np.int16)
ind_mat_e1 = np.zeros(e1_neur.shape[0], dtype=np.int16)
for ee, eneur in enumerate(ens_neur):
ind_mat_red[ee] = np.where(ind_red == eneur)[0][0]
for ee, eneur in enumerate(e2_neur):
ind_mat_e2[ee] = np.where(ind_red == eneur)[0][0]
for ee, eneur in enumerate(e1_neur):
ind_mat_e1[ee] = np.where(ind_red == eneur)[0][0]
FC_ens_red = FC_red[ind_mat_red, :]
FC_ens_red[:, ind_mat_red] = np.nan
FC_pval_ens_red = FC_pval_red[ind_mat_red, :]
FC_pval_ens_red[:, ind_mat_red] = 1
FC_red_ens = FC_red[:, ind_mat_red]
FC_red_ens[ind_mat_red, :] = np.nan
FC_pval_red_ens = FC_pval_red[:, ind_mat_red]
FC_pval_red_ens[ind_mat_red, :] = 1
if (len(ind_mat_e2) == 2) & (len(ind_mat_e1) == 2):
FC_e2_e1 = np.concatenate((FC_red[ind_mat_e2, ind_mat_e1], FC_red[ind_mat_e1, ind_mat_e2]))
FC_pval_e2_e1 = np.concatenate(
(FC_pval_red[ind_mat_e2, ind_mat_e1], FC_pval_red[ind_mat_e1, ind_mat_e2]))
FC_ens_ens = np.concatenate(
([FC_red[ind_mat_e2[0], ind_mat_e2[1]]], [FC_red[ind_mat_e1[0], ind_mat_e1[1]]]))
FC_pval_ens_ens = np.concatenate(
([FC_pval_red[ind_mat_e2[0], ind_mat_e2[1]]], [FC_pval_red[ind_mat_e1[0], ind_mat_e1[1]]]))
calculate_ens = 0
elif (len(ind_mat_e2) > 0) & (len(ind_mat_e1) > 0):
FC_e2_e1 = np.concatenate((FC_red[ind_mat_e2, ind_mat_e1], FC_red[ind_mat_e1, ind_mat_e2]))
FC_pval_e2_e1 = np.concatenate(
(FC_pval_red[ind_mat_e2, ind_mat_e1], FC_pval_red[ind_mat_e1, ind_mat_e2]))
calculate_ens = 1
else:
FC_e2_e1 = np.zeros(1)
FC_pval_e2_e1 = np.ones(1)
calculate_ens = 1
if calculate_ens == 1:
if len(ind_mat_e2) == 2:
FC_ens_ens = FC_red[ind_mat_e2[0], ind_mat_e2[1]]
FC_pval_ens_ens = FC_pval_red[ind_mat_e2[0], ind_mat_e2[1]]
elif len(ind_mat_e1) == 2:
FC_ens_ens = FC_red[ind_mat_e1[0], ind_mat_e1[1]]
FC_pval_ens_ens = FC_pval_red[ind_mat_e1[0], ind_mat_e1[1]]
else:
FC_ens_ens = np.zeros(1)
FC_pval_ens_ens = np.ones(1)
# obtain GC values, raw are values GC, per are number of connections with p<0.05
# obtain ens ens connectivity
raw_ens_ens = np.nanmean(FC_ens_ens[FC_pval_ens_ens < 0.05])
if np.nansum(raw_ens_ens) == 0:
raw_ens_ens = 0
per_ens_ens = np.nansum(FC_pval_ens_ens < 0.05) / np.prod(FC_pval_ens_ens.shape)
raw_e2_e1 = np.nanmean(FC_e2_e1[FC_pval_e2_e1 < 0.05])
if np.nansum(raw_e2_e1) == 0:
raw_e2_e1 = 0
per_e2_e1 = np.nansum(FC_pval_e2_e1 < 0.05) / np.prod(FC_pval_e2_e1.shape)
# obtain red connectivity
raw_ens_red = np.nanmean(FC_ens_red[FC_pval_ens_red < 0.05])
if np.nansum(raw_ens_red) == 0:
raw_ens_red = 0
raw_red_ens = np.nanmean(FC_red_ens[FC_pval_red_ens < 0.05])
if np.nansum(raw_red_ens) == 0:
raw_red_ens = 0
per_ens_red = np.nansum(FC_pval_ens_red < 0.05) / np.prod(FC_pval_ens_red.shape)
per_red_ens = np.nansum(FC_pval_red_ens < 0.05) / np.prod(FC_pval_red_ens.shape)
# obtain green connectivity
raw_ens_ind = np.nanmean(FC_ens_indirect[FC_pval_ens_indirect < 0.05])
if np.nansum(raw_ens_ind) == 0:
raw_ens_ind = 0
raw_ind_ens = np.nanmean(FC_indirect_ens[FC_pval_indirect_ens < 0.05])
if np.nansum(raw_ind_ens) == 0:
raw_ind_ens = 0
per_ens_ind = np.nansum(FC_pval_ens_indirect < 0.05) / np.prod(FC_pval_ens_indirect.shape)
per_ind_ens = np.nansum(FC_pval_indirect_ens < 0.05) / np.prod(FC_pval_indirect_ens.shape)
# df ens-ens
df.loc[dfind, 'GC_raw_ens_ens'] = raw_ens_ens
df.loc[dfind, 'GC_per_ens_ens'] = per_ens_ens
df.loc[dfind, 'GC_raw_e2_e1'] = raw_e2_e1
df.loc[dfind, 'GC_per_e2_e1'] = per_e2_e1
# df red-ind
df.loc[dfind, 'GC_raw_ens_red'] = raw_ens_red
df.loc[dfind, 'GC_raw_red_ens'] = raw_red_ens
df.loc[dfind, 'GC_per_ens_red'] = per_ens_red
df.loc[dfind, 'GC_per_red_ens'] = per_red_ens
df.loc[dfind, 'GC_raw_ens_ind'] = raw_ens_ind
df.loc[dfind, 'GC_raw_ind_ens'] = raw_ind_ens
df.loc[dfind, 'GC_per_ens_ind'] = per_ens_ind
df.loc[dfind, 'GC_per_ind_ens'] = per_ind_ens
# df ratio
if raw_ens_ind > 0:
df.loc[dfind, 'GC_raw_ratio_ens_x'] = raw_ens_red / raw_ens_ind
else:
df.loc[dfind, 'GC_raw_ratio_ens_x'] = np.nan
if raw_ind_ens > 0:
df.loc[dfind, 'GC_raw_ratio_x_ens'] = raw_red_ens / raw_ind_ens
else:
df.loc[dfind, 'GC_raw_ratio_x_ens'] = np.nan
if per_ens_ind > 0:
df.loc[dfind, 'GC_per_ratio_ens_x'] = per_ens_red / per_ens_ind
else:
df.loc[dfind, 'GC_per_ratio_ens_x'] = np.nan
if per_ind_ens > 0:
df.loc[dfind, 'GC_per_ratio_x_ens'] = per_red_ens / per_ind_ens
else:
df.loc[dfind, 'GC_per_ratio_x_ens'] = np.nan
if to_plot:
plot_results(folder_plots, df, single_animal=False, mode='GC')
# save!
df.to_hdf(to_save_df, key='df', mode='w')
def modify_df_speed(df, folder_proc, time_learning=10):
'''
to modify an entry to the pd dataframe based on experimental time of learning in min. time_learning (min)
'''
animals = os.listdir(folder_proc)
for aa, animal in enumerate(animals):
folder_path = os.path.join(folder_proc, animal)
filenames = os.listdir(folder_path)
for dd, filename in enumerate(filenames):
day = filename[-17:-11]
print('processing animal: ' + animal + ' and day: ' + day)
dfind = df.index[(df['animal'] == animal) & (df['day'] == int(day))]
file_template = "full_{}_{}__data.hdf5"
file_name = os.path.join(folder_proc, animal, file_template.format(animal, day))
f = h5py.File(file_name, 'r')
ens_neur = np.asarray(f['ens_neur'])
online_data = np.asarray(f['online_data'])
dff = np.asarray(f['dff'])
cursor = np.asarray(f['cursor'])
startBMI = np.asarray(f['trial_start'])[0]
frame_time = np.int(startBMI / 15 * time_learning)
online_data = online_data[online_data[:, 1] < frame_time, 2:]
f.close()
ens_neur = np.int16(ens_neur[~np.isnan(ens_neur)])
if len(ens_neur) > 0:
dff_ens = dff[ens_neur, :]
# dynamic range
# there are some dff that are crazy weird, to avoid them for deteriorating the dataset any std>1 will be ignored
auxpostwhostd = []
for nn in np.arange(dff_ens.shape[0]):
aux_std = np.nanstd(dff_ens[nn, startBMI:startBMI + frame_time])
if aux_std < 1:
auxpostwhostd.append(aux_std)
df.loc[dfind, 'post_whole_std_mean'] = np.nanmean(auxpostwhostd)
df.loc[dfind, 'post_whole_std_max'] = np.nanmax(auxpostwhostd)
df.loc[dfind, 'post_whole_std_min'] = np.nanmin(auxpostwhostd)
auxonstd = []
for nn in np.arange(online_data.shape[1]):
pseudo_dff = (online_data[:, nn] - np.nanmean(online_data[:, nn])) / np.nanmean(online_data[:, nn])
auxonstd.append(np.nanstd(pseudo_dff))
df.loc[dfind, 'onstd_mean'] = np.nanmean(auxonstd)
df.loc[dfind, 'onstd_max'] = np.nanmax(auxonstd)
df.loc[dfind, 'onstd_min'] = np.nanmin(auxonstd)
df.loc[dfind, 'cursor_std'] = np.nanstd(cursor[:frame_time])
to_save_df = os.path.join(folder_main, 'df_all.hdf5')
df.to_hdf(to_save_df, key='df', mode='w')
return df
def bootstrap_pandas(len_df, X_df, Y_df, bts_n=1000):
''' Bootstrap pd
df = panda dataframe
n = size of bootstrap matrix (how many values)
returns dfbst
'''
if bts_n == 0:
bts_n = len_df
bootst_ind = np.floor(np.random.rand(bts_n) * len_df).astype(int)
X_df_bst = X_df.iloc[bootst_ind]
Y_df_bst = Y_df.iloc[bootst_ind]
# dfbst = df.iloc[bootst_ind]
return bootst_ind, X_df_bst, Y_df_bst
def split_df(df, bts_n=1000, learn_stat_colum='totalPC', size_split_test=0.2, classif=False, synthetic=False):
'''
Function to calculate the xgboost model
'''
# select X and Y
columns = df.columns
if classif:
labels_to_study = columns[10:].tolist()
Y_df = df['ITPTlabel']
elif synthetic:
Y_df = df.loc[:, 'PC_fake']
labels_to_study = columns[1:]
else:
labels_to_study = [columns[3]] + columns[10:].tolist()
Y_df = df.loc[:, learn_stat_colum]
X_df = df.loc[:, labels_to_study]
if np.isinf(np.nansum(Y_df)):
X_df = X_df[~np.isinf(Y_df)]
Y_df = Y_df[~np.isinf(Y_df)]
if np.sum(np.isnan(Y_df)) > 0:
X_df = X_df[~np.isnan(Y_df)]
Y_df = Y_df[~np.isnan(Y_df)]
# split in train/test
X_df_train, X_df_test, Y_df_train, Y_df_test = train_test_split(X_df, Y_df, test_size=size_split_test)
return X_df_train, X_df_test, Y_df_train, Y_df_test
def calculate_model(X_df, Y_df, learning_rate=0.1, xgboost_rep=100):
model = xgboost.train({"learning_rate": learning_rate}, xgboost.DMatrix(X_df, label=Y_df), xgboost_rep)
return model
def calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test):
'''
function to calculate the error of the bootstrap by rule 0.632
'''
# calculate errors
error_samp = mean_squared_error(model.predict(xgboost.DMatrix(X_df_train_bst, label=Y_df_train_bst)),
Y_df_train_bst)
error_test = mean_squared_error(model.predict(xgboost.DMatrix(X_df_test, label=Y_df_test)), Y_df_test)
error_bst = 0.632 * error_test + 0.368 * error_samp
return error_bst
def calculate_bst_length_optimal(df, rep=100, bst_num=None):
'''
function to calculate the best length for the bootstrap depending on the rule 0.632
'''
# some values for HPM
# 0.200 for 40/60 split (minimum error 0.15)
# 0.204 for 20/80 (0.133 for error<0.15)
# 0.188 for 1000 xgboost_rep (0.124 for error<0.15) 20/80
# 0.190 for learning 0.1 (0.123 for error<0.15)
# 0.198 for learning 0.3 (0.126 for error<0.15)
if bst_num == None:
bst_num = np.arange(0, 10000, 1000)
bst_num[0] = 100
error_bst = np.zeros((rep, bst_num.shape[0])) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, bn in enumerate(bst_num):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, bn)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst)
error_bst[rr, ind] = calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
return error_bst
def calculate_size_train_optimal(df, rep=100, size_num=None):
'''
function to calculate the best size for the XGboost depending on the rule 0.632
'''
if size_num == None:
size_num = np.arange(0.1, 0.6, 0.1)
error_bst = np.zeros((rep, size_num.shape[0])) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, sn in enumerate(size_num):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, size_split_test=sn)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst)
error_bst[rr, ind] = calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
return error_bst
def calculate_learning_optimal(df, rep=100, learn_num=None, classif=False):
'''
function to calculate the learning_rate for the XGboost depending on the rule 0.632
'''
if learn_num == None:
learn_num = np.arange(0, 0.4, 0.05)
learn_num[0] = 0.01
error_bst = np.zeros((rep, learn_num.shape[0])) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, ln in enumerate(learn_num):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, classif=classif)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst, learning_rate=ln)
error_bst[rr, ind] = calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
return error_bst
def calculate_xgbrep_optimal(df, rep=100, xgrep_num=None):
'''
function to calculate the best number of trees for the XGboost depending on the rule 0.632
'''
if xgrep_num == None:
xgrep_num = np.asarray([20, 50, 100, 200, 500, 1000, 2000])
error_bst = np.zeros((rep, xgrep_num.shape[0])) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, xn in enumerate(xgrep_num):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst, xgboost_rep=xn)
error_bst[rr, ind] = calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
return error_bst
def calculate_learn_stat_optimal(df, rep=100, bts_n=1000, classif=False):
'''
function to calculate the error for each learning stat for the XGboost depending on the rule 0.632
'''
columns = df.columns.tolist()
columns_ler = [columns[6]] # columns[4:10]
error_bst = np.zeros((rep, len(columns_ler))) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, cn in enumerate(columns_ler):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, learn_stat_colum=cn, classif=classif)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst)
error_bst[rr, ind] = calculate_bst632(model, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
return error_bst
def calculate_learn_stat_mseoptimal(df, rep=100, bts_n=1000, classif=False):
'''
function to calculate the error for each learning stat for the XGboost depending on the rule 0.632
'''
columns = df.columns.tolist()
columns_ler = [columns[6]] # columns[4:10]
error_bst = np.zeros((rep, len(columns_ler))) + np.nan
for rr in np.arange(rep):
print('repetition: ' + str(rr))
for ind, cn in enumerate(columns_ler):
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, learn_stat_colum=cn, classif=classif)
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train, bts_n)
model = calculate_model(X_df_train_bst, Y_df_train_bst)
aux_predict = model.predict(xgboost.DMatrix(X_df_test, label=Y_df_test))
error_bst[rr, ind] = mean_squared_error(Y_df_test, aux_predict)
return error_bst
def calculate_all_errors(df, folder_main, rep=1000):
'''
function to calculate and store all the errors to obtain the optimal parameters for the model
'''
f = h5py.File(os.path.join(folder_main, 'plots', 'utils', 'tpc_error_optimal.h5py'), 'w-')
error_length = calculate_bst_length_optimal(df, rep)
error_size = calculate_size_train_optimal(df, rep)
error_learning = calculate_learning_optimal(df, rep)
error_xgrep = calculate_xgbrep_optimal(df, rep)
error_learn_stat = calculate_learn_stat_optimal(df, rep)
error_learn_statmse = calculate_learn_stat_mseoptimal(df, rep)
f.create_dataset('error_length', data=error_length)
f.create_dataset('error_size', data=error_size)
f.create_dataset('error_learning', data=error_learning)
f.create_dataset('error_xgrep', data=error_xgrep)
f.create_dataset('error_learn_stat', data=error_learn_stat)
f.create_dataset('error_learn_statmse', data=error_learn_statmse)
f.close()
def obtain_shap_iter(df, folder_main, bts_n=1000, mod_n=10000, mod_x=100, error_bstmax=[0.02, 0.2], \
error_msemax=[0.03, 0.3], size_split_test=0.2, max_iter=40, stability_var=0.6, classif=False,
synthetic=False, toplot=True):
'''
obtain shap values of mod_n different XGboost model if the conditions for error of the model and stability are set
obtain stabitlity of feature: correlation of original shap values and bootstrap values to see if values are miningful or noise
'''
columns = df.columns.tolist()
if classif:
# this is to classify IT vs PT
columns_ler = [columns[3]]
labels_to_study = columns[10:] # columns[10:]
elif synthetic:
columns_ler = [columns[0]]
labels_to_study = columns[1:]
else:
# this is to study the learning stats on columns_ler
columns_ler = [columns[6]] # columns[4:10] #[columns[6]]# [columns[3]]
labels_to_study = [columns[3]] + columns[10:] # columns[10:]
test_size = np.ceil(len(df) * (size_split_test)).astype(int)
train_size = np.floor(len(df) * (1 - size_split_test)).astype(int)
all_shap = np.zeros((len(columns_ler), mod_n, test_size, len(labels_to_study))) + np.nan
all_y_pred = np.zeros((len(columns_ler), mod_n, test_size)) + np.nan
all_mse = np.zeros((len(columns_ler), mod_n)) + np.nan
shap_correlations = np.zeros((len(columns_ler), mod_n, mod_x, len(labels_to_study))) + np.nan
explainer_val = np.zeros((len(columns_ler), mod_n)) + np.nan
all_df = np.zeros((len(columns_ler), mod_n, test_size)) + np.nan
number_models = np.zeros(len(columns_ler), dtype=int)
for cc, col_ler in enumerate(columns_ler):
i = 0
iteri = 0
while i < mod_n:
if iteri < max_iter:
all_shap_train_aux = np.zeros((mod_x + 1, train_size, len(labels_to_study))) + np.nan
shap_cor_aux = np.zeros((mod_x, len(labels_to_study))) + np.nan
j = 1
iterj = 0
# make splits for original model
X_df_train, X_df_test, Y_df_train, Y_df_test = split_df(df, bts_n, col_ler,
size_split_test=size_split_test,
classif=classif, synthetic=synthetic)
# calculate original
model_original = calculate_model(X_df_train, Y_df_train)
number_models[cc] += 1
# first check for the model (using bst632)
error_bst = calculate_bst632(model_original, X_df_train, X_df_test, Y_df_train, Y_df_test)
# second check for the model (using mse)
aux_predict = model_original.predict(xgboost.DMatrix(X_df_test, label=Y_df_test))
aux_mse = mean_squared_error(Y_df_test, aux_predict)
# if the model is good enough (mse/bst error low)
if (error_bst < error_bstmax[cc]) & (aux_mse < error_msemax[cc]):
explainer_train = shap.TreeExplainer(model_original,
feature_perturbation='tree_path_dependent') # True to data!, data=X_df_test, feature_perturbation='interventional')
# just in case the size differs we will take only the size of X_df
all_shap_train_aux[0, :len(X_df_train), :] = explainer_train.shap_values(X_df_train)
# bootstrap check for stability of features
while j < (mod_x + 1):
if iterj > max_iter:
print(
'too many iterations, check that the maximum error is not too restrictive or split was just bad')
break
print('repetition: ' + str(i) + ':' + str(j) + ', with iterj: ' + str(iterj))
_, X_df_train_bst, Y_df_train_bst = bootstrap_pandas(len(X_df_train), X_df_train, Y_df_train,
bts_n)
model_bst = calculate_model(X_df_train_bst, Y_df_train_bst)
error_bst = calculate_bst632(model_bst, X_df_train_bst, X_df_test, Y_df_train_bst, Y_df_test)
if error_bst < error_bstmax[cc]:
explainer_bst = shap.TreeExplainer(model_bst,
feature_perturbation='tree_path_dependent') # True to data!, data=X_df_test, feature_perturbation='interventional')
all_shap_train_aux[j, :len(X_df_train), :] = explainer_bst.shap_values(X_df_train)
# check correlation of features with features from original model
for ll, label_ts in enumerate(labels_to_study):
if np.nansum(all_shap_train_aux[j, :len(X_df_train), ll]) > 0:
shap_cor_aux[j - 1, ll] = np.corrcoef(all_shap_train_aux[0, :len(X_df_train), ll], \
all_shap_train_aux[j, :len(X_df_train), ll])[
0, 1]
j += 1
iterj = 0
else:
iterj += 1
aaux = np.nanmean(all_shap_train_aux, 0)
# if the model had a relatively low error and the features were not only noise
if (np.nansum(aaux) != 0) & (np.nanmean(shap_cor_aux) > stability_var):
# first store everything we used to calculate stability
shap_correlations[cc, i, :, :] = shap_cor_aux
# keep info of the xgboost performance
all_y_pred[cc, i, :len(X_df_test)] = aux_predict
all_mse[cc, i] = aux_mse
# now calculate the shap_values with the X_df_test FINALLY!
explainer_test = shap.TreeExplainer(model_original,
feature_perturbation='tree_path_dependent') # True to data!, data=X_df_train, feature_perturbation='interventional')
all_shap[cc, i, :len(X_df_test), :] = explainer_test.shap_values(X_df_test)
explainer_val[cc, i] = explainer_test.expected_value
ind_aux = np.zeros(X_df_test.shape[0], dtype=np.int16)
for xx in np.arange(X_df_test.shape[0]):
ind_aux[xx] = df.index.get_loc(X_df_test.index[xx])
all_df[cc, i, :len(X_df_test)] = ind_aux
# update
i += 1
iteri = 0
print('new model processed: ' + str(i) + '/' + str(mod_n))
else:
print('model not up to specs, repeting model. Iteri: ' + str(iteri))
print(np.nanmean(shap_cor_aux))
iteri += 1
else:
print('too high bst ' + str(~(error_bst < error_bstmax[cc])) + ' or mse error ' + \
str(~(aux_mse < error_msemax[cc])) + ', repeting split. Iteri: ' + str(iteri))
iteri += 1
else:
print('Error too high to continue. Maxiter reached for ' + col_ler)
print(' ')
print('I REPEAT!!!!! Error to high to continue. Maxiter reached')
break
# check mean shap values.
all_shap_reshape = np.reshape(all_shap, [len(columns_ler), np.prod(all_shap.shape[1:3]), all_shap.shape[3]])
all_df_reshape = np.reshape(all_df, [len(columns_ler), np.prod(all_df.shape[1:3])])
shap_experiment_mean = np.zeros((len(columns_ler), len(df), len(labels_to_study))) + np.nan
shap_experiment_std = np.zeros((len(columns_ler), len(df), len(labels_to_study))) + np.nan
shap_experiment_sem = np.zeros((len(columns_ler), len(df), len(labels_to_study))) + np.nan
bins_zscore = np.arange(-2, 2, 0.1)
spread = np.zeros((len(columns_ler), len(df), len(labels_to_study), len(bins_zscore) - 1)) + np.nan
all_ind = np.zeros((len(columns_ler), len(df))) + np.nan
for cc, col_ler in enumerate(columns_ler):
for ind in np.arange(len(df)):
aux_ind = np.where(all_df_reshape[cc, :] == ind)[0]
all_ind[cc, ind] = len(aux_ind)
if all_ind[cc, ind] > 0:
aux_shap = all_shap_reshape[cc, aux_ind, :]
shap_experiment_mean[cc, ind, :] = np.nanmean(aux_shap, 0)
shap_experiment_std[cc, ind, :] = np.nanstd(aux_shap, 0)
shap_experiment_sem[cc, ind, :] = np.nanstd(aux_shap, 0) / np.sqrt(len(aux_ind))
for ll, label_ts in enumerate(labels_to_study):
[h, b] = np.histogram(zscore(aux_shap[:, ll]), bins_zscore)
spread[cc, ind, ll, :] = h
# the spread was gaussian
if classif:
f = h5py.File(os.path.join(folder_main, 'XGShap_classif_model.h5py'), 'w-')
else:
f = h5py.File(os.path.join(folder_main, 'XGShap_model.h5py'), 'w-')
for key in ['labels_to_study', 'test_size', 'train_size', 'all_shap', 'all_y_pred', 'all_mse', 'shap_correlations', \
'explainer_val', 'all_df', 'number_models', 'all_shap_reshape', 'all_df_reshape',
'shap_experiment_mean', \
'shap_experiment_std', 'shap_experiment_sem', 'spread']:
try:
f.create_dataset(key, data=eval(key))
except TypeError:
try:
f.attrs[key] = eval(key)
except:
print('ERROR ' + key)
f.close()
# lets get plotting!!!
if toplot:
sizesubpl = np.ceil(len(labels_to_study) / 6).astype('int')
# check stability of features
folder_plots_sta = os.path.join(folder_main, 'plots', 'XGBoost', 'feature_stability')
print('plotting feature stability')
if not os.path.exists(folder_plots_sta):
os.makedirs(folder_plots_sta)
bins_cor = np.arange(stability_var - 0.2, 1, 0.01)
for cc, col_ler in enumerate(columns_ler):
fig1 = plt.figure(figsize=(17, 9))
for ll, label_ts in enumerate(labels_to_study):
ax0 = fig1.add_subplot(sizesubpl, 6, ll + 1)
if np.nansum(shap_correlations[cc, :, :, ll]) > 0:
[h, b] = np.histogram(shap_correlations[cc, :, :, ll], bins_cor)
ax0.bar(b[1:], h, width=0.01)
ax0.set_xlabel(label_ts)
fig1.tight_layout()
fig1.savefig(os.path.join(folder_plots_sta, col_ler + '_stability_features.png'), bbox_inches="tight")
fig1.savefig(os.path.join(folder_plots_sta, col_ler + '_stability_features.eps'), bbox_inches="tight")
plt.close('all')
# check IT/PT shap values
folder_plots_ITPT = os.path.join(folder_main, 'plots', 'XGBoost', 'ITPT')
print('ITPT stuff')
if not os.path.exists(folder_plots_ITPT):
os.makedirs(folder_plots_ITPT)
all_IT = np.zeros(len(columns_ler)) + np.nan
all_PT = np.zeros(len(columns_ler)) + np.nan
bins_shap = np.arange(-0.002, 0.002, 0.00005)
for cc, col_ler in enumerate(columns_ler):
aux_IT = shap_experiment_mean[cc, df['ITPTlabel'] == 0, 0]
aux_PT = shap_experiment_mean[cc, df['ITPTlabel'] == 1, 0]
all_IT[cc] = np.nanmean(aux_IT)
all_PT[cc] = np.nanmean(aux_PT)
fig2 = plt.figure(figsize=(12, 4))
ax1 = fig2.add_subplot(1, 2, 1)
[h_IT, b] = np.histogram(aux_IT, bins_shap)
[h_PT, b] = np.histogram(aux_PT, bins_shap)
ax1.bar(b[1:], h_IT, width=0.00005, label='IT')
ax1.bar(b[1:], h_PT, width=0.00005, label='PT')
ax1.legend()
ax2 = fig2.add_subplot(1, 2, 2)
ax2.bar([0.4, 1.4], [all_IT[cc], all_PT[cc]], width=0.8, \
yerr=[pd.DataFrame(aux_IT).sem(0).values[0], pd.DataFrame(aux_PT).sem(0).values[0]], \
error_kw=dict(ecolor='k'))
ax2.set_xticks([0.4, 1.4])