MCPcopy
hub / github.com/pydata/xarray / test_apply_missing_dims

Function test_apply_missing_dims

xarray/tests/test_computation.py:260–419  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

258
259
260def test_apply_missing_dims() -> None:
261 ## Single arg
262
263 def add_one(a, core_dims, on_missing_core_dim):
264 return apply_ufunc(
265 lambda x: x + 1,
266 a,
267 input_core_dims=core_dims,
268 output_core_dims=core_dims,
269 on_missing_core_dim=on_missing_core_dim,
270 )
271
272 array = np.arange(6).reshape(2, 3)
273 variable = xr.Variable(["x", "y"], array)
274 variable_no_y = xr.Variable(["x", "z"], array)
275
276 ds = xr.Dataset({"x_y": variable, "x_z": variable_no_y})
277
278 # Check the standard stuff works OK
279 assert_identical(
280 add_one(ds[["x_y"]], core_dims=[["y"]], on_missing_core_dim="raise"),
281 ds[["x_y"]] + 1,
282 )
283
284 # `raise` — should raise on a missing dim
285 with pytest.raises(ValueError):
286 add_one(ds, core_dims=[["y"]], on_missing_core_dim="raise")
287
288 # `drop` — should drop the var with the missing dim
289 assert_identical(
290 add_one(ds, core_dims=[["y"]], on_missing_core_dim="drop"),
291 (ds + 1).drop_vars("x_z"),
292 )
293
294 # `copy` — should not add one to the missing with `copy`
295 copy_result = add_one(ds, core_dims=[["y"]], on_missing_core_dim="copy")
296 assert_identical(copy_result["x_y"], (ds + 1)["x_y"])
297 assert_identical(copy_result["x_z"], ds["x_z"])
298
299 ## Multiple args
300
301 def sum_add(a, b, core_dims, on_missing_core_dim):
302 return apply_ufunc(
303 lambda a, b, axis=None: a.sum(axis) + b.sum(axis),
304 a,
305 b,
306 input_core_dims=core_dims,
307 on_missing_core_dim=on_missing_core_dim,
308 )
309
310 # Check the standard stuff works OK
311 assert_identical(
312 sum_add(
313 ds[["x_y"]],
314 ds[["x_y"]],
315 core_dims=[["x", "y"], ["x", "y"]],
316 on_missing_core_dim="raise",
317 ),

Callers

nothing calls this directly

Calls 7

drop_varsMethod · 0.95
assignMethod · 0.95
add_oneFunction · 0.85
sum_addFunction · 0.85
arangeMethod · 0.80
assert_identicalFunction · 0.70
sumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…