Ejemplo n.º 1
0
    def test_3d_cube_tied_values_random_seed(self):
        """
        Test that the plugin returns the correct cube data for a
        3d input cube, when there are tied values witin the
        raw ensemble realizations. The random seed is specified to ensure that
        only one option, out of the two possible options will be returned.
        """
        raw_data = np.array([[[1, 1]], [[3, 2]], [[2, 2]]])

        calibrated_data = np.array([[[1, 1]], [[2, 2]], [[3, 3]]])

        # Reordering of the calibrated_data array to match
        # the raw_data ordering
        result_data = np.array([[[1, 1]], [[3, 2]], [[2, 3]]])

        cube = self.cube.copy()
        cube = cube[:, :, :2, 0]

        raw_cube = cube.copy()
        raw_cube.data = raw_data

        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data
        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube, random_seed=0)
        self.assertArrayAlmostEqual(result.data, result_data)
Ejemplo n.º 2
0
    def test_2d_cube_random_ordering(self):
        """
        Test that the plugin returns the correct cube data for a
        2d input cube, if random ordering is selected.

        Random ordering does not use the ordering from the raw ensemble,
        and instead just orders the input values randomly.
        """
        raw_data = np.array([[3], [2], [1]])

        calibrated_data = np.array([[1], [2], [3]])

        cube = self.cube.copy()
        cube = cube[:, :, 0, 0]
        raw_cube = cube.copy()
        raw_cube.data = raw_data
        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube,
                                 raw_cube,
                                 random_ordering=True)

        permutations = list(itertools.permutations(raw_data))
        permutations = [np.array(permutation) for permutation in permutations]

        matches = [
            np.array_equal(aresult, result.data) for aresult in permutations
        ]
        self.assertIn(True, matches)
Ejemplo n.º 3
0
    def test_unordered_data(self):
        """
        Test that the plugin returns an iris.cube.Cube with the correct data.
        ECC orders the calibrated data based on the ordering of the raw data.
        This could mean that the calibrated data appears out of order.
        ECC does not reorder the calibrated data in a monotonically-increasing
        order.
        """
        raw_data = np.array([[[[5, 5, 5], [7, 5, 5], [5, 5, 5]]],
                             [[[4, 4, 4], [4, 4, 4], [4, 4, 4]]],
                             [[[6, 6, 6], [6, 6, 6], [6, 6, 6]]]])

        calibrated_data = np.array([[[[4, 5, 4], [4, 5, 4], [4, 5, 4]]],
                                    [[[5, 6, 5], [5, 6, 5], [5, 6, 5]]],
                                    [[[6, 7, 6], [6, 7, 6], [6, 7, 6]]]])

        # This reordering does not pay attention to the values within the
        # calibrated data, the rankings created to perform the sorting are
        # taken exclusively from the raw_data.
        result_data = np.array([[[[5, 6, 5], [6, 6, 5], [5, 6, 5]]],
                                [[[4, 5, 4], [4, 5, 4], [4, 5, 4]]],
                                [[[6, 7, 6], [5, 7, 6], [6, 7, 6]]]])

        raw_cube = self.cube.copy()
        raw_cube.data = raw_data
        calibrated_cube = self.cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        self.assertArrayAlmostEqual(result.data, result_data)
Ejemplo n.º 4
0
    def test_3d_cube_tied_values(self):
        """
        Test that the plugin returns the correct cube data for a
        3d input cube, when there are tied values witin the
        raw ensemble realizations. As there are two possible options for the
        result data, as the tie is decided randomly, both possible result
        data options are checked.
        """
        raw_data = np.array([[[1, 1]], [[3, 2]], [[2, 2]]])

        calibrated_data = np.array([[[1, 1]], [[2, 2]], [[3, 3]]])

        # Reordering of the calibrated_data array to match
        # the raw_data ordering
        result_data_first = np.array([[[1, 1]], [[3, 2]], [[2, 3]]])

        result_data_second = np.array([[[1, 1]], [[3, 3]], [[2, 2]]])

        cube = self.cube.copy()
        cube = cube[:, :, :2, 0]

        raw_cube = cube.copy()
        raw_cube.data = raw_data

        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data
        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        permutations = [result_data_first, result_data_second]
        matches = [
            np.array_equal(aresult, result.data) for aresult in permutations
        ]
        self.assertIn(True, matches)
