Esempio n. 1
0
def test_transfo_null():
    dir_tmp = "."

    print("Null warping field")

    print(" Create some recognizable data")
    data = np.ones((7, 8, 9), order="F")
    path_src = "warp-src.nii"
    img_src = fake_image_sct_custom(data).save(path_src)

    print(" Create a null warping field")
    data = np.zeros((7, 8, 9, 1, 3), order="F")
    path_warp = "warp-field.nii"
    img_warp = fake_image_sct_custom(data)
    img_warp.header.set_intent('vector', (), '')
    img_warp.save(path_warp)

    print(" Apply")
    path_dst = "warp-dst.nii"
    xform = sct_apply_transfo.Transform(input_filename=path_src,
                                        fname_dest=path_src,
                                        list_warp=[path_warp],
                                        output_filename=path_dst)
    xform.apply()

    img_src2 = msct_image.Image(path_src)
    img_dst = msct_image.Image(path_dst)

    print(" Check that the source wasn't altered")
    assert (img_src.data == img_src2.data).all()

    assert img_src.orientation == img_dst.orientation
    assert img_src.data.shape == img_dst.data.shape

    print(
        " Check that the destination is identical to the source since the field was null"
    )
    dat_src = img_src.data
    dat_dst = np.array(img_dst.data)
    for idx_slice, (slice_src, slice_dst) in enumerate(
            msct_image.SlicerMany((img_src, img_dst), msct_image.Slicer)):
        slice_src = np.array(slice_src)
        slice_dst = np.array(slice_dst)
        print(slice_src)
        print(slice_dst)
        assert np.allclose(slice_src, slice_dst)
    assert np.allclose(dat_src, dat_dst)
Esempio n. 2
0
def test_slicer(fake_3dimage_sct, fake_3dimage_sct2):
    im3d = fake_3dimage_sct.copy()
    slicer = msct_image.Slicer(im3d, "LPI")
    if 0:
        for im2d in slicer:
            print(im2d)
    assert slicer._nb_slices == 9
    assert 100 < np.mean(slicer[0]) < 200

    slicer = msct_image.Slicer(im3d, "LPS")
    if 0:
        for im2d in slicer:
            print(im2d)
    assert slicer._nb_slices == 9
    assert 900 < np.mean(slicer[0]) < 1000

    with pytest.raises(ValueError):
        slicer = msct_image.Slicer(im3d, "LI")

    with pytest.raises(ValueError):
        slicer = msct_image.Slicer(im3d, "L")

    with pytest.raises(ValueError):
        slicer = msct_image.Slicer(im3d, "Lx")

    with pytest.raises(ValueError):
        slicer = msct_image.Slicer(im3d, "LA")

    im3d2 = fake_3dimage_sct.copy()
    im3d2.data += 1000
    slicer = msct_image.SlicerMany((im3d, im3d2),
                                   msct_image.Slicer,
                                   orientation="LPI")
    for idx_slice, (im2d_a, im2d_b) in enumerate(slicer):
        assert np.all(im2d_b == im2d_a + 1000)

    with pytest.raises(ValueError):
        slicer = msct_image.SlicerMany((im3d, im3d2),
                                       msct_image.Slicer,
                                       orientation="IS")

    im3d2 = fake_3dimage_sct2.copy()
    with pytest.raises(ValueError):
        slicer = msct_image.SlicerMany((im3d, im3d2),
                                       msct_image.Slicer,
                                       orientation="LPI")

    with pytest.raises(ValueError):
        slicer = msct_image.Slicer(1)

    im3d = fake_3dimage_sct.copy()

    d = im3d.data.copy()

    slicer = msct_image.Slicer(im3d, "RPI")
    if 0:
        for im2d in slicer:
            print(im2d)
    slicer = msct_image.Slicer(im3d, "ASR")
    if 0:
        for im2d in slicer:
            print(im2d)

    msct_image.Slicer(im3d, "ASR")[0][0, 0] = -1

    for idx_slice, (slice2d_new, slice2d_old) in enumerate(
            msct_image.SlicerMany((im3d, fake_3dimage_sct.copy()),
                                  msct_image.Slicer,
                                  orientation="ASR")):
        if idx_slice == 0:
            assert slice2d_new[0, 0] == -1
            slice2d_new[0, 0] = slice2d_old[0, 0]
        assert (slice2d_new == slice2d_old).all()