Exemplo n.º 1
0
def test_sct_register_multimodal_with_softmask(tmp_path):
    """
    Verify that softmask is actually applied during registration.

    NB: For 'gaussian', ANTs binaries can't handle softmasks natively, so SCT should be applying
        the mask directly to the image. Related links:
            * https://github.com/ivadomed/pipeline-hemis/issues/3.
            * https://github.com/spinalcordtoolbox/spinalcordtoolbox/issues/3075
    """
    fname_mask = str(tmp_path / 'mask_t2.nii.gz')
    fname_t2 = sct_test_path('t2', 't2.nii.gz')
    fname_t1 = sct_test_path('t1', 't1w.nii.gz')
    fname_warp = str(tmp_path / "warp_t1w2t2.nii.gz")

    sct_create_mask.main([
        '-i', fname_t2, '-p',
        f"centerline,{sct_test_path('t2', 't2_centerline-manual.nii.gz')}",
        '-o', fname_mask, '-f', 'gaussian'
    ])
    sct_register_multimodal.main([
        '-i', fname_t1, '-d', fname_t2, '-dseg',
        sct_test_path('t2', 't2_seg-manual.nii.gz'), '-param',
        "step=1,type=im,algo=slicereg,metric=CC", '-m', fname_mask, '-ofolder',
        str(tmp_path), '-r', '0', '-v', '2'
    ])

    # If registration was successful, the warping field should be non-empty
    assert np.any(Image(fname_warp).data)
Exemplo n.º 2
0
def test_sct_register_multimodal_mask_files_exist(tmp_path):
    """
    Run the script without validating results.

    - TODO: Write a check that verifies the registration results.
    - TODO: Parametrize this test to add '-initwarpinv warp_anat2template.nii.gz',
            after the file is added to sct_testing_data:
            https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/3407#discussion_r646895013
    """
    fname_mask = str(tmp_path / 'mask_mt1.nii.gz')
    sct_create_mask.main([
        '-i',
        sct_test_path('mt', 'mt1.nii.gz'), '-p',
        f"centerline,{sct_test_path('mt', 'mt1_seg.nii.gz')}", '-size', '35mm',
        '-f', 'cylinder', '-o', fname_mask
    ])
    sct_register_multimodal.main([
        '-i',
        sct_dir_local_path('data/PAM50/template/', 'PAM50_t2.nii.gz'), '-iseg',
        sct_dir_local_path('data/PAM50/template/', 'PAM50_cord.nii.gz'), '-d',
        sct_test_path('mt', 'mt1.nii.gz'), '-dseg',
        sct_test_path('mt', 'mt1_seg.nii.gz'), '-param',
        'step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3',
        '-m', fname_mask, '-initwarp',
        sct_test_path('t2', 'warp_template2anat.nii.gz'), '-ofolder',
        str(tmp_path)
    ])

    for path in ["PAM50_t2_reg.nii.gz", "warp_PAM50_t22mt1.nii.gz"]:
        assert os.path.exists(tmp_path / path)

    # Because `-initwarp` was specified (but `-initwarpinv` wasn't) the dest->seg files should NOT exist
    for path in ["mt1_reg.nii.gz", "warp_mt12PAM50_t2.nii.gz"]:
        assert not os.path.exists(tmp_path / path)
