Esempio n. 1
0
def test_orientation_from_to_string():
    ras = np.array(((0, 1), (1, 1), (2, 1)))
    lps = np.array(((0, -1), (1, -1), (2, 1)))
    asl = np.array(((1, 1), (2, 1), (0, -1)))
    assert_array_equal(orientation_from_string('ras'), ras)
    assert_array_equal(orientation_from_string('lps'), lps)
    assert_array_equal(orientation_from_string('asl'), asl)
    assert_raises(ValueError, orientation_from_string, 'aasl')

    assert orientation_to_string(ras) == 'ras'
    assert orientation_to_string(lps) == 'lps'
    assert orientation_to_string(asl) == 'asl'
Esempio n. 2
0
def test_orientation_from_to_string():
    ras = np.array(((0,1), (1,1), (2,1)))
    lps = np.array(((0,-1), (1,-1), (2,1)))
    asl = np.array(((1,1), (2,1), (0,-1)))
    assert_array_equal(orientation_from_string('ras'), ras)
    assert_array_equal(orientation_from_string('lps'), lps)
    assert_array_equal(orientation_from_string('asl'), asl)
    assert_raises(ValueError, orientation_from_string, 'aasl')

    assert orientation_to_string(ras) == 'ras'
    assert orientation_to_string(lps) == 'lps'
    assert orientation_to_string(asl) == 'asl'
Esempio n. 3
0
def test_voxel_ornt():
    sh = (40, 40, 40)
    sz = (1, 2, 3)
    I4 = np.eye(4)

    ras = orientation_from_string('ras')
    sra = orientation_from_string('sra')
    lpi = orientation_from_string('lpi')
    srp = orientation_from_string('srp')

    affine = reorder_voxels_affine(ras, ras, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(sra, sra, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(lpi, lpi, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(srp, srp, sh, sz)
    assert_array_equal(affine, I4)

    streamlines = make_streamlines()
    box = np.array(sh) * sz

    sra_affine = reorder_voxels_affine(ras, sra, sh, sz)
    toras_affine = reorder_voxels_affine(sra, ras, sh, sz)
    assert_array_equal(np.dot(toras_affine, sra_affine), I4)
    expected_sl = (sl[:, [2, 0, 1]] for sl in streamlines)
    test_sl = move_streamlines(streamlines, sra_affine)
    for _ in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))

    lpi_affine = reorder_voxels_affine(ras, lpi, sh, sz)
    toras_affine = reorder_voxels_affine(lpi, ras, sh, sz)
    assert_array_equal(np.dot(toras_affine, lpi_affine), I4)
    expected_sl = (box - sl for sl in streamlines)
    test_sl = move_streamlines(streamlines, lpi_affine)
    for _ in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))

    srp_affine = reorder_voxels_affine(ras, srp, sh, sz)
    toras_affine = reorder_voxels_affine(srp, ras, (40, 40, 40), (3, 1, 2))
    assert_array_equal(np.dot(toras_affine, srp_affine), I4)
    expected_sl = [sl.copy() for sl in streamlines]
    for sl in expected_sl:
        sl[:, 1] = box[1] - sl[:, 1]
    expected_sl = (sl[:, [2, 0, 1]] for sl in expected_sl)
    test_sl = move_streamlines(streamlines, srp_affine)
    for _ in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))
Esempio n. 4
0
def test_voxel_ornt():
    sh = (40, 40, 40)
    sz = (1, 2, 3)
    I4 = np.eye(4)

    ras = orientation_from_string('ras')
    sra = orientation_from_string('sra')
    lpi = orientation_from_string('lpi')
    srp = orientation_from_string('srp')

    affine = reorder_voxels_affine(ras, ras, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(sra, sra, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(lpi, lpi, sh, sz)
    assert_array_equal(affine, I4)
    affine = reorder_voxels_affine(srp, srp, sh, sz)
    assert_array_equal(affine, I4)

    streamlines = make_streamlines()
    box = np.array(sh)*sz

    sra_affine = reorder_voxels_affine(ras, sra, sh, sz)
    toras_affine = reorder_voxels_affine(sra, ras, sh, sz)
    assert_array_equal(np.dot(toras_affine, sra_affine), I4)
    expected_sl = (sl[:, [2, 0, 1]] for sl in streamlines)
    test_sl = move_streamlines(streamlines, sra_affine)
    for ii in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))

    lpi_affine = reorder_voxels_affine(ras, lpi, sh, sz)
    toras_affine = reorder_voxels_affine(lpi, ras, sh, sz)
    assert_array_equal(np.dot(toras_affine, lpi_affine), I4)
    expected_sl = (box - sl for sl in streamlines)
    test_sl = move_streamlines(streamlines, lpi_affine)
    for ii in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))

    srp_affine = reorder_voxels_affine(ras, srp, sh, sz)
    toras_affine = reorder_voxels_affine(srp, ras, (40, 40, 40), (3, 1, 2))
    assert_array_equal(np.dot(toras_affine, srp_affine), I4)
    expected_sl = [sl.copy() for sl in streamlines]
    for sl in expected_sl:
        sl[:, 1] = box[1] - sl[:, 1]
    expected_sl = (sl[:, [2, 0, 1]] for sl in expected_sl)
    test_sl = move_streamlines(streamlines, srp_affine)
    for ii in xrange(len(streamlines)):
        assert_array_equal(next(test_sl), next(expected_sl))
Esempio n. 5
0
def flexi_tvis_affine(sl_vox_order, grid_affine, dim, voxel_size):
    """ Computes the mapping from voxel indices to streamline points,
        reconciling streamlines and grids with different voxel orders

    Parameters
    ----------
    sl_vox_order : string of length 3
        a string that describes the voxel order of the streamlines (ex: LPS)
    grid_affine : array (4, 4),
        An affine matrix describing the current space of the grid in relation
        to RAS+ scanner space
    dim : tuple of length 3
        dimension of the grid
    voxel_size : array (3,0)
        voxel size of the grid

    Returns
    -------
    flexi_tvis_aff : this affine maps between a grid and a trackvis space
    """

    sl_ornt = orientation_from_string(str(sl_vox_order))
    grid_ornt = nib.io_orientation(grid_affine)
    reorder_grid = reorder_voxels_affine(
        grid_ornt, sl_ornt, np.array(dim)-1, np.array([1,1,1]))

    tvis_aff = affine_for_trackvis(voxel_size)

    flexi_tvis_aff = np.dot(tvis_aff, reorder_grid)

    return flexi_tvis_aff
Esempio n. 6
0
def flexi_tvis_affine(sl_vox_order, grid_affine, dim, voxel_size):
    """ Computes the mapping from voxel indices to streamline points,
        reconciling streamlines and grids with different voxel orders

    Parameters
    ----------
    sl_vox_order : string of length 3
        a string that describes the voxel order of the streamlines (ex: LPS)
    grid_affine : array (4, 4),
        An affine matrix describing the current space of the grid in relation
        to RAS+ scanner space
    dim : tuple of length 3
        dimension of the grid
    voxel_size : array (3,0)
        voxel size of the grid

    Returns
    -------
    flexi_tvis_aff : this affine maps between a grid and a trackvis space
    """

    sl_ornt = orientation_from_string(str(sl_vox_order))
    grid_ornt = nib.io_orientation(grid_affine)
    reorder_grid = reorder_voxels_affine(
        grid_ornt, sl_ornt, np.array(dim)-1, np.array([1,1,1]))

    tvis_aff = affine_for_trackvis(voxel_size)

    flexi_tvis_aff = np.dot(tvis_aff, reorder_grid)

    return flexi_tvis_aff