-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathana_sMRI_change_AMsub2cortex.py
1330 lines (1049 loc) · 57.1 KB
/
ana_sMRI_change_AMsub2cortex.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 necessary libraries for data manipulation, neuroimaging data processing, and machine learning
import os
import numpy as np
import nibabel as nib
import pandas as pd
import joblib
from nilearn import datasets as ds
import matplotlib.pylab as plt
from sklearn.preprocessing import StandardScaler
from nilearn.signal import clean
from nilearn import datasets as ds
from sklearn.cross_decomposition import PLSCanonical
import seaborn as sns
from scipy.stats import scoreatpercentile
from scipy.stats import pearsonr
DECONF = True # Flag for deconfounding, not used in the snippet provided
TAR_ANA = 'AM' # Target analysis set to Amygdala
# Load UKBiobank data
ukbb = pd.read_csv('/Users/dblab/Desktop/Project Amygdala/Amygdala/ukb40500_cut_merged.csv/ukb40500_cut_merged.csv', low_memory=True)
# Load descriptions dictionary for region volumes
descr_dict = joblib.load('/Users/dblab/Desktop/Project Amygdala/Amygdala/descr_dict')
# Process and filter columns for specific regions based on the FSL atlas, excluding Diederichsen cerebellar atlas
ukbb_HO20 = ukbb.loc[:, '25782-2.0':'25892-2.0'] # Timepoint 2 data
ukbb_HO20 = ukbb_HO20.iloc[:, ~ukbb_HO20.columns.str.contains('-3.0')]
ukbb_HO30 = ukbb.loc[:, '25782-3.0':'25892-3.0'] # Timepoint 3 data
ukbb_HO30 = ukbb_HO30.iloc[:, ~ukbb_HO30.columns.str.contains('-2.0')]
# Rename columns using descriptions for easier understanding
HO_vol_names = np.array([descr_dict[c]['descr'].split('Volume of grey matter in ')[1] for c in ukbb_HO20.columns])
ukbb_HO20.columns = HO_vol_names
ukbb_HO30.columns = HO_vol_names
# Load demographic and clinical information
eid = ukbb['eid']
age_T2 = ukbb.loc[:, '21003-2.0'] # Age at Timepoint 2
age_T3 = ukbb.loc[:, '21003-3.0'] # Age at Timepoint 3
sex = ukbb.loc[:, '31-0.0'] # Sex
# Function to impute missing values non-parametrically
def NonparametricImpute(input_vars):
nan_inds = np.where(np.isnan(input_vars))[0]
pres_inds = np.where(~np.isnan(input_vars))[0]
rs = np.random.RandomState(0)
rs.shuffle(pres_inds)
input_vars[nan_inds] = input_vars[pres_inds[:len(nan_inds)]]
return input_vars
# Load amygdala subregion measurements for first timepoint
COLS_NAMES = []
COLS_IDS = []
for fname in ['/Users/dblab/Desktop/Project Amygdala/Amygdala/subcortical_labels_%s.txt' % TAR_ANA]:
with open(fname) as f:
lines=f.readlines()
for line in lines:
a, b = line.split('\t')[0], line.strip().split('\t')[1]
COLS_IDS.append(a + '-2.0')
COLS_NAMES.append(b)
COLS_NAMES = np.array(COLS_NAMES)
COLS_IDS = np.array(COLS_IDS)
dfS20 = ukbb.loc[:, COLS_IDS]
dfS20.columns = np.array([str(c.encode("ascii")) for c in COLS_NAMES])
dfS20 = ukbb.loc[:, COLS_IDS]
dfS20.columns = np.array([str(c.encode("ascii")) for c in COLS_NAMES])
# Load amygdala subregion measurements for second timepoint
TAR_ANA = 'AM'
COLS_NAMES = []
COLS_IDS = []
for fname in ['/Users/dblab/Desktop/Project Amygdala/Amygdala/subcortical_labels_%s.txt' % TAR_ANA]:
with open(fname) as f:
lines=f.readlines()
f.close()
for line in lines:
a = line[:line.find('\t')]
b = line[line.find('\t') + 1:].rsplit('\n')[0]
COLS_IDS.append(a + '-3.0')
COLS_NAMES.append(b)
COLS_NAMES = np.array(COLS_NAMES)
COLS_IDS = np.array(COLS_IDS)
sub_dict = {COLS_IDS[i_col] : COLS_NAMES[i_col] for i_col in range(len(COLS_IDS))}
dfS30 = ukbb.loc[:, COLS_IDS]
dfS30.columns = np.array([str(c.encode("ascii")) for c in COLS_NAMES])
# remove columns with excessive missingness in 2nd time point
subs_keep = dfS30.isna().sum(1) == 0
dfS20 = dfS20.loc[subs_keep]
dfS30 = dfS30.loc[subs_keep]
ukbb_HO20 = ukbb_HO20.loc[subs_keep]
ukbb_HO30 = ukbb_HO30.loc[subs_keep]
eid = eid.loc[subs_keep]
age_T2 = age_T2[subs_keep]
age_T3 = age_T3[subs_keep]
sex = sex[subs_keep]
ukbb_2tp = ukbb[subs_keep]
# Standardize (z-score) the datasets for both time points
S_scaler = StandardScaler()
FS_AM20 = dfS20.values
FS_AM20_ss = S_scaler.fit_transform(FS_AM20)
FS_AM30 = dfS30.values
FS_AM30_ss = S_scaler.transform(FS_AM30)
HO_scaler = StandardScaler()
FS_HO20 = ukbb_HO20.values
FS_HO20_ss = HO_scaler.fit_transform(FS_HO20)
FS_HO30 = ukbb_HO30.values
FS_HO30_ss = HO_scaler.transform(FS_HO30)
# remove the amygdala from HO atlas space
idx_nonAM = ~ukbb_HO20.columns.str.contains('Amygdala')
FS_HO20 = FS_HO20[:, idx_nonAM]
FS_HO20_ss = FS_HO20_ss[:, idx_nonAM]
FS_HO30 = FS_HO30[:, idx_nonAM]
FS_HO30_ss = FS_HO30_ss[:, idx_nonAM]
ukbb_HO20 = ukbb_HO20.loc[:, idx_nonAM]
ukbb_HO30 = ukbb_HO30.loc[:, idx_nonAM]
# Impute missing values using a Nonparametric method (not provided here, ensure implementation is available)
FS_AM20_ss = NonparametricImpute(FS_AM20_ss)
FS_AM30_ss = NonparametricImpute(FS_AM30_ss)
FS_HO20_ss = NonparametricImpute(FS_HO20_ss)
FS_HO30_ss = NonparametricImpute(FS_HO30_ss)
# Ensure there are no missing values after imputation
assert np.all(~np.isnan(FS_AM20_ss))
assert np.all(~np.isnan(FS_AM30_ss))
assert np.all(~np.isnan(FS_HO20_ss))
assert np.all(~np.isnan(FS_HO30_ss))
# Remove columns related to whole-HC measures
keep_col_inds = ~dfS30.columns.str.contains('Whole') # remove 6 whle-HC measures
dfS20 = dfS20.loc[:, keep_col_inds]
dfS30 = dfS30.loc[:, keep_col_inds]
FS_AM20 = FS_AM20[:, keep_col_inds]
FS_AM20_ss = FS_AM20_ss[:, keep_col_inds]
FS_AM30 = FS_AM30[:, keep_col_inds]
FS_AM30_ss = FS_AM30_ss[:, keep_col_inds]
COLS_NAMES = COLS_NAMES[keep_col_inds]
# Deconfound brain structural measures if required
if DECONF:
# Additional behavior data for deconfounding
beh = ukbb_2tp
age = StandardScaler().fit_transform(beh['21022-0.0'].values[:, np.newaxis]) # Age at recruitment
age2 = age ** 2
sex = np.array(pd.get_dummies(beh['31-0.0']).values, dtype=np.int) # Sex
sex_x_age = sex * age
sex_x_age2 = sex * age2
head_motion_rest = np.nan_to_num(beh['25741-2.0'].values) # Mean rfMRI head motion
head_motion_task = np.nan_to_num(beh['25742-2.0'].values) # Mean tfMRI head motion
# added during previous paper revisions
head_size = np.nan_to_num(beh['25006-2.0'].values) # Volume of gray matter
body_mass = np.nan_to_num(beh['21001-0.0'].values) # BMI
# motivated by Elliott et al., 2018
# exact location of the head and the radio-frequency receiver coil in the scanner
head_pos_x = np.nan_to_num(beh['25756-2.0'].values)
head_pos_y = np.nan_to_num(beh['25757-2.0'].values)
head_pos_z = np.nan_to_num(beh['25758-2.0'].values)
head_pos_table = np.nan_to_num(beh['25759-2.0'].values)
scan_site_dummies = pd.get_dummies(beh['54-2.0']).values
#Ensure no Nans
assert np.any(np.isnan(head_motion_rest)) == False
assert np.any(np.isnan(head_motion_task)) == False
assert np.any(np.isnan(head_size)) == False
assert np.any(np.isnan(body_mass)) == False
print('Deconfounding brain structural measures space!')
#construct confounding matrix
conf_mat = np.hstack([
# age, age2, sex, sex_x_age, sex_x_age2,
np.atleast_2d(head_motion_rest).T, np.atleast_2d(head_motion_task).T,
np.atleast_2d(head_size).T, np.atleast_2d(body_mass).T,
np.atleast_2d(head_pos_x).T, np.atleast_2d(head_pos_y).T,
np.atleast_2d(head_pos_z).T, np.atleast_2d(head_pos_table).T,
np.atleast_2d(scan_site_dummies)
])
# Deconfounding using nilearn's clean function
FS_AM20_ss = clean(FS_AM20_ss, confounds=conf_mat,
detrend=False, standardize=False)
FS_AM30_ss = clean(FS_AM30_ss, confounds=conf_mat,
detrend=False, standardize=False)
FS_HO20_ss = clean(FS_HO20_ss, confounds=conf_mat,
detrend=False, standardize=False)
FS_HO30_ss = clean(FS_HO30_ss, confounds=conf_mat,
detrend=False, standardize=False)
#Get the change in gray matter volume in the amygdala
dfAM20minus30 = pd.DataFrame(
FS_AM20_ss - FS_AM30_ss, columns=dfS20.columns)
#Get the change in gray matter volume in the (sub)cortex
dfHO20minus30 = pd.DataFrame(
FS_HO20_ss - FS_HO30_ss, columns=ukbb_HO20.columns)
#Difference in volume between second and third timepoints of the left subregions of the amygdala
dfAM20minus30_L = pd.DataFrame(dfAM20minus30.filter(regex='left').values, columns=dfAM20minus30.loc[:,['left' in i for i in dfAM20minus30.columns]].columns)
#Difference in volume between second and third timepoints of the right subregions of the amygdala
dfAM20minus30_R = pd.DataFrame(dfAM20minus30.filter(regex='right').values, columns=dfAM20minus30.loc[:,['right' in i for i in dfAM20minus30.columns]].columns)
#Difference between second and third timepoints
n_comps = 8
#Analysis of difference in brain volumes between the two time regions
#PLS Canonical
pls = PLSCanonical(n_components=n_comps)
pls.fit(dfAM20minus30, dfHO20minus30)
r2 = pls.score(dfAM20minus30, dfHO20minus30) # coefficient of determination :math:`R^2`
est = pls
actual_Rs = np.array([pearsonr(X_coef, Y_coef)[0] for X_coef, Y_coef in
zip(est.x_scores_.T, est.y_scores_.T)])
print(actual_Rs)
# [0.19654273, 0.24062467, 0.21549389, 0.19554875, 0.21232852, 0.25243018, 0.21673239, 0.22932024]
#Analysis of the differences of the left subregions of the amygdala and the cortex between second and third time points
#PLS Canonical
pls_left= PLSCanonical(n_components=n_comps)
pls_left.fit(dfAM20minus30_L, dfHO20minus30)
r2 = pls_left.score(dfAM20minus30_L, dfHO20minus30) # coefficient of determination :math:`R^2`
est = pls_left
actual_Rs = np.array([pearsonr(X_coef, Y_coef)[0] for X_coef, Y_coef in
zip(est.x_scores_.T, est.y_scores_.T)])
print(actual_Rs)
# [0.17969291 0.22206758 0.24594784 0.19278412 0.20976133]
#Analysis of the differences of the right subregions of the amygdala and the cortex between second and third time points
#PLS Canonical
pls_right = PLSCanonical(n_components=n_comps)
pls_right.fit(dfAM20minus30_R, dfHO20minus30)
r2 = pls_right.score(dfAM20minus30_R, dfHO20minus30) # coefficient of determination :math:`R^2`
est = pls_right
actual_Rs = np.array([pearsonr(X_coef, Y_coef)[0] for X_coef, Y_coef in
zip(est.x_scores_.T, est.y_scores_.T)])
print(actual_Rs)
# [0.15075179 0.21492126 0.21132531 0.22200016 0.20072646]
#Fetch the Harvard Oxford Atlases
HO_atlas_cort = ds.fetch_atlas_harvard_oxford('cort-maxprob-thr50-1mm', symmetric_split=True)
HO_atlas_sub = ds.fetch_atlas_harvard_oxford('sub-maxprob-thr50-1mm', symmetric_split=True)
OUT_DIR = 'AMsub2cortex/dfAM20minus30/pls'
#Difference between second and third timepoints
# Loop over each component in the PLS (Partial Least Squares) analysis or similar
for i_mode in range(n_comps):
# Initialize an array to store the Spatial Effect Size (SES) data in brain space,
# matching the shape of the cortical atlas maps
SES_in_brain_data = np.zeros((HO_atlas_cort.maps.shape))
dfX = dfHO20minus30 # DataFrame containing the brain data differences between timepoints
# Loop over each feature (brain region) in the DataFrame
for i_feat in range(dfX.shape[-1]):
cur_feat_name = dfX.columns[i_feat].split(' (')[0] # Extract the brain region name
# Special case handling for brain stem or renaming conventions
if 'Stem' in cur_feat_name:
pass # Skip the brain stem or handle it separately
else:
# Prefix region names with hemisphere based on the column name
cur_feat_name = ('Right ' if 'right' in dfX.columns[i_feat] else 'Left ') + cur_feat_name
# Adjust naming conventions for compatibility with atlas labels
if 'Ventral Striatum' in cur_feat_name:
cur_feat_name = cur_feat_name.replace('Ventral Striatum', 'Accumbens')
b_found_roi = False # Flag to check if the region of interest (ROI) was found in the atlas
# Loop over cortical labels in the atlas to find a match with the current feature
for i_cort_label, cort_label in enumerate(HO_atlas_cort.labels):
if cur_feat_name in cort_label:
# If matched, find all voxels corresponding to this cortical label
b_roi_mask = HO_atlas_cort.maps.get_data() == i_cort_label
n_roi_vox = np.sum(b_roi_mask) # Count the voxels in the ROI
print(f'Found: {cort_label} ({n_roi_vox} voxels)')
# Assign the PLS loading for this feature to all voxels in the ROI
SES_in_brain_data[b_roi_mask] = pls.y_loadings_[i_feat, i_mode]
b_found_roi = True
# Repeat the process for subcortical labels if the ROI wasn't found in cortical labels
for i_cort_label, cort_label in enumerate(HO_atlas_sub.labels):
if cur_feat_name in cort_label:
b_roi_mask = HO_atlas_sub.maps.get_data() == i_cort_label
n_roi_vox = np.sum(b_roi_mask)
print(f'Found: {cort_label} ({n_roi_vox} voxels)')
SES_in_brain_data[b_roi_mask] = pls.y_loadings_[i_feat, i_mode]
b_found_roi = True
# If the ROI was not found in either cortical or subcortical labels, print a message
if not b_found_roi:
print(f'NOT Found: {cur_feat_name} !!!')
# After processing all features for the current component, save the SES data as a NIfTI image
SES_name = f'mode{i_mode + 1}'
SES_in_brain_nii = nib.Nifti1Image(SES_in_brain_data, HO_atlas_cort.maps.affine)
# Define the output directory and file name, and save the NIfTI image
SES_in_brain_nii.to_filename(OUT_DIR + '/' + SES_name + '_coef.nii.gz')
#Left and Right subregions of the amygdala seperately and cortex regions difference between second and third time points
OUT_DIR_4 = 'AMsub2cortex/dfAM20minus30_L/pls'
OUT_DIR_6 = 'AMsub2cortex/dfAM20minus30_R/pls'
# Loop over each component in the PLS analysis or similar
for i_mode in range(n_comps):
# Initialize two arrays to store the Spatial Effect Size (SES) data in brain space for left and right hemispheres
SES_in_brain_data_L = np.zeros((HO_atlas_cort.maps.shape))
SES_in_brain_data_R = np.zeros((HO_atlas_cort.maps.shape))
dfX = dfHO20minus30 # DataFrame with the differences in brain data between two time points
# Loop over each feature (brain region) in the DataFrame
for i_feat in range(dfX.shape[-1]):
cur_feat_name = dfX.columns[i_feat].split(' (')[0] # Extract the brain region name
# Skip processing for the brain stem or apply specific handling
if 'Stem' in cur_feat_name:
pass
else:
# Adjust the feature name to include hemisphere information based on the column name
cur_feat_name = ('Right ' if 'right' in dfX.columns[i_feat] else 'Left ') + cur_feat_name
# Adjust naming conventions for compatibility with atlas labels (specific case handling)
if 'Ventral Striatum' in cur_feat_name:
cur_feat_name = cur_feat_name.replace('Ventral Striatum', 'Accumbens')
b_found_roi = False # Flag to check if the region of interest (ROI) was found in the atlas
# Loop over cortical labels in the atlas to find a match with the current feature
for i_cort_label, cort_label in enumerate(HO_atlas_cort.labels):
if cur_feat_name in cort_label:
# If matched, find all voxels corresponding to this cortical label
b_roi_mask = HO_atlas_cort.maps.get_data() == i_cort_label
n_roi_vox = np.sum(b_roi_mask) # Count the voxels in the ROI
print(f'Found: {cort_label} ({n_roi_vox} voxels)')
# Assign the PLS loading for this feature to all voxels in the ROI for left and right SES data
SES_in_brain_data_L[b_roi_mask] = pls_left.y_loadings_[i_feat, i_mode]
SES_in_brain_data_R[b_roi_mask] = pls_right.y_loadings_[i_feat, i_mode]
b_found_roi = True
# Repeat the process for subcortical labels if the ROI wasn't found in cortical labels
for i_cort_label, cort_label in enumerate(HO_atlas_sub.labels):
if cur_feat_name in cort_label:
b_roi_mask = HO_atlas_sub.maps.get_data() == i_cort_label
n_roi_vox = np.sum(b_roi_mask)
print(f'Found: {cort_label} ({n_roi_vox} voxels)')
SES_in_brain_data_L[b_roi_mask] = pls_left.y_loadings_[i_feat, i_mode]
SES_in_brain_data_R[b_roi_mask] = pls_right.y_loadings_[i_feat, i_mode]
b_found_roi = True
# If the ROI was not found in either cortical or subcortical labels, print a message
if not b_found_roi:
print(f'NOT Found: {cur_feat_name} !!!')
# After processing all features for the current component, save the SES data as NIfTI images for both hemispheres
SES_name = f'mode{i_mode + 1}'
# Save left hemisphere SES data as a NIfTI file
SES_in_brain_nii_L = nib.Nifti1Image(SES_in_brain_data_L, HO_atlas_cort.maps.affine)
SES_in_brain_nii_L.to_filename(OUT_DIR_4 + '/' + SES_name + '_left_amygdala_coef.nii.gz')
# Save right hemisphere SES data as a NIfTI file
SES_in_brain_nii_R = nib.Nifti1Image(SES_in_brain_data_R, HO_atlas_cort.maps.affine)
SES_in_brain_nii_R.to_filename(OUT_DIR_6 + '/' + SES_name + '_right_amygdala_coef.nii.gz')
#Difference between second and third timepoints PLS
# Define a suffix for file naming, useful for specifying conditions or parameters
SUFFIX = ''
# Iterate over each component in the PLS analysis
for counter, i_comp in enumerate(range(n_comps)):
n_rois = pls.x_loadings_.shape[0] # Number of ROIs/features in the PLS model
X_AM_weights = pls.x_loadings_[:, i_comp] # Extract weights (loadings) for the current component
# Create a figure for the heatmap
f = plt.figure(figsize=(9, 6), dpi=600) # Define figure size and resolution
# Initialize an array to store the component weights for visualization
X_comp_weights = np.zeros((n_rois, 1))
X_comp_weights[:, 0] = X_AM_weights # Assign the weights to the array
# Create a DataFrame for the weights, indexing by modified column names from dfAM20minus30
dfdata = pd.DataFrame(X_AM_weights, index=(dfAM20minus30.columns.str.replace('b', '')).str.replace("'",''), columns=[''])
# Save the weights to CSV and Excel files for external analysis and sharing
dfdata.to_csv('%s/pls_AM_topcomp%i%s_style_.csv' % (OUT_DIR, counter + 1, SUFFIX))
dfdata.to_excel('%s/pls_AM_topcomp%i%s_style_.xls' % (OUT_DIR, counter + 1, SUFFIX))
# Generate a heatmap of the component weights using seaborn
ax = sns.heatmap(dfdata, cbar=True, linewidths=.75,
cbar_kws={'shrink': 0.5}, # Customization for the colorbar
square=True,
cmap=plt.cm.RdBu_r, center=0) # Use diverging color map centered at 0
# Set font sizes for the heatmap labels
ax.set_xticklabels(ax.get_xticklabels(), fontsize=10)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=10)
# Workaround for a matplotlib bug that cuts off the top and bottom of the heatmap
b, t = plt.ylim() # Get the current bottom and top limits
b += 0.5 # Adjust the bottom limit
t -= 0.5 # Adjust the top limit
plt.ylim(b, t) # Apply the new limits
plt.tight_layout() # Adjust subplots to fit into the figure area
# Save the heatmap to PNG and PDF files for high-quality visualizations
plt.savefig('%s/pls_AM_topcomp%i%s_style_.png' % (OUT_DIR, counter + 1, SUFFIX), DPI=200)
plt.savefig('%s/pls_AM_topcomp%i%s_style_.pdf' % (OUT_DIR, counter + 1, SUFFIX))
#Left Amygdala with cortex PLS
# Iterate over each component from the PLS analysis specific to the left amygdala
for counter, i_comp in enumerate(range(n_comps)):
n_rois = pls_left.x_loadings_.shape[0] # Number of regions of interest (ROIs) or features
X_AM_weights = pls_left.x_loadings_[:, i_comp] # Extract weights for the current component
# Create a figure for the heatmap visualization
f = plt.figure(figsize=(10, 7))
# Initialize an array to store the component weights for visualization
X_comp_weights = np.zeros((n_rois, 1))
X_comp_weights[:, 0] = X_AM_weights
# Prepare DataFrame of the weights, with modified column names for readability
dfdata = pd.DataFrame(X_AM_weights, index=(dfAM20minus30_L.columns.str.replace('b', '')).str.replace("'",''), columns=[''])
# Save the component weights to CSV and Excel files
dfdata.to_csv('%s/left_AM_topcomp%i%s_style_.csv' % (OUT_DIR_4, counter + 1, SUFFIX))
dfdata.to_excel('%s/left_AM_topcomp%i%s_style_.xls' % (OUT_DIR_4, counter + 1, SUFFIX))
# Generate and customize the heatmap using seaborn
ax = sns.heatmap(dfdata, cbar=True, linewidths=.75, cbar_kws={'shrink': 0.5}, square=True, cmap=plt.cm.RdBu_r, center=0)
ax.set_xticklabels(ax.get_xticklabels(), fontsize=9)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=9)
# Adjust the heatmap display to ensure no cutoff at top/bottom
b, t = plt.ylim()
plt.ylim(b + 0.5, t - 0.5)
plt.tight_layout()
# Save the heatmap as high-resolution PNG and PDF
plt.savefig('%s/left_AM_topcomp%i%s_style_.png' % (OUT_DIR_4, counter + 1, SUFFIX), DPI=600)
plt.savefig('%s/left_AM_topcomp%i%s_style_.pdf' % (OUT_DIR_4, counter + 1, SUFFIX))
#Right Amygdala with cortex PLS
# Similar loop for the right amygdala, generating heatmaps for each PLS componen
for counter, i_comp in enumerate(range(n_comps)):
# The process repeats as above, with specifics adjusted for the right amygdala
# This includes loading the PLS model for the right amygdala, preparing the data,
# generating heatmaps, and saving the results to files designated for right amygdala analysis
n_rois = pls_right.x_loadings_.shape[0]
X_AM_weights = pls_right.x_loadings_[:, i_comp]
f = plt.figure(figsize=(10, 7))
X_comp_weights = np.zeros((n_rois, 1))
X_comp_weights[:, 0] = X_AM_weights
dfdata = pd.DataFrame(X_AM_weights, index=(dfAM20minus30_R.columns.str.replace('b', '')).str.replace("'",''), columns=[''])
dfdata.to_csv('%s/right_AM_topcomp%i%s_style_.csv' % (OUT_DIR_6, counter + 1, SUFFIX))
dfdata.to_excel('%s/right_AM_topcomp%i%s_style_.xls' % (OUT_DIR_6, counter + 1, SUFFIX))
ax = sns.heatmap(dfdata, cbar=True, linewidths=.75,
cbar_kws={'shrink': 0.5}, #'orientation': 'horizontal'}, #, 'label': 'Functional coupling deviation'},
square=True,
cmap=plt.cm.RdBu_r, center=0)
ax.set_xticklabels(ax.get_xticklabels(), fontsize=9)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=9)
# fix for mpl bug that cuts off top/bottom of seaborn viz
b, t = plt.ylim() # discover the values for bottom and top
b += 0.5 # Add 0.5 to the bottom
t -= 0.5 # Subtract 0.5 from the top
plt.ylim(b, t) # update the ylim(bottom, top) values
plt.tight_layout()
plt.savefig('%s/right_AM_topcomp%i%s_style_.png' % (OUT_DIR_6, counter + 1, SUFFIX), DPI=600)
plt.savefig('%s/right_AM_topcomp%i%s_style_.pdf' % (OUT_DIR_6, counter + 1, SUFFIX))
#########################################################################
#Hemispheric Difference Analysis
# Number of bootstrap permutations and components to keep
n_BS_perm = 100
n_keep = 8
# Initialize lists to store bootstrap results
BS_diff = []
list_l_x, list_l_y, list_r_x, list_r_y = [], [], [], []
# Bootstrap permutation loop
for i_BS in range(n_BS_perm):
print(i_BS)
# Generate a random sample of indices for bootstrap
bs_rs = np.random.RandomState(i_BS)
bs_sample_inds = bs_rs.randint(0, len(dfAM20minus30_L), len(dfAM20minus30_L))
# Create bootstrap samples for left and right amygdala and cortex data
bs_X_train_l = dfAM20minus30_L.iloc[bs_sample_inds, :]
bs_X_train_r = dfAM20minus30_R.iloc[bs_sample_inds, :]
bs_Y_train = dfHO20minus30.iloc[bs_sample_inds, :]
# Fit PLS models for left and right amygdala with bootstrap samples
est_l = PLSCanonical(n_components=n_keep, scale=False)
est_l.fit(bs_X_train_l, bs_Y_train)
list_l_x.append(est_l.x_loadings_)
list_l_y.append(est_l.y_loadings_)
est_r = PLSCanonical(n_components=n_keep, scale=False)
est_r.fit(bs_X_train_r, bs_Y_train)
list_r_x.append(est_r.x_loadings_)
list_r_y.append(est_r.y_loadings_)
# Save bootstrap results for further analysis
today_stamp = '201010' # Current date stamp for file naming
np.save('BS_dump_list_full_l_x' + today_stamp, np.array(list_l_x))
np.save('BS_dump_list_full_l_y' + today_stamp, np.array(list_l_y))
np.save('BS_dump_list_full_r_x' + today_stamp, np.array(list_r_x))
np.save('BS_dump_list_full_r_y' + today_stamp, np.array(list_r_y))
# AM side
# Calculate the differences between left and right amygdala component loadings
it_diffs_x = np.zeros((n_BS_perm, n_keep, pls_left.x_loadings_.shape[0]))
# Loop through each permutation and component
for i_bs in range(n_BS_perm):
for i_org_comp in range(n_keep):
# Calculate correlations between original and bootstrap loadings for left and right
l_rhos_x, r_rhos_x = np.zeros((n_keep)), np.zeros((n_keep))
for i_bs_comp in range(n_keep):
l_rhos_x[i_bs_comp], _ = pearsonr(pls_left.x_loadings_[:, i_org_comp], list_l_x[i_bs][:, i_bs_comp])
r_rhos_x[i_bs_comp], _ = pearsonr(pls_right.x_loadings_[:, i_org_comp], list_r_x[i_bs][:, i_bs_comp])
# Identify and adjust the most correlated component
good_comp_l_x_ind = np.argmax(np.abs(l_rhos_x))
good_comp_l_x = list_l_x[i_bs, :, good_comp_l_x_ind] * np.sign(l_rhos_x[good_comp_l_x_ind])
good_comp_r_x_ind = np.argmax(np.abs(r_rhos_x))
good_comp_r_x = list_r_x[i_bs, :, good_comp_r_x_ind] * np.sign(r_rhos_x[good_comp_r_x_ind])
# Store the difference between adjusted components
it_diffs_x[i_bs, i_org_comp] = good_comp_l_x - good_comp_r_x
# Determine significant differences based on a threshold
THRESH = 10 # Percentile threshold for significance
rel_mask_x = np.zeros((n_keep, pls_left.x_loadings_.shape[0]))
for i_comp in range(n_keep):
lower_th = scoreatpercentile(it_diffs_x[:, i_comp, :], THRESH, axis=0)
upper_th = scoreatpercentile(it_diffs_x[:, i_comp, :], 100 - THRESH, axis=0)
rel_mask_x[i_comp] = ((lower_th < 0) & (upper_th < 0)) | ((lower_th > 0) & (upper_th > 0))
# Print components with significant differences
if np.sum(rel_mask_x[i_comp]) > 0:
print('Amygdala component %i: %i hits' % (i_comp + 1, np.sum(rel_mask_x[i_comp])))
print(list(dfAM20minus30_L.columns[np.array(rel_mask_x[i_comp], dtype=bool)].str.replace('(left hemisphere)', '')))
np.save('rel_mask_x_full_THRESH%i' % THRESH, rel_mask_x)
# HO side
# Initialize an array to store the differences in component loadings between hemispheres for cortex data
it_diffs_y = np.zeros((n_BS_perm, n_keep, pls_left.y_loadings_.shape[0]))
# Iterate through bootstrap permutations
for i_bs in range(n_BS_perm):
for i_org_comp in range(n_keep):
# Calculate correlation coefficients between the original and bootstrap samples for left and right hemispheres
l_rhos_y = np.zeros(n_keep)
r_rhos_y = np.zeros(n_keep)
for i_bs_comp in range(n_keep):
l_rhos_y[i_bs_comp], _ = pearsonr(pls_left.y_loadings_[:, i_org_comp], list_l_y[i_bs][:, i_bs_comp])
r_rhos_y[i_bs_comp], _ = pearsonr(pls_right.y_loadings_[:, i_org_comp], list_r_y[i_bs][:, i_bs_comp])
# Identify the component from the bootstrap samples that best matches the original component
good_comp_l_y_ind = np.argmax(np.abs(l_rhos_y))
good_comp_l_y = list_l_y[i_bs, :, good_comp_l_y_ind] * np.sign(l_rhos_y[good_comp_l_y_ind])
good_comp_r_y_ind = np.argmax(np.abs(r_rhos_y))
good_comp_r_y = list_r_y[i_bs, :, good_comp_r_y_ind] * np.sign(r_rhos_y[good_comp_r_y_ind])
# Calculate the difference between the matched components for left and right hemispheres
it_diffs_y[i_bs, i_org_comp] = good_comp_l_y - good_comp_r_y
# Assess significance of the differences using a threshold
rel_mask_y = np.zeros((n_keep, pls_left.y_loadings_.shape[0]))
THRESH = 10
for i_comp in range(n_keep):
lower_th = scoreatpercentile(it_diffs_y[:, i_comp, :], THRESH, axis=0)
upper_th = scoreatpercentile(it_diffs_y[:, i_comp, :], 100 - THRESH, axis=0)
rel_mask_y[i_comp] = ((lower_th < 0) & (upper_th < 0)) | ((lower_th > 0) & (upper_th > 0))
n_hits = np.sum(rel_mask_y[i_comp])
if n_hits > 0:
print(f'Cortex component {i_comp + 1}: {n_hits} hits')
print(list(dfHO20minus30.columns[np.array(rel_mask_y[i_comp], dtype=bool)]))
np.save('rel_mask_y_full_THRESH%i' % THRESH, rel_mask_y)
# dump Cortex results in nifti format
# Load the significant mask and iterate through each component to create NIfTI images
rel_mask_y = np.load('rel_mask_y_full_THRESH%i.npy' % THRESH)
for i_comp in range(n_keep):
out_nii = np.zeros(HO_atlas_cort.maps.shape) # Initialize output NIfTI image
comp_HO_weights = pls_left.y_loadings_[:, i_comp] - pls_right.y_loadings_[:, i_comp]
comp_HO_weights[rel_mask_y[i_comp] == 0] = 0 # Zero out non-significant weights
for i_feat in range(dfX.shape[-1]): # Iterate over features to map weights to ROIs
cur_feat_name = dfX.columns[i_feat].split(' (')[0]
# Adjust feature names for specific cases
if 'Stem' in cur_feat_name:
pass
else:
cur_feat_name = ('Right ' if 'right' in dfX.columns[i_feat] else 'Left ') + cur_feat_name
if 'Ventral Striatum' in cur_feat_name:
cur_feat_name = cur_feat_name.replace('Ventral Striatum', 'Accumbens')
b_found_roi = False
# Map component weights to corresponding ROIs in the atlas
for i_cort_label, cort_label in enumerate(HO_atlas_cort.labels):
if cur_feat_name in cort_label:
b_roi_mask = HO_atlas_cort.maps.get_data() == i_cort_label
print('Found: %s (%i voxels)' % (cort_label, n_roi_vox))
out_nii[b_roi_mask] = comp_HO_weights[i_feat]
b_found_roi = True
# Map component weights to corresponding ROIs in the atlas
for i_cort_label, cort_label in enumerate(HO_atlas_sub.labels):
if cur_feat_name in cort_label:
b_roi_mask = HO_atlas_sub.maps.get_data() == i_cort_label
n_roi_vox = np.sum(b_roi_mask)
print('Found: %s (%i voxels)' % (cort_label, n_roi_vox))
out_nii[b_roi_mask] = comp_HO_weights[i_feat]
b_found_roi = True
print('Comp %i: dumping %i region weights.' % (
(i_comp + 1), np.sum(comp_HO_weights != 0)))
SES_name = f'mode{i_mode + 1}'
out_nii= nib.Nifti1Image(out_nii, HO_atlas_cort.maps.affine)
out_nii.to_filename('AMsub2cortex/Hemispheric Difference Analysis/AMsub2HOcomp%i_%ihits_AMLeft_vs_AMRight.nii.gz' % ((i_comp + 1, int(n_hits))))
OUT_DIR_7 = 'AMsub2cortex/Hemispheric Difference Analysis'
# Iterate over components to visualize and save differences in amygdala components
for counter, i_comp in enumerate(range(n_comps)):
n_rois = pls_right.x_loadings_.shape[0]
X_AM_weights = pls_left.x_loadings_[:, i_comp] - pls_right.x_loadings_[:, i_comp]
# Create a figure for the heatmap visualization
f = plt.figure(figsize=(10, 7))
X_comp_weights = np.zeros((n_rois, 1))
X_comp_weights[:, 0] = X_AM_weights
# Prepare DataFrame of the weights, with modified column names for readability
dfdata = pd.DataFrame(X_AM_weights, index=((dfAM20minus30_L.columns.str.replace('b', '')).str.replace("'",'')).str.replace('(left hemisphere)', 'Group Difference'), columns=[''])
# Save the component weights to CSV and Excel files
dfdata.to_csv('%s/Hemispheric_Difference_AM_topcomp%i%s_style_.csv' % (OUT_DIR_7, counter + 1, SUFFIX))
dfdata.to_excel('%s/Hemispheric_Difference_AM_topcomp%i%s_style_.xls' % (OUT_DIR_7, counter + 1, SUFFIX))
# Generate and customize the heatmap using seaborn
ax = sns.heatmap(dfdata, cbar=True, linewidths=.75,
cbar_kws={'shrink': 0.5}, #'orientation': 'horizontal'}, #, 'label': 'Functional coupling deviation'},
square=True,
cmap=plt.cm.RdBu_r, center=0)
ax.set_xticklabels(ax.get_xticklabels(), fontsize=13)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=13)
# Adjust the heatmap display to ensure no cutoff at top/bottom
# fix for mpl bug that cuts off top/bottom of seaborn viz
b, t = plt.ylim() # discover the values for bottom and top
b += 0.5 # Add 0.5 to the bottom
t -= 0.5 # Subtract 0.5 from the top
plt.ylim(b, t) # update the ylim(bottom, top) values
plt.tight_layout()
plt.savefig('%s/Hemispheric_Difference_AM_topcomp%i%s_style_.pdf' % (OUT_DIR_7, counter + 1, SUFFIX))
#Histogram
OUT_DIR_8 = 'AMsub2cortex/Amygdala Subregion Histograms'
for name in dfAM20minus30.columns:
# Clean column names for labeling
dfAM20minus30_temp = dfAM20minus30.rename(columns = {(dfAM20minus30.columns[dfAM20minus30.columns.get_loc(name)]) : ((dfAM20minus30.columns[dfAM20minus30.columns.get_loc(name)]).replace('b', '')).replace("'",'')})
hist = plt.figure()
# Generate and save histogram
dfAM20minus30_temp.hist(column = ((dfAM20minus30.columns[dfAM20minus30.columns.get_loc(name)]).replace('b', '')).replace("'",''), bins=30)
plt.savefig('%s/%s_Histogram.pdf' % (OUT_DIR_8, ((dfAM20minus30.columns[dfAM20minus30.columns.get_loc(name)]).replace('b', '')).replace("'",'')), DPI=600)
####################################################################################
# Expression Level for each participant
#Fit Transform
n_comps = 8
#Analysis of difference in brain volumes between the two time regions
#PLS Canonical
n_comps = 8 # Number of components to consider in the PLS analysis
# Perform PLS Canonical Analysis to explore the relationship between amygdala and cortex volume changes
pls_phe = PLSCanonical(n_components=n_comps)
X_m, Y_m = pls_phe.fit_transform(dfAM20minus30, dfHO20minus30) # Fit and transform the data
# Initialize a DataFrame to store the expression levels for each participant
express = pd.DataFrame(columns=['index', 'x', 'y'])
express['index'] = age_T3.index
express.set_index("index", inplace=True)
#Age/Sex
# Extract and filter sex information for participants
sex = ukbb.loc[:, '31-0.0':'31-0.0']
sex = sex[subs_keep] # Filter based on previously determined indices
# Group participants by age to identify those with the same age (and potentially sex)
age = age_T3[age_T3.duplicated(keep=False)]
age = age.groupby(age.columns.tolist()).apply(lambda x: tuple(x.index)).tolist()
# Create a DataFrame to differentiate between male and female participants with the same age
same_sex_age = pd.DataFrame(columns=['male', 'female'])
temp_m = []
temp_f = []
for i in range(len(age)):
tuple_m = ()
tuple_f = ()
for index in age[i]:
if int(sex.loc[index]) == 1:
tuple_m = tuple_m + (index,)
else:
tuple_f = tuple_f + (index,)
temp_m.append(tuple_m)
temp_f.append(tuple_f)
same_sex_age['male'] = temp_m
same_sex_age['female'] = temp_f
OUT_DIR_9 = 'AMsub2cortex/Age_Sex/Amygdala'
# Loop over the number of components to analyze expression levels separately for male and female participants
for counter, mode in enumerate(range(n_comps)):
express['x'] = X_m[:, mode] # Amygdala expression levels
express['y'] = Y_m[:, mode] # Cortex expression levels
# Initialize lists to store median expression levels and thresholds for confidence intervals
median_xm, median_xf = [], []
lower_th_m, lower_th_f = [], []
upper_th_m, upper_th_f = [], []
temp_list_age = []
# Analyze expression levels for male participants
# This involves calculating median expression levels and confidence intervals
for tuples in same_sex_age['male']:
if len(tuples)>6:
temp_list_x = []
for index in tuples:
temp_list_x.append(express.loc[index,'x'])
temp_list_age.append(age_T3.loc[index,'21003-3.0'])
median_xm.append(np.median(temp_list_x))
it_diffs = []
for i in range(100):
bs_rs = np.random.RandomState(100)
bs_sample_inds = bs_rs.randint(0, len(temp_list_x), len(temp_list_x))
temp_list_x_train = [temp_list_x[i] for i in bs_sample_inds]
it_diffs.append(np.median(np.array(temp_list_x_train)))
lower_th_m.append(scoreatpercentile(it_diffs, 5, axis=0))
upper_th_m.append(scoreatpercentile(it_diffs, 100 - 5, axis=0))
temp_set = set()
age_m = [x for x in temp_list_age if x not in temp_set and (temp_set.add(x) or True)]
temp_list_age = []
# Similar steps are followed for female participants
for tuples in same_sex_age['female']:
if len(tuples)>10:
temp_list_x = []
for index in tuples:
temp_list_x.append(express.loc[index,'x'])
temp_list_age.append(age_T3.loc[index,'21003-3.0'])
median_xf.append(np.median(temp_list_x))
it_diffs = []
for i in range(100):
bs_rs = np.random.RandomState(100)
bs_sample_inds = bs_rs.randint(0, len(temp_list_x), len(temp_list_x))
temp_list_x_train = [temp_list_x[i] for i in bs_sample_inds]
it_diffs.append(np.median(np.array(temp_list_x_train)))
lower_th_f.append(scoreatpercentile(it_diffs, 5, axis=0))
upper_th_f.append(scoreatpercentile(it_diffs, 100 - 5, axis=0))
temp_set = set()
age_f = [x for x in temp_list_age if x not in temp_set and (temp_set.add(x) or True)]
# Plotting the results with error bars and linear fits to visualize trends
plt.errorbar(age_m, median_xm, yerr=[lower_th_m,upper_th_m], fmt='o', color = 'dodgerblue', mec='black')
plt.errorbar(age_f, median_xf, yerr=[lower_th_f,upper_th_f], fmt='o', color = 'purple', mec='black')
plt.legend(["Male", "Female"], loc='lower left')
m, b = np.polyfit(age_m, median_xm, 1)
mf, bf = np.polyfit(age_f, median_xf, 1)
plt.plot(age_m, m*np.array(age_m) + b, 'dodgerblue', mec = 'black')
plt.plot(age_f, mf*np.array(age_f) + bf, 'purple', mec = 'black')
plt.xlabel("Age (years)")
plt.ylabel("Subject Expression of Amygdala Structual Plasticity (+/-)")
plt.savefig('%s/Age_Sex_mode_%i.pdf' % (OUT_DIR_9, counter + 1), bbox_inches='tight')
plt.show()
OUT_DIR_10 = 'AMsub2cortex/Age_Sex/Brain'
# Similar steps are followed to analyze cortex expression levels
# The process involves fitting PLS models, extracting expression levels,
# and analyzing these levels in relation to age and sex, specifically for the cortex
for counter,mode in enumerate(range(n_comps)):
express['x'] = X_m[:,mode] # Amygdala expression levels
express['y'] = Y_m[:,mode] # Cortex expression levels
# Initialize lists to store median expression levels and thresholds for confidence intervals
median_ym, median_yf = [], []
lower_th_m, lower_th_f = [], []
upper_th_m, upper_th_f = [], []
temp_list_age = []
# Analyze expression levels for male participants
# This involves calculating median expression levels and confidence intervals
for tuples in same_sex_age['male']:
if len(tuples)>10:
temp_list_y = []
for index in tuples:
temp_list_y.append(express.loc[index,'y'])
temp_list_age.append(age_T3.loc[index,'21003-3.0'])
median_ym.append(np.median(temp_list_y))
it_diffs = []
for i in range(100):
bs_rs = np.random.RandomState(100)
bs_sample_inds = bs_rs.randint(0, len(temp_list_y), len(temp_list_y))
temp_list_y_train = [temp_list_y[i] for i in bs_sample_inds]
it_diffs.append(np.median(np.array(temp_list_y_train)))
lower_th_m.append(scoreatpercentile(it_diffs, 5, axis=0))
upper_th_m.append(scoreatpercentile(it_diffs, 100 - 5, axis=0))
temp_set = set()
age_m = [x for x in temp_list_age if x not in temp_set and (temp_set.add(x) or True)]
temp_list_age = []
# Similar steps are followed for female participants
for tuples in same_sex_age['female']:
if len(tuples)>6:
temp_list_y = []
for index in tuples:
temp_list_y.append(express.loc[index,'x'])
temp_list_age.append(age_T3.loc[index,'21003-3.0'])
median_yf.append(np.median(temp_list_y))
it_diffs = []
for i in range(100):
bs_rs = np.random.RandomState(100)
bs_sample_inds = bs_rs.randint(0, len(temp_list_y), len(temp_list_y))
temp_list_y_train = [temp_list_y[i] for i in bs_sample_inds]
it_diffs.append(np.median(np.array(temp_list_y_train)))
lower_th_f.append(scoreatpercentile(it_diffs, 5, axis=0))
upper_th_f.append(scoreatpercentile(it_diffs, 100 - 5, axis=0))
temp_set = set()
age_f = [x for x in temp_list_age if x not in temp_set and (temp_set.add(x) or True)]
# Plotting the results with error bars and linear fits to visualize trends
plt.errorbar(age_m, median_ym, yerr=[lower_th_m,upper_th_m], fmt='o', color = 'dodgerblue', mec='black')
plt.errorbar(age_f, median_yf, yerr=[lower_th_f,upper_th_f], fmt='o', color = 'purple', mec='black')
plt.legend(["Male", "Female"], loc='lower left')
m, b = np.polyfit(age_m, median_ym, 1)
mf, bf = np.polyfit(age_f, median_yf, 1)
plt.plot(age_m, m*np.array(age_m) + b, 'dodgerblue', mec = 'black')
plt.plot(age_f, mf*np.array(age_f) + bf, 'purple', mec = 'black')
plt.xlabel("Age (years)")
plt.ylabel("Subject Expression of Brain Structual Plasticity (+/-)")
plt.savefig('%s/Age_Sex_mode_%i.pdf' % (OUT_DIR_10, counter + 1), bbox_inches='tight')
plt.show()
##############################################################################
#Age/Sex analysis
#PheWAS with the 1414 participants at the first time point
pls_phe_20_1414 = PLSCanonical(n_components=n_comps)
X_m, Y_m = pls_phe_20_1414.fit_transform(
pd.DataFrame(FS_AM20_ss, columns=dfS20.columns), pd.DataFrame(FS_HO20_ss, columns=ukbb_HO20.columns))
express = pd.DataFrame(columns= ['index', 'x', 'y'], index = age_T2.index)
express['index'] = age_T2.index
express.set_index("index", inplace=True)
sex = ukbb.loc[:, '31-0.0':'31-0.0']
sex = sex[subs_keep]
age = age_T2[age_T2.duplicated(keep=False)]
age = age.groupby(age.columns.tolist()).apply(lambda x: tuple(x.index)).tolist()
same_sex_age = pd.DataFrame(columns = ['male', 'female'])
temp_m = []
temp_f = []
for i in range(len(age)):
tuple_m = ()
tuple_f = ()
for index in age[i]:
if int(sex.loc[index]) == 1:
tuple_m = tuple_m + (index,)
else:
tuple_f = tuple_f + (index,)
temp_m.append(tuple_m)
temp_f.append(tuple_f)
same_sex_age['male'] = temp_m
same_sex_age['female'] = temp_f
#PheWAS with the 1414 participants at the first time point
OUT_DIR_10 = 'AMsub2cortex/Age_Sex/Brain/First Time Point'
for counter,mode in enumerate(range(n_comps)):
express['x'] = X_m[:,mode]
express['y'] = Y_m[:,mode]
median_ym = []
median_yf = []
lower_th_m = []
lower_th_f = []
upper_th_m = []
upper_th_f = []
temp_list_age = []
for tuples in same_sex_age['male']:
if len(tuples)>10:
temp_list_y = []
for index in tuples:
temp_list_y.append(express.loc[index,'y'])
temp_list_age.append(age_T2.loc[index,'21003-2.0'])
median_ym.append(np.median(temp_list_y))
it_diffs = []
for i in range(100):
bs_rs = np.random.RandomState(100)
bs_sample_inds = bs_rs.randint(0, len(temp_list_y), len(temp_list_y))
temp_list_y_train = [temp_list_y[i] for i in bs_sample_inds]
it_diffs.append(np.median(np.array(temp_list_y_train)))
lower_th_m.append(scoreatpercentile(it_diffs, 5, axis=0))
upper_th_m.append(scoreatpercentile(it_diffs, 100 - 5, axis=0))
temp_set = set()