Example #1
0
    def test_orientation(self):
        """
        The array returned by `unit_gridpoints()` should have the correct
        ordering.

        """
        for shape in itertools.product((3, 5, 8), repeat=2):
            with self.subTest(shape=shape):
                n, m = shape
                a = 0.5 * float(n - 1)
                b = 0.5 * float(m - 1)

                ji = unit_gridpoints(shape, mode="ji")
                xy = unit_gridpoints(shape, mode="xy")
                # `out[0]` is the top-left corner
                numpy.testing.assert_array_almost_equal(ji[0], (-a, -b))
                numpy.testing.assert_array_almost_equal(xy[0], (-b, a))
                # `out[m - 1]` is the top-right corner
                numpy.testing.assert_array_almost_equal(ji[m - 1], (-a, b))
                numpy.testing.assert_array_almost_equal(xy[m - 1], (b, a))
                # `out[(n - 1) * m]` is the bottom-left corner
                numpy.testing.assert_array_almost_equal(
                    ji[(n - 1) * m], (a, -b))
                numpy.testing.assert_array_almost_equal(
                    xy[(n - 1) * m], (-b, -a))
                # `out[-1]` is the bottom right corner
                numpy.testing.assert_array_almost_equal(ji[-1], (a, b))
                numpy.testing.assert_array_almost_equal(xy[-1], (b, -a))
Example #2
0
 def test_known_values(self):
     """`unit_gridpoints()` should return known result with known input."""
     shape = (3, 5)
     ji = unit_gridpoints(shape, mode="ji")
     xy = unit_gridpoints(shape, mode="xy")
     _ji = numpy.column_stack((
         [-1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
         [-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2],
     ))
     _xy = numpy.column_stack((
         [-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2],
         [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1],
     ))
     numpy.testing.assert_array_almost_equal(xy, _xy)
     numpy.testing.assert_array_almost_equal(ji, _ji)
Example #3
0
 def test_zero_mean(self):
     """The array returned by `unit_gridpoints()` should have zero mean."""
     for shape in itertools.product((3, 5, 8), repeat=2):
         for mode in ("ji", "xy"):
             with self.subTest(shape=shape, mode=mode):
                 pts = unit_gridpoints(shape, mode=mode)
                 numpy.testing.assert_array_almost_equal(
                     numpy.mean(pts, axis=0), 0)
Example #4
0
    def test_shape(self):
        """
        The array returned by `unit_gridpoints()` should have the correct shape.

        """
        for shape in itertools.product((3, 5, 8), repeat=2):
            for mode in ("ji", "xy"):
                with self.subTest(shape=shape, mode=mode):
                    n, m = shape
                    pts = unit_gridpoints(shape, mode=mode)
                    self.assertEqual(pts.ndim, 2)
                    self.assertEqual(pts.shape[0], n * m)
                    self.assertEqual(pts.shape[1], 2)
Example #5
0
    def test_incomplete_grid(self):
        """
        `estimate_grid_orientation()` should return the expected
        AffineTransform for a grid with one point missing.

        """
        for shape in [(5, 5), (8, 8)]:
            for i in range(50):
                with self.subTest(shape=shape, i=i):
                    n, m = shape
                    shuffle = self._rng.permutation(n * m)
                    tform = random_affine_transform(self._rng)
                    xy = tform.apply(
                        unit_gridpoints(shape, mode="xy")[shuffle])[:-1]
                    out = estimate_grid_orientation(xy, shape, AffineTransform)
                    numpy.testing.assert_array_almost_equal(
                        tform.matrix, out.matrix)
                    numpy.testing.assert_array_almost_equal(
                        tform.translation, out.translation)
Example #6
0
    def test_full_grid(self):
        """
        `nearest_neighbor_graph()` should return the expected graph for a full
        grid.

        """
        for shape in itertools.product((5, 8), repeat=2):
            for i in range(50):
                with self.subTest(shape=shape, i=i):
                    n, m = shape
                    shuffle = self._rng.permutation(n * m)
                    index = numpy.argsort(shuffle)
                    tform = random_affine_transform(self._rng)
                    xy = tform.apply(
                        unit_gridpoints(shape, mode="xy")[shuffle])
                    graph = unit_gridpoints_graph(shape, tform, index)
                    out = nearest_neighbor_graph(xy)
                    numpy.testing.assert_array_almost_equal(
                        graph.adjacency_matrix(), out.adjacency_matrix())
Example #7
0
    def test_incomplete_grid(self):
        """
        `nearest_neighbor_graph()` should return the expected graph for a grid
        with one point missing.

        """
        for shape in itertools.product((5, 8), repeat=2):
            for i in range(50):
                with self.subTest(shape=shape, i=i):
                    n, m = shape
                    shuffle = self._rng.permutation(n * m)
                    index = numpy.argsort(shuffle)
                    tform = random_affine_transform(self._rng)
                    xy = tform.apply(
                        unit_gridpoints(shape, mode="xy")[shuffle])[:-1]
                    _graph = unit_gridpoints_graph(shape, tform, index)
                    vertex = n * m - 1
                    # To prevent a RuntimeError loop over a copy
                    for neighbor in _graph[vertex].copy():
                        _graph.remove_edge((vertex, neighbor))
                    graph = _graph[:-1]
                    out = nearest_neighbor_graph(xy)
                    numpy.testing.assert_array_almost_equal(
                        graph.adjacency_matrix(), out.adjacency_matrix())