示例#1
0
def test_mesh_laplacian():
    td = get_testdir()
    s = Surface(op.join(td, 'in.surf.gii'))

    try:
        s.mesh_laplacian(-1)
    except Exception as e:
        assert isinstance(
            e, ValueError), 'negative distance weight should give ValueError'

    for w in range(4):
        lap = s.mesh_laplacian(distance_weight=w)
        assert (lap[np.diag_indices(lap.shape[0])] <
                0).min(), 'positive diagonal'

    outs = Surface(op.join(td, 'out.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    hemi = toblerone.Hemisphere(s, outs, 'L')
    hemi2 = toblerone.Hemisphere(s, outs, 'R')
    proj = toblerone.projection.Projector([hemi, hemi2], spc)

    for w in range(4):
        lap = proj.mesh_laplacian(w)
        n = proj.hemi_dict['L'].n_points
        assert not slice_sparse(lap, slice(0, n), slice(n, 2 * n)).nnz
        assert not slice_sparse(lap, slice(n, 2 * n), slice(0, n)).nnz
        assert not (lap[diag_indices(2 * n)] > 0).any()
示例#2
0
def test_structure():
    td = get_testdir()
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    ins = Surface(op.join(td, 'in.surf.gii'), name='L')
    s2r = np.identity(4)

    fracs = pvestimation.structure(surf=op.join(td, 'in.surf.gii'),
                                   ref=spc,
                                   struct2ref=s2r,
                                   cores=1,
                                   flirt=True,
                                   coords='fsl',
                                   struct=op.join(td, 'ref.nii.gz'))

    superfactor = 10
    spc_high = spc.resize_voxels(1.0 / superfactor)
    voxelised = np.zeros(spc_high.size.prod(), dtype=NP_FLOAT)
    ins.index_on(spc_high)
    ins.indexed.voxelised = ins.voxelise(spc_high, 1)

    reindex_in = ins.reindexing_filter(spc_high)
    voxelised[reindex_in[1]] = (
        ins.indexed.voxelised[reindex_in[0]]).astype(NP_FLOAT)

    voxelised = voxelised.reshape(spc_high.size)
    truth = sum_array_blocks(voxelised, 3 * [superfactor]) / superfactor**3

    np.testing.assert_array_almost_equal(fracs, truth, 2)
示例#3
0
def test_discriminated_laplacian():
    td = get_testdir()
    s = Surface(op.join(td, 'in.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))

    for w in range(4):
        lap = s.discriminated_laplacian(spc, distance_weight=w, in_weight=100)
        assert (lap[np.diag_indices(lap.shape[0])] <
                0).min(), 'positive diagonal'
示例#4
0
def test_transform_projector():
    td = get_testdir()
    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    proj = toblerone.Projector(hemi, spc)

    # proj = toblerone.Projector.load('temp.h5')
    trans = np.eye(4)
    trans[:3, 3] = 0.4 * np.random.rand(3)
    proj2 = proj.transform(trans, spc)

    assert (proj.surf2vol_matrix(False) !=
            proj2.surf2vol_matrix(False)).A.any()
示例#5
0
def test_projector_partial_fov():
    td = get_testdir()
    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    spc = spc.resize([1, 1, 1], spc.size - 2)
    projector = toblerone.projection.Projector(hemi, spc, cores=8)
    sdata = np.ones(hemi.inSurf.n_points, dtype=NP_FLOAT)
    vdata = np.ones(spc.size.prod(), dtype=NP_FLOAT)
    # ndata = np.concatenate((sdata, vdata))
    # proj = projector.surf2vol(sdata, True)

    adj = projector.adjacency_matrix()
    lap = projector.discriminated_laplacian(1, 100)
示例#6
0
def test_projection():
    td = get_testdir()
    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    sdata = np.ones(hemi.inSurf.n_points, dtype=NP_FLOAT)
    vdata = np.ones(spc.size.prod(), dtype=NP_FLOAT)
    ndata = np.concatenate((sdata, vdata))
    projector = toblerone.projection.Projector(hemi, spc, 10)

    # volume to surface
    v2s = projector.vol2surf(vdata, False)
    v2s_edge = projector.vol2surf(vdata, True)
    assert (v2s <= v2s_edge).all(), "edge correction did not increase signal"
    v2s = projector.vol2surf_matrix(False)
    v2s_edge = projector.vol2surf_matrix(True)
    assert (v2s_edge.data >=
            v2s.data).all(), 'egde correction: some weights should increase'

    # surface to volume
    s2v = projector.surf2vol(sdata, False)
    s2v_pv = projector.surf2vol(sdata, True)
    assert (s2v_pv <= s2v).all(), "pv weighting did not reduce signal"
    s2v = projector.surf2vol_matrix(False)
    assert (s2v.sum(1).max() - 1) < 1e-6, 'total voxel weight > 1'
    s2v_pv = projector.surf2vol_matrix(False)
    assert (s2v_pv.data <=
            s2v.data).all(), 'pv weighting should reduce voxel weights'

    # volume to node
    v2n = projector.vol2node(vdata, False)
    v2n_edge = projector.vol2node(vdata, True)
    assert (v2n <= v2n_edge).all(), "edge correction did not increase signal"
    v2n = projector.vol2node_matrix(False)
    assert (v2n.sum(1).max() - 1) < 1e-6, 'total node weight > 1'
    v2n_edge = projector.vol2node_matrix(True)
    assert (v2n_edge.sum(1).max() -
            1) > 1e-6, 'edge correction: node should have weight > 1'

    # node to volume
    n2v = projector.node2vol(ndata, False)
    n2v_pv = projector.node2vol(ndata, True)
    assert (n2v_pv <= n2v).all(), "pv weighting did not reduce signal"
    n2v = projector.node2vol_matrix(False)
    assert (n2v.sum(1).max() - 1) < 1e-6, 'total voxel weight > 1'
    n2v_pv = projector.node2vol_matrix(True)
    assert (n2v.sum(1).max() - 1) < 1e-6, 'total voxel weight > 1'
示例#7
0
def test_indexing():
    td = get_testdir()
    surf = toblerone.Surface(op.join(td, 'out.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    surf.index_on(spc, 1)
    surf.indexed.voxelised = surf.voxelise(spc, 1)

    truth = pickle.load(open(op.join(td, 'out_indexed.pkl'), 'rb'))
    truthspace = truth.indexed.space
    space = surf.indexed.space
    assocs = surf.indexed.assocs
    assert (np.array_equal(truth.indexed.assocs.indices, assocs.indices)
            and (np.array_equal(truth.indexed.assocs.data, assocs.data)))
    assert np.all(space.bbox_origin == truthspace.bbox_origin)
    assert np.all(space.size == truthspace.size)
    assert np.all(space.offset == truthspace.offset)
    assert np.array_equal(surf.indexed.voxelised, truth.indexed.voxelised)
示例#8
0
def test_projector_hdf5():
    td = get_testdir()
    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    proj = toblerone.Projector(hemi, spc)
    proj.save('proj.h5')
    proj2 = toblerone.Projector.load('proj.h5')

    assert np.array_equiv(proj.pvs, proj2.pvs)
    assert np.array_equiv(proj.spc, proj2.spc)
    assert np.array_equiv(proj.vox_tri_mats[0].data,
                          proj2.vox_tri_mats[0].data)
    assert np.array_equiv(proj.vtx_tri_mats[0].data,
                          proj2.vtx_tri_mats[0].data)

    os.remove('proj.h5')
示例#9
0
def test_projector_rois():
    td = get_testdir()
    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    fracs = nibabel.load(op.join(td, 'sph_fractions.nii.gz')).get_fdata()
    hemi.pvs = fracs.reshape(-1, 3)
    puta = Surface.manual(0.25 * hemi.inSurf.points, hemi.inSurf.tris,
                          'L_Puta')
    rois = {'L_Puta': puta}
    proj = toblerone.projection.Projector(hemi, spc, rois=rois, cores=8)

    ndata = np.ones(proj.n_nodes)
    ndata[-1] = 2
    vdata = proj.node2vol(ndata, True)
    spc.save_image(vdata, 'n2v.nii.gz')

    ndata = proj.vol2node(vdata, True)
    print(ndata)
示例#10
0
def test_proj_properties():
    td = get_testdir()
    ins = Surface(op.join(td, 'in.surf.gii'))
    outs = Surface(op.join(td, 'out.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    hemi = toblerone.Hemisphere(ins, outs, 'L')
    proj = toblerone.projection.Projector(hemi, spc)
    assert proj.n_hemis == 1
    assert 'L' in proj.hemi_dict
    assert hemi.midsurface()
    assert proj['LPS']

    hemi2 = toblerone.Hemisphere(ins, outs, 'R')
    proj = toblerone.projection.Projector([hemi, hemi2], spc)
    assert proj.n_hemis == 2
    assert proj['RWS']
    assert ('L' in proj.hemi_dict) & ('R' in proj.hemi_dict)
    for h, s in zip(proj.iter_hemis, ['L', 'R']):
        assert h.side == s

    assert proj.n_surf_points == 2 * ins.n_points
示例#11
0
def test_enclosing_space():
    td = get_testdir()
    surf = toblerone.Surface(op.join(td, 'out.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    surf.index_on(spc, 1)
    assert utils.space_encloses_surface(surf.indexed.space,
                                        surf.indexed.points_vox)

    spc2 = spc.resize([5, 5, 5], [5, 5, 5])
    surf2 = toblerone.Surface(op.join(td, 'out.surf.gii'))
    surf2.index_on(spc2, 1)
    assert not utils.space_encloses_surface(spc2, surf2.indexed.points_vox)
    assert surf.indexed.space == surf2.indexed.space

    surf = toblerone.Surface(op.join(td, 'out.surf.gii'))
    start = surf.points.min(0)
    end = surf.points.max(0)
    vsize = (start - end) / 10
    spc = toblerone.ImageSpace.create_axis_aligned(start, end, vsize)
    surf.index_on(spc, 1)
    assert utils.space_encloses_surface(surf.indexed.space,
                                        surf.indexed.points_vox)
示例#12
0
def test_cortex():
    td = get_testdir()
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))

    ins = op.join(td, 'in.surf.gii')
    outs = op.join(td, 'out.surf.gii')
    hemi = Hemisphere(ins, outs, 'L')
    s2r = np.identity(4)
    supersampler = np.random.randint(3, 6, 3)
    fracs = estimators._cortex(hemi, spc, s2r, supersampler, 8, False)
    spc.save_image(fracs, f'{td}/fracs.nii.gz')

    # REFRESH the surfaces of the hemisphere before starting again - indexing!
    hemi = Hemisphere(ins, outs, 'L')
    superfactor = 10
    spc_high = spc.resize_voxels(1.0 / superfactor)
    voxelised = np.zeros(spc_high.size.prod(), dtype=NP_FLOAT)

    hemi.inSurf.index_on(spc_high)
    hemi.inSurf.indexed.voxelised = hemi.inSurf.voxelise(spc_high, 1)

    reindex_in = hemi.inSurf.reindexing_filter(spc_high)
    voxelised[reindex_in[1]] = -(
        hemi.inSurf.indexed.voxelised[reindex_in[0]]).astype(NP_FLOAT)

    hemi.outSurf.index_on(spc_high)
    hemi.outSurf.indexed.voxelised = hemi.outSurf.voxelise(spc_high, 1)
    reindex_out = hemi.outSurf.reindexing_filter(spc_high)
    voxelised[reindex_out[1]] += hemi.outSurf.indexed.voxelised[reindex_out[0]]

    voxelised = voxelised.reshape(spc_high.size)
    truth = sum_array_blocks(voxelised, 3 * [superfactor]) / superfactor**3
    spc.save_image(truth, f'{td}/truth.nii.gz')

    # truth = np.squeeze(nibabel.load(op.join(td, 'truth.nii.gz')).get_fdata())
    np.testing.assert_array_almost_equal(fracs[..., 0], truth, 2)
示例#13
0
def test_adjacency():
    td = get_testdir()
    s = Surface(op.join(td, 'in.surf.gii'))
    for w in range(4):
        adj = s.adjacency_matrix(w)
        assert not (adj.data < 0).any(), 'negative value in adjacency matrix'

    try:
        s.adjacency_matrix(-1)
    except Exception as e:
        assert isinstance(
            e, ValueError), 'negative distance weight should give ValueError'

    outs = Surface(op.join(td, 'out.surf.gii'))
    spc = toblerone.ImageSpace(op.join(td, 'ref.nii.gz'))
    hemi = toblerone.Hemisphere(s, outs, 'L')
    hemi2 = toblerone.Hemisphere(s, outs, 'R')
    proj = toblerone.projection.Projector([hemi, hemi2], spc)
    n = proj.hemi_dict['L'].n_points

    for w in range(4):
        adj = proj.adjacency_matrix(w)
        assert not slice_sparse(adj, slice(0, n), slice(n, 2 * n)).nnz
        assert not slice_sparse(adj, slice(n, 2 * n), slice(0, n)).nnz