def test_feature_resample(): from dipy.tracking.streamline import set_number_of_points # Test subclassing Feature class ResampleFeature(dipymetric.Feature): def __init__(self, nb_points): super(ResampleFeature, self).__init__(is_order_invariant=False) self.nb_points = nb_points if nb_points <= 0: raise ValueError( "ResampleFeature: `nb_points` must be strictly positive: {0}" .format(nb_points)) def infer_shape(self, streamline): return (self.nb_points, streamline.shape[1]) def extract(self, streamline): return set_number_of_points(streamline, self.nb_points) assert_raises(ValueError, dipymetric.ResampleFeature, nb_points=0) assert_raises(ValueError, ResampleFeature, nb_points=0) max_points = max(map(len, [s1, s2, s3, s4])) for nb_points in [1, 5, 2 * max_points]: for feature in [ dipymetric.ResampleFeature(nb_points), ResampleFeature(nb_points) ]: for s in [s1, s2, s3, s4]: # Test method infer_shape assert_equal(feature.infer_shape(s), (nb_points, s.shape[1])) # Test method extract features = feature.extract(s) assert_equal(features.shape, (nb_points, s.shape[1])) assert_array_almost_equal(features, set_number_of_points(s, nb_points)) # This feature type is not order invariant assert_false(feature.is_order_invariant) for s in [s1, s2, s3, s4]: features = feature.extract(s) features_flip = feature.extract(s[::-1]) assert_array_equal(features_flip, set_number_of_points(s[::-1], nb_points)) assert_true(np.any(np.not_equal(features, features_flip)))
def test_quickbundles_shape_uncompatibility(): # QuickBundles' old default metric (AveragePointwiseEuclideanMetric, # aka MDF) requires that all streamlines have the same number of points. metric = dipymetric.AveragePointwiseEuclideanMetric() qb = QuickBundles(threshold=20., metric=metric) assert_raises(ValueError, qb.cluster, data) # QuickBundles' new default metric (AveragePointwiseEuclideanMetric, # aka MDF combined with ResampleFeature) will automatically resample # streamlines so they all have 18 points. qb = QuickBundles(threshold=20.) clusters1 = qb.cluster(data) feature = dipymetric.ResampleFeature(nb_points=18) metric = dipymetric.AveragePointwiseEuclideanMetric(feature) qb = QuickBundles(threshold=20., metric=metric) clusters2 = qb.cluster(data) assert_array_equal(list(itertools.chain(*clusters1)), list(itertools.chain(*clusters2)))