def calc_roi_mean_allsubgroup(src_file, roi_files, group_labels, trg_file): """ Calculate ROI means for each subject of each subgroup in corresponding hemisphere. For example, subgourp1's right ROI1 will be used to calculate ROI means not only for subgroup1's subjects, but also for other subgroups' subjects. """ reader = CiftiReader(src_file) labels = np.unique(group_labels) roi_mean_rows = [] for hemi in hemis: maps = reader.get_data(brain_structure[hemi], True) for label1 in labels: sub_maps = np.atleast_2d(maps[group_labels == label1]) for label2 in labels: roi_file = roi_files.format(hemi=hemi[0], label=label2) roi_mask = nib.load(roi_file).get_data().ravel() roi_labels = np.unique(roi_mask) for roi_label in roi_labels: if roi_label == 0: continue roi_vertices = np.where(roi_mask == roi_label)[0] roi_name = roi_names.format(hemi=hemi[0], label=label2, roi_label=int(roi_label)) roi_name += '_in_subgroup{}'.format(label1) roi_means = np.mean(sub_maps[:, roi_vertices], 1) roi_mean_row = [roi_name] roi_mean_row.extend([str(_) for _ in roi_means]) roi_mean_rows.append(','.join(roi_mean_row)) open(trg_file, 'w+').writelines('\n'.join(roi_mean_rows))
def gender_diff_roi_mean_allsubgroup(data_file, gender_file, roi_files, group_labels, trg_file): cifti_reader = CiftiReader(data_file) gender_labels = np.array(open(gender_file).read().split(' ')) group_labels_uniq = np.unique(group_labels) rows = [] for hemi in hemis: data = cifti_reader.get_data(brain_structure[hemi], True) for group_label1 in group_labels_uniq: sub_data_m = np.atleast_2d(data[np.logical_and(gender_labels == 'M', group_labels == group_label1)]) sub_data_f = np.atleast_2d(data[np.logical_and(gender_labels == 'F', group_labels == group_label1)]) for group_label2 in group_labels_uniq: roi_file = roi_files.format(hemi=hemi[0], label=group_label2) roi_mask = nib.load(roi_file).get_data().ravel() roi_labels = np.unique(roi_mask) for roi_label in roi_labels: if roi_label == 0: continue roi_vertices = np.where(roi_mask == roi_label)[0] roi_means_m = np.mean(sub_data_m[:, roi_vertices], 1) roi_means_f = np.mean(sub_data_f[:, roi_vertices], 1) roi_name = roi_names.format(hemi=hemi[0], label=group_label2, roi_label=int(roi_label)) item_name_m = roi_name + '_in_group{}_male'.format(group_label1) item_name_f = roi_name + '_in_group{}_female'.format(group_label1) row_m = [item_name_m] row_f = [item_name_f] row_m.extend([str(_) for _ in roi_means_m]) row_f.extend([str(_) for _ in roi_means_f]) rows.append(','.join(row_m)) rows.append(','.join(row_f)) open(trg_file, 'w+').writelines('\n'.join(rows))
def calc_meas_individual(hemi='lh'): import nibabel as nib import numpy as np import pickle as pkl from commontool.io.io import CiftiReader from cxy_hcp_ffa.lib.predefine import hemi2stru, roi2label # inputs work_dir = pjoin(proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/tfMRI') rois_file = pjoin(proj_dir, 'analysis/s2/1080_fROI/' f'refined_with_Kevin/rois_v3_{hemi}.nii.gz') meas_file = pjoin(proj_dir, 'analysis/s2/activation.dscalar.nii') # outputs out_file = pjoin(work_dir, f'individual_activ_{hemi}.pkl') rois = nib.load(rois_file).get_data().squeeze().T n_roi = len(roi2label) meas_reader = CiftiReader(meas_file) meas = meas_reader.get_data(hemi2stru[hemi], True) n_subj = meas.shape[0] roi_meas = {'shape': 'n_roi x n_subj', 'roi': list(roi2label.keys()), 'meas': np.ones((n_roi, n_subj)) * np.nan} for roi_idx, roi in enumerate(roi_meas['roi']): lbl_idx_arr = rois == roi2label[roi] for subj_idx in range(n_subj): lbl_idx_vec = lbl_idx_arr[subj_idx] if np.any(lbl_idx_vec): roi_meas['meas'][roi_idx, subj_idx] = np.mean( meas[subj_idx][lbl_idx_vec]) pkl.dump(roi_meas, open(out_file, 'wb'))
def calc_roi_mean_intrasubgroup(src_file, roi_files, group_labels, trg_file): """ Calculate ROI means for each subject in corresponding subgroup. """ reader = CiftiReader(src_file) labels = np.unique(group_labels) roi_mean_rows = [] for hemi in hemis: maps = reader.get_data(brain_structure[hemi], True) for label in labels: sub_maps = np.atleast_2d(maps[group_labels == label]) roi_file = roi_files.format(hemi=hemi[0], label=label) roi_mask = nib.load(roi_file).get_data().ravel() roi_labels = np.unique(roi_mask) for roi_label in roi_labels: if roi_label == 0: continue roi_vertices = np.where(roi_mask == roi_label)[0] roi_name = roi_names.format(hemi=hemi[0], label=label, roi_label=int(roi_label)) roi_means = np.mean(sub_maps[:, roi_vertices], 1) roi_mean_row = [roi_name] roi_mean_row.extend([str(_) for _ in roi_means]) roi_mean_rows.append(','.join(roi_mean_row)) open(trg_file, 'w+').writelines('\n'.join(roi_mean_rows))
def test_merge_nifti2cifti(): import nibabel as nib # get data data_l = nib.load(r'E:\tmp\l_vcAtlas_refine2.nii.gz').get_data().ravel() data_r = nib.load(r'E:\tmp\r_vcAtlas_refine2.nii.gz').get_data().ravel() cifti_reader = CiftiReader( r'E:\useful_things\data\HCP\HCP_S1200_GroupAvg_v1' r'\HCP_S1200_997_tfMRI_FACE-AVG_level2_cohensd_hp200_s4_MSMAll.dscalar.nii' ) # get valid data bm_lr = cifti_reader.brain_models( ['CIFTI_STRUCTURE_CORTEX_LEFT', 'CIFTI_STRUCTURE_CORTEX_RIGHT']) idx2vtx_l = list(bm_lr[0].vertex_indices) idx2vtx_r = list(bm_lr[1].vertex_indices) cifti_data_l = data_l[idx2vtx_l] cifti_data_r = data_r[idx2vtx_r] # get edge_list cifti_data = np.c_[np.atleast_2d(cifti_data_l), np.atleast_2d(cifti_data_r)] save2cifti(r'E:\tmp\vcAtlas_refine.dscalar.nii', cifti_data, bm_lr, ['surface vcAtlas'])
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', 'va': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.midthickness_MSMAll_va.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') meas_id_file = pjoin(proj_dir, 'data/HCP/subject_id_1096') 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 = open(meas_id_file).read().splitlines() 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): if morph == 'va': out_dict['meas'][roi_idx, subj_idx] = np.sum( meas[subj_idx][lbl_idx_vec]) else: out_dict['meas'][roi_idx, subj_idx] = np.mean( meas[subj_idx][lbl_idx_vec]) pkl.dump(out_dict, open(trg_file, 'wb'))
def extract_run_series(src_file, hemis, brain_structures, trg_regions_lr): region_series_list = [] reader = CiftiReader(src_file) for hemi in hemis: series = reader.get_data(brain_structures[hemi], True) for trg_region in trg_regions_lr[hemi]: region_series_list.append(np.nanmean(series[:, trg_region], 1)) run_series = np.array(region_series_list) return run_series
def calc_morphology_individual(hemi='lh', morph='thickness'): import nibabel as nib import numpy as np import pickle as pkl from commontool.io.io import CiftiReader from cxy_hcp_ffa.lib.predefine import hemi2stru, roi2label rois_file = pjoin( proj_dir, 'analysis/s2/1080_fROI/' f'refined_with_Kevin/rois_v3_{hemi}.nii.gz') subj_file = pjoin(proj_dir, 'analysis/s2/subject_id') meas_id_file = pjoin(proj_dir, 'data/HCP/subject_id_1096') morph2meas = { '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', 'va': '/nfs/p1/public_dataset/datasets/hcp/DATA/' 'HCP_S1200_GroupAvg_v1/HCP_S1200_GroupAvg_v1/' 'S1200.All.midthickness_MSMAll_va.32k_fs_LR.dscalar.nii' } meas_file = morph2meas[morph] trg_file = pjoin(work_dir, f'individual_{morph}_{hemi}.pkl') rois = nib.load(rois_file).get_data().squeeze().T n_roi = len(roi2label) subj_ids = open(subj_file).read().splitlines() n_subj = len(subj_ids) meas_reader = CiftiReader(meas_file) meas_ids = open(meas_id_file).read().splitlines() meas_indices = [meas_ids.index(i) for i in subj_ids] meas = meas_reader.get_data(hemi2stru[hemi], True)[meas_indices] roi_meas = { 'shape': 'n_roi x n_subj', 'roi': list(roi2label.keys()), 'meas': np.ones((n_roi, n_subj)) * np.nan } for roi_idx, roi in enumerate(roi_meas['roi']): lbl_idx_arr = rois == roi2label[roi] for subj_idx in range(n_subj): lbl_idx_vec = lbl_idx_arr[subj_idx] if np.any(lbl_idx_vec): if morph == 'va': tmp = np.sum(meas[subj_idx][lbl_idx_vec]) else: tmp = np.mean(meas[subj_idx][lbl_idx_vec]) roi_meas['meas'][roi_idx, subj_idx] = tmp pkl.dump(roi_meas, open(trg_file, 'wb'))
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 calc_meas(gid=1, hemi='lh'): """ 用一半被试的MPM去提取另一半被试的激活值 如果某个被试没有某个ROI,就不提取该被试该ROI的信号 Args: gid (int, optional): group ID. Defaults to '1'. hemi (str, optional): hemisphere. Defaults to 'lh'. """ import numpy as np import pickle as pkl import nibabel as nib from cxy_hcp_ffa.lib.predefine import hemi2stru, roi2label from commontool.io.io import CiftiReader gh_ids = (gid * 10 + 1, gid * 10 + 2) gh_id_file = pjoin(split_dir, f'half_id_{hemi}.npy') mpm_file = pjoin(split_dir, 'MPM_GH{gh_id}_{hemi}.nii.gz') 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') src_file = pjoin(proj_dir, 'analysis/s2/activation.dscalar.nii') trg_file = pjoin(work_dir, f'G{gid}_activ_{hemi}.pkl') gh_id_vec = np.load(gh_id_file) gid_idx_vec = np.logical_or(gh_id_vec == gh_ids[0], gh_id_vec == gh_ids[1]) hid_vec = gh_id_vec[gid_idx_vec] 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(src_file) meas = meas_reader.get_data(hemi2stru[hemi], True)[gid_idx_vec] 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 gh_id in gh_ids: hid_idx_vec = hid_vec == gh_id mpm = nib.load(mpm_file.format(gh_id=gh_id, hemi=hemi)).get_data().squeeze() for roi_idx, roi in enumerate(out_dict['roi']): roi_idx_vec = np.any(roi_maps == roi2label[roi], 1) valid_idx_vec = np.logical_and(~hid_idx_vec, roi_idx_vec) mpm_idx_vec = mpm == roi2label[roi] meas_masked = meas[valid_idx_vec][:, mpm_idx_vec] out_dict['meas'][roi_idx][valid_idx_vec] = np.mean(meas_masked, 1) pkl.dump(out_dict, open(trg_file, 'wb'))
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 prepare_data(): from os.path import join as pjoin from commontool.io.io import CiftiReader, save2nifti hemi = 'lh' brain_structure = { 'lh': 'CIFTI_STRUCTURE_CORTEX_LEFT', 'rh': 'CIFTI_STRUCTURE_CORTEX_RIGHT' } proj_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_pattern' data_dir = pjoin(proj_dir, 'data/HCP') trg_dir = pjoin(proj_dir, f'analysis/s2_rh') trg_file = pjoin(trg_dir, f'curvature_{hemi}.nii.gz') subj_id_file = pjoin(trg_dir, 'subject_id') subj_id_all_file = pjoin(data_dir, 'structure/subject_id_curv') data_file = pjoin( data_dir, 'structure/S1200.All.curvature_MSMAll.32k_fs_LR.dscalar.nii') subj_ids = open(subj_id_file).read().splitlines() subj_ids_all = open(subj_id_all_file).read().splitlines() data = CiftiReader(data_file).get_data(brain_structure[hemi], True) indices = [] for i in subj_ids: indices.append(subj_ids_all.index(i)) data_new = data[indices] data_new = data_new.T[:, None, None, :] save2nifti(trg_file, data_new)
def save_mean_maps(): import os import numpy as np from os.path import join as pjoin from commontool.io.io import CiftiReader, save2nifti # predefine parameters cluster_nums = [2] hemi = 'lh' structure_name = 'curv' project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' src_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_curvature_MSMAll_32k_fs_LR.dscalar.nii') cluster_num_dirs = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/{}clusters') brain_structure = hemi2structure[hemi] # prepare data reader = CiftiReader(src_file) maps = reader.get_data(brain_structure, True) for cluster_num in cluster_nums: # get clustering labels of subjects cluster_num_dir = cluster_num_dirs.format(cluster_num) group_labels_path = pjoin(cluster_num_dir, 'group_labels') with open(group_labels_path) as rf: group_labels = np.array(rf.read().split(' '), dtype=np.uint16) mean_maps = np.zeros((0, maps.shape[1])) for label in sorted(set(group_labels)): subgroup_maps = maps[group_labels == label] subgroup_maps_mean = np.atleast_2d(np.mean(subgroup_maps, 0)) mean_maps = np.r_[mean_maps, subgroup_maps_mean] # output out_dir = pjoin(cluster_num_dir, 'structure') if not os.path.exists(out_dir): os.makedirs(out_dir) save2nifti( pjoin(out_dir, '{}_{}_mean_maps.nii.gz'.format(hemi, structure_name)), mean_maps) print('{}_{}clusters: done'.format(structure_name, cluster_num))
def calc_meas_MPM(hemi='lh'): """ 用一半被试的MPM去提取另一半被试的激活值 如果某个被试没有某个ROI,就不提取该被试该ROI的信号 Args: hemi (str, optional): hemisphere. Defaults to 'lh'. """ import numpy as np import pickle as pkl import nibabel as nib from cxy_hcp_ffa.lib.predefine import hemi2stru, roi2label from commontool.io.io import CiftiReader hids = (1, 2) hid_file = pjoin(split_dir, 'half_id.npy') mpm_file = pjoin(split_dir, 'MPM_half{hid}_{hemi}.nii.gz') roi_file = pjoin(proj_dir, 'analysis/s2/1080_fROI/refined_with_Kevin/' f'rois_v3_{hemi}.nii.gz') src_file = pjoin(proj_dir, 'analysis/s2/activation.dscalar.nii') trg_file = pjoin(work_dir, f'activ_{hemi}.pkl') hid_vec = np.load(hid_file) n_subj = len(hid_vec) roi_maps = nib.load(roi_file).get_data().squeeze().T n_roi = len(roi2label) meas_reader = CiftiReader(src_file) meas = meas_reader.get_data(hemi2stru[hemi], True) out_dict = {'shape': 'n_roi x n_subj', 'roi': list(roi2label.keys()), 'meas': np.ones((n_roi, n_subj)) * np.nan} for hid in hids: hid_idx_vec = hid_vec == hid mpm = nib.load(mpm_file.format(hid=hid, hemi=hemi) ).get_data().squeeze() for roi_idx, roi in enumerate(out_dict['roi']): roi_idx_vec = np.any(roi_maps == roi2label[roi], 1) valid_idx_vec = np.logical_and(~hid_idx_vec, roi_idx_vec) mpm_idx_vec = mpm == roi2label[roi] meas_masked = meas[valid_idx_vec][:, mpm_idx_vec] out_dict['meas'][roi_idx][valid_idx_vec] = np.mean(meas_masked, 1) pkl.dump(out_dict, open(trg_file, 'wb'))
def get_roi_pattern(): import os import numpy as np import nibabel as nib from os.path import join as pjoin from FFA_pattern.tool import get_roi_pattern from commontool.io.io import CiftiReader print('Start: predefine some variates') # ----------------------- # predefine parameters hemi = 'lh' brain_structure = { 'lh': 'CIFTI_STRUCTURE_CORTEX_LEFT', 'rh': 'CIFTI_STRUCTURE_CORTEX_RIGHT' } zscore = True # If true, do z-score on each subject's ROI pattern thr = None # a threshold used to cut FFA_data before clustering (default: None) bin = False # If true, binarize FFA_data according to clustering_thr size_min = 0 # only work with threshold # predefine paths proj_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_pattern' trg_dir = pjoin(proj_dir, f'analysis/s2/lh/zscore') if not os.path.isdir(trg_dir): os.makedirs(trg_dir) roi_file = pjoin(proj_dir, f'data/HCP/label/MMPprob_OFA_FFA_thr1_{hemi}.label') activ_file = pjoin(proj_dir, f'analysis/s2/activation.dscalar.nii') # geo_files = '/nfs/p1/public_dataset/datasets/hcp/DATA/HCP_S1200_GroupAvg_v1/' \ # 'HCP_S1200_GroupAvg_v1/S1200.{}.white_MSMAll.32k_fs_LR.surf.gii' geo_files = None # mask_file = pjoin(proj_dir, f'data/HCP_face-avg/s2/patches_15/crg2.3/{hemi[0]}FFA_patch_maps_lt15.nii.gz') mask_file = None # ----------------------- print('Finish: predefine some variates') if mask_file is not None: mask = nib.load(mask_file).get_data().squeeze().T != 0 else: mask = None if geo_files is None: faces = None else: raise NotImplementedError # activ = nib.load(activ_file).get_data().squeeze().T activ = CiftiReader(activ_file).get_data(brain_structure[hemi], True) roi = nib.freesurfer.read_label(roi_file) roi_patterns = get_roi_pattern(activ, roi, zscore, thr, bin, size_min, faces, mask) np.save(pjoin(trg_dir, f'roi_patterns.npy'), roi_patterns)
def PA_plot(src_file, brain_structures, PAs, masks, group_labels, axes, color, item, zscore, item_ys_dict, item_mask_values_dict): item_ys_dict[item] = np.zeros_like(axes, np.object) item_mask_values_dict[item] = np.zeros_like(axes, np.object) reader = CiftiReader(src_file) for col in range(2): PA = PAs[col] mask = masks[col] x = np.arange(len(PA)) data = reader.get_data(brain_structures[col], True) PA_maps = data[:, PA] mask_maps = data[:, mask] # if zscore: # PA_maps = stats.zscore(PA_maps, 1) # mask_maps = stats.zscore(mask_maps, 1) for row, label in enumerate(sorted(set(group_labels))): subgroup_PA_maps = np.atleast_2d(PA_maps[group_labels == label]) subgroup_mask_maps = np.atleast_2d( mask_maps[group_labels == label]) y = np.mean(subgroup_PA_maps, 0) mask_value = np.mean(subgroup_mask_maps, 0) if zscore: y = stats.zscore(y) mask_value = stats.zscore(mask_value) axes[row, col].plot(x, y, color=color, label=item) else: sem = stats.sem(subgroup_PA_maps, 0) axes[row, col].errorbar(x, y, yerr=sem, color=color, label=item) axes[row, col].set_title('group' + str(label)) item_ys_dict[item][row, col] = y item_mask_values_dict[item][row, col] = mask_value
def calc_connect(series_files, subject, hemis, brain_structures, group_labels_uniq, roi_files, label1, count, sub_subject_num): # check if the file exists series_LR_file = series_files.format(subject=subject, phase='LR') series_RL_file = series_files.format(subject=subject, phase='RL') pass_subject = False log_list = [] if not os.path.exists(series_LR_file): pass_subject = True log_list.append('Path-{} does not exist!'.format(series_LR_file)) if not os.path.exists(series_RL_file): pass_subject = True log_list.append('Path-{} dose not exist!'.format(series_RL_file)) if pass_subject: return log_list result = dict() reader_LR = CiftiReader(series_LR_file) reader_RL = CiftiReader(series_RL_file) for hemi1 in hemis: series_LR, map_shape_LR, idx2vtx_LR = reader_LR.get_data( brain_structures[hemi1]) series_RL, map_shape_RL, idx2vtx_RL = reader_RL.get_data( brain_structures[hemi1]) assert map_shape_LR == map_shape_RL and idx2vtx_LR == idx2vtx_RL series_LR = stats.zscore(series_LR, 0) series_RL = stats.zscore(series_RL, 0) series = np.r_[series_LR, series_RL] vtx2idx = np.ones(map_shape_LR, dtype=np.uint16) * -1 vtx2idx[idx2vtx_LR] = np.arange(len(idx2vtx_LR)) for label2 in group_labels_uniq: for hemi2 in hemis: roi_file = roi_files.format(hemi2=hemi2[0], label2=label2) roi_labels_arr = nib.load(roi_file).get_data().ravel() roi_labels_uniq = np.unique(roi_labels_arr).astype(np.uint8) for roi_label in roi_labels_uniq: if roi_label == 0: continue seed_vertices = np.where(roi_labels_arr == roi_label)[0] seed_series = np.mean(series[:, vtx2idx[seed_vertices]], 1) connections = np.ones_like(vtx2idx, np.float64) * math.nan for idx in range(series.shape[1]): trg_series = series[:, idx] connections[idx2vtx_LR[idx]] = stats.pearsonr( seed_series, trg_series)[0] result[(hemi2[0], label2, roi_label, hemi1[0], label1)] = connections result[('subject', label1)] = subject print('group{}_subject{}/{}_{}{}_FFA{}_connect_{}'.format( label1, count, sub_subject_num, hemi2[0], label2, roi_label, hemi1[0])) return result
def get_valid_id(sess=1, run='LR'): import os import time from commontool.io.io import CiftiReader # parameters subj_id_file = pjoin(proj_dir, 'analysis/s2/subject_id') maps_files = '/nfs/m1/hcp/{0}/MNINonLinear/Results/rfMRI_REST{1}_{2}/'\ 'rfMRI_REST{1}_{2}_Atlas_MSMAll_hp2000_clean.dtseries.nii' # outputs log_file = pjoin(work_dir, f'get_valid_id_log_{sess}_{run}') trg_file = pjoin(work_dir, f'rfMRI_REST{sess}_{run}_id') subj_ids = open(subj_id_file).read().splitlines() n_subj = len(subj_ids) valid_ids = [] log_writer = open(log_file, 'w') for idx, subj_id in enumerate(subj_ids, 1): time1 = time.time() maps_file = maps_files.format(subj_id, sess, run) if not os.path.exists(maps_file): msg = f'{maps_file} is not exist.' print(msg) log_writer.write(f'{msg}\n') continue try: data = CiftiReader(maps_file).get_data() except OSError: msg = f'{maps_file} meets OSError.' print(msg) log_writer.write(f'{msg}\n') continue if data.shape[0] != 1200: msg = f'The number of time points in {maps_file} is not 1200.' print(msg) log_writer.write(f'{msg}\n') continue valid_ids.append(subj_id) print(f'Finished: {idx}/{n_subj}, cost: {time.time() - time1} seconds') log_writer.close() # save out with open(trg_file, 'w') as wf: wf.write('\n'.join(valid_ids))
def calc_subgroup_mean_representation(): import os import numpy as np import nibabel as nib import pickle as pkl from os.path import join as pjoin from scipy.spatial.distance import cdist from commontool.io.io import CiftiReader hemi = 'rh' brain_structure = { 'lh': 'CIFTI_STRUCTURE_CORTEX_LEFT', 'rh': 'CIFTI_STRUCTURE_CORTEX_RIGHT' } proj_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_pattern' clustering_dir = pjoin(proj_dir, 'analysis/s2_rh') n_cluster_dir = pjoin(clustering_dir, 'raw/HAC_ward_euclidean/100clusters') repre_dir = pjoin(n_cluster_dir, 'activation/representation') if not os.path.exists(repre_dir): os.makedirs(repre_dir) roi_file = pjoin( proj_dir, 'data/HCP/label/MMPprob_OFA_FFA_thr1_{}.label'.format(hemi)) activ_file = pjoin(clustering_dir, 'activation.dscalar.nii') group_labels_file = pjoin(n_cluster_dir, 'group_labels') roi = nib.freesurfer.read_label(roi_file) # activ = nib.load(activ_file).get_data().squeeze().T activ = CiftiReader(activ_file).get_data(brain_structure[hemi], True) group_labels = np.array(open(group_labels_file).read().split(' '), dtype=np.uint16) roi_activ = activ[:, roi] labels_uniq = np.unique(group_labels) representations = dict() for label in labels_uniq: sub_roi_activ = np.atleast_2d(roi_activ[group_labels == label]) sub_roi_mean = np.mean(sub_roi_activ, 0)[None, :] representations[str(label)] = 1 - cdist(sub_roi_mean, sub_roi_activ, 'correlation')[0] pkl.dump(representations, open(pjoin(repre_dir, f'representation_{hemi}.pkl'), 'wb'))
import os import numpy as np from os.path import join as pjoin from commontool.io.io import CiftiReader, save2cifti project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' curv_file = pjoin(project_dir, 'data/HCP_1080/S1200_1080_curvature_MSMAll_32k_fs_LR.dscalar.nii') aparc_file = pjoin(project_dir, 'data/HCP_1080/S1200_1080_aparc_a2009s_32k_fs_LR.dlabel.nii') cluster_num_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters') group_labels_file = pjoin(cluster_num_dir, 'group_labels') mfs_dir = pjoin(cluster_num_dir, 'mfs') if not os.path.exists(mfs_dir): os.makedirs(mfs_dir) curv_reader = CiftiReader(curv_file) aparc_reader = CiftiReader(aparc_file) sulc_mask = curv_reader.get_data() < 0 fusiform_mask = np.logical_or(aparc_reader.get_data() == 21, aparc_reader.get_data() == 96) with open(group_labels_file) as rf: group_labels = np.array(rf.read().split(' '), dtype=np.uint16) mfs_prob_maps = [] fusiform_prob_maps = [] map_names = [] for label in sorted(set(group_labels)): indices = group_labels == label subgroup_mfs_mask = np.logical_and(sulc_mask[indices], fusiform_mask[indices]) subgroup_mfs_prob = np.mean(subgroup_mfs_mask, 0) subgroup_fusiform_prob = np.mean(fusiform_mask[indices], 0)
with open(filler_file0) as f0: fillers_c0 = f0.read().splitlines() src_paths = get_strings_by_filling(string, [fillers_c0]) log_file = open(os.path.join(target_dir, 'cifti_merge_log'), 'w+') merged_data = [] map_names = [] 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='')
for hemi in hemis: subFFA_file = subFFA_files.format(hemi[0], group_label) subFFA_file_name = os.path.basename(subFFA_file) subFFA_name = subFFA_file_name.split('.')[0] subFFA_data = nib.load(subFFA_file).get_data().ravel() trg_regions_lr[hemi].append(np.where(subFFA_data != 0)[0]) label_names_lr[hemi].append(subFFA_name) subFFA_data_uniq = np.unique(subFFA_data).astype(np.uint8) for subFFA_label in subFFA_data_uniq: if subFFA_label != 0: trg_regions_lr[hemi].append( np.where(subFFA_data == subFFA_label)[0]) label_names_lr[hemi].append(subFFA_name + str(subFFA_label)) reader = CiftiReader(acti_maps_file) # label_names = label_names_lr[hemis[0]] + label_names_lr[hemis[1]] # open(pjoin(acti_analysis_dir, 'npy_column_name'), 'w+').write(','.join(label_names)) # ---activation intensity start--- # for group_label in group_labels_uniq: # indices = np.where(group_labels == group_label)[0] # roi_means_arr = np.zeros((len(indices), 0)) # for hemi in hemis: # acti_maps = reader.get_data(brain_structure[hemi], True) # sub_acti_maps = acti_maps[indices] # for region in trg_regions_lr[hemi]: # roi_mean = np.atleast_2d(np.nanmean(sub_acti_maps[:, region], 1)).T # roi_means_arr = np.c_[roi_means_arr, roi_mean] # np.save(pjoin(acti_analysis_dir, 'g{}_intensity.npy'.format(group_label)), roi_means_arr) # ---activation intensity end---
'data/S1200_1080_WM_cope{0}_{1}_s2_MSMAll_32k_fs_LR.dscalar.nii') group_labels_path = pjoin(n_clusters_dir, 'group_labels') # ----------------------- # get data with open(group_labels_path) as f: group_labels = np.array(f.read().split(' ')) # analyze labels # -------------- cope_dict = dict() for roi in rois: cope_dict[roi] = dict() for k, v in interested_copes.items(): cope_file = cope_files.format(k, v) cope_data = CiftiReader(cope_file).get_data(brain_structure, True) for roi in rois: roi_vertices = nib.freesurfer.read_label(roi_file.format(roi)) cope_data_roi = cope_data[:, roi_vertices] if roi2label: # get subgroup data sub_cope_data_roi = np.atleast_2d( cope_data_roi[group_labels == roi2label[roi]]) sub_cope_data_roi_mean = np.mean(sub_cope_data_roi, axis=1) cope_dict[roi][v] = sub_cope_data_roi_mean else: cope_data_roi_mean = np.mean(cope_data_roi, axis=1) cope_dict[roi][v] = cope_data_roi_mean print('Finished: {0}_{1}'.format(k, v)) # output copes
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()
from os.path import join as pjoin from commontool.io.io import CiftiReader, save2nifti project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' acti_file = pjoin( project_dir, 'data/HCP_face-avg/s2/S1200.1080.FACE-AVG_level2_zstat_hp200_s2_MSMAll.dscalar.nii' ) patch_dir = pjoin(project_dir, 'data/HCP_face-avg/s2/patches_15/crg') patch_file = pjoin(patch_dir, 'rFFA_patch_maps_lt15.nii.gz') max_maps_file = pjoin(patch_dir, 'rFFA_max_maps_lt15.nii.gz') prob_max_map_file = pjoin(patch_dir, 'rFFA_prob_max_map_lt15.nii.gz') brain_structure = 'CIFTI_STRUCTURE_CORTEX_RIGHT' acti_maps = CiftiReader(acti_file).get_data(brain_structure, True) patch_maps = nib.load(patch_file).get_data() max_maps = np.zeros_like(patch_maps) for row in range(acti_maps.shape[0]): labels = np.unique(patch_maps[row]) for label in labels: if label == 0: continue acti_map_tmp = acti_maps[row].copy() not_label_indices = np.logical_not(patch_maps[row] == label) acti_map_tmp[not_label_indices] = -np.inf max_idx = np.argmax(acti_map_tmp) max_maps[row, max_idx] = label prob_max_map = np.mean(max_maps > 0, 0) header = nib.Nifti2Header()
} # predefine paths project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' maps_file = pjoin( project_dir, 'data/HCP_1080/S1200_1080_WM_Mean_BOLD_Signal_MSMAll_32k_fs_LR.dscalar.nii' ) cluster_num_dir = pjoin(project_dir, 's2_25_zscore/HAC_ward_euclidean/2clusters') group_labels_file = pjoin(cluster_num_dir, 'group_labels') mean_bold_signal_dir = pjoin(cluster_num_dir, 'mean_bold_signal') if not os.path.exists(mean_bold_signal_dir): os.makedirs(mean_bold_signal_dir) # ----------------------- maps = CiftiReader(maps_file).get_data(brain_structure[hemi], True) group_labels = np.array(open(group_labels_file).read().split(' '), dtype=np.uint16) # ---mean_map start--- # save2mean_map(maps, group_labels, # pjoin(mean_bold_signal_dir, '{}_mean_maps.nii.gz'.format(hemi))) # ---mean_map end--- compare_dir = pjoin(mean_bold_signal_dir, 'compare') if not os.path.exists(compare_dir): os.makedirs(compare_dir) # ---compare start--- pair_labels = [1, 2] samples1 = maps[group_labels == pair_labels[0]].T samples2 = maps[group_labels == pair_labels[1]].T
if __name__ == '__main__': import numpy as np from os.path import join as pjoin from commontool.io.io import CiftiReader, save2cifti project_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_clustering' test_par = pjoin(project_dir, 'data/HCP/tseries_test_dir') series_LR_files = pjoin(test_par, '{subject}/tfMRI_WM_LR_Atlas_MSMAll.dtseries.nii') series_RL_files = pjoin(test_par, '{subject}/tfMRI_WM_RL_Atlas_MSMAll.dtseries.nii') mean_signal_maps_out = pjoin(test_par, 'tfMRI_WM_Mean_BOLD_Signal_MSMAll_new.dscalar.nii') subject_ids = open(pjoin(test_par, 'subject_id')).read().splitlines() mean_signal_maps = [] for subject in subject_ids: series_LR_file = series_LR_files.format(subject=subject) series_RL_file = series_RL_files.format(subject=subject) reader_LR = CiftiReader(series_LR_file) reader_RL = CiftiReader(series_RL_file) series_LR = reader_LR.get_data() series_RL = reader_RL.get_data() series = np.r_[series_LR, series_RL] mean_signal = np.mean(series, 0) mean_signal_maps.append(mean_signal) print('Finish:', subject) save2cifti(mean_signal_maps_out, np.array(mean_signal_maps), reader_RL.brain_models(), subject_ids)
maps_file = pjoin( project_dir, 'data/HCP_face-avg/s2/S1200.1080.FACE-AVG_level2_zstat_hp200_s2_MSMAll.dscalar.nii' ) subject_ids_file = pjoin(project_dir, 'data/HCP_face-avg/s2/subject_id') patch_dir = pjoin( project_dir, 'data/HCP_face-avg/s2/patches_15/{}{}'.format(method, threshold)) if not os.path.exists(patch_dir): os.makedirs(patch_dir) lFFA_vertices = nib.freesurfer.read_label(lFFA_label_file) lFFA_vertices = set(lFFA_vertices.astype(np.uint16)) rFFA_vertices = nib.freesurfer.read_label(rFFA_label_file) rFFA_vertices = set(rFFA_vertices.astype(np.uint16)) c_reader = CiftiReader(maps_file) lmaps = c_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True) rmaps = c_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True) with open(subject_ids_file) as rf: subject_ids = rf.read().splitlines() g_reader_l = GiftiReader( '/nfs/p1/public_dataset/datasets/hcp/DATA/HCP_S1200_GroupAvg_v1/' 'HCP_S1200_GroupAvg_v1/S1200.L.white_MSMAll.32k_fs_LR.surf.gii') g_reader_r = GiftiReader( '/nfs/p1/public_dataset/datasets/hcp/DATA/HCP_S1200_GroupAvg_v1/' 'HCP_S1200_GroupAvg_v1/S1200.R.white_MSMAll.32k_fs_LR.surf.gii') lFFA_patch_maps = np.zeros_like(lmaps) lFFA_patch_stats = [] for idx, lmap in enumerate(lmaps):
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'