def test_grid_transform_noname_targetdata(): """Check handling of a `target_data` input without name""" ( source, grid_kwargs, target, transform_kwargs, _, _, ) = construct_test_source_data(cases["linear_depth_dens"]) axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) source_da = source.data target_data = transform_kwargs.pop("target_data") target_data.name = None # the name of target_data is only used if `target` is provided as numpy array target = target.data # the high level routines should be able to deal with all cases (no error flag exception like in the mid level) with pytest.warns(UserWarning): transformed = grid.transform(source_da, axis, target, target_data=target_data, **transform_kwargs) "TRANSFORMED_DIMENSION" in transformed.dims
def test_grid_transform_multidim_other_dims_error(request, multidim_cases): # broadcast the 1d column agains some other dims and make sure that the 1d results are still valid source, grid_kwargs, target, transform_kwargs, expected, error_flag = multidim_cases na = 3 source = source * xr.DataArray(np.ones([na]), dims=["a"]) # broadcast the target, but in this case # rename one of the dimensions of the target array, which is not along the # axis of transformation (this could be the case if e.g. temperature is on a different # x grid than velocity) target_data = transform_kwargs.pop("target_data", None) if target_data is not None: target_data = target_data * xr.DataArray(np.ones([na]), dims=["a_other"]) # calculate the multidimensional result axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) with pytest.raises(ValueError): _ = grid.transform(source.data, axis, target, target_data=target_data, **transform_kwargs) else: # When target_data is none its taken as a 1D-coordinate, no checking needed pytest.skip()
def test_grid_transform_multidim(request, client, multidim_cases): # broadcast the 1d column agains some other dims and make sure that the 1d results are still valid source, grid_kwargs, target, transform_kwargs, expected, error_flag = multidim_cases na = 8 source = source.expand_dims(a=na).copy(deep=True) # broadcast the target_data manually target_data = transform_kwargs.pop("target_data", None) if target_data is not None: target_data = target_data.expand_dims(a=na).copy(deep=True) if client != "no_client": target_data = target_data.chunk({"a": 1}) if client != "no_client": source = source.chunk({"a": 1}) # calculate the multidimensional result axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) # the high level tests should deal with all error cases client = request.getfixturevalue(client) transformed = grid.transform(source.data, axis, target, target_data=target_data, **transform_kwargs).load() _, expected_broadcasted = xr.broadcast(transformed, expected) xr.testing.assert_allclose(transformed, expected_broadcasted.data)
def test_grid_transform_bypass_checks(bypass_checks): """Check that the bypass checks option still delivers the right results for monotonically increasing data""" ( source, grid_kwargs, target, transform_kwargs, expected, _, ) = construct_test_source_data(cases["linear_depth_dens"]) axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) target_data = transform_kwargs.pop("target_data", None) transformed = grid.transform( source.data, axis, target, target_data=target_data, bypass_checks=bypass_checks, **transform_kwargs ) xr.testing.assert_allclose(transformed, expected.data)
def test_transform_error_periodic(multidim_cases): source, grid_kwargs, target, transform_kwargs, expected, error_flag = multidim_cases axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, **grid_kwargs) with pytest.raises(ValueError): _ = grid.transform(source.data, axis, target, **transform_kwargs)
def test_grid_transform_input_check(): ( source, grid_kwargs, target, transform_kwargs, _, _, ) = construct_test_source_data(cases["linear_depth_dens"]) axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) # construct output name transform_kwargs.setdefault("suffix", "") # Make sure that a sensible error is raised if xr.Dataset is provided # for either one of `source`, `target` or `target_data` input arguments. match_msg = r"needs to be a" with pytest.raises(ValueError, match=r"`da` " + match_msg): grid.transform(source, axis, target, **transform_kwargs) with pytest.raises(ValueError, match=match_msg): grid.transform(source.data, axis, target.to_dataset(name="dummy"), **transform_kwargs) transform_kwargs["target_data"] = transform_kwargs[ "target_data"].to_dataset(name="dummy") with pytest.raises(ValueError, match=match_msg): grid.transform(source.data, axis, target, **transform_kwargs)
def test_grid_transform_noname(multidim_cases): source, grid_kwargs, target, transform_kwargs, expected, error_flag = multidim_cases axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) source_da = source.data source_da.name = None # the high level routines should be able to deal with all cases (no error flag exception like in the mid level) transformed = grid.transform(source_da, axis, target, **transform_kwargs) assert transformed.name is None
def test_grid_transform(all_cases): source, grid_kwargs, target, transform_kwargs, expected, error_flag = all_cases axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) # construct output name transform_kwargs.setdefault("suffix", "") output_name = "data" + transform_kwargs["suffix"] # the high level routines should be able to deal with all cases (no error flag exception like in the mid level) transformed = grid.transform(source.data, axis, target, **transform_kwargs) xr.testing.assert_allclose(transformed, expected[output_name])
def test_conservative_interp_warn(): ( source, grid_kwargs, target, transform_kwargs, _, _, ) = construct_test_source_data(cases["conservative_depth_temp"]) axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) with pytest.warns(UserWarning): _ = grid.transform(source.data, axis, target, **transform_kwargs)
def test_chunking_dim_error(): """Assure that error is raised when we chunk along the 'vertical' dimension""" ( source, grid_kwargs, target, transform_kwargs, _, _, ) = construct_test_source_data(cases["linear_depth_dens"]) source = source.chunk({"depth": 1}) axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) with pytest.raises(ValueError): _ = grid.transform(source.data, axis, target, **transform_kwargs)
def test_grid_transform_auto_naming(multidim_cases): # only test a few cases """Check that the naming for the new dimension is adapted for the output if the target is not passed as xr.Dataarray""" source, grid_kwargs, target, transform_kwargs, expected, error_flag = multidim_cases axis = list(grid_kwargs["coords"].keys())[0] grid = Grid(source, periodic=False, **grid_kwargs) # modify the expected naming and convert target to numpy array target_data = transform_kwargs.setdefault("target_data", None) if transform_kwargs["target_data"] is None: # When no target_data is provided default to axis coordinates depending on method if transform_kwargs["method"] == "linear": expected_data_coord = grid.axes[axis].coords["center"] elif transform_kwargs["method"] == "conservative": expected_data_coord = grid.axes[axis].coords["outer"] else: # When target_data is provided check against the name of the dataarray expected_data_coord = target_data.name target = target.data transformed = grid.transform(source.data, axis, target, **transform_kwargs) assert expected_data_coord in transformed.coords