From 74e8290015a2ebedd4c3d2c7cf16214c2a871d1d Mon Sep 17 00:00:00 2001 From: Bill Little Date: Wed, 3 May 2017 10:50:07 +0100 Subject: [PATCH 1/7] Fix concatenate fill value. --- lib/iris/_concatenate.py | 21 ++-- .../concatenate/concat_masked_2x2d.cml | 2 +- .../concatenate/concat_masked_2y2d.cml | 2 +- lib/iris/tests/test_concatenate.py | 97 ++++++++----------- 4 files changed, 49 insertions(+), 73 deletions(-) diff --git a/lib/iris/_concatenate.py b/lib/iris/_concatenate.py index 8b41936e98..ef1b93274d 100644 --- a/lib/iris/_concatenate.py +++ b/lib/iris/_concatenate.py @@ -416,7 +416,6 @@ def match(self, other, error_on_mismatch): - scalar coords - attributes - dtype - - fill_value Args: @@ -465,14 +464,6 @@ def match(self, other, error_on_mismatch): msgs.append(msg_template.format('Data types', '', self.data_type, other.data_type)) - # Check fill value. - if self.fill_value != other.fill_value: - # Allow fill-value promotion if either fill-value is None. - if self.fill_value is not None and other.fill_value is not None: - msgs.append(msg_template.format('Fill values', '', - self.fill_value, - other.fill_value)) - # Check _cell_measures_and_dims if self.cell_measures_and_dims != other.cell_measures_and_dims: msgs.append(msg_template.format('CellMeasures', '', @@ -727,10 +718,14 @@ def register(self, cube, axis=None, error_on_mismatch=False, # Check for compatible coordinate signatures. if match: - if self._cube_signature.fill_value is None and \ - cube_signature.fill_value is not None: - # Perform proto-cube fill-value promotion. - self._cube_signature.fill_value = cube_signature.fill_value + fill_value = self._cube_signature.fill_value + # Determine whether the fill value requires to be + # demoted to the default value. + if fill_value is not None: + if cube_signature.fill_value is None or \ + cube_signature.fill_value != fill_value: + # Demote the fill value to the default. + self._cube_signature.fill_value = None coord_signature = _CoordSignature(cube_signature) candidate_axis = self._coord_signature.candidate_axis( coord_signature) diff --git a/lib/iris/tests/results/concatenate/concat_masked_2x2d.cml b/lib/iris/tests/results/concatenate/concat_masked_2x2d.cml index 92d00b09a5..d1fa542249 100644 --- a/lib/iris/tests/results/concatenate/concat_masked_2x2d.cml +++ b/lib/iris/tests/results/concatenate/concat_masked_2x2d.cml @@ -1,6 +1,6 @@ - + - + Date: Wed, 3 May 2017 12:41:45 +0100 Subject: [PATCH 3/7] Added merge invariant tests. --- lib/iris/tests/test_merge.py | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/iris/tests/test_merge.py b/lib/iris/tests/test_merge.py index a6efcc6f1e..60bc59c6f0 100644 --- a/lib/iris/tests/test_merge.py +++ b/lib/iris/tests/test_merge.py @@ -244,6 +244,45 @@ def test__masked_ndarray(self): self.assertEqual(result.dtype, self.dtype) self._check_fill_value(result, fill0, fill1) + def test_fill_value_invariant_to_order__same_non_None(self): + fill_value = 1234 + cubes = [self._make_cube(i, mask=True, + fill_value=fill_value) for i in range(3)] + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).merge_cube() + self.assertEqual(result.fill_value, fill_value) + self.assertEqual(result.data.fill_value, fill_value) + + def test_fill_value_invariant_to_order__all_None(self): + cubes = [self._make_cube(i, mask=True, + fill_value=None) for i in range(3)] + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).merge_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + + def test_fill_value_invariant_to_order__different_non_None(self): + cubes = [self._make_cube(0, mask=True, fill_value=1234)] + cubes.append(self._make_cube(1, mask=True, fill_value=2341)) + cubes.append(self._make_cube(2, mask=True, fill_value=3412)) + cubes.append(self._make_cube(3, mask=True, fill_value=4123)) + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).merge_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + + def test_fill_value_invariant_to_order__mixed(self): + cubes = [self._make_cube(0, mask=True, fill_value=None)] + cubes.append(self._make_cube(1, mask=True, fill_value=1234)) + cubes.append(self._make_cube(2, mask=True, fill_value=4321)) + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).merge_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + @tests.skip_data class TestDataMerge(tests.IrisTest): From fb0a3a51b308d7966c408b6ec2e498667cff29c9 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Wed, 3 May 2017 12:58:31 +0100 Subject: [PATCH 4/7] Added concatenate invariant tests. --- lib/iris/tests/test_concatenate.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/iris/tests/test_concatenate.py b/lib/iris/tests/test_concatenate.py index 85e5181db6..24c7a49675 100644 --- a/lib/iris/tests/test_concatenate.py +++ b/lib/iris/tests/test_concatenate.py @@ -26,6 +26,7 @@ # before importing anything else. import iris.tests as tests +import itertools import numpy as np import numpy.ma as ma @@ -729,6 +730,49 @@ def test_concat_2x2d_aux_xy_bounds(self): self.assertEqual(len(result), 1) self.assertEqual(result[0].shape, (2, 4)) + def test_fill_value_invariant_to_order__same_non_None(self): + y = (0, 2) + fill_value = 1234 + cubes = [_make_cube((i*2, i*2+2), y, i, mask=True, + fill_value=fill_value) for i in range(3)] + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).concatenate_cube() + self.assertEqual(result.fill_value, fill_value) + self.assertEqual(result.data.fill_value, fill_value) + + def test_fill_value_invariant_to_order__all_None(self): + y = (0, 2) + cubes = [_make_cube((i*2, i*2+2), y, i, mask=True, + fill_value=None) for i in range(3)] + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).concatenate_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + + def test_fill_value_invariant_to_order__different_non_None(self): + y = (0, 2) + cubes = [_make_cube((0, 2), y, 0, mask=True, fill_value=1234)] + cubes.append(_make_cube((2, 4), y, 1, mask=True, fill_value=2341)) + cubes.append(_make_cube((4, 6), y, 2, mask=True, fill_value=3412)) + cubes.append(_make_cube((6, 8), y, 3, mask=True, fill_value=4123)) + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).concatenate_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + + def test_fill_value_invariant_to_order__mixed(self): + y = (0, 2) + cubes = [_make_cube((0, 2), y, 0, mask=True, fill_value=None)] + cubes.append(_make_cube((2, 4), y, 1, mask=True, fill_value=1234)) + cubes.append(_make_cube((4, 6), y, 2, mask=True, fill_value=4321)) + for combo in itertools.permutations(cubes): + result = iris.cube.CubeList(combo).concatenate_cube() + self.assertIsNone(result.fill_value) + np_fill_value = ma.masked_array(0, dtype=result.dtype).fill_value + self.assertEqual(result.data.fill_value, np_fill_value) + class TestMulti2D(tests.IrisTest): def test_concat_4x2d_aux_xy(self): From e222205d9e542ebcb216d1129540c70b0d5ba9ac Mon Sep 17 00:00:00 2001 From: Bill Little Date: Thu, 4 May 2017 11:57:16 +0100 Subject: [PATCH 5/7] Review actions. --- .../tests/results/concatenate/concat_2y2d.cml | 2 +- lib/iris/tests/test_concatenate.py | 46 +++++++++++++++---- lib/iris/tests/test_merge.py | 10 ++-- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/iris/tests/results/concatenate/concat_2y2d.cml b/lib/iris/tests/results/concatenate/concat_2y2d.cml index 9c11ea11ba..533cadc621 100644 --- a/lib/iris/tests/results/concatenate/concat_2y2d.cml +++ b/lib/iris/tests/results/concatenate/concat_2y2d.cml @@ -1,6 +1,6 @@ - +