def get_roi_idx_vec(): """ Get index vector with bool values for each ROI. The length of each index vector is matched with 1080 subjects. True value means the ROI is delineated in the corresponding subject. """ import numpy as np import pandas as pd import nibabel as nib from cxy_hcp_ffa.lib.predefine import roi2label # inputs hemis = ('lh', 'rh') roi_file = pjoin(work_dir, 'rois_v3_{hemi}.nii.gz') # outputs out_file = pjoin(work_dir, 'rois_v3_idx_vec.csv') out_dict = {} for hemi in hemis: roi_maps = nib.load(roi_file.format(hemi=hemi)).get_data().squeeze().T for roi, lbl in roi2label.items(): out_dict[f'{hemi}_{roi}'] = np.any(roi_maps == lbl, axis=1) out_df = pd.DataFrame(out_dict) out_df.to_csv(out_file, index=False)
def roi_stats(gh_id=11, hemi='lh'): import numpy as np import nibabel as nib import pickle as pkl from cxy_hcp_ffa.lib.predefine import roi2label from commontool.io.io import save2nifti gh_id_file = pjoin(split_dir, f'half_id_{hemi}.npy') roi_file = pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' f'rois_v3_{hemi}.nii.gz') info_trg_file = pjoin(split_dir, f'rois_info_GH{gh_id}_{hemi}.pkl') prob_trg_file = pjoin(split_dir, f'prob_maps_GH{gh_id}_{hemi}.nii.gz') gh_id_idx_vec = np.load(gh_id_file) == gh_id rois = nib.load(roi_file).get_data().squeeze().T[gh_id_idx_vec] # prepare rois information dict rois_info = dict() for roi in roi2label.keys(): rois_info[roi] = dict() prob_maps = [] for roi, label in roi2label.items(): # get indices of subjects which contain the roi indices = rois == label subj_indices = np.any(indices, 1) # calculate the number of the valid subjects n_subject = np.sum(subj_indices) rois_info[roi]['n_subject'] = n_subject # calculate roi sizes for each valid subject sizes = np.sum(indices[subj_indices], 1) rois_info[roi]['sizes'] = sizes # calculate roi probability map among valid subjects prob_map = np.mean(indices[subj_indices], 0) prob_maps.append(prob_map) prob_maps = np.array(prob_maps) # save out pkl.dump(rois_info, open(info_trg_file, 'wb')) save2nifti(prob_trg_file, prob_maps.T[:, None, None, :])
def calc_TM(proj_name='HCPD', meas_name='thickness', atlas_name='MPM1'): """ Calculate thickness or myelination """ import time import pandas as pd import nibabel as nib from cxy_hcp_ffa.lib.predefine import roi2label, hemi2stru from commontool.io.io import CiftiReader # inputs hemis = ('lh', 'rh') info_file = pjoin(work_dir, f'{proj_name}_SubjInfo.csv') label2roi = {} for k, v in roi2label.items(): label2roi[v] = k atlas2file = { 'MPM1': pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'MPM_v3_{hemi}_0.25.nii.gz') } atlas_file = atlas2file[atlas_name] proj2par = { 'HCPD': '/nfs/e1/HCPD/fmriresults01', 'HCPA': '/nfs/e1/HCPA/fmriresults01' } proj_par = proj2par[proj_name] meas2file = { 'myelin': pjoin( proj_par, '{sid}_V1_MR/MNINonLinear/fsaverage_LR32k/' '{sid}_V1_MR.MyelinMap_BC_MSMAll.32k_fs_LR.dscalar.nii'), 'thickness': pjoin( proj_par, '{sid}_V1_MR/MNINonLinear/fsaverage_LR32k/' '{sid}_V1_MR.thickness_MSMAll.32k_fs_LR.dscalar.nii') } meas_file = meas2file[meas_name] # outputs out_file = pjoin(work_dir, f'{proj_name}_{meas_name}_{atlas_name}.csv') # prepare df = pd.read_csv(info_file) n_subj = df.shape[0] hemi2atlas = {} hemi2atlas_labels = {} for hemi in hemis: atlas_map = nib.load( atlas_file.format(hemi=hemi)).get_fdata().squeeze() hemi2atlas[hemi] = atlas_map atlas_labels = np.unique(atlas_map) hemi2atlas_labels[hemi] = atlas_labels[atlas_labels != 0] # calculate for i, idx in enumerate(df.index, 1): time1 = time.time() sid = df.loc[idx, 'subID'] reader = CiftiReader(meas_file.format(sid=sid)) for hemi in hemis: meas_map = reader.get_data(hemi2stru[hemi], True)[0] atlas_map = hemi2atlas[hemi] atlas_labels = hemi2atlas_labels[hemi] for lbl in atlas_labels: roi = label2roi[lbl] col = f"{roi.split('-')[0]}_{hemi}" meas = np.mean(meas_map[atlas_map == lbl]) df.loc[idx, col] = meas print(f'Finished {i}/{n_subj}: cost {time.time() - time1}') # save df.to_csv(out_file, index=False)