There is a newer version of the record available.

Published June 8, 2022 | Version v1.2.0
Software Open

astropenguin/xarray-dataclasses: Update release

Authors/Creators

  • 1. Nagoya University

Description

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):
        """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: https://github.com/astropenguin/xarray-dataclasses/compare/v1.1.0...v1.2.0

Files

astropenguin/xarray-dataclasses-v1.2.0.zip

Files (486.1 kB)

Name Size Download all
md5:5824d8149cac7f5f936e0006d55c6cf9
486.1 kB Preview Download

Additional details