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 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_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 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 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 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(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 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 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_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)
# 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--- # ---intra subgroup activation pattern similarity start--- for group_label in group_labels_uniq: pattern_similarities_list = [] for hemi in hemis: acti_maps = reader.get_data(brain_structure[hemi], True) sub_acti_maps = acti_maps[group_labels == group_label] subj_num = sub_acti_maps.shape[0] for region in trg_regions_lr[hemi]: sub_acti_maps_masked = sub_acti_maps[:, region] pattern_similarities = [ pearsonr(sub_acti_maps_masked[i], sub_acti_maps_masked[j])[0] for i in range(subj_num - 1) for j in range(i + 1, subj_num) ] pattern_similarities_list.append(pattern_similarities) out_file = pjoin( acti_analysis_dir, 'g{}_intra_pattern_similarity.npy'.format(group_label)) np.save(out_file, np.array(pattern_similarities_list).T)
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='') 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_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()
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()
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): patch_stat = [subject_ids[idx]]
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)
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) mfs_prob_maps.append(subgroup_mfs_prob)
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'
def assess_n_cluster(): import os import numpy as np import nibabel as nib from os.path import join as pjoin from scipy import stats from matplotlib import pyplot as plt from commontool.io.io import CiftiReader from commontool.algorithm.tool import elbow_score from FFA_pattern.tool import ClusteringVlineMoverPlotter print('Start: predefine some variates') # ----------------------- # predefine parameters hemi = 'lh' # 'lh', 'rh', 'both' brain_structure = { 'lh': 'CIFTI_STRUCTURE_CORTEX_LEFT', 'rh': 'CIFTI_STRUCTURE_CORTEX_RIGHT' } weight_type = ('dissimilar', 'euclidean') clustering_method = 'HAC_ward_euclidean' max_n_cluster = 100 # dice, modularity, silhouette, gap statistic, elbow_inner_standard # elbow_inner_centroid, elbow_inner_pairwise, elbow_inter_centroid, elbow_inter_pairwise assessment_metric_pairs = [ # ['dice', 'modularity'], ['elbow_inner_standard'], # ['modularity'], # ['silhouette'], # ['gap statistic'] ] assessments_dict = dict() for metric_pair in assessment_metric_pairs: for metric in metric_pair: assessments_dict[metric] = [] is_graph_needed = 'modularity' in assessments_dict.keys() # predefine paths proj_dir = '/nfs/s2/userhome/chenxiayu/workingdir/study/FFA_pattern' pattern_dir = pjoin(proj_dir, 'analysis/s2/lh/zscore') meth_dir = pjoin(pattern_dir, clustering_method) result_dir = pjoin(meth_dir, 'results') roi_files = pjoin(proj_dir, 'data/HCP/label/MMPprob_OFA_FFA_thr1_{}.label') activ_file = pjoin(proj_dir, 'analysis/s2/activation.dscalar.nii') pattern_file = pjoin(pattern_dir, 'roi_patterns.npy') # ----------------------- print('Finish: predefine some variates') print('Start: prepare data') # ----------------------- # prepare patterns reader = CiftiReader(activ_file) if hemi == 'both': roi_lh = nib.freesurfer.read_label(roi_files.format('lh')) roi_rh = nib.freesurfer.read_label(roi_files.format('rh')) activ_roi_lh = reader.get_data(brain_structure['lh'], True)[:, roi_lh] activ_roi_rh = reader.get_data(brain_structure['rh'], True)[:, roi_rh] # activ_roi_lh = nib.load(activ_file.format('lh')).get_data().squeeze().T[:, roi_lh] # activ_roi_rh = nib.load(activ_file.format('rh')).get_data().squeeze().T[:, roi_rh] activ_roi = np.c_[activ_roi_lh, activ_roi_rh] else: roi = nib.freesurfer.read_label(roi_files.format(hemi)) activ_roi = reader.get_data(brain_structure[hemi], True)[:, roi] # activ_roi = nib.load(activ_file).get_data().squeeze().T[:, roi] patterns = np.load(pattern_file) # ----------------------- print('Finish: prepare data') # structure graph # ----------------------- if is_graph_needed: from commontool.algorithm.graph import array2graph print('Start: structure graph') graph = array2graph(patterns, weight_type, edges='upper_right_triangle') print('Finish: structure graph') else: graph = None # ----------------------- print('Start: calculate assessments') # ----------------------- labels_list = [] labels_files = [ pjoin(result_dir, item) for item in os.listdir(result_dir) if 'group_labels' in item ] for labels_file in labels_files: labels_list.append( np.array(open(labels_file).read().split(' '), dtype=np.uint16)) labels_list.sort(key=lambda x: len(set(x))) n_clusters = np.array([len(set(labels)) for labels in labels_list]) indices = np.where(n_clusters > max_n_cluster)[0] if len(indices): end_idx = indices[0] labels_list = labels_list[:end_idx] n_clusters = n_clusters[:end_idx] n_labels = len(labels_list) for idx, labels in enumerate(labels_list): labels_uniq = np.unique(labels) for metric in assessments_dict.keys(): if metric == 'dice': from commontool.algorithm.tool import calc_overlap sub_dices = [] for label in labels_uniq: subgroup_activ_roi = np.atleast_2d( activ_roi[labels == label]) subgroup_activ_roi_mean = np.mean(subgroup_activ_roi, 0) collection1 = np.where(subgroup_activ_roi_mean > 2.3)[0] collection2s = [ np.where(i > 2.3)[0] for i in subgroup_activ_roi ] tmp_dices = map(lambda c2: calc_overlap(collection1, c2), collection2s) sub_dices.extend(tmp_dices) assessments_dict[metric].append(sub_dices) elif metric == 'modularity': from community import modularity partition_dict = {k: v for k, v in enumerate(labels)} assessments_dict[metric].append( modularity(partition_dict, graph, weight='weight')) elif metric == 'silhouette': from sklearn.metrics import silhouette_score # https://stackoverflow.com/questions/19197715/scikit-learn-k-means-elbow-criterion # http://scikit-learn.org/stable/modules/generated/sklearn.metrics.silhouette_score.html#sklearn.metrics.silhouette_score if len(labels_uniq) > 1: assessments_dict[metric].append( silhouette_score(patterns, labels, metric=weight_type[1], random_state=0)) elif 'elbow' in metric: tmp = metric.split('_') assessment = elbow_score(patterns, labels, metric=weight_type[1], type=(tmp[1], tmp[2])) assessments_dict[metric].append(assessment) elif metric == 'gap statistic': pass else: raise RuntimeError( "{} isn't a valid assessment metric".format(metric)) print('Assessment calculated: {0}/{1}'.format(idx + 1, n_labels)) if 'gap statistic' in assessments_dict.keys(): from commontool.algorithm.cluster import hac_scipy from FFA_pattern.tool import k_means, gap_stat_mine if 'HAC' in clustering_method: cluster_method = hac_scipy elif 'KM' in clustering_method: cluster_method = k_means else: raise RuntimeError( "analysis-{} isn't supported at present!".format( clustering_method)) labels_list, gaps, s, k_selected = gap_stat_mine( patterns, n_clusters, cluster_method=cluster_method) assessments_dict['gap statistic'] = (gaps, s, k_selected) x = np.arange(n_labels) x_labels = n_clusters vline_plotter_holder = [] for metric_pair in assessment_metric_pairs: # plot assessment curve v_plotter = ClusteringVlineMoverPlotter(patterns, labels_list, meth_dir, result_dir) if metric_pair[0] == 'dice': y = np.mean(assessments_dict[metric_pair[0]], 1) sem = stats.sem(assessments_dict[metric_pair[0]], 1) v_plotter.axes[0].plot(x, y, 'b.-') v_plotter.axes[0].fill_between(x, y - sem, y + sem, alpha=0.5) elif 'elbow' in metric_pair[0]: y = assessments_dict[metric_pair[0]] v_plotter.axes[0].plot(x, y, 'k.-') x1 = x[:-1] y1 = [y[i] - y[i + 1] for i in x1] fig1, ax1 = plt.subplots() ax1.plot(x1, y1, 'k.-') # ax1.set_title('assessment for #subgroup') # ax1.set_xlabel('#subgroup') ax1.set_xlabel('k') ax1.set_ylabel(metric_pair[0] + "'") if len(x1) > 20: middle_idx = int(len(x1) / 2) ax1.set_xticks(x1[[0, middle_idx, -1]]) ax1.set_xticklabels(x_labels[1:][[0, middle_idx, -1]]) else: ax1.set_xticks(x1) ax1.set_xticklabels(x_labels[1:]) x2 = x1[:-1] y2 = [y1[i] - y1[i + 1] for i in x2] fig2, ax2 = plt.subplots() ax2.plot(x2, y2, 'k.-') # ax2.set_title('assessment for #subgroups') # ax2.set_xlabel('#subgroups') # ax2.set_ylabel(metric_pair[0] + "''") ax2.set_xlabel('k') ax2.set_ylabel('-\u25b3V\u2096') if len(x2) > 20: middle_idx = int(len(x2) / 2) ax2.set_xticks(x2[[0, middle_idx, -1]]) ax2.set_xticklabels(x_labels[1:-1][[0, middle_idx, -1]]) else: ax2.set_xticks(x2) ax2.set_xticklabels(x_labels[1:-1]) elif metric_pair[0] == 'gap statistic': v_plotter.axes[0].plot(x, assessments_dict[metric_pair[0]][0], 'b.-') v_plotter.axes[0].fill_between( x, assessments_dict[metric_pair[0]][0] - assessments_dict[metric_pair[0]][1], assessments_dict[metric_pair[0]][0] + assessments_dict[metric_pair[0]][1], alpha=0.5) elif metric_pair[0] == 'silhouette': if x_labels[0] == 1: v_plotter.axes[0].plot(x[1:], assessments_dict[metric_pair[0]], 'b.-') else: v_plotter.axes[0].plot(x, assessments_dict[metric_pair[0]], 'b.-') else: v_plotter.axes[0].plot(x, assessments_dict[metric_pair[0]], 'b.-') # v_plotter.axes[0].set_title('assessment for #subgroups') # v_plotter.axes[0].set_xlabel('#subgroups') v_plotter.axes[0].set_xlabel('k') if n_labels > 2: if metric_pair[0] == 'gap statistic': vline_idx = np.where( x_labels == assessments_dict[metric_pair[0]][2])[0][0] else: vline_idx = int(n_labels / 2) v_plotter.axes[0].set_xticks(x[[0, vline_idx, -1]]) v_plotter.axes[0].set_xticklabels(x_labels[[0, vline_idx, -1]]) # plt.setp(v_plotter.axes[0].get_xticklabels(), rotation=-90, ha='left', rotation_mode='anchor') else: if metric_pair[0] == 'gap statistic': vline_idx = np.where( x_labels == assessments_dict[metric_pair[0]][2])[0][0] else: vline_idx = 0 v_plotter.axes[0].set_xticks(x) v_plotter.axes[0].set_xticklabels(x_labels) # v_plotter.axes[0].set_ylabel(metric_pair[0], color='b') # v_plotter.axes[0].tick_params('y', colors='b') v_plotter.axes[0].set_ylabel('W\u2096') if len(metric_pair) == 2: # plot another assessment curve in a twin axis # https://matplotlib.org/examples/api/two_scales.html v_plotter.add_twinx(0) if metric_pair[1] == 'dice': y = np.mean(assessments_dict[metric_pair[1]], 1) sem = stats.sem(assessments_dict[metric_pair[1]], 1) v_plotter.axes_twin[0].plot(x, y, 'r.-') v_plotter.axes_twin[0].fill_between(x, y - sem, y + sem, alpha=0.5) elif metric_pair[1] == 'gap statistic': raise RuntimeError( "{} can't be plot at twin axis at present!".format( metric_pair[1])) elif metric_pair[1] == 'silhouette': if x_labels[0] == 1: v_plotter.axes_twin[0].plot( x[1:], assessments_dict[metric_pair[1]], 'b.-') else: v_plotter.axes_twin[0].plot( x, assessments_dict[metric_pair[1]], 'b.-') else: v_plotter.axes_twin[0].plot(x, assessments_dict[metric_pair[1]], 'r.-') v_plotter.axes_twin[0].set_ylabel(metric_pair[1], color='r') v_plotter.axes_twin[0].tick_params('y', colors='r') v_plotter.add_vline_mover(vline_idx=vline_idx, x_round=True) v_plotter.figure.tight_layout() vline_plotter_holder.append(v_plotter) # ----------------------- print('Finish: calculate assessments') plt.show()
clustering_dir = pjoin(analysis_dir, clustering_method) clustering_result_dir = pjoin(clustering_dir, 'clustering_results') FFA_label_files = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/label/{}FFA_25.label') maps_file = pjoin(project_dir, 'data/HCP_1080/face-avg_s2/S1200.1080.FACE-AVG_level2_zstat_hp200_s2_MSMAll.dscalar.nii') FFA_pattern_files = pjoin(analysis_dir, '{}FFA_patterns.nii.gz') # ----------------------- print('Finish: predefine some variates') print('Start: prepare data') # ----------------------- # prepare FFA patterns reader = CiftiReader(maps_file) if hemi == 'both': lFFA_vertices = nib.freesurfer.read_label(FFA_label_files.format('l')) rFFA_vertices = nib.freesurfer.read_label(FFA_label_files.format('r')) lFFA_maps = reader.get_data(brain_structure['lh'], True)[:, lFFA_vertices] rFFA_maps = reader.get_data(brain_structure['rh'], True)[:, rFFA_vertices] # lFFA_maps = nib.load(maps_file.format('lh')).get_data()[:, lFFA_vertices] # rFFA_maps = nib.load(maps_file.format('rh')).get_data()[:, rFFA_vertices] FFA_maps = np.c_[lFFA_maps, rFFA_maps] lFFA_patterns = nib.load(FFA_pattern_files.format('l')).get_data() rFFA_patterns = nib.load(FFA_pattern_files.format('r')).get_data() FFA_patterns = np.c_[lFFA_patterns, rFFA_patterns] else: FFA_vertices = nib.freesurfer.read_label(FFA_label_files.format(hemi[0])) FFA_maps = reader.get_data(brain_structure[hemi], True)[:, FFA_vertices] # FFA_maps = nib.load(maps_file.format(hemi)).get_data()[:, FFA_vertices] FFA_patterns = nib.load(FFA_pattern_files.format(hemi[0])).get_data() # ----------------------- print('Finish: prepare data')
trg_file = '/nfs/t3/workingshop/chenxiayu/study/FFA_clustering/data/HCP_1080/face-avg_s2/retest/' \ 'S1200_retest_WM_cope{0}_{1}_s2_MSMAll_32k_fs_LR.dscalar.nii' with open(subject_id_file) as rf: subject_ids = rf.read().splitlines() maps_new_dict = OrderedDict() map_names_new_dict = OrderedDict() for k in interested_copes.keys(): maps_new_dict[k] = [] map_names_new_dict[k] = [] brain_models = None for subject_id in subject_ids: src_file = src_files.format(subject_id) reader = CiftiReader(src_file) maps = reader.get_data() map_names = reader.map_names() if brain_models is None: brain_models = reader.brain_models() # make sure that we get right copes for k, v in interested_copes.items(): if v not in map_names[k]: raise RuntimeError("subject-{0}'s cope{1} is not {2}".format( subject_id, k, v)) for k in interested_copes.keys(): maps_new_dict[k].append(maps[k].copy()) map_names_new_dict[k].append(map_names[k]) print('Finished: merge {0}_cope{1}'.format(subject_id, k))
project_dir, 'data/HCP_face-avg/s2/S1200.1080.FACE-AVG_level2_zstat_hp200_s2_MSMAll.dscalar.nii' ) print('Finish: predefine some variates') # ----------------------- # prepare data # ----------------------- print('Start: prepare data') # prepare FFA patterns FFA_vertices = nib.freesurfer.read_label(FFA_label_path) # lFFA_vertices = nib.freesurfer.read_label(lFFA_label_path) # rFFA_vertices = nib.freesurfer.read_label(rFFA_label_path) maps_reader = CiftiReader(maps_path) maps = maps_reader.get_data(brain_structure, True) # lmaps = maps_reader.get_data('CIFTI_STRUCTURE_CORTEX_LEFT', True) # rmaps = maps_reader.get_data('CIFTI_STRUCTURE_CORTEX_RIGHT', True) 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')
n_clusters_dir = pjoin(subproject_dir, '{}clusters'.format(n_clusters)) result_dir = pjoin(n_clusters_dir, result_name) if not os.path.exists(result_dir): os.makedirs(result_dir) 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