def test_simple_multiple_coords(self): expected_result = np.array(2.5) r = iintrp.linear(self.simple2d_cube, [ ('dim1', 4), ('dim2', 3.5), ]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False) # Check that it doesn't matter if you do the interpolation in separate steps... r = iintrp.linear(self.simple2d_cube, [('dim2', 3.5)]) r = iintrp.linear(r, [('dim1', 4)]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False) r = iintrp.linear(self.simple2d_cube, [('dim1', 4)]) r = iintrp.linear(r, [('dim2', 3.5)]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False)
def _assert_expected_call(self, sample_points, sample_points_call, cinterp_patch, linear_patch): linear_patch.return_value = self.scheme linear(self.cube, sample_points, self.extrapolation) linear_patch.assert_called_once_with(self.extrapolation) cinterp_patch.assert_called_once_with(sample_points_call, self.scheme)
def test_simple_coord_linear_extrapolation(self): r = iintrp.linear( self.simple2d_cube, [('dim2', 2.5)], extrapolation_mode='linear') self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_coord_linear_extrapolation.cml')) np.testing.assert_array_equal(r.data, np.array([-1., 2., 5., 8.], dtype=self.simple2d_cube.data.dtype)) r = iintrp.linear(self.simple2d_cube, [('dim1', 1)]) np.testing.assert_array_equal(r.data, np.array([-3., -2., -1.], dtype=self.simple2d_cube.data.dtype))
def test_simple_multiple_points_circular(self): r = iintrp.linear(self.simple2d_cube_circular, [('theta', [0., 60., 120., 180.])]) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_points_circular.cml')) # check that the values returned by theta 0 & 360 are the same... r1 = iintrp.linear(self.simple2d_cube_circular, [('theta', 360)]) r2 = iintrp.linear(self.simple2d_cube_circular, [('theta', 0)]) np.testing.assert_array_almost_equal(r1.data, r2.data)
def test_simple_multiple_point(self): r = iintrp.linear(self.simple2d_cube, [('dim1', [4, 5])]) np.testing.assert_array_equal(r.data, np.array([[ 1.5, 2.5, 3.5], [ 3., 4., 5. ]])) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_points.cml')) # Check that numpy arrays specifications work r = iintrp.linear(self.simple2d_cube, [('dim1', np.array([4, 5]))]) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_points.cml'))
def test_single_point_to_scalar(self): # Slice to form (3, 1) shaped cube. cube = self.cube[:, 2:3] r = iintrp.linear(cube, [('longitude', 1.)]) self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_scalar_0')) # Slice to form (1, 4) shaped cube. cube = self.cube[1:2, :] r = iintrp.linear(cube, [('latitude', 1.)]) self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_scalar_1'))
def test_multiple_points(self): # Slice to form (3, 1) shaped cube. cube = self.cube[:, 2:3] r = iintrp.linear(cube, [('longitude', [1., 2., 3., 4.])]) self.assertCMLApproxData( r, ('analysis', 'interpolation', 'linear', 'single_pt_to_many_0')) # Slice to form (1, 4) shaped cube. cube = self.cube[1:2, :] r = iintrp.linear(cube, [('latitude', [1., 2., 3., 4.])]) self.assertCMLApproxData( r, ('analysis', 'interpolation', 'linear', 'single_pt_to_many_1'))
def test_multiple_points(self): # Slice to form (3, 1) shaped cube. cube = self.cube[:, 2:3] r = iintrp.linear(cube, [('longitude', [1., 2., 3., 4.])]) self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_many_0')) # Slice to form (1, 4) shaped cube. cube = self.cube[1:2, :] r = iintrp.linear(cube, [('latitude', [1., 2., 3., 4.])]) self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_many_1'))
def test_multi(self): # Testing interpolation on specified points on cube with # multidimensional coordinates. interp_cube = linear(self.cube, {'latitude': 1.5, 'longitude': 1.5}) self.assertCMLApproxData(interp_cube, ('experimental', 'analysis', 'interpolate', 'linear_nd_2_coords.cml'))
def test_simple_multiple_coords(self): expected_result = np.array(2.5) r = iintrp.linear(self.simple2d_cube, [('dim1', 4), ('dim2', 3.5), ]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False) # Check that it doesn't matter if you do the interpolation in separate steps... r = iintrp.linear(self.simple2d_cube, [('dim2', 3.5)]) r = iintrp.linear(r, [('dim1', 4)]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False) r = iintrp.linear(self.simple2d_cube, [('dim1', 4)]) r = iintrp.linear(r, [('dim2', 3.5)]) np.testing.assert_array_equal(r.data, expected_result) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords.cml'), checksum=False)
def test_extrapolation_mode_same_pt(self): # Slice to form (3, 1) shaped cube. cube = self.cube[:, 2:3] src_points = cube.coord('longitude').points r = iintrp.linear(cube, [('longitude', src_points)], extrapolation_mode='linear') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_same_pt')) r = iintrp.linear(cube, [('longitude', src_points)], extrapolation_mode='nan') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_same_pt')) r = iintrp.linear(cube, [('longitude', src_points)], extrapolation_mode='error') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_same_pt'))
def test_dim_to_aux(self): cube = self.simple2d_cube other = iris.coords.DimCoord([1, 2, 3, 4], long_name='was_dim') cube.add_aux_coord(other, 0) r = iintrp.linear(cube, [('dim1', [7, 3, 5])]) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'dim_to_aux.cml'))
def test_single(self): # Interpolation on the 1d coordinate. interp_cube = linear(self.cube, {'wibble': 20}) self.assertArrayEqual(np.mean(self.cube.data, axis=0), interp_cube.data) self.assertCMLApproxData(interp_cube, ('experimental', 'analysis', 'interpolate', 'linear_nd.cml'))
def linear(cube, sample_points, extrapolation_mode='linear'): msg = (_INTERPOLATE_DEPRECATION_WARNING + '\n' + 'Please replace usage of iris.analysis.interpolate.linear() with ' 'iris.cube.Cube.interpolate(..., scheme=iris.analysis.Linear()).') _warn_deprecated(msg) return oldinterp.linear(cube, sample_points, extrapolation_mode=extrapolation_mode)
def test_circular_vs_non_circular_coord(self): cube = self.simple2d_cube_circular other = iris.coords.AuxCoord([10, 6, 7, 4], long_name='other') cube.add_aux_coord(other, 1) samples = [0, 60, 300] r = iintrp.linear(cube, [('theta', samples)]) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'circular_vs_non_circular.cml'))
def test_shared_axis(self): c = self.simple2d_cube_extended r = iintrp.linear(c, [('dim2', [3.5, 3.25])]) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_shared_axis.cml')) self.assertRaises(ValueError, iintrp.linear, c, [('dim2', [3.5, 3.25]), ('shared_x_coord', [9, 7])])
def test_single(self): # Interpolation on the 1d coordinate. interp_cube = linear(self.cube, {'wibble': 20}) self.assertArrayEqual(np.mean(self.cube.data, axis=0), interp_cube.data) self.assertCMLApproxData( interp_cube, ('experimental', 'analysis', 'interpolate', 'linear_nd.cml'))
def test_mask_retention(self): cube = stock.realistic_4d_w_missing_data() interp_cube = linear(cube, [('pressure', [850, 950])]) self.assertIsInstance(interp_cube.data, np.ma.MaskedArray) # this value is masked in the input self.assertTrue(cube.data.mask[0, 2, 2, 0]) # and is still masked in the output self.assertTrue(interp_cube.data.mask[0, 1, 2, 0])
def test_simple_single_point(self): r = iintrp.linear(self.simple2d_cube, [('dim1', 4)]) self.assertCML( r, ('analysis', 'interpolation', 'linear', 'simple_single_point.cml'), checksum=False) np.testing.assert_array_equal( r.data, np.array([1.5, 2.5, 3.5], dtype=self.simple2d_cube.data.dtype))
def test_positive(self): # Check we can interpolate from a Cube defined over [0, 360). cube = self._create_cube([0, 90, 180, 270]) samples = [('longitude', np.arange(-360, 720, 45))] result = interpolate.linear(cube, samples, extrapolation_mode='nan') normalise_order(result) self.assertCMLApproxData(result, ('analysis', 'interpolation', 'linear', 'circular_wrapping', 'positive'))
def test_circular(self): res = iintrp.linear(self.cube, [('longitude', 359.8)]) normalise_order(res) lon_coord = self.cube.coord('longitude').points expected = self.cube.data[..., 0] + \ ((self.cube.data[..., -1] - self.cube.data[..., 0]) * (((360 - 359.8) - lon_coord[0]) / ((360 - lon_coord[-1]) - lon_coord[0]))) self.assertArrayAllClose(res.data, expected, rtol=1.0e-6) # check that the values returned by lon 0 & 360 are the same... r1 = iintrp.linear(self.cube, [('longitude', 360)]) r2 = iintrp.linear(self.cube, [('longitude', 0)]) np.testing.assert_array_equal(r1.data, r2.data) self.assertCML(res, ('analysis', 'interpolation', 'linear', 'real_circular_2dslice.cml'), checksum=False)
def test_bounded_coordinate(self): # The results should be exactly the same as for the # non-bounded case. cube = self.simple2d_cube cube.coord('dim1').guess_bounds() r = iintrp.linear(cube, [('dim1', [4, 5])]) np.testing.assert_array_equal(r.data, np.array([[ 1.5, 2.5, 3.5], [ 3., 4., 5. ]])) normalise_order(r) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_points.cml'))
def test_extrapolation_mode_different_pts(self): # Slice to form (3, 1) shaped cube. cube = self.cube[:, 2:3] src_points = cube.coord('longitude').points new_points_single = src_points + 0.2 new_points_multiple = [src_points[0], src_points[0] + 0.2, src_points[0] + 0.4] new_points_scalar = src_points[0] + 0.2 # 'nan' mode r = iintrp.linear(cube, [('longitude', new_points_single)], extrapolation_mode='nan') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_single_pt_nan')) r = iintrp.linear(cube, [('longitude', new_points_multiple)], extrapolation_mode='nan') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_many_nan')) r = iintrp.linear(cube, [('longitude', new_points_scalar)], extrapolation_mode='nan') self.assertCMLApproxData(r, ('analysis', 'interpolation', 'linear', 'single_pt_to_scalar_nan')) # 'error' mode with self.assertRaises(ValueError): r = iintrp.linear(cube, [('longitude', new_points_single)], extrapolation_mode='error') with self.assertRaises(ValueError): r = iintrp.linear(cube, [('longitude', new_points_multiple)], extrapolation_mode='error') with self.assertRaises(ValueError): r = iintrp.linear(cube, [('longitude', new_points_scalar)], extrapolation_mode='error')
def test_2slices(self): r = iintrp.linear(self.cube, [('latitude', 0.0), ('longitude', 0.0)]) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'real_2slices.cml'))
def test_slice(self): r = iintrp.linear(self.cube, [('latitude', 0)]) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'real_2dslice.cml'))
def test_points_datatype_casting(self): # this test tries to extract a float from an array of type integer. the result should be of type float. r = iintrp.linear(self.simple2d_cube_extended, [('shared_x_coord', 7.5)]) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_casting_datatype.cml'))
def test_simple_coord_nan_extrapolation(self): r = iintrp.linear( self.simple2d_cube, [('dim2', 2.5)], extrapolation_mode='nan') self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_coord_nan_extrapolation.cml'))
def test_monotonic_decreasing_coord(self): c = self.simple2d_cube[::-1] r = iintrp.linear(c, [('dim1', 4)]) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_single_point.cml'), checksum=False) np.testing.assert_array_equal(r.data, np.array([1.5, 2.5, 3.5], dtype=self.simple2d_cube.data.dtype))
def test_simple_coord_linear_extrapolation_multipoint2(self): r = iintrp.linear( self.simple2d_cube, [('dim1', [1, 10])], extrapolation_mode='linear') self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_coord_linear_extrapolation_multipoint2.cml'))
def test_multiple_coord_linear_extrapolation(self): r = iintrp.linear(self.simple2d_cube, [('dim2', 9), ('dim1', 1.5)]) self.assertCML(r, ('analysis', 'interpolation', 'linear', 'simple_multiple_coords_extrapolation.cml'))
def test_lots_of_points(self): r = iintrp.linear(self.simple2d_cube, [('dim1', np.linspace(3, 9, 20))])
def test_single_extrapolation(self): # Interpolation on the 1d coordinate with extrapolation. interp_cube = linear(self.cube, {'wibble': np.float32(1.5)}) expected = ('experimental', 'analysis', 'interpolate', 'linear_nd_with_extrapolation.cml') self.assertCMLApproxData(interp_cube, expected)