|
| 1 | +"""Edge cases of the unxts.interop.xarray accessors and conversion helpers.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import unxts.interop.xarray as ux |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | +import unxt as u |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + ("fn", "match"), |
| 12 | + [ |
| 13 | + (ux.extract_unit_attributes, "Cannot extract unit attributes from type"), |
| 14 | + (ux.extract_units, "Cannot extract units from type"), |
| 15 | + ], |
| 16 | +) |
| 17 | +def test_extract_from_an_unsupported_type(fn, match): |
| 18 | + with pytest.raises(TypeError, match=match): |
| 19 | + fn(object()) |
| 20 | + |
| 21 | + |
| 22 | +def test_attach_units_to_an_unsupported_type(): |
| 23 | + with pytest.raises(TypeError, match="Cannot attach units to type"): |
| 24 | + ux.attach_units(object(), {None: "m"}) |
| 25 | + |
| 26 | + |
| 27 | +def test_strip_units_from_an_unsupported_type(): |
| 28 | + with pytest.raises(TypeError, match="Cannot strip units from type"): |
| 29 | + ux.strip_units(object()) |
| 30 | + |
| 31 | + |
| 32 | +def test_quantify_rejects_a_bad_units_argument(): |
| 33 | + da = xr.DataArray([1.0, 2.0], dims=["x"]) |
| 34 | + with pytest.raises(TypeError, match="units must be a string"): |
| 35 | + da.unxt.quantify(1.0) |
| 36 | + |
| 37 | + |
| 38 | +def test_extract_units_of_an_unquantified_dataarray(): |
| 39 | + """Nothing carries a unit, so nothing is extracted.""" |
| 40 | + da = xr.DataArray([1.0, 2.0], dims=["i"], coords={"i": [0, 1]}) |
| 41 | + assert ux.extract_units(da) == {} |
| 42 | + assert ux.extract_units(xr.Dataset({"a": ("x", [1.0, 2.0])})) == {} |
| 43 | + |
| 44 | + |
| 45 | +def _dataarray_with_coord_units() -> xr.DataArray: |
| 46 | + return xr.DataArray( |
| 47 | + [1.0, 2.0], |
| 48 | + dims=["i"], |
| 49 | + coords={"i": [0, 1], "x": ("i", [0.0, 1.0], {"units": "s"})}, |
| 50 | + attrs={"units": "m"}, |
| 51 | + ).unxt.quantify() |
| 52 | + |
| 53 | + |
| 54 | +def test_dataarray_round_trip_with_coordinate_units(): |
| 55 | + """Coordinate units survive quantify -> dequantify on a DataArray.""" |
| 56 | + q = _dataarray_with_coord_units() |
| 57 | + assert ux.extract_units(q)["x"] == u.unit("s") |
| 58 | + |
| 59 | + back = q.unxt.dequantify() |
| 60 | + assert back.attrs["units"] == "m" |
| 61 | + assert back.coords["x"].attrs["units"] == "s" |
| 62 | + |
| 63 | + |
| 64 | +def test_dequantify_honours_the_format_spec(): |
| 65 | + """``format`` is forwarded to ``builtins.format`` for the unit string.""" |
| 66 | + q = _dataarray_with_coord_units() |
| 67 | + back = q.unxt.dequantify(format="latex") |
| 68 | + assert back.attrs["units"] == format(u.unit("m"), "latex") |
| 69 | + |
| 70 | + |
| 71 | +def test_dataset_quantify_with_explicit_units_mapping(): |
| 72 | + """An explicit mapping overrides (and supplements) the ``units`` attributes.""" |
| 73 | + ds = xr.Dataset({"a": ("x", [1.0, 2.0], {"units": "m"}), "b": ("x", [3.0, 4.0])}) |
| 74 | + q = ds.unxt.quantify({"b": "s"}) |
| 75 | + assert u.unit_of(q["a"].data) == u.unit("m") |
| 76 | + assert u.unit_of(q["b"].data) == u.unit("s") |
| 77 | + |
| 78 | + |
| 79 | +def test_dataset_round_trip_with_coordinate_units(): |
| 80 | + """Coordinate units survive quantify -> dequantify on a Dataset.""" |
| 81 | + ds = xr.Dataset( |
| 82 | + {"a": ("i", [1.0, 2.0], {"units": "m"})}, |
| 83 | + coords={"i": [0, 1], "x": ("i", [0.0, 1.0], {"units": "s"})}, |
| 84 | + ) |
| 85 | + q = ds.unxt.quantify() |
| 86 | + back = q.unxt.dequantify(format="latex") |
| 87 | + assert back["a"].attrs["units"] == format(u.unit("m"), "latex") |
| 88 | + assert back.coords["x"].attrs["units"] == format(u.unit("s"), "latex") |
| 89 | + |
| 90 | + |
| 91 | +def test_dequantify_a_dataarray_whose_data_has_no_unit(): |
| 92 | + """Only the coordinate carries a unit, so no data-level ``units`` attribute.""" |
| 93 | + da = xr.DataArray( |
| 94 | + [1.0, 2.0], |
| 95 | + dims=["i"], |
| 96 | + coords={"i": [0, 1], "x": ("i", [0.0, 1.0], {"units": "s"})}, |
| 97 | + ).unxt.quantify() |
| 98 | + back = da.unxt.dequantify() |
| 99 | + assert "units" not in back.attrs |
| 100 | + assert back.coords["x"].attrs["units"] == "s" |
0 commit comments