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
Copy file name to clipboardExpand all lines: README.md
+12-5Lines changed: 12 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,12 +291,15 @@ class Image(AsDataArray):
291
291
y: Coordof[YAxis] =0
292
292
```
293
293
294
-
### Custom DataArray and Dataset factories
294
+
### Options for DataArray and Dataset creation
295
+
296
+
For customization, users can add a special class attribute, `__dataoptions__`, to a DataArray or Dataset class.
297
+
A custom factory for DataArray or Dataset creation is only supported in the current implementation.
295
298
296
-
For customization, users can use a function or a class to create an initial DataArray or Dataset object by specifying a special class attribute, `__dataarray_factory__` or `__dataset_factory__`, respectively.
297
299
298
300
```python
299
301
import xarray as xr
302
+
from xarray_dataclasses import DataOptions
300
303
301
304
302
305
classCustom(xr.DataArray):
@@ -308,19 +311,23 @@ class Custom(xr.DataArray):
308
311
print("Custom method!")
309
312
310
313
314
+
dataoptions = DataOptions(Custom)
315
+
316
+
311
317
@dataclass
312
318
classImage(AsDataArray):
313
319
"""Specs for a monochromatic image."""
314
320
321
+
__dataoptions__ = dataoptions
322
+
315
323
data: Data[tuple[X, Y], float]
316
324
x: Coord[X, int] =0
317
325
y: Coord[Y, int] =0
318
-
__dataarray_factory__ = Custom
319
326
320
327
321
328
image = Image.ones([3, 3])
322
-
isinstance(image, Custom) # True
323
-
image.custom_method() # Custom method!
329
+
isinstance(image, Custom) # True
330
+
image.custom_method() # Custom method!
324
331
```
325
332
326
333
### DataArray and Dataset creation without shorthands
0 commit comments