def getBIDSinputAndOutput(BIDS, dataset, sub, ses, acq, derivativesOutput): T1path = join(BIDS, dataset, f"sub-{sub}", f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz") utils.checkPathError(T1path) out = join(BIDS, "derivatives", f"{derivativesOutput}") utils.checkPathError(out) outputpath = join(BIDS, "derivatives", f"{derivativesOutput}", f"sub-{sub}", f"ses-{ses}") utils.checkPathAndMake(outputpath, outputpath) return T1path, outputpath
def applywarp_to_atlas(atlas_directory, atlas_paths, preop_T1_std, mni_warp, atlas_registration_output_path): full_atlas_paths = [] for a in range(len(atlas_paths)): utils.checkPathError( f"{os.path.join(atlas_directory, atlas_paths[a])}") full_atlas_paths.append( f"{os.path.join(atlas_directory, atlas_paths[a])}") utils.checkPathError(mni_warp) utils.checkPathError(preop_T1_std) utils.checkPathError(atlas_registration_output_path) for a in range(len(full_atlas_paths)): atlasName = basename(splitext(splitext(full_atlas_paths[a])[0])[0]) outputAtlasName = join(atlas_registration_output_path, atlasName + ".nii.gz") #if not os.path.exists(outputAtlasName): print( f"\r{np.round((a+1)/len(full_atlas_paths)*100,2)}% {atlasName[0:20]} ", end="\r") if not utils.checkIfFileExists(outputAtlasName, printBOOL=False): cmd = f"applywarp -i { full_atlas_paths[a]} -r {preop_T1_std} -w {mni_warp} --interp=nn -o {outputAtlasName}" os.system(cmd)
def get_tracts(singularity_bind_path, path_dsiStudioSingularity, path_dwi, output_directory): utils.checkPathError(singularity_bind_path) utils.checkPathError(path_dsiStudioSingularity) utils.checkPathError(path_dwi) utils.checkPathError(output_directory) base, split, basesplit = utils.baseSplitextNiiGz(path_dwi) source_name = f"{join(output_directory, basesplit)}.src.gz" fib_name = f"{join(output_directory, basesplit)}.fib.gz" trk_name = f"{join(output_directory, basesplit)}.trk.gz" print("\n\nCreating Source File in DSI Studio\n\n") cmd = f"singularity exec --bind {singularity_bind_path} {path_dsiStudioSingularity} dsi_studio --action=src --source={path_dwi} --output={source_name}" os.system(cmd) print("\n\nCreating Reconstruction File in DSI Studio\n\n") cmd = f"singularity exec --bind {singularity_bind_path} {path_dsiStudioSingularity} dsi_studio --action=rec --source={source_name} --method=4 --param0=1.25" os.system(cmd) #rename dsistudio output of .fib file because there is no way to output a specific name on cammnad line cmd = f"mv {source_name}*.fib.gz {fib_name}" os.system(cmd) print("\n\nCreating Tractography File in DSI Studio\n\n") cmd = f"singularity exec --bind {singularity_bind_path} {path_dsiStudioSingularity} dsi_studio --action=trk --source={fib_name} --min_length=10 --max_length=800 --thread_count=16 --fiber_count=1000000 --output={trk_name}" os.system(cmd)
f"sub-{sub}", f"ses-{ses}", "matrices") utils.checkPathAndMake(atlas_registration, atlas_registration, printBOOL=False) utils.checkPathAndMake(structural_matrices, structural_matrices, printBOOL=False) preop_T1 = os.path.join(paths.BIDS_DERIVATIVES_QSIPREP, "qsiprep", f"sub-{sub}", "anat", f"sub-{sub}_desc-preproc_T1w.nii.gz") preop_T1_mask = os.path.join(paths.BIDS_DERIVATIVES_QSIPREP, "qsiprep", f"sub-{sub}", "anat", f"sub-{sub}_desc-brain_mask.nii.gz") preop_T1_bet = os.path.join(atlas_registration, f"sub-{sub}_desc-preproc_T1w_brain.nii.gz") utils.checkPathError(preop_T1) utils.checkPathError(preop_T1_mask) utils.checkPathError(paths.MNI_TEMPLATE) utils.checkPathError(paths.MNI_TEMPLATE_BRAIN) utils.checkPathError(paths.ATLASES) utils.checkPathError(paths.ATLAS_FILES_PATH) utils.checkPathError(structural_matrices) cmd = f"cp {preop_T1} {os.path.join(atlas_registration, os.path.basename(preop_T1))}" os.system(cmd) cmd = f"cp {preop_T1_mask} {os.path.join(atlas_registration, os.path.basename(preop_T1_mask))}" os.system(cmd) cmd = f"fslmaths {preop_T1} -mul {preop_T1_mask} {preop_T1_bet}" os.system(cmd) preop_T1 = os.path.join(atlas_registration, os.path.basename(preop_T1))
raise IOError( f"\n\n\n\nOutput Name must end with .csv.\n\nWhat is given:\n{outputName}\n\n\n\n" ) print( f"\n\n\n\n\n\n\n\n\n\n{outputName} saving to: \n{join(outputDirectory, outputName)}\n\n\n\n" ) fillerString = "\n###########################\n###########################\n###########################\n###########################\n" #atl.get_electrodeTissueMetadata(join(outputDirectory, outputName), join(outputDirectory, "electrodeTissueMetadata.json")) if os.path.exists(join(outputDirectory, outputName)): print( f"\n\nNote: Electrode localization file alread exists in \n{join(outputDirectory, outputName)}\n" ) if rerun == False: sys.exit() """ Examples: #electrodePreopT1Coordinates = "/media/arevell/sharedSSD/linux/data/BIDS/PIER/sub-RID0648/ses-implant01/ieeg/sub-RID0648_ses-implant01_space-preimplantT1w_electrodes.tsv" electrodePreopT1Coordinates = "/media/arevell/sharedSSD/linux/data/BIDS/derivatives/atlasLocalization/sub-RID0648/electrodenames_coordinates_native_and_T1.csv" preopT1 = "/media/arevell/sharedSSD/linux/data/BIDS/derivatives/atlasLocalization/sub-RID0648/T00_RID0648_mprage.nii.gz" preopT1bet = "/media/arevell/sharedSSD/linux/data/BIDS/derivatives/atlasLocalization/sub-RID0648/T00_RID0648_mprage_brainBrainExtractionBrain.nii.gz" MNItemplate = "/media/arevell/sharedSSD/linux/revellLab/tools/mniTemplate/mni_icbm152_t1_tal_nlin_asym_09c_182x218x182.nii.gz" MNItemplatebet = "/media/arevell/sharedSSD/linux/revellLab/tools/mniTemplate/mni_icbm152_t1_tal_nlin_asym_09c_182x218x182_brain.nii.gz" atlasDirectory = "/media/arevell/sharedSSD/linux/revellLab/tools/atlases/atlases" atlasLabelsPath = "/media/arevell/sharedSSD/linux/revellLab/tools/atlases/atlasLabels" outputDirectory = "/media/arevell/sharedSSD/linux/data/BIDS/derivatives/atlasLocalization/sub-RID0648" outputName = "sub-RID0648_atlasLocalization.csv" utils.checkPathError(electrodePreopT1Coordinates)
def mni_registration_to_T1(sub, paths, SESSION_RESEARCH3T="research3Tv[0-9][0-9]"): #getting directories and files #structural connectivity derivatives atlas_registration, structural_matrices, mni_images, mni_warp, ses, preop_T1, preop_T1_std, path_fib, path_trk = get_atlas_registration_and_tractography_paths( sub=sub, paths=paths, SESSION_RESEARCH3T=SESSION_RESEARCH3T) if not utils.checkIfFileExists(f"{mni_images}_fnirt.nii.gz", printBOOL=False): #QSI prep outputs preop_T1 = os.path.join(paths.BIDS_DERIVATIVES_QSIPREP, "qsiprep", f"sub-{sub}", "anat", f"sub-{sub}_desc-preproc_T1w.nii.gz") preop_T1_mask = os.path.join(paths.BIDS_DERIVATIVES_QSIPREP, "qsiprep", f"sub-{sub}", "anat", f"sub-{sub}_desc-brain_mask.nii.gz") preop_T1_bet = os.path.join( atlas_registration, f"sub-{sub}_desc-preproc_T1w_brain.nii.gz") #checking all images exist utils.checkPathError(preop_T1) utils.checkPathError(preop_T1_mask) utils.checkPathError(paths.MNI_TEMPLATE) utils.checkPathError(paths.MNI_TEMPLATE_BRAIN) #copying relevant images from QSI prep to registration folder cmd = f"cp {preop_T1} {os.path.join(atlas_registration, os.path.basename(preop_T1))}" os.system(cmd) cmd = f"cp {preop_T1_mask} {os.path.join(atlas_registration, os.path.basename(preop_T1_mask))}" os.system(cmd) cmd = f"fslmaths {preop_T1} -mul {preop_T1_mask} {preop_T1_bet}" os.system(cmd) #intemediary files output names preop_T1 = os.path.join(atlas_registration, os.path.basename(preop_T1)) preop_T1_mask = os.path.join(atlas_registration, os.path.basename(preop_T1_mask)) preop_T1_std = f"{splitext(splitext(preop_T1)[0])[0]}_std.nii.gz" preop_T1_bet_std = f"{splitext(splitext(preop_T1_bet)[0])[0]}_std.nii.gz" #Begin Pipeline: Orient all images to standard RAS fillerString = "\n###########################" * 3 print( f"\n\n{fillerString}\nPart 1 of 2\nReorientation of Images\nEstimated time: 10-30 seconds{fillerString}\nReorient all images to standard RAS\n" ) cmd = f"fslreorient2std {preop_T1} {preop_T1_std}" print(cmd) os.system(cmd) cmd = f"fslreorient2std {preop_T1_bet} {preop_T1_bet_std}" print(cmd) os.system(cmd) #visualize utils.show_slices(f"{preop_T1_std}", low=0.33, middle=0.5, high=0.66, save=True, saveFilename=join(atlas_registration, "pic_T1.png")) utils.show_slices(f"{preop_T1_bet_std}", low=0.33, middle=0.5, high=0.66, save=True, saveFilename=join(atlas_registration, "pic_T1_brain.png")) #Registration of MNI to patient space (atlases are all in MNI space, so using this warp to apply to the atlases) print( f"\n\n{fillerString}\nPart 2 of 2\nMNI and atlas registration\nEstimated time: 1-2+ hours{fillerString}\nRegistration of MNI template to patient space\n" ) #linear reg of MNI to preopT1 space if not utils.checkIfFileExists(f"{mni_images}_flirt.nii.gz", printBOOL=False): cmd = f"flirt -in {paths.MNI_TEMPLATE_BRAIN} -ref {preop_T1_bet_std} -dof 12 -out {mni_images}_flirt -omat {mni_images}_flirt.mat -v" print(cmd) os.system(cmd) #non linear reg of MNI to preopT1 space utils.show_slices(f"{mni_images}_flirt.nii.gz", low=0.33, middle=0.5, high=0.66, save=True, saveFilename=join(atlas_registration, "pic_mni_to_T1_flirt.png")) print( "\n\nLinear registration of MNI template to image is done\n\nStarting Non-linear registration:\n\n\n" ) if not utils.checkIfFileExists(f"{mni_images}_fnirt.nii.gz", printBOOL=False): cmd = f"fnirt --in={paths.MNI_TEMPLATE} --ref={preop_T1_std} --aff={mni_images}_flirt.mat --iout={mni_images}_fnirt -v --cout={mni_images}_coef --fout={mni_images}_warp" print(cmd) os.system(cmd) utils.show_slices(f"{mni_images}_fnirt.nii.gz", low=0.33, middle=0.5, high=0.66, save=True, saveFilename=join(atlas_registration, "pic_mni_to_T1_fnirt.png")) print(f"\n\n{fillerString}\nDone{fillerString}\n\n\n\n") else: print(f"\n\n\n\nMNI registration already performed\n\n\n\n")
def get_structural_connectivity(singularity_bind_path, path_dsiStudioSingularity, path_fib, path_trk, preop3T, atlas, output): utils.checkPathError(singularity_bind_path) utils.checkPathError(path_dsiStudioSingularity) utils.checkPathError(path_fib) utils.checkPathError(path_trk) utils.checkPathError(preop3T) utils.checkPathError(atlas) cmd = f"singularity exec --bind {singularity_bind_path} {path_dsiStudioSingularity} dsi_studio --action=ana --source={path_fib} --tract={path_trk} --t1t2={preop3T} --connectivity={atlas} --connectivity_type=pass --connectivity_threshold=0 --output={output}" os.system(cmd)