Exemplo n.º 1
0
 def test_external_mask_square(self):
     """Test the _calculate_neighbourhood method when an external mask is
     passed in and re-masking is applied."""
     plugin = NeighbourhoodProcessing("square", self.RADIUS)
     plugin.nb_size = self.nbhood_size
     result = plugin._calculate_neighbourhood(self.data_for_masked_tests,
                                              mask=self.mask)
     self.assertArrayAlmostEqual(result.data, self.expected_array)
     self.assertArrayAlmostEqual(result.mask, self.expected_mask)
Exemplo n.º 2
0
    def test_masked_array_re_mask_true_square(self):
        """Test the _calculate_neighbourhood method when masked data is
        passed in and re-masking is applied."""

        input_data = np.ma.masked_where(self.mask == 0,
                                        self.data_for_masked_tests)
        plugin = NeighbourhoodProcessing("square", self.RADIUS)
        plugin.nb_size = self.nbhood_size
        result = plugin._calculate_neighbourhood(input_data)
        self.assertArrayAlmostEqual(result.data, self.expected_array)
        self.assertArrayAlmostEqual(result.mask, self.expected_mask)
Exemplo n.º 3
0
    def test_masked_array_re_mask_false(self):
        """Test the _calculate_neighbourhood method when masked data is
        passed in and re-masking is not applied."""

        input_data = np.ma.masked_where(self.mask == 0,
                                        self.data_for_masked_tests)
        plugin = NeighbourhoodProcessing("square", self.RADIUS, re_mask=False)
        plugin.nb_size = self.nbhood_size
        result = plugin._calculate_neighbourhood(input_data)
        self.assertArrayAlmostEqual(result, self.expected_array)
        with self.assertRaises(AttributeError):
            result.mask
