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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
from __future__ import annotations
import abc
from collections.abc import Hashable, Iterable, Mapping
from typing import TYPE_CHECKING, Any, Generic, TypeVar
import numpy as np
from xarray.core.dataarray import DataArray
from xarray.core.indexes import Index
from xarray.core.indexing import IndexSelResult
from xarray.core.utils import is_scalar
from xarray.core.variable import Variable
from xarray.structure.alignment import broadcast
if TYPE_CHECKING:
from scipy.spatial import KDTree
from xarray.core.types import Self
class TreeAdapter(abc.ABC):
"""Lightweight adapter abstract class for plugging in 3rd-party structures
like :py:class:`scipy.spatial.KDTree` or :py:class:`sklearn.neighbors.KDTree`
into :py:class:`~xarray.indexes.NDPointIndex`.
"""
@abc.abstractmethod
def __init__(self, points: np.ndarray, *, options: Mapping[str, Any]):
"""
Parameters
----------
points : ndarray of shape (n_points, n_coordinates)
Two-dimensional array of points/samples (rows) and their
corresponding coordinate labels (columns) to index.
"""
...
@abc.abstractmethod
def query(self, points: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""Query points.
Parameters
----------
points: ndarray of shape (n_points, n_coordinates)
Two-dimensional array of points/samples (rows) and their
corresponding coordinate labels (columns) to query.
Returns
-------
distances : ndarray of shape (n_points)
Distances to the nearest neighbors.
indices : ndarray of shape (n_points)
Indices of the nearest neighbors in the array of the indexed
points.
"""
...
def equals(self, other: Self) -> bool:
"""Check equality with another TreeAdapter of the same kind.
Parameters
----------
other :
The other TreeAdapter object to compare with this object.
"""
raise NotImplementedError
class ScipyKDTreeAdapter(TreeAdapter):
""":py:class:`scipy.spatial.KDTree` adapter for :py:class:`~xarray.indexes.NDPointIndex`."""
_kdtree: KDTree
def __init__(self, points: np.ndarray, options: Mapping[str, Any]):
from scipy.spatial import KDTree
self._kdtree = KDTree(points, **options)
def query(self, points: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
return self._kdtree.query(points)
def equals(self, other: Self) -> bool:
return np.array_equal(self._kdtree.data, other._kdtree.data)
def get_points(coords: Iterable[Variable | Any]) -> np.ndarray:
"""Re-arrange data from a sequence of xarray coordinate variables or
labels into a 2-d array of shape (n_points, n_coordinates).
"""
data = [c.values if isinstance(c, Variable | DataArray) else c for c in coords]
return np.stack([np.ravel(d) for d in data]).T
T_TreeAdapter = TypeVar("T_TreeAdapter", bound=TreeAdapter)
class NDPointIndex(Index, Generic[T_TreeAdapter]):
"""Xarray index for irregular, n-dimensional data.
This index may be associated with a set of coordinate variables representing
the arbitrary location of data points in an n-dimensional space. All
coordinates must have the same shape and dimensions. The number of
associated coordinate variables must correspond to the number of dimensions
of the space.
This index supports label-based selection (nearest neighbor lookup). It also
has limited support for alignment.
By default, this index relies on :py:class:`scipy.spatial.KDTree` for fast
lookup.
Do not use :py:meth:`~xarray.indexes.NDPointIndex.__init__` directly. Instead
use :py:meth:`xarray.Dataset.set_xindex` or
:py:meth:`xarray.DataArray.set_xindex` to create and set the index from
existing coordinates (see the example below).
Examples
--------
An example using a dataset with 2-dimensional coordinates.
>>> xx = [[1.0, 2.0], [3.0, 0.0]]
>>> yy = [[11.0, 21.0], [29.0, 9.0]]
>>> ds = xr.Dataset(coords={"xx": (("y", "x"), xx), "yy": (("y", "x"), yy)})
>>> ds
<xarray.Dataset> Size: 64B
Dimensions: (y: 2, x: 2)
Coordinates:
xx (y, x) float64 32B 1.0 2.0 3.0 0.0
yy (y, x) float64 32B 11.0 21.0 29.0 9.0
Dimensions without coordinates: y, x
Data variables:
*empty*
Creation of a NDPointIndex from the "xx" and "yy" coordinate variables:
>>> ds = ds.set_xindex(("xx", "yy"), xr.indexes.NDPointIndex)
>>> ds
<xarray.Dataset> Size: 64B
Dimensions: (y: 2, x: 2)
Coordinates:
* xx (y, x) float64 32B 1.0 2.0 3.0 0.0
* yy (y, x) float64 32B 11.0 21.0 29.0 9.0
Dimensions without coordinates: y, x
Data variables:
*empty*
Indexes:
┌ xx NDPointIndex (ScipyKDTreeAdapter)
└ yy
Point-wise (nearest-neighbor) data selection using Xarray's advanced
indexing, i.e., using arbitrary dimension(s) for the Variable objects passed
as labels:
>>> ds.sel(
... xx=xr.Variable("points", [1.9, 0.1]),
... yy=xr.Variable("points", [13.0, 8.0]),
... method="nearest",
... )
<xarray.Dataset> Size: 32B
Dimensions: (points: 2)
Coordinates:
xx (points) float64 16B 1.0 0.0
yy (points) float64 16B 11.0 9.0
Dimensions without coordinates: points
Data variables:
*empty*
Data selection with scalar labels:
>>> ds.sel(xx=1.9, yy=13.0, method="nearest")
<xarray.Dataset> Size: 16B
Dimensions: ()
Coordinates:
xx float64 8B 1.0
yy float64 8B 11.0
Data variables:
*empty*
Data selection with broadcasting the input labels:
>>> ds.sel(xx=1.9, yy=xr.Variable("points", [13.0, 8.0]), method="nearest")
<xarray.Dataset> Size: 32B
Dimensions: (points: 2)
Coordinates:
xx (points) float64 16B 1.0 0.0
yy (points) float64 16B 11.0 9.0
Dimensions without coordinates: points
Data variables:
*empty*
>>> da = xr.DataArray(
... [[45.1, 53.3], [65.4, 78.2]],
... coords={"u": [1.9, 0.1], "v": [13.0, 8.0]},
... dims=("u", "v"),
... )
>>> ds.sel(xx=da.u, yy=da.v, method="nearest")
<xarray.Dataset> Size: 64B
Dimensions: (u: 2, v: 2)
Coordinates:
xx (u, v) float64 32B 1.0 0.0 1.0 0.0
yy (u, v) float64 32B 11.0 9.0 11.0 9.0
Dimensions without coordinates: u, v
Data variables:
*empty*
Data selection with array-like labels (implicit dimensions):
>>> ds.sel(xx=[[1.9], [0.1]], yy=[[13.0], [8.0]], method="nearest")
<xarray.Dataset> Size: 32B
Dimensions: (y: 2, x: 1)
Coordinates:
xx (y, x) float64 16B 1.0 0.0
yy (y, x) float64 16B 11.0 9.0
Dimensions without coordinates: y, x
Data variables:
*empty*
"""
_tree_obj: T_TreeAdapter
_coord_names: tuple[Hashable, ...]
_dims: tuple[Hashable, ...]
_shape: tuple[int, ...]
def __init__(
self,
tree_obj: T_TreeAdapter,
*,
coord_names: tuple[Hashable, ...],
dims: tuple[Hashable, ...],
shape: tuple[int, ...],
):
# this constructor is "private"
assert isinstance(tree_obj, TreeAdapter)
self._tree_obj = tree_obj
assert len(coord_names) == len(dims) == len(shape)
self._coord_names = coord_names
self._dims = dims
self._shape = shape
@classmethod
def from_variables(
cls,
variables: Mapping[Any, Variable],
*,
options: Mapping[str, Any],
) -> Self:
if len({var.dims for var in variables.values()}) > 1:
var_names = ",".join(vn for vn in variables)
raise ValueError(
f"variables {var_names} must all have the same dimensions and the same shape"
)
var0 = next(iter(variables.values()))
if len(variables) != len(var0.dims):
raise ValueError(
f"the number of variables {len(variables)} doesn't match "
f"the number of dimensions {len(var0.dims)}"
)
opts = dict(options)
tree_adapter_cls: type[T_TreeAdapter] = opts.pop("tree_adapter_cls", None)
if tree_adapter_cls is None:
tree_adapter_cls = ScipyKDTreeAdapter
points = get_points(variables.values())
return cls(
tree_adapter_cls(points, options=opts),
coord_names=tuple(variables),
dims=var0.dims,
shape=var0.shape,
)
def create_variables(
self, variables: Mapping[Any, Variable] | None = None
) -> dict[Any, Variable]:
if variables is not None:
for var in variables.values():
# maybe re-sync variable dimensions with the index object
# returned by NDPointIndex.rename()
if var.dims != self._dims:
var.dims = self._dims
return dict(**variables)
else:
return {}
def equals(
self, other: Index, *, exclude: frozenset[Hashable] | None = None
) -> bool:
if not isinstance(other, NDPointIndex):
return False
if type(self._tree_obj) is not type(other._tree_obj):
return False
return self._tree_obj.equals(other._tree_obj)
def _get_dim_indexers(
self,
indices: np.ndarray,
label_dims: tuple[Hashable, ...],
label_shape: tuple[int, ...],
) -> dict[Hashable, Variable]:
"""Returns dimension indexers based on the query results (indices) and
the original label dimensions and shape.
1. Unravel the flat indices returned from the query
2. Reshape the unraveled indices according to indexers shapes
3. Wrap the indices in xarray.Variable objects.
"""
dim_indexers = {}
u_indices = list(np.unravel_index(indices.ravel(), self._shape))
for dim, ind in zip(self._dims, u_indices, strict=False):
dim_indexers[dim] = Variable(label_dims, ind.reshape(label_shape))
return dim_indexers
def sel(
self, labels: dict[Any, Any], method=None, tolerance=None
) -> IndexSelResult:
if method != "nearest":
raise ValueError(
"NDPointIndex only supports selection with method='nearest'"
)
missing_labels = set(self._coord_names) - set(labels)
if missing_labels:
missing_labels_str = ",".join([f"{name}" for name in missing_labels])
raise ValueError(f"missing labels for coordinate(s): {missing_labels_str}.")
# maybe convert labels into xarray DataArray objects
xr_labels: dict[Any, DataArray] = {}
for name, lbl in labels.items():
if isinstance(lbl, DataArray):
xr_labels[name] = lbl
elif isinstance(lbl, Variable):
xr_labels[name] = DataArray(lbl)
elif is_scalar(lbl):
xr_labels[name] = DataArray(lbl, dims=())
elif np.asarray(lbl).ndim == len(self._dims):
xr_labels[name] = DataArray(lbl, dims=self._dims)
else:
raise ValueError(
"invalid label value. NDPointIndex only supports advanced (point-wise) indexing "
"with the following label value kinds:\n"
"- xarray.DataArray or xarray.Variable objects\n"
"- scalar values\n"
"- unlabelled array-like objects with the same number of dimensions "
f"than the {self._coord_names} coordinate variables ({len(self._dims)})"
)
# broadcast xarray labels against one another and determine labels shape and dimensions
broadcasted = broadcast(*xr_labels.values())
label_dims = broadcasted[0].dims
label_shape = broadcasted[0].shape
xr_labels = dict(zip(xr_labels, broadcasted, strict=True))
# get and return dimension indexers
points = get_points(xr_labels[name] for name in self._coord_names)
_, indices = self._tree_obj.query(points)
dim_indexers = self._get_dim_indexers(indices, label_dims, label_shape)
return IndexSelResult(dim_indexers=dim_indexers)
def rename(
self,
name_dict: Mapping[Any, Hashable],
dims_dict: Mapping[Any, Hashable],
) -> Self:
if not set(self._coord_names) & set(name_dict) and not set(self._dims) & set(
dims_dict
):
return self
new_coord_names = tuple(name_dict.get(n, n) for n in self._coord_names)
new_dims = tuple(dims_dict.get(d, d) for d in self._dims)
return type(self)(
self._tree_obj,
coord_names=new_coord_names,
dims=new_dims,
shape=self._shape,
)
def _repr_inline_(self, max_width: int) -> str:
tree_obj_type = self._tree_obj.__class__.__name__
return f"{self.__class__.__name__} ({tree_obj_type})"
|