def test_composite_pattern_basic(self):
        """
        Test that a composite pattern consisting of just one Gaussian
        is the same as that actual Gaussian pattern, and that a
        composite pattern of two rectangles is the same as adding the
        two individual matrices.
        """
        bbox = BoundingBox(radius=0.5)
        g = Gaussian(size=0.2, aspect_ratio=0.5, orientation=0, x=0.2, y=-0.03)
        c = Composite(generators=[g], bounds=bbox, xdensity=7, ydensity=7)
        assert_array_equal(g(bounds=bbox, xdensity=7, ydensity=7), c())

        r1 = Rectangle(size=0.2,
                       aspect_ratio=1,
                       x=0.3,
                       y=0.3,
                       orientation=0,
                       smoothing=0.0)
        r2 = Rectangle(size=0.2,
                       aspect_ratio=1,
                       x=-0.3,
                       y=-0.3,
                       orientation=0,
                       bounds=BoundingBox(radius=0.8),
                       xdensity=2,
                       smoothing=0.0)
        c_true = r1(bounds=bbox, xdensity=7, ydensity=7) + r2(
            bounds=bbox, xdensity=7, ydensity=7)
        c = Composite(generators=[r1, r2], bounds=bbox, xdensity=7, ydensity=7)
        assert_array_equal(c(), c_true)
    def test_constant(self):
        """
        Constant overrides PatternGenerator's usual matrix creation.
        """
        pattern_bounds = BoundingBox(points=((0.3, 0.2), (0.5, 0.5)))

        pattern_target = np.array([[1, 1], [1, 1], [1, 1]])

        c = Constant(bounds=pattern_bounds, xdensity=10.0, ydensity=10)
        assert_array_equal(c(), pattern_target)
    def test_a_basic_patterngenerator(self):
        pattern_bounds = BoundingBox(points=((0.3, 0.2), (0.5, 0.5)))

        pattern_target = np.array([[1, 1], [1, 1], [1, 1]])

        r = Rectangle(bounds=pattern_bounds,
                      xdensity=10,
                      ydensity=10,
                      aspect_ratio=1,
                      size=1,
                      smoothing=0.0)
        assert_array_equal(r(), pattern_target)
    def test_composite_pattern_moves(self):
        """
        Test that moving a composite pattern yields the correct pattern.
        """
        bbox = BoundingBox(radius=0.5)
        g = Gaussian(size=0.2,
                     aspect_ratio=0.5,
                     orientation=np.pi / 3,
                     x=0,
                     y=0)
        c = Composite(generators=[g],
                      x=-0.3,
                      y=0.4,
                      xdensity=4,
                      ydensity=4,
                      bounds=bbox)
        g_moved = g(x=-0.3, y=0.4, xdensity=4, ydensity=4, bounds=bbox)

        assert_array_equal(c(), g_moved)
    def test_orientation_and_rotation(self):
        """
        Test that a pattern is drawn with the correct orientation,
        and is rotated correctly.
        """
        ### Test initial orientation and 90-degree rotation
        target = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0],
                           [0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0],
                           [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0]])

        bounds = BoundingBox(radius=0.3)
        xdensity = 10
        ydensity = 10
        width = 2.0 / xdensity
        height = 4.0 / ydensity

        rect = Rectangle(size=height,
                         aspect_ratio=width / height,
                         smoothing=0.0,
                         xdensity=xdensity,
                         ydensity=ydensity,
                         bounds=bounds)

        assert_array_equal(rect(), target)
        assert_array_equal(rect(orientation=np.pi / 2), np.rot90(target))

        ### 45-degree rotation about the origin
        rot_45 = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0],
                           [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]])

        assert_array_equal(rect(orientation=np.pi / 4), rot_45)

        ### 45-degree rotation that's not about the origin
        rot_45_offset = np.array([[0, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0],
                                  [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0],
                                  [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])

        assert_array_equal(
            rect(x=-1.0 / xdensity, y=1.0 / ydensity, orientation=np.pi / 4),
            rot_45_offset)
Beispiel #6
0
    def minimal_line_thickness(self, c):
        """
        help function used by the test_* functions.

        It checks that the line width is of one pixel
        """
        radius = c['radius']
        bounds = BoundingBox(radius=radius)
        xdensity = c['density']
        ydensity = c['density']
        x = c['x']
        for orientation in [0, pi / 4, pi / 2]:
            c['orientation'] = orientation
            im = imagen.Line(scale=1,
                             offset=0,
                             orientation=orientation,
                             enforce_minimal_thickness=True,
                             thickness=0.0,
                             x=x,
                             y=0.,
                             smoothing=0.0,
                             xdensity=xdensity,
                             ydensity=ydensity,
                             bounds=bounds)()
            number_of_ones = (im == 1).sum()
            if orientation == pi / 4:
                y_crossing = where(im[:, -1] >= 1)[0][0]
            else:
                y_crossing = 0.0
            width_in_pixels = 2 * radius * ydensity - y_crossing
            msg = """
                Thickness of the line should be of one pixel.
                Actual line has an everage thickness of: %f.
                Experimental conditions:
                """ % (number_of_ones / float(width_in_pixels)) + str(c)
            self.assertEqual(number_of_ones, width_in_pixels, msg=msg)
    def test_position(self):
        """
        Test that a pattern is drawn correctly at different
        locations.
        """

        initial = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0],
                            [0, 0, 0, 0]])

        r = Rectangle(bounds=BoundingBox(radius=2),
                      xdensity=1,
                      ydensity=1,
                      aspect_ratio=1,
                      size=2,
                      smoothing=0.0)
        assert_array_equal(r(), initial)

        ### x offset
        x_offset = np.array([[0, 0, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1],
                             [0, 0, 0, 0]])

        assert_array_equal(r(x=1), x_offset)

        ### y offset
        y_offset = np.rot90(x_offset)
        assert_array_equal(r(y=1), y_offset)

        ### x and y offset
        target = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])

        width = 0.2
        height = 0.4

        r = Rectangle(bounds=BoundingBox(radius=0.5),
                      xdensity=10,
                      ydensity=10,
                      smoothing=0.0,
                      aspect_ratio=width / height,
                      size=height)

        assert_array_equal(r(x=-0.4, y=-0.3), target)

        ### x and y offset with bounds offset by the same
        target = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

        width = 0.2
        height = 0.4

        bounds = BoundingBox(points=((-0.9, -0.8), (0.1, 0.2)))
        r = Rectangle(bounds=bounds,
                      xdensity=10,
                      ydensity=10,
                      smoothing=0.0,
                      aspect_ratio=width / height,
                      size=height)

        assert_array_equal(r(x=-0.4, y=-0.3), target)