def load_fibers(main_folder,n,index_to_text_file,fig_type,nii_file): tract_file = main_folder + r'\streamlines' + n + '_'+fig_type+'.trk' streamlines = load_ft(tract_file,nii_file) lab_labels_index, affine = nodes_by_index_general(main_folder, atlas='yeo7_200') labels_headers, idx = nodes_labels_yeo7(index_to_text_file) new_data, m, grouping = non_weighted_con_mat_mega(streamlines, lab_labels_index, affine, idx, main_folder) h = labels_headers return labels_headers, idx, m, grouping, h
def atlas_and_idx(atlas_type, atlas_main_folder): if 'yeo' in atlas_type: mni_atlas_label = f'{atlas_main_folder}{os.sep}{atlas_type}{os.sep}{atlas_type}_atlas.nii' atlas_labels = nib.load(mni_atlas_label).get_fdata() idx_to_txt = f'{atlas_main_folder}{os.sep}{atlas_type}{os.sep}index2label.txt' idx = nodes_labels_yeo7(idx_to_txt)[1] elif 'bna' in atlas_type: mni_atlas_label = f'{atlas_main_folder}{os.sep}BN_Atlas_274_combined_1mm.nii' atlas_labels = nib.load(mni_atlas_label).get_fdata() idx_to_txt = f'{atlas_main_folder}{os.sep}BNA_with_cerebellum.csv' idx = nodes_labels_bna(idx_to_txt)[1] return atlas_labels, mni_atlas_label, idx
def _calc_weights(self): ''' Calculate connectivity matrices based on chosen atlas and than calculate the weighted average to compute mean ADD in each label ''' import numpy as np from weighted_tracts import nodes_by_index_general, nodes_labels_yeo7, nodes_labels_bna, non_weighted_con_mat_mega, weighted_con_mat_mega lab_labels_index, affine = nodes_by_index_general( self.folder_name, atlas=self.atlas_type) if 'yeo' in self.atlas_type: labels_headers, idx = nodes_labels_yeo7(self.idx_to_txt) elif 'bna' in self.atlas_type: labels_headers, idx = nodes_labels_bna(self.idx_to_txt) new_data, num_mat, grouping = non_weighted_con_mat_mega( self.streamlines, lab_labels_index, affine, idx, self.folder_name, fig_type=self.atlas_type) add_mat = weighted_con_mat_mega(self.weight_name, grouping, idx, self.folder_name, fig_type=self.atlas_type)[1] mutual_mat = add_mat * num_mat #mutual_mat[mutual_mat<=0]= np.nan weights = np.sum(mutual_mat, 1) / np.sum(num_mat, 1) weights[np.isnan(weights)] = 0 weights_dict = {idx[i]: weights[i] for i in range(len(idx))} self.labels_weights = weights_dict
def create_labels_dict(atlas='yeo7'): if atlas == 'yeo7': labels_headers, idx = nodes_labels_yeo7(index_to_text_file) labels_dict = {} sides_dict = {} net_dict = {} for l, i in zip(labels_headers, idx): lparts = l.split('_') labels_dict[i] = f'{lparts[1]}_{lparts[2]}_{lparts[-1]}' net_dict[i] = f'{lparts[1]}_{lparts[2]}' #net_dict[i] = f'{lparts[2]}' if '_LH_' in l: sides_dict[i] = 'LH' elif '_RH_' in l: sides_dict[i] = 'RH' else: sides_dict[i] = 'C' return sides_dict, labels_dict, net_dict elif atlas == 'aal3': labels_headers, idx = nodes_labels_aal3(index_to_text_file) labels_dict = {} sides_dict = {} for l, i in zip(labels_headers, idx): labels_dict[i] = l if '_L' in l: sides_dict[i] = 'LH' elif '_R' in l: sides_dict[i] = 'RH' else: sides_dict[i] = 'C' return sides_dict, labels_dict
mat_file_ax = subj_folder + s + r'\weighted_wholebrain_4d_labmask_yeo7_200_nonnorm.npy' mat_ax = np.load(mat_file_ax) mat_dist_ax = mat_ax / np.nanmax(mat_ax[:]) mat_norm_ax = 1 / mat_dist_ax mat_norm_ax[np.isinf(mat_norm_ax)] = 0 mat_norm_ax[np.isnan(mat_norm_ax)] = 0 mat_file_num = subj_folder + s + r'\non-weighted_wholebrain_4d_labmask_yeo7_200_nonnorm.npy' mat_num = np.load(mat_file_num) mat_dist_num = mat_num / np.nanmax(mat_num[:]) #mat_norm_num = 1 / mat_dist_num mat_norm_num = -1 * np.log(mat_dist_num) mat_norm_num[np.isinf(mat_norm_num)] = 0 mat_norm_num[np.isnan(mat_norm_num)] = 0 idx = nodes_labels_yeo7(index_to_text_file)[1] id = np.argsort(idx) g_ax = from_mat_2_graph(mat_norm_ax, id) g_num = from_mat_2_graph(mat_norm_num, id) aspl_ax = nx.average_shortest_path_length(g_ax, weight='weight', method='dijkstra') aspl_num = nx.average_shortest_path_length(g_num, weight='weight', method='dijkstra') aspl_nw = nx.average_shortest_path_length(g_num, weight=None) aspl[:, i] = [aspl_nw, aspl_num, aspl_ax] eff_ax = 1 / aspl_ax
import networkx as nx import nxviz as nv import matplotlib.pyplot as plt import numpy as np from all_subj import index_to_text_file from weighted_tracts import nodes_labels_yeo7,nodes_labels_aal3 from network_analysis.specific_functional_yeo7networks import network_id_list atlas = 'yeo7' network = 'sommot' side = 'both' if atlas == 'yeo7': labels_headers, idx = nodes_labels_yeo7(index_to_text_file) n = len(idx) start = int(n/2-1) cont = start+1 id = idx[start::-1]+idx[cont::] elif atlas == 'aal3': labels_headers, idx = nodes_labels_aal3(index_to_text_file) n = len(idx) id = list(range(n)) start = 77 #end of left sided areas cont = start+1 id = id[start::-1]+id[cont::] nodes_labels={} for l,i in zip(labels_headers,id): lparts = l.split('_') #nodes_labels[i] = '_'.join(lparts[1:]) nodes_labels[i]=f'{lparts[1]}_{lparts[2]}_{lparts[-1]}' #nodes_labels[i]=l