Ejemplo n.º 1
0
def test_projection_matrix():
    mesh = flat_mesh(5, 7, 4)
    img = z_const_img(5, 7, 13)
    proj = surface._projection_matrix(
        mesh, np.eye(4), img.shape, radius=2., n_points=10)
    # proj matrix has shape (n_vertices, img_size)
    assert_equal(proj.shape, (5 * 7, 5 * 7 * 13))
    # proj.dot(img) should give the values of img at the vertices' locations
    values = proj.dot(img.ravel()).reshape((5, 7))
    assert_array_almost_equal(values, img[:, :, 0])
    mesh = flat_mesh(5, 7)
    proj = surface._projection_matrix(
        mesh, np.eye(4), (5, 7, 1), radius=.1, n_points=10)
    assert_array_almost_equal(proj.toarray(), np.eye(proj.shape[0]))
    mask = np.ones(img.shape, dtype=int)
    mask[0] = 0
    proj = surface._projection_matrix(
        mesh, np.eye(4), img.shape, radius=2., n_points=10, mask=mask)
    proj = proj.toarray()
    # first row of the mesh is masked
    assert_array_almost_equal(proj.sum(axis=1)[:7], np.zeros(7))
    assert_array_almost_equal(proj.sum(axis=1)[7:], np.ones(proj.shape[0] - 7))
    # mask and img should have the same shape
    assert_raises(ValueError, surface._projection_matrix,
                  mesh, np.eye(4), img.shape, mask=np.ones((3, 3, 2)))
Ejemplo n.º 2
0
def test_projection_matrix():
    mesh = flat_mesh(5, 7, 4)
    img = z_const_img(5, 7, 13)
    proj = surface._projection_matrix(
        mesh, np.eye(4), img.shape, radius=2., n_points=10)
    # proj matrix has shape (n_vertices, img_size)
    assert proj.shape == (5 * 7, 5 * 7 * 13)
    # proj.dot(img) should give the values of img at the vertices' locations
    values = proj.dot(img.ravel()).reshape((5, 7))
    assert_array_almost_equal(values, img[:, :, 0])
    mesh = flat_mesh(5, 7)
    proj = surface._projection_matrix(
        mesh, np.eye(4), (5, 7, 1), radius=.1, n_points=10)
    assert_array_almost_equal(proj.toarray(), np.eye(proj.shape[0]))
    mask = np.ones(img.shape, dtype=int)
    mask[0] = 0
    proj = surface._projection_matrix(
        mesh, np.eye(4), img.shape, radius=2., n_points=10, mask=mask)
    proj = proj.toarray()
    # first row of the mesh is masked
    assert_array_almost_equal(proj.sum(axis=1)[:7], np.zeros(7))
    assert_array_almost_equal(proj.sum(axis=1)[7:], np.ones(proj.shape[0] - 7))
    # mask and img should have the same shape
    pytest.raises(ValueError, surface._projection_matrix,
                  mesh, np.eye(4), img.shape, mask=np.ones((3, 3, 2)))
Ejemplo n.º 3
0
def test_sampling_between_surfaces(projection):
    projector = {"nearest": surface._nearest_voxel_sampling,
                 "linear": surface._interpolation_sampling}[projection]
    mesh = flat_mesh(13, 7, 3.)
    inner_mesh = flat_mesh(13, 7, 1)
    img = z_const_img(5, 7, 13).T
    projection = projector(
        [img], mesh, np.eye(4),
        kind="auto", n_points=100, inner_mesh=inner_mesh)
    assert_array_almost_equal(
        projection.ravel(), img[:, :, 1:4].mean(axis=-1).ravel())
Ejemplo n.º 4
0
def test_sample_locations():
    # check positions of samples on toy example, with an affine != identity
    # flat horizontal mesh
    mesh = flat_mesh(5, 7)
    affine = np.diagflat([10, 20, 30, 1])
    inv_affine = np.linalg.inv(affine)
    # transform vertices to world space
    vertices = np.asarray(
        resampling.coord_transform(*mesh[0].T, affine=affine)).T
    # compute by hand the true offsets in voxel space
    # (transformed by affine^-1)
    ball_offsets = surface._load_uniform_ball_cloud(10)
    ball_offsets = np.asarray(
        resampling.coord_transform(*ball_offsets.T, affine=inv_affine)).T
    line_offsets = np.zeros((10, 3))
    line_offsets[:, 2] = np.linspace(1, -1, 10)
    line_offsets = np.asarray(
        resampling.coord_transform(*line_offsets.T, affine=inv_affine)).T
    # check we get the same locations
    for kind, offsets in [('line', line_offsets), ('ball', ball_offsets)]:
        locations = surface._sample_locations(
            [vertices, mesh[1]], affine, 1., kind=kind, n_points=10)
        true_locations = np.asarray([vertex + offsets for vertex in mesh[0]])
        assert_array_equal(locations.shape, true_locations.shape)
        assert_array_almost_equal(true_locations, locations)
    pytest.raises(ValueError, surface._sample_locations,
                  mesh, affine, 1., kind='bad_kind')
Ejemplo n.º 5
0
def test_vertex_outer_normals():
    # compute normals for a flat horizontal mesh, they should all be (0, 0, 1)
    mesh = flat_mesh(5, 7)
    computed_normals = surface._vertex_outer_normals(mesh)
    true_normals = np.zeros((len(mesh[0]), 3))
    true_normals[:, 2] = 1
    assert_array_almost_equal(computed_normals, true_normals)
