def test_interpolator_overspecified(self): # Over specification by means of interpolating over two coordinates # mapped to the same dimension. msg = 'Coordinates repeat a data dimension - '\ 'the interpolation would be over-specified' with self.assertRaisesRegexp(ValueError, msg): LinearInterpolator(self.cube, ['wibble', 'height'])
def test_interpolate_non_monotonic(self): self.cube.add_aux_coord( iris.coords.AuxCoord([0, 3, 2], long_name='non-monotonic'), 1) msg = ('Cannot interpolate over the non-monotonic coordinate ' 'non-monotonic.') with self.assertRaisesRegexp(ValueError, msg): LinearInterpolator(self.cube, ['non-monotonic'])
def test_unsorted_datadim_mapping(self): # Currently unsorted data dimension mapping is not supported as the # indexing is not yet clever enough to remap the interpolated # coordinates. self.cube.transpose((0, 2, 1)) interpolator = LinearInterpolator(self.cube, ['latitude']) msg = 'Currently only increasing data_dims is supported.' with self.assertRaisesRegexp(NotImplementedError, msg): interpolator([0])
def setUp(self): ThreeDimCube.setUp(self) self.cube.coord('longitude')._points = np.linspace(0, 360, 4, endpoint=False) self.cube.coord('longitude').circular = True self.cube.coord('longitude').units = 'degrees' self.interpolator = LinearInterpolator(self.cube, ['longitude'], extrapolation_mode='nan') self.cube_reverselons = self.cube[:, :, ::-1] self.interpolator_reverselons = LinearInterpolator( self.cube_reverselons, ['longitude'], extrapolation_mode='nan') self.testpoints_fully_wrapped = ([[180, 270]], [[-180, -90]]) self.testpoints_partially_wrapped = ([[180, 90]], [[-180, 90]]) self.testpoints_fully_wrapped_twice = ([np.linspace(-360, 360, 100)], [ (np.linspace(-360, 360, 100) + 360) % 360 ])
def test_fully_wrapped_not_circular(self): cube = stock.lat_lon_cube() new_long = cube.coord('longitude').copy( cube.coord('longitude').points + 710) cube.remove_coord('longitude') cube.add_dim_coord(new_long, 1) interpolator = LinearInterpolator(cube, ['longitude']) res = interpolator([-10]) self.assertArrayEqual(res.data, cube[:, 1].data)
def test_interpolator_overspecified_scalar(self): # Over specification by means of interpolating over one dimension # coordinate and a scalar coordinate (not mapped to a dimension). self.cube.add_aux_coord(iris.coords.AuxCoord(1, long_name='scalar'), None) msg = 'Coordinates repeat a data dimension - '\ 'the interpolation would be over-specified' with self.assertRaisesRegexp(ValueError, msg): LinearInterpolator(self.cube, ['wibble', 'scalar'])
def test_properties(self): interpolator = LinearInterpolator(self.cube, ['latitude']) # Default extrapolation mode. self.assertEqual(interpolator.extrapolation_mode, 'linear') # Access to cube property of the LinearInterpolator instance. self.assertEqual(interpolator.cube, self.cube) # Access to the resulting coordinate which we are interpolating over. self.assertEqual(interpolator.coords, [self.cube.coord('latitude')])
def test_orthogonal_cube(self): interpolator = LinearInterpolator(self.cube, ['grid_latitude']) result_cube = interpolator([1]) # Explicit mask comparison to ensure mask retention. # Masked value input self.assertTrue(self.cube.data.mask[0, 0, 0, 0]) # Mask retention on output self.assertTrue(result_cube.data.mask[0, 0, 0]) self.assertCML( result_cube, ('experimental', 'analysis', 'interpolate', 'LinearInterpolator', 'orthogonal_cube_with_factory.cml'))
def setUp(self): """ thingness / (1) (wibble: 2; latitude: 1) Dimension coordinates: wibble x - latitude - x Auxiliary coordinates: height x - bar - x foo - x Scalar coordinates: longitude: 0 """ ThreeDimCube.setUp(self) self.cube = self.cube[:, 0:1, 0] self.interpolator = LinearInterpolator(self.cube, ['latitude'])
def test_interpolate_data_default_extrapolation(self): # Sample point outside the coordinate range. interpolator = LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='linear') result = interpolator([[-1]]) self._interpolate_data_linear_extrapolation(result)
def check_expected(): # Check a simple case is equivalent to extracting the first row. self.interpolator = LinearInterpolator(self.cube, ['latitude']) expected = self.data[:, 0:1, :] result = self.interpolator([[0]]) self.assertArrayEqual(result.data, expected)
def test_circular(self): self.cube.coord('longitude').circular = True interpolator = LinearInterpolator(self.cube, ['longitude']) self.assertTrue(interpolator.cube.has_lazy_data())
def _extrapolation_dtype(self, dtype): interpolator = LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='nan') result = interpolator([[-1]]) self.assertTrue(np.all(np.isnan(result.data)))
def setUp(self): ThreeDimCube.setUp(self) coords = ['height', 'longitude'] self.interpolator = LinearInterpolator(self.cube, coords)
def test_interpolate_data_error_on_extrapolation(self): msg = 'One of the requested xi is out of bounds in dimension 0' interpolator = LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='error') with assert_raises_regexp(ValueError, msg): interpolator([[-1]])
def setUp(self): ThreeDimCube.setUp(self) self.interpolator = LinearInterpolator(self.cube, ['latitude', 'longitude'])
def test_multi_dim_coord_interpolation(self): msg = 'Interpolation coords must be 1-d for rectilinear interpolation.' with self.assertRaisesRegexp(ValueError, msg): interpolator = LinearInterpolator(self.cube, ['foo', 'bar']) interpolator([[15], [10]])
def test_interpolate_data_nan_extrapolation_not_needed(self): # No extrapolation for a single length dimension. interpolator = LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='nan') result = interpolator([[0]]) self.assertArrayEqual(result.data, self.cube.data)
def test_interpolate_data_unsupported_extrapolation(self): msg = "Extrapolation mode 'unsupported' not supported" with assert_raises_regexp(ValueError, msg): LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='unsupported')
def test_interpolate_data_nan_extrapolation(self): interpolator = LinearInterpolator(self.cube, ['latitude'], extrapolation_mode='nan') result = interpolator([[1001]]) self.assertTrue(np.all(np.isnan(result.data)))
def test_interpolate_bad_coord_name(self): with self.assertRaises(iris.exceptions.CoordinateNotFoundError): LinearInterpolator(self.cube, ['doesnt exist'])
def test_interpolate_data_dtype_casting(self): data = self.data.astype(int) self.cube.data = data self.interpolator = LinearInterpolator(self.cube, ['latitude']) result = self.interpolator([[0.125]]) self.assertEqual(result.data.dtype, np.float64)
def test_default_collapse_scalar(self): interpolator = LinearInterpolator(self.cube, ['wibble']) result = interpolator([0]) self.assertEqual(result.shape, (3, 4))
def test_no_collapse_scalar(self): interpolator = LinearInterpolator(self.cube, ['wibble']) result = interpolator([0], collapse_scalar=False) self.assertEqual(result.shape, (1, 3, 4))