Beispiel #1
0
    def test_convolve_looping(self):
        """
        Test the slow and fast convolution implementations - ensure that they
        are identical.
        """

        k = gaussian_kernel(3)
        input = np.random.randint(10, size=(50, 50))

        slow_output = convolve(input, k, slow=True)
        fast_output = convolve(input, k)

        # There may be some tiny rounding differences. 
        assert((abs(fast_output - slow_output) < 1e-13).all())
Beispiel #2
0
    def test_compare_with_mask(self):
        """
        Compare output between Python and Fortran implementations with masking.
        """

        with nc.Dataset(os.path.join(self.data_dir, 'taux.nc')) as f:
            taux_in = np.array(f.variables['taux'][0, :], dtype='float64')

        mask_py = np.zeros_like(taux_in, dtype='bool')
        mask_py[np.where(taux_in == 0)] = True

        mask_f = np.ones_like(taux_in)
        mask_f[np.where(taux_in == 0)] = 0.0

        # Run the scipy version.
        taux_sc = ndimage.gaussian_filter(taux_in, sigma=4.0, truncate=1.0)
        # To do a realistic comparison we need to mask out land points.
        taux_sc = taux_sc * np.logical_not(mask_py)

        # A lower truncation leads to a smaller kernel and hence less guessing
        # in the case of a masked input. This gives a better result for masked
        # inputs.
        k = gaussian_kernel(4.0, truncate=1.0)
        # Run the Python version.
        taux_py = convolve(taux_in, k, mask_py)

        # Run the Fortran version.
        _, taux_f = run_fortran_gaussian_filter(4.0, 1.0, 9, 9, taux_in, mask_f)

        assert(np.max(abs(taux_py - taux_f)) < 1e-14)
        assert(abs(1 - np.sum(taux_in) / np.sum(taux_f)) < 1e-4)
Beispiel #3
0
    def test_filter_with_mask(self):
        """
        Some basic tests with masking. 
        """

        input = np.random.random(size=(100, 100))
        mask = np.zeros_like(input, dtype='bool')
        mask[0::2, :] = True

        # Lots of mask.
        result = convolve(input, gaussian_kernel(1), mask)
        assert(abs(1 - np.sum(result) / np.sum(input)) < 1e-3)

        # No mask, blur everything.
        mask = np.zeros_like(input, dtype='bool')
        result = convolve(input, gaussian_kernel(1), mask)
        assert(abs(1 - np.sum(result) / np.sum(input)) < 1e-12)

        # All mask - does nothing.
        mask = np.ones_like(input, dtype='bool')
        result = convolve(input, gaussian_kernel(1), mask)
        assert(np.array_equal(input, result))
Beispiel #4
0
def drawHeatMap(logs, max_x, max_y):
    bins = log2Mat(logs, max_x, max_y)
    bin255 = 255 / bins.max() * bins
    print(bin255.max())
    gaussian_fitered_bins = convolve(bin255, gaussian_kernel(4, truncate=2.0))
    pil_img = Image.fromarray(
        np.uint8(255 / gaussian_fitered_bins.max() * gaussian_fitered_bins))
    pil_img.save('lena_RGBs.jpg')
    print("bin m", bins.max())

    draw(bin255, "")
    draw(255 / gaussian_fitered_bins.max() * gaussian_fitered_bins,
         "_gaussian")
Beispiel #5
0
    def test_filter_without_mask(self):
        """
        Run the Gaussian filter without a mask and compare to python solution. 
        """

        with nc.Dataset(os.path.join(self.data_dir, 'taux.nc')) as f:
            taux_in = f.variables['taux'][0, :]

        taux = ndimage.gaussian_filter(taux_in, sigma=3)
        my_taux = convolve(taux_in, gaussian_kernel(3))

        assert((abs(taux - my_taux) < 1e-6).all())
        assert(abs(1 - np.sum(taux) / np.sum(my_taux)) < 1e-4)
        assert(abs(1 - np.sum(taux_in) / np.sum(my_taux)) < 1e-4)
Beispiel #6
0
    def test_convolve(self):
        """
        Test that convolution routine is correct.

        Compare to ndimage.convolve.
        """

        input = np.random.random(size=(100, 100))
        k = gaussian_kernel(1)

        my_output = convolve(input, k)
        output = ndimage.convolve(input, k)

        assert((abs(my_output - output) < 1e-15).all())
Beispiel #7
0
    def test_convolve(self):
        """
        Test that convolution routine is correct.

        Compare to Python implementation.
        """

        k_p = gaussian_kernel(1.0, 4.0)
        kx, ky = k_p.shape
        input = np.random.random(size=(100, 100))
        _, output_f = run_fortran_gaussian_filter(1.0, 4.0, kx, ky, input)
        output_p = convolve(input, k_p)

        assert((abs(output_p - output_f) < 1e-15).all())
Beispiel #8
0
    def test_compare_without_mask(self):
        """
        Compare output between Python and Fortran implementations no masking.
        """

        with nc.Dataset(os.path.join(self.data_dir, 'taux.nc')) as f:
            taux_in = np.array(f.variables['taux'][0, :], dtype='float64')

        # Scipy
        taux_sc = ndimage.gaussian_filter(taux_in, sigma=4.0, truncate=1.0)

        # Run the Python version.
        k = gaussian_kernel(4.0, truncate=1.0)
        taux_py = convolve(taux_in, k)

        _, taux_f = run_fortran_gaussian_filter(4.0, 1.0, 9, 9, taux_in)

        # Check that all implementations are (very) close.
        assert(np.max(abs(taux_sc - taux_py)) < 1e-14)
        assert(np.max(abs(taux_sc - taux_py)) < 1e-14)
Beispiel #9
0
    def test_realistic_with_mask(self):
        """
        Test a realistic field and mask.
        """

        with nc.Dataset(os.path.join(self.data_dir, 'taux.nc')) as f:
            taux_in = f.variables['taux'][0, :]

        mask = np.zeros_like(taux_in, dtype='bool')
        mask[np.where(taux_in == 0)] = True

        taux = ndimage.gaussian_filter(taux_in, sigma=3)
        # To do a realistic comparison we need to mask out land points.
        taux = taux * np.logical_not(mask)

        # A lower truncation leads to a smaller kernel and hence less guessing
        # in the case of a masked input. This gives a better result for masked
        # inputs.
        k = gaussian_kernel(4, truncate=1)

        my_taux = convolve(taux_in, k, mask)