1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
import numpy as np
import pandas as pd
import pytest
import xarray as xr
from xarray.core.indexes import IndexSelResult, PandasIndex
from pint_xarray import unit_registry as ureg
from pint_xarray.index import PintIndex
def indexer_equal(first, second):
if type(first) is not type(second):
return False
if isinstance(first, np.ndarray):
return np.all(first == second)
else:
return first == second
@pytest.mark.parametrize(
"base_index",
[
PandasIndex(pd.Index([1, 2, 3]), dim="x"),
PandasIndex(pd.Index([0.1, 0.2, 0.3]), dim="x"),
PandasIndex(pd.Index([1j, 2j, 3j]), dim="y"),
],
)
@pytest.mark.parametrize("units", [ureg.Unit("m"), ureg.Unit("s")])
def test_init(base_index, units):
index = PintIndex(index=base_index, units={base_index.dim: units})
assert index.index.equals(base_index)
assert index.units == {base_index.dim: units}
def test_init_error():
base_index = PandasIndex(pd.Index([1, 2, 3]), dim="x")
with pytest.raises(TypeError, match="dict of coordinate names to units"):
PintIndex(index=base_index, units=ureg.Unit("s"))
def test_replace():
old_index = PandasIndex([1, 2, 3], dim="x")
new_index = PandasIndex([0.1, 0.2, 0.3], dim="x")
old = PintIndex(index=old_index, units={"x": ureg.Unit("m")})
new = old._replace(new_index)
assert new.index.equals(new_index)
assert new.units == old.units
# no mutation
assert old.index.equals(old_index)
@pytest.mark.parametrize(
["wrapped_index", "units", "expected"],
(
pytest.param(
PandasIndex(pd.Index([1, 2, 3]), dim="x"),
{"x": ureg.Unit("m")},
{"x": xr.Variable("x", ureg.Quantity([1, 2, 3], "m"))},
),
pytest.param(
PandasIndex(pd.Index([1j, 2j, 3j]), dim="y"),
{"y": ureg.Unit("ms")},
{"y": xr.Variable("y", ureg.Quantity([1j, 2j, 3j], "ms"))},
),
),
)
def test_create_variables(wrapped_index, units, expected):
index = PintIndex(index=wrapped_index, units=units)
actual = index.create_variables()
assert list(actual.keys()) == list(expected.keys())
assert all([actual[k].equals(expected[k]) for k in expected.keys()])
@pytest.mark.parametrize(
["labels", "expected"],
(
({"x": ureg.Quantity(1, "m")}, IndexSelResult(dim_indexers={"x": 0})),
({"x": ureg.Quantity(3000, "mm")}, IndexSelResult(dim_indexers={"x": 2})),
({"x": ureg.Quantity(0.002, "km")}, IndexSelResult(dim_indexers={"x": 1})),
(
{"x": ureg.Quantity([0.002, 0.004], "km")},
IndexSelResult(dim_indexers={"x": np.array([1, 3])}),
),
(
{"x": slice(ureg.Quantity(2, "m"), ureg.Quantity(3000, "mm"))},
IndexSelResult(dim_indexers={"x": slice(1, 3)}),
),
),
)
def test_sel(labels, expected):
index = PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"), units={"x": ureg.Unit("m")}
)
actual = index.sel(labels)
assert isinstance(actual, IndexSelResult)
assert list(actual.dim_indexers.keys()) == list(expected.dim_indexers.keys())
assert all(
[
indexer_equal(actual.dim_indexers[k], expected.dim_indexers[k])
for k in expected.dim_indexers.keys()
]
)
@pytest.mark.parametrize(
["labels", "expected"],
(
(
{"x": ureg.Quantity(1.1, "m")},
IndexSelResult(dim_indexers={"x": np.array(0)}),
),
(
{"x": ureg.Quantity(3100, "mm")},
IndexSelResult(dim_indexers={"x": np.array(2)}),
),
(
{"x": ureg.Quantity(0.0021, "km")},
IndexSelResult(dim_indexers={"x": np.array(1)}),
),
(
{"x": ureg.Quantity([0.0021, 0.0041], "km")},
IndexSelResult(dim_indexers={"x": np.array([1, 3])}),
),
),
)
def test_sel_nearest(labels, expected):
index = PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"), units={"x": ureg.Unit("m")}
)
actual = index.sel(labels, method="nearest")
assert isinstance(actual, IndexSelResult)
assert actual.dim_indexers.keys() == expected.dim_indexers.keys()
assert all(
indexer_equal(actual.dim_indexers[k], expected.dim_indexers[k])
for k in expected.dim_indexers.keys()
)
@pytest.mark.parametrize(
"indexers",
({"y": 0}, {"y": [1, 2]}, {"y": slice(0, None, 2)}, {"y": xr.Variable("y", [1])}),
)
def test_isel(indexers):
wrapped_index = PandasIndex(pd.Index([1, 2, 3, 4]), dim="y")
index = PintIndex(index=wrapped_index, units={"y": ureg.Unit("s")})
actual = index.isel(indexers)
wrapped_ = wrapped_index.isel(indexers)
if wrapped_ is not None:
expected = PintIndex(
index=wrapped_index.isel(indexers), units={"y": ureg.Unit("s")}
)
else:
expected = None
assert (actual is None and expected is None) or actual.equals(expected)
@pytest.mark.parametrize(
["other", "expected"],
(
(
PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"),
units={"x": ureg.Unit("cm")},
),
True,
),
(PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"), False),
(
PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"),
units={"x": ureg.Unit("m")},
),
False,
),
(
PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="y"),
units={"y": ureg.Unit("cm")},
),
False,
),
(
PintIndex(
index=PandasIndex(pd.Index([1, 3, 3, 4]), dim="x"),
units={"x": ureg.Unit("cm")},
),
False,
),
),
)
def test_equals(other, expected):
index = PintIndex(
index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"), units={"x": ureg.Unit("cm")}
)
actual = index.equals(other)
assert actual == expected
@pytest.mark.filterwarnings("error")
def test_align_equals_warning():
index1 = PintIndex(
index=PandasIndex(pd.Index([1, 2]), dim="x"),
units={"x": ureg.Unit("m")},
)
index2 = PintIndex(
index=PandasIndex(pd.Index([0, 1, 2]), dim="y"),
units={"y": ureg.Unit("m")},
)
ds = xr.Dataset(
{"a": (["y", "x"], [[-1, 1], [0, 2], [1, 3]])},
coords=xr.Coordinates(
{"x": [1, 2], "y": [0, 1, 2]}, indexes={"x": index1, "y": index2}
),
)
# trigger comparison
ds["a"] * ds["x"] * ds["y"]
@pytest.mark.parametrize(
["shifts", "expected_index"],
(
({"x": 0}, PandasIndex(pd.Index([-2, -1, 0, 1, 2]), dim="x")),
({"x": 1}, PandasIndex(pd.Index([2, -2, -1, 0, 1]), dim="x")),
({"x": 2}, PandasIndex(pd.Index([1, 2, -2, -1, 0]), dim="x")),
({"x": -1}, PandasIndex(pd.Index([-1, 0, 1, 2, -2]), dim="x")),
({"x": -2}, PandasIndex(pd.Index([0, 1, 2, -2, -1]), dim="x")),
),
)
def test_roll(shifts, expected_index):
index = PintIndex(
index=PandasIndex(pd.Index([-2, -1, 0, 1, 2]), dim="x"),
units={"x": ureg.Unit("m")},
)
actual = index.roll(shifts)
expected = index._replace(expected_index)
assert actual.equals(expected)
@pytest.mark.parametrize("dims_dict", ({"y": "x"}, {"y": "z"}))
@pytest.mark.parametrize("name_dict", ({"y2": "y3"}, {"y2": "y1"}))
def test_rename(name_dict, dims_dict):
wrapped_index = PandasIndex(pd.Index([1, 2], name="y2"), dim="y")
index = PintIndex(index=wrapped_index, units={"y": ureg.Unit("m")})
actual = index.rename(name_dict, dims_dict)
expected = PintIndex(
index=wrapped_index.rename(name_dict, dims_dict), units=index.units
)
assert actual.equals(expected)
@pytest.mark.parametrize("indexer", ([0], slice(0, 2)))
def test_getitem(indexer):
wrapped_index = PandasIndex(pd.Index([1, 2], name="y2"), dim="y")
index = PintIndex(index=wrapped_index, units={"y": ureg.Unit("m")})
actual = index[indexer]
expected = PintIndex(index=wrapped_index[indexer], units=index.units)
assert actual.equals(expected)
@pytest.mark.parametrize("wrapped_index", (PandasIndex(pd.Index([1, 2]), dim="x"),))
def test_repr_inline(wrapped_index):
index = PintIndex(index=wrapped_index, units={"x": ureg.Unit("m")})
# TODO: parametrize
actual = index._repr_inline_(90)
assert "PintIndex" in actual
assert wrapped_index.__class__.__name__ in actual
assert "units" in actual
@pytest.mark.parametrize("wrapped_index", (PandasIndex(pd.Index([1, 2]), dim="x"),))
def test_repr(wrapped_index):
index = PintIndex(index=wrapped_index, units={"x": ureg.Unit("m")})
# TODO: parametrize
actual = repr(index)
assert "<PintIndex" in actual
assert "'x': 'm'" in actual
assert wrapped_index.__class__.__name__ in actual
|