From e9f4c9121170ef496f5f544f6f82105828148d98 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 17 Jun 2020 01:07:42 +0200 Subject: [PATCH 01/31] add a function to attach units to xarray objects --- pintxarray/tests/utils.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/pintxarray/tests/utils.py b/pintxarray/tests/utils.py index b69dd0c7..3501498f 100644 --- a/pintxarray/tests/utils.py +++ b/pintxarray/tests/utils.py @@ -1,10 +1,8 @@ -from contextlib import contextmanager import re +from contextlib import contextmanager import pytest - import xarray as xr - from pint.quantity import Quantity @@ -61,6 +59,38 @@ def extract_units(obj): return units +def attach_units(obj, units): + if isinstance(obj, xr.DataArray): + ds = obj._to_temp_dataset() + new_name = list(ds.data_vars.keys())[0] + units[new_name] = units.get(obj.name) + new_ds = attach_units(ds, units) + new_obj = obj._from_temp_dataset(new_ds) + elif isinstance(obj, xr.Dataset): + data_vars = { + name: attach_units(array.variable, {None: units.get(name)}) + for name, array in obj.data_vars.items() + } + + coords = { + name: attach_units(array.variable, {None: units.get(name)}) + for name, array in obj.coords.items() + } + + new_obj = xr.Dataset(data_vars=data_vars, coords=coords, attrs=obj.attrs) + elif isinstance(obj, xr.Variable): + new_data = attach_units(obj.data, units) + new_obj = obj.copy(data=new_data) + elif isinstance(obj, Quantity): + raise ValueError( + f"cannot attach {units.get(None)} to {obj}: already a quantity" + ) + else: + new_obj = Quantity(obj, units.get(None)) + + return new_obj + + def assert_units_equal(a, b): __tracebackhide__ = True assert extract_units(a) == extract_units(b) From d047f0d428d4d3cd27b92d71b7457b05de6bb6d0 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 17 Jun 2020 01:11:12 +0200 Subject: [PATCH 02/31] remove the old and commented out assert_equal_with_units function --- pintxarray/tests/utils.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/pintxarray/tests/utils.py b/pintxarray/tests/utils.py index 3501498f..f571b2d6 100644 --- a/pintxarray/tests/utils.py +++ b/pintxarray/tests/utils.py @@ -94,37 +94,3 @@ def attach_units(obj, units): def assert_units_equal(a, b): __tracebackhide__ = True assert extract_units(a) == extract_units(b) - - -# def assert_equal_with_units(a, b): -# # works like xr.testing.assert_equal, but also explicitly checks units -# # so, it is more like assert_identical -# __tracebackhide__ = True -# -# if isinstance(a, xr.Dataset) or isinstance(b, xr.Dataset): -# a_units = extract_units(a) -# b_units = extract_units(b) -# -# a_without_units = strip_units(a) -# b_without_units = strip_units(b) -# -# assert a_without_units.equals(b_without_units), formatting.diff_dataset_repr( -# a, b, "equals" -# ) -# assert a_units == b_units -# else: -# a = a if not isinstance(a, (xr.DataArray, xr.Variable)) else a.data -# b = b if not isinstance(b, (xr.DataArray, xr.Variable)) else b.data -# -# assert type(a) == type(b) or ( -# isinstance(a, Quantity) and isinstance(b, Quantity) -# ) -# -# # workaround until pint implements allclose in __array_function__ -# if isinstance(a, Quantity) or isinstance(b, Quantity): -# assert ( -# hasattr(a, "magnitude") and hasattr(b, "magnitude") -# ) and np.allclose(a.magnitude, b.magnitude, equal_nan=True) -# assert (hasattr(a, "units") and hasattr(b, "units")) and a.units == b.units -# else: -# assert np.allclose(a, b, equal_nan=True) From d2c4a04b5741da0353e019852b37a21923d48a8d Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 27 Jun 2020 14:34:06 +0200 Subject: [PATCH 03/31] add utility functions to attach, convert, extract, and strip units --- pintxarray/conversion.py | 82 ++++++++++++++++++ pintxarray/tests/test_conversion.py | 129 ++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 pintxarray/conversion.py create mode 100644 pintxarray/tests/test_conversion.py diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py new file mode 100644 index 00000000..b11aa06a --- /dev/null +++ b/pintxarray/conversion.py @@ -0,0 +1,82 @@ +import pint + + +def array_convert_units(data, unit): + """ convert the units of an array + + This is roughly the same as ``data.to(unit)``. + """ + if unit is None: + return data + + if not isinstance(unit, (str, pint.Unit)): + raise ValueError(f"cannot use {unit!r} as a unit") + elif isinstance(unit, str) and not isinstance(data, pint.Quantity): + raise ValueError(f"cannot convert a non-quantity using {unit!r} as unit") + + registry = data._REGISTRY if isinstance(unit, str) else unit._REGISTRY + + if not isinstance(data, pint.Quantity): + data = registry.Quantity(data, "dimensionless") + + return data.to(unit) + + +def array_attach_units(data, unit, registry=None): + """ attach a unit to the data + + Parameters + ---------- + data : array-like + The data to attach units to. + unit : str or pint.Unit + The desired unit. + registry : pint.UnitRegistry, optional + The registry to use if ``unit`` is a string. Must not be + specified otherwise. + + Returns + ------- + quantity : pint.Quantity + """ + + if unit is None: + return data + + if not isinstance(unit, (pint.Unit, str)): + raise ValueError(f"cannot use {unit!r} as a unit") + + if isinstance(data, pint.Quantity): + raise ValueError( + f"Cannot attach unit {unit!r} to quantity: data " + f"already has units {data.units}" + ) + + if registry is None: + if isinstance(unit, str): + raise ValueError( + "cannot use a string as unit without specifying a registry" + ) + + registry = unit._REGISTRY + + return registry.Quantity(data, unit) + + +def array_extract_units(data): + """ extract the units of an array + + If ``data`` is not a quantity, the units are ``None`` + """ + try: + return data.units + except AttributeError: + return None + + +def array_strip_units(data): + """ strip the units of a quantity """ + try: + return data.magnitude + except AttributeError: + return data diff --git a/pintxarray/tests/test_conversion.py b/pintxarray/tests/test_conversion.py new file mode 100644 index 00000000..c6a7ce60 --- /dev/null +++ b/pintxarray/tests/test_conversion.py @@ -0,0 +1,129 @@ +import numpy as np +import pint +import pytest + +from pintxarray import conversion + +from .utils import assert_array_equal, assert_array_units_equal + +unit_registry = pint.UnitRegistry() + +pytestmark = pytest.mark.filterwarnings("error::pint.UnitStrippedWarning") + + +class TestArrayFunctions: + @pytest.mark.parametrize( + "registry", + ( + pytest.param(None, id="without registry"), + pytest.param(unit_registry, id="with registry"), + ), + ) + @pytest.mark.parametrize( + "unit", + ( + pytest.param(1, id="not a unit"), + pytest.param(None, id="no unit"), + pytest.param("m", id="string"), + pytest.param(unit_registry.m, id="unit object"), + ), + ) + @pytest.mark.parametrize( + "data", + ( + pytest.param(np.array([0, 1]), id="array_like"), + pytest.param(np.array([1, 2]) * unit_registry.m, id="quantity"), + ), + ) + def test_array_attach_units(self, data, unit, registry): + if unit == 1: + match = "cannot use .+ as a unit" + elif isinstance(data, pint.Quantity) and unit is not None: + match = "already has units" + elif isinstance(unit, str) and registry is None: + match = "a string as unit" + else: + match = None + + if match is not None: + with pytest.raises(ValueError, match=match): + conversion.array_attach_units(data, unit, registry=registry) + + return + + expected = unit_registry.Quantity(data, "m") if unit is not None else data + actual = conversion.array_attach_units(data, unit, registry=registry) + + assert_array_units_equal(expected, actual) + assert_array_equal(expected, actual) + + @pytest.mark.parametrize( + "unit", + ( + pytest.param(1, id="not a unit"), + pytest.param(None, id="no unit"), + pytest.param("mm", id="string"), + pytest.param(unit_registry.mm, id="unit object"), + ), + ) + @pytest.mark.parametrize( + "data", + ( + pytest.param(np.array([0, 1]), id="array_like"), + pytest.param(np.array([1, 2]) * unit_registry.m, id="quantity"), + ), + ) + def test_array_convert_units(self, data, unit): + if unit == 1: + error = ValueError + match = "cannot use .+ as a unit" + elif not isinstance(data, pint.Quantity) and isinstance(unit, str): + error = ValueError + match = "cannot convert a non-quantity using .+ as unit" + elif not isinstance(data, pint.Quantity) and unit is not None: + error = pint.DimensionalityError + match = None + else: + error = None + match = None + + if error is not None: + with pytest.raises(error, match=match): + conversion.array_convert_units(data, unit) + + return + + expected = ( + unit_registry.Quantity(np.array([1000, 2000]), "mm") + if unit is not None + else data + ) + actual = conversion.array_convert_units(data, unit) + + assert_array_equal(expected, actual) + + @pytest.mark.parametrize( + "data", + ( + pytest.param(np.array([0, 1]), id="array_like"), + pytest.param(np.array([1, 2]) * unit_registry.m, id="quantity"), + ), + ) + def test_array_extract_units(self, data): + expected = unit_registry.m if isinstance(data, pint.Quantity) else None + actual = conversion.array_extract_units(data) + + assert expected == actual + + @pytest.mark.parametrize( + "data", + ( + pytest.param(np.array([1, 2]), id="array_like"), + pytest.param(np.array([1, 2]) * unit_registry.m, id="quantity"), + ), + ) + def test_array_strip_units(self, data): + expected = np.array([1, 2]) + actual = conversion.array_strip_units(data) + + assert_array_equal(expected, actual) From f8567c28d2c4084a946baba8675054daf5696e57 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 27 Jun 2020 14:42:21 +0200 Subject: [PATCH 04/31] add the missing assert functions --- pintxarray/tests/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pintxarray/tests/utils.py b/pintxarray/tests/utils.py index f571b2d6..99b95e4c 100644 --- a/pintxarray/tests/utils.py +++ b/pintxarray/tests/utils.py @@ -1,6 +1,7 @@ import re from contextlib import contextmanager +import numpy as np import pytest import xarray as xr from pint.quantity import Quantity @@ -91,6 +92,24 @@ def attach_units(obj, units): return new_obj +def assert_array_units_equal(a, b): + __tracebackhide__ = True + + units_a = getattr(a, "units", None) + units_b = getattr(b, "units", None) + + assert units_a == units_b + + +def assert_array_equal(a, b): + __tracebackhide__ = True + + a_ = getattr(a, "magnitude", a) + b_ = getattr(b, "magnitude", b) + + np.testing.assert_array_equal(a_, b_) + + def assert_units_equal(a, b): __tracebackhide__ = True assert extract_units(a) == extract_units(b) From 33ddd399481e9f947286cebb2032e885bd00e6da Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 00:38:40 +0200 Subject: [PATCH 05/31] reorder the array functions --- pintxarray/conversion.py | 55 +++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py index b11aa06a..da30e9c7 100644 --- a/pintxarray/conversion.py +++ b/pintxarray/conversion.py @@ -1,27 +1,6 @@ import pint -def array_convert_units(data, unit): - """ convert the units of an array - - This is roughly the same as ``data.to(unit)``. - """ - if unit is None: - return data - - if not isinstance(unit, (str, pint.Unit)): - raise ValueError(f"cannot use {unit!r} as a unit") - elif isinstance(unit, str) and not isinstance(data, pint.Quantity): - raise ValueError(f"cannot convert a non-quantity using {unit!r} as unit") - - registry = data._REGISTRY if isinstance(unit, str) else unit._REGISTRY - - if not isinstance(data, pint.Quantity): - data = registry.Quantity(data, "dimensionless") - - return data.to(unit) - - def array_attach_units(data, unit, registry=None): """ attach a unit to the data @@ -63,6 +42,40 @@ def array_attach_units(data, unit, registry=None): return registry.Quantity(data, unit) +def array_convert_units(data, unit): + """ convert the units of an array + + This is roughly the same as ``data.to(unit)``. + + Parameters + ---------- + data : quantity or array-like + The data to convert. If it is not a quantity, it is assumed to be + dimensionless. + unit : str or pint.Unit + The unit to convert to. If a string ``data`` has to be a quantity. + + Returns + ------- + result : pint.Quantity + The converted data + """ + if unit is None: + return data + + if not isinstance(unit, (str, pint.Unit)): + raise ValueError(f"cannot use {unit!r} as a unit") + elif isinstance(unit, str) and not isinstance(data, pint.Quantity): + raise ValueError(f"cannot convert a non-quantity using {unit!r} as unit") + + registry = data._REGISTRY if isinstance(unit, str) else unit._REGISTRY + + if not isinstance(data, pint.Quantity): + data = registry.Quantity(data, "dimensionless") + + return data.to(unit) + + def array_extract_units(data): """ extract the units of an array From b1d9268dcac944fd8807a2e96d95d0d42f0d1d39 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 12:42:00 +0200 Subject: [PATCH 06/31] implement a extract_units function --- pintxarray/conversion.py | 29 +++++++++++++++++ pintxarray/tests/test_conversion.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py index da30e9c7..6b52fb96 100644 --- a/pintxarray/conversion.py +++ b/pintxarray/conversion.py @@ -1,4 +1,5 @@ import pint +from xarray import DataArray, Dataset, Variable def array_attach_units(data, unit, registry=None): @@ -93,3 +94,31 @@ def array_strip_units(data): return data.magnitude except AttributeError: return data + + +def extract_units(obj): + if isinstance(obj, Dataset): + vars_units = { + name: array_extract_units(value.data) + for name, value in obj.data_vars.items() + } + coords_units = { + name: array_extract_units(value.data) for name, value in obj.coords.items() + } + + units = {**vars_units, **coords_units} + elif isinstance(obj, DataArray): + vars_units = {obj.name: array_extract_units(obj.data)} + coords_units = { + name: array_extract_units(value.data) for name, value in obj.coords.items() + } + + units = {**vars_units, **coords_units} + elif isinstance(obj, Variable): + vars_units = {None: array_extract_units(obj.data)} + + units = {**vars_units} + else: + raise ValueError(f"unknown type: {type(obj)}") + + return units diff --git a/pintxarray/tests/test_conversion.py b/pintxarray/tests/test_conversion.py index c6a7ce60..10a94bac 100644 --- a/pintxarray/tests/test_conversion.py +++ b/pintxarray/tests/test_conversion.py @@ -1,6 +1,7 @@ import numpy as np import pint import pytest +from xarray import DataArray, Dataset, Variable from pintxarray import conversion @@ -127,3 +128,50 @@ def test_array_strip_units(self, data): actual = conversion.array_strip_units(data) assert_array_equal(expected, actual) + + +class TestXarrayFunctions: + @pytest.mark.parametrize( + "units", + ( + pytest.param({None: None, "u": None}, id="no units"), + pytest.param({None: unit_registry.m, "u": None}, id="data units"), + pytest.param({None: None, "u": unit_registry.s}, id="coord units"), + pytest.param( + {None: unit_registry.m, "u": unit_registry.s}, id="data and coord units" + ), + ), + ) + @pytest.mark.parametrize("typename", ("Variable", "DataArray", "Dataset")) + def test_extract_units(self, typename, units): + if typename == "Variable": + data_units = units.get(None) or 1 + data = np.linspace(0, 1, 2) * data_units + + units = units.copy() + units.pop("u") + + obj = Variable("x", data) + elif typename == "DataArray": + data_units = units.get(None) or 1 + data = np.linspace(0, 1, 2) * data_units + + coord_units = units.get("u") or 1 + coords = {"u": ("x", np.arange(2) * coord_units)} + + obj = DataArray(data, dims="x", coords=coords) + elif typename == "Dataset": + data_units = units.get(None) + data1 = np.linspace(-1, 1, 2) * (data_units or 1) + data2 = np.linspace(0, 1, 2) * (data_units or 1) + + coord_units = units.get("u") or 1 + coords = {"u": ("x", np.arange(2) * coord_units)} + + units = units.copy() + units.pop(None) + units.update({"a": data_units, "b": data_units}) + + obj = Dataset({"a": ("x", data1), "b": ("x", data2)}, coords=coords) + + assert conversion.extract_units(obj) == units From 00ea3fd7d78b768d2c2171cc03f3be55fafba2be Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 12:51:06 +0200 Subject: [PATCH 07/31] implement convert_units --- pintxarray/conversion.py | 37 ++++++++++++++++++++ pintxarray/tests/test_conversion.py | 54 ++++++++++++++++++++++++++++- pintxarray/tests/utils.py | 1 + 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py index 6b52fb96..7c4e5b82 100644 --- a/pintxarray/conversion.py +++ b/pintxarray/conversion.py @@ -96,6 +96,43 @@ def array_strip_units(data): return data +def convert_units(obj, units): + if not isinstance(units, dict): + units = {None: units} + + if isinstance(obj, Variable): + new_data = array_convert_units(obj.data, units.get(None)) + new_obj = obj.copy(data=new_data) + elif isinstance(obj, DataArray): + original_name = obj.name + name = obj.name if obj.name is not None else "" + + units_ = units.copy() + units_[name] = units_[obj.name] + + ds = obj.rename(name).to_dataset() + converted = convert_units(ds, units_) + + new_obj = converted[name].rename(original_name) + elif isinstance(obj, Dataset): + coords = { + name: convert_units(data.variable, units.get(name)) + if name not in obj.dims + else data + for name, data in obj.coords.items() + } + data_vars = { + name: convert_units(data.variable, units.get(name)) + for name, data in obj.items() + } + + new_obj = Dataset(coords=coords, data_vars=data_vars, attrs=obj.attrs) + else: + raise ValueError("cannot convert non-xarray objects") + + return new_obj + + def extract_units(obj): if isinstance(obj, Dataset): vars_units = { diff --git a/pintxarray/tests/test_conversion.py b/pintxarray/tests/test_conversion.py index 10a94bac..62a7bb0a 100644 --- a/pintxarray/tests/test_conversion.py +++ b/pintxarray/tests/test_conversion.py @@ -5,7 +5,7 @@ from pintxarray import conversion -from .utils import assert_array_equal, assert_array_units_equal +from .utils import assert_array_equal, assert_array_units_equal, assert_equal unit_registry = pint.UnitRegistry() @@ -131,6 +131,58 @@ def test_array_strip_units(self, data): class TestXarrayFunctions: + @pytest.mark.parametrize( + "coords", + ( + pytest.param({}, id="no coords"), + pytest.param( + {"u": ("x", [10, 3, 4] * unit_registry.m)}, id="non-dimension coord" + ), + pytest.param( + {"x": [0, 1, 2]}, + id="dimension coordinate", + marks=pytest.mark.xfail( + reason="converting indexes not implemented yet" + ), + ), + ), + ) + @pytest.mark.parametrize("typename", ("Variable", "DataArray", "Dataset")) + def test_convert_units(self, typename, coords): + if typename == "Variable": + if coords: + pytest.skip("Variable doesn't store coordinates") + + data = np.linspace(0, 1, 3) * unit_registry.m + obj = Variable(dims="x", data=data) + units = {None: unit_registry.mm} + elif typename == "DataArray": + obj = DataArray( + dims="x", data=np.linspace(0, 1, 3) * unit_registry.Pa, coords=coords + ) + units = {None: unit_registry.hPa} + if "u" in coords: + units["u"] = unit_registry.mm + elif typename == "Dataset": + obj = Dataset( + data_vars={ + "a": ("x", np.linspace(-1, 1, 3) * unit_registry.s), + "b": ("x", np.linspace(1, 2, 3) * unit_registry.kg), + }, + coords=coords, + ) + units = { + "a": unit_registry.ms, + "b": unit_registry.gram, + } + if "u" in coords: + units["u"] = unit_registry.mm + + actual = conversion.convert_units(obj, units) + + assert conversion.extract_units(actual) == units + assert_equal(obj, actual) + @pytest.mark.parametrize( "units", ( diff --git a/pintxarray/tests/utils.py b/pintxarray/tests/utils.py index 99b95e4c..f625d0ce 100644 --- a/pintxarray/tests/utils.py +++ b/pintxarray/tests/utils.py @@ -5,6 +5,7 @@ import pytest import xarray as xr from pint.quantity import Quantity +from xarray.testing import assert_equal # noqa: F401 @contextmanager From 54f385baad3df804821b180ca043c71b43af8d38 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 13:41:05 +0200 Subject: [PATCH 08/31] implement attach_units --- pintxarray/conversion.py | 34 ++++++++++++++++++++++ pintxarray/tests/test_conversion.py | 45 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py index 7c4e5b82..0aa2a86b 100644 --- a/pintxarray/conversion.py +++ b/pintxarray/conversion.py @@ -96,6 +96,40 @@ def array_strip_units(data): return data +def attach_units(obj, units, registry=None): + if isinstance(obj, DataArray): + old_name = obj.name + new_name = old_name if old_name is not None else "" + ds = obj.rename(new_name).to_dataset() + units = units.copy() + units[new_name] = units.get(old_name) + + new_ds = attach_units(ds, units, registry=registry) + new_obj = new_ds.get(new_name).rename(old_name) + elif isinstance(obj, Dataset): + data_vars = { + name: attach_units( + array.variable, {None: units.get(name)}, registry=registry + ) + for name, array in obj.data_vars.items() + } + coords = { + name: attach_units( + array.variable, {None: units.get(name)}, registry=registry + ) + for name, array in obj.coords.items() + } + + new_obj = Dataset(data_vars=data_vars, coords=coords, attrs=obj.attrs) + elif isinstance(obj, Variable): + new_data = array_attach_units(obj.data, units.get(None), registry=registry) + new_obj = obj.copy(data=new_data) + else: + raise ValueError(f"cannot attach units to {obj!r}: unknown type") + + return new_obj + + def convert_units(obj, units): if not isinstance(units, dict): units = {None: units} diff --git a/pintxarray/tests/test_conversion.py b/pintxarray/tests/test_conversion.py index 62a7bb0a..eebc5594 100644 --- a/pintxarray/tests/test_conversion.py +++ b/pintxarray/tests/test_conversion.py @@ -131,6 +131,51 @@ def test_array_strip_units(self, data): class TestXarrayFunctions: + @pytest.mark.parametrize( + "obj", + ( + pytest.param(Variable("x", np.linspace(0, 1, 5)), id="Variable"), + pytest.param( + DataArray( + data=np.linspace(0, 1, 5), + dims="x", + coords={"u": ("x", np.arange(5))}, + ), + id="DataArray", + ), + pytest.param( + Dataset( + { + "a": ("x", np.linspace(-1, 1, 5)), + "b": ("x", np.linspace(0, 1, 5)), + }, + coords={"u": ("x", np.arange(5))}, + ), + id="Dataset", + ), + ), + ) + @pytest.mark.parametrize( + "units", + ( + pytest.param({None: None, "u": None}, id="no units"), + pytest.param({None: unit_registry.m, "u": None}, id="data units"), + pytest.param({None: None, "u": unit_registry.s}, id="coord units"), + ), + ) + def test_attach_units(self, obj, units): + if isinstance(obj, Variable) and "u" in units: + pytest.skip(msg="variables don't have coordinates") + + if isinstance(obj, Dataset): + units = units.copy() + data_units = units.pop(None) + units.update({"a": data_units, "b": data_units}) + + actual = conversion.attach_units(obj, units) + + assert conversion.extract_units(actual) == units + @pytest.mark.parametrize( "coords", ( From 3101db25187c8632fd3b5cba3e44099ef6966d29 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 13:54:08 +0200 Subject: [PATCH 09/31] implement strip_units --- pintxarray/conversion.py | 26 +++++++++++++++++++++ pintxarray/tests/test_conversion.py | 36 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/pintxarray/conversion.py b/pintxarray/conversion.py index 0aa2a86b..b7aedb29 100644 --- a/pintxarray/conversion.py +++ b/pintxarray/conversion.py @@ -193,3 +193,29 @@ def extract_units(obj): raise ValueError(f"unknown type: {type(obj)}") return units + + +def strip_units(obj): + if isinstance(obj, Variable): + data = array_strip_units(obj.data) + new_obj = obj.copy(data=data) + elif isinstance(obj, DataArray): + original_name = obj.name + name = obj.name if obj.name is not None else "" + ds = obj.rename(name).to_dataset() + stripped = strip_units(ds) + + new_obj = stripped[name].rename(original_name) + elif isinstance(obj, Dataset): + data_vars = { + name: strip_units(array.variable) for name, array in obj.data_vars.items() + } + coords = { + name: strip_units(array.variable) for name, array in obj.coords.items() + } + + new_obj = Dataset(data_vars=data_vars, coords=coords, attrs=obj.attrs) + else: + raise ValueError("cannot strip units from {obj!r}: unknown type") + + return new_obj diff --git a/pintxarray/tests/test_conversion.py b/pintxarray/tests/test_conversion.py index eebc5594..e89e648a 100644 --- a/pintxarray/tests/test_conversion.py +++ b/pintxarray/tests/test_conversion.py @@ -272,3 +272,39 @@ def test_extract_units(self, typename, units): obj = Dataset({"a": ("x", data1), "b": ("x", data2)}, coords=coords) assert conversion.extract_units(obj) == units + + @pytest.mark.parametrize( + "obj", + ( + pytest.param(Variable("x", [0, 4, 3] * unit_registry.m), id="Variable"), + pytest.param( + DataArray( + dims="x", + data=[0, 4, 3] * unit_registry.m, + coords={"u": ("x", [2, 3, 4] * unit_registry.s)}, + ), + id="DataArray", + ), + pytest.param( + Dataset( + data_vars={ + "a": ("x", [3, 2, 5] * unit_registry.Pa), + "b": ("x", [0, 2, -1] * unit_registry.kg), + }, + coords={"u": ("x", [2, 3, 4] * unit_registry.s)}, + ), + id="Dataset", + ), + ), + ) + def test_strip_units(self, obj): + if isinstance(obj, Variable): + expected_units = {None: None} + elif isinstance(obj, DataArray): + expected_units = {None: None} + expected_units.update({name: None for name in obj.coords.keys()}) + elif isinstance(obj, Dataset): + expected_units = {name: None for name in obj.variables.keys()} + + actual = conversion.strip_units(obj) + assert conversion.extract_units(actual) == expected_units From b37bdd4b19a59ce4eb920e070dcd9da0f3da5279 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 14:35:57 +0200 Subject: [PATCH 10/31] isort and black --- pintxarray/tests/test_accessors.py | 15 ++++----------- setup.cfg | 11 ++++++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pintxarray/tests/test_accessors.py b/pintxarray/tests/test_accessors.py index 534a0b3b..e8e5b227 100644 --- a/pintxarray/tests/test_accessors.py +++ b/pintxarray/tests/test_accessors.py @@ -1,19 +1,12 @@ +import numpy as np import pytest - import xarray as xr -from xarray.testing import assert_equal - -import numpy as np from numpy.testing import assert_array_equal - from pint import UnitRegistry +from pint.errors import UndefinedUnitError +from xarray.testing import assert_equal -# from pint.unit import Unit -from pint.errors import UndefinedUnitError # , DimensionalityError - -# from pintxarray.accessors import PintDataArrayAccessor, PintDatasetAccessor -from .utils import raises_regex # extract_units - +from .utils import raises_regex # make sure scalars are converted to 0d arrays so quantities can # always be treated like ndarrays diff --git a/setup.cfg b/setup.cfg index 2e2b5767..1388ac34 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,4 +23,13 @@ ignore = # line break before binary operator W503 exclude= - .eggs \ No newline at end of file + .eggs + +[isort] +default_section = THIRDPARTY +known_first_party = pintxarray +multi_line_output = 3 +include_trailing_comma = True +force_grid_wrap = 0 +use_parentheses = True +line_length = 88 \ No newline at end of file From 013b05655cc6b96f0fce8ac1877a73acecd4737f Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 14:36:20 +0200 Subject: [PATCH 11/31] implement DataArray.pint.to --- pintxarray/accessors.py | 59 +++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/pintxarray/accessors.py b/pintxarray/accessors.py index b9f08598..8b724d5e 100644 --- a/pintxarray/accessors.py +++ b/pintxarray/accessors.py @@ -1,19 +1,18 @@ # TODO is it possible to import pint-xarray from within xarray if pint is present? +import numpy as np +import pint +from pint.quantity import Quantity +from pint.unit import Unit from xarray import ( - register_dataarray_accessor, - register_dataset_accessor, DataArray, Dataset, Variable, + register_dataarray_accessor, + register_dataset_accessor, ) from xarray.core.npcompat import IS_NEP18_ACTIVE -import numpy as np - -import pint -from pint.quantity import Quantity -from pint.unit import Unit - +from . import conversion if not hasattr(Quantity, "__array_function__"): raise ImportError( @@ -32,6 +31,28 @@ # TODO type hints +def is_dict_like(obj): + return hasattr(obj, "keys") and hasattr(obj, "__getitem__") + + +def either_dict_or_kwargs(positional, keywords, method_name): + if positional is not None: + if not is_dict_like(positional): + raise ValueError( + f"the first argument to .{method_name} must be a dictionary" + ) + if keywords: + raise ValueError( + "cannot specify both keyword and positional " + f"arguments to .{method_name}" + ) + return positional + else: + # Need an explicit cast to appease mypy due to invariance; see + # https://github.com/python/mypy/issues/6228 + return keywords + + def _array_attach_units(data, unit, convert_from=None): """ Internal utility function for attaching units to a numpy-like array, @@ -233,15 +254,19 @@ def registry(self): def registry(self, _): raise AttributeError("Don't try to change the registry once created") - def to(self, units): - quantity = self.da.data.to(units) - return DataArray( - dim=self.da.dims, - data=quantity, - coords=self.da.coords, - attrs=self.da.attrs, - encoding=self.da.encoding, - ) + def to(self, units=None, *, registry=None, **unit_kwargs): + if isinstance(units, (str, pint.Unit)): + unit_kwargs[self.da.name] = units + units = None + elif not is_dict_like(units): + raise ValueError( + "units must be either a string, a pint.Unit object or a dict-like," + f" but got {units!r}" + ) + + units = either_dict_or_kwargs(units, unit_kwargs, "to") + + return conversion.convert_units(self.da, units) def to_base_units(self): quantity = self.da.data.to_base_units() From 860aad2629e64c65bc9f425b0a896a12b8b566ee Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 14:38:23 +0200 Subject: [PATCH 12/31] implement Dataset.pint.to --- pintxarray/accessors.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pintxarray/accessors.py b/pintxarray/accessors.py index 8b724d5e..f25562cd 100644 --- a/pintxarray/accessors.py +++ b/pintxarray/accessors.py @@ -374,6 +374,11 @@ def dequantify(self): } return Dataset(dequantified_vars, coords=self.ds.coords, attrs=self.ds.attrs) + def to(self, units, **unit_kwargs): + units = either_dict_or_kwargs(units, unit_kwargs, "to") + + return conversion.convert_units(self.ds, units) + def to_base_units(self): base_vars = {name: da.pint.to_base_units() for name, da in self.ds.items()} return Dataset(base_vars, coords=self.ds.coords, attrs=self.ds.attrs) From 0f076a679180818743592a678e3d7523445772e5 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sun, 28 Jun 2020 14:52:57 +0200 Subject: [PATCH 13/31] set units as optional --- pintxarray/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pintxarray/accessors.py b/pintxarray/accessors.py index f25562cd..fd29f7c3 100644 --- a/pintxarray/accessors.py +++ b/pintxarray/accessors.py @@ -374,7 +374,7 @@ def dequantify(self): } return Dataset(dequantified_vars, coords=self.ds.coords, attrs=self.ds.attrs) - def to(self, units, **unit_kwargs): + def to(self, units=None, **unit_kwargs): units = either_dict_or_kwargs(units, unit_kwargs, "to") return conversion.convert_units(self.ds, units) From 5b1d3343670f116c6697a342e059face9a39199c Mon Sep 17 00:00:00 2001 From: Keewis Date: Mon, 29 Jun 2020 17:08:12 +0200 Subject: [PATCH 14/31] remove a leftover comment about mypy --- pintxarray/accessors.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pintxarray/accessors.py b/pintxarray/accessors.py index fd29f7c3..48f5d75d 100644 --- a/pintxarray/accessors.py +++ b/pintxarray/accessors.py @@ -48,8 +48,6 @@ def either_dict_or_kwargs(positional, keywords, method_name): ) return positional else: - # Need an explicit cast to appease mypy due to invariance; see - # https://github.com/python/mypy/issues/6228 return keywords From e3d3e72308eac53c1963c3d594b9b1387398cbd6 Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 2 Jul 2020 01:09:13 +0200 Subject: [PATCH 15/31] update the minimum required pint version --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 1388ac34..56d16ab6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,7 +9,7 @@ url = https://github.com/TomNicholas/pint-xarray [options] packages = find: python_requires = >=3.6 -install_requires = numpy>=1.17.1; xarray>=0.15.1; pint>=0.12 +install_requires = numpy>=1.17.1; xarray>=0.15.1; pint>=0.13 [flake8] ignore = From 5052f765853a66f7de306d7544bce30075ccec92 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 19:00:43 +0200 Subject: [PATCH 16/31] document Dataset.pint.to --- docs/api.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api.rst b/docs/api.rst index 4a904190..4bcb96aa 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -13,6 +13,7 @@ Dataset Dataset.pint.quantify Dataset.pint.dequantify + Dataset.pint.to Dataset.pint.to_base_units Dataset.pint.to_system From 9a17b22a849f7dffb7265bf314a2edc692491b0d Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 19:44:30 +0200 Subject: [PATCH 17/31] add docstrings to the conversion methods --- pint_xarray/accessors.py | 82 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 48f5d75d..7b30e6bd 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -252,7 +252,47 @@ def registry(self): def registry(self, _): raise AttributeError("Don't try to change the registry once created") - def to(self, units=None, *, registry=None, **unit_kwargs): + def to(self, units=None, **unit_kwargs): + """ convert the quantities in a DataArray + + Parameters + ---------- + units : str or pint.Unit or mapping of str to str or pint.Unit + The units to convert to. If a unit name or pint.Unit, + convert the DataArray's data. If a dict-like, it has to + map a variable name to a unit name or pint.Unit object. + **unit_kwargs + The kwargs form of ``units``. Can only be used for + variable names that are strings and valid python identifiers. + + Returns + ------- + object : DataArray + A new object with converted units. + + Examples + -------- + >>> da = xr.DataArray( + >>> data=np.linspace(0, 1, 5) * ureg.m, + >>> coords={"u": ("x", np.arange(5) * ureg.s)}, + >>> dims="x", + >>> name="arr", + >>> ) + >>> ureg = pint.UnitRegistry(force_ndarray_like=True) + + Convert the data + >>> da.pint.to("mm") + >>> da.pint.to(ureg.mm) + + Convert coordinates + >>> da.pint.to({"u": ureg.ms}) + >>> da.pint.to(u="ms") + + Convert both simultaneously + >>> da.pint.to("mm", u="ms") + >>> da.pint.to({"arr": ureg.mm, "u": ureg.ms}) + >>> da.pint.to(arr="mm", u="ms") + """ if isinstance(units, (str, pint.Unit)): unit_kwargs[self.da.name] = units units = None @@ -373,6 +413,46 @@ def dequantify(self): return Dataset(dequantified_vars, coords=self.ds.coords, attrs=self.ds.attrs) def to(self, units=None, **unit_kwargs): + """ convert the quantities in a DataArray + + Parameters + ---------- + units : str or pint.Unit or mapping of str to str or pint.Unit + The units to convert to. If a unit name or pint.Unit, + convert the DataArray's data. If a dict-like, it has to + map a variable name to a unit name or pint.Unit object. + **unit_kwargs + The kwargs form of ``units``. Can only be used for + variable names that are strings and valid python identifiers. + + Returns + ------- + object : DataArray + A new object with converted units. + + Examples + -------- + >>> ds = xr.Dataset( + ... data_vars={ + ... "a": ("x", np.linspace(0, 1, 5) * ureg.m), + ... "b": ("x", np.linspace(-1, 0, 5) * ureg.kg), + ... }, + ... coords={"u": ("x", np.arange(5) * ureg.s)}, + ... ) + >>> ureg = pint.UnitRegistry(force_ndarray_like=True) + + Convert the data + >>> ds.pint.to({"a": "mm", "b": ureg.g}) + >>> ds.pint.to(a=ureg.mm, b="g") + + Convert coordinates + >>> ds.pint.to({"u": ureg.ms}) + >>> ds.pint.to(u="ms") + + Convert both simultaneously + >>> ds.pint.to(a=ureg.mm, b=ureg.g, u="ms") + >>> ds.pint.to({"a": "mm", "b": "g", "u": ureg.ms}) + """ units = either_dict_or_kwargs(units, unit_kwargs, "to") return conversion.convert_units(self.ds, units) From 50a7c8e7c2d723e292adb418655f0d2595b05425 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 19:48:26 +0200 Subject: [PATCH 18/31] don't require the variable names to be str --- pint_xarray/accessors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 7b30e6bd..d8240a18 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -257,7 +257,7 @@ def to(self, units=None, **unit_kwargs): Parameters ---------- - units : str or pint.Unit or mapping of str to str or pint.Unit + units : str or pint.Unit or mapping of hashable to str or pint.Unit The units to convert to. If a unit name or pint.Unit, convert the DataArray's data. If a dict-like, it has to map a variable name to a unit name or pint.Unit object. @@ -417,7 +417,7 @@ def to(self, units=None, **unit_kwargs): Parameters ---------- - units : str or pint.Unit or mapping of str to str or pint.Unit + units : str or pint.Unit or mapping of hashable to str or pint.Unit The units to convert to. If a unit name or pint.Unit, convert the DataArray's data. If a dict-like, it has to map a variable name to a unit name or pint.Unit object. From cc999e41858fc36d0da1481a7d8b5cd1652c84cf Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 19:57:19 +0200 Subject: [PATCH 19/31] don't try to always convert the DataArray's unit --- pint_xarray/conversion.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pint_xarray/conversion.py b/pint_xarray/conversion.py index b7aedb29..58252105 100644 --- a/pint_xarray/conversion.py +++ b/pint_xarray/conversion.py @@ -142,7 +142,8 @@ def convert_units(obj, units): name = obj.name if obj.name is not None else "" units_ = units.copy() - units_[name] = units_[obj.name] + if obj.name in units_: + units_[name] = units_[obj.name] ds = obj.rename(name).to_dataset() converted = convert_units(ds, units_) From cf47518c5376888ee60c02252b065a1df84edc4a Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 20:07:28 +0200 Subject: [PATCH 20/31] add the example output --- pint_xarray/accessors.py | 110 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 7 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index d8240a18..3da1037d 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -273,25 +273,65 @@ def to(self, units=None, **unit_kwargs): Examples -------- >>> da = xr.DataArray( - >>> data=np.linspace(0, 1, 5) * ureg.m, - >>> coords={"u": ("x", np.arange(5) * ureg.s)}, - >>> dims="x", - >>> name="arr", - >>> ) - >>> ureg = pint.UnitRegistry(force_ndarray_like=True) + ... data=np.linspace(0, 1, 5) * ureg.m, + ... coords={"u": ("x", np.arange(5) * ureg.s)}, + ... dims="x", + ... name="arr", + ... ) + >>> da + + + Coordinates: + u (x) int64 + Dimensions without coordinates: x Convert the data >>> da.pint.to("mm") + + + Coordinates: + u (x) int64 + Dimensions without coordinates: x >>> da.pint.to(ureg.mm) + + + Coordinates: + u (x) int64 + Dimensions without coordinates: x Convert coordinates >>> da.pint.to({"u": ureg.ms}) + + + Coordinates: + u (x) float64 >> da.pint.to(u="ms") + + + Coordinates: + u (x) float64 >> da.pint.to("mm", u="ms") + + + Coordinates: + u (x) float64 >> da.pint.to({"arr": ureg.mm, "u": ureg.ms}) + + + Coordinates: + u (x) float64 >> da.pint.to(arr="mm", u="ms") + + + Coordinates: + u (x) float64 >> ureg = pint.UnitRegistry(force_ndarray_like=True) + >>> ds + + Dimensions: (x: 5) + Coordinates: + u (x) int64 + Dimensions without coordinates: x + Data variables: + a (x) float64 + b (x) float64 Convert the data >>> ds.pint.to({"a": "mm", "b": ureg.g}) + + Dimensions: (x: 5) + Coordinates: + u (x) int64 + Dimensions without coordinates: x + Data variables: + a (x) float64 >> ds.pint.to(a=ureg.mm, b="g") + + Dimensions: (x: 5) + Coordinates: + u (x) int64 + Dimensions without coordinates: x + Data variables: + a (x) float64 >> ds.pint.to({"u": ureg.ms}) + + Dimensions: (x: 5) + Coordinates: + u (x) float64 + b (x) float64 >>> ds.pint.to(u="ms") + + Dimensions: (x: 5) + Coordinates: + u (x) float64 + b (x) float64 Convert both simultaneously >>> ds.pint.to(a=ureg.mm, b=ureg.g, u="ms") + + Dimensions: (x: 5) + Coordinates: + u (x) float64 >> ds.pint.to({"a": "mm", "b": "g", "u": ureg.ms}) + + Dimensions: (x: 5) + Coordinates: + u (x) float64 Date: Wed, 8 Jul 2020 20:20:50 +0200 Subject: [PATCH 21/31] make sure conversion of only the coords works --- pint_xarray/tests/test_conversion.py | 36 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/pint_xarray/tests/test_conversion.py b/pint_xarray/tests/test_conversion.py index 6205a44d..261ed9be 100644 --- a/pint_xarray/tests/test_conversion.py +++ b/pint_xarray/tests/test_conversion.py @@ -176,6 +176,7 @@ def test_attach_units(self, obj, units): assert conversion.extract_units(actual) == units + @pytest.mark.parametrize("variant", ("data", "coords", "both")) @pytest.mark.parametrize( "coords", ( @@ -193,21 +194,32 @@ def test_attach_units(self, obj, units): ), ) @pytest.mark.parametrize("typename", ("Variable", "DataArray", "Dataset")) - def test_convert_units(self, typename, coords): + def test_convert_units(self, typename, coords, variant): + if variant in ("coords", "both") and not coords: + pytest.skip("no coords set") + if variant == "data" and coords: + pytest.skip("no need to test conversion of data") + if typename == "Variable": - if coords: + if coords or variant != "data": pytest.skip("Variable doesn't store coordinates") data = np.linspace(0, 1, 3) * unit_registry.m obj = Variable(dims="x", data=data) units = {None: unit_registry.mm} + expected_units = units elif typename == "DataArray": obj = DataArray( dims="x", data=np.linspace(0, 1, 3) * unit_registry.Pa, coords=coords ) - units = {None: unit_registry.hPa} - if "u" in coords: - units["u"] = unit_registry.mm + + variants = { + "data": {None: unit_registry.hPa}, + "coords": {"u": unit_registry.mm}, + "both": {None: unit_registry.hPa, "u": unit_registry.mm}, + } + units = variants.get(variant) + expected_units = {**conversion.extract_units(obj), **units} elif typename == "Dataset": obj = Dataset( data_vars={ @@ -216,16 +228,22 @@ def test_convert_units(self, typename, coords): }, coords=coords, ) - units = { + + data_units = { "a": unit_registry.ms, "b": unit_registry.gram, } - if "u" in coords: - units["u"] = unit_registry.mm + variants = { + "data": data_units, + "coords": {"u": unit_registry.mm}, + "both": dict(u=unit_registry.mm, **data_units), + } + units = variants.get(variant) + expected_units = {**conversion.extract_units(obj), **units} actual = conversion.convert_units(obj, units) - assert conversion.extract_units(actual) == units + assert conversion.extract_units(actual) == expected_units assert_equal(obj, actual) @pytest.mark.parametrize( From e9705153babe28d57574d6e990fc2f0f10266726 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 20:21:28 +0200 Subject: [PATCH 22/31] don't raise if the default value for units is passed --- pint_xarray/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 3da1037d..bf992e93 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -336,7 +336,7 @@ def to(self, units=None, **unit_kwargs): if isinstance(units, (str, pint.Unit)): unit_kwargs[self.da.name] = units units = None - elif not is_dict_like(units): + elif units is not None and not is_dict_like(units): raise ValueError( "units must be either a string, a pint.Unit object or a dict-like," f" but got {units!r}" From 30b75bc98e3639c8d1fbb1b9233a530753e1ef0f Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 20:25:54 +0200 Subject: [PATCH 23/31] undo the test changes --- pint_xarray/tests/test_conversion.py | 34 ++++++++-------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/pint_xarray/tests/test_conversion.py b/pint_xarray/tests/test_conversion.py index 261ed9be..40013774 100644 --- a/pint_xarray/tests/test_conversion.py +++ b/pint_xarray/tests/test_conversion.py @@ -176,7 +176,6 @@ def test_attach_units(self, obj, units): assert conversion.extract_units(actual) == units - @pytest.mark.parametrize("variant", ("data", "coords", "both")) @pytest.mark.parametrize( "coords", ( @@ -194,32 +193,22 @@ def test_attach_units(self, obj, units): ), ) @pytest.mark.parametrize("typename", ("Variable", "DataArray", "Dataset")) - def test_convert_units(self, typename, coords, variant): - if variant in ("coords", "both") and not coords: - pytest.skip("no coords set") - if variant == "data" and coords: - pytest.skip("no need to test conversion of data") - + def test_convert_units(self, typename, coords): if typename == "Variable": - if coords or variant != "data": + if coords: pytest.skip("Variable doesn't store coordinates") data = np.linspace(0, 1, 3) * unit_registry.m obj = Variable(dims="x", data=data) units = {None: unit_registry.mm} - expected_units = units elif typename == "DataArray": obj = DataArray( dims="x", data=np.linspace(0, 1, 3) * unit_registry.Pa, coords=coords ) - variants = { - "data": {None: unit_registry.hPa}, - "coords": {"u": unit_registry.mm}, - "both": {None: unit_registry.hPa, "u": unit_registry.mm}, - } - units = variants.get(variant) - expected_units = {**conversion.extract_units(obj), **units} + units = {None: unit_registry.hPa} + if coords: + units["u"] = unit_registry.mm elif typename == "Dataset": obj = Dataset( data_vars={ @@ -229,21 +218,16 @@ def test_convert_units(self, typename, coords, variant): coords=coords, ) - data_units = { + units = { "a": unit_registry.ms, "b": unit_registry.gram, } - variants = { - "data": data_units, - "coords": {"u": unit_registry.mm}, - "both": dict(u=unit_registry.mm, **data_units), - } - units = variants.get(variant) - expected_units = {**conversion.extract_units(obj), **units} + if coords: + units["u"] = unit_registry.mm actual = conversion.convert_units(obj, units) - assert conversion.extract_units(actual) == expected_units + assert conversion.extract_units(actual) == units assert_equal(obj, actual) @pytest.mark.parametrize( From f2c17c369376803df9c7d1775436ac251cd351ec Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 20:44:18 +0200 Subject: [PATCH 24/31] properly separate the conversions --- pint_xarray/tests/test_conversion.py | 84 +++++++++++++++++++--------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/pint_xarray/tests/test_conversion.py b/pint_xarray/tests/test_conversion.py index 40013774..e77366d2 100644 --- a/pint_xarray/tests/test_conversion.py +++ b/pint_xarray/tests/test_conversion.py @@ -177,57 +177,91 @@ def test_attach_units(self, obj, units): assert conversion.extract_units(actual) == units @pytest.mark.parametrize( - "coords", + "variant", ( - pytest.param({}, id="no coords"), + "data", pytest.param( - {"u": ("x", [10, 3, 4] * unit_registry.m)}, id="non-dimension coord" - ), - pytest.param( - {"x": [0, 1, 2]}, - id="dimension coordinate", - marks=pytest.mark.xfail( - reason="converting indexes not implemented yet" - ), + "dims", marks=pytest.mark.xfail(reason="indexes don't support units") ), + "coords", ), ) @pytest.mark.parametrize("typename", ("Variable", "DataArray", "Dataset")) - def test_convert_units(self, typename, coords): + def test_convert_units(self, typename, variant): if typename == "Variable": - if coords: + if variant != "data": pytest.skip("Variable doesn't store coordinates") data = np.linspace(0, 1, 3) * unit_registry.m obj = Variable(dims="x", data=data) units = {None: unit_registry.mm} + expected_units = units elif typename == "DataArray": + unit_variants = { + "data": (unit_registry.Pa, 1, 1), + "dims": (1, unit_registry.s, 1), + "coords": (1, 1, unit_registry.m), + } + data_unit, dim_unit, coord_unit = unit_variants.get(variant) + + coords = { + "data": {}, + "dims": {"x": [0, 1, 2] * dim_unit}, + "coords": {"u": ("x", [10, 3, 4] * coord_unit)}, + } + obj = DataArray( - dims="x", data=np.linspace(0, 1, 3) * unit_registry.Pa, coords=coords + dims="x", + data=np.linspace(0, 1, 3) * data_unit, + coords=coords.get(variant), ) + template = { + **{obj.name: None}, + **{name: None for name in obj.coords}, + } + units = { + "data": {None: unit_registry.hPa}, + "dims": {"x": unit_registry.ms}, + "coords": {"u": unit_registry.mm}, + }.get(variant) - units = {None: unit_registry.hPa} - if coords: - units["u"] = unit_registry.mm + expected_units = {**template, **units} elif typename == "Dataset": + unit_variants = { + "data": ((unit_registry.s, unit_registry.kg), 1, 1), + "dims": ((1, 1), unit_registry.s, 1), + "coords": ((1, 1), 1, unit_registry.m), + } + (data_unit1, data_unit2), dim_unit, coord_unit = unit_variants.get(variant) + + coords = { + "data": {}, + "dims": {"x": [0, 1, 2] * dim_unit}, + "coords": {"u": ("x", [10, 3, 4] * coord_unit)}, + } + obj = Dataset( data_vars={ - "a": ("x", np.linspace(-1, 1, 3) * unit_registry.s), - "b": ("x", np.linspace(1, 2, 3) * unit_registry.kg), + "a": ("x", np.linspace(-1, 1, 3) * data_unit1), + "b": ("x", np.linspace(1, 2, 3) * data_unit2), }, - coords=coords, + coords=coords.get(variant), ) - units = { - "a": unit_registry.ms, - "b": unit_registry.gram, + template = { + **{name: None for name in obj.data_vars.keys()}, + **{name: None for name in obj.coords.keys()}, } - if coords: - units["u"] = unit_registry.mm + units = { + "data": {"a": unit_registry.ms, "b": unit_registry.g}, + "dims": {"x": unit_registry.ms}, + "coords": {"u": unit_registry.mm}, + }.get(variant) + expected_units = {**template, **units} actual = conversion.convert_units(obj, units) - assert conversion.extract_units(actual) == units + assert conversion.extract_units(actual) == expected_units assert_equal(obj, actual) @pytest.mark.parametrize( From 2e07ad8d54eefc1d6d55364337ce6c8f879bc42d Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 20:47:50 +0200 Subject: [PATCH 25/31] try separating the example descriptions --- pint_xarray/accessors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index bf992e93..a420020d 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -286,6 +286,7 @@ def to(self, units=None, **unit_kwargs): Dimensions without coordinates: x Convert the data + >>> da.pint.to("mm") @@ -300,6 +301,7 @@ def to(self, units=None, **unit_kwargs): Dimensions without coordinates: x Convert coordinates + >>> da.pint.to({"u": ureg.ms}) @@ -314,6 +316,7 @@ def to(self, units=None, **unit_kwargs): Dimensions without coordinates: x Convert both simultaneously + >>> da.pint.to("mm", u="ms") @@ -490,6 +493,7 @@ def to(self, units=None, **unit_kwargs): b (x) float64 Convert the data + >>> ds.pint.to({"a": "mm", "b": ureg.g}) Dimensions: (x: 5) @@ -510,6 +514,7 @@ def to(self, units=None, **unit_kwargs): b (x) float64 >> ds.pint.to({"u": ureg.ms}) Dimensions: (x: 5) @@ -530,6 +535,7 @@ def to(self, units=None, **unit_kwargs): b (x) float64 Convert both simultaneously + >>> ds.pint.to(a=ureg.mm, b=ureg.g, u="ms") Dimensions: (x: 5) From 1e20fda2a4ed41e194f5a94df3e0d2f949d61a95 Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 21:03:50 +0200 Subject: [PATCH 26/31] properly refer to pint.Unit --- pint_xarray/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index a420020d..9cced061 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -260,7 +260,7 @@ def to(self, units=None, **unit_kwargs): units : str or pint.Unit or mapping of hashable to str or pint.Unit The units to convert to. If a unit name or pint.Unit, convert the DataArray's data. If a dict-like, it has to - map a variable name to a unit name or pint.Unit object. + map a variable name to a unit name or `pint.Unit` object. **unit_kwargs The kwargs form of ``units``. Can only be used for variable names that are strings and valid python identifiers. From 3077b60081fa8b3e754341e978cce5ea3d61f72b Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 21:04:25 +0200 Subject: [PATCH 27/31] show how to use a dict-like to convert the data --- pint_xarray/accessors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 9cced061..a6a26960 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -299,6 +299,12 @@ def to(self, units=None, **unit_kwargs): Coordinates: u (x) int64 Dimensions without coordinates: x + >>> da.pint.to({da.name: "mm"}) + + + Coordinates: + u (x) int64 + Dimensions without coordinates: x Convert coordinates From c875299eb81c34e6dfbfd6cd34b4ca83f25e4aac Mon Sep 17 00:00:00 2001 From: Keewis Date: Wed, 8 Jul 2020 21:10:03 +0200 Subject: [PATCH 28/31] fix the docstrings --- pint_xarray/accessors.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index a6a26960..4b6d1a51 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -257,10 +257,11 @@ def to(self, units=None, **unit_kwargs): Parameters ---------- - units : str or pint.Unit or mapping of hashable to str or pint.Unit - The units to convert to. If a unit name or pint.Unit, - convert the DataArray's data. If a dict-like, it has to - map a variable name to a unit name or `pint.Unit` object. + units : str or pint.Unit or mapping of hashable to str or pint.Unit, optional + The units to convert to. If a unit name or + :py:class`pint.Unit` object, convert the DataArray's + data. If a dict-like, it has to map a variable name to a + unit name or :py:class:`pint.Unit` object. **unit_kwargs The kwargs form of ``units``. Can only be used for variable names that are strings and valid python identifiers. @@ -466,10 +467,8 @@ def to(self, units=None, **unit_kwargs): Parameters ---------- - units : str or pint.Unit or mapping of hashable to str or pint.Unit - The units to convert to. If a unit name or pint.Unit, - convert the DataArray's data. If a dict-like, it has to - map a variable name to a unit name or pint.Unit object. + units : mapping of hashable to str or pint.Unit, optional + Maps variable names to the unit to convert to. **unit_kwargs The kwargs form of ``units``. Can only be used for variable names that are strings and valid python identifiers. From 682ef8467a6eff3acd41991b654bcac06dfd97fa Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 9 Jul 2020 12:53:11 +0200 Subject: [PATCH 29/31] add a comment on the source of the utility function --- pint_xarray/accessors.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 4b6d1a51..134df42c 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -35,6 +35,7 @@ def is_dict_like(obj): return hasattr(obj, "keys") and hasattr(obj, "__getitem__") +# based on xarray.core.utils.either_dict_or_kwargs def either_dict_or_kwargs(positional, keywords, method_name): if positional is not None: if not is_dict_like(positional): From 426c60781a73b649f5596410fae8ee969d28a097 Mon Sep 17 00:00:00 2001 From: Keewis Date: Fri, 10 Jul 2020 15:08:27 +0200 Subject: [PATCH 30/31] link to the source and add xarray's license --- licenses/XARRAY_LICENSE | 191 +++++++++++++++++++++++++++++++++++++++ pint_xarray/accessors.py | 1 + 2 files changed, 192 insertions(+) create mode 100644 licenses/XARRAY_LICENSE diff --git a/licenses/XARRAY_LICENSE b/licenses/XARRAY_LICENSE new file mode 100644 index 00000000..37ec93a1 --- /dev/null +++ b/licenses/XARRAY_LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 134df42c..ed22e58a 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -36,6 +36,7 @@ def is_dict_like(obj): # based on xarray.core.utils.either_dict_or_kwargs +# https://github.com/pydata/xarray/blob/v0.15.1/xarray/core/utils.py#L249-L268 def either_dict_or_kwargs(positional, keywords, method_name): if positional is not None: if not is_dict_like(positional): From 7ac1c56a874d89d0f46a065af975a8c17f58e198 Mon Sep 17 00:00:00 2001 From: Keewis Date: Fri, 10 Jul 2020 16:05:33 +0200 Subject: [PATCH 31/31] add a copyright notice to the xarray license --- licenses/XARRAY_LICENSE | 3 +++ 1 file changed, 3 insertions(+) diff --git a/licenses/XARRAY_LICENSE b/licenses/XARRAY_LICENSE index 37ec93a1..978b509e 100644 --- a/licenses/XARRAY_LICENSE +++ b/licenses/XARRAY_LICENSE @@ -1,3 +1,6 @@ +Copyright 2014-2020, xarray developers + + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/