def complete_preproc_wo_fieldmap(outdir, dwi, bval, bvec, subject_id, fs_subjects_dir = None, invertX = True, invertY = False, invertZ = False, delete_steps = False): """ Function that runs all preprocessing steps using Connectomist but with BrainSuite for the correction of susceptibility distortions. Parameters ---------- outdir: Str, path to folder where all the preprocessing will be done. dwi Str, path to input Nifti DW data. bval: Str, path to Nifti's associated .bval file. bvec: Str, path to Nifti's associated .bval file. subject_id: Str, subject identifier used in Freesurfer. fs_subjects_dir: If the Freesurfer $SUBJECTS_DIR environment variable is not set, or to bypass it, pass the path. invertX: Bool, if True invert x-axis in diffusion model. invertY: Bool, same as invertX but for y-axis. invertZ: Bool, same as invertX but for z-axis. delete_steps: Bool, if True remove all intermediate files and directories at the end of preprocessing, to keep only selected files: preprocessed Nifti + bval + bvec + outliers.py + nodif_brain.nii.gz Returns ------- outdir: Directory with the preprocessed files. <unit> <output name="preproc_dwi" type="File" /> <output name="preproc_bval" type="File" /> <output name="preproc_bvec" type="File" /> <input name="outdir" type="Directory" /> <input name="dwi" type="File" /> <input name="bval" type="File" /> <input name="bvec" type="File" /> <input name="subject_id" type="Str" /> <input name="fs_subjects_dir" type="Str" /> <input name="invertX" type="Bool" /> <input name="invertY" type="Bool" /> <input name="invertZ" type="Bool" /> <input name="delete_steps" type="Bool" /> </unit> """ ### Step 0 - Initialization # If Freesurfer SUBJECTS_DIR is not passed, it should be set as environment variable if fs_subjects_dir is None: if "SUBJECTS_DIR" in os.environ: fs_subjects_dir = os.environ["SUBJECTS_DIR"] else: raise ValueError("Missing <SUBJECTS_DIR>: set the $SUBJECTS_DIR " "environment variable for Freesurfer or pass it " "as an argument.") # Raise an Exception if BrainsuiteCheck is not installed check_brainsuite_installation() # Create the preprocessing output directory if not existing if not os.path.isdir(outdir): os.makedirs(outdir) ### Step 1 - Import files to Connectomist and choose q-space model raw_dwi_dir = os.path.join(outdir, "01-Import_and_qspace_model") dwi_data_import_and_qspace_sampling(raw_dwi_dir, dwi = dwi, bval = bval, bvec = bvec, manufacturer = "Siemens", # unused but required invertX = invertX, invertY = invertY, invertZ = invertZ, subject_id = subject_id) ### Step 2 --- Create a brain mask rough_mask_dir = os.path.join(outdir, "02-Rough_mask") dwi_rough_mask_extraction(rough_mask_dir, raw_dwi_dir) ### Step 3 - Detect and correct outlying diffusion slices outliers_dir = os.path.join(outdir, "03-Outliers") dwi_outlier_detection(outliers_dir, raw_dwi_dir, rough_mask_dir) # Export outliers.py path_outliers_py = os.path.join(outliers_dir, "outliers.py") shutil.copy(path_outliers_py, outdir) ### Step 4 - Eddy current and motion correction eddy_motion_dir = os.path.join(outdir, "04-Eddy_current_and_motion") dwi_eddy_current_and_motion_correction(eddy_motion_dir, raw_dwi_dir, rough_mask_dir, outliers_dir) ### Step 5 - Convert Connectomist result to Nifti with bval/bvec dwi, bval, bvec = export_eddy_motion_results_to_nifti(eddy_motion_dir, filename = "dwi_ecc") ### Step 6 - Susceptibility correction using BrainSuite brainsuite_dir = os.path.join(outdir, "05-Suceptibility_BrainSuite") if not os.path.isdir(brainsuite_dir): os.mkdir(brainsuite_dir) dwi_wo_susceptibility, bval, bvec = \ brainsuite_susceptibility_correction(outdir = brainsuite_dir, dwi = dwi, bval = bval, bvec = bvec, subject_id = subject_id, fs_subjects_dir = fs_subjects_dir, qc_dir = outdir) ### Step 7 - move corrected diffusion to outdir dwi_preproc = os.path.join(outdir, "dwi.nii.gz") bval_preproc = os.path.join(outdir, "dwi.bval") bvec_preproc = os.path.join(outdir, "dwi.bvec") shutil.copyfile(dwi_wo_susceptibility, dwi_preproc) shutil.copyfile(bval, bval_preproc) shutil.copyfile(bvec, bvec_preproc) ### Step 8 - Create a T2 brain mask of preprocessed DWI data bet2_prefix = os.path.join(outdir, "nodif_brain") nodif_brain = os.path.join(bet2_prefix + ".nii.gz") nodif_brain_mask = os.path.join(bet2_prefix + "_mask.nii.gz") bet2(dwi, bet2_prefix, f=0.25, m=True) ### Step 9 - clean intermediate directories if requested if delete_steps: intermediate_directories = [raw_dwi_dir, rough_mask_dir, outliers_dir, eddy_motion_dir, brainsuite_dir] for directory in intermediate_directories: shutil.rmtree(directory) return outdir, dwi_preproc, bval_preproc, bvec_preproc, nodif_brain, nodif_brain_mask
def complete_preproc_wo_fieldmap(outdir, dwi, bval, bvec, phase_enc_dir, subject_id, subjects_dir=None, invertX=True, invertY=False, invertZ=False, delete_steps=False): """ Function that runs all preprocessing steps using Connectomist but with BrainSuite for the correction of susceptibility distortions. Parameters ---------- outdir: str Path to directory where all the preprocessing will be done. dwi: str path to input diffusion-weighted Nifti data. bval: str Path to the Nifti's associated .bval file. bvec: str Ppath to Nifti's associated .bval file. phase_enc_dir: str In plane phase encoding direction, "y", "y-", "x" or "x-". subject_id: str Subject identifier used in Freesurfer. subjects_dir: Str, default None If the Freesurfer $SUBJECTS_DIR environment variable is not set, or to bypass it, pass the path. invertX: bool, default True If True invert x-axis in diffusion model. invertY: bool, default False Same as invertX but for y-axis. invertZ: bool, default False Same as invertX but for z-axis. delete_steps: bool If True remove all intermediate files and directories at the end of preprocessing, to keep only selected files: - preprocessed Nifti + bval + bvec + outliers.py + nodif_brain*.nii.gz Returns ------- outdir: str, Path to directory with the preprocessed files. <unit> <output name="preproc_dwi" type="File" /> <output name="preproc_bval" type="File" /> <output name="preproc_bvec" type="File" /> <input name="outdir" type="Directory" /> <input name="dwi" type="File" /> <input name="bval" type="File" /> <input name="bvec" type="File" /> <input name="phase_enc_dir" type="Str" /> <input name="subject_id" type="Str" /> <input name="subjects_dir" type="Str" /> <input name="invertX" type="Bool" /> <input name="invertY" type="Bool" /> <input name="invertZ" type="Bool" /> <input name="delete_steps" type="Bool" /> </unit> """ # Step 0 - Initialization # Freesurfer 'subjects_dir' has to be passed or set as environment variable subjects_dir = get_or_check_freesurfer_subjects_dir(subjects_dir) # Raise an Exception if BrainsuiteCheck is not installed check_brainsuite_installation() # Create the preprocessing output directory if not existing if not os.path.isdir(outdir): os.makedirs(outdir) # Step 1 - Import files to Connectomist and choose q-space model raw_dwi_dir = os.path.join(outdir, "01-Import_and_qspace_model") # The manufacturer option is unused (use brainsuite for susceptibility # correction) but required dwi_data_import_and_qspace_sampling(raw_dwi_dir, dwi=dwi, bval=bval, bvec=bvec, manufacturer="Siemens", invertX=invertX, invertY=invertY, invertZ=invertZ, subject_id=subject_id) # Step 2 - Create a brain mask rough_mask_dir = os.path.join(outdir, "02-Rough_mask") dwi_rough_mask_extraction(rough_mask_dir, raw_dwi_dir) # Step 3 - Detect and correct outlying diffusion slices outliers_dir = os.path.join(outdir, "03-Outliers") dwi_outlier_detection(outliers_dir, raw_dwi_dir, rough_mask_dir) # Export outliers.py path_outliers_py = os.path.join(outliers_dir, "outliers.py") shutil.copy(path_outliers_py, outdir) # Step 4 - Eddy current and motion correction eddy_motion_dir = os.path.join(outdir, "04-Eddy_current_and_motion") dwi_eddy_current_and_motion_correction(eddy_motion_dir, raw_dwi_dir, rough_mask_dir, outliers_dir) # Step 5 - Convert Connectomist result to Nifti with bval/bvec dwi, bval, bvec = export_eddy_motion_results_to_nifti(eddy_motion_dir, filename="dwi_ecc") # Step 6 - Susceptibility correction using BrainSuite brainsuite_dir = os.path.join(outdir, "05-Suceptibility_BrainSuite") if not os.path.isdir(brainsuite_dir): os.mkdir(brainsuite_dir) dwi_wo_susceptibility, bval, bvec = \ brainsuite_susceptibility_correction(outdir=brainsuite_dir, dwi=dwi, bval=bval, bvec=bvec, phase_enc_dir=phase_enc_dir, subject_id=subject_id, subjects_dir=subjects_dir, qc_dir=outdir) # Step 7 - move corrected diffusion to outdir dwi_preproc = os.path.join(outdir, "dwi.nii.gz") bval_preproc = os.path.join(outdir, "dwi.bval") bvec_preproc = os.path.join(outdir, "dwi.bvec") shutil.copyfile(dwi_wo_susceptibility, dwi_preproc) shutil.copyfile(bval, bval_preproc) shutil.copyfile(bvec, bvec_preproc) # Step 8 - Create a T2 brain mask of preprocessed DWI data bet2_prefix = os.path.join(outdir, "nodif_brain") nodif_brain = os.path.join(bet2_prefix + ".nii.gz") nodif_brain_mask = os.path.join(bet2_prefix + "_mask.nii.gz") bet2(dwi, bet2_prefix, f=0.25, m=True) # Step 9 - clean intermediate directories if requested if delete_steps: intermediate_directories = [ raw_dwi_dir, rough_mask_dir, outliers_dir, eddy_motion_dir, brainsuite_dir ] for directory in intermediate_directories: shutil.rmtree(directory) return (outdir, dwi_preproc, bval_preproc, bvec_preproc, nodif_brain, nodif_brain_mask)
def complete_preproc_wo_fieldmap(outdir, dwi, bval, bvec, phase_enc_dir, subject_id, subjects_dir=None, invertX=True, invertY=False, invertZ=False, delete_steps=False): """ Function that runs all preprocessing steps using Connectomist but with BrainSuite for the correction of susceptibility distortions. Parameters ---------- outdir: str Path to directory where all the preprocessing will be done. dwi: str path to input diffusion-weighted Nifti data. bval: str Path to the Nifti's associated .bval file. bvec: str Ppath to Nifti's associated .bval file. phase_enc_dir: str In plane phase encoding direction, "y", "y-", "x" or "x-". subject_id: str Subject identifier used in Freesurfer. subjects_dir: Str, default None If the Freesurfer $SUBJECTS_DIR environment variable is not set, or to bypass it, pass the path. invertX: bool, default True If True invert x-axis in diffusion model. invertY: bool, default False Same as invertX but for y-axis. invertZ: bool, default False Same as invertX but for z-axis. delete_steps: bool If True remove all intermediate files and directories at the end of preprocessing, to keep only selected files: - preprocessed Nifti + bval + bvec + outliers.py + nodif_brain*.nii.gz Returns ------- outdir: str, Path to directory with the preprocessed files. <unit> <output name="preproc_dwi" type="File" /> <output name="preproc_bval" type="File" /> <output name="preproc_bvec" type="File" /> <input name="outdir" type="Directory" /> <input name="dwi" type="File" /> <input name="bval" type="File" /> <input name="bvec" type="File" /> <input name="phase_enc_dir" type="Str" /> <input name="subject_id" type="Str" /> <input name="subjects_dir" type="Str" /> <input name="invertX" type="Bool" /> <input name="invertY" type="Bool" /> <input name="invertZ" type="Bool" /> <input name="delete_steps" type="Bool" /> </unit> """ # Step 0 - Initialization # Freesurfer 'subjects_dir' has to be passed or set as environment variable subjects_dir = get_or_check_freesurfer_subjects_dir(subjects_dir) # Raise an Exception if BrainsuiteCheck is not installed check_brainsuite_installation() # Create the preprocessing output directory if not existing if not os.path.isdir(outdir): os.makedirs(outdir) # Step 1 - Import files to Connectomist and choose q-space model raw_dwi_dir = os.path.join(outdir, "01-Import_and_qspace_model") # The manufacturer option is unused (use brainsuite for susceptibility # correction) but required dwi_data_import_and_qspace_sampling(raw_dwi_dir, dwi=dwi, bval=bval, bvec=bvec, manufacturer="Siemens", invertX=invertX, invertY=invertY, invertZ=invertZ, subject_id=subject_id) # Step 2 - Create a brain mask rough_mask_dir = os.path.join(outdir, "02-Rough_mask") dwi_rough_mask_extraction(rough_mask_dir, raw_dwi_dir) # Step 3 - Detect and correct outlying diffusion slices outliers_dir = os.path.join(outdir, "03-Outliers") dwi_outlier_detection(outliers_dir, raw_dwi_dir, rough_mask_dir) # Export outliers.py path_outliers_py = os.path.join(outliers_dir, "outliers.py") shutil.copy(path_outliers_py, outdir) # Step 4 - Eddy current and motion correction eddy_motion_dir = os.path.join(outdir, "04-Eddy_current_and_motion") dwi_eddy_current_and_motion_correction(eddy_motion_dir, raw_dwi_dir, rough_mask_dir, outliers_dir) # Step 5 - Convert Connectomist result to Nifti with bval/bvec dwi, bval, bvec = export_eddy_motion_results_to_nifti(eddy_motion_dir, filename="dwi_ecc") # Step 6 - Susceptibility correction using BrainSuite brainsuite_dir = os.path.join(outdir, "05-Suceptibility_BrainSuite") if not os.path.isdir(brainsuite_dir): os.mkdir(brainsuite_dir) dwi_wo_susceptibility, bval, bvec = \ brainsuite_susceptibility_correction(outdir=brainsuite_dir, dwi=dwi, bval=bval, bvec=bvec, phase_enc_dir=phase_enc_dir, subject_id=subject_id, subjects_dir=subjects_dir, qc_dir=outdir) # Step 7 - move corrected diffusion to outdir dwi_preproc = os.path.join(outdir, "dwi.nii.gz") bval_preproc = os.path.join(outdir, "dwi.bval") bvec_preproc = os.path.join(outdir, "dwi.bvec") shutil.copyfile(dwi_wo_susceptibility, dwi_preproc) shutil.copyfile(bval, bval_preproc) shutil.copyfile(bvec, bvec_preproc) # Step 8 - Create a T2 brain mask of preprocessed DWI data bet2_prefix = os.path.join(outdir, "nodif_brain") nodif_brain = os.path.join(bet2_prefix + ".nii.gz") nodif_brain_mask = os.path.join(bet2_prefix + "_mask.nii.gz") bet2(dwi, bet2_prefix, f=0.25, m=True) # Step 9 - clean intermediate directories if requested if delete_steps: intermediate_directories = [raw_dwi_dir, rough_mask_dir, outliers_dir, eddy_motion_dir, brainsuite_dir] for directory in intermediate_directories: shutil.rmtree(directory) return (outdir, dwi_preproc, bval_preproc, bvec_preproc, nodif_brain, nodif_brain_mask)