def main(): studydir = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1' epi_txt = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1_epilepsy_imgs.txt' nonepi_txt = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1_nonepilepsy_imgs_37.txt' with open(epi_txt) as f: epiIds = f.readlines() with open(nonepi_txt) as f: nonepiIds = f.readlines() epiIds = list(map(lambda x: x.strip(), epiIds)) nonepiIds = list(map(lambda x: x.strip(), nonepiIds)) epi_files = list() nonepi_files = list() for sub in epiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): epi_files.append(fname) for sub in nonepiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): nonepi_files.append(fname) epi_data = load_bfp_data(epi_files, 171) nonepi_data = load_bfp_data(nonepi_files, 171) t = time.time() X2, Os, Costdif, TotalError = groupBrainSync(nonepi_data) elapsed = time.time() - t np.savez('grp_atlas2.npz', X2=X2, Os=Os) atlas_data, _, _ = normalizeData(np.mean(X2, axis=1)) np.savez('grp_atlas.npz', atlas_data=atlas_data) # Do Pointwise stats # pointwise_stats(epi_data, nonepi_data) vis_grayord_sigcorr(pval, rval, cf.outname, cf.out_dir, int(cf.smooth_iter), cf.save_surfaces, cf.save_figures, 'True') print('done')
pbar = tqdm(total=len(subj)) for i in range(len(subj)): print(str(n)) n = n + 1 fname = os.path.join(dirname, subj[i], 'func', subj[i] + ext) if os.path.isfile(fname): df = spio.loadmat(fname) data = df['dtseries'].T if int(data.shape[0]) == LenTime: sub_ID.append(subj[i]) sub_fname.append(fname) pbar.update(1) np.savetxt(out_dir + "/subjects.csv", sub_ID, delimiter=",", fmt='%s') write_text_timestamp( flog, 'data for ' + str(len(sub_ID)) + ' subjects will be used for atlas creation') sub_data = load_bfp_data(sub_fname, LenTime) #%% run group sync groupatlas_data, _, _, _ = groupBrainSync(sub_data) spio.savemat(os.path.join(out_dir + '/group_atlas.mat'), {'groupatlas_data': groupatlas_data}) write_text_timestamp(flog, 'completed group sync') #%% find representative subject subRef_data, q = IDrefsub_BrainSync(sub_data) write_text_timestamp(flog, 'representative subject is ' + str(sub_ID[q])) #%% sync group atlas to representative subject atlas_data, _ = brainSync(subRef_data, groupatlas_data) spio.savemat(os.path.join(out_dir + '/atlas.mat'), {'atlas_data': atlas_data}) write_text_timestamp( flog, 'group atlas synced to representative subject. group-MDS_atlas done.')
def load_all_data(studydir, epi_txt, test_epi_txt, nonepi_txt, test_nonepi_txt, atlas_labels): atlas = spio.loadmat(atlas_labels) gord_labels = atlas['labels'].squeeze() label_ids = np.unique(gord_labels) # unique label ids # remove WM label from connectivity analysis label_ids = np.setdiff1d(label_ids, (2000, 0)) with open(epi_txt) as f: epiIds = f.readlines() with open(nonepi_txt) as f: nonepiIds = f.readlines() epiIds = list(map(lambda x: x.strip(), epiIds)) nonepiIds = list(map(lambda x: x.strip(), nonepiIds)) # random.shuffle(epiIds) # random.shuffle(nonepiIds) # print(len(epiIds), epiIds) epi_files = list() nonepi_files = list() for sub in epiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): epi_files.append(fname) for sub in nonepiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): nonepi_files.append(fname) epi_data = load_bfp_data(epi_files, 171) nonepi_data = load_bfp_data(nonepi_files, 171) # nsub = epi_data.shape[2] #============================================================== nsub = min(epi_data.shape[2], nonepi_data.shape[2]) epiIds = epiIds[:nsub] nonepiIds = nonepiIds[:nsub] #=============================================================== conn_mat = np.zeros((nsub * aug_times, len(label_ids), len(label_ids))) parcorr_mat = np.zeros((nsub * aug_times, len(label_ids), len(label_ids))) cent_mat = np.zeros((nsub * aug_times, len(label_ids))) input_feat = np.zeros( (nsub * aug_times, len(label_ids), epi_data.shape[0])) print(conn_mat.shape, input_feat.shape) print(epi_data.shape, nonepi_data.shape, gord_labels.shape) _, _, ref_sub = get_connectivity(nonepi_data[:, :, 0], labels=gord_labels, label_ids=label_ids) for subno in range(nsub): # num of subjects conn_mat[subno:subno+aug_times, :, :], parcorr_mat[subno:subno+aug_times, :, :], \ time_series = get_connectivity(epi_data[:, :, subno], labels=gord_labels, label_ids=label_ids) #G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[subno, :, :])) #cent = nx.eigenvector_centrality(G, weight='weight') #cent_mat[subno, :] = np.array(list(cent.items()))[:,1] # print(ref_sub.shape, time_series.shape) # input_feat[subno, :, :, :] = np.transpose(brainSync(ref_sub.T, time_series.T)[0]) # input_feat[subno, :, :, :] = time_series np.savez( '../PTE_parPearson_BCI-DNI_aug10.npz', conn_mat=conn_mat, partial_mat=parcorr_mat, features=input_feat, # 36x16x171 label_ids=label_ids, cent_mat=cent_mat) ##============================================================================ print("non_epi") # nsub = nonepi_data.shape[2] conn_mat = np.zeros((nsub * aug_times, len(label_ids), len(label_ids))) parcorr_mat = np.zeros((nsub * aug_times, len(label_ids), len(label_ids))) cent_mat = np.zeros((nsub * aug_times, len(label_ids))) input_feat = np.zeros( (nsub * aug_times, len(label_ids), epi_data.shape[0])) print(conn_mat.shape, input_feat.shape) # here we are using same number of training subjects for epi and nonepi. for subno in range(nsub): conn_mat[subno:subno+aug_times, :, :], parcorr_mat[subno:subno+aug_times, :, :], \ time_series = get_connectivity(nonepi_data[:, :, subno], labels=gord_labels, label_ids=label_ids) #G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[subno, :, :])) #cent = nx.eigenvector_centrality(G, weight='weight') # cent_mat[subno, :] = np.array(list(cent.items()))[:,1] # input_feat[subno, :, :] = np.transpose(brainSync(ref_sub.T, time_series.T)[0]) #We are not using time series directly as input features, so the input feature here is just zeros. np.savez( '../NONPTE_parPearson_BCI-DNI_aug10.npz', conn_mat=conn_mat, # n_subjects*16*16 partial_mat=parcorr_mat, features=input_feat, # n_subjects * 16 x 171 label_ids=label_ids, cent_mat=cent_mat) print('done')
def main(): studydir = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1' epi_txt = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1_epilepsy_imgs.txt' nonepi_txt = '/ImagePTE1/ajoshi/fitbir/preproc/maryland_rao_v1_nonepilepsy_imgs_37.txt' atlas_labels = '/ImagePTE1/ajoshi/code_farm/bfp/supp_data/BCI-DNI_brain_grayordinate_labels.mat' atlas = spio.loadmat(atlas_labels) gord_labels = atlas['labels'].squeeze() label_ids = np.unique(gord_labels) # unique label ids #label_ids = [301, 300, 401, 400, 101, 100, 201, 200, 501, 500, 900] ''' label_ids = [3, 100, 101, 184, 185, 200, 201, 300, 301, 400, 401, 500, 501, 800, 850, 900] ''' #label_ids=[301, 300, 401, 400, 101, 100, 201, 200, 501, 500, 900] # remove WM label from connectivity analysis label_ids = np.setdiff1d(label_ids, (2000, 0)) with open(epi_txt) as f: epiIds = f.readlines() with open(nonepi_txt) as f: nonepiIds = f.readlines() epiIds = list(map(lambda x: x.strip(), epiIds)) nonepiIds = list(map(lambda x: x.strip(), nonepiIds)) epi_files = list() nonepi_files = list() epi_ids = list() nonepi_ids = list() for sub in epiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): epi_files.append(fname) epi_ids.append(sub) else: print('File does not exist: %s' % fname) for sub in nonepiIds: fname = os.path.join(studydir, sub, 'BFP', sub, 'func', sub + '_rest_bold.32k.GOrd.mat') if os.path.isfile(fname): nonepi_files.append(fname) nonepi_ids.append(sub) else: print('File does not exist: %s' % fname) epi_data = load_bfp_data(epi_files, 171) nonepi_data = load_bfp_data(nonepi_files, 171) nsub = min(epi_data.shape[2], nonepi_data.shape[2]) epi_ids = epi_ids[:nsub] nonepi_ids = nonepi_ids[:nsub] conn_mat = np.zeros((len(label_ids), len(label_ids), nsub)) cent_mat = np.zeros((len(label_ids), nsub)) for subno in range(nsub): conn_mat[:, :, subno] = get_connectivity(epi_data[:, :, subno], labels=gord_labels, label_ids=label_ids) G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[:, :, subno])) cent = nx.eigenvector_centrality(G, weight='weight') cent_mat[:, subno] = np.array(list(cent.items()))[:, 1] np.savez('PTE_graphs.npz', conn_mat=conn_mat, label_ids=label_ids, labels=gord_labels, cent_mat=cent_mat, sub_ids=epi_ids) conn_mat = np.zeros((len(label_ids), len(label_ids), nsub)) cent_mat = np.zeros((len(label_ids), nsub)) for subno in range(nsub): conn_mat[:, :, subno] = get_connectivity(nonepi_data[:, :, subno], labels=gord_labels, label_ids=label_ids) G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[:, :, subno])) cent = nx.eigenvector_centrality(G, weight='weight') cent_mat[:, subno] = np.array(list(cent.items()))[:, 1] np.savez('NONPTE_graphs.npz', conn_mat=conn_mat, label_ids=label_ids, labels=gord_labels, cent_mat=cent_mat, sub_ids=nonepi_ids) print('done')
subTest_varmain = sp.zeros(len(subTest_IDs)); subTest_varc1 = sp.zeros(len(subTest_IDs)); subTest_varc2 = sp.zeros(len(subTest_IDs)) for ind in range(len(sub_ID)): varmain = reg_var[ind] varc1 = reg_cvar1[ind] varc2 = reg_cvar2[ind] if int(subAtlas_idx[ind]) !=1: subTest_varmain[count1] = varmain subTest_varc1[count1] = varc1 subTest_varc2[count1] = varc2 count1 +=1 np.savetxt(cf.out_dir + "/subjects_testing.csv", subTest_IDs, delimiter=",", fmt='%s') write_text_timestamp(log_fname, str(len(subTest_IDs)) + ' subjects will be used for hypothesis testing.') #%% # reads reference data and creates atlas by BrainSync algorithm subAtlas_data = load_bfp_data(subAtlas_fname, int(cf.lentime)) if cf.atlas_groupsync == 'True': write_text_timestamp(log_fname, 'User Option: Group BrainSync algorithm will be used for atlas creation') atlas_data, _, _, _ = groupBrainSync(subAtlas_data) write_text_timestamp(log_fname, 'Done creating atlas') else: write_text_timestamp(log_fname, 'User Option: representative subject will be used for atlas creation') subRef_data, subRef_num = IDrefsub_BrainSync(subAtlas_data) write_text_timestamp(log_fname, 'Subject number ' + str(subRef_num) + ' will be used for atlas creation') atlas_data = generate_avgAtlas(subRef_data, subAtlas_data) spio.savemat(os.path.join(cf.out_dir + '/atlas.mat'), {'atlas_data': atlas_data}) del subAtlas_data #%% sync and calculates geodesic distances subTest_data = load_bfp_data(subTest_fname, int(cf.lentime))
def load_all_data(atlas_labels): atlas = spio.loadmat(atlas_labels) gord_labels = atlas['labels'].squeeze() label_ids = np.unique(gord_labels) # unique label ids # remove WM label from connectivity analysis label_ids = np.setdiff1d(label_ids, (2000, 0)) # with open(epi_txt) as f: # epiIds = f.readlines() # # with open(nonepi_txt) as f: # nonepiIds = f.readlines() # # epiIds = list(map(lambda x: x.strip(), epiIds)) # nonepiIds = list(map(lambda x: x.strip(), nonepiIds)) # # random.shuffle(epiIds) # # random.shuffle(nonepiIds) # # print(len(epiIds), epiIds) # epi_files = list() # nonepi_files = list() # # for sub in epiIds: # fname = os.path.join(studydir, sub, 'BFP', sub, 'func', # sub + '_rest_bold.32k.GOrd.mat') # if os.path.isfile(fname): # epi_files.append(fname) # # for sub in nonepiIds: # fname = os.path.join(studydir, sub, 'BFP', sub, 'func', # sub + '_rest_bold.32k.GOrd.mat') # if os.path.isfile(fname): # nonepi_files.append(fname) adhd_fnames, tdc_fnames, gt_labels = load_csv() adhd_data = load_bfp_data(adhd_fnames, 235) tdc_data = load_bfp_data(tdc_fnames, 235) nsub = adhd_data.shape[2] conn_mat = np.zeros((nsub, len(label_ids), len(label_ids))) parcorr_mat = np.zeros((nsub, len(label_ids), len(label_ids))) cent_mat = np.zeros((nsub, len(label_ids))) input_feat = np.zeros((nsub, len(label_ids), adhd_data.shape[0])) print(conn_mat.shape, input_feat.shape) print(adhd_data.shape, tdc_data.shape, gord_labels.shape) _, _, ref_sub = get_connectivity(tdc_data[:, :, 0], labels=gord_labels, label_ids=label_ids) for subno in range(nsub): # num of subjects conn_mat[subno, :, :], parcorr_mat[ subno, :, :], time_series = get_connectivity(adhd_data[:, :, subno], labels=gord_labels, label_ids=label_ids) #G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[subno, :, :])) #cent = nx.eigenvector_centrality(G, weight='weight') #cent_mat[subno, :] = np.array(list(cent.items()))[:,1] # print(ref_sub.shape, time_series.shape) input_feat[subno, :, :] = np.transpose( brainSync(ref_sub.T, time_series.T)[0]) np.savez( '../ADHD_parPearson_BCI-DNI.npz', conn_mat=conn_mat, partial_mat=parcorr_mat, features=input_feat, # 36x16x171 label_ids=label_ids, cent_mat=cent_mat) ##============================================================================ print("healthy subjects") nsub = tdc_data.shape[2] conn_mat = np.zeros((nsub, len(label_ids), len(label_ids))) parcorr_mat = np.zeros((nsub, len(label_ids), len(label_ids))) cent_mat = np.zeros((nsub, len(label_ids))) input_feat = np.zeros((nsub, len(label_ids), tdc_data.shape[0])) print(conn_mat.shape, input_feat.shape) # here we are using same number of training subjects for epi and nonepi. for subno in range(nsub): conn_mat[subno, :, :], parcorr_mat[ subno, :, :], time_series = get_connectivity(tdc_data[:, :, subno], labels=gord_labels, label_ids=label_ids) #G = nx.convert_matrix.from_numpy_array(np.abs(conn_mat[subno, :, :])) #cent = nx.eigenvector_centrality(G, weight='weight') # cent_mat[subno, :] = np.array(list(cent.items()))[:,1] input_feat[subno, :, :] = np.transpose( brainSync(ref_sub.T, time_series.T)[0]) np.savez( '../TDC_parPearson_BCI-DNI.npz', conn_mat=conn_mat, # n_subjects*16*16 partial_mat=parcorr_mat, features=input_feat, # n_subjects * 16 x 171 label_ids=label_ids, cent_mat=cent_mat) print('done')