def test_sel_date_slice_or_list(da, index, date_type): expected = xr.DataArray([1, 2], coords=[index[:2]], dims=['time']) result = da.sel(time=slice(date_type(1, 1, 1), date_type(1, 12, 30))) assert_identical(result, expected) result = da.sel(time=[date_type(1, 1, 1), date_type(1, 2, 1)]) assert_identical(result, expected)
def test_sel_date_scalar_backfill(da, date_type, index, sel_kwargs): expected = xr.DataArray(3).assign_coords(time=index[2]) result = da.sel(time=date_type(1, 4, 1), **sel_kwargs) assert_identical(result, expected) expected = xr.DataArray(3).assign_coords(time=index[2]) result = da.sel(time=date_type(1, 11, 1), **sel_kwargs) assert_identical(result, expected)
def test_isel(da, index): expected = xr.DataArray(1).assign_coords(time=index[0]) result = da.isel(time=0) assert_identical(result, expected) expected = xr.DataArray([1, 2], coords=[index[:2]], dims=['time']) result = da.isel(time=[0, 1]) assert_identical(result, expected)
def test_sel_date_list_pad(da, date_type, index, sel_kwargs): expected = xr.DataArray([2, 2], coords=[[index[1], index[1]]], dims=['time']) result = da.sel(time=[date_type(1, 3, 1), date_type(1, 4, 1)], **sel_kwargs) assert_identical(result, expected)
def test_sel_date_scalar_backfill(da, date_type, index, sel_kwargs): expected = xr.DataArray(3).assign_coords(time=index[2]) result = da.sel(time=date_type(1, 4, 1), **sel_kwargs) assert_identical(result, expected) expected = xr.DataArray(3).assign_coords(time=index[2]) result = da.sel(time=date_type(1, 11, 1), **sel_kwargs) assert_identical(result, expected)
def test_isel(da, index): expected = xr.DataArray(1).assign_coords(time=index[0]) result = da.isel(time=0) assert_identical(result, expected) expected = xr.DataArray([1, 2], coords=[index[:2]], dims=["time"]) result = da.isel(time=[0, 1]) assert_identical(result, expected)
def test_sel_date_list_backfill(da, date_type, index, sel_kwargs): expected = xr.DataArray([3, 3], coords=[[index[2], index[2]]], dims=["time"]) result = da.sel(time=[date_type(1, 3, 1), date_type(1, 4, 1)], **sel_kwargs) assert_identical(result, expected)
def test_interpolate_chunk_1d(method, data_ndim, interp_ndim, nscalar, chunked): """Interpolate nd array with multiple independent indexers It should do a series of 1d interpolation """ # 3d non chunked data x = np.linspace(0, 1, 5) y = np.linspace(2, 4, 7) z = np.linspace(-0.5, 0.5, 11) da = xr.DataArray( data=np.sin(x[:, np.newaxis, np.newaxis]) * np.cos(y[:, np.newaxis]) * np.exp(z), coords=[("x", x), ("y", y), ("z", z)], ) kwargs = {"fill_value": "extrapolate"} # choose the data dimensions for data_dims in permutations(da.dims, data_ndim): # select only data_ndim dim da = da.isel( # take the middle line {dim: len(da.coords[dim]) // 2 for dim in da.dims if dim not in data_dims} ) # chunk data da = da.chunk(chunks={dim: i + 1 for i, dim in enumerate(da.dims)}) # choose the interpolation dimensions for interp_dims in permutations(da.dims, interp_ndim): # choose the scalar interpolation dimensions for scalar_dims in combinations(interp_dims, nscalar): dest = {} for dim in interp_dims: if dim in scalar_dims: # take the middle point dest[dim] = 0.5 * (da.coords[dim][0] + da.coords[dim][-1]) else: # pick some points, including outside the domain before = 2 * da.coords[dim][0] - da.coords[dim][1] after = 2 * da.coords[dim][-1] - da.coords[dim][-2] dest[dim] = np.linspace(before, after, len(da.coords[dim]) * 13) if chunked: dest[dim] = xr.DataArray(data=dest[dim], dims=[dim]) dest[dim] = dest[dim].chunk(2) actual = da.interp(method=method, **dest, kwargs=kwargs) expected = da.compute().interp(method=method, **dest, kwargs=kwargs) assert_identical(actual, expected) # all the combinations are usually not necessary break break break
def test_xr_crps_ensemble_dask(a_dask, b_dask): actual = xr_crps_ensemble(a_dask, b_dask) expected = crps_ensemble(a_dask, b_dask) expected = xr.DataArray(expected, coords=a_dask.coords) # test for numerical identity of xr_crps and crps assert_identical(actual, expected) # test that xr_crps_ensemble returns chunks assert actual.chunks is not None # show that crps_ensemble returns no chunks assert expected.chunks is None
def test_interp1d_complex_out_of_bounds() -> None: """Ensure complex nans are used by default""" da = xr.DataArray( np.exp(0.3j * np.arange(4)), [("time", np.arange(4))], ) expected = da.interp(time=3.5, kwargs=dict(fill_value=np.nan + np.nan * 1j)) actual = da.interp(time=3.5) assert_identical(actual, expected)
def test_xr_threshold_brier_score_dask(a_dask, b_dask): threshold = .5 actual = xr_threshold_brier_score(a_dask, b_dask, threshold) expected = threshold_brier_score(a_dask, b_dask, threshold) expected = xr.DataArray(expected, coords=a_dask.coords) # test for numerical identity of xr_threshold and threshold assert_identical(actual, expected) # test that xr_crps_ensemble returns chunks assert actual.chunks is not None # show that crps_ensemble returns no chunks assert expected.chunks is None
def test_xr_crps_gaussian_dask(a_dask, b_dask): mu = b_dask.mean('time') sig = b_dask.std('time') actual = xr_crps_gaussian(a_dask, mu, sig) expected = crps_gaussian(a_dask, mu, sig) expected = xr.DataArray(expected, coords=a_dask.coords) # test for numerical identity of xr_crps and crps assert_identical(actual, expected) # test that xr_crps_ensemble returns chunks assert actual.chunks is not None # show that crps_ensemble returns no chunks assert expected.chunks is None
def test_rolling_iter(self, da: DataArray, center: bool, size: int) -> None: rolling_obj = da.rolling(time=size, center=center) rolling_obj_mean = rolling_obj.mean() assert len(rolling_obj.window_labels) == len(da["time"]) assert_identical(rolling_obj.window_labels, da["time"]) for i, (label, window_da) in enumerate(rolling_obj): assert label == da["time"].isel(time=i) actual = rolling_obj_mean.isel(time=i) expected = window_da.mean("time") np.testing.assert_allclose(actual.values, expected.values)
def test_dtype() -> None: data_vars = dict( a=("time", np.array([1, 1.25, 2])), b=("time", np.array([True, True, False], dtype=bool)), c=("time", np.array(["start", "start", "end"], dtype=str)), ) time = np.array([0, 0.25, 1], dtype=float) expected = xr.Dataset(data_vars, coords=dict(time=time)) actual = xr.Dataset( {k: (dim, arr[[0, -1]]) for k, (dim, arr) in data_vars.items()}, coords=dict(time=time[[0, -1]]), ) actual = actual.interp(time=time, method="linear") assert_identical(expected, actual)
def test_interpolate_chunk_advanced(method): """Interpolate nd array with an nd indexer sharing coordinates.""" # Create original array x = np.linspace(-1, 1, 5) y = np.linspace(-1, 1, 7) z = np.linspace(-1, 1, 11) t = np.linspace(0, 1, 13) q = np.linspace(0, 1, 17) da = xr.DataArray( data=np.sin(x[:, np.newaxis, np.newaxis, np.newaxis, np.newaxis]) * np.cos(y[:, np.newaxis, np.newaxis, np.newaxis]) * np.exp(z[:, np.newaxis, np.newaxis]) * t[:, np.newaxis] + q, dims=("x", "y", "z", "t", "q"), coords={ "x": x, "y": y, "z": z, "t": t, "q": q, "label": "dummy_attr" }, ) # Create indexer into `da` with shared coordinate ("full-twist" Möbius strip) theta = np.linspace(0, 2 * np.pi, 5) w = np.linspace(-0.25, 0.25, 7) r = xr.DataArray( data=1 + w[:, np.newaxis] * np.cos(theta), coords=[("w", w), ("theta", theta)], ) x = r * np.cos(theta) y = r * np.sin(theta) z = xr.DataArray( data=w[:, np.newaxis] * np.sin(theta), coords=[("w", w), ("theta", theta)], ) kwargs = {"fill_value": None} expected = da.interp(t=0.5, x=x, y=y, z=z, kwargs=kwargs, method=method) da = da.chunk(2) x = x.chunk(1) z = z.chunk(3) actual = da.interp(t=0.5, x=x, y=y, z=z, kwargs=kwargs, method=method) assert_identical(actual, expected)
def test_threshold_brier_score_dask(o_dask, f_prob_dask, keep_attrs): threshold = 0.5 actual = threshold_brier_score(o_dask, f_prob_dask, threshold, keep_attrs=keep_attrs) expected = properscoring.threshold_brier_score(o_dask, f_prob_dask, threshold, axis=0) expected = xr.DataArray(expected, coords=o_dask.coords).mean() # test for numerical identity of xskillscore threshold and properscorin threshold assert_identical(actual, expected.assign_attrs(**actual.attrs)) # test that xskillscore crps_ensemble returns chunks assert actual.chunks is not None # show that properscoring crps_ensemble returns no chunks assert expected.chunks is None if keep_attrs: assert actual.attrs == o_dask.attrs else: assert actual.attrs == {}
def test_threshold_brier_score_api_and_inputs( o, f_prob, keep_attrs, input_type, chunk_bool ): """Test that threshold_brier_score keeps attributes, chunking, input types and equals properscoring.threshold_brier_score.""" o, f_prob = modify_inputs(o, f_prob, input_type, chunk_bool) threshold = 0.5 actual = threshold_brier_score(o, f_prob, threshold, keep_attrs=keep_attrs) if input_type == "DataArray": # properscoring allows only DataArrays expected = properscoring.threshold_brier_score(o, f_prob, threshold, axis=0) expected = xr.DataArray(expected, coords=o.coords).mean() expected["threshold"] = threshold # test for numerical identity of xs threshold and properscoring threshold if keep_attrs: expected = expected.assign_attrs(**actual.attrs) assert_identical(actual, expected) # test that returns chunks assert_chunk(actual, chunk_bool) # test that attributes are kept assert_keep_attrs(actual, o, keep_attrs) # test that input types equal output types assign_type_input_output(actual, o)
def test_sel_date_list_nearest(da, date_type, index, sel_kwargs): expected = xr.DataArray( [2, 2], coords=[[index[1], index[1]]], dims=['time']) result = da.sel( time=[date_type(1, 3, 1), date_type(1, 4, 1)], **sel_kwargs) assert_identical(result, expected) expected = xr.DataArray( [2, 3], coords=[[index[1], index[2]]], dims=['time']) result = da.sel( time=[date_type(1, 3, 1), date_type(1, 12, 1)], **sel_kwargs) assert_identical(result, expected) expected = xr.DataArray( [3, 3], coords=[[index[2], index[2]]], dims=['time']) result = da.sel( time=[date_type(1, 11, 1), date_type(1, 12, 1)], **sel_kwargs) assert_identical(result, expected)
def test_roc_bin_edges_symmetric_asc_or_desc(observation_1d_long, forecast_1d_long, symmetric_edges): """Test that roc bin_edges works increasing or decreasing order.""" fpr, tpr, area = roc( observation_1d_long, forecast_1d_long, symmetric_edges, drop_intermediate=False, return_results="all_as_tuple", ) fpr2, tpr2, area2 = roc( observation_1d_long, forecast_1d_long, symmetric_edges[::-1], drop_intermediate=False, return_results="all_as_tuple", ) assert_identical(fpr, fpr2.sortby(fpr.probability_bin)) assert_identical(tpr, tpr2.sortby(tpr.probability_bin)) assert_identical(area, area2)
def test_sel_date_scalar(da, date_type, index): expected = xr.DataArray(1).assign_coords(time=index[0]) result = da.sel(time=date_type(1, 1, 1)) assert_identical(result, expected)
def test_brier_score_forecast_member_dim(o, f_prob): """Check that brier_score allows forecasts with member dim and binary/boolean data.""" f_prob > 0.5 o = o > 0.5 assert_identical(brier_score(o, f_prob), brier_score(o, f_prob.mean("member")))
def test_string_slice_length_one_index(length_one_index): da = xr.DataArray([1], coords=[length_one_index], dims=['time']) result = da.sel(time=slice('0001', '0001')) assert_identical(result, da)
def test_sel_date_scalar(da, date_type, index): expected = xr.DataArray(1).assign_coords(time=index[0]) result = da.sel(time=date_type(1, 1, 1)) assert_identical(result, expected)
def test_sel_string_or_list(da, index, sel_arg): expected = xr.DataArray([1, 2], coords=[index[:2]], dims=['time']) result = da.sel(time=sel_arg) assert_identical(result, expected)
def test_groupby(da): result = da.groupby('time.month').sum('time') expected = xr.DataArray([4, 6], coords=[[1, 2]], dims=['month']) assert_identical(result, expected)
def test_string_slice_length_one_index(length_one_index): da = xr.DataArray([1], coords=[length_one_index], dims=['time']) result = da.sel(time=slice('0001', '0001')) assert_identical(result, da)
def test_sel_date_distant_date(da, date_type, index): expected = xr.DataArray(4).assign_coords(time=index[3]) result = da.sel(time=date_type(2000, 1, 1), method="nearest") assert_identical(result, expected)
def test_groupby(da): result = da.groupby("time.month").sum("time") expected = xr.DataArray([4, 6], coords=[[1, 2]], dims=["month"]) assert_identical(result, expected)
def test_groupby(da): result = da.groupby('time.month').sum('time') expected = xr.DataArray([4, 6], coords=[[1, 2]], dims=['month']) assert_identical(result, expected)
def test_sel_string_or_list(da, index, sel_arg): expected = xr.DataArray([1, 2], coords=[index[:2]], dims=['time']) result = da.sel(time=sel_arg) assert_identical(result, expected)