def calc_pattern_corr_between_twins(meas_name='thickness'): """ Calculate spatial pattern correlation between each twin pair. """ import pandas as pd import nibabel as nib from commontool.io.io import CiftiReader from scipy.stats import pearsonr twins_id_file = pjoin(work_dir, 'twins_id_1080.csv') meas2file = { 'thickness': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.thickness_MSMAll.32k_fs_LR.dscalar.nii', 'myelin': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.MyelinMap_BC_MSMAll.32k_fs_LR.dscalar.nii', 'activ': pjoin(proj_dir, 'analysis/s2/activation.dscalar.nii') } hemis = ('lh', 'rh') hemi2stru = { 'lh': 'CIFTI_STRUCTURE_CORTEX_LEFT', 'rh': 'CIFTI_STRUCTURE_CORTEX_RIGHT' } mpm_file = pjoin( proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' 'MPM_v3_{hemi}_0.25.nii.gz') roi2label = {'IOG-face': 1, 'pFus-face': 2, 'mFus-face': 3} zyg2label = {'MZ': 1, 'DZ': 2} out_file = pjoin(work_dir, f'twins_pattern-corr_{meas_name}.csv') df = pd.read_csv(twins_id_file) meas_reader = CiftiReader(meas2file[meas_name]) meas_ids = [int(name.split('_')[0]) for name in meas_reader.map_names()] twin1_indices = [meas_ids.index(i) for i in df['twin1']] twin2_indices = [meas_ids.index(i) for i in df['twin2']] out_df = pd.DataFrame() out_df['zygosity'] = df['zygosity'] out_df['zyg'] = [zyg2label[zyg] for zyg in df['zygosity']] for hemi in hemis: meas1 = meas_reader.get_data(hemi2stru[hemi], True)[twin1_indices] meas2 = meas_reader.get_data(hemi2stru[hemi], True)[twin2_indices] mpm = nib.load(mpm_file.format(hemi=hemi)).get_data().squeeze() for roi, lbl in roi2label.items(): idx_vec = mpm == lbl out_df[f"{hemi}_{roi.split('-')[0]}"] = [ pearsonr(i[idx_vec], j[idx_vec])[0] for i, j in zip(meas1, meas2) ] out_df.to_csv(out_file, index=False)
def cifti_io(): reader1 = CiftiReader(r'E:\useful_things\data\HCP\HCP_S1200_GroupAvg_v1\HCP_S1200_GroupAvg_v1' r'\HCP_S1200_997_tfMRI_ALLTASKS_level2_cohensd_hp200_s4_MSMAll.dscalar.nii') save2cifti(r'E:\tmp\HCP_S1200_997_tfMRI_FACE-AVG_level2_cohensd_hp200_s4_MSMAll.dscalar.nii', np.atleast_2d(reader1.get_data()[19]), reader1.brain_models(), reader1.map_names([19]), reader1.volume, reader1.label_tables([19])) reader2 = CiftiReader(r'E:\tmp\HCP_S1200_997_tfMRI_FACE-AVG_level2_cohensd_hp200_s4_MSMAll.dscalar.nii') data1 = reader1.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', zeroize=True)[19] print(data1) data2 = reader2.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', zeroize=True)[0] print(data2) print(np.max(data1 - data2), np.min(data1 - data2))
def calc_meas_individual(gid=1, hemi='lh', morph='thickness'): """ Calculate morphology using individual ROIs. """ import nibabel as nib import numpy as np import pickle as pkl from cxy_hcp_ffa.lib.predefine import hemi2stru, roi2label from commontool.io.io import CiftiReader morph2file = { 'thickness': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.thickness_MSMAll.32k_fs_LR.dscalar.nii', 'myelin': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.MyelinMap_BC_MSMAll.32k_fs_LR.dscalar.nii' } meas_file = morph2file[morph] gid_file = pjoin(proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' f'grouping/group_id_{hemi}.npy') roi_file = pjoin(proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' f'rois_v3_{hemi}.nii.gz') subj_file = pjoin(proj_dir, 'analysis/s2/subject_id') trg_file = pjoin(work_dir, f'individual_G{gid}_{morph}_{hemi}.pkl') gid_idx_vec = np.load(gid_file) == gid subj_ids = open(subj_file).read().splitlines() subj_ids = [subj_ids[i] for i in np.where(gid_idx_vec)[0]] n_subj = len(subj_ids) roi_maps = nib.load(roi_file).get_data().squeeze().T[gid_idx_vec] meas_reader = CiftiReader(meas_file) meas_ids = [name.split('_')[0] for name in meas_reader.map_names()] meas_indices = [meas_ids.index(i) for i in subj_ids] meas = meas_reader.get_data(hemi2stru[hemi], True)[meas_indices] out_dict = {'shape': 'n_roi x n_subj', 'roi': list(roi2label.keys()), 'subject': subj_ids, 'meas': np.ones((len(roi2label), n_subj)) * np.nan} for roi_idx, roi in enumerate(out_dict['roi']): lbl_idx_arr = roi_maps == roi2label[roi] for subj_idx in range(n_subj): lbl_idx_vec = lbl_idx_arr[subj_idx] if np.any(lbl_idx_vec): out_dict['meas'][roi_idx, subj_idx] = np.mean( meas[subj_idx][lbl_idx_vec]) pkl.dump(out_dict, open(trg_file, 'wb'))
reader = None for fpath in src_paths: if not os.path.exists(fpath): message = 'Path-{0} does not exist!\n'.format(fpath) print(message, end='') log_file.writelines(message) continue reader = CiftiReader(fpath) # If don't use .copy(), the merged_data will share the same data object with data in reader. # As a result, the memory space occupied by the whole data in reader will be reserved. # But we only need one row of the whole data, so we can use the .copy() to make the element # avoid being a reference to the row of the whole data. Then, the whole data in reader will # be regarded as a garbage and collected by Garbage Collection Program. merged_data.append(reader.get_data()[column - 1].copy()) map_names.append(reader.map_names()[column - 1]) print('Merged:', fpath) if reader is None: message = "Can't find any valid source path\n" print(message, end='') log_file.writelines(message) else: message = 'Start save2cifti\n' print(message, end='') log_file.writelines(message) save2cifti(out_path, np.array(merged_data), reader.brain_models(), map_names) log_file.write('done') log_file.close()
def different_activation_gender_roi2allsubgroup(b_dict): # file paths maps_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_WM_cope19_FACE-AVG_s2_MSMAll_32k_fs_LR.dscalar.nii' ) cluster_num_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters') roi_files = pjoin(cluster_num_dir, 'activation/{hemi}{label}_FFA.nii.gz') group_labels_file = pjoin(cluster_num_dir, 'group_labels') with open(group_labels_file) as f: group_labels = np.array(f.read().split(' '), dtype=np.uint16) cifti_reader = CiftiReader(maps_file) lmaps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True) rmaps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True) # get gender labels subjects = np.array(b_dict['Subject']) genders = np.array(b_dict['Gender']) subjects_m = subjects[genders == 'M'] subjects_f = subjects[genders == 'F'] map2subject = [name.split('_')[0] for name in cifti_reader.map_names()] gender_labels = np.zeros((len(map2subject), ), dtype=np.str) for idx, subj_id in enumerate(map2subject): if subj_id in subjects_m: gender_labels[idx] = 'M' elif subj_id in subjects_f: gender_labels[idx] = 'F' means_m = [] means_f = [] sems_m = [] sems_f = [] for roi in rois: roi_file = roi_files.format(roi[:-1]) roi_mask = nib.load(roi_file).get_data().ravel() roi_vertices = np.where(roi_mask == int(roi[-1]))[0] if roi[0] == 'l': roi_maps = lmaps[:, roi_vertices] elif roi[0] == 'r': roi_maps = rmaps[:, roi_vertices] else: raise RuntimeError("invalid roi name: {}".format(roi)) male_indices = np.logical_and(gender_labels == 'M', group_labels == roi[1]) female_indices = np.logical_and(gender_labels == 'F', group_labels == roi[1]) roi_map_means_m = np.mean(roi_maps[male_indices], 1) roi_map_means_f = np.mean(roi_maps[female_indices], 1) # print('the number of males about {0}: {1}'.format(roi, roi_map_means_m.shape[0])) # print('the number of females about {0}: {1}'.format(roi, roi_map_means_f.shape[0])) print('{0}_male vs. {0}_female: p={1}'.format( roi, ttest_ind(roi_map_means_m, roi_map_means_f)[1])) means_m.append(np.mean(roi_map_means_m)) means_f.append(np.mean(roi_map_means_f)) sems_m.append(sem(roi_map_means_m)) sems_f.append(sem(roi_map_means_f)) x = np.arange(len(rois)) fig, ax = plt.subplots() width = auto_bar_width(x, 2) rects1 = ax.bar(x, means_m, width, color='b', alpha=0.5, yerr=sems_m, ecolor='blue') rects2 = ax.bar(x + width, means_f, width, color='r', alpha=0.5, yerr=sems_f, ecolor='red') # show_bar_value(rects1, '.3f') # show_bar_value(rects2, '.3f') ax.legend((rects1, rects2), ('male', 'female')) ax.set_xticks(x + width / 2.0) ax.set_xticklabels(rois) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_ylabel('activation (z-stat)') plt.tight_layout() plt.show()
def different_activation_gender_1080(b_dict): # file paths maps_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_WM_cope19_FACE-AVG_s2_MSMAll_32k_fs_LR.dscalar.nii' ) lFFA_file = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/label/lFFA_25.label') rFFA_file = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/label/rFFA_25.label') # get maps cifti_reader = CiftiReader(maps_file) lFFA = nib.freesurfer.read_label(lFFA_file) rFFA = nib.freesurfer.read_label(rFFA_file) lFFA_maps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True)[:, lFFA] rFFA_maps = cifti_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True)[:, rFFA] # get subjects' ids subjects = np.array(b_dict['Subject']) genders = np.array(b_dict['Gender']) subjects_m = subjects[genders == 'M'] subjects_f = subjects[genders == 'F'] map2subject = [name.split('_')[0] for name in cifti_reader.map_names()] gender_labels = np.zeros((lFFA_maps.shape[0], ), dtype=np.str) for idx, subj_id in enumerate(map2subject): if subj_id in subjects_m: gender_labels[idx] = 'M' elif subj_id in subjects_f: gender_labels[idx] = 'F' lFFA_maps_mean = np.mean(lFFA_maps, 1) rFFA_maps_mean = np.mean(rFFA_maps, 1) lFFA_maps_mean_m = np.mean(lFFA_maps[gender_labels == 'M'], 1) lFFA_maps_mean_f = np.mean(lFFA_maps[gender_labels == 'F'], 1) rFFA_maps_mean_m = np.mean(rFFA_maps[gender_labels == 'M'], 1) rFFA_maps_mean_f = np.mean(rFFA_maps[gender_labels == 'F'], 1) print('lFFA vs. rFFA: p={}'.format( ttest_ind(lFFA_maps_mean, rFFA_maps_mean)[1])) print('lFFA_male vs. lFFA_female: p={}'.format( ttest_ind(lFFA_maps_mean_m, lFFA_maps_mean_f)[1])) print('rFFA_male vs. rFFA_female: p={}'.format( ttest_ind(rFFA_maps_mean_m, rFFA_maps_mean_f)[1])) print('lFFA_male vs. rFFA_male: p={}'.format( ttest_ind(lFFA_maps_mean_m, rFFA_maps_mean_m)[1])) print('lFFA_female vs. rFFA_female: p={}'.format( ttest_ind(lFFA_maps_mean_f, rFFA_maps_mean_f)[1])) l_mean = np.mean(lFFA_maps_mean) r_mean = np.mean(rFFA_maps_mean) l_sem = sem(lFFA_maps_mean) r_sem = sem(rFFA_maps_mean) l_m_mean = np.mean(lFFA_maps_mean_m) l_f_mean = np.mean(lFFA_maps_mean_f) r_m_mean = np.mean(rFFA_maps_mean_m) r_f_mean = np.mean(rFFA_maps_mean_f) l_m_sem = sem(lFFA_maps_mean_m) l_f_sem = sem(lFFA_maps_mean_f) r_m_sem = sem(rFFA_maps_mean_m) r_f_sem = sem(rFFA_maps_mean_f) x = np.arange(2) fig, ax = plt.subplots() width = auto_bar_width(x, 3) rects1 = ax.bar(x, [l_mean, r_mean], width, color='g', alpha=0.5, yerr=[l_sem, r_sem], ecolor='green') rects2 = ax.bar(x - width, [l_m_mean, r_m_mean], width, color='b', alpha=0.5, yerr=[l_m_sem, r_m_sem], ecolor='blue') rects3 = ax.bar(x + width, [l_f_mean, r_f_mean], width, color='r', alpha=0.5, yerr=[l_f_sem, r_f_sem], ecolor='red') # show_bar_value(rects1, '.3f') # show_bar_value(rects2, '.3f') # show_bar_value(rects3, '.3f') ax.legend((rects1, rects2, rects3), ('both', 'male', 'female')) ax.set_xticks(x) ax.set_xticklabels(['lFFA_2mm', 'rFFA_2mm']) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_ylabel('activation (z-stat)') plt.tight_layout() plt.show()
if __name__ == '__main__': from os.path import join as pjoin from commontool.io.io import CiftiReader, save2cifti # predefine some variates project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' reader = CiftiReader( '/nfs/p1/public_dataset/datasets/hcp/DATA/HCP_S1200_GroupAvg_v1/' 'HCP_S1200_GroupAvg_v1/S1200.All.MyelinMap_BC_MSMAll.32k_fs_LR.dscalar.nii' ) data = reader.get_data() names = reader.map_names() subj_id_file = pjoin(project_dir, 'data/HCP_face-avg/s2/subject_id') with open(subj_id_file) as rf: subj_ids = rf.read().splitlines() names_1080 = [_ + '_MyelinMap' for _ in subj_ids] indices = [] for name in names_1080: if name in names: indices.append(names.index(name)) data_1080 = data[indices] # output save2cifti( pjoin( project_dir, 'data/HCP_face-avg/S1200_1080_MyelinMap_BC_MSMAll_32k_fs_LR.dscalar.nii'
FFA_maps = maps[:, FFA_vertices] # lFFA_maps = lmaps[:, lFFA_vertices] # rFFA_maps = rmaps[:, rFFA_vertices] # FFA_maps = np.c_[lFFA_maps, rFFA_maps] FFA_patterns = map2pattern(FFA_maps, clustering_thr, clustering_bin, clustering_zscore) # lFFA_patterns = map2pattern(lFFA_maps, clustering_thr, clustering_bin, clustering_zscore) # rFFA_patterns = map2pattern(rFFA_maps, clustering_thr, clustering_bin, clustering_zscore) # FFA_patterns = np.c_[lFFA_patterns, rFFA_patterns] # show FFA_patterns imshow(FFA_patterns, 'vertices', 'subjects', 'jet', 'activation') # prepare subject ids subject_ids = [name.split('_')[0] for name in maps_reader.map_names()] subject_ids = np.array(subject_ids) print('Finish: prepare data') # ----------------------- # structure graph # ----------------------- if is_graph_needed: import networkx as nx from commontool.algorithm.graph import array2adjacent_matrix print('Start: structure graph') # create adjacent matrix n_subjects = FFA_patterns.shape[0] edges = [(i, j) for i in range(n_subjects)
rFFA_label = pjoin(project_dir, 'data/HCP_face-avg/label/rFFA_2mm.label') FSR_maps = pjoin( project_dir, 'data/HCP_face-avg/s2/S1200.1080.FACE-AVG_level2_zstat_hp200_s2_MSMAll.dscalar.nii' ) group_labels_path = pjoin(n_clusters_dir, 'group_labels') # ----------------------- # get data rFFA_vertices = nib.freesurfer.read_label(rFFA_label) reader = CiftiReader(FSR_maps) data = reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True) rFFA_data = data[:, rFFA_vertices] with open(group_labels_path) as f: group_labels = np.array(f.read().split(' ')) subjects_id = [name.split('_')[0] for name in reader.map_names()] subjects_id = np.array(subjects_id) # analyze labels # -------------- stats_table_titles = [ 'regroup_id', '#subjects', 'map_min', 'map_max', 'map_mean', 'rFFA_min', 'rFFA_max', 'rFFA_mean', 'subgroups' ] stats_table_content = dict() for title in stats_table_titles: # initialize statistics table content stats_table_content[title] = [] mean_maps = np.zeros((0, data.shape[1])) prob_maps = np.zeros((0, data.shape[1]))