def __init__(self, subj_fol, atlas_name='yeo7_1000', weight_by='3_2_AxPasi7', tractography_type='wholebrain_5d_labmask_msmt', atlas_main_folder=r'C:\Users\Admin\my_scripts\aal\yeo'): self.folder_name = subj_fol #full path of subj folder self.tractography_type = tractography_type + '.trk' self.nii_file_name = load_dwi_files(self.folder_name)[5] self.streamline_name = self._streamline_name() self.weight_name = weight_by self.streamlines = self._load_streamlines() self.atlas_type = str.lower(atlas_name) self.atlas_main_folder = atlas_main_folder self.mask = self._load_gm_mask() if 'yeo' in self.atlas_type: self.atlas_file_name = os.path.join( self.folder_name, 'r' + atlas_name + '_atlas.nii') self.idx_to_txt = f'{self.atlas_main_folder}{os.sep}{self.atlas_type}{os.sep}index2label.txt' self.mni_atlas_label = f'{self.atlas_main_folder}{os.sep}{self.atlas_type}{os.sep}{self.atlas_type}_atlas.nii' elif 'bna' in self.atlas_type: self.atlas_type = atlas_name self.atlas_file_name = os.path.join( self.folder_name, 'rBN_Atlas_274_combined_1mm.nii') self.idx_to_txt = f'{self.atlas_main_folder}{os.sep}BNA_with_cerebellum.csv' self.mni_atlas_label = f'{self.atlas_main_folder}{os.sep}BN_Atlas_274_combined_1mm.nii'
def extract_one_bundle(moved, target, full_bund_name, bundle_num, subj, folder_name, rt, mct): dipy_home = find_home() #moved = set_number_of_points(moved, 20) recognized_bundle, bundle_labels, model = find_bundle( dipy_home, moved, bundle_num, rt, mct) nii_file = load_dwi_files(folder_name)[5] bundle = target[bundle_labels] if len(bundle) < 20: model = [] recognized_bundle = [] bundle_labels = [] print(f"Couldn't find {full_bund_name} for {subj}") else: keep_s, keep_i = remove_cci_outliers(bundle) new_s = [] new_s += [bundle[s1] for s1 in keep_i] save_ft(folder_name, '', new_s, nii_file, file_name=rf'\{full_bund_name}.trk') return model, recognized_bundle, bundle_labels
def _load_gm_mask(self): ''' Load tissue scan (from Fast in preprocessing) to choose GM mask ''' labels = load_dwi_files(self.folder_name)[3] mask = (labels == 2) return mask
def mean_bundle_weight(folder_name, bundle_file_name): if not os.path.exists(bundle_file_name): mean_bundle = np.nan else: bundle = load_trk(bundle_file_name, "same", bbox_valid_check=False) bundle = bundle.streamlines bvec_file = load_dwi_files(folder_name)[6] mean_vols = weighting_streamlines(folder_name, bundle, bvec_file, weight_by='AxPasi7') mean_bundle = np.nanmean(mean_vols) return mean_bundle
out=np.zeros_like(vols_sum), where=counts != 0) return vox_vol if __name__ == '__main__': main_feo = r'F:\Hila\balance\eo' main_fec = r'F:\Hila\balance\ec' subj_folder = glob(os.path.join(main_feo, '*/')) + glob( os.path.join(main_fec, '*/')) all_subj_folders = [] for sfol in subj_folder[::]: for folder_name in glob(os.path.join(sfol, '*/'))[::]: dir_name = folder_name + 'streamlines' gtab, data, affine, labels, white_matter, nii_file, bvec_file = load_dwi_files( folder_name, small_delta=15) #tract_path = f'{dir_name}{n}_wholebrain_5d_labmask_msmt.trk' for tfiles in glob(os.path.join(dir_name, '*')): if 'wholebrain_4d_labmask.trk' in tfiles: tract_path = os.path.join(dir_name, tfiles) continue streamlines = load_ft(tract_path, nii_file) mean_vol_per_tract = weighting_streamlines(folder_name, streamlines, bvec_file, weight_by='3_2_AxPasi7') vox_vol = vol_map(streamlines, affine, mean_vol_per_tract, white_matter.shape) empty_header = nib.Nifti1Header()
vec_mean = [] vec_median = [] for subj in os.listdir(group): folder_name = os.path.join(group, subj) full_bund_name = f'{subj}_{file_bundle_name}' bundle_file_name = rf'{folder_name}\streamlines\{full_bund_name}.trk' if not os.path.exists(bundle_file_name): print('Moving on!') vec_mean.append(np.nan) vec_median.append(np.nan) continue bundle = load_trk(bundle_file_name, "same", bbox_valid_check=False) bundle = bundle.streamlines bvec_file = load_dwi_files(folder_name)[6] mean_vols = weighting_streamlines( folder_name, bundle, bvec_file, weight_by='ADD_along_streamlines_WMmasked') vec_mean.append(np.nanmean(mean_vols)) vec_median.append(np.nanmedian(mean_vols)) val_dict[bundle_name + '_mean_' + str.split(group, os.sep)[-2]] = vec_mean val_dict[bundle_name + '_median_' + str.split(group, os.sep)[-2]] = vec_median table_vals = pd.DataFrame(val_dict, index=os.listdir(group))
def clean_non_cc(grouping, idx, clean_grouping, clean_streamlines): for pair, tracts in grouping.items(): if pair[0] == 0 or pair[1] == 0: continue else: if pair[0] in list(np.asarray(idx[0:int(len(idx)/2)])+1) and pair[1] in list(np.asarray(idx[int(len(idx)/2):len(idx)])+1): clean_streamlines+=tracts if pair[1] in list(np.asarray(idx[0:int(len(idx)/2)])+1) and pair[0] in list(np.asarray(idx[int(len(idx)/2):len(idx)])+1): clean_streamlines+=tracts clean_streamlines, keep_streamlines_idx = remove_cci_outliers(clean_streamlines) return clean_streamlines if __name__ == '__main__': subj = all_subj_folders names = all_subj_names fig_type = 'cc_10d_labmask' weight_by='2_2_AxPasi7' for s, n in zip(subj[26::], names[26::]): main_folder = subj_folder + 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) clean_grouping, m_clean, clean_streamlines, m_weighted = create_empty_vars() weight_by_data, affine = extract_weighted_data(bvec_file,weight_by) clean_streamlines = clean_non_cc(grouping, idx, clean_grouping, clean_streamlines) save_ft(main_folder, n, clean_streamlines, nii_file, file_name='_'+ fig_type+'_cleaned.trk')