本文整理匯總了Python中dipy.direction.ProbabilisticDirectionGetter.from_pmf方法的典型用法代碼示例。如果您正苦於以下問題:Python ProbabilisticDirectionGetter.from_pmf方法的具體用法?Python ProbabilisticDirectionGetter.from_pmf怎麽用?Python ProbabilisticDirectionGetter.from_pmf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dipy.direction.ProbabilisticDirectionGetter
的用法示例。
在下文中一共展示了ProbabilisticDirectionGetter.from_pmf方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_ProbabilisticDirectionGetter
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
def test_ProbabilisticDirectionGetter():
# Test the constructors and errors of the ProbabilisticDirectionGetter
class SillyModel(SphHarmModel):
sh_order = 4
def fit(self, data, mask=None):
coeff = np.zeros(data.shape[:-1] + (15,))
return SphHarmFit(self, coeff, mask=None)
model = SillyModel(gtab=None)
data = np.zeros((3, 3, 3, 7))
# Test if the tracking works on different dtype of the same data.
for dtype in [np.float32, np.float64]:
fit = model.fit(data.astype(dtype))
# Sample point and direction
point = np.zeros(3)
dir = unit_octahedron.vertices[0].copy()
# make a dg from a fit
dg = ProbabilisticDirectionGetter.from_shcoeff(fit.shm_coeff, 90,
unit_octahedron)
state = dg.get_direction(point, dir)
npt.assert_equal(state, 1)
# Make a dg from a pmf
N = unit_octahedron.theta.shape[0]
pmf = np.zeros((3, 3, 3, N))
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 90, unit_octahedron)
state = dg.get_direction(point, dir)
npt.assert_equal(state, 1)
# pmf shape must match sphere
bad_pmf = pmf[..., 1:]
npt.assert_raises(ValueError, ProbabilisticDirectionGetter.from_pmf,
bad_pmf, 90, unit_octahedron)
# pmf must have 4 dimensions
bad_pmf = pmf[0, ...]
npt.assert_raises(ValueError, ProbabilisticDirectionGetter.from_pmf,
bad_pmf, 90, unit_octahedron)
# pmf cannot have negative values
pmf[0, 0, 0, 0] = -1
npt.assert_raises(ValueError, ProbabilisticDirectionGetter.from_pmf,
pmf, 90, unit_octahedron)
# Check basis_type keyword
dg = ProbabilisticDirectionGetter.from_shcoeff(fit.shm_coeff, 90,
unit_octahedron,
basis_type="mrtrix")
npt.assert_raises(ValueError,
ProbabilisticDirectionGetter.from_shcoeff,
fit.shm_coeff, 90, unit_octahedron,
basis_type="not a basis")
示例2: LocalTracking
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
distribution of small fiber bundles within each voxel. We can use this
distribution for probabilistic fiber tracking. One way to do this is to
represent the FOD using a discrete sphere. This discrete FOD can be used by the
Probabilistic Direction Getter as a PMF for sampling tracking directions. We
need to clip the FOD to use it as a PMF because the latter cannot have negative
values. (Ideally the FOD should be strictly positive, but because of noise
and/or model failures sometimes it can have negative values).
"""
from dipy.direction import ProbabilisticDirectionGetter
from dipy.data import small_sphere
from dipy.io.trackvis import save_trk
fod = csd_fit.odf(small_sphere)
pmf = fod.clip(min=0)
prob_dg = ProbabilisticDirectionGetter.from_pmf(pmf, max_angle=30.,
sphere=small_sphere)
streamlines = LocalTracking(prob_dg, classifier, seeds, affine, step_size=.5)
save_trk("probabilistic_small_sphere.trk", streamlines, affine, labels.shape)
"""
One disadvantage of using a discrete PMF to represent possible tracking
directions is that it tends to take up a lot of memory (RAM). The size of the
PMF, the FOD in this case, must be equal to the number of possible tracking
directions on the hemisphere, and every voxel has a unique PMF. In this case
the data is ``(81, 106, 76)`` and ``small_sphere`` has 181 directions so the
FOD is ``(81, 106, 76, 181)``. One way to avoid sampling the PMF and holding it
in memory is to build the direction getter directly from the spherical harmonic
representation of the FOD. By using this approach, we can also use a larger
sphere, like ``default_sphere`` which has 362 directions on the hemisphere,
without having to worry about memory limitations.
"""
示例3: test_particle_filtering_tractography
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
def test_particle_filtering_tractography():
"""This tests that the ParticleFilteringTracking produces
more streamlines connecting the gray matter than LocalTracking.
"""
sphere = get_sphere('repulsion100')
step_size = 0.2
# Simple tissue masks
simple_wm = np.array([[0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]])
simple_wm = np.dstack([np.zeros(simple_wm.shape),
simple_wm,
simple_wm,
simple_wm,
np.zeros(simple_wm.shape)])
simple_gm = np.array([[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0]])
simple_gm = np.dstack([np.zeros(simple_gm.shape),
simple_gm,
simple_gm,
simple_gm,
np.zeros(simple_gm.shape)])
simple_csf = np.ones(simple_wm.shape) - simple_wm - simple_gm
tc = ActTissueClassifier.from_pve(simple_wm, simple_gm, simple_csf)
seeds = seeds_from_mask(simple_wm, density=2)
# Random pmf in every voxel
shape_img = list(simple_wm.shape)
shape_img.extend([sphere.vertices.shape[0]])
np.random.seed(0) # Random number generator initialization
pmf = np.random.random(shape_img)
# Test that PFT recover equal or more streamlines than localTracking
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 60, sphere)
local_streamlines_generator = LocalTracking(dg, tc, seeds, np.eye(4),
step_size, max_cross=1,
return_all=False)
local_streamlines = Streamlines(local_streamlines_generator)
pft_streamlines_generator = ParticleFilteringTracking(
dg, tc, seeds, np.eye(4), step_size, max_cross=1, return_all=False,
pft_back_tracking_dist=1, pft_front_tracking_dist=0.5)
pft_streamlines = Streamlines(pft_streamlines_generator)
npt.assert_(np.array([len(pft_streamlines) > 0]))
npt.assert_(np.array([len(pft_streamlines) >= len(local_streamlines)]))
# Test that all points are equally spaced
for l in [1, 2, 5, 10, 100]:
pft_streamlines = ParticleFilteringTracking(dg, tc, seeds, np.eye(4),
step_size, max_cross=1,
return_all=True, maxlen=l)
for s in pft_streamlines:
for i in range(len(s) - 1):
npt.assert_almost_equal(np.linalg.norm(s[i] - s[i + 1]),
step_size)
# Test that all points are within the image volume
seeds = seeds_from_mask(np.ones(simple_wm.shape), density=1)
pft_streamlines_generator = ParticleFilteringTracking(
dg, tc, seeds, np.eye(4), step_size, max_cross=1, return_all=True)
pft_streamlines = Streamlines(pft_streamlines_generator)
for s in pft_streamlines:
npt.assert_(np.all((s + 0.5).astype(int) >= 0))
npt.assert_(np.all((s + 0.5).astype(int) < simple_wm.shape))
# Test that the number of streamline return with return_all=True equal the
# number of seeds places
npt.assert_(np.array([len(pft_streamlines) == len(seeds)]))
# Test non WM seed position
seeds = [[0, 5, 4], [0, 0, 1], [50, 50, 50]]
pft_streamlines_generator = ParticleFilteringTracking(
dg, tc, seeds, np.eye(4), step_size, max_cross=1, return_all=True)
pft_streamlines = Streamlines(pft_streamlines_generator)
npt.assert_equal(len(pft_streamlines[0]), 3) # INVALIDPOINT
npt.assert_equal(len(pft_streamlines[1]), 3) # ENDPOINT
npt.assert_equal(len(pft_streamlines[2]), 1) # OUTSIDEIMAGE
# Test with wrong tissueclassifier type
tc_bin = BinaryTissueClassifier(simple_wm)
npt.assert_raises(ValueError,
lambda: ParticleFilteringTracking(dg, tc_bin, seeds,
np.eye(4), step_size))
# Test with invalid back/front tracking distances
npt.assert_raises(
ValueError,
lambda: ParticleFilteringTracking(dg, tc, seeds, np.eye(4), step_size,
pft_back_tracking_dist=0,
pft_front_tracking_dist=0))
npt.assert_raises(
ValueError,
lambda: ParticleFilteringTracking(dg, tc, seeds, np.eye(4), step_size,
#.........這裏部分代碼省略.........
示例4: test_stop_conditions
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
def test_stop_conditions():
"""This tests that the Local Tracker behaves as expected for the
following tissue types.
"""
# TissueTypes.TRACKPOINT = 1
# TissueTypes.ENDPOINT = 2
# TissueTypes.INVALIDPOINT = 0
tissue = np.array([[2, 1, 1, 2, 1],
[2, 2, 1, 1, 2],
[1, 1, 1, 1, 1],
[1, 1, 1, 2, 2],
[0, 1, 1, 1, 2],
[0, 1, 1, 0, 2],
[1, 0, 1, 1, 1]])
tissue = tissue[None]
sphere = HemiSphere.from_sphere(unit_octahedron)
pmf_lookup = np.array([[0., 0., 0., ],
[0., 0., 1.]])
pmf = pmf_lookup[(tissue > 0).astype("int")]
# Create a seeds along
x = np.array([0., 0, 0, 0, 0, 0, 0])
y = np.array([0., 1, 2, 3, 4, 5, 6])
z = np.array([1., 1, 1, 0, 1, 1, 1])
seeds = np.column_stack([x, y, z])
# Set up tracking
endpoint_mask = tissue == TissueTypes.ENDPOINT
invalidpoint_mask = tissue == TissueTypes.INVALIDPOINT
tc = ActTissueClassifier(endpoint_mask, invalidpoint_mask)
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 60, sphere)
# valid streamlines only
streamlines_generator = LocalTracking(direction_getter=dg,
tissue_classifier=tc,
seeds=seeds,
affine=np.eye(4),
step_size=1.,
return_all=False)
streamlines_not_all = iter(streamlines_generator)
# all streamlines
streamlines_all_generator = LocalTracking(direction_getter=dg,
tissue_classifier=tc,
seeds=seeds,
affine=np.eye(4),
step_size=1.,
return_all=True)
streamlines_all = iter(streamlines_all_generator)
# Check that the first streamline stops at 0 and 3 (ENDPOINT)
y = 0
sl = next(streamlines_not_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 3])
npt.assert_equal(len(sl), 4)
sl = next(streamlines_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 3])
npt.assert_equal(len(sl), 4)
# Check that the first streamline stops at 0 and 4 (ENDPOINT)
y = 1
sl = next(streamlines_not_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 4])
npt.assert_equal(len(sl), 5)
sl = next(streamlines_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 4])
npt.assert_equal(len(sl), 5)
# This streamline should be the same as above. This row does not have
# ENDPOINTs, but the streamline should stop at the edge and not include
# OUTSIDEIMAGE points.
y = 2
sl = next(streamlines_not_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 4])
npt.assert_equal(len(sl), 5)
sl = next(streamlines_all)
npt.assert_equal(sl[0], [0, y, 0])
npt.assert_equal(sl[-1], [0, y, 4])
npt.assert_equal(len(sl), 5)
# If we seed on the edge, the first (or last) point in the streamline
# should be the seed.
y = 3
sl = next(streamlines_not_all)
npt.assert_equal(sl[0], seeds[y])
sl = next(streamlines_all)
npt.assert_equal(sl[0], seeds[y])
# The last 3 seeds should not produce streamlines,
# INVALIDPOINT streamlines are rejected (return_all=False).
#.........這裏部分代碼省略.........
示例5: test_probabilistic_odf_weighted_tracker
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
def test_probabilistic_odf_weighted_tracker():
"""This tests that the Probabalistic Direction Getter plays nice
LocalTracking and produces reasonable streamlines in a simple example.
"""
sphere = HemiSphere.from_sphere(unit_octahedron)
# A simple image with three possible configurations, a vertical tract,
# a horizontal tract and a crossing
pmf_lookup = np.array([[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.],
[.6, .4, 0.]])
simple_image = np.array([[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 3, 2, 2, 2, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
])
simple_image = simple_image[..., None]
pmf = pmf_lookup[simple_image]
seeds = [np.array([1., 1., 0.])] * 30
mask = (simple_image > 0).astype(float)
tc = ThresholdTissueClassifier(mask, .5)
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 90, sphere,
pmf_threshold=0.1)
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 1.)
expected = [np.array([[0., 1., 0.],
[1., 1., 0.],
[2., 1., 0.],
[2., 2., 0.],
[2., 3., 0.],
[2., 4., 0.],
[2., 5., 0.]]),
np.array([[0., 1., 0.],
[1., 1., 0.],
[2., 1., 0.],
[3., 1., 0.],
[4., 1., 0.]])]
def allclose(x, y):
return x.shape == y.shape and np.allclose(x, y)
path = [False, False]
for sl in streamlines:
if allclose(sl, expected[0]):
path[0] = True
elif allclose(sl, expected[1]):
path[1] = True
else:
raise AssertionError()
npt.assert_(all(path))
# The first path is not possible if 90 degree turns are excluded
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 80, sphere,
pmf_threshold=0.1)
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 1.)
for sl in streamlines:
npt.assert_(np.allclose(sl, expected[1]))
# The first path is not possible if pmf_threshold > 0.67
# 0.4/0.6 < 2/3, multiplying the pmf should not change the ratio
dg = ProbabilisticDirectionGetter.from_pmf(10*pmf, 90, sphere,
pmf_threshold=0.67)
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 1.)
for sl in streamlines:
npt.assert_(np.allclose(sl, expected[1]))
# Test non WM seed position
seeds = [[0, 0, 0], [5, 5, 5]]
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 0.2, max_cross=1,
return_all=True)
streamlines = Streamlines(streamlines)
npt.assert_(len(streamlines[0]) == 3) # INVALIDPOINT
npt.assert_(len(streamlines[1]) == 1) # OUTSIDEIMAGE
# Test that all points are within the image volume
seeds = seeds_from_mask(np.ones(mask.shape), density=2)
streamline_generator = LocalTracking(dg, tc, seeds, np.eye(4), 0.5,
return_all=True)
streamlines = Streamlines(streamline_generator)
for s in streamlines:
npt.assert_(np.all((s + 0.5).astype(int) >= 0))
npt.assert_(np.all((s + 0.5).astype(int) < mask.shape))
# Test that the number of streamline return with return_all=True equal the
# number of seeds places
npt.assert_(np.array([len(streamlines) == len(seeds)]))
# Test reproducibility
tracking_1 = Streamlines(LocalTracking(dg, tc, seeds, np.eye(4),
0.5,
random_seed=0)).data
tracking_2 = Streamlines(LocalTracking(dg, tc, seeds, np.eye(4),
#.........這裏部分代碼省略.........
示例6: test_ProbabilisticOdfWeightedTracker
# 需要導入模塊: from dipy.direction import ProbabilisticDirectionGetter [as 別名]
# 或者: from dipy.direction.ProbabilisticDirectionGetter import from_pmf [as 別名]
def test_ProbabilisticOdfWeightedTracker():
"""This tests that the Probabalistic Direction Getter plays nice
LocalTracking and produces reasonable streamlines in a simple example.
"""
sphere = HemiSphere.from_sphere(unit_octahedron)
# A simple image with three possible configurations, a vertical tract,
# a horizontal tract and a crossing
pmf_lookup = np.array([[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.],
[.5, .5, 0.]])
simple_image = np.array([[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 3, 2, 2, 2, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
])
simple_image = simple_image[..., None]
pmf = pmf_lookup[simple_image]
seeds = [np.array([1., 1., 0.])] * 30
mask = (simple_image > 0).astype(float)
tc = ThresholdTissueClassifier(mask, .5)
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 90, sphere)
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 1.)
expected = [np.array([[ 0., 1., 0.],
[ 1., 1., 0.],
[ 2., 1., 0.],
[ 2., 2., 0.],
[ 2., 3., 0.],
[ 2., 4., 0.],
[ 2., 5., 0.]]),
np.array([[ 0., 1., 0.],
[ 1., 1., 0.],
[ 2., 1., 0.],
[ 3., 1., 0.],
[ 4., 1., 0.]])
]
def allclose(x, y):
return x.shape == y.shape and np.allclose(x, y)
path = [False, False]
for sl in streamlines:
dir = ( -sphere.vertices[0] ).copy()
if allclose(sl, expected[0]):
path[0] = True
elif allclose(sl, expected[1]):
path[1] = True
else:
raise AssertionError()
npt.assert_(all(path))
# The first path is not possible if 90 degree turns are excluded
dg = ProbabilisticDirectionGetter.from_pmf(pmf, 80, sphere)
streamlines = LocalTracking(dg, tc, seeds, np.eye(4), 1.)
for sl in streamlines:
npt.assert_(np.allclose(sl, expected[1]))