Exemplo n.º 3
0
def register_data(im_src, im_dest, param_reg, path_copy_warp=None, rm_tmp=True):
    '''

    Parameters
    ----------
    im_src: class Image: source image
    im_dest: class Image: destination image
    param_reg: str: registration parameter
    path_copy_warp: path: path to copy the warping fields

    Returns: im_src_reg: class Image: source image registered on destination image
    -------

    '''
    # im_src and im_dest are already preprocessed (in theory: im_dest = mean_image)
    # binarize images to get seg
    im_src_seg = binarize(im_src, thr_min=1, thr_max=1)
    im_dest_seg = binarize(im_dest)
    # create tmp dir and go in it
    tmp_dir = tmp_create()
    curdir = os.getcwd()
    os.chdir(tmp_dir)
    # save image and seg
    fname_src = 'src.nii.gz'
    im_src.save(fname_src)
    fname_src_seg = 'src_seg.nii.gz'
    im_src_seg.save(fname_src_seg)
    fname_dest = 'dest.nii.gz'
    im_dest.save(fname_dest)
    fname_dest_seg = 'dest_seg.nii.gz'
    im_dest_seg.save(fname_dest_seg)
    # do registration using param_reg
    sct_register_multimodal.main(args=['-i', fname_src,
                                       '-d', fname_dest,
                                       '-iseg', fname_src_seg,
                                       '-dseg', fname_dest_seg,
                                       '-param', param_reg])

    # get registration result
    fname_src_reg = add_suffix(fname_src, '_reg')
    im_src_reg = Image(fname_src_reg)
    # get out of tmp dir
    os.chdir(curdir)

    # copy warping fields
    if path_copy_warp is not None and os.path.isdir(os.path.abspath(path_copy_warp)):
        path_copy_warp = os.path.abspath(path_copy_warp)
        file_src = extract_fname(fname_src)[1]
        file_dest = extract_fname(fname_dest)[1]
        fname_src2dest = 'warp_' + file_src + '2' + file_dest + '.nii.gz'
        fname_dest2src = 'warp_' + file_dest + '2' + file_src + '.nii.gz'
        copy(os.path.join(tmp_dir, fname_src2dest), path_copy_warp)
        copy(os.path.join(tmp_dir, fname_dest2src), path_copy_warp)

    if rm_tmp:
        # remove tmp dir
        rmtree(tmp_dir)
    # return res image
    return im_src_reg, fname_src2dest, fname_dest2src
Exemplo n.º 4
0
def test_sct_register_multimodal_mt0_image_data_within_threshold(
        use_seg, param, fname_gt, tmp_path):
    """Run the CLI script and verify that the output image data is close to a reference image (within threshold)."""
    fname_out_src = str(tmp_path / 'mt0_reg.nii.gz')
    fname_out_dest = str(tmp_path / 'mt0_reg_inv.nii.gz')
    fname_owarp = str(tmp_path / 'warp_mt02mt1.nii.gz')
    fname_owarpinv = str(tmp_path / 'warp_mt12mt0.nii.gz')

    argv = [
        '-i', 'mt/mt0.nii.gz', '-d', 'mt/mt1.nii.gz', '-x', 'linear', '-r',
        '0', '-param', param, '-o', fname_out_src, '-owarp', fname_owarp,
        '-owarpinv', fname_owarpinv
    ]
    seg_argv = ['-iseg', 'mt/mt0_seg.nii.gz', '-dseg', 'mt/mt1_seg.nii.gz']
    sct_register_multimodal.main(argv=(argv + seg_argv) if use_seg else argv)

    for f in [fname_out_src, fname_out_dest, fname_owarp, fname_owarpinv]:
        assert os.path.isfile(f)
Exemplo n.º 5
0
def test_sct_register_multimodal_mask_no_checks(tmp_path):
    """Run the script without validating results.

    TODO: Write a check that verifies the registration results as part of
    https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/3246."""
    fname_mask = str(tmp_path / 'mask_mt1.nii.gz')
    sct_create_mask.main([
        '-i',
        sct_test_path('mt', 'mt1.nii.gz'), '-p',
        f"centerline,{sct_test_path('mt', 'mt1_seg.nii.gz')}", '-size', '35mm',
        '-f', 'cylinder', '-o', fname_mask
    ])
    sct_register_multimodal.main([
        '-i',
        sct_dir_local_path('data/PAM50/template/', 'PAM50_t2.nii.gz'), '-iseg',
        sct_dir_local_path('data/PAM50/template/', 'PAM50_cord.nii.gz'), '-d',
        sct_test_path('mt', 'mt1.nii.gz'), '-dseg',
        sct_test_path('mt', 'mt1_seg.nii.gz'), '-param',
        'step=1,type=seg,algo=centermass:step=2,type=seg,algo=bsplinesyn,slicewise=1,iter=3',
        '-m', fname_mask, '-initwarp',
        sct_test_path('t2', 'warp_template2anat.nii.gz')
    ])