Exemple #1
0
def test_reorient_dwi():
    """
    Test reorient_dwi functionality
    """
    base_dir = os.path.abspath(
        pkg_resources.resource_filename("pynets", "../data/examples"))
    test_dir = f"{base_dir}/003/test_out/test_reorient_dwi"

    # iso_eddy_corrected_data_denoised_LAS.nii.gz was the original image in
    # radiological orientation. fslswapdim and fslorient manually used to
    # create RAS image. This test attempts to convert RAS image back to LAS.
    # Confirms by checking output array is equal to origal LAS image array.

    dwi_prep_rad = f"{test_dir}/iso_eddy_corrected_data_denoised_LAS.nii.gz"
    dwi_prep_neu = f"{test_dir}/iso_eddy_corrected_data_denoised_RAS.nii.gz"
    bvecs_orig = f"{test_dir}/bvec.bvec"
    out_dir = f"{test_dir}/output"

    dwi_prep_out, bvecs_out = utils.reorient_dwi(dwi_prep_neu, bvecs_orig,
                                                 out_dir)

    orig_rad = nib.load(dwi_prep_rad)
    orig_rad_data = orig_rad.get_data()

    reorient_rad = nib.load(dwi_prep_out)
    reorient_rad_data = reorient_rad.get_data()

    reorient_check = np.array_equal(orig_rad_data, reorient_rad_data)
    bvec_check = np.array_equal(bvecs_orig, bvecs_out)

    assert bvec_check is False
    assert reorient_check is True
Exemple #2
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 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.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