/usr/lib/python2.7/dist-packages/dipy/tracking/local/tests/test_tissue_classifier.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import numpy as np
import numpy.testing as npt
import scipy.ndimage
from dipy.core.ndindex import ndindex
from dipy.tracking.local import (BinaryTissueClassifier,
ThresholdTissueClassifier,
ActTissueClassifier)
from dipy.tracking.local.localtracking import TissueTypes
def test_binary_tissue_classifier():
"""This tests that the binary tissue classifier returns expected
tissue types.
"""
mask = np.random.random((4, 4, 4))
mask[mask < 0.4] = 0.0
btc_boolean = BinaryTissueClassifier(mask > 0)
btc_float64 = BinaryTissueClassifier(mask)
# test voxel center
for ind in ndindex(mask.shape):
pts = np.array(ind, dtype='float64')
state_boolean = btc_boolean.check_point(pts)
state_float64 = btc_float64.check_point(pts)
if mask[ind] > 0:
npt.assert_equal(state_boolean, TissueTypes.TRACKPOINT)
npt.assert_equal(state_float64, TissueTypes.TRACKPOINT)
else:
npt.assert_equal(state_boolean, TissueTypes.ENDPOINT)
npt.assert_equal(state_float64, TissueTypes.ENDPOINT)
# test random points in voxel
for ind in ndindex(mask.shape):
for _ in range(50):
pts = np.array(ind, dtype='float64') + np.random.random(3) - 0.5
state_boolean = btc_boolean.check_point(pts)
state_float64 = btc_float64.check_point(pts)
if mask[ind] > 0:
npt.assert_equal(state_boolean, TissueTypes.TRACKPOINT)
npt.assert_equal(state_float64, TissueTypes.TRACKPOINT)
else:
npt.assert_equal(state_boolean, TissueTypes.ENDPOINT)
npt.assert_equal(state_float64, TissueTypes.ENDPOINT)
# test outside points
outside_pts = [[100, 100, 100], [0, -1, 1], [0, 10, 2],
[0, 0.5, -0.51], [0, -0.51, 0.1]]
for pts in outside_pts:
pts = np.array(pts, dtype='float64')
state_boolean = btc_boolean.check_point(pts)
state_float64 = btc_float64.check_point(pts)
npt.assert_equal(state_boolean, TissueTypes.OUTSIDEIMAGE)
npt.assert_equal(state_float64, TissueTypes.OUTSIDEIMAGE)
def test_threshold_tissue_classifier():
"""This tests that the thresholdy tissue classifier returns expected
tissue types.
"""
tissue_map = np.random.random((4, 4, 4))
ttc = ThresholdTissueClassifier(tissue_map.astype('float32'), 0.5)
# test voxel center
for ind in ndindex(tissue_map.shape):
pts = np.array(ind, dtype='float64')
state = ttc.check_point(pts)
if tissue_map[ind] > 0.5:
npt.assert_equal(state, TissueTypes.TRACKPOINT)
else:
npt.assert_equal(state, TissueTypes.ENDPOINT)
# test random points in voxel
inds = [[0, 1.4, 2.2], [0, 2.3, 2.3], [0, 2.2, 1.3], [0, 0.9, 2.2],
[0, 2.8, 1.1], [0, 1.1, 3.3], [0, 2.1, 1.9], [0, 3.1, 3.1],
[0, 0.1, 0.1], [0, 0.9, 0.5], [0, 0.9, 0.5], [0, 2.9, 0.1]]
for pts in inds:
pts = np.array(pts, dtype='float64')
state = ttc.check_point(pts)
res = scipy.ndimage.map_coordinates(
tissue_map, np.reshape(pts, (3, 1)), order=1, mode='nearest')
if res > 0.5:
npt.assert_equal(state, TissueTypes.TRACKPOINT)
else:
npt.assert_equal(state, TissueTypes.ENDPOINT)
# test outside points
outside_pts = [[100, 100, 100], [0, -1, 1], [0, 10, 2],
[0, 0.5, -0.51], [0, -0.51, 0.1]]
for pts in outside_pts:
pts = np.array(pts, dtype='float64')
state = ttc.check_point(pts)
npt.assert_equal(state, TissueTypes.OUTSIDEIMAGE)
def test_act_tissue_classifier():
"""This tests that the act tissue classifier returns expected
tissue types.
"""
gm = np.random.random((4, 4, 4))
wm = np.random.random((4, 4, 4))
csf = np.random.random((4, 4, 4))
tissue_sum = gm + wm + csf
gm /= tissue_sum
wm /= tissue_sum
csf /= tissue_sum
act_tc = ActTissueClassifier(include_map=gm, exclude_map=csf)
# test voxel center
for ind in ndindex(wm.shape):
pts = np.array(ind, dtype='float64')
state = act_tc.check_point(pts)
if csf[ind] > 0.5:
npt.assert_equal(state, TissueTypes.INVALIDPOINT)
elif gm[ind] > 0.5:
npt.assert_equal(state, TissueTypes.ENDPOINT)
else:
npt.assert_equal(state, TissueTypes.TRACKPOINT)
# test random points in voxel
inds = [[0, 1.4, 2.2], [0, 2.3, 2.3], [0, 2.2, 1.3], [0, 0.9, 2.2],
[0, 2.8, 1.1], [0, 1.1, 3.3], [0, 2.1, 1.9], [0, 3.1, 3.1],
[0, 0.1, 0.1], [0, 0.9, 0.5], [0, 0.9, 0.5], [0, 2.9, 0.1]]
for pts in inds:
pts = np.array(pts, dtype='float64')
state = act_tc.check_point(pts)
gm_res = scipy.ndimage.map_coordinates(
gm, np.reshape(pts, (3, 1)), order=1, mode='nearest')
csf_res = scipy.ndimage.map_coordinates(
csf, np.reshape(pts, (3, 1)), order=1, mode='nearest')
if csf_res > 0.5:
npt.assert_equal(state, TissueTypes.INVALIDPOINT)
elif gm_res > 0.5:
npt.assert_equal(state, TissueTypes.ENDPOINT)
else:
npt.assert_equal(state, TissueTypes.TRACKPOINT)
# test outside points
outside_pts = [[100, 100, 100], [0, -1, 1], [0, 10, 2],
[0, 0.5, -0.51], [0, -0.51, 0.1]]
for pts in outside_pts:
pts = np.array(pts, dtype='float64')
state = act_tc.check_point(pts)
npt.assert_equal(state, TissueTypes.OUTSIDEIMAGE)
if __name__ == '__main__':
run_module_suite()
|