An international mega-analysis of psychedelic drug effects on brain circuit function.
The 2 matches
- [1] § Methods › Brain signal denoising ↔ ana8_uncorrected_data_pipelines_250820_NM.py, lines 138–215 · score 0.76 · noise components, independent components, ICA AROMA, aggressive, CSF, regresses
- [2] § Methods › Brain signal denoising ↔ ana8_uncorrected_data_pipelines_250820_NM.py, lines 138–215 · score 0.71 · discrete cosine, component, masks, anatomical, WM, threshold
Paper
Loaded from Europe PMC by your browser, not stored by OSCR: doi.org · Europe PMC
The paper is loaded when this pane is shown.
The authors' code
Python · 4,756 lines · 195 KB · no license · 2 matches
- import os
- import numpy as np
- import scipy as sc
- import json
- import glob
- import pandas as pd
- from sklearn.preprocessing import StandardScaler
- from nilearn.input_data import NiftiLabelsMasker
- from nilearn.signal import clean
- from nilearn.interfaces.fmriprep import load_confounds, load_confounds_components
- from nilearn.interfaces.fmriprep.load_confounds_components import _load_high_pass, _load_motion, _load_wm_csf, _load_global_signal, _load_compcor, _load_scrub
- from nilearn import datasets as ds
- import seaborn as sns
- from matplotlib import pylab as plt
- import pymc as pm
- import arviz
- from nilearn.interfaces.fmriprep import load_confounds_strategy
- n_rois = 100 #200#, 400
- for strategy in [
- # ['motion', 'high_pass', 'compcor'],
- # ['motion', 'high_pass', 'wm_csf', 'scrub'],
- # ['wm_csf', 'high_pass', 'ica_aroma'],
- # ['motion', 'high_pass', 'compcor', 'global_signal'],
- # ['motion', 'high_pass', 'wm_csf', 'scrub', 'global_signal'],
- ['wm_csf', 'high_pass', 'ica_aroma', 'global_signal']
- ]:
- # ['motion', 'high_pass', 'global_signal'],
- # ['motion', 'high_pass', 'wm_csf', 'global_signal'],
- # ['wm_csf', 'high_pass', 'ica_aroma', 'global_signal'],
- # ['motion', 'high_pass', 'compcor', 'global_signal'],
- # ['motion', 'high_pass', 'wm_csf', 'scrub', 'global_signal']]:
- # ['motion', 'compcor'],
- # ['motion', 'wm_csf', 'scrub'],
- # ['aroma', 'wm_csf'],
- # ['motion', 'compcor', 'global_signal'],
- # ['motion', 'wm_csf', 'scrub', 'global_signal'],
- # ['aroma', 'wm_csf', 'global_signal']]:
- # ['motion', 'global_signal'], # for the roadout of fMRIPrep outputs
- # ['wm_csf', 'global_signal'],
- # ['ica_aroma', 'global_signal'],
- # ['high_pass', 'global_signal'],
- # ['scrub', 'global_signal'],
- # ['compcor', 'global_signal'],
- # ['motion'], # for the roadout of fMRIPrep outputs
- # ['wm_csf'],
- # ['ica_aroma'],
- # ['high_pass'],
- # ['scrub'],
- # ['compcor']
- CCs_all = []
- FDs_all = []
- conds_all = []
- study_id_all = []
- drug_all = [] # 0=psilocy (blue); 1=LSD (orange); 2=Aya (green); 3=mescaline (red), 4=DMT (purple)
- drug_labels = np.array(['psilocybin', 'LSD', 'Aya', 'mescaline', 'DMT'])
- plt.close('all')
- strategy_suffix = '_'.join(strategy)
- try:
- os.mkdir(strategy_suffix)
- except:
- pass
- # atlas = ds.fetch_atlas_schaefer_2018(n_rois=n_rois, yeo_networks=7)
- atlas = ds.fetch_atlas_schaefer_2018(n_rois=n_rois, yeo_networks=17)
- cort_labels = [l.decode() for l in atlas.labels]
- masker = NiftiLabelsMasker(labels_img=atlas.maps)
- masker.fit()
- subcort_labels = list(pd.read_csv('_atlas_defs/Tian_subcortical38_labels.txt', header=None).values[:, 0])
- claustrum_labels = list(pd.read_csv('_atlas_defs/Claustrum_labels.txt', header=None).values[:, 0])
- cerebellum_labels = list(pd.read_csv('_atlas_defs/Cerebellum_labels.txt', header=None).values[:, 0])
- # sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- full_labels = cort_labels + subcort_labels + claustrum_labels + cerebellum_labels
- # construct subcort atlas
- # sb_nii_paths = []
- # for i in range(50):
- # cur_nii = glob.glob('_scripts_data/collaboration_psychedelics_ROIs/subcortical/Tian_subcortex_*_%i.nii.gz' % (i + 1))
- # if not len(cur_nii):
- # pass
- # else:
- # sb_nii_paths.append(cur_nii)
- # construct cerebellum atlas
- from nilearn.image import math_img
- import nibabel as nib
- from nilearn.image import resample_img
- cereb_nii_paths = []
- cereb_nii_data = np.zeros( (97, 115, 97)).astype(int)
- for i in range(17):
- cur_nii = glob.glob('_scripts_data/collaboration_psychedelics_ROIs/cerebellum/rBuckner_cerebellum_%i.nii.gz' % (i + 1))
- if not len(cur_nii):
- pass
- else:
- print(cur_nii)
- cereb_nii_paths.append(cur_nii[0])
- cur_nii_bin = nib.load(cur_nii[0]).get_fdata().astype(int)
- print(cur_nii_bin.sum())
- cereb_nii_data += cur_nii_bin
- head = nib.load(cereb_nii_paths[0]).header
- aff = nib.load(cereb_nii_paths[0]).affine
- cereb_nii = nib.Nifti1Image(cereb_nii_data, affine=aff, header=head)
- cereb_nii.to_filename('atlas_cereb_Buckner17.nii.gz')
- cereb_nii_r = resample_img(cereb_nii,
- target_affine=masker.labels_img_.affine,
- target_shape=masker.labels_img_.shape,
- interpolation='nearest')
- cereb_nii_r.to_filename('atlas_cereb_Buckner17_r.nii.gz')
- masker_cereb = NiftiLabelsMasker(labels_img=cereb_nii_r)
- masker_cereb.fit()
- masker_cereb.labels_img_.to_filename('atlas_cereb_Buckner17_masker_cereb.nii.gz')
- study_id = []
- # net17_labels = []
- # net7_label_names = np.array(['Vis', 'Som', 'DorsAtt', 'SalVent', 'Limbic',
- # 'Cont', 'Default', 'TempPar'])
- # for l in cort_labels:
- # if '_Vis' in l:
- # cur_l = 1
- # elif '_Som' in l:
- # cur_l = 2
- # elif 'DorsAtt' in l:
- # cur_l = 3
- # elif '_SalVent' in l:
- # cur_l = 4
- # elif '_Limbic' in l:
- # cur_l = 5
- # elif '_Cont' in l:
- # cur_l = 6
- # elif 'Default' in l:
- # cur_l = 7
- # elif 'TempPar' in l:
- # cur_l = 8
- # else:
- # print('error !')
- # net17_labels.append(cur_l)
- # net17_labels = np.array(net17_labels)
- # net7_regcnt = np.bincount(net17_labels - 1)
- net17_strs = [s.split('H_')[1].split('_')[0] for s in cort_labels]
- net17_label_names = np.unique(net17_strs)
- cort_dict = {net17_label_names[i]: (i+1) for i in np.arange(17)}
- net17_labels = []
- for cur_net_lab in net17_strs:
- net17_labels.append(cort_dict[cur_net_lab])
- net17_labels = np.array(net17_labels)
- # Maastrict data
- sub_strs = [p.split('/')[-1] for p in glob.glob('_data_uncorrected_fullcolumns/Maastricht_Psilocybin/sub-*')]
- cond = pd.read_csv('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected/Maastricht/ROI_figures/conditions.txt', delimiter='\t')
- def get_my_confounds_df(conf_t_path, conf_j_path, strategy):
- conf_t = pd.read_csv(conf_t_path, delimiter='\t')
- df_out = pd.DataFrame([])
- new_cols = None
- for cur_stra in strategy:
- try:
- if cur_stra == 'motion':
- # full: translation/rotation + quadratic terms (24 parameters)
- new_cols = getattr(load_confounds_components, '_load_motion')(conf_t, 'full')
- elif cur_stra == 'wm_csf':
- # basic: the averages in each mask (2 parameters)
- new_cols = getattr(load_confounds_components, '_load_wm_csf')(conf_t, 'basic')
- elif cur_stra == 'global_signal':
- # basic: just the global signal (1 parameter)
- new_cols = getattr(load_confounds_components, '_load_global_signal')(conf_t, 'basic')
- elif cur_stra == 'ica_aroma':
- # basic: aggressive/classic AROMA, only regress out the noise independent components
- new_cols = getattr(load_confounds_components, '_load_ica_aroma')(conf_t, 'basic')
- elif cur_stra == 'high_pass':
- # add cosine columns to confound matrix to be regressed out
- # adds discrete cosines transformation basis regressors to handle low-frequency signal drifts
- new_cols = getattr(load_confounds_components, '_load_high_pass')(conf_t)
- elif cur_stra == 'scrub':
- # we "tag" rfMRI scans with 0/1 indicators that are then regressed out, instead of removing scans from time series
- # fd_threhsold = 0.5 more regular threshold (Lior, various papers !!!)
- new_cols = getattr(load_confounds_components, '_load_scrub')(conf_t, scrub=5, fd_threshold=0.5, std_dvars_threshold=3)
- elif cur_stra == 'compcor':
- # anat_combined: noise components calculated using a white matter and CSF combined anatomical mask
- with open(conf_j_path, "rb") as f:
- conf_j = json.load(f)
- new_cols = getattr(load_confounds_components, '_load_compcor')(conf_t, conf_j, 'anat_combined', 5)
- except:
- print(f'!!! Exception when grapping {cur_stra} columns for {conf_j_path}')
- if new_cols is not None:
- df_out = pd.concat([df_out, new_cols], axis=1)
- return df_out
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- print(sub_name)
- conf_t_path = '/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Maastricht_Psilocybin/ROI_confounds/%s/func/%s_task-rest_desc-confounds_timeseries.tsv' % (sub_name, sub_name)
- df = pd.read_csv(conf_t_path, delimiter='\t')
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- plt.title('FD (#volumes > 0.5mm out of %i total): Maastricht_Psilocybin' % len(df.framewise_displacement))
- # plt.title('FD (#volumes > 0.5mm): Maastricht_Psilocybin\nimages > 0.2: %i/%i\nimages > 0.5: %i/%i\nimages > 0.8: %i/%i' % (
- # sum(df.framewise_displacement >0.2), len(df.framewise_displacement),
- # sum(df.framewise_displacement >0.5), len(df.framewise_displacement),
- # sum(df.framewise_displacement >0.8), len(df.framewise_displacement)))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Maastricht_Psilocybin_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): Maastricht_Psilocybin' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Maastricht_Psilocybin_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): Maastricht_Psilocybin')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Maastricht_Psilocybin_dvars.png')
- CCs = []
- conds = []
- n_skipped = 0
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Maastricht_Psilocybin/%s/func/%s_schaefer100_TS_noDenosing.csv' % (sub_name, sub_name), header=None)
- subcort = pd.read_csv('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected/Maastricht/ROI_figures/%s/func/%s_subcortical38_TS_noDenosing.csv' % (sub_name, sub_name), header=None)
- claustrum = pd.read_csv('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected/Maastricht/ROI_figures/%s/func/%s_claustrum_TS_noDenosing.csv' % (sub_name, sub_name), header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb = pd.read_csv('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected/Maastricht/ROI_figures/%s/func/%s_cerebellum_TS_noDenosing.csv' % (sub_name, sub_name), header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts))
- conf_t_path = '/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Maastricht_Psilocybin/ROI_confounds/%s/func/%s_task-rest_desc-confounds_timeseries.tsv' % (sub_name, sub_name)
- conf_j_path = '/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Maastricht_Psilocybin/ROI_confounds/%s/func/%s_task-rest_desc-confounds_timeseries.json' % (sub_name, sub_name)
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- conf = pd.DataFrame(np.nan_to_num(conf))
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- n_skipped += 1
- print('Skipping subject: %s' % sub_name)
- continue
- # nii_fmri = masker.inverse_transform(sub_ts)
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- else:
- continue
- print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- cur_cond = cond[cond.Participant.values == int(sub_name[-2:])].Group_1isplacebo
- if len(cur_cond) == 0:
- continue
- cur_cond = int(cur_cond.values == 2) # 1 == drug / 0 == placebo
- study_id_all.append(0)
- CCs.append(sub_CC)
- conds.append(cur_cond)
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(cur_cond)
- drug_all.append(0)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/maastrich_coverage.nii.gz')
- CCs = np.array(CCs)
- conds = np.array(conds)
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- CCs = np.nan_to_num(CCs)
- out_mat = np.zeros((161, 161))
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_avg_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/maastricht_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/maastricht_cumabs_inter_regnorm_noabs.pdf')
- # Palhano data: 18 subs, 2 conds; Aya dataset ! (mislabeled)
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Palhano_Psilocybin/*/sub-*/sub-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'preacute' if 'predosing' in sub_name else 'acute'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-ayahuasca%s_desc-confounds_timeseries.tsv' % cur_cond
- sub_name = sub_strs[0].split('/')[-2]
- print(sub_name)
- df = pd.read_csv(conf_t_path, delimiter='\t')
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): palhano')
- plt.title('FD (#volumes > 0.5mm out of %i total): palhano' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_palhano_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): palhano' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_palhano_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): palhano')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_palhano_dvars.png')
- CCs = []
- conds = []
- n_skipped = 0
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_subcortical38_TS_noDenosing.csv'
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_claustrum_TS_noDenosing.csv'
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_cerebellum_TS_noDenosing.csv'
- cereb = pd.read_csv(cereb_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- assert n_rois + 61 == sub_ts.shape[1]
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts))
- cur_cond = 'preacute' if 'predosing' in sub_name else 'acute'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-ayahuasca%s_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- n_skipped += 1
- print('Skipping subject: %s' % sub_name)
- continue
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- # signals=np.nan_to_num(sub_ts_z.values), standardize=False, np.nan_to_num(confounds=conf.iloc[:len(sub_ts_z)]))
- else:
- continue
- # print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(1)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'acute'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'acute'))
- drug_all.append(2)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/palhano_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Palhano_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Palhano_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Palhano_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/palhano_cumabs_inter_regnorm_noabs.pdf')
- # Zurich data / LSD: 25 subs, 2 conds (Flora, Katrin)
- del out_df
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Zurich_LSD_Psilocybin/output_LSD/sub-*/ses-*/func/sub-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'lsd' if 'ses-lsd' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- df = pd.read_csv(conf_t_path, delimiter='\t')
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): zurichlsd')
- plt.title('FD (#volumes > 0.5mm out of %i total): zurichlsd' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_zurichlsd_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): zurichlsd' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Zurichlsd_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): zurichlsd')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_zurichlsd_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_subcortical38_TS_noDenosing.csv'
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_claustrum_TS_noDenosing.csv'
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_cerebellum_TS_noDenosing.csv'
- cereb = pd.read_csv(cereb_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- assert n_rois + 61 == sub_ts.shape[1]
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts))
- cur_cond = 'lsd' if 'ses-lsd' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- conf_j_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.json'
- if not os.path.exists(conf_t_path):
- print('Skipping this subject: confound file not found!')
- continue
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- n_skipped += 1
- print('Skipping subject: %s' % sub_name)
- continue
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- else:
- continue
- print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(2)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'lsd'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'lsd'))
- drug_all.append(1)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/zurichlsd_coverage.nii.gz')
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Zurich_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Zurich_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.5)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_regnorm.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_regnorm.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichLSD_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_regnorm_noabs.pdf')
- # Zurich data / Psilo: 25 subs, 2 conds (Flora, Katrin)
- del out_df
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Zurich_LSD_Psilocybin/output_Psilo/sub-*/ses-*/func/sub-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'lsd' if 'ses-lsd' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): zurichpsilo')
- plt.title('FD (#volumes > 0.5mm out of %i total): zurichpsilo' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_zurichpsilo_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): zurichpsilo' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_zurichpsilo_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): zurichpsilo')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_zurichpsilo_dvars.png')
- CCs = []
- conds = []
- n_skipped = 0
- for sub_name in sub_strs:
- # print(sub_name)
- try:
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_subcortical38_TS_noDenosing.csv'
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_claustrum_TS_noDenosing.csv'
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_cerebellum_TS_noDenosing.csv'
- cereb = pd.read_csv(cereb_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- assert n_rois + 61 == sub_ts.shape[1]
- except:
- print(f'CSV probably corrupted: {sub_name}')
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 'psi' if '-psi' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- conf_j_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.json'
- if not os.path.exists(conf_t_path):
- print('Skipping this subject: confound file not found!')
- continue
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- n_skipped += 1
- print('Skipping subject: %s' % sub_name)
- continue
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- else:
- continue
- print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(3)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'psi'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'psi'))
- drug_all.append(0)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/zurichpsilo_coverage.nii.gz')
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- if len(CCs) > 0:
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Zurich_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Zurich_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.5)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/ZurichPsilo_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/zurich_cumabs_inter_regnorm_noabs.pdf')
- # continue # HACK !!! skipping Barrett for now
- # Barrett data: 38 subs, 2 conds (Barrett, Manoj Doss)
- del out_df
- # import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Barrett_Psilocybin/*/*/*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'psilocybin' if 'Session5' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- sub_name = sub_strs[0].split('/')[-3]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): barrett')
- plt.title('FD (#volumes > 0.5mm out of %i total): barrett' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_barrett_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): barrett' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_barrett_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): barrett')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_barrett_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_subcortical38_TS_noDenosing.csv'
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_claustrum_TS_noDenosing.csv'
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_cerebellum_TS_noDenosing.csv'
- cereb = pd.read_csv(cereb_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- assert n_rois + 61 == sub_ts.shape[1]
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 'psilocybin' if 'Session5' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.tsv'
- conf_j_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_desc-confounds_timeseries.json'
- if not os.path.exists(conf_t_path):
- print('Skipping this subject: confound file not found!')
- continue
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- else:
- continue
- print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- # reg_avg_activity_bar.append(sub_ts.median(0).values)
- # reg_avg_activity_bar_std.append(sub_ts.median(0).values)
- #reg_avg_activity.append(sub_ts.mean(0).values)
- study_id_all.append(4)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'psilocybin'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'psilocybin'))
- drug_all.append(0)
- # study_id.append(3)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/barrett_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_avg_CC_drug_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_df = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Barrett_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm_noabs.pdf')
- # Roseman data: Psilocybin, 15 subjects
- del out_df
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Roseman_Psilocybin/sub-*/ses-*/func/sub-*_ses-*_run*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'PSILO' if 'PSILO' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): Roseman_Psilocybin')
- plt.title('FD (#volumes > 0.5mm out of %i total): Roseman_Psilocybin' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_Psilocybin_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): Roseman_Psilocybin' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_Psilocybin_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): Roseman_Psilocybin')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_Psilocybin_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_subcortical38_TS_noDenosing.csv'
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_claustrum_TS_noDenosing.csv'
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cereb_path = conf_path = sub_name.split('_schaefer100_TS')[0] + '_cerebellum_TS_noDenosing.csv'
- cereb = pd.read_csv(cereb_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cereb], axis=1)
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 'PSILO' if 'PSILO' in sub_name else 'plcb'
- conf_t_path = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- if not os.path.exists(conf_t_path):
- print('Skipping this subject: confound file not found!')
- continue
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- else:
- continue
- print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- # reg_avg_activity_bar.append(sub_ts.median(0).values)
- # reg_avg_activity_bar_std.append(sub_ts.median(0).values)
- #reg_avg_activity.append(sub_ts.mean(0).values)
- study_id_all.append(5)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'PSILO'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'PSILO'))
- drug_all.append(0)
- # study_id.append(3)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/rosemanpsilo_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_avg_CC_drug_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_drug_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb - CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb-drug.pdf')
- from sklearn.linear_model import LinearRegression
- out_df = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_df[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanPsilocybin_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm_noabs.pdf')
- # Roseman data: LSD, 38 subjects
- del out_df
- # TODO: do the cross-corr in run1 and in run3 for a given subject first, then average the two
- # to obtain a single cross-corr matrix for a given subject
- # goal being to have one set of obsevation per subject (for lat statistical inference)
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Roseman_LSD/sub-*/ses-*/func/sub-*_ses-*_run-1_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'plcb' if '-plcb' in sub_name else 'LSD'
- conf_t_path = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): Roseman_LSD')
- plt.title('FD (#volumes > 0.5mm out of %i total): Roseman_LSD' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_LSD_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): Roseman_LSD' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_LSD_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): Roseman_LSD')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Roseman_LSD_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts1 = pd.read_csv(sub_name, header=None)
- subcort1_path = sub_name.replace('schaefer%s' % n_rois, 'subcortical38')
- subcort1 = pd.read_csv(subcort1_path, header=None)
- claustrum1_path = sub_name.replace('schaefer%s' % n_rois, 'claustrum')
- claustrum1 = pd.read_csv(claustrum1_path, header=None)
- tmp = clean(
- signals=claustrum1.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum1.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum1.iloc[:, [0, 3]] = tmp
- cerebellum1_path = sub_name.replace('schaefer%s' % n_rois, 'cerebellum')
- cerebellum1 = pd.read_csv(cerebellum1_path, header=None)
- sub_ts1 = pd.concat([sub_ts1, subcort1, claustrum1, cerebellum1], axis=1)
- assert sub_ts1.shape[1] == 161
- sub_ts_z1 = pd.DataFrame(StandardScaler().fit_transform(sub_ts1), columns=full_labels)
- cur_cond = 'plcb' if '-plcb' in sub_name else 'LSD'
- conf_t_path1 = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path1 = conf_t_path.replace('.tsv', '.json')
- if not os.path.exists(conf_t_path1):
- print('Skipping this subject: confound file not found!')
- continue
- conf1 = get_my_confounds_df(conf_t_path1, conf_j_path1, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path1, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if len(conf1) != 0:
- conf1 = np.nan_to_num(conf1)
- sub_ts_z_deconf1 = clean(
- signals=sub_ts_z1.values, standardize=False, confounds=np.nan_to_num(conf1))
- else:
- continue
- sub_CC1 = np.arctanh(np.corrcoef(sub_ts_z_deconf1.T))
- sub_name2 = sub_name.replace('run-1', 'run-3')
- sub_ts2 = pd.read_csv(sub_name2, header=None)
- subcort2_path = subcort1_path.replace('run-1', 'run-3')
- subcort2 = pd.read_csv(subcort2_path, header=None)
- claustrum2_path = claustrum1_path.replace('run-1', 'run-3')
- claustrum2 = pd.read_csv(claustrum2_path, header=None)
- tmp = clean(
- signals=claustrum2.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum2.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum2.iloc[:, [0, 3]] = tmp
- cerebellum2_path = cerebellum1_path.replace('run-1', 'run-3')
- cerebellum2 = pd.read_csv(cerebellum2_path, header=None)
- sub_ts2 = pd.concat([sub_ts2, subcort2, claustrum2, cerebellum2], axis=1)
- assert sub_ts2.shape[1] == 161
- sub_ts_z2 = pd.DataFrame(StandardScaler().fit_transform(sub_ts2), columns=full_labels)
- conf_t_path2 = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-3_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path2 = conf_t_path.replace('.tsv', '.json')
- if not os.path.exists(conf_t_path2):
- print('Skipping this subject: confound file not found!')
- continue
- conf2 = get_my_confounds_df(conf_t_path2, conf_j_path2, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path2, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if len(conf2) != 0:
- conf2 = np.nan_to_num(conf2)
- sub_ts_z_deconf2 = clean(
- signals=sub_ts_z2.values, standardize=False, confounds=np.nan_to_num(conf2))
- else:
- continue
- sub_CC2 = np.arctanh(np.corrcoef(sub_ts_z_deconf2.T))
- # reg_avg_activity_bar.append(sub_ts.median(0).values)
- # reg_avg_activity_bar_std.append(sub_ts.median(0).values)
- #reg_avg_activity.append(sub_ts.mean(0).values)
- CCs.append(np.mean([sub_CC1, sub_CC2], axis=0)) # concatenating across run1 and run3 cross-corr matrices
- conds.append(int(cur_cond == 'LSD'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(np.mean([sub_CC1, sub_CC2], axis=0)) # concatenating across run1 and run3 cross-corr matrices
- conds_all.append(int(cur_cond == 'LSD'))
- study_id_all.append(6)
- drug_all.append(1)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/rosemanlsd_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_avg_CC_drug_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_drug_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb - CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb-drug.pdf')
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_avg_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/RosemanLSD_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm_noabs.pdf')
- # Basel LSD data: 50 subs, 2 conds
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Basel_LSD_TS/sub*/*/func/sub-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'plcb' if 'ses-plcb' in sub_name else 'LSD'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- df = pd.read_csv(conf_t_path, delimiter='\t')
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): BaselLSD')
- plt.title('FD (#volumes > 0.5mm out of %i total): Basel_LSD' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_BaselLSD_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): BaselLSD' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_BaselLSD_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): BaselLSD')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_BaselLSD_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = sub_name.replace('_schaefer%s' % n_rois, 'subcortical38')
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = sub_name.replace('_schaefer%s' % n_rois, 'claustrum')
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cerebellum_path = sub_name.replace('_schaefer%s' % n_rois, 'cerebellum')
- cerebellum = pd.read_csv(cerebellum_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cerebellum], axis=1)
- assert sub_ts.shape[1] == 161
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 'plcb' if 'ses-plcb' in sub_name else 'LSD'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- # subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + 'subcortical38_TS_noDenosing.csv'
- # subcort = pd.read_csv(subcort_path)
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- # signals=np.nan_to_num(sub_ts_z.values), standardize=False, np.nan_to_num(confounds=conf.iloc[:len(sub_ts_z)]))
- else:
- continue
- # print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(7)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'LSD'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'LSD'))
- drug_all.append(1)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/BaselLSD_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/BaselLSD_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/BaselLSD_cumabs_inter_regnorm_noabs.pdf')
- # Basel LSD/Mesc/Psi data: 34 subs, 4 conds - LSD arm !!!
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Basel_LSD_Mescaline_Psilocybin/*/sub-*/*/func/sub-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'plcb'
- if 'ses-LSD' in sub_name:
- cur_cond = 'lsd'
- elif 'ses-mesc' in sub_name:
- cur_cond = 'mesc'
- elif 'ses-psil' in sub_name:
- cur_cond = 'psil'
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- print(sub_name.split('/')[-4])
- df = pd.read_csv(conf_t_path, delimiter='\t')
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): Basel4cond')
- plt.title('FD (#volumes > 0.5mm out of %i total): Basel4cond' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Basel4cond_FDabove0.5mm.png', dpi=300)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): Basel4cond' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Basel4cond_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): Basel4cond')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Basel4cond_dvars.png', dpi=300)
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = sub_name.replace('_schaefer%s' % n_rois, 'subcortical38')
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = sub_name.replace('_schaefer%s' % n_rois, 'claustrum')
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cerebellum_path = sub_name.replace('_schaefer%s' % n_rois, 'cerebellum')
- cerebellum = pd.read_csv(cerebellum_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cerebellum], axis=1)
- assert sub_ts.shape[1] == 161
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 0
- if 'ses-LSD' in sub_name:
- cur_cond = 1
- elif 'ses-mesc' in sub_name:
- continue # see below
- elif 'ses-psil' in sub_name:
- continue # see below
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- # subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + 'subcortical38_TS_noDenosing.csv'
- # subcort = pd.read_csv(subcort_path)
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- # signals=np.nan_to_num(sub_ts_z.values), standardize=False, np.nan_to_num(confounds=conf.iloc[:len(sub_ts_z)]))
- else:
- continue
- # print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(8)
- CCs.append(sub_CC)
- conds.append(int(cur_cond))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond))
- drug_all.append(1) # 0=psyloc (blue); 1=LSD (orange); 2=Aya (green)
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condLSD_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condLSD_cumabs_inter_regnorm_noabs.pdf')
- # Basel LSD/Mesc/Psi data: 34 subs, 4 conds - Mesc arm !!!
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Basel_LSD_Mescaline_Psilocybin/*/sub-*/*/func/sub-*_schaefer100_TS_noDenosing.csv')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = sub_name.replace('_schaefer%s' % n_rois, 'subcortical38')
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = sub_name.replace('_schaefer%s' % n_rois, 'claustrum')
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cerebellum_path = sub_name.replace('_schaefer%s' % n_rois, 'cerebellum')
- cerebellum = pd.read_csv(cerebellum_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cerebellum], axis=1)
- assert sub_ts.shape[1] == 161
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 0
- if 'ses-LSD' in sub_name:
- continue
- elif 'ses-mesc' in sub_name:
- cur_cond = 1
- elif 'ses-psil' in sub_name:
- continue
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- # subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + 'subcortical38_TS_noDenosing.csv'
- # subcort = pd.read_csv(subcort_path)
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- # signals=np.nan_to_num(sub_ts_z.values), standardize=False, np.nan_to_num(confounds=conf.iloc[:len(sub_ts_z)]))
- else:
- continue
- # print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # # stop
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(9)
- CCs.append(sub_CC)
- conds.append(int(cur_cond))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond))
- drug_all.append(3) # 0=psyloc (blue); 1=LSD (orange); 2=Aya (green); 3=Mesc
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=cort_labels, yticklabels=cort_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condmesc_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condmesc_cumabs_inter_regnorm_noabs.pdf')
- # Basel LSD/Mesc/Psi data: 34 subs, 4 conds - psilocybin arm !!!
- import os
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Basel_LSD_Mescaline_Psilocybin/*/sub-*/*/func/sub-*_schaefer100_TS_noDenosing.csv')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- # print(sub_name)
- sub_ts = pd.read_csv(sub_name, header=None)
- subcort_path = sub_name.replace('_schaefer%s' % n_rois, 'subcortical38')
- subcort = pd.read_csv(subcort_path, header=None)
- claustrum_path = sub_name.replace('_schaefer%s' % n_rois, 'claustrum')
- claustrum = pd.read_csv(claustrum_path, header=None)
- tmp = clean(
- signals=claustrum.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum.iloc[:, [0, 3]] = tmp
- cerebellum_path = sub_name.replace('_schaefer%s' % n_rois, 'cerebellum')
- cerebellum = pd.read_csv(cerebellum_path, header=None)
- sub_ts = pd.concat([sub_ts, subcort, claustrum, cerebellum], axis=1)
- assert sub_ts.shape[1] == 161
- sub_ts_z = pd.DataFrame(StandardScaler().fit_transform(sub_ts), columns=full_labels)
- cur_cond = 0
- if 'ses-LSD' in sub_name:
- continue
- elif 'ses-mesc' in sub_name:
- continue
- elif 'ses-psil' in sub_name:
- cur_cond = 1
- conf_t_path = sub_name.split('_schaefer100_TS')[0] + '_task-rest_run-01_desc-confounds_timeseries.tsv'
- conf_j_path = conf_t_path.replace('.tsv', '.json')
- conf = get_my_confounds_df(conf_t_path, conf_j_path, strategy)
- # motion correction
- df = pd.read_csv(conf_t_path, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- # subcort_path = conf_path = sub_name.split('_schaefer100_TS')[0] + 'subcortical38_TS_noDenosing.csv'
- # subcort = pd.read_csv(subcort_path)
- if len(conf) != 0:
- sub_ts_z_deconf = clean(
- signals=np.nan_to_num(sub_ts_z.values), standardize=False, confounds=np.nan_to_num(conf))
- # signals=np.nan_to_num(sub_ts_z.values), standardize=False, np.nan_to_num(confounds=conf.iloc[:len(sub_ts_z)]))
- else:
- continue
- # print(f'{sub_name} has {len(conf.columns)} columns to noise clean!')
- # sub_ts_z_deconf = pd.DataFrame(
- # sub_ts_z_deconf, columns=cort_labels)
- # # stop
- # sub_CC = np.corrcoef(sub_ts_z_deconf.T)
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf.T))
- study_id_all.append(10)
- CCs.append(sub_CC)
- conds.append(int(cur_cond))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond))
- drug_all.append(0) # 0=psyloc (blue); 1=LSD (orange); 2=Aya (green); 3=Mesc
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_avg_CC_drug_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_avg_CC_drug_max.pdf')
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_intra.pdf')
- # ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_inter.pdf')
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_inter_norm.pdf')
- ax = pd.DataFrame(cum_intra_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net,
- index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/Basel4condpsil_cumabs_inter_regnorm_%s.pdf' % strategy_suffix)
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Basel4condpsil_cumabs_inter_regnorm_noabs.pdf')
- # Timmerman data: DMT, 20-ish subjects
- del out_df
- # TODO: do the cross-corr in run1 and in run3 for a given subject first, then average the two
- # to obtain a single cross-corr matrix for a given subject
- # goal being to have one set of obsevation per subject (for lat statistical inference)
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Timmermann_DMT/sub-*/ses-*/func/sub-*_ses-*schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- cur_cond = 'PCB' if 'PCB' in sub_name else 'DMT'
- conf_t_path = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): timmermann_DMT')
- plt.title('FD (#volumes > 0.5mm out of %i total): timmermann_DMT' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_timmermann_DMT_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): timmermann_DMT' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_timmermann_DMT_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): timmermann_DMT')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_timmermann_DMT_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- print(sub_name)
- sub_ts1 = pd.read_csv(sub_name, header=None)
- subcort1_path = sub_name.replace('_schaefer%s' % n_rois, 'subcortical38')
- subcort1 = pd.read_csv(subcort1_path, header=None)
- claustrum1_path = sub_name.replace('_schaefer%s' % n_rois, 'claustrum')
- claustrum1 = pd.read_csv(claustrum1_path, header=None)
- tmp = clean(
- signals=claustrum1.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum1.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum1.iloc[:, [0, 3]] = tmp
- cerebellum1_path = sub_name.replace('_schaefer%s' % n_rois, 'cerebellum')
- cerebellum1 = pd.read_csv(cerebellum1_path, header=None)
- sub_ts1 = pd.concat([sub_ts1, subcort1, claustrum1, cerebellum1], axis=1)
- assert sub_ts1.shape[1] == 161
- sub_ts_z1 = pd.DataFrame(StandardScaler().fit_transform(sub_ts1), columns=full_labels)
- #print(sub_ts_z1)
- cur_cond = 'PCB' if 'PCB' in sub_name else 'DMT'
- conf_t_path1 = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-1_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path1 = conf_t_path.replace('.tsv', '.json')
- # motion correction
- df = pd.read_csv(conf_t_path1, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if not os.path.exists(conf_t_path1):
- print('Skipping this subject: confound file not found!')
- continue
- conf1 = get_my_confounds_df(conf_t_path1, conf_j_path1, strategy)
- if len(conf1) != 0:
- conf1 = np.nan_to_num(conf1)
- sub_ts_z_deconf1 = clean(
- signals=sub_ts_z1.values, standardize=False, confounds=np.nan_to_num(conf1))
- else:
- continue
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf1.T))
- # reg_avg_activity_bar.append(sub_ts.median(0).values)
- # reg_avg_activity_bar_std.append(sub_ts.median(0).values)
- #reg_avg_activity.append(sub_ts.mean(0).values)
- study_id_all.append(11)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'DMT'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'DMT'))
- drug_all.append(4) # 0=psyloc (blue); 1=LSD (orange); 2=Aya (green); 3=Mesc; 4=DMT
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_avg_CC_drug_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_drug_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb - CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb-drug.pdf')
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- #y = np.arctanh(CCs[:, i_roi, j_roi])
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_avg_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/timmermann_dmt_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm_noabs.pdf')
- # Siegel data: 6 subjects
- del out_df
- # TODO: do the cross-corr in run1 and in run3 for a given subject first, then average the two
- # to obtain a single cross-corr matrix for a given subject
- # goal being to have one set of obsevation per subject (for lat statistical inference)
- sub_strs = glob.glob('/Users/danilo/f/project_METAPSYCHO_BOLDpsychCONS/_data_uncorrected_fullcolumns/Siegel/sub-*_ses-*_schaefer100_TS_noDenosing.csv')
- # QC: FD + dvars
- study_FDs = []
- study_dvars = []
- for sub_name in sub_strs:
- conf_t_path = sub_name.split('_ses-')[0] + '_ses-base_task-rest_run-01_desc-confounds_timeseries.tsv'
- sub_name = sub_strs[0].split('/')[-4]
- print(sub_name)
- try:
- df = pd.read_csv(conf_t_path, delimiter='\t')
- except:
- continue
- cur_fd = (df.framewise_displacement > 0.5).sum()
- study_FDs.append(cur_fd)
- print(cur_fd)
- cur_dvars = df.dvars.mean()
- study_dvars.append(cur_dvars)
- print(cur_dvars)
- plt.figure()
- plt.hist(study_FDs, bins=20)
- # plt.title('FD (#volumes > 0.5mm): timmermann_DMT')
- plt.title('FD (#volumes > 0.5mm out of %i total): Siegel' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Siegel_FDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(np.array(study_FDs) / len(df.framewise_displacement), bins=20)
- plt.title('FD (fraction of volumes > 0.5mm out of %i total): Siegel' % len(df.framewise_displacement))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Siegel_fracFDabove0.5mm.png', dpi=600)
- plt.figure()
- plt.hist(study_dvars, bins=20)
- plt.title('DVARS (mean across TS): Siegel')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/QC_Siegel_dvars.png')
- CCs = []
- n_skipped = 0
- conds = []
- for sub_name in sub_strs:
- print(sub_name)
- sub_ts1 = pd.read_csv(sub_name, header=None)
- subcort1_path = sub_name.replace('_schaefer%s' % n_rois, '-subcortical38')
- subcort1 = pd.read_csv(subcort1_path, header=None)
- claustrum1_path = sub_name.replace('_schaefer%s' % n_rois, '_claustrum')
- claustrum1 = pd.read_csv(claustrum1_path, header=None)
- tmp = clean(
- signals=claustrum1.iloc[:, [0, 3]].to_numpy(),
- confounds=claustrum1.iloc[:, [1, 2, 4, 5]].to_numpy(),
- detrend=False, standardize=False, standardize_confounds=False, filter=False)
- claustrum1.iloc[:, [0, 3]] = tmp
- cerebellum1_path = sub_name.replace('_schaefer%s' % n_rois, '_cerebellum')
- cerebellum1 = pd.read_csv(cerebellum1_path, header=None)
- sub_ts1 = pd.concat([sub_ts1, subcort1, claustrum1, cerebellum1], axis=1)
- assert sub_ts1.shape[1] == 161
- sub_ts_z1 = pd.DataFrame(StandardScaler().fit_transform(sub_ts1), columns=full_labels)
- #print(sub_ts_z1)
- cur_cond = 'base' if 'base' in sub_name else 'psil'
- conf_t_path1 = sub_name.split('_ses-')[0] + '_ses-%s_task-rest_run-01_desc-confounds_timeseries.tsv' % cur_cond
- conf_j_path1 = conf_t_path.replace('.tsv', '.json')
- # motion correction
- df = pd.read_csv(conf_t_path1, delimiter='\t')
- sub_motion_frac = (df.framewise_displacement > 0.5).sum() / len(df.framewise_displacement)
- if sub_motion_frac > 0.15:
- print('Skipping subject: %s' % sub_name)
- n_skipped += 1
- continue
- if not os.path.exists(conf_t_path1):
- print('Skipping this subject: confound file not found!')
- continue
- conf1 = get_my_confounds_df(conf_t_path1, conf_j_path1, strategy)
- if len(conf1) != 0:
- conf1 = np.nan_to_num(conf1)
- sub_ts_z_deconf1 = clean(
- signals=sub_ts_z1.values, standardize=False, confounds=np.nan_to_num(conf1))
- else:
- continue
- sub_CC = np.arctanh(np.corrcoef(sub_ts_z_deconf1.T))
- # reg_avg_activity_bar.append(sub_ts.median(0).values)
- # reg_avg_activity_bar_std.append(sub_ts.median(0).values)
- #reg_avg_activity.append(sub_ts.mean(0).values)
- study_id_all.append(12)
- CCs.append(sub_CC)
- conds.append(int(cur_cond == 'psil'))
- FDs_all.append(df.framewise_displacement.mean())
- CCs_all.append(sub_CC)
- conds_all.append(int(cur_cond == 'psil'))
- drug_all.append(0) # 0=psyloc (blue); 1=LSD (orange); 2=Aya (green); 3=Mesc; 4=DMT
- CCs = np.array(CCs)
- conds = np.array(conds)
- masker.inverse_transform(sub_ts_z1.mean(0)).to_filename('_results_uncorrected_data_pipelines_ana8/siegel_coverage.nii.gz')
- CC_avg_plcb = CCs[conds == 0, :, :].mean(0)
- CC_avg_drug = CCs[conds == 1, :, :].mean(0)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_avg_CC_plcb_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels, vmin=-1, vmax=+1)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_avg_CC_drug_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(16, 10))
- sns.set(font_scale=0.4)
- sns.heatmap(CC_avg_drug - CC_avg_plcb, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_avg_CC_drug-plcb_%s.pdf' % strategy_suffix)
- # CC_avg_plcb_max = CCs[conds == 0, :, :].max(0)
- # CC_avg_drug_max = CCs[conds == 1, :, :].max(0)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug_max, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-1, vmax=+1)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_drug_max.pdf')
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(16, 10))
- # sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_plcb - CC_avg_drug, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/Barrett_avg_CC_plcb-drug.pdf')
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- CCs = np.nan_to_num(CCs)
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = CCs[:, i_roi, j_roi]
- #y = np.arctanh(CCs[:, i_roi, j_roi])
- X = conds[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_avg_lrcoef_%s.pdf' % strategy_suffix)
- # import matplotlib.pylab as plt
- # plt.figure(figsize=(20, 14))
- # sns.set(font_scale=0.4)
- # sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- # center=0, vmax=.35)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barret_lrcoef_viridis.pdf')
- cum_intra_net = np.zeros((17))
- cum_intra_net_cnt = np.zeros((17))
- cum_inter_net = np.zeros((17))
- cum_inter_net_cnt = np.zeros((17))
- for i in range(n_rois): # rows
- for j in range(n_rois): # columns
- if i==j: # skip connections to self
- continue
- if net17_labels[i] == net17_labels[j]: # intra-network edge !
- cum_intra_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_intra_net_cnt[net17_labels[i] - 1] += 1
- if net17_labels[i] != net17_labels[j]: # inter-network edge !
- cum_inter_net[net17_labels[i] - 1] += np.abs(out_df.iloc[i, j])
- cum_inter_net_cnt[net17_labels[i] - 1] += 1
- ax = pd.DataFrame(cum_intra_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Intra-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_avg_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- ax = pd.DataFrame(cum_inter_net, index=net17_label_names).plot.bar(fontsize=8, legend=False)
- plt.ylabel('cumulative absolute beta coefficients', fontsize=10)
- plt.title('Inter-network drug effects', fontsize=12)
- plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter.pdf')
- plt.savefig('_results_uncorrected_data_pipelines_ana8/siegel_cumabs_intra_regnorm_%s.pdf' % strategy_suffix)
- # ax = pd.DataFrame(cum_intra_net / cum_intra_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_norm.pdf')
- # ax = pd.DataFrame(cum_inter_net / cum_inter_net_cnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_norm.pdf')
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('cumulative absolute beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm.pdf')
- # # no abs
- # cum_intra_net = np.zeros((17))
- # cum_intra_net_cnt = np.zeros((17))
- # cum_inter_net = np.zeros((17))
- # cum_inter_net_cnt = np.zeros((17))
- # for i in range(n_rois): # rows
- # for j in range(n_rois): # columns
- # if i==j: # skip connections to self
- # continue
- # if net17_labels[i] == net17_labels[j]: # intra-network edge !
- # cum_intra_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_intra_net_cnt[net17_labels[i] - 1] += 1
- # if net17_labels[i] != net17_labels[j]: # inter-network edge !
- # cum_inter_net[net17_labels[i] - 1] += out_df.iloc[i, j]
- # cum_inter_net_cnt[net17_labels[i] - 1] += 1
- # ax = pd.DataFrame(cum_intra_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Intra-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_intra_regnorm_noabs.pdf')
- # ax = pd.DataFrame(cum_inter_net / net7_regcnt,
- # index=net17_label_names).plot.bar(fontsize=8, legend=False)
- # plt.ylabel('average beta coefficients (normalized)', fontsize=10)
- # plt.title('Inter-network drug effects', fontsize=12)
- # plt.tight_layout()
- # plt.savefig('_results_uncorrected/barrett_cumabs_inter_regnorm_noabs.pdf')
- plt.close('all')
- ##########################################################################################
- # now running actual stats on all the aggregated data
- ##########################################################################################
- FDs_all = np.array(FDs_all)
- CCs_all = np.array(CCs_all)
- conds_all = np.array(conds_all)
- study_id_all = np.array(study_id_all)
- drug_all = np.array(drug_all)
- n_studies = len(np.unique(study_id_all))
- from scipy.stats import ttest_rel, ttest_ind
- # appeal / R3: do subjects move their heads differently during placebo vs. target ?
- from scipy.stats import ttest_rel, ttest_ind
- n = min(sum(conds_all==0),
- sum(conds_all==1)
- )
- t, p = ttest_rel(
- FDs_all[(conds_all==0)][:n],
- FDs_all[(conds_all==1)][:n]
- )
- print('all drugs')
- print(p)
- all_avg_pvalue = 0
- all_avg_tvalue = 0
- a_all = []
- b_all = []
- for i_drug in np.unique(drug_all):
- n = min(sum((conds_all==0) & (drug_all==i_drug)),
- sum((conds_all==1) & (drug_all==i_drug))
- )
- a = FDs_all[(conds_all==0) & (drug_all==i_drug)][:n]
- b = FDs_all[(conds_all==1) & (drug_all==i_drug)][:n]
- t, p = ttest_rel(
- a, # cond_all is drug status
- b
- )
- print(drug_labels[i_drug])
- print(p)
- all_avg_pvalue += p
- all_avg_tvalue += t
- a_all += list(a)
- b_all += list(b)
- print('--- average p value ---')
- print(p / (np.max(drug_all) + 1)) # p-value = 0.09263450080062638 + higher order; FDup2 p= ; FDup3 p=; FDup4, log p=; exp p=; 1/x p=, sqrt(p) =
- print('--- average t value ---')
- print(t / (np.max(drug_all) + 1)) # p-value = 0.09263450080062638 + higher order; FDup2 p= ; FDup3 p=; FDup4, log p=; exp p=; 1/x p=, sqrt(p) =
- #boxplot
- import seaborn as sns
- X = pd.DataFrame((a_all, b_all)).T
- X.columns = ['placebo', 'drug']
- plt.figure(figsize=(6, 10))
- sns.boxplot(X)
- plt.xticks(fontsize=14)
- plt.yticks(fontsize=14)
- plt.title('framewise displacement', fontsize=18)
- plt.savefig('_results_uncorrected_data_pipelines_ana8/FD_dotbydot.pdf')
- # drug by drug with higher transformations
- X = np.zeros((7, 5))
- X_t = np.zeros((7, 5))
- for i_drug in np.unique(drug_all):
- n = min(sum((conds_all==0) & (drug_all==i_drug)),
- sum((conds_all==1) & (drug_all==i_drug))
- )
- a = FDs_all[(conds_all==0) & (drug_all==i_drug)][:n]
- b = FDs_all[(conds_all==1) & (drug_all==i_drug)][:n]
- t, p = ttest_rel(
- a, # cond_all is drug status
- b
- )
- print(drug_labels[i_drug])
- print(p)
- X[0, i_drug] = p
- X_t[0, i_drug] = t
- t, p = ttest_rel(
- a*a, # cond_all is drug status
- b*b
- )
- print(drug_labels[i_drug])
- print(p)
- X[1, i_drug] = p
- X_t[1, i_drug] = t
- t, p = ttest_rel(
- a*a*a, # cond_all is drug status
- b*b*b
- )
- print(drug_labels[i_drug])
- print(p)
- X[2, i_drug] = p
- X_t[2, i_drug] = t
- t, p = ttest_rel(
- a*a*a*a, # cond_all is drug status
- b*b*b*b
- )
- print(drug_labels[i_drug])
- print(p)
- X[3, i_drug] = p
- X_t[3, i_drug] = t
- t, p = ttest_rel(
- np.log(a), # cond_all is drug status
- np.log(b)
- )
- print(drug_labels[i_drug])
- print(p)
- X[4, i_drug] = p
- X_t[4, i_drug] = t
- t, p = ttest_rel(
- 1 / np.array(a), # cond_all is drug status
- 1 / np.array(b)
- )
- print(drug_labels[i_drug])
- print(p)
- X[5, i_drug] = p
- X_t[5, i_drug] = t
- t, p = ttest_rel(
- np.sqrt(a), # cond_all is drug status
- np.sqrt(b)
- )
- print(drug_labels[i_drug])
- print(p)
- X[6, i_drug] = p
- X_t[6, i_drug] = t
- tranfs = ['x', 'x2', 'x3', 'x4' , 'log(x)', '1/x', 'sqrt(x)']
- pX = pd.DataFrame(X, columns=drug_labels, index=tranfs)
- pX_t = pd.DataFrame(X_t, columns=drug_labels, index=tranfs)
- pX.to_csv('_results_uncorrected_data_pipelines_ana8/FD_drugbydrug_pvalues.csv')
- pX_t = np.abs(pX_t)
- pX_t.to_csv('_results_uncorrected_data_pipelines_ana8/FD_drugbydrug_tvalues.csv')
- # pX.to_csv('_results_uncorrected_data_pipelines_ana8/FD_drugbydrug_pvalues.csv', float_format='%.4')
- # pX_t.to_csv('_results_uncorrected_data_pipelines_ana8/FD_drugbydrug_tvalues.csv', float_format='%.4')
- #acc = cross_val_score(estimator=LinearSVC(), X=FDs_all[:, None], y=conds_all[:, None], cv=KFold(n_splits=10, random_state=43, shuffle=True)); print(acc.mean()); print(acc.std()*2)
- # 0.5984875983061102
- # 0.15332339004087756
- CCs_ravelized = CCs_all.reshape(CCs_all.shape[0], CCs_all.shape[1]*CCs_all.shape[1])
- CCs_ravelized = np.nan_to_num(CCs_ravelized)
- y = np.array(FDs_all > np.median(FDs_all))[:, None]
- acc = cross_val_score(estimator=LinearSVC(), X=CCs_ravelized, y=y, cv=KFold(n_splits=10, random_state=43, shuffle=True)); print(acc.mean()); print(acc.std()*2)
- # 0.501814882032668 -> so, no part int he brain, systematically distinguishes between high- versus low-motion participant connectomes in a robust way
- # 0.15367835036406347 most holistic analyses that we can think of
- for i_drug in np.arange(5):
- acc = cross_val_score(estimator=LinearSVC(), X=CCs_ravelized[drug_all==i_drug], y=y[drug_all==i_drug], cv=KFold(n_splits=10, random_state=43, shuffle=True)); print(acc.mean()); print(acc.std()*2)
- # none of 5 drugs, has above chance SVC classifier; when assessed drug by drug; that conclusion extends to any between-network and with-network effects as well
- from sklearn.model_selection import cross_val_score, LeaveOneGroupOut, KFold
- from sklearn.svm import LinearSVC
- from scipy.stats import pearsonr
- out_mat_placebo = np.zeros((161, 161))
- out_mat_drug = np.zeros((161, 161))
- for i_study in np.unique(study_id_all):
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- y = np.nan_to_num(CCs_all[:, i_roi, j_roi])
- x = FDs_all
- r, p = pearsonr(x[(conds_all==0) & (i_study==study_id_all)], y[(conds_all==0) & (i_study==study_id_all)])
- out_mat_placebo[i_roi, j_roi] = r
- r, p = pearsonr(x[(conds_all==1) & (i_study==study_id_all)], y[(conds_all==1) & (i_study==study_id_all)])
- out_mat_drug[i_roi, j_roi] = r
- out_df_placebo = pd.DataFrame(out_mat_placebo, columns=full_labels, index=full_labels)
- out_df_placebo.to_csv('_results_uncorrected_data_pipelines_ana8/FD-FCmap_placebo_study%i_%s.csv' % (i_study+1, strategy_suffix))
- out_df_drug = pd.DataFrame(out_mat_drug, columns=full_labels, index=full_labels)
- out_df_drug.to_csv('_results_uncorrected_data_pipelines_ana8/FD-FCmap_drug_study%i_%s.csv' % (i_study+1, strategy_suffix))
- print(ttest_ind(out_mat_placebo.ravel(), out_mat_drug.ravel()))
- from scipy.stats import pearsonr
- out_mat_placebo = np.zeros((161, 161))
- out_mat_drug = np.zeros((161, 161))
- for i_drug in np.unique(drug_all):
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- y = np.nan_to_num(CCs_all[:, i_roi, j_roi])
- x = FDs_all
- r, p = pearsonr(x[(conds_all==0) & (i_drug==drug_all)], y[(conds_all==0) & (i_drug==drug_all)])
- out_mat_placebo[i_roi, j_roi] = r
- r, p = pearsonr(x[(conds_all==1) & (i_drug==drug_all)], y[(conds_all==1) & (i_drug==drug_all)])
- out_mat_drug[i_roi, j_roi] = r
- out_df_placebo = pd.DataFrame(out_mat_placebo, columns=full_labels, index=full_labels)
- out_df_placebo.to_csv('_results_uncorrected_data_pipelines_ana8/FD-FCmap_placebo_psyche%s_%s.csv' % (drug_labels[i_drug], strategy_suffix))
- out_df_drug = pd.DataFrame(out_mat_drug, columns=full_labels, index=full_labels)
- out_df_drug.to_csv('_results_uncorrected_data_pipelines_ana8/FD-FCmap_drug_psyche%s_%s.csv' % (drug_labels[i_drug], strategy_suffix))
- print(ttest_ind(out_mat_placebo.ravel(), out_mat_drug.ravel()))
- # revision: compute FD susceptibility map
- from scipy.stats import pearsonr
- out_mat = np.zeros((161, 161))
- out_mat_p = np.zeros((161, 161))
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- y = np.nan_to_num(CCs_all[:, i_roi, j_roi])
- x = FDs_all
- r, p = pearsonr(x, y)
- out_mat[i_roi, j_roi], out_mat_p[i_roi, j_roi] = r, p
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.close('all')
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5}, vmin=-1.0, vmax=1.0,
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_FD_suscept_%s.pdf' % strategy_suffix)
- # out_mat[np.where(out_mat_p > (.05))] = 0
- out_mat[np.where(out_mat_p > (.05/25921))] = 0
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5}, vmin=-1.0, vmax=1.0,
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_FD_suscept_p05_%s.pdf' % strategy_suffix)
- for i_drug in np.unique(drug_all):
- # revision: compute FD susceptibility map
- from scipy.stats import pearsonr
- out_mat = np.zeros((161, 161))
- out_mat_p = np.zeros((161, 161))
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- y = np.nan_to_num(CCs_all[:, i_roi, j_roi])
- x = FDs_all
- r, p = pearsonr(x[drug_all==i_drug], y[drug_all==i_drug])
- out_mat[i_roi, j_roi], out_mat_p[i_roi, j_roi] = r, p
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.close('all')
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5}, vmin=-1.0, vmax=1.0,
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_FD_suscept_%s_drug%s.pdf' % (strategy_suffix, drug_labels[i_drug]))
- # out_mat[np.where(out_mat_p > (.05))] = 0
- out_mat[np.where(out_mat_p > (.05/25921))] = 0
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5}, vmin=-1.0, vmax=1.0,
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_FD_suscept_p05_%s_drug%s.pdf' % (strategy_suffix, drug_labels[i_drug]))
- # aggregate statistics: reg-reg conn
- from sklearn.linear_model import LinearRegression
- out_mat = np.zeros((161, 161))
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = np.nan_to_num(CCs_all[:, i_roi, j_roi])
- X = conds_all[:, None]
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- import matplotlib.pylab as plt
- plt.close('all')
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_lrcoef_%s.pdf' % strategy_suffix)
- import matplotlib.pylab as plt
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(np.abs(out_df), cmap='viridis', square=True, cbar_kws={"shrink": 0.5},
- center=0)#, vmax=.5)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_lrcoef_viridis_%s.pdf' % strategy_suffix)
- # visu
- from nilearn.input_data import NiftiLabelsMasker
- masker = NiftiLabelsMasker(atlas.maps)
- masker.fit()
- regionwise_effects = np.abs(out_df).values.sum(1)[None, :]
- out_nii1 = masker.inverse_transform(regionwise_effects)
- out_nii2 = masker_cereb.inverse_transform(regionwise_effects[:, -17:])
- out_nii = math_img('A + B', A=out_nii1, B=out_nii2)
- out_nii.to_filename('_results_uncorrected_data_pipelines_ana8/13studies_avg_effects_%s.nii.gz' % strategy_suffix)
- # aggregate statistics: reg-reg conn (PER DRUG)
- from sklearn.linear_model import LinearRegression
- for i_drug in np.unique(drug_all):
- out_mat = np.zeros((161, 161))
- for i_roi in range(161):
- for j_roi in range(161):
- if i_roi == j_roi:
- continue
- lr = LinearRegression(fit_intercept=True)
- y = np.nan_to_num(CCs_all[drug_all == i_drug, i_roi, j_roi])
- X = np.nan_to_num(conds_all[drug_all == i_drug, None])
- lr.fit(X, y)
- out_mat[i_roi, j_roi] = lr.coef_[0]
- out_df = pd.DataFrame(out_mat, columns=full_labels, index=full_labels)
- # visu
- import matplotlib.pylab as plt
- plt.close('all')
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- sns.heatmap(out_df, cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0)
- plt.tight_layout()
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_lrcoef_%s_drug%s.pdf' % (strategy_suffix, drug_labels[i_drug]))
- # from nilearn.input_data import NiftiLabelsMasker
- # masker = NiftiLabelsMasker(atlas.maps)
- # masker.fit()
- # regionwise_effects = np.abs(out_df).values.sum(1)[None, :]
- # out_nii = masker.inverse_transform(regionwise_effects)
- from nilearn.input_data import NiftiLabelsMasker
- masker = NiftiLabelsMasker(atlas.maps)
- masker.fit()
- regionwise_effects = np.abs(out_df).values.sum(1)[None, :]
- out_nii1 = masker.inverse_transform(regionwise_effects)
- out_nii2 = masker_cereb.inverse_transform(regionwise_effects[:, -17:])
- out_nii = math_img('A + B', A=out_nii1, B=out_nii2)
- out_nii.to_filename('_results_uncorrected_data_pipelines_ana8/13studies_avg_effects_%s_drug%s.nii.gz' % (strategy_suffix, drug_labels[i_drug]))
- CC_avg_plcb = np.nanmean(CCs_all[(drug_all == i_drug) & (conds_all == 0), :, :], axis=0)
- CC_avg_drug = np.nanmean(CCs_all[(drug_all == i_drug) & (conds_all == 1), :, :], axis=0)
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug - CC_avg_plcb , cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-0.25, vmax=+0.25)
- sns.heatmap(np.nan_to_num(CC_avg_drug - CC_avg_plcb) , cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- pd.DataFrame(np.nan_to_num(CC_avg_drug - CC_avg_plcb), index=full_labels, columns=full_labels).to_csv('_results_uncorrected_data_pipelines_ana8/13studies_drug-plcb_%s_drug%s.csv' % (strategy_suffix, drug_labels[i_drug]))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_drug-plcb_%s_drug%s.pdf' % (strategy_suffix, drug_labels[i_drug]))
- #all
- CC_avg_plcb = np.nanmean(CCs_all[(conds_all == 0), :, :], axis=0)
- CC_avg_drug = np.nanmean(CCs_all[(conds_all == 1), :, :], axis=0)
- plt.figure(figsize=(20, 14))
- sns.set(font_scale=0.4)
- # sns.heatmap(CC_avg_drug - CC_avg_plcb , cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- # center=0, xticklabels=cort_labels, yticklabels=cort_labels, vmin=-0.25, vmax=+0.25)
- sns.heatmap(np.nan_to_num(CC_avg_drug - CC_avg_plcb) , cmap='RdBu_r', square=True, cbar_kws={"shrink": 0.5},
- center=0, xticklabels=full_labels, yticklabels=full_labels)
- plt.tight_layout()
- pd.DataFrame(np.nan_to_num(CC_avg_drug - CC_avg_plcb), index=full_labels, columns=full_labels).to_csv('_results_uncorrected_data_pipelines_ana8/13studies_drug-plcb_%s_drugALL.csv' % (strategy_suffix))
- plt.savefig('_results_uncorrected_data_pipelines_ana8/13studies_drug-plcb_%s_drugALL.pdf' % (strategy_suffix))
- # # average CCs by network (to stablize the estimates that we input into the BHM), HPinterc
- # # net-net
- for cur_net1 in np.unique(net17_labels):
- for cur_net2 in np.unique(net17_labels):
- if cur_net1 == cur_net2:
- continue
- print('-' * 80)
- cur_net_name1 = net17_label_names[cur_net1 - 1]
- cur_net_name2 = net17_label_names[cur_net2 - 1]
- print(cur_net_name1)
- print('-- and --')
- print(cur_net_name2)
- net_inds1 = np.where(net17_labels == cur_net1)[0]
- net_inds2 = np.where(net17_labels == cur_net2)[0]
- y = CCs_all[:, net_inds1, :][:, :, net_inds2]
- y = y.reshape((len(y), len(net_inds1)*len(net_inds2))).mean(1)
- print(pd.DataFrame(y).describe())
- # count 273.000000
- # mean 0.340264
- # std 0.134900
- # min 0.078441
- # 25% 0.238487
- # 50% 0.314088
- # 75% 0.428571
- # max 0.750653
- n_drugs = len(np.unique(drug_all))
- pm_varnames = []
- with pm.Model() as hierarchical_model:
- hp_drugclass = pm.Normal('hp_drugclass', mu=0., sigma=1, shape=n_drugs)
- hp_drugclass_priors = list(np.zeros(n_studies))
- for j_drug in range(n_drugs):
- for k_study in np.unique(study_id_all[drug_all == j_drug]):
- hp_drugclass_priors[k_study] = hp_drugclass[j_drug]
- alpha_drugclass = pm.Normal('alpha_drugclass', mu=hp_drugclass_priors, sigma=1, shape=n_studies)
- # hp_cond = pm.Normal('hp_cond', mu=0., sigma=1, shape=1)
- # alpha_cond = pm.Normal('alpha_cond', mu=hp_cond, sigma=1, shape=2)
- # alpha_cond = pm.Normal('alpha_cond', mu=0, sigma=1, shape=2)
- # beta_cond = pm.Normal('beta_cond', mu=0, sigma=1)
- hp_beta = pm.Normal('hp_beta', mu=0., sigma=1, shape=1)
- slope_betas = pm.Normal('slope_betas', mu=hp_beta, sigma=1, shape=n_drugs)
- # beta_study_priors = list(np.zeros(n_studies))
- # for j_drug in range(n_drugs):
- # for k_study in np.unique(study_id_all[drug_all == j_drug]):
- # beta_study_priors[k_study] = hp_beta[j_drug]
- # beta_inter = pm.Normal('beta_inter_cond_drug', mu=beta_study_priors, sigma=2, shape=n_studies)
- # reg_effect = alpha_cond[conds]
- reg_effect = alpha_drugclass[study_id_all] + slope_betas[drug_all] * conds_all
- eps = pm.HalfCauchy('eps', 5) # Model error
- group_like = pm.Normal('beh_like', mu=reg_effect, sigma=eps, observed=y)
- with hierarchical_model:
- hierarchical_trace = pm.sample(draws=10000, n_init=5000, #init='advi',
- chains=3, cores=9, progressbar=True,
- random_seed=[1, 2, 3]) # one per chain needed
- print(pm.summary(hierarchical_trace, hdi_prob=0.66))
- OUT_DIR = '_results_uncorrected_data_pipelines_ana8'
- output_name = 'net-net_HPinterc_effect_%s-%s' % (cur_net_name1, cur_net_name2)
- t = hierarchical_trace
- # for cur_roi in hierarchical_model.vars:
- for cur_roi in hierarchical_model.named_vars:
- from matplotlib.lines import Line2D
- cur_roi = cur_roi
- plt.close('all')
- # THRESH = 0.5
- # THRESH = 1.0
- n_last_chains = 500
- try:
- suffix = ''
- fig = arviz.plot_posterior(t, var_names=cur_roi, textsize=15.0,
- hdi_prob=0.66)
- # fig = pm.plot_posterior(t[-n_last_chains:], varnames=[cur_roi],
- # credible_interval=0.95)
- # try:
- # for i_higher_cat in range(n_meta_cat):
- # fig[i_higher_cat].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- plt.tight_layout()
- plt.savefig('%s/%s_%s_posterior%s.png' % (OUT_DIR, output_name, cur_roi, suffix), dpi=150)
- fig = arviz.plot_trace(t, var_names=[cur_roi])
- # max_abs_mode = np.max(np.abs(hierarchical_trace2[-n_last_chains:][cur_roi].mean(0)))
- # fig = pm.traceplot(t[-n_last_chains:], varnames=[cur_roi])
- # max_abs_mode = np.max(np.abs(t[-n_last_chains:][cur_roi].mean(0)))
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # try:
- # fig[1][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # try:
- # if max_abs_mode < THRESH and not 'nuisance' in cur_roi:
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # post_lines = fig[0][0].get_lines()
- # custom_lines = [Line2D([0], [0], color=l.get_c(), lw=4) for l in post_lines]
- # if subgroup_labels is None:
- # subgroup_labels = ['subgroup %i' % i for i in range(len(custom_lines))]
- # fig[0][0].legend(custom_lines, subgroup_labels, loc='upper left', prop={'size': 7.5})
- plt.tight_layout()
- plt.savefig('%s/%s_%s%s.png' % (OUT_DIR, output_name, cur_roi,
- suffix), dpi=150)
- # plt.savefig('%s/%s_%s.png' % (OUT_DIR, output_name, cur_roi), dpi=150)
- except:
- pass
- # # average CCs by network (to stablize the estimates that we input into the BHM), HPinterc
- # # net-net (across drug average)
- for cur_net1 in np.unique(net17_labels):
- for cur_net2 in np.unique(net17_labels):
- if cur_net1 == cur_net2:
- continue
- print('-' * 80)
- cur_net_name1 = net17_label_names[cur_net1 - 1]
- cur_net_name2 = net17_label_names[cur_net2 - 1]
- print(cur_net_name1)
- print('-- and --')
- print(cur_net_name2)
- net_inds1 = np.where(net17_labels == cur_net1)[0]
- net_inds2 = np.where(net17_labels == cur_net2)[0]
- y = CCs_all[:, net_inds1, :][:, :, net_inds2]
- y = y.reshape((len(y), len(net_inds1)*len(net_inds2))).mean(1)
- print(pd.DataFrame(y).describe())
- # count 273.000000
- # mean 0.340264
- # std 0.134900
- # min 0.078441
- # 25% 0.238487
- # 50% 0.314088
- # 75% 0.428571
- # max 0.750653
- n_drugs = len(np.unique(drug_all))
- pm_varnames = []
- with pm.Model() as hierarchical_model:
- hp_drugclass = pm.Normal('hp_drugclass', mu=0., sigma=1, shape=n_drugs)
- hp_drugclass_priors = list(np.zeros(n_studies))
- for j_drug in range(n_drugs):
- for k_study in np.unique(study_id_all[drug_all == j_drug]):
- hp_drugclass_priors[k_study] = hp_drugclass[j_drug]
- alpha_drugclass = pm.Normal('alpha_drugclass', mu=hp_drugclass_priors, sigma=1, shape=n_studies)
- # hp_cond = pm.Normal('hp_cond', mu=0., sigma=1, shape=1)
- # alpha_cond = pm.Normal('alpha_cond', mu=hp_cond, sigma=1, shape=2)
- # alpha_cond = pm.Normal('alpha_cond', mu=0, sigma=1, shape=2)
- # beta_cond = pm.Normal('beta_cond', mu=0, sigma=1)
- #hp_beta = pm.Normal('hp_beta', mu=0., sigma=1, shape=1)
- slope_betas = pm.Normal('slope_betas', mu=0, sigma=1, shape=1)
- # beta_study_priors = list(np.zeros(n_studies))
- # for j_drug in range(n_drugs):
- # for k_study in np.unique(study_id_all[drug_all == j_drug]):
- # beta_study_priors[k_study] = hp_beta[j_drug]
- # beta_inter = pm.Normal('beta_inter_cond_drug', mu=beta_study_priors, sigma=2, shape=n_studies)
- # reg_effect = alpha_cond[conds]
- reg_effect = alpha_drugclass[study_id_all] + slope_betas * conds_all
- eps = pm.HalfCauchy('eps', 5) # Model error
- group_like = pm.Normal('beh_like', mu=reg_effect, sigma=eps, observed=y)
- with hierarchical_model:
- hierarchical_trace = pm.sample(draws=10000, n_init=5000, #init='advi',
- chains=3, cores=9, progressbar=True,
- random_seed=[1, 2, 3]) # one per chain needed
- print(pm.summary(hierarchical_trace, hdi_prob=0.66))
- OUT_DIR = '_results_uncorrected_data_pipelines_ana8'
- output_name = 'net-net_HPinterc_effect_%s-%s_across' % (cur_net_name1, cur_net_name2)
- t = hierarchical_trace
- for cur_roi in hierarchical_model.named_vars:
- from matplotlib.lines import Line2D
- cur_roi = cur_roi
- plt.close('all')
- # THRESH = 0.5
- # THRESH = 1.0
- n_last_chains = 500
- try:
- suffix = ''
- fig = arviz.plot_posterior(t, var_names=cur_roi, textsize=15.0,
- hdi_prob=0.66)
- # fig = pm.plot_posterior(t[-n_last_chains:], varnames=[cur_roi],
- # credible_interval=0.95)
- # try:
- # for i_higher_cat in range(n_meta_cat):
- # fig[i_higher_cat].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- plt.tight_layout()
- plt.savefig('%s/%s_%s_posterior%s.png' % (OUT_DIR, output_name, cur_roi, suffix), dpi=150)
- fig = arviz.plot_trace(t, var_names=[cur_roi])
- # max_abs_mode = np.max(np.abs(hierarchical_trace2[-n_last_chains:][cur_roi].mean(0)))
- # fig = pm.traceplot(t[-n_last_chains:], varnames=[cur_roi])
- # max_abs_mode = np.max(np.abs(t[-n_last_chains:][cur_roi].mean(0)))
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # try:
- # fig[1][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # try:
- # if max_abs_mode < THRESH and not 'nuisance' in cur_roi:
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # post_lines = fig[0][0].get_lines()
- # custom_lines = [Line2D([0], [0], color=l.get_c(), lw=4) for l in post_lines]
- # if subgroup_labels is None:
- # subgroup_labels = ['subgroup %i' % i for i in range(len(custom_lines))]
- # fig[0][0].legend(custom_lines, subgroup_labels, loc='upper left', prop={'size': 7.5})
- plt.tight_layout()
- plt.savefig('%s/%s_%s%s.png' % (OUT_DIR, output_name, cur_roi,
- suffix), dpi=150)
- # plt.savefig('%s/%s_%s.png' % (OUT_DIR, output_name, cur_roi), dpi=150)
- except:
- pass
- # # average CCs by network (to stablize the estimates that we input into the BHM), HPinterc
- # # net-THA-D
- net_THAD_labels = np.array([s.startswith('THA-D') * 20 for s in full_labels])
- for cur_net1 in np.unique(net17_labels):
- for cur_net2 in np.unique(net_THAD_labels):
- if cur_net2 == 0:
- continue
- print('-' * 80)
- cur_net_name1 = net17_label_names[cur_net1 - 1]
- cur_net_name2 = 'THA-D'
- print(cur_net_name1)
- print('-- and --')
- print(cur_net_name2)
- net_inds1 = np.where(net17_labels == cur_net1)[0]
- net_inds2 = np.where(net_THAD_labels == cur_net2)[0]
- y = CCs_all[:, net_inds1, :][:, :, net_inds2]
- y = y.reshape((len(y), len(net_inds1)*len(net_inds2))).mean(1)
- print(pd.DataFrame(y).describe())
- # count 273.000000
- # mean 0.340264
- # std 0.134900
- # min 0.078441
- # 25% 0.238487
- # 50% 0.314088
- # 75% 0.428571
- # max 0.750653
- n_drugs = len(np.unique(drug_all))
- pm_varnames = []
- with pm.Model() as hierarchical_model:
- hp_drugclass = pm.Normal('hp_drugclass', mu=0., sigma=1, shape=n_drugs)
- hp_drugclass_priors = list(np.zeros(n_studies))
- for j_drug in range(n_drugs):
- for k_study in np.unique(study_id_all[drug_all == j_drug]):
- hp_drugclass_priors[k_study] = hp_drugclass[j_drug]
- alpha_drugclass = pm.Normal('alpha_drugclass', mu=hp_drugclass_priors, sigma=1, shape=n_studies)
- # hp_cond = pm.Normal('hp_cond', mu=0., sigma=1, shape=1)
- # alpha_cond = pm.Normal('alpha_cond', mu=hp_cond, sigma=1, shape=2)
- # alpha_cond = pm.Normal('alpha_cond', mu=0, sigma=1, shape=2)
- # beta_cond = pm.Normal('beta_cond', mu=0, sigma=1)
- hp_beta = pm.Normal('hp_beta', mu=0., sigma=1, shape=1)
- slope_betas = pm.Normal('slope_betas', mu=hp_beta, sigma=1, shape=n_drugs)
- # beta_study_priors = list(np.zeros(n_studies))
- # for j_drug in range(n_drugs):
- # for k_study in np.unique(study_id_all[drug_all == j_drug]):
- # beta_study_priors[k_study] = hp_beta[j_drug]
- # beta_inter = pm.Normal('beta_inter_cond_drug', mu=beta_study_priors, sigma=2, shape=n_studies)
- # reg_effect = alpha_cond[conds]
- reg_effect = alpha_drugclass[study_id_all] + slope_betas[drug_all] * conds_all
- eps = pm.HalfCauchy('eps', 5) # Model error
- group_like = pm.Normal('beh_like', mu=reg_effect, sigma=eps, observed=y)
- with hierarchical_model:
- hierarchical_trace = pm.sample(draws=10000, n_init=5000, #init='advi',
- chains=3, cores=9, progressbar=True,
- random_seed=[1, 2, 3]) # one per chain needed
- print(pm.summary(hierarchical_trace, hdi_prob=0.66))
- OUT_DIR = '_results_uncorrected_data_pipelines_ana8'
- output_name = 'net-THAD_HPinterc_effect_%s-%s' % (cur_net_name1, cur_net_name2)
- t = hierarchical_trace
- for cur_roi in hierarchical_model.named_vars:
- from matplotlib.lines import Line2D
- cur_roi = cur_roi
- plt.close('all')
- # THRESH = 0.5
- # THRESH = 1.0
- n_last_chains = 500
- try:
- suffix = ''
- fig = arviz.plot_posterior(t, var_names=cur_roi, textsize=15.0,
- hdi_prob=0.66)
- # fig = pm.plot_posterior(t[-n_last_chains:], varnames=[cur_roi],
- # credible_interval=0.95)
- # try:
- # for i_higher_cat in range(n_meta_cat):
- # fig[i_higher_cat].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- plt.tight_layout()
- plt.savefig('%s/%s_%s_posterior%s.png' % (OUT_DIR, output_name, cur_roi, suffix), dpi=150)
- fig = arviz.plot_trace(t, var_names=[cur_roi])
- # max_abs_mode = np.max(np.abs(hierarchical_trace2[-n_last_chains:][cur_roi].mean(0)))
- # fig = pm.traceplot(t[-n_last_chains:], varnames=[cur_roi])
- # max_abs_mode = np.max(np.abs(t[-n_last_chains:][cur_roi].mean(0)))
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # try:
- # fig[1][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # try:
- # if max_abs_mode < THRESH and not 'nuisance' in cur_roi:
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # post_lines = fig[0][0].get_lines()
- # custom_lines = [Line2D([0], [0], color=l.get_c(), lw=4) for l in post_lines]
- # if subgroup_labels is None:
- # subgroup_labels = ['subgroup %i' % i for i in range(len(custom_lines))]
- # fig[0][0].legend(custom_lines, subgroup_labels, loc='upper left', prop={'size': 7.5})
- plt.tight_layout()
- plt.savefig('%s/%s_%s%s.png' % (OUT_DIR, output_name, cur_roi,
- suffix), dpi=150)
- # plt.savefig('%s/%s_%s.png' % (OUT_DIR, output_name, cur_roi), dpi=150)
- except:
- pass
- # # average CCs by network (to stablize the estimates that we input into the BHM), HPinterc
- # # net-THA-D (across drug average)
- net_THAD_labels = np.array([s.startswith('THA-D') * 20 for s in full_labels])
- for cur_net1 in np.unique(net17_labels):
- for cur_net2 in np.unique(net_THAD_labels):
- if cur_net2 == 0:
- continue
- print('-' * 80)
- cur_net_name1 = net17_label_names[cur_net1 - 1]
- cur_net_name2 = 'THA-D'
- print(cur_net_name1)
- print('-- and --')
- print(cur_net_name2)
- net_inds1 = np.where(net17_labels == cur_net1)[0]
- net_inds2 = np.where(net_THAD_labels == cur_net2)[0]
- y = CCs_all[:, net_inds1, :][:, :, net_inds2]
- y = y.reshape((len(y), len(net_inds1)*len(net_inds2))).mean(1)
- print(pd.DataFrame(y).describe())
- # count 273.000000
- # mean 0.340264
- # std 0.134900
- # min 0.078441
- # 25% 0.238487
- # 50% 0.314088
- # 75% 0.428571
- # max 0.750653
- n_drugs = len(np.unique(drug_all))
- pm_varnames = []
- with pm.Model() as hierarchical_model:
- hp_drugclass = pm.Normal('hp_drugclass', mu=0., sigma=1, shape=n_drugs)
- hp_drugclass_priors = list(np.zeros(n_studies))
- for j_drug in range(n_drugs):
- for k_study in np.unique(study_id_all[drug_all == j_drug]):
- hp_drugclass_priors[k_study] = hp_drugclass[j_drug]
- alpha_drugclass = pm.Normal('alpha_drugclass', mu=hp_drugclass_priors, sigma=1, shape=n_studies)
- # hp_cond = pm.Normal('hp_cond', mu=0., sigma=1, shape=1)
- # alpha_cond = pm.Normal('alpha_cond', mu=hp_cond, sigma=1, shape=2)
- # alpha_cond = pm.Normal('alpha_cond', mu=0, sigma=1, shape=2)
- # beta_cond = pm.Normal('beta_cond', mu=0, sigma=1)
- #hp_beta = pm.Normal('hp_beta', mu=0., sigma=1, shape=1)
- slope_betas = pm.Normal('slope_betas', mu=0, sigma=1, shape=1)
- # beta_study_priors = list(np.zeros(n_studies))
- # for j_drug in range(n_drugs):
- # for k_study in np.unique(study_id_all[drug_all == j_drug]):
- # beta_study_priors[k_study] = hp_beta[j_drug]
- # beta_inter = pm.Normal('beta_inter_cond_drug', mu=beta_study_priors, sigma=2, shape=n_studies)
- # reg_effect = alpha_cond[conds]
- reg_effect = alpha_drugclass[study_id_all] + slope_betas * conds_all
- eps = pm.HalfCauchy('eps', 5) # Model error
- group_like = pm.Normal('beh_like', mu=reg_effect, sigma=eps, observed=y)
- with hierarchical_model:
- hierarchical_trace = pm.sample(draws=10000, n_init=5000, #init='advi',
- chains=3, cores=9, progressbar=True,
- random_seed=[1, 2, 3]) # one per chain needed
- print(pm.summary(hierarchical_trace, hdi_prob=0.66))
- OUT_DIR = '_results_uncorrected_data_pipelines_ana8'
- output_name = 'net-THAD_HPinterc_effect_%s-%s_across' % (cur_net_name1, cur_net_name2)
- t = hierarchical_trace
- for cur_roi in hierarchical_model.named_vars:
- from matplotlib.lines import Line2D
- cur_roi = cur_roi
- plt.close('all')
- # THRESH = 0.5
- # THRESH = 1.0
- n_last_chains = 500
- try:
- suffix = ''
- fig = arviz.plot_posterior(t, var_names=cur_roi, textsize=15.0,
- hdi_prob=0.66)
- # fig = pm.plot_posterior(t[-n_last_chains:], varnames=[cur_roi],
- # credible_interval=0.95)
- # try:
- # for i_higher_cat in range(n_meta_cat):
- # fig[i_higher_cat].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- plt.tight_layout()
- plt.savefig('%s/%s_%s_posterior%s.png' % (OUT_DIR, output_name, cur_roi, suffix), dpi=150)
- fig = arviz.plot_trace(t, var_names=[cur_roi])
- # max_abs_mode = np.max(np.abs(hierarchical_trace2[-n_last_chains:][cur_roi].mean(0)))
- # fig = pm.traceplot(t[-n_last_chains:], varnames=[cur_roi])
- # max_abs_mode = np.max(np.abs(t[-n_last_chains:][cur_roi].mean(0)))
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # try:
- # fig[1][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # try:
- # if max_abs_mode < THRESH and not 'nuisance' in cur_roi:
- # fig[0][0].set_xlim(-THRESH, THRESH) # make plots more comparable
- # except:
- # pass
- # post_lines = fig[0][0].get_lines()
- # custom_lines = [Line2D([0], [0], color=l.get_c(), lw=4) for l in post_lines]
- # if subgroup_labels is None:
- # subgroup_labels = ['subgroup %i' % i for i in range(len(custom_lines))]
- # fig[0][0].legend(custom_lines, subgroup_labels, loc='upper left', prop={'size': 7.5})
- plt.tight_layout()
- plt.savefig('%s/%s_%s%s.png' % (OUT_DIR, output_name, cur_roi,
- suffix), dpi=150)
- # plt.savefig('%s/%s_%s.png' % (OUT_DIR, output_name, cur_roi), dpi=150)
- except:
- pass
- # # average CCs by network (to stablize the estimates that we input into the BHM), HPinterc
- # # net-GP
- # In [22]: np.array(full_labels)[net_GP_labels > 0]
- # Out[22]: array(['pGP-rh', 'aGP-rh', 'pGP-lh', 'aGP-lh'], dtype='<U36')
- net_GP_labels = np.array([(s.find('GP') != -1) * 20 for s in full_labels])
- for cur_net1 in np.unique(net17_labels):
- for cur_net2 in np.unique(net_GP_labels):
- if cur_net2 == 0:
- continue
- print('-' * 80)
- cur_net_name1 = net17_label_names[cur_net1 - 1]
- cur_net_name2 = 'GP'
- print(cur_net_name1)
- print('-- and --')
- print(cur_net_name2)
- net_inds1 = np.where(net17_labels == cur_net1)[0]
- net_inds2 = np.where(net_GP_labels == cur_net2)[0]
- y = CCs_all[:, net_inds1, :][:, :, net_inds2]
- y = y.reshape((len(y), len(net_inds1)*len(net_inds2))).mean(1)
- print(pd.DataFrame(y).describe())
- # count 273.000000
- # mean 0.340264
- # std 0.134900
- # min 0.078441
- # 25% 0.238487
- # 50% 0.314088
- # 75% 0.428571
- # max 0.750653
- n_drugs = len(np.unique(drug_all))
- pm_varnames = []
- with pm.Model() as hierarchical_model:
- hp_drugclass = pm.Normal('hp_drugclass', mu=0., sigma=1, shape=n_drugs)
- hp_drugclass_priors = list(np.zeros(n_studies))
- for j_drug in range(n_drugs):
- for k_study in np.unique(study_id_all[drug_all == j_drug]):
- hp_drugclass_priors[k_study] = hp_drugclass[j_drug]
- alpha_drugclass = pm.Normal('alpha_drugclass', mu=hp_drugclass_priors, sigma=1, shape=n_studies)
- # hp_cond = pm.Normal('hp_cond', mu=0., sigma=1, shape=1)
- # alpha_cond = pm.Normal('alpha_cond', mu=hp_cond, sigma=1, shape=2)
- # alpha_cond = pm.Normal('alpha_cond', mu=0, sigma=1, shape=2)
- # beta_cond = pm.Normal('beta_cond', mu=0, sigma=1)
- hp_beta = pm.Normal('hp_beta', mu=0., sigma=1, shape=1)
- slope_betas = pm.Normal('slope_betas', mu=hp_beta, sigma=1, shape=n_drugs)
- # beta_study_priors = list(np.zeros(n_studies))
- # for j_drug in range(n_drugs):
- # for k_study in np.unique(study_id_all[drug_all == j_drug]):
- # beta_study_priors[k_study] = hp_beta[j_drug]
- # beta_inter = pm.Normal('beta_inter_cond_drug', mu=beta_study_priors, sigma=2, shape=n_studies)
- # reg_effect = alpha_cond[conds]
- reg_effect = alpha_drugclass[study_id_all] + slope_betas[drug_all] * conds_all
- eps = pm.HalfCauchy('eps', 5) # Model error
- group_like = pm.Normal('beh_like', mu=reg_effect, sigma=eps, observed=y)
- with hierarchical_model:
- hierarchical_trace = pm.sample(draws=10000, n_init=5000, #init='advi',
- chains=3, cores=9, progressbar=True,
- random_seed=[1, 2, 3]) # one per chain needed
- print(pm.summary(hierarchical_trace, hdi_prob=0.66))
- OUT_DIR = '_results_uncorrected_data_pipelines_ana8'
- output_name = 'net-GP_HPinterc_effect_%s-%s' % (cur_net_name1, cur_net_name2)
- t = hierarchical_trace
- for cur_roi in hierarchical_model.named_vars:
- from matplotlib.lines import Line2D
- cur_roi = cur_roi
- plt.close('all')
- # THRESH = 0.5
- # THRESH = 1.0
- n_last_chains = 500
- try:
- suffix = ''
- fig = arviz.plot_posterior(t, var_names=cur_roi, textsize=15.0,
- hdi_prob=0.66)
- # fig = pm.plot_posterior(t[-n_last_chains:], varnames=[cur_roi],
- #
ana8_uncorrected_data_pipelines_250820_NM.py at commit 0c58406, no license · at the source
Overview
and 7 other authors
Olaf Sporns10, Joshua Siegel11, Nico Dosenbach11, David J Nutt3, Robin L Carhart-Harris1, Emmanuel A Stamatakis12, Danilo Bzdok13,1414 affiliations
- Department of Neurology, University of California San Francisco, San Francisco, CA USA
- Department of Psychiatry and Behavioral Sciences, Center for Psychedelic Research and Therapy, The University of Texas at Austin Dell Medical School, Austin, TX USA
- Department of Psychology, University of Exeter, Exeter, UK
- Department of Adult Psychiatry and Psychotherapy, University of Zurich, Zurich, Switzerland
- Brain Institute, Universidade Federal do Rio Grande do Norte, Natal, Brazil
- Center for Psychedelic and Consciousness Research, Johns Hopkins University School of Medicine, Baltimore, MD USA
- Department of Neuropsychology and Psychopharmacology, Faculty of Psychology and Neuroscience, Maastricht University, Maastricht, the Netherlands
- Neurobiology Research Unit, Rigshospitalet, Copenhagen, Denmark
- Department of Clinical Research, Clinical Pharmacology, University Hospital Basel, University of Basel, Basel, Switzerland
- Department of Psychological and Brain Sciences, Indiana University, Bloomington, IN USA
- Department of Psychiatry, NYU Langone Center for Psychedelic Medicine, NYU Grossman School of Medicine, New York, NY USA
- Division of Anaesthesia and Department of Clinical Neurosciences, University of Cambridge, Cambridge, UK
- Department of Biomedical Engineering, The Neuro - Montreal Neurological Institute (MNI), McConnell Brain Imaging Centre (BIC), McGill University, Montreal, Quebec Canada
- Mila - Quebec Artificial Intelligence Institute, Montreal, Quebec Canada
Abstract
The abstract is not reproduced here: the paper's license (CC BY-NC-ND) does not allow it. Read it in the paper, at the publisher or on Europe PMC.
Repository
Its files are read in the Code ↔ Paper reader above, with 2 matches between paragraphs and lines of code.
banilo/BOLD_psychedelics_consortium
0c5840620e54026734f9035a6d49ad54ef46d9a4, 11 December 2025Availability: 1 check, the latest on 29 September 2026: the link answers
- 29 September 2026: the link answers
2 files
- ana8_uncorrected_data_pi
pelines_250820_NM.py , Python, 4,756 lines, 2 matches - README.md, Text, 1 line
Code availability statement
The paper has a code availability statement. Its license (CC BY-NC-ND) does not allow reproducing it here; in short, from what the harvester recognized in it:
- it points to the authors' code: banilo/
BOLD_psychedelics_consor tium
Read it in the paper: doi.org/10.1038/s41591-026-04287-9.
Tracing map
Proposed by the machine: these links were found in the paper and verified at the source, without human review. The map will receive a Zenodo DOI once one of the paper's authors has validated it with their ORCID.
What the map holds:
- 1 repository of the authors' code, each at its verified commit, with its license and how the link was found in the paper;
- 1 script, each with its path and the digest of its content;
- 2 matches between paragraphs of the paper and lines of the code (method lexical-v1);
- neither the text of the paper nor the code itself.
Its JSON (tracing-map.json) is deposited on Zenodo with its DOI once the map is validated.
Data
No dataset and no data link were found in the paper.
Data availability statement
The paper has a data availability statement. Its license (CC BY-NC-ND) does not allow reproducing it here; in short, from what the harvester recognized in it:
- no repository, dataset or request procedure was recognized in it
Read it in the paper: doi.org/10.1038/s41591-026-04287-9.
Versions
The history of this record: each version stored by the harvester or made by a correction of its authors or of the maintainers of its code, and what changed in its facts. The texts of the paper (its abstract, its availability statements) are not part of it; versions that changed only those are not listed.
Version 1, 29 September 2026: the first record
Recorded: type, language, journal, volume, issue, pages, dates, 27 authors, 2 keywords, 11 MeSH terms, 1 funder, 64 references.
Cite
This paper
Girn, M., Doss, M. K., Roseman, L., Preller, K. H., Palhano-Fontes, F., Pasquini, L., Barrett, F. S., Mallaroni, P., Mason, N. L., Timmermann, C., McCulloch, D. E., Fisher, P. M., Winston, B. S., Moujaes, F., Muller, F., Liechti, M. E., Vollenweider, F. X., Ramaekers, J. G., Kuypers, K., . . . Bzdok, D. (2026). An international mega-analysis of psychedelic drug effects on brain circuit function. Nature medicine, 32(4), 1543-1554. https://
BibTeX
@article{girn2026interna
author = {Girn, Manesh and Doss, Manoj K and Roseman, Leor and Preller, Katrin H and Palhano-Fontes, Fernanda and Pasquini, Lorenzo and Barrett, Frederick S and Mallaroni, Pablo and Mason, Natasha L and Timmermann, Christopher and McCulloch, Drummond E and Fisher, Patrick M and Winston, Brian S and Moujaes, Flora and Muller, Felix and Liechti, Matthias E and Vollenweider, Franz X and Ramaekers, Johannes G and Kuypers, Kim and Araujo, Draulio B and Sporns, Olaf and Siegel, Joshua and Dosenbach, Nico and Nutt, David J and Carhart-Harris, Robin L and Stamatakis, Emmanuel A and Bzdok, Danilo},
title = {{An international mega-analysis of psychedelic drug effects on brain circuit function}},
journal = {Nature medicine},
year = {2026},
month = apr,
volume = {32},
number = {4},
pages = {1543--1554},
publisher = {Nature Portfolio},
issn = {1078-8956},
doi = {10.1038/
url = {https://
pmid = {41942645},
pmcid = {PMC13099416}
}
RIS
TY - JOUR
AU - Girn, Manesh
AU - Doss, Manoj K
AU - Roseman, Leor
AU - Preller, Katrin H
AU - Palhano-Fontes, Fernanda
AU - Pasquini, Lorenzo
AU - Barrett, Frederick S
AU - Mallaroni, Pablo
AU - Mason, Natasha L
AU - Timmermann, Christopher
AU - McCulloch, Drummond E
AU - Fisher, Patrick M
AU - Winston, Brian S
AU - Moujaes, Flora
AU - Muller, Felix
AU - Liechti, Matthias E
AU - Vollenweider, Franz X
AU - Ramaekers, Johannes G
AU - Kuypers, Kim
AU - Araujo, Draulio B
AU - Sporns, Olaf
AU - Siegel, Joshua
AU - Dosenbach, Nico
AU - Nutt, David J
AU - Carhart-Harris, Robin L
AU - Stamatakis, Emmanuel A
AU - Bzdok, Danilo
TI - An international mega-analysis of psychedelic drug effects on brain circuit function
T2 - Nature medicine
J2 - Nat Med
PY - 2026
DA - 2026/
VL - 32
IS - 4
SP - 1543
EP - 1554
SN - 1078-8956
PB - Nature Portfolio
DO - 10.1038/
UR - https://
LA - en
ER -
CSL-JSON
{
"id": "10.1038/
"type": "article-journal",
"title": "An international mega-analysis of psychedelic drug effects on brain circuit function",
"container-title": "Nature medicine",
"author": [
{
"family": "Girn",
"given": "Manesh"
},
{
"family": "Doss",
"given": "Manoj K"
},
{
"family": "Roseman",
"given": "Leor"
},
{
"family": "Preller",
"given": "Katrin H"
},
{
"family": "Palhano-Fontes",
"given": "Fernanda"
},
{
"family": "Pasquini",
"given": "Lorenzo"
},
{
"family": "Barrett",
"given": "Frederick S"
},
{
"family": "Mallaroni",
"given": "Pablo"
},
{
"family": "Mason",
"given": "Natasha L"
},
{
"family": "Timmermann",
"given": "Christopher"
},
{
"family": "McCulloch",
"given": "Drummond E"
},
{
"family": "Fisher",
"given": "Patrick M"
},
{
"family": "Winston",
"given": "Brian S"
},
{
"family": "Moujaes",
"given": "Flora"
},
{
"family": "Muller",
"given": "Felix"
},
{
"family": "Liechti",
"given": "Matthias E"
},
{
"family": "Vollenweider",
"given": "Franz X"
},
{
"family": "Ramaekers",
"given": "Johannes G"
},
{
"family": "Kuypers",
"given": "Kim"
},
{
"family": "Araujo",
"given": "Draulio B"
},
{
"family": "Sporns",
"given": "Olaf"
},
{
"family": "Siegel",
"given": "Joshua"
},
{
"family": "Dosenbach",
"given": "Nico"
},
{
"family": "Nutt",
"given": "David J"
},
{
"family": "Carhart-Harris",
"given": "Robin L"
},
{
"family": "Stamatakis",
"given": "Emmanuel A"
},
{
"family": "Bzdok",
"given": "Danilo"
}
],
"container-title-short":
"volume": "32",
"issue": "4",
"page": "1543-1554",
"DOI": "10.1038/
"PMID": "41942645",
"PMCID": "PMC13099416",
"ISSN": "1078-8956",
"publisher": "Nature Portfolio",
"URL": "https://
"language": "en",
"issued": {
"date-parts": [
[
2026,
4,
6
]
]
}
}
The tracing map gets a citation of its own once an author has validated it and it has a DOI.
Similar papers
The papers with a page that share the most with this one: the tools found in their code, their categories, datasets, cited references and authors, the rarest counting most.
- [1] doi:10.1038/s41586-026-10910-z [code]
- Psychedelics align brain activity with context.Journal: NatureIn common: NiBabel, SciPy, NumPy, fMRI, 16 references, 2 authors
- [2] doi:10.1038/s41467-026-74215-5 [code]
- Multi-metric evaluations of acute psychedelic effects on fMRI brain entropy.Journal: Nature communicationsIn common: scikit-learn, SciPy, Matplotlib, 1 other tool, fMRI, 15 references, author Drummond E-Wen McCulloch
- [3] doi:10.1002/hbm.70596 [code]
- Modeled Long-Term Effects of Psilocybin on Dynamic Activity and Effective Connectivity of Fronto-Striatal-Thalamic
Circuits. Journal: Human brain mappingIn common: systems, 10 references, author David J Nutt - [4] doi:10.1162/imag.a.1262 [code]
- Frame-wise multi-echo distortion correction for superior functional MRI.Journal: Imaging neuroscience (Cambridge, Mass.)In common: Nilearn, NiBabel, seaborn, 4 other tools, fMRI, 4 references, author Nico UF Dosenbach
- [5] doi:10.1371/journal.pbio.3003684 [code]
- The retrieval of previously learned motor memories is facilitated by the reinstatement of default mode network manifold structures.Journal: PLoS biologyIn common: Nilearn, NiBabel, seaborn, 5 other tools, fMRI, 7 references
- [6] doi:10.1162/netn.a.547 [code]
- An evaluation of the efficacy of single-echo and multi-echo fMRI denoising strategies.Journal: Network neuroscience (Cambridge, Mass.)In common: Nilearn, NiBabel, seaborn, 5 other tools, fMRI, 6 references
- [7] doi:10.1038/s41467-026-75959-w [code]
- Charting higher-order models of brain function beyond pairwise interactions.Journal: Nature communicationsIn common: Nilearn, NiBabel, seaborn, 5 other tools, 6 references
- [8] doi:10.1162/imag.a.1269 [code]
- From early to contemporary normative modeling: Mapping individual differences in neurophysiological signals.Journal: Imaging neuroscience (Cambridge, Mass.)In common: ArviZ, PyMC, Nilearn, 7 other tools, 1 reference
- [9] doi:10.1038/s41467-026-72940-5 [code]
- Cerebellar growth is associated with domain-specific cerebral maturation and socio-linguistic behavior.Journal: Nature communicationsIn common: ArviZ, PyMC, NiBabel, 6 other tools, 2 references
- [10] doi:10.21203/rs.3.rs-9326213/v1 [code]
- Multi-task fMRI outperforms resting-state fMRI for revealing task-invariant organization of the human brainJournal: Research Square (preprint)In common: Nilearn, NiBabel, seaborn, 5 other tools, fMRI, 5 references
Contribute
The authors of this paper can claim it, correct its record and validate its tracing map, and the maintainers of its code (its owner, or a public member of its organization) correct what it says of their repository; anyone signed in can ask for its removal. Every request goes to OSCR's own machine, which answers it; your account page follows them.
Sign in with ORCID to claim this paper as one of its authors, correct its record or validate its tracing map: when the paper's metadata lists your ORCID iD, you are recognized at once. Maintainers of its code: sign in with GitHub, then claim the repository on your account page.
Claim this paper
Correct its record
Say what each link of this record is, remove the ones that are not the paper's, add the ones that are missing. The correction becomes a new version of the record, in its Versions section.
Validate its tracing map
You validate the map as this page shows it: 1 repository of the authors' code, each at its verified commit and with its license, 1 script, and 2 matches between paragraphs and code (see the Code and Map sections). It then receives a DOI on Zenodo, with you (your ORCID iD) and OSCR as its creators; the code itself is not deposited.
The map's fingerprint: sha256:3f0f417565d92144…
Add the badge to its README
The badge links the code to this page. Copy one of these into the README of the paper's code: only you decide where it goes, and nothing is changed for you.
Markdown
[, paste the snippet at the top, then “Commit changes…” and, to review it first, “Create a new branch and start a pull request”. You open the pull request; OSCR asks for no permission.
Request its removal
To ask OSCR to remove this record, the copies of its authors' scripts or its tracing map, use the removal request page: signed in, you say who you are, what to remove and why, then review and confirm the request. Published rules decide every request (how).
Discussion, reproductions, activity
Discussion: questions and error reports about this paper and its code, from signed-in readers and its authors. It opens with sign-in.
Reproductions: reports from readers who ran the authors' code: what they reproduced, with which environment, commit and data. It opens with sign-in.
Activity: what happens around this paper: new versions of its record, its map's validation, discussions and reproductions. It opens with sign-in.
