def find_centroids(streamlines,th,max_clus, folder_name='', vol_weighted = True, weight_by = '1.5_2_AxPasi5'): from dipy.segment.clustering import QuickBundles ''' parameters: streamlines: ArraySequence of streamlines th: float for max dist from a streamline to the centroids. Above this value a new centroid is created as long as the number of centroids < max clus. max_clus: int of max number of clusters :return: centroids: a list of streamilnes centroid representation s_list: a list of lists. Each inner list is the group of streamlines cooresponds to a single centroid. ''' qb = QuickBundles(th,max_nb_clusters=max_clus) qbmap = qb.cluster(streamlines) centroids = qbmap.centroids s_list = [] for i in qbmap.clusters: s_list.append(list(i)) if vol_weighted: bvec_file = load_dwi_files(folder_name)[6] vec_vols = weight_clus_by_vol_img(weight_by,s_list, folder_name, bvec_file) return centroids, s_list, vec_vols else: return centroids, s_list
m_weighted[pair[1] - 1, pair[0] - 1] = mean_path_vol return m_clean, m_weighted if __name__ == '__main__': subj = all_subj_folders names = all_subj_names index_to_text_file = r'C:\Users\Admin\my_scripts\aal\megaatlas\megaatlascortex2nii.txt' fig_types = ['cc_1d', 'genu_1d', 'body_1d', 'splenium_1d'] weight_by = '1.5_2_AxPasi5' a = True for s, n in zip(subj, names): for fig_type in fig_types: main_folder = r'C:\Users\Admin\my_scripts\Ax3D_Pack\V6\after_file_prep' + s gtab, data, affine, labels, white_matter, nii_file, bvec_file = load_dwi_files( main_folder) labels_headers, idx, m, grouping, h = load_fibers( main_folder, n, index_to_text_file, fig_type, nii_file) #if os.path.exists(os.path.join(main_folder, 'non-weighted('+fig_type+', MegaAtlas).png')): #if not os.path.exists(os.path.join(main_folder, 'non-weighted(' + fig_type + ', MegaAtlas).png')): if a: clean_grouping, m_clean, clean_streamlines, m_weighted = create_empty_vars( ) weight_by_data, affine = extract_weighted_data( bvec_file, weight_by) clean_grouping, clean_streamlines = clean_non_cc( grouping, idx, clean_grouping, clean_streamlines) m_clean, m_weighted = cleaned_tracts_to_mat( clean_grouping, m_clean, weight_by_data, affine, m_weighted)
for i,s in enumerate(s_list): mean_vol_per_tract = weighting_streamlines(folder_name,s,bvec_file,show=False,weight_by=weight_by) vec_vols[i] = np.nanmean(mean_vol_per_tract) return vec_vols if __name__ == '__main__': main_folder = subj_folder for n,s in zip(all_subj_names[1::],all_subj_folders[1::]): folder_name = main_folder+s dir_name = folder_name + '\streamlines' sft_target = load_trk(f'{dir_name}{n}_wholebrain_3d.trk', "same", bbox_valid_check=False) streamlines = sft_target.streamlines bvec_file = load_dwi_files(folder_name)[6] index_to_text_file = r'C:\Users\hila\data\megaatlas\megaatlas2nii.txt' idx = nodes_labels_mega(index_to_text_file)[1] lab_labels_index, affine = nodes_by_index_mega(folder_name) m, grouping = utils.connectivity_matrix(streamlines, affine, lab_labels_index, return_mapping=True, mapping_as_streamlines=True) mat_file = f'{folder_name}\weighted_mega_wholebrain_4d_labmask.npy' con_mat = np.load(mat_file) id = np.argsort(idx) con_mat = con_mat[id] con_mat = con_mat[:, id] vec_vols = [] s_list = []