def test_bicubic(): grid = pyinterp.backends.xarray.Grid2D(xr.load_dataset(GRID).mss) lon = np.arange(-180, 180, 1) + 1 / 3.0 lat = np.arange(-90, 90, 1) + 1 / 3.0 x, y = np.meshgrid(lon, lat, indexing="ij") z = grid.bicubic(collections.OrderedDict(lon=x.flatten(), lat=y.flatten())) assert isinstance(z, np.ndarray) for fitting_model in [ 'linear', 'bicubic', 'polynomial', 'c_spline', 'c_spline_periodic', 'akima', 'akima_periodic', 'steffen' ]: other = grid.bicubic(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), fitting_model=fitting_model) assert (z - other).mean() != 0 with pytest.raises(ValueError): grid.bicubic(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), bounds_error=True) with pytest.raises(ValueError): grid.bicubic(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), bounds_error=True, boundary="sym") x_axis = pyinterp.Axis(np.linspace(-180, 179, 360), is_circle=True) y_axis = pyinterp.Axis(np.linspace(-90, 90, 181), is_circle=False) z_axis = pyinterp.Axis(np.linspace(0, 10, 10), is_circle=False) matrix, _ = np.meshgrid(x_axis[:], y_axis[:]) grid = pyinterp.Grid2D(x_axis, y_axis, matrix.T) assert isinstance(grid, pyinterp.Grid2D) with pytest.raises(ValueError): pyinterp.bicubic(grid, x.flatten(), y.flatten(), fitting_model='_') with pytest.raises(ValueError): pyinterp.bicubic(grid, x.flatten(), y.flatten(), boundary='_') grid = pyinterp.Grid2D(x_axis.flip(inplace=False), y_axis, matrix.T) with pytest.raises(ValueError): pyinterp.bicubic(grid, x.flatten(), y.flatten()) grid = pyinterp.Grid2D(x_axis, y_axis.flip(), matrix.T) with pytest.raises(ValueError): pyinterp.bicubic(grid, x.flatten(), y.flatten()) matrix, _, _ = np.meshgrid(x_axis[:], y_axis[:], z_axis[:]) grid = pyinterp.Grid3D(x_axis, y_axis, z_axis, matrix.transpose(1, 0, 2)) with pytest.raises(ValueError): pyinterp.bicubic(grid, x.flatten(), y.flatten()) grid = pyinterp.backends.xarray.RegularGridInterpolator( xr.load_dataset(GRID).mss) assert grid.ndim == 2 assert isinstance(grid.grid, pyinterp.backends.xarray.Grid2D) z = grid(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), method="bicubic", bicubic_kwargs=dict(nx=3, ny=3)) assert isinstance(z, np.ndarray)
def test_core_class_suffix(self): lon = pyinterp.Axis(np.arange(0, 360, 1), is_circle=True) lat = pyinterp.Axis(np.arange(-80, 80, 1), is_circle=False) for dtype in [ "float64", "float32", "int64", "uint64", "int32", "uint32", "int16", "uint16", "int8", "uint8" ]: matrix, _ = np.meshgrid(lon[:], lat[:]) self.assertIsInstance( pyinterp.Grid2D(lon, lat, matrix.T.astype(dtype=getattr(np, dtype))), pyinterp.Grid2D) with self.assertRaises(ValueError): pyinterp.Grid2D(lon, lat, matrix.astype(np.complex))
def test_gauss_seidel(self): grid = self._load() _, filled0 = pyinterp.fill.gauss_seidel(grid, num_threads=0) _, filled1 = pyinterp.fill.gauss_seidel(grid, num_threads=1) _, filled2 = pyinterp.fill.gauss_seidel(grid, first_guess='zero', num_threads=0) data = np.copy(grid.array) data[np.isnan(data)] = 0 filled0[np.isnan(filled0)] = 0 filled1[np.isnan(filled1)] = 0 filled2[np.isnan(filled2)] = 0 self.assertEqual((filled0 - filled1).mean(), 0) self.assertEqual(np.ma.fix_invalid(grid.array - filled1).mean(), 0) self.assertNotEqual((data - filled1).mean(), 0) self.assertNotEqual((filled2 - filled1).mean(), 0) with self.assertRaises(ValueError): pyinterp.fill.gauss_seidel(grid, '_') x_axis = pyinterp.Axis(np.linspace(-180, 180, 10), is_circle=True) y_axis = pyinterp.Axis(np.linspace(-90, 90, 10), is_circle=False) data = np.random.rand(len(x_axis), len(y_axis)) grid = pyinterp.Grid2D(x_axis, y_axis, data) _, filled0 = pyinterp.fill.gauss_seidel(grid, num_threads=0) self.assertIsInstance(filled0, np.ndarray)
def test_gauss_seidel(): grid = load_data() _, filled0 = pyinterp.fill.gauss_seidel(grid, num_threads=0) _, filled1 = pyinterp.fill.gauss_seidel(grid, num_threads=1) _, filled2 = pyinterp.fill.gauss_seidel(grid, first_guess='zero', num_threads=0) data = np.copy(grid.array) data[np.isnan(data)] = 0 filled0[np.isnan(filled0)] = 0 filled1[np.isnan(filled1)] = 0 filled2[np.isnan(filled2)] = 0 assert (filled0 - filled1).mean() == 0 assert np.ma.fix_invalid(grid.array - filled1).mean() == 0 assert (data - filled1).mean() != 0 assert (filled2 - filled1).mean() != 0 with pytest.raises(ValueError): pyinterp.fill.gauss_seidel(grid, '_') x_axis = pyinterp.Axis(np.linspace(-180, 180, 10), is_circle=True) y_axis = pyinterp.Axis(np.linspace(-90, 90, 10), is_circle=False) data = np.random.rand(len(x_axis), len(y_axis)) grid = pyinterp.Grid2D(x_axis, y_axis, data) _, filled0 = pyinterp.fill.gauss_seidel(grid, num_threads=0) assert isinstance(filled0, np.ndarray)
def _build_interpolator(self): """ build interpolation function """ x_axis = pyinterp.Axis(self._x, is_circle=True) y_axis = pyinterp.Axis(self._y) self._z[self._z.mask] = float("nan") self._grid = pyinterp.Grid2D(x_axis, y_axis, self._z.data)
def test_core_variate_interpolator(self): lon = pyinterp.Axis(np.arange(0, 360, 1), is_circle=True) lat = pyinterp.Axis(np.arange(-80, 80, 1), is_circle=False) matrix, _ = np.meshgrid(lon[:], lat[:]) grid = pyinterp.Grid2D(lon, lat, matrix.T) with self.assertRaises(TypeError): pyinterp.grid._core_variate_interpolator(None, "_") with self.assertRaises(ValueError): pyinterp.grid._core_variate_interpolator(grid, '_')
def reproj_with_manual_grid(da, x_coords, y_coords, new_grid): x_axis = pyinterp.Axis(da.x.values) y_axis = pyinterp.Axis(da.y.values) grid = pyinterp.Grid2D(x_axis, y_axis, da.data.T) reproj_data = (pyinterp .bivariate(grid, x_coords, y_coords) .reshape((len(new_grid['x_coords']), len(new_grid['y_coords']))) ) return reproj_data
def test_biavariate(): grid = pyinterp.backends.xarray.Grid2D(xr.load_dataset(GRID).mss) assert isinstance(grid, pyinterp.backends.xarray.Grid2D) assert isinstance(grid, pyinterp.Grid2D) other = pickle.loads(pickle.dumps(grid)) assert isinstance(other, pyinterp.backends.xarray.Grid2D) assert isinstance(grid, pyinterp.Grid2D) assert isinstance(grid.x, pyinterp.Axis) assert isinstance(grid.y, pyinterp.Axis) assert isinstance(grid.array, np.ndarray) lon = np.arange(-180, 180, 1) + 1 / 3.0 lat = np.arange(-90, 90, 1) + 1 / 3.0 x, y = np.meshgrid(lon, lat, indexing="ij") z = grid.bivariate( collections.OrderedDict(lon=x.flatten(), lat=y.flatten())) assert isinstance(z, np.ndarray) z = grid.bivariate(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), interpolator="nearest") assert isinstance(z, np.ndarray) z = grid.bivariate(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), interpolator="inverse_distance_weighting") assert isinstance(z, np.ndarray) grid = pyinterp.backends.xarray.Grid2D(xr.load_dataset(GRID).mss, geodetic=False) assert isinstance(grid, pyinterp.backends.xarray.Grid2D) w = grid.bivariate(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), interpolator="inverse_distance_weighting") assert np.ma.fix_invalid(z).mean() != np.ma.fix_invalid(w).mean() with pytest.raises(TypeError): grid.bivariate((x.flatten(), y.flatten())) with pytest.raises(IndexError): grid.bivariate( collections.OrderedDict(lon=x.flatten(), lat=y.flatten(), time=np.arange(3))) with pytest.raises(IndexError): grid.bivariate( collections.OrderedDict(longitude=x.flatten(), lat=y.flatten())) with pytest.raises(ValueError): grid.bivariate(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), bounds_error=True) lon = pyinterp.Axis(np.linspace(0, 360, 100), is_circle=True) lat = pyinterp.Axis(np.linspace(-80, 80, 50), is_circle=False) array, _ = np.meshgrid(lon[:], lat[:]) with pytest.raises(ValueError): pyinterp.Grid2D(lon, lat, array) grid = pyinterp.Grid2D(lon, lat, array.T) assert isinstance(grid, pyinterp.Grid2D) assert isinstance(str(grid), str) with pytest.raises(ValueError): pyinterp.Grid2D(lon, lat, array, increasing_axes='_') grid = pyinterp.backends.xarray.RegularGridInterpolator( xr.load_dataset(GRID).mss) z = grid(collections.OrderedDict(lon=x.flatten(), lat=y.flatten()), method="bilinear") assert isinstance(z, np.ndarray)