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)))
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)))
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()
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()
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())
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())