Ejemplo n.º 5
0
    def test_2d_cube(self):
        """
        Test that the plugin returns the correct cube data for a
        2d input cube.
        """
        raw_data = np.array([[3],
                             [2],
                             [1]])

        calibrated_data = np.array([[1],
                                    [2],
                                    [3]])

        result_data = np.array([[3],
                                [2],
                                [1]])

        cube = self.cube.copy()
        cube = cube[:, :, 0, 0]
        raw_cube = cube.copy()
        raw_cube.data = raw_data
        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        self.assertArrayAlmostEqual(result.data, result_data)
Ejemplo n.º 6
0
    def test_3d_cube(self):
        """Test that the plugin returns the correct cube data for a
        3d input cube."""
        raw_data = np.array(
            [[[1, 1]],
             [[3, 2]],
             [[2, 3]]])

        calibrated_data = np.array(
            [[[1, 1]],
             [[2, 2]],
             [[3, 3]]])

        # Reordering of the calibrated_data array to match
        # the raw_data ordering
        result_data = np.array(
            [[[1, 1]],
             [[3, 2]],
             [[2, 3]]])

        cube = self.cube.copy()
        cube = cube[:, :, :2, 0]

        raw_cube = cube.copy()
        raw_cube.data = raw_data

        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)

        self.assertArrayAlmostEqual(result.data, result_data)
Ejemplo n.º 7
0
    def test_ordered_data(self):
        """
        Test that the plugin returns an Iris Cube where the cube data is an
        ordered numpy array for the calibrated data with the same ordering
        as the raw data.
        """
        raw_data = np.array([[[[1, 1, 1],
                               [1, 1, 1],
                               [1, 1, 1]]],
                             [[[2, 2, 2],
                               [2, 2, 2],
                               [2, 2, 2]]],
                             [[[3, 3, 3],
                               [3, 3, 3],
                               [3, 3, 3]]]])

        calibrated_data = raw_data

        raw_cube = self.cube.copy()
        raw_cube.data = raw_data
        calibrated_cube = self.cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        self.assertArrayAlmostEqual(result.data, calibrated_data)
Ejemplo n.º 8
0
    def test_basic(self):
        """Test that the plugin returns an iris.cube.Cube."""
        raw_data = np.array([[[[1, 1, 1],
                               [1, 1, 1],
                               [1, 1, 1]]],
                             [[[2, 2, 2],
                               [2, 2, 2],
                               [2, 2, 2]]],
                             [[[3, 3, 3],
                               [3, 3, 3],
                               [3, 3, 3]]]])
        calibrated_data = np.array(
            [[[[0.71844843, 0.71844843, 0.71844843],
               [0.71844843, 0.71844843, 0.71844843],
               [0.71844843, 0.71844843, 0.71844843]]],
             [[[2., 2., 2.],
               [2., 2., 2.],
               [2., 2., 2.]]],
             [[[3.28155157, 3.28155157, 3.28155157],
               [3.28155157, 3.28155157, 3.28155157],
               [3.28155157, 3.28155157, 3.28155157]]]])

        raw_cube = self.cube.copy()
        raw_cube.data = raw_data
        calibrated_cube = self.cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        self.assertIsInstance(result, Cube)
Ejemplo n.º 9
0
    def test_3d_cube_masked_nans(self):
        """Test that the plugin returns the correct cube data for a
        3d input cube with a mask applied to each realization, and there are
        nans under the mask."""
        mask = np.array([[[True, False]], [[True, False]], [[True, False]]])
        raw_data = np.array([[[1, 9]], [[3, 5]], [[2, 7]]])

        calibrated_data = np.ma.MaskedArray(
            [[[np.nan, 6]], [[np.nan, 8]], [[np.nan, 10]]],
            mask=mask,
            dtype=np.float32)

        # Reordering of the calibrated_data array to match
        # the raw_data ordering
        result_data = np.array([[[np.nan, 10]], [[np.nan, 6]], [[np.nan, 8]]],
                               dtype=np.float32)

        cube = self.cube[:, :, :2, 0].copy()

        raw_cube = cube.copy()
        raw_cube.data = raw_data

        calibrated_cube = cube.copy()
        calibrated_cube.data = calibrated_data

        plugin = Plugin()
        result = plugin.rank_ecc(calibrated_cube, raw_cube)
        self.assertArrayAlmostEqual(result.data.data, result_data)
        self.assertArrayEqual(result.data.mask, mask)
        self.assertEqual(result.data.dtype, np.float32)