""" Unit processing_library for convolutional Gridding


"""
import random
import unittest

import numpy
from numpy.testing import assert_allclose

from processing_library.fourier_transforms.convolutional_gridding import w_beam, coordinates, \
    coordinates2, coordinateBounds, convolutional_degrid, convolutional_grid


class TestConvolutionalGridding(unittest.TestCase):
    
    @staticmethod
    def assertAlmostEqualScalar(a, result=1.0):
        result * numpy.ones_like(result)

    def test_coordinates(self):
        for N in [4, 5, 6, 7, 8, 9, 1000, 1001, 1002, 1003]:
            low, high = coordinateBounds(N)
            c = coordinates(N)
            cx, cy = coordinates2(N)
            self.assertAlmostEqual(numpy.min(c), low)
            self.assertAlmostEqual(numpy.max(c), high)
            self.assertAlmostEqual(numpy.min(cx), low)
            self.assertAlmostEqual(numpy.max(cx), high)
            self.assertAlmostEqual(numpy.min(cy), low)
            self.assertAlmostEqual(numpy.max(cy), high)
            assert c[N // 2] == 0
            assert (cx[N // 2, :] == 0).all()
            assert (cy[:, N // 2] == 0).all()

    @staticmethod
    def _test_pattern(npixel):
        return coordinates2(npixel)[0] + coordinates2(npixel)[1] * 1j

    def test_anti_aliasing_calculate(self):
        for shape in [(64, 64), (128, 128), (256, 256)]:
            _, aaf = anti_aliasing_calculate(shape, 8)
            self.assertAlmostEqual(numpy.max(aaf[..., aaf.shape[1] // 2, aaf.shape[0] // 2]), 0.18712109669890534)

    def test_w_kernel_beam(self):
        assert_allclose(numpy.real(w_beam(5, 0.1, 0))[0, 0], 1.0)
        self.assertAlmostEqualScalar(w_beam(5, 0.1, 100)[2, 2], 1)
        self.assertAlmostEqualScalar(w_beam(10, 0.1, 100)[5, 5], 1)
        self.assertAlmostEqualScalar(w_beam(11, 0.1, 1000)[5, 5], 1)
    
    def test_convolutional_grid(self):
        npixel = 256
        nvis = 10000
        nchan = 1
        npol = 4
        uvgrid = numpy.zeros([nchan, npol, npixel, npixel], dtype='complex')
        gcf, kernel = anti_aliasing_calculate((npixel, npixel), 8)
        # kernel has shape [kernel_oversampling, kernel_oversampling, npixel, npixel] The fractional
        # part of the coordinate maps onto the first two axes.
        # Make some uv coordinates with random locations
        uvcoords = numpy.array([[random.uniform(-0.25, 0.25), random.uniform(-0.25, 0.25)] for ivis in range(nvis)])
        # Make some visibilities, all complex unity
        vis = numpy.ones([nvis, nchan, npol], dtype='complex')
        visweights = numpy.ones([nvis, nchan, npol])
        kernels = (numpy.zeros([nvis], dtype='int'), [kernel])
        frequencymap = numpy.zeros([nvis], dtype='int')
        # On my laptop the following takes about 34ss for 4e6 * 50 = 2e8 points so about 170 ns per point
        uvgrid, sumwt = convolutional_grid(kernels, uvgrid, vis, visweights, uvcoords, frequencymap)
        assert numpy.sum(sumwt) > 0.0
        assert uvgrid.shape[0] == nchan
        assert uvgrid.shape[1] == npol
        assert uvgrid.shape[2] == npixel
        assert uvgrid.shape[3] == npixel

    def test_convolutional_degrid(self):
        npixel = 256
        nvis = 100000
        nchan = 1
        npol = 4
        uvgrid = numpy.ones([nchan, npol, npixel, npixel], dtype='complex')
        gcf, kernel = anti_aliasing_calculate((npixel, npixel), 8)
        # kernel has shape [kernel_oversampling, kernel_oversampling, npixel, npixel] The fractional
        # part of the coordinate maps onto the first two axes.
        # Make some uv coordinates with random locations
        uvcoords = numpy.array([[random.uniform(-0.25, 0.25), random.uniform(-0.25, 0.25)] for ivis in range(nvis)])
        vshape = [nvis, npol]
        kernels = (numpy.zeros([nvis], dtype='int'), [kernel])
        frequencymap = numpy.zeros([nvis], dtype='int')

        vis = convolutional_degrid(kernels, vshape, uvgrid, uvcoords, frequencymap)
        assert vis.shape[0] == nvis
        assert vis.shape[1] == npol


if __name__ == '__main__':
    unittest.main()

@@ -275,51 +275,9 @@
 unds
-, convolutional_degrid, convolutional_grid
 %0A
+
 %0A%0Acl
@@ -1226,280 +1226,8 @@
 1j%0A%0A
-    def test_anti_aliasing_calculate(self):%0A        for shape in %5B(64, 64), (128, 128), (256, 256)%5D:%0A            _, aaf = anti_aliasing_calculate(shape, 8)%0A            self.assertAlmostEqual(numpy.max(aaf%5B..., aaf.shape%5B1%5D // 2, aaf.shape%5B0%5D // 2%5D), 0.18712109669890534)%0A%0A
     
@@ -1530,2213 +1530,13 @@
  1)%0A
+
     %0A
-    def test_convolutional_grid(self):%0A        npixel = 256%0A        nvis = 10000%0A        nchan = 1%0A        npol = 4%0A        uvgrid = numpy.zeros(%5Bnchan, npol, npixel, npixel%5D, dtype='complex')%0A        gcf, kernel = anti_aliasing_calculate((npixel, npixel), 8)%0A        # kernel has shape %5Bkernel_oversampling, kernel_oversampling, npixel, npixel%5D The fractional%0A        # part of the coordinate maps onto the first two axes.%0A        # Make some uv coordinates with random locations%0A        uvcoords = numpy.array(%5B%5Brandom.uniform(-0.25, 0.25), random.uniform(-0.25, 0.25)%5D for ivis in range(nvis)%5D)%0A        # Make some visibilities, all complex unity%0A        vis = numpy.ones(%5Bnvis, nchan, npol%5D, dtype='complex')%0A        visweights = numpy.ones(%5Bnvis, nchan, npol%5D)%0A        kernels = (numpy.zeros(%5Bnvis%5D, dtype='int'), %5Bkernel%5D)%0A        frequencymap = numpy.zeros(%5Bnvis%5D, dtype='int')%0A        # On my laptop the following takes about 34ss for 4e6 * 50 = 2e8 points so about 170 ns per point%0A        uvgrid, sumwt = convolutional_grid(kernels, uvgrid, vis, visweights, uvcoords, frequencymap)%0A        assert numpy.sum(sumwt) %3E 0.0%0A        assert uvgrid.shape%5B0%5D == nchan%0A        assert uvgrid.shape%5B1%5D == npol%0A        assert uvgrid.shape%5B2%5D == npixel%0A        assert uvgrid.shape%5B3%5D == npixel%0A%0A    def test_convolutional_degrid(self):%0A        npixel = 256%0A        nvis = 100000%0A        nchan = 1%0A        npol = 4%0A        uvgrid = numpy.ones(%5Bnchan, npol, npixel, npixel%5D, dtype='complex')%0A        gcf, kernel = anti_aliasing_calculate((npixel, npixel), 8)%0A        # kernel has shape %5Bkernel_oversampling, kernel_oversampling, npixel, npixel%5D The fractional%0A        # part of the coordinate maps onto the first two axes.%0A        # Make some uv coordinates with random locations%0A        uvcoords = numpy.array(%5B%5Brandom.uniform(-0.25, 0.25), random.uniform(-0.25, 0.25)%5D for ivis in range(nvis)%5D)%0A        vshape = %5Bnvis, npol%5D%0A        kernels = (numpy.zeros(%5Bnvis%5D, dtype='int'), %5Bkernel%5D)%0A        frequencymap = numpy.zeros(%5Bnvis%5D, dtype='int')%0A%0A        vis = convolutional_degrid(kernels, vshape, uvgrid, uvcoords, frequencymap)%0A        assert vis.shape%5B0%5D == nvis%0A        assert vis.shape%5B1%5D == npol%0A%0A%0A
 if _

Remove tests of convolutiongrid