Example #1
0
 def setUp(self):
     importlib.reload(
         common)  # to avoid problems when running multiple tests
     self.grey = common.make_grey()
     self.segments = common.make_shapes()
     self.density = Density()
     self.density.calculate(image=self.grey, segments=self.segments)
Example #2
0
    def testMerge(self):
        """
        Test merge
        """

        # one region, mean
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes, ids=[1, 4])
        dist.calculate(regionIds=[3], regions=self.bound, mode='mean')
        np_test.assert_almost_equal(dist.distance[dist.ids],
                                    self.dist_mean[[0, 0], [0, 2]])
        np_test.assert_almost_equal(dist._closestRegion, [3] * 2)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids], [3] * 2)

        # other region
        dist_2 = DistanceTo(segments=shapes, ids=[3, 6])
        dist_2.calculate(regionIds=[3], regions=self.bound, mode='mean')
        np_test.assert_almost_equal(dist_2.distance[dist_2.ids],
                                    self.dist_mean[[0, 0], [1, 3]])
        np_test.assert_almost_equal(dist._closestRegion, [3] * 2)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids], [3] * 2)

        # merge replace
        dist.merge(new=dist_2, mode='replace')
        np_test.assert_equal(dist.ids, [1, 3, 4, 6])
        np_test.assert_almost_equal(dist.distance[dist.ids],
                                    self.dist_mean[0, :])
        np_test.assert_almost_equal(dist._closestRegion, [3] * 4)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids], [3] * 4)
Example #3
0
    def testCalculate(self):
        """
        Tests calculate
        """

        # one region, mean
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        dist.calculate(regionIds=[3], regions=self.bound, mode='mean')
        np_test.assert_almost_equal(dist._distance, self.dist_mean[0, :])
        np_test.assert_almost_equal(dist.distance[dist.ids],
                                    self.dist_mean[0, :])
        np_test.assert_almost_equal(dist._closestRegion, [3] * 4)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids], [3] * 4)

        # two regions, min
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        dist.calculate(regionIds=[3, 4], regions=self.bound, mode='min')
        np_test.assert_almost_equal(dist._distance, self.dist_cl_min)
        np_test.assert_almost_equal(dist.distance[dist.ids], self.dist_cl_min)
        np_test.assert_almost_equal(dist._closestRegion, self.cl_reg_min)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids],
                                    self.cl_reg_min)

        # segments and regions from the same object
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes, ids=[1, 4, 6])
        dist.calculate(regionIds=[3], mode='min')
        np_test.assert_almost_equal(dist.distance[dist.ids], self.seg_3_min)

        # segments and regions from the same object
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes, ids=[1, 6])
        dist.calculate(regionIds=[3, 4], mode='min')
        np_test.assert_almost_equal(dist.distance[dist.ids], self.seg_3_4_min)
        np_test.assert_almost_equal(dist.closestRegion[dist.ids],
                                    self.seg_3_4_closest)

        # insets
        shapes = common.make_shapes()
        shapes.makeInset(ids=[1, 3])
        bound = Segment(self.bound.data)
        bound.makeInset(ids=[4])
        dist = DistanceTo(segments=shapes, ids=[1, 3])
        dist.calculate(regionIds=[4], regions=bound, mode='min')
        np_test.assert_almost_equal(dist.distance[dist.ids],
                                    [6, numpy.sqrt(29)])

        # median, surface 1
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.calculate(segments=shapes,
                                  regionIds=[3, 4],
                                  regions=self.bound,
                                  mode='median',
                                  surface=1)
        np_test.assert_almost_equal(dist._distance, self.dist_cl_median_s1)
        np_test.assert_equal(dist._closestRegion, self.cl_median_s1)
Example #4
0
 def setUp(self):
     importlib.reload(
         common)  # to avoid problems when running multiple tests
     self.shapes = common.make_shapes()
Example #5
0
    def testGetDistanceToRegion(self):
        """
        Tests getDistanceToRegion
        """

        # min
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='min')
        np_test.assert_almost_equal(all_dist, self.dist_min)

        # max
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='max')
        np_test.assert_almost_equal(all_dist, self.dist_max)

        # mean
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='mean')
        np_test.assert_almost_equal(all_dist, self.dist_mean)

        # median
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='median')
        np_test.assert_almost_equal(all_dist, self.dist_median)

        # median, surface 1
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='median',
                                             surface=1)
        np_test.assert_almost_equal(all_dist, self.dist_median_s1)

        # center
        shapes = common.make_shapes()
        dist = DistanceTo(segments=shapes)
        all_dist = dist.getDistanceToRegions(segments=shapes.data,
                                             segmentIds=shapes.ids,
                                             regionIds=[3, 4],
                                             regions=self.bound.data,
                                             mode='center')
        np_test.assert_almost_equal(all_dist, self.dist_center)