You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -11,7 +11,7 @@ xarray extension for typed DataArray and Dataset creation
11
11
12
12
## Overview
13
13
14
-
xarray-dataclasses is a Python package that makes it easy to create typed DataArray and Dataset objects of [xarray]using [the Python's dataclass].
14
+
xarray-dataclasses is a Python package that makes it easy to create [xarray]'s DataArray and Dataset objects that are "typed" (i.e. fixed dimensions, data type, coordinates, attributes, and name) using [the Python's dataclass]:
- NumPy-like filled-data creation is also available:
42
+
```python
43
+
image = Image.zeros([2, 2], x=[0, 1], y=[0, 1])
44
+
```
45
+
- Support for features by [the Python's dataclass] (`field`, `__post_init__`, ...).
46
+
- Support for static type check by [Pyright].
48
47
49
48
### Installation
50
49
51
50
```shell
52
-
$ pip install xarray-dataclasses
51
+
pip install xarray-dataclasses
53
52
```
54
53
55
-
56
-
## Background
57
-
58
-
[xarray] is useful for handling labeled multi-dimensional data, but it is a bit troublesome to create DataArray and Dataset objects with fixed dimensions, data type, or coordinates (typed DataArray and typed Dataset).
59
-
For example, let us think about the following DataArray specifications for a monochromatic image.
60
-
61
-
- Dimensions of data must be `("x", "y")`.
62
-
- Data type of data must be `float`.
63
-
- Data type of dimensions must be `int`.
64
-
- Default value of dimensions must be `0`.
65
-
66
-
Then a function to create a typed DataArray object is something like this.
67
-
68
-
```python
69
-
import numpy as np
70
-
import xarray as xr
71
-
72
-
73
-
defcreate_image(data, x=0, y=0):
74
-
"""Create a monochromatic image."""
75
-
data = np.array(data)
76
-
77
-
if x ==0:
78
-
x = np.full(data.shape[0], x)
79
-
else:
80
-
x = np.array(x)
81
-
82
-
if y ==0:
83
-
y = np.full(data.shape[1], y)
84
-
else:
85
-
y = np.array(y)
86
-
87
-
return xr.DataArray(
88
-
data=data.astype(float),
89
-
dims=("x", "y"),
90
-
coords={
91
-
"x": ("x", x.astype(int)),
92
-
"y": ("y", y.astype(int)),
93
-
},
94
-
)
95
-
96
-
97
-
image = create_image([[0, 1], [2, 3]])
98
-
```
99
-
100
-
The issues are
101
-
102
-
- It is not easy to figure out the specifications from the code.
103
-
- It is not easy to reuse the code, for example, to add new coordinates.
104
-
105
-
xarray-dataclasses resolves them by defining the specifications as a dataclass.
106
-
As shown in the code in the overview, the specifications become much easier to read.
107
-
108
-
- The type hints have complete information for DataArray creation.
109
-
- The default values are given as class variables.
110
-
- The mix-in class `AsDataArray` provides class methods such as `new()`.
111
-
- The extension of the specifications is easy by class inheritance.
112
-
113
54
## Basic usage
114
55
115
56
xarray-dataclasses uses [the Python's dataclass].
116
-
Please learn how to use it before proceeding.
117
-
Data (or data variables), coordinates, attributes, and a name of a DataArray or a Dataset object are defined as dataclass fields with the following type hints.
57
+
Data (or data variables), coordinates, attributes, and a name of DataArray or Dataset objects will be defined as dataclass fields by special type hints (`Data`, `Coord`, `Attr`, `Name`), respectively.
118
58
Note that the following code is supposed in the examples below.
119
59
120
60
```python
@@ -130,14 +70,15 @@ Y = Literal["y"]
130
70
131
71
### Data field
132
72
133
-
The data field is a field whose value will become the data of a DataArray object or a data variable of a Dataset object.
73
+
Data field is a field whose value will become the data of a DataArray object or a data variable of a Dataset object.
134
74
The type hint `Data[TDims, TDtype]` fixes the dimensions and the data type of the object.
0 commit comments