예제 #1
0
def test_openmp_locks():

    static = []
    moving = []
    pts = 20

    for i in range(1000):
        s = np.random.rand(pts, 3)
        static.append(s)
        moving.append(s + 2)

    moving = moving[2:]

    points, offsets = unlist_streamlines(static)
    points2, offsets2 = unlist_streamlines(moving)

    D = np.zeros((len(offsets), len(offsets2)), dtype='f8')

    _bundle_minimum_distance_matrix(points, points2, len(offsets),
                                    len(offsets2), pts, D)

    dist1 = 0.25 * (np.sum(np.min(D, axis=0)) / float(D.shape[1]) +
                    np.sum(np.min(D, axis=1)) / float(D.shape[0]))**2

    dist2 = _bundle_minimum_distance(points, points2, len(offsets),
                                     len(offsets2), pts)

    assert_almost_equal(dist1, dist2, 6)
예제 #2
0
def test_openmp_locks():

    static = []
    moving = []
    pts = 20

    for i in range(1000):
        s = np.random.rand(pts, 3)
        static.append(s)
        moving.append(s + 2)

    moving = moving[2:]

    points, offsets = unlist_streamlines(static)
    points2, offsets2 = unlist_streamlines(moving)

    D = np.zeros((len(offsets), len(offsets2)), dtype='f8')

    _bundle_minimum_distance_matrix(points, points2,
                                    len(offsets), len(offsets2),
                                    pts, D)

    dist1 = 0.25 * (np.sum(np.min(D, axis=0)) / float(D.shape[1]) +
                    np.sum(np.min(D, axis=1)) / float(D.shape[0])) ** 2

    dist2 = _bundle_minimum_distance(points, points2,
                                     len(offsets), len(offsets2),
                                     pts)

    assert_almost_equal(dist1, dist2, 6)
예제 #3
0
def test_unlist_relist_streamlines():
    streamlines = [np.random.rand(10, 3), np.random.rand(20, 3), np.random.rand(5, 3)]
    points, offsets = unlist_streamlines(streamlines)
    assert_equal(offsets.dtype, np.dtype("i8"))
    assert_equal(points.shape, (35, 3))
    assert_equal(len(offsets), len(streamlines))

    streamlines2 = relist_streamlines(points, offsets)
    assert_equal(len(streamlines), len(streamlines2))
    for i in range(len(streamlines)):
        assert_array_equal(streamlines[i], streamlines2[i])
예제 #4
0
def test_unlist_relist_streamlines():
    streamlines = [np.random.rand(10, 3),
                   np.random.rand(20, 3),
                   np.random.rand(5, 3)]
    points, offsets = unlist_streamlines(streamlines)
    assert_equal(offsets.dtype, np.dtype('i8'))
    assert_equal(points.shape, (35, 3))
    assert_equal(len(offsets), len(streamlines))

    streamlines2 = relist_streamlines(points, offsets)
    assert_equal(len(streamlines), len(streamlines2))
    for i in range(len(streamlines)):
        assert_array_equal(streamlines[i], streamlines2[i])
예제 #5
0
def test_efficient_bmd():

    a = np.array([[1, 1, 1],
                  [2, 2, 2],
                  [3, 3, 3]])

    streamlines = [a, a + 2, a + 4]

    points, offsets = unlist_streamlines(streamlines)
    points = points.astype(np.double)
    points2 = points.copy()

    D = np.zeros((len(offsets), len(offsets)), dtype='f8')

    _bundle_minimum_distance_matrix(points, points2,
                                    len(offsets), len(offsets),
                                    a.shape[0], D)

    assert_equal(np.sum(np.diag(D)), 0)

    points2 += 2

    _bundle_minimum_distance_matrix(points, points2,
                                    len(offsets), len(offsets),
                                    a.shape[0], D)

    streamlines2 = relist_streamlines(points2, offsets)
    D2 = distance_matrix_mdf(streamlines, streamlines2)

    assert_array_almost_equal(D, D2)

    cols = D2.shape[1]
    rows = D2.shape[0]

    dist = 0.25 * (np.sum(np.min(D2, axis=0)) / float(cols) +
                   np.sum(np.min(D2, axis=1)) / float(rows)) ** 2

    dist2 = _bundle_minimum_distance(points, points2,
                                     len(offsets), len(offsets),
                                     a.shape[0])
    assert_almost_equal(dist, dist2)
예제 #6
0
def write_ply(fname,
              data,
              comments=['DTI Tractography, produced by fiber-track']):
    """
    Write bundles to ply file
    :param fname:
    :param data:
    :param comments:
    :return:
    """
    points, idx = unlist_streamlines(data)
    with open(fname, 'w') as f:
        f.write('ply\nformat ascii 1.0\n')
        for com in comments:
            f.write('comment ' + com + '\n')
        f.write('element vertices ' + str(len(points)))
        f.write('\nproperty float x\nproperty float y\nproperty float z\n')
        f.write('element fiber {}\nproperty int endindex'.format(len(idx)))
        f.write('\nend_header\n')
        for vert in points:
            f.write(
                str(vert[0]) + ' ' + str(vert[1]) + ' ' + str(vert[2]) + '\n')
        for i in idx:
            f.write(str(i) + '\n')
예제 #7
0
 def _set_moving(self, moving):
     self.moving_centered_pts, _ = unlist_streamlines(moving)
예제 #8
0
 def _set_static(self, static):
     static_centered_pts, st_idx = unlist_streamlines(static)
     self.static_centered_pts = np.ascontiguousarray(static_centered_pts,
                                                     dtype=np.float64)
     self.block_size = st_idx[0]
예제 #9
0
 def _set_moving(self, moving):
     self.moving_centered_pts, _ = unlist_streamlines(moving)
예제 #10
0
 def _set_static(self, static):
     static_centered_pts, st_idx = unlist_streamlines(static)
     self.static_centered_pts = np.ascontiguousarray(static_centered_pts,
                                                     dtype=np.float64)
     self.block_size = st_idx[0]