Пример #1
0
def test_reorient_img():
    """
    Test reorient_img functionality
    """
    base_dir = os.path.abspath(
        pkg_resources.resource_filename("pynets", "../data/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 = utils.reorient_img(img_in_radio, out_radio_dir)
    RAStoRAS_img_out = 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
Пример #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