def test_weighted_sum_consistency(self): # weighted sum with unit weights should be the same as a sum cube = tests.stock.simple_1d() normal_sum = cube.collapsed('foo', iris.analysis.SUM) weights = np.ones_like(cube.data) weighted_sum = cube.collapsed('foo', iris.analysis.SUM, weights=weights) self.assertArrayAlmostEqual(normal_sum.data, weighted_sum.data)
def test_fast_percentile_3d_masked(self): cube = tests.stock.simple_3d_mask() msg = 'Cannot use fast np.percentile method with masked array.' with self.assertRaisesRegexp(TypeError, msg): cube.collapsed('wibble', iris.analysis.PERCENTILE, percent=75, fast_percentile_method=True)
def test_percentile_1d(self): cube = tests.stock.simple_1d() first_quartile = cube.collapsed("foo", iris.analysis.PERCENTILE, percent=25) np.testing.assert_array_almost_equal(first_quartile.data, np.array([2.5], dtype=np.float32)) self.assertCML(first_quartile, ("analysis", "first_quartile_foo_1d.cml"), checksum=False) third_quartile = cube.collapsed("foo", iris.analysis.PERCENTILE, percent=75) np.testing.assert_array_almost_equal(third_quartile.data, np.array([7.5], dtype=np.float32)) self.assertCML(third_quartile, ("analysis", "third_quartile_foo_1d.cml"), checksum=False)
def test_percentile_2d(self): cube = tests.stock.simple_2d() first_quartile = cube.collapsed("foo", iris.analysis.PERCENTILE, percent=25) np.testing.assert_array_almost_equal(first_quartile.data, np.array([0.75, 4.75, 8.75], dtype=np.float32)) self.assertCML(first_quartile, ("analysis", "first_quartile_foo_2d.cml"), checksum=False) first_quartile = cube.collapsed(("foo", "bar"), iris.analysis.PERCENTILE, percent=25) np.testing.assert_array_almost_equal(first_quartile.data, np.array([2.75], dtype=np.float32)) self.assertCML(first_quartile, ("analysis", "first_quartile_foo_bar_2d.cml"), checksum=False)
def test_percentile_2d(self): cube = tests.stock.simple_2d() first_quartile = cube.collapsed('foo', iris.analysis.PERCENTILE, percent=25) numpy.testing.assert_array_almost_equal(first_quartile.data, numpy.array([0.75, 4.75, 8.75], dtype=numpy.float32)) self.assertCML(first_quartile, ('analysis', 'first_quartile_foo_2d.cml'), checksum=False) first_quartile = cube.collapsed(('foo', 'bar'), iris.analysis.PERCENTILE, percent=25) numpy.testing.assert_array_almost_equal(first_quartile.data, numpy.array([2.75], dtype=numpy.float32)) self.assertCML(first_quartile, ('analysis', 'first_quartile_foo_bar_2d.cml'), checksum=False)
def test_percentile_1d(self): cube = tests.stock.simple_1d() first_quartile = cube.collapsed('foo', iris.analysis.PERCENTILE, percent=25) numpy.testing.assert_array_almost_equal(first_quartile.data, numpy.array([2.5], dtype=numpy.float32)) self.assertCML(first_quartile, ('analysis', 'first_quartile_foo_1d.cml'), checksum=False) third_quartile = cube.collapsed('foo', iris.analysis.PERCENTILE, percent=75) numpy.testing.assert_array_almost_equal(third_quartile.data, numpy.array([7.5], dtype=numpy.float32)) self.assertCML(third_quartile, ('analysis', 'third_quartile_foo_1d.cml'), checksum=False)
def test_count_2d(self): cube = tests.stock.simple_2d() gt6 = cube.collapsed('foo', iris.analysis.COUNT, function=lambda val: val >= 6) numpy.testing.assert_array_almost_equal(gt6.data, numpy.array([0, 2, 4], dtype=numpy.float32)) self.assertCML(gt6, ('analysis', 'count_foo_2d.cml'), checksum=False) gt6 = cube.collapsed('bar', iris.analysis.COUNT, function=lambda val: val >= 6) numpy.testing.assert_array_almost_equal(gt6.data, numpy.array([1, 1, 2, 2], dtype=numpy.float32)) self.assertCML(gt6, ('analysis', 'count_bar_2d.cml'), checksum=False) gt6 = cube.collapsed(('foo', 'bar'), iris.analysis.COUNT, function=lambda val: val >= 6) numpy.testing.assert_array_almost_equal(gt6.data, numpy.array([6], dtype=numpy.float32)) self.assertCML(gt6, ('analysis', 'count_foo_bar_2d.cml'), checksum=False)
def test_weighted_mean_little(self): data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) weights = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]], dtype=np.float32) cube = iris.cube.Cube(data, long_name="test_data", units="1") hcs = iris.coord_systems.GeogCS(6371229) lat_coord = iris.coords.DimCoord(np.array([1, 2, 3], dtype=np.float32), long_name="lat", units="1", coord_system=hcs) lon_coord = iris.coords.DimCoord(np.array([1, 2, 3], dtype=np.float32), long_name="lon", units="1", coord_system=hcs) cube.add_dim_coord(lat_coord, 0) cube.add_dim_coord(lon_coord, 1) cube.add_aux_coord(iris.coords.AuxCoord(np.arange(3, dtype=np.float32), long_name="dummy", units=1), 1) self.assertCML(cube, ('analysis', 'weighted_mean_source.cml')) a = cube.collapsed('lat', iris.analysis.MEAN, weights=weights) # np.ma.average doesn't apply type promotion rules in some versions, # and instead makes the result type float64. To ignore that case we # fix up the dtype here if it is promotable from float32. We still want # to catch cases where there is a loss of precision however. if a.dtype > np.float32: cast_data = a.data.astype(np.float32) a.replace(cast_data, fill_value=a.fill_value) self.assertCMLApproxData(a, ('analysis', 'weighted_mean_lat.cml')) b = cube.collapsed(lon_coord, iris.analysis.MEAN, weights=weights) if b.dtype > np.float32: cast_data = b.data.astype(np.float32) b.replace(cast_data, fill_value=b.fill_value) b.data = np.asarray(b.data) self.assertCMLApproxData(b, ('analysis', 'weighted_mean_lon.cml')) self.assertEqual(b.coord('dummy').shape, (1, )) # test collapsing multiple coordinates (and the fact that one of the coordinates isn't the same coordinate instance as on the cube) c = cube.collapsed([lat_coord[:], lon_coord], iris.analysis.MEAN, weights=weights) if c.dtype > np.float32: cast_data = c.data.astype(np.float32) c.replace(cast_data, fill_value=c.fill_value) self.assertCMLApproxData(c, ('analysis', 'weighted_mean_latlon.cml')) self.assertEqual(c.coord('dummy').shape, (1, )) # Check new coord bounds - made from points self.assertArrayEqual(c.coord('lat').bounds, [[1, 3]]) # Check new coord bounds - made from bounds cube.coord('lat').bounds = [[0.5, 1.5], [1.5, 2.5], [2.5, 3.5]] c = cube.collapsed(['lat', 'lon'], iris.analysis.MEAN, weights=weights) self.assertArrayEqual(c.coord('lat').bounds, [[0.5, 3.5]]) cube.coord('lat').bounds = None # Check there was no residual change self.assertCML(cube, ('analysis', 'weighted_mean_source.cml'))
def collapse_test_common(self, cube, a_name, b_name, *args, **kwargs): # preserve filenames from before the introduction of "grid_" in rotated coord names. a_filename = a_name.replace("grid_", "") b_filename = b_name.replace("grid_", "") # compare dual and single stage collapsing dual_stage = cube.collapsed(a_name, iris.analysis.MEAN) dual_stage = dual_stage.collapsed(b_name, iris.analysis.MEAN) self.assertCMLApproxData(dual_stage, ('cube_collapsed', '%s_%s_dual_stage.cml' % (a_filename, b_filename)), *args, **kwargs) single_stage = cube.collapsed([a_name, b_name], iris.analysis.MEAN) self.assertCMLApproxData(single_stage, ('cube_collapsed', '%s_%s_single_stage.cml' % (a_filename, b_filename)), *args, **kwargs) # Compare the cube bits that should match self.partial_compare(dual_stage, single_stage)
def test_weighted_sum_1d(self): # verify 1d weighted sum is correct cube = tests.stock.simple_1d() weights = np.array([0.05, 0.05, 0.1, 0.1, 0.2, 0.3, 0.2, 0.1, 0.1, 0.05, 0.05]) result = cube.collapsed("foo", iris.analysis.SUM, weights=weights) self.assertAlmostEqual(result.data, 6.5) self.assertCML(result, ("analysis", "sum_weighted_1d.cml"), checksum=False)
def test_count_2d(self): cube = tests.stock.simple_2d() gt6 = cube.collapsed("foo", iris.analysis.COUNT, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([0, 2, 4], dtype=np.float32)) gt6.data = gt6.data.astype("i8") self.assertCML(gt6, ("analysis", "count_foo_2d.cml"), checksum=False) gt6 = cube.collapsed("bar", iris.analysis.COUNT, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([1, 1, 2, 2], dtype=np.float32)) gt6.data = gt6.data.astype("i8") self.assertCML(gt6, ("analysis", "count_bar_2d.cml"), checksum=False) gt6 = cube.collapsed(("foo", "bar"), iris.analysis.COUNT, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([6], dtype=np.float32)) gt6.data = gt6.data.astype("i8") self.assertCML(gt6, ("analysis", "count_foo_bar_2d.cml"), checksum=False)
def test_weighted_sum_1d(self): # verify 1d weighted sum is correct cube = tests.stock.simple_1d() weights = np.array([.05, .05, .1, .1, .2, .3, .2, .1, .1, .05, .05]) result = cube.collapsed('foo', iris.analysis.SUM, weights=weights) self.assertAlmostEqual(result.data, 6.5) self.assertCML(result, ('analysis', 'sum_weighted_1d.cml'), checksum=False)
def test_weighted_sum_2d(self): # verify 2d weighted sum is correct cube = tests.stock.simple_2d() weights = np.array([0.3, 0.4, 0.3]) weights = iris.util.broadcast_to_shape(weights, cube.shape, [0]) result = cube.collapsed("bar", iris.analysis.SUM, weights=weights) self.assertArrayAlmostEqual(result.data, np.array([4.0, 5.0, 6.0, 7.0])) self.assertCML(result, ("analysis", "sum_weighted_2d.cml"), checksum=False)
def test_percentile_3d_notmasked(self): cube = tests.stock.simple_3d() last_quartile = cube.collapsed("wibble", iris.analysis.PERCENTILE, percent=75) np.testing.assert_array_almost_equal( last_quartile.data, np.array([[9.0, 10.0, 11.0, 12.0], [13.0, 14.0, 15.0, 16.0], [17.0, 18.0, 19.0, 20.0]], dtype=np.float32), ) self.assertCML(last_quartile, ("analysis", "last_quartile_foo_3d_notmasked.cml"), checksum=False)
def _check_collapsed_percentile(self, cube, percents, collapse_coord, expected_result, CML_filename=None, **kwargs): expected_result = np.array(expected_result, dtype=np.float32) result = cube.collapsed(collapse_coord, iris.analysis.PERCENTILE, percent=percents, **kwargs) np.testing.assert_array_almost_equal(result.data, expected_result) if CML_filename is not None: self.assertCML(result, ('analysis', CML_filename), checksum=False)
def test_weighted_sum_2d(self): # verify 2d weighted sum is correct cube = tests.stock.simple_2d() weights = np.array([.3, .4, .3]) weights = iris.util.broadcast_to_shape(weights, cube.shape, [0]) result = cube.collapsed('bar', iris.analysis.SUM, weights=weights) self.assertArrayAlmostEqual(result.data, np.array([4., 5., 6., 7.])) self.assertCML(result, ('analysis', 'sum_weighted_2d.cml'), checksum=False)
def test_weighted_rms(self): cube = tests.stock.simple_2d() # modify cube data so that the results are nice numbers cube.data = np.array([[4, 7, 10, 8], [21, 30, 12, 24], [14, 16, 20, 8]], dtype=np.float64) weights = np.array([[1, 4, 3, 2], [6, 4.5, 1.5, 3], [2, 1, 1.5, 0.5]], dtype=np.float64) expected_result = np.array([8.0, 24.0, 16.0]) result = cube.collapsed("foo", iris.analysis.RMS, weights=weights) self.assertArrayAlmostEqual(result.data, expected_result) self.assertCML(result, ("analysis", "rms_weighted_2d.cml"), checksum=False)
def test_multi_d(self): cube = iris.tests.stock.realistic_4d() # TODO: Re-instate surface_altitude & hybrid-height once we're # using the post-CF test results. cube.remove_aux_factory(cube.aux_factories[0]) cube.remove_coord('surface_altitude') self.assertCML(cube, ('cube_collapsed', 'original.cml')) # Compare 2-stage collapsing with a single stage collapse over 2 Coords. self.collapse_test_common(cube, 'grid_latitude', 'grid_longitude', decimal=1) self.collapse_test_common(cube, 'grid_longitude', 'grid_latitude', decimal=1) self.collapse_test_common(cube, 'time', 'grid_latitude', decimal=1) self.collapse_test_common(cube, 'grid_latitude', 'time', decimal=1) self.collapse_test_common(cube, 'time', 'grid_longitude', decimal=1) self.collapse_test_common(cube, 'grid_longitude', 'time', decimal=1) self.collapse_test_common(cube, 'grid_latitude', 'model_level_number', decimal=1) self.collapse_test_common(cube, 'model_level_number', 'grid_latitude', decimal=1) self.collapse_test_common(cube, 'grid_longitude', 'model_level_number', decimal=1) self.collapse_test_common(cube, 'model_level_number', 'grid_longitude', decimal=1) self.collapse_test_common(cube, 'time', 'model_level_number', decimal=1) self.collapse_test_common(cube, 'model_level_number', 'time', decimal=1) self.collapse_test_common(cube, 'model_level_number', 'time', decimal=1) self.collapse_test_common(cube, 'time', 'model_level_number', decimal=1) # Collapse 3 things at once. triple_collapse = cube.collapsed(['model_level_number', 'time', 'grid_longitude'], iris.analysis.MEAN) self.assertCMLApproxData(triple_collapse, ('cube_collapsed', 'triple_collapse_ml_pt_lon.cml'), decimal=1) triple_collapse = cube.collapsed(['grid_latitude', 'model_level_number', 'time'], iris.analysis.MEAN) self.assertCMLApproxData(triple_collapse, ('cube_collapsed', 'triple_collapse_lat_ml_pt.cml'), decimal=1) # Ensure no side effects self.assertCML(cube, ('cube_collapsed', 'original.cml'))
def test_proportion_2d(self): cube = tests.stock.simple_2d() gt6 = cube.collapsed('foo', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal( gt6.data, np.array([0, 0.5, 1], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_foo_2d.cml'), checksum=False) gt6 = cube.collapsed('bar', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal( gt6.data, np.array([1 / 3, 1 / 3, 2 / 3, 2 / 3], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_bar_2d.cml'), checksum=False) gt6 = cube.collapsed(('foo', 'bar'), iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([0.5], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_foo_bar_2d.cml'), checksum=False) # mask the data cube.data = ma.array(cube.data, mask=cube.data % 2) cube.data.mask[1, 2] = True gt6_masked = cube.collapsed('bar', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal( gt6_masked.data, ma.array([1 / 3, None, 1 / 2, None], mask=[False, True, False, True], dtype=np.float32)) self.assertCML(gt6_masked, ('analysis', 'proportion_foo_2d_masked.cml'), checksum=False)
def test_weighted_mean_little(self): data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) weights = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]], dtype=np.float32) cube = iris.cube.Cube(data, long_name="test_data", units="1") hcs = iris.coord_systems.GeogCS(6371229) lat_coord = iris.coords.DimCoord( np.array([1, 2, 3], dtype=np.float32), long_name="lat", units="1", coord_system=hcs ) lon_coord = iris.coords.DimCoord( np.array([1, 2, 3], dtype=np.float32), long_name="lon", units="1", coord_system=hcs ) cube.add_dim_coord(lat_coord, 0) cube.add_dim_coord(lon_coord, 1) cube.add_aux_coord(iris.coords.AuxCoord(np.arange(3, dtype=np.float32), long_name="dummy", units=1), 1) self.assertCML(cube, ("analysis", "weighted_mean_source.cml")) a = cube.collapsed("lat", iris.analysis.MEAN, weights=weights) self.assertCMLApproxData(a, ("analysis", "weighted_mean_lat.cml")) b = cube.collapsed(lon_coord, iris.analysis.MEAN, weights=weights) b.data = np.asarray(b.data) self.assertCMLApproxData(b, ("analysis", "weighted_mean_lon.cml")) self.assertEquals(b.coord("dummy").shape, (1,)) # test collapsing multiple coordinates (and the fact that one of the coordinates isn't the same coordinate instance as on the cube) c = cube.collapsed([lat_coord[:], lon_coord], iris.analysis.MEAN, weights=weights) self.assertCMLApproxData(c, ("analysis", "weighted_mean_latlon.cml")) self.assertEquals(c.coord("dummy").shape, (1,)) # Check new coord bounds - made from points self.assertArrayEqual(c.coord("lat").bounds, [[1, 3]]) # Check new coord bounds - made from bounds cube.coord("lat").bounds = [[0.5, 1.5], [1.5, 2.5], [2.5, 3.5]] c = cube.collapsed(["lat", "lon"], iris.analysis.MEAN, weights=weights) self.assertArrayEqual(c.coord("lat").bounds, [[0.5, 3.5]]) cube.coord("lat").bounds = None # Check there was no residual change self.assertCML(cube, ("analysis", "weighted_mean_source.cml"))
def test_percentile_2d(self): cube = tests.stock.simple_2d() first_quartile = cube.collapsed('foo', iris.analysis.PERCENTILE, percent=25) np.testing.assert_array_almost_equal( first_quartile.data, np.array([0.75, 4.75, 8.75], dtype=np.float32)) self.assertCML(first_quartile, ('analysis', 'first_quartile_foo_2d.cml'), checksum=False) first_quartile = cube.collapsed(('foo', 'bar'), iris.analysis.PERCENTILE, percent=25) np.testing.assert_array_almost_equal( first_quartile.data, np.array([2.75], dtype=np.float32)) self.assertCML(first_quartile, ('analysis', 'first_quartile_foo_bar_2d.cml'), checksum=False)
def test_percentile_3d_notmasked(self): cube = tests.stock.simple_3d() last_quartile = cube.collapsed('wibble', iris.analysis.PERCENTILE, percent=75) np.testing.assert_array_almost_equal(last_quartile.data, np.array([[9., 10., 11., 12.], [13., 14., 15., 16.], [17., 18., 19., 20.]], dtype=np.float32)) self.assertCML(last_quartile, ('analysis', 'last_quartile_foo_3d_notmasked.cml'), checksum=False)
def test_weighted_rms(self): cube = tests.stock.simple_2d() # modify cube data so that the results are nice numbers cube.data = np.array( [[4, 7, 10, 8], [21, 30, 12, 24], [14, 16, 20, 8]], dtype=np.float64) weights = np.array([[1, 4, 3, 2], [6, 4.5, 1.5, 3], [2, 1, 1.5, 0.5]], dtype=np.float64) expected_result = np.array([8.0, 24.0, 16.0]) result = cube.collapsed('foo', iris.analysis.RMS, weights=weights) self.assertArrayAlmostEqual(result.data, expected_result) self.assertCML(result, ('analysis', 'rms_weighted_2d.cml'), checksum=False)
def test_proportion_2d(self): cube = tests.stock.simple_2d() gt6 = cube.collapsed('foo', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([0, 0.5, 1], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_foo_2d.cml'), checksum=False) gt6 = cube.collapsed('bar', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([1 / 3, 1 / 3, 2 / 3, 2 / 3], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_bar_2d.cml'), checksum=False) gt6 = cube.collapsed(('foo', 'bar'), iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6.data, np.array([0.5], dtype=np.float32)) self.assertCML(gt6, ('analysis', 'proportion_foo_bar_2d.cml'), checksum=False) # mask the data cube.data = ma.array(cube.data, mask=cube.data % 2) cube.data.mask[1, 2] = True gt6_masked = cube.collapsed('bar', iris.analysis.PROPORTION, function=lambda val: val >= 6) np.testing.assert_array_almost_equal(gt6_masked.data, ma.array([1 / 3, None, 1 / 2, None], mask=[False, True, False, True], dtype=np.float32)) self.assertCML(gt6_masked, ('analysis', 'proportion_foo_2d_masked.cml'), checksum=False)
def test_percentile_3d_notmasked(self): cube = tests.stock.simple_3d() last_quartile = cube.collapsed('wibble', iris.analysis.PERCENTILE, percent=75) np.testing.assert_array_almost_equal( last_quartile.data, np.array([[9., 10., 11., 12.], [13., 14., 15., 16.], [17., 18., 19., 20.]], dtype=np.float32)) self.assertCML(last_quartile, ('analysis', 'last_quartile_foo_3d_notmasked.cml'), checksum=False)
def column_integral(cube, **kwargs): """Calculate the sum of a cube along the z-axis Args: cube (iris.cube.Cube): The 3d cube to be collapsed **kwargs: Additional keywords to pass to :py:class:`iris.analysis.SUM` Returns: iris.cube.Cube: A 2d collapsed cube """ z = cube.coord(axis='z', dim_coords=True) return cube.collapsed(z.name(), iris.analysis.SUM, **kwargs)
def test_weighted_mean_little(self): data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) weights = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]], dtype=np.float32) cube = iris.cube.Cube(data, long_name="test_data", units="1") hcs = iris.coord_systems.GeogCS(6371229) lat_coord = iris.coords.DimCoord(np.array([1, 2, 3], dtype=np.float32), long_name="lat", units="1", coord_system=hcs) lon_coord = iris.coords.DimCoord(np.array([1, 2, 3], dtype=np.float32), long_name="lon", units="1", coord_system=hcs) cube.add_dim_coord(lat_coord, 0) cube.add_dim_coord(lon_coord, 1) cube.add_aux_coord( iris.coords.AuxCoord(np.arange(3, dtype=np.float32), long_name="dummy", units=1), 1) self.assertCML(cube, ('analysis', 'weighted_mean_source.cml')) a = cube.collapsed('lat', iris.analysis.MEAN, weights=weights) # np.ma.average doesn't apply type promotion rules in some versions, # and instead makes the result type float64. To ignore that case we # fix up the dtype here if it is promotable from float32. We still want # to catch cases where there is a loss of precision however. if a.dtype > np.float32: cast_data = a.data.astype(np.float32) a.replace(cast_data, fill_value=a.fill_value) self.assertCMLApproxData(a, ('analysis', 'weighted_mean_lat.cml')) b = cube.collapsed(lon_coord, iris.analysis.MEAN, weights=weights) if b.dtype > np.float32: cast_data = b.data.astype(np.float32) b.replace(cast_data, fill_value=b.fill_value) b.data = np.asarray(b.data) self.assertCMLApproxData(b, ('analysis', 'weighted_mean_lon.cml')) self.assertEqual(b.coord('dummy').shape, (1, )) # test collapsing multiple coordinates (and the fact that one of the coordinates isn't the same coordinate instance as on the cube) c = cube.collapsed([lat_coord[:], lon_coord], iris.analysis.MEAN, weights=weights) if c.dtype > np.float32: cast_data = c.data.astype(np.float32) c.replace(cast_data, fill_value=c.fill_value) self.assertCMLApproxData(c, ('analysis', 'weighted_mean_latlon.cml')) self.assertEqual(c.coord('dummy').shape, (1, )) # Check new coord bounds - made from points self.assertArrayEqual(c.coord('lat').bounds, [[1, 3]]) # Check new coord bounds - made from bounds cube.coord('lat').bounds = [[0.5, 1.5], [1.5, 2.5], [2.5, 3.5]] c = cube.collapsed(['lat', 'lon'], iris.analysis.MEAN, weights=weights) self.assertArrayEqual(c.coord('lat').bounds, [[0.5, 3.5]]) cube.coord('lat').bounds = None # Check there was no residual change self.assertCML(cube, ('analysis', 'weighted_mean_source.cml'))
def test_multi_d(self): cube = iris.tests.stock.realistic_4d() # TODO: Re-instate surface_altitude & hybrid-height once we're # using the post-CF test results. cube.remove_aux_factory(cube.aux_factories[0]) cube.remove_coord('surface_altitude') self.assertCML(cube, ('cube_collapsed', 'original.cml')) # Compare 2-stage collapsing with a single stage collapse # over 2 Coords. self.collapse_test_common(cube, 'grid_latitude', 'grid_longitude', rtol=1e-05) self.collapse_test_common(cube, 'grid_longitude', 'grid_latitude', rtol=1e-05) self.collapse_test_common(cube, 'time', 'grid_latitude', rtol=1e-05) self.collapse_test_common(cube, 'grid_latitude', 'time', rtol=1e-05) self.collapse_test_common(cube, 'time', 'grid_longitude', rtol=1e-05) self.collapse_test_common(cube, 'grid_longitude', 'time', rtol=1e-05) self.collapse_test_common(cube, 'grid_latitude', 'model_level_number', rtol=5e-04) self.collapse_test_common(cube, 'model_level_number', 'grid_latitude', rtol=5e-04) self.collapse_test_common(cube, 'grid_longitude', 'model_level_number', rtol=5e-04) self.collapse_test_common(cube, 'model_level_number', 'grid_longitude', rtol=5e-04) self.collapse_test_common(cube, 'time', 'model_level_number', rtol=5e-04) self.collapse_test_common(cube, 'model_level_number', 'time', rtol=5e-04) self.collapse_test_common(cube, 'model_level_number', 'time', rtol=5e-04) self.collapse_test_common(cube, 'time', 'model_level_number', rtol=5e-04) # Collapse 3 things at once. triple_collapse = cube.collapsed(['model_level_number', 'time', 'grid_longitude'], iris.analysis.MEAN) self.assertCMLApproxData(triple_collapse, ('cube_collapsed', ('triple_collapse_ml_pt_' 'lon.cml')), rtol=5e-04) triple_collapse = cube.collapsed(['grid_latitude', 'model_level_number', 'time'], iris.analysis.MEAN) self.assertCMLApproxData(triple_collapse, ('cube_collapsed', ('triple_collapse_lat_ml' '_pt.cml')), rtol=0.05) # KNOWN PROBLEM: the previous 'rtol' is very large. # Numpy 1.10 and 1.11 give significantly different results here. # This may relate to known problems with summing over large arrays, # which were largely fixed in numpy 1.9 but still occur in some cases, # as-of numpy 1.11. # Ensure no side effects self.assertCML(cube, ('cube_collapsed', 'original.cml'))
def test_count(self): cube = tests.stock.simple_1d() gt5 = cube.collapsed('foo', iris.analysis.COUNT, function=lambda val: val >= 5) np.testing.assert_array_almost_equal(gt5.data, np.array([6])) gt5.data = gt5.data.astype('i8') self.assertCML(gt5, ('analysis', 'count_foo_1d.cml'), checksum=False)
def test_proportion(self): cube = tests.stock.simple_1d() r = cube.data >= 5 gt5 = cube.collapsed('foo', iris.analysis.PROPORTION, function=lambda val: val >= 5) np.testing.assert_array_almost_equal(gt5.data, np.array([6 / 11.])) self.assertCML(gt5, ('analysis', 'proportion_foo_1d.cml'), checksum=False)