def test_reorient_img(): """ Test reorient_img functionality """ base_dir = str(Path(__file__).parent / "examples") test_dir = f"{base_dir}/003/test_out/test_reorient_img" # X axis increasing right to left (Radiological) img_in_radio = f"{test_dir}/sub-003_T1w_LAS.nii.gz" out_radio_dir = f"{test_dir}/output_LAS" # X axis increase from left to right (Neurological) img_in_neuro = f"{test_dir}/sub-003_T1w_RAS.nii.gz" out_neuro_dir = f"{test_dir}/output_RAS" # Outputs should be in neurological orientation. LAStoRAS_img_out = reg_utils.reorient_img(img_in_radio, out_radio_dir) RAStoRAS_img_out = reg_utils.reorient_img(img_in_neuro, out_neuro_dir) # Original RAS data orig_RAS_img = nib.load(img_in_neuro) # Output from LAS input LAStoRAS_img = nib.load(LAStoRAS_img_out) # Output from RAS input RAStoRAS_img = nib.load(RAStoRAS_img_out) # Assert that arrays are equal check_LAS_input = np.allclose(LAStoRAS_img.affine.astype('int'), orig_RAS_img.affine.astype('int')) check_RAS_input = np.allclose(RAStoRAS_img.affine.astype('int'), orig_RAS_img.affine.astype('int')) check_both_outputs = np.allclose(LAStoRAS_img.affine.astype('int'), RAStoRAS_img.affine.astype('int')) assert check_LAS_input is True assert check_RAS_input is True assert check_both_outputs is True
def check_orient_and_dims(infile, vox_size, bvecs=None): """ An API to reorient any image to RAS+ and resample any image to a given voxel resolution. Parameters ---------- infile : str File path to a dwi Nifti1Image. vox_size : str Voxel size in mm. (e.g. 2mm). bvecs : str File path to corresponding bvecs file if infile is a dwi. Returns ------- outfile : str File path to the reoriented and/or resample Nifti1Image. bvecs : str File path to corresponding reoriented bvecs file if outfile is a dwi. """ import os import os.path as op from pynets.registration.reg_utils import reorient_dwi, reorient_img, match_target_vox_res outdir = op.dirname(infile) img = nib.load(infile) vols = img.shape[-1] reoriented = "%s%s%s%s" % (outdir, '/', infile.split('/')[-1].split('.nii.gz')[0], '_pre_reor.nii.gz') resampled = "%s%s%s%s" % (outdir, '/', os.path.basename(infile).split('.nii.gz')[0], '_pre_res.nii.gz') # Check orientation if (vols > 1) and (bvecs is not None): # dwi case outdir = "%s%s" % (outdir, '/std_dmri') if not os.path.isdir(outdir): os.mkdir(outdir) # Check orientation if not os.path.isfile(reoriented): [infile, bvecs] = reorient_dwi(infile, bvecs, outdir) # Check dimensions if not os.path.isfile(resampled): outfile = match_target_vox_res(infile, vox_size, outdir, sens='dwi') elif (vols > 1) and (bvecs is None): # func case outdir = "%s%s" % (outdir, '/std_fmri') if not os.path.isdir(outdir): os.mkdir(outdir) # Check orientation if not os.path.isfile(reoriented): infile = reorient_img(infile, outdir) # Check dimensions if not os.path.isfile(resampled): outfile = match_target_vox_res(infile, vox_size, outdir, sens='func') else: # t1w case outdir = "%s%s" % (outdir, '/std_anat_') if not os.path.isdir(outdir): os.mkdir(outdir) # Check orientation if not os.path.isfile(reoriented): infile = reorient_img(infile, outdir) if not os.path.isfile(resampled): # Check dimensions outfile = match_target_vox_res(infile, vox_size, outdir, sens='t1w') print(outfile) if bvecs is None: return outfile else: return outfile, bvecs
def check_orient_and_dims(infile, outdir, vox_size, bvecs=None, overwrite=True): """ An API to reorient any image to RAS+ and resample any image to a given voxel resolution. Parameters ---------- infile : str File path to a dwi Nifti1Image. outdir : str Path to base derivatives directory. vox_size : str Voxel size in mm. (e.g. 2mm). bvecs : str File path to corresponding bvecs file if infile is a dwi. overwrite : bool Boolean indicating whether to overwrite existing outputs. Default is True. Returns ------- outfile : str File path to the reoriented and/or resample Nifti1Image. bvecs : str File path to corresponding reoriented bvecs file if outfile is a dwi. """ from pynets.registration.reg_utils import ( reorient_dwi, reorient_img, match_target_vox_res, ) img = nib.load(infile) vols = img.shape[-1] # Check orientation if (vols > 1) and (bvecs is not None): # dwi case # Check orientation if ("reor-RAS" not in infile) or (overwrite is True): [infile, bvecs] = reorient_dwi(infile, bvecs, outdir, overwrite=overwrite) # Check dimensions if ("res-" not in infile) or (overwrite is True): outfile = match_target_vox_res(infile, vox_size, outdir, overwrite=overwrite) print(outfile) else: outfile = infile elif (vols > 1) and (bvecs is None): # func case # Check orientation if ("reor-RAS" not in infile) or (overwrite is True): infile = reorient_img(infile, outdir, overwrite=overwrite) # Check dimensions if ("res-" not in infile) or (overwrite is True): outfile = match_target_vox_res(infile, vox_size, outdir, overwrite=overwrite) print(outfile) else: outfile = infile else: # t1w case # Check orientation if ("reor-RAS" not in infile) or (overwrite is True): infile = reorient_img(infile, outdir, overwrite=overwrite) # Check dimensions if ("res-" not in infile) or (overwrite is True): outfile = match_target_vox_res(infile, vox_size, outdir, overwrite=overwrite) print(outfile) else: outfile = infile if bvecs is None: return outfile else: return outfile, bvecs