Ejemplo n.º 6
0
def test_sample_locations():
    # check positions of samples on toy example, with an affine != identity
    # flat horizontal mesh
    mesh = flat_mesh(5, 7)
    affine = np.diagflat([10, 20, 30, 1])
    inv_affine = np.linalg.inv(affine)
    # transform vertices to world space
    vertices = np.asarray(
        resampling.coord_transform(*mesh[0].T, affine=affine)).T
    # compute by hand the true offsets in voxel space
    # (transformed by affine^-1)
    ball_offsets = surface._load_uniform_ball_cloud(10)
    ball_offsets = np.asarray(
        resampling.coord_transform(*ball_offsets.T, affine=inv_affine)).T
    line_offsets = np.zeros((10, 3))
    line_offsets[:, 2] = np.linspace(-1, 1, 10)
    line_offsets = np.asarray(
        resampling.coord_transform(*line_offsets.T, affine=inv_affine)).T
    # check we get the same locations
    for kind, offsets in [('line', line_offsets), ('ball', ball_offsets)]:
        locations = surface._sample_locations(
            [vertices, mesh[1]], affine, 1., kind=kind, n_points=10)
        true_locations = np.asarray([vertex + offsets for vertex in mesh[0]])
        assert_array_equal(locations.shape, true_locations.shape)
        assert_array_almost_equal(true_locations, locations)
    assert_raises(ValueError, surface._sample_locations,
                  mesh, affine, 1., kind='bad_kind')
Ejemplo n.º 7
0
def test_vertex_outer_normals():
    # compute normals for a flat horizontal mesh, they should all be (0, 0, 1)
    mesh = flat_mesh(5, 7)
    computed_normals = surface._vertex_outer_normals(mesh)
    true_normals = np.zeros((len(mesh[0]), 3))
    true_normals[:, 2] = 1
    assert_array_almost_equal(computed_normals, true_normals)
Ejemplo n.º 8
0
def test_sampling():
    mesh = flat_mesh(5, 7, 4)
    img = z_const_img(5, 7, 13)
    mask = np.ones(img.shape, dtype=int)
    mask[0] = 0
    projectors = [
        surface._nearest_voxel_sampling, surface._interpolation_sampling
    ]
    for kind in ('line', 'ball'):
        for projector in projectors:
            projection = projector([img],
                                   mesh,
                                   np.eye(4),
                                   kind=kind,
                                   radius=0.)
            assert_array_almost_equal(projection.ravel(), img[:, :, 0].ravel())
            projection = projector([img],
                                   mesh,
                                   np.eye(4),
                                   kind=kind,
                                   radius=0.,
                                   mask=mask)
            assert_array_almost_equal(projection.ravel()[7:], img[1:, :,
                                                                  0].ravel())
            assert np.isnan(projection.ravel()[:7]).all()
Ejemplo n.º 9
0
def test_sampling(kind, use_inner_mesh, projection):
    mesh = flat_mesh(5, 7, 4)
    img = z_const_img(5, 7, 13)
    mask = np.ones(img.shape, dtype=int)
    mask[0] = 0
    projector = {
        "nearest": surface._nearest_voxel_sampling,
        "linear": surface._interpolation_sampling
    }[projection]
    inner_mesh = mesh if use_inner_mesh else None
    projection = projector([img],
                           mesh,
                           np.eye(4),
                           kind=kind,
                           radius=0.,
                           inner_mesh=inner_mesh)
    assert_array_almost_equal(projection.ravel(), img[:, :, 0].ravel())
    projection = projector([img],
                           mesh,
                           np.eye(4),
                           kind=kind,
                           radius=0.,
                           mask=mask,
                           inner_mesh=inner_mesh)
    assert_array_almost_equal(projection.ravel()[7:], img[1:, :, 0].ravel())
    assert np.isnan(projection.ravel()[:7]).all()
Ejemplo n.º 10
0
def test_sample_locations_depth(depth, n_points):
    mesh = flat_mesh(5, 7)
    radius = 8.
    locations = surface._sample_locations(
        mesh, np.eye(4), radius, n_points=n_points, depth=depth)
    offsets = np.asarray([[0., 0., - z * radius] for z in depth])
    expected = np.asarray([vertex + offsets for vertex in mesh[0]])
    assert np.allclose(locations, expected)
Ejemplo n.º 11
0
def test_sample_locations_between_surfaces(depth, n_points):
    inner = flat_mesh(5, 7)
    outer = inner[0] + [0., 0., 1.], inner[1]
    locations = surface._sample_locations_between_surfaces(
        outer, inner, np.eye(4), n_points=n_points, depth=depth)
    if depth is None:
        # can be simplified when we drop support for np 1.15
        # (broadcasting linspace)
        expected = np.asarray(
            [np.linspace(b, a, n_points)
             for (a, b) in zip(inner[0].ravel(), outer[0].ravel())])
        expected = np.rollaxis(
            expected.reshape((*outer[0].shape, n_points)), 2, 1)
    else:
        offsets = ([[0., 0., - z] for z in depth])
        expected = np.asarray([vertex + offsets for vertex in outer[0]])
    assert np.allclose(locations, expected)
Ejemplo n.º 12
0
def test_sampling():
    mesh = flat_mesh(5, 7, 4)
    img = z_const_img(5, 7, 13)
    mask = np.ones(img.shape, dtype=int)
    mask[0] = 0
    projectors = [surface._nearest_voxel_sampling,
                  surface._interpolation_sampling]
    for kind in ('line', 'ball'):
        for projector in projectors:
            projection = projector([img], mesh, np.eye(4),
                                   kind=kind, radius=0.)
            assert_array_almost_equal(projection.ravel(), img[:, :, 0].ravel())
            projection = projector([img], mesh, np.eye(4),
                                   kind=kind, radius=0., mask=mask)
            assert_array_almost_equal(projection.ravel()[7:],
                                      img[1:, :, 0].ravel())
            assert_true(np.isnan(projection.ravel()[:7]).all())