Skip to content

v1.2.0 (2022-06-09)

Compare
Choose a tag to compare
@astropenguin astropenguin released this 08 Jun 15:33
· 96 commits to main since this release
8508b6e

From this release, special type hints (Attr, Coord, Data, Name) can be used within union types.
This would be useful if you want to customize values after dataclass object creation (__post_init__).
The following example automatically sets ranged values to x and y coordinates if they are not specified.

import numpy as np
from dataclasses import dataclass
from typing import Literal, Optional
from xarray_dataclasses import AsDataArray, Coord, Data


X = Literal["x"]
Y = Literal["y"]


@dataclass
class Image(AsDataArray):
    """2D image as DataArray."""

    data: Data[tuple[X, Y], float]
    x: Optional[Coord[X, int]] = None
    y: Optional[Coord[Y, int]] = None

    def __post_init__(self) -> None:
        """Set ranged values to x and y."""
        shape = np.shape(self.data)

        if self.x is None:
            self.x = np.arange(shape[0])

        if self.y is None:
            self.y = np.arange(shape[1])

What's Changed

Full Changelog: v1.1.0...v1.2.0