Exemple #1
0
def test_match_target_vox_res():
    """
    Test match_target_vox_res functionality
    """
    base_dir = str(Path(__file__).parent/"examples")
    test_out = f"{base_dir}/003/test_out/test_match_target_vox_res"

    # Orig anat input has isotropic (1x1x1mm) dimensions.
    anat_img_file = f"{test_out}/sub-003_T1w_pre_res_res-2mm.nii.gz"
    anat_vox_size = '2mm'
    anat_out_dir = test_out
    anat_img_file = reg_utils.match_target_vox_res(anat_img_file, anat_vox_size, anat_out_dir)
    anat_new_img = nib.load(anat_img_file)
    anat_dims = anat_new_img.header.get_zooms()
    anat_success = True
    for anat_dim in np.round(anat_dims[:3], 2):
        if anat_dim != 2:
            anat_success = False

    # Orig dMRI image has anisotropic (1.75x1.75x3mm) dimensions.
    dwi_img_file = f"{test_out}/sub-003_dwi_pre_res_res-1mm.nii.gz"
    dwi_vox_size = '1mm'
    dwi_out_dir = test_out
    dwi_img_file = reg_utils.match_target_vox_res(dwi_img_file, dwi_vox_size, dwi_out_dir)
    dwi_new_img = nib.load(dwi_img_file)
    dwi_dims = dwi_new_img.header.get_zooms()
    dwi_success = True
    for dwi_dim in np.round(dwi_dims[:3], 2):
        if dwi_dim != 1:
            dwi_success = False

    assert anat_img_file is not None
    assert anat_success is True
    assert dwi_img_file is not None
    assert dwi_success is True
Exemple #2
0
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
Exemple #3
0
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