def extract_net_parcel_info(): """ 把每个网络的编号,名字以及所包含的MMP parcel的编号和名字列出来 """ from magicbox.io.io import CiftiReader # inputs net_file = pjoin(work_dir, 'networks.dlabel.nii') # outputs out_file = pjoin(work_dir, 'net_parcel_info.txt') # prepare reader = CiftiReader(net_file) net_names = reader.map_names() lbl_tables = reader.label_tables() # calculate & save wf = open(out_file, 'w') for net_idx, net_name in enumerate(net_names): wf.write(f'>>>{net_idx+1}-{net_name}\n') lbl_tab = lbl_tables[net_idx] for lbl_k, lbl_v in lbl_tab.items(): if lbl_k == 0: continue wf.write(f'{lbl_k}-{lbl_v.label[:-4]}\n') wf.write('<<<\n') wf.close()
def make_non_outlier_map(fpath, thr, roi_name, out_file_mask=None, out_file_prob=None): """ 将同时在thr%以上的被试中被认定为outlier的顶点判定为跨被试的outlier If out_file_mask is not None, save mask map. (.dlabel.nii or .npy) If out_file_prob is not None, save prob map. (.dscalar.nii) """ # prepare data = np.load(fpath) n_subj, n_vtx = data.shape atlas1 = Atlas('Cole_visual_LR') atlas2 = Atlas('Cole_visual_ROI') assert atlas1.maps.shape == (1, LR_count_32k) assert atlas2.maps.shape == (1, LR_count_32k) roi_idx_map = atlas1.maps[0] == atlas1.roi2label[roi_name] if roi_name == 'R_cole_visual': prefix = 'R_' elif roi_name == 'L_cole_visual': prefix = 'L_' else: raise ValueError("error roi_name:", roi_name) mmp_reader = CiftiReader(mmp_map_file) mmp_lbl_tab = mmp_reader.label_tables()[0] # calculate if out_file_mask is not None: data_tmp = np.sum(data, axis=0) outlier_vec = data_tmp > thr / 100 * n_subj print(f'#outliers/total: {np.sum(outlier_vec)}/{n_vtx}') mask_npy = np.zeros(LR_count_32k, bool) mask_npy[roi_idx_map] = ~outlier_vec if out_file_mask.endswith('.npy'): np.save(out_file_mask, mask_npy) elif out_file_mask.endswith('.dlabel.nii'): mask_cii = atlas2.maps.copy() mask_cii[0, ~mask_npy] = np.nan lbl_tab = nib.cifti2.cifti2.Cifti2LabelTable() for roi, lbl in atlas2.roi2label.items(): if roi.startswith(prefix): lbl_tab[lbl] = mmp_lbl_tab[lbl] save2cifti(out_file_mask, mask_cii, mmp_reader.brain_models(), label_tables=[lbl_tab]) else: raise ValueError("Not supported file name:", out_file_mask) if out_file_prob is not None: data_tmp = np.mean(data, axis=0) prob_map = np.ones((1, LR_count_32k), dtype=np.float64) * np.nan prob_map[0, roi_idx_map] = data_tmp assert out_file_prob.endswith('.dscalar.nii') save2cifti(out_file_prob, prob_map, mmp_reader.brain_models())
def separate_networks(): """ 把ColeNetwork的12个网络分到单独的map里。 每个map的MMP parcel的label保留原来的样子。 需要注意的是multimodal_glasser的MMP_mpmLR32k.dlabel.nii中, ROI label的编号1~180是右脑,181~360是左脑。0对应的是??? 而cortex_parcel_network_assignments.mat中0~359的index是左脑在先 """ import numpy as np import nibabel as nib from scipy.io import loadmat from cxy_visual_dev.lib.ColeNet import get_name_label_of_ColeNetwork from magicbox.io.io import CiftiReader, save2cifti # inputs mmp_file = '/nfs/p1/atlases/multimodal_glasser/surface/'\ 'MMP_mpmLR32k.dlabel.nii' roi2net_file = pjoin(cole_dir, 'cortex_parcel_network_assignments.mat') # outputs out_file = pjoin(work_dir, 'networks.dlabel.nii') # load mmp_reader = CiftiReader(mmp_file) mmp_map = mmp_reader.get_data()[0] lbl_tab_raw = mmp_reader.label_tables()[0] roi2net = loadmat(roi2net_file)['netassignments'][:, 0] roi2net = np.r_[roi2net[180:], roi2net[:180]] net_labels = np.unique(roi2net) # prepare data = np.zeros((len(net_labels), len(mmp_map)), dtype=np.uint16) map_names = [] label_tables = [] net_lbl2name = {} for name, lbl in zip(*get_name_label_of_ColeNetwork()): net_lbl2name[lbl] = name # calculate for net_idx, net_lbl in enumerate(net_labels): roi_labels = np.where(roi2net == net_lbl)[0] + 1 lbl_tab = nib.cifti2.cifti2.Cifti2LabelTable() lbl_tab[0] = lbl_tab_raw[0] for roi_lbl in roi_labels: data[net_idx, mmp_map == roi_lbl] = roi_lbl lbl_tab[roi_lbl] = lbl_tab_raw[roi_lbl] map_names.append(net_lbl2name[net_lbl]) label_tables.append(lbl_tab) # save save2cifti(out_file, data, mmp_reader.brain_models(), map_names, label_tables=label_tables)
def separate_networks(): """ 把ColeNetwork的12个网络分到单独的map里。 每个map的MMP parcel的label保留原来的样子。 需要注意的是multimodal_glasser的MMP_mpmLR32k.dlabel.nii中, ROI label的编号1~180是右脑,181~360是左脑。0对应的是??? 而cortex_parcel_network_assignments.mat中0~359的index是左脑在先 """ # outputs out_file = pjoin(work_dir, 'networks.dlabel.nii') # load mmp_reader = CiftiReader(mmp_map_file) mmp_map = mmp_reader.get_data()[0] lbl_tab_raw = mmp_reader.label_tables()[0] roi2net = loadmat(cole_net_assignment_file)['netassignments'][:, 0] roi2net = np.r_[roi2net[180:], roi2net[:180]] net_labels = np.unique(roi2net) # prepare data = np.zeros((len(net_labels), len(mmp_map)), dtype=np.uint16) map_names = [] label_tables = [] # calculate for net_idx, net_lbl in enumerate(net_labels): roi_labels = np.where(roi2net == net_lbl)[0] + 1 lbl_tab = nib.cifti2.cifti2.Cifti2LabelTable() lbl_tab[0] = lbl_tab_raw[0] for roi_lbl in roi_labels: data[net_idx, mmp_map == roi_lbl] = roi_lbl lbl_tab[roi_lbl] = lbl_tab_raw[roi_lbl] map_names.append(cole_label2name[net_lbl]) label_tables.append(lbl_tab) # save save2cifti(out_file, data, mmp_reader.brain_models(), map_names, label_tables=label_tables)