Exemplo n.º 4
0
 def test_basic_circular(self):
     """Test the _calculate_neighbourhood method with a circular neighbourhood."""
     expected_array = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 1.0, 0.8, 1.0, 1.0],
         [1.0, 0.8, 0.8, 0.8, 1.0],
         [1.0, 1.0, 0.8, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     plugin = NeighbourhoodProcessing("circular", self.RADIUS)
     plugin.kernel = self.circular_kernel
     result = plugin._calculate_neighbourhood(self.data)
     self.assertArrayAlmostEqual(result.data, expected_array)
Exemplo n.º 5
0
 def test_basic_square(self):
     """Test the _calculate_neighbourhood method with a square neighbourhood."""
     expected_array = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 8 / 9, 8 / 9, 8 / 9, 1.0],
         [1.0, 8 / 9, 8 / 9, 8 / 9, 1.0],
         [1.0, 8 / 9, 8 / 9, 8 / 9, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     plugin = NeighbourhoodProcessing("square", self.RADIUS)
     plugin.nb_size = self.nbhood_size
     result = plugin._calculate_neighbourhood(self.data)
     self.assertArrayAlmostEqual(result, expected_array)
Exemplo n.º 6
0
 def test_basic_square_sum(self):
     """Test the _calculate_neighbourhood method calculating a sum in
     a square neighbourhood."""
     expected_array = np.array([
         [4.0, 6.0, 6.0, 6.0, 4.0],
         [6.0, 8.0, 8.0, 8.0, 6.0],
         [6.0, 8.0, 8.0, 8.0, 6.0],
         [6.0, 8.0, 8.0, 8.0, 6.0],
         [4.0, 6.0, 6.0, 6.0, 4.0],
     ])
     plugin = NeighbourhoodProcessing("square", self.RADIUS, sum_only=True)
     plugin.nb_size = self.nbhood_size
     result = plugin._calculate_neighbourhood(self.data)
     self.assertArrayAlmostEqual(result, expected_array)
Exemplo n.º 7
0
 def test_basic_circular_sum(self):
     """Test the _calculate_neighbourhood method calculating a sum in
     a circular neighbourhood."""
     expected_array = np.array([
         [5.0, 5.0, 5.0, 5.0, 5.0],
         [5.0, 5.0, 4.0, 5.0, 5.0],
         [5.0, 4.0, 4.0, 4.0, 5.0],
         [5.0, 5.0, 4.0, 5.0, 5.0],
         [5.0, 5.0, 5.0, 5.0, 5.0],
     ])
     plugin = NeighbourhoodProcessing("circular",
                                      self.RADIUS,
                                      sum_only=True)
     plugin.kernel = self.circular_kernel
     result = plugin._calculate_neighbourhood(self.data)
     self.assertArrayAlmostEqual(result.data, expected_array)
Exemplo n.º 8
0
    def test_masked_array_re_mask_true_circular(self):
        """Test the _calculate_neighbourhood method when masked data is
        passed in and re-masking is applied with a circular neighbourhood."""

        expected_array = np.array([
            [np.nan, 0.5, 0.5, 0.5, 1.0],
            [1.0, 1.0, 0.6, 0.5, 0.0],
            [np.nan, 1.0, 0.75, 0.4, 0.0],
            [np.nan, 1.0, 1.0, 0.5, 0.5],
            [np.nan, 1.0, 0.75, 0.5, 0.0],
        ])
        input_data = np.ma.masked_where(self.mask == 0,
                                        self.data_for_masked_tests)
        plugin = NeighbourhoodProcessing("circular", self.RADIUS)
        plugin.kernel = self.circular_kernel
        result = plugin._calculate_neighbourhood(input_data)

        self.assertArrayAlmostEqual(result.data, expected_array)
        self.assertArrayAlmostEqual(result.mask, self.expected_mask)
Exemplo n.º 9
0
 def test_complex(self):
     """Test that data containing complex numbers is sensibly processed"""
     self.data = self.data.astype(complex)
     self.data[1, 3] = 0.5 + 0.5j
     self.data[4, 3] = 0.4 + 0.6j
     expected_array = np.array([
         [
             1.0 + 0.0j,
             1.0 + 0.0j,
             0.91666667 + 0.083333333j,
             0.91666667 + 0.083333333j,
             0.875 + 0.125j,
         ],
         [
             1.0 + 0.0j,
             0.88888889 + 0.0j,
             0.83333333 + 0.055555556j,
             0.83333333 + 0.055555556j,
             0.91666667 + 0.083333333j,
         ],
         [
             1.0 + 0.0j,
             0.88888889 + 0.0j,
             0.83333333 + 0.055555556j,
             0.83333333 + 0.055555556j,
             0.91666667 + 0.083333333j,
         ],
         [
             1.0 + 0.0j,
             0.88888889 + 0.0j,
             0.82222222 + 0.066666667j,
             0.82222222 + 0.066666667j,
             0.9 + 0.1j,
         ],
         [1.0 + 0.0j, 1.0 + 0.0j, 0.9 + 0.1j, 0.9 + 0.1j, 0.85 + 0.15j],
     ])
     plugin = NeighbourhoodProcessing("square", self.RADIUS)
     plugin.nb_size = self.nbhood_size
     result = plugin._calculate_neighbourhood(self.data)
     self.assertArrayAlmostEqual(result, expected_array)
Exemplo n.º 10
0
    def test_external_mask_with_masked_data_square(self):
        """Test the _calculate_neighbourhood method when masked data is
        passed in and an external mask is passed in and re-masking is applied."""
        mask = np.array([
            [1, 0, 1, 1, 0],
            [1, 1, 1, 1, 0],
            [1, 0, 1, 1, 1],
            [1, 0, 1, 1, 0],
            [1, 0, 1, 1, 0],
        ])
        external_mask = np.array([
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
        ])

        self.data = np.ma.masked_where(mask == 0, self.data_for_masked_tests)
        plugin = NeighbourhoodProcessing("square", self.RADIUS)
        plugin.nb_size = self.nbhood_size
        result = plugin._calculate_neighbourhood(self.data, external_mask)
        self.assertArrayAlmostEqual(result.data, self.expected_array)
        self.assertArrayAlmostEqual(result.mask, self.expected_mask)