A wrapper around duck arrays with named dimensions and attributes which describe a single Array. Numeric operations on this object implement array broadcasting and dimension alignment based on dimension names, rather than axis order. Parameters ---------- dims : st
| 213 | |
| 214 | |
| 215 | class NamedArray(NamedArrayAggregations, Generic[_ShapeType_co, _DType_co]): |
| 216 | """ |
| 217 | A wrapper around duck arrays with named dimensions |
| 218 | and attributes which describe a single Array. |
| 219 | Numeric operations on this object implement array broadcasting and |
| 220 | dimension alignment based on dimension names, |
| 221 | rather than axis order. |
| 222 | |
| 223 | |
| 224 | Parameters |
| 225 | ---------- |
| 226 | dims : str or iterable of hashable |
| 227 | Name(s) of the dimension(s). |
| 228 | data : array-like or duck-array |
| 229 | The actual data that populates the array. Should match the |
| 230 | shape specified by `dims`. |
| 231 | attrs : dict, optional |
| 232 | A dictionary containing any additional information or |
| 233 | attributes you want to store with the array. |
| 234 | Default is None, meaning no attributes will be stored. |
| 235 | |
| 236 | Raises |
| 237 | ------ |
| 238 | ValueError |
| 239 | If the `dims` length does not match the number of data dimensions (ndim). |
| 240 | |
| 241 | |
| 242 | Examples |
| 243 | -------- |
| 244 | >>> data = np.array([1.5, 2, 3], dtype=float) |
| 245 | >>> narr = NamedArray(("x",), data, {"units": "m"}) # TODO: Better name than narr? |
| 246 | """ |
| 247 | |
| 248 | __slots__ = ("_attrs", "_data", "_dims") |
| 249 | |
| 250 | _data: duckarray[Any, _DType_co] |
| 251 | _dims: _Dims |
| 252 | _attrs: dict[Any, Any] | None |
| 253 | |
| 254 | def __init__( |
| 255 | self, |
| 256 | dims: _DimsLike, |
| 257 | data: duckarray[Any, _DType_co], |
| 258 | attrs: _AttrsLike = None, |
| 259 | ): |
| 260 | self._data = data |
| 261 | self._dims = self._parse_dimensions(dims) |
| 262 | self._attrs = dict(attrs) if attrs else None |
| 263 | |
| 264 | def __init_subclass__(cls, **kwargs: Any) -> None: |
| 265 | if NamedArray in cls.__bases__ and (cls._new == NamedArray._new): |
| 266 | # Type hinting does not work for subclasses unless _new is |
| 267 | # overridden with the correct class. |
| 268 | raise TypeError( |
| 269 | "Subclasses of `NamedArray` must override the `_new` method." |
| 270 | ) |
| 271 | super().__init_subclass__(**kwargs) |
| 272 |
no outgoing calls
searching dependent graphs…