Beispiel #1
0
 def test_add_2D_kernels(self):
     """
     Check if adding of two 1D kernels works.
     """
     box_1 = Box2DKernel(3)
     box_2 = Box2DKernel(1)
     box_sum_1 = box_1 + box_2
     box_sum_2 = box_2 + box_1
     ref = [[1 / 9., 1 / 9., 1 / 9.], [1 / 9., 1 + 1 / 9., 1 / 9.],
            [1 / 9., 1 / 9., 1 / 9.]]
     ref_1 = [[1 / 9., 1 / 9., 1 / 9.], [1 / 9., 1 / 9., 1 / 9.],
              [1 / 9., 1 / 9., 1 / 9.]]
     assert_almost_equal(box_2.array, [[1]], decimal=12)
     assert_almost_equal(box_1.array, ref_1, decimal=12)
     assert_almost_equal(box_sum_1.array, ref, decimal=12)
     assert_almost_equal(box_sum_2.array, ref, decimal=12)
Beispiel #2
0
    def test_box_kernels_even_size(self, width):
        """
        Check if BoxKernel work properly with even sizes.
        """
        kernel_1D = Box1DKernel(width)
        assert kernel_1D.shape[0] % 2 != 0
        assert kernel_1D.array.sum() == 1.

        kernel_2D = Box2DKernel(width)
        assert np.all([_ % 2 != 0 for _ in kernel_2D.shape])
        assert kernel_2D.array.sum() == 1.
Beispiel #3
0
    def test_smallkernel_vs_Box2DKernel(self, width):
        """
        Test smoothing of an image with a single positive pixel
        """
        kernel1 = np.ones([width, width]) / width**2
        kernel2 = Box2DKernel(width)

        c2 = convolve_fft(delta_pulse_2D, kernel2, boundary='fill')
        c1 = convolve_fft(delta_pulse_2D, kernel1, boundary='fill')

        assert_almost_equal(c1, c2, decimal=12)
Beispiel #4
0
    def test_custom_2D_kernel(self):
        """
        Check CustomKernel against Box2DKernel.
        """
        # Define one dimensional array:
        array = np.ones((5, 5))
        custom = CustomKernel(array)
        custom.normalize()
        box = Box2DKernel(5)

        c2 = convolve(delta_pulse_2D, custom, boundary='fill')
        c1 = convolve(delta_pulse_2D, box, boundary='fill')
        assert_almost_equal(c1, c2, decimal=12)
Beispiel #5
0
 def test_array_keyword_not_allowed(self):
     """
     Regression test for issue #10439
     """
     x = np.ones([10, 10])
     with pytest.raises(TypeError, match=r".* allowed .*"):
         AiryDisk2DKernel(2, array=x)
         Box1DKernel(2, array=x)
         Box2DKernel(2, array=x)
         Gaussian1DKernel(2, array=x)
         Gaussian2DKernel(2, array=x)
         RickerWavelet1DKernel(2, array=x)
         RickerWavelet2DKernel(2, array=x)
         Model1DKernel(Gaussian1D(1, 0, 2), array=x)
         Model2DKernel(Gaussian2D(1, 0, 0, 2, 2), array=x)
         Ring2DKernel(9, 8, array=x)
         Tophat2DKernel(2, array=x)
         Trapezoid1DKernel(2, array=x)
         Trapezoid1DKernel(2, array=x)
Beispiel #6
0
    def test_check_kernel_attributes(self):
        """
        Check if kernel attributes are correct.
        """
        box = Box2DKernel(5)

        # Check truncation
        assert box.truncation == 0

        # Check model
        assert isinstance(box.model, Box2D)

        # Check center
        assert box.center == [2, 2]

        # Check normalization
        box.normalize()
        assert_almost_equal(box._kernel_sum, 1., decimal=12)

        # Check separability
        assert box.separable
    def test_smallkernel_Box2DKernel(self, shape, width):
        """
        Test smoothing of an image with a single positive pixel

        Compares a small uniform kernel to the Box2DKernel
        """

        kernel1 = np.ones([width, width]) / float(width)**2
        kernel2 = Box2DKernel(width, mode='oversample', factor=10)

        x = np.zeros(shape)
        xslice = tuple([slice(sh // 2, sh // 2 + 1) for sh in shape])
        x[xslice] = 1.0

        c2 = convolve_fft(x, kernel2, boundary='fill')
        c1 = convolve_fft(x, kernel1, boundary='fill')

        assert_almost_equal(c1, c2, decimal=12)

        c2 = convolve(x, kernel2, boundary='fill')
        c1 = convolve(x, kernel1, boundary='fill')

        assert_almost_equal(c1, c2, decimal=12)
Beispiel #8
0
def image_std(image, radius):
    """
    Calculates the standard deviation of an image, using a moving window of specified radius.
    
    Arguments:
    -----------
        image: np.array
            2D array containing the pixel intensities of a single-band image
        radius: int
            radius defining the moving window used to calculate the standard deviation. For example,
            radius = 1 will produce a 3x3 moving window.
        
    Returns:    
    -----------
        win_std: np.array
            2D array containing the standard deviation of the image
        
    """  
    
    # convert to float
    image = image.astype(float)
    # first pad the image
    image_padded = np.pad(image, radius, 'reflect')
    # window size
    win_width = radius*2 + 1
    # calculate std
    kernel = Box2DKernel(width=win_width)
    win_mean = convolution.convolve(image_padded, kernel, normalization_zero_tol=0)
    win_sqr_mean = convolution.convolve(image_padded ** 2, kernel, normalization_zero_tol=0)


    win_var = win_sqr_mean - win_mean**2
    win_std = np.sqrt(win_var)
    # remove padding
    win_std = win_std[radius:-radius, radius:-radius]

    return win_std
KERNELS = []

for shape in SHAPES_ODD + NOSHAPE:
    for width in WIDTHS:

        KERNELS.append(
            Gaussian2DKernel(width,
                             x_size=shape[0],
                             y_size=shape[1],
                             mode='oversample',
                             factor=10))

        KERNELS.append(
            Box2DKernel(width,
                        x_size=shape[0],
                        y_size=shape[1],
                        mode='oversample',
                        factor=10))

        KERNELS.append(
            Tophat2DKernel(width,
                           x_size=shape[0],
                           y_size=shape[1],
                           mode='oversample',
                           factor=10))
        KERNELS.append(
            Moffat2DKernel(width,
                           2,
                           x_size=shape[0],
                           y_size=shape[1],
                           mode='oversample',