xarray-dataclasses
:zap: xarray data creation by data classes
Science Score: 67.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 3 DOI reference(s) in README -
○Academic publication links
-
✓Committers with academic emails
1 of 5 committers (20.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.0%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
:zap: xarray data creation by data classes
Basic Info
- Host: GitHub
- Owner: astropenguin
- License: mit
- Language: Python
- Default Branch: main
- Homepage: https://astropenguin.github.io/xarray-dataclasses/v2.0.0
- Size: 3.4 MB
Statistics
- Stars: 80
- Watchers: 3
- Forks: 7
- Open Issues: 12
- Releases: 28
Topics
Metadata Files
README.md
xarray-dataclasses
xarray data creation by data classes
Overview
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:
```python from dataclasses import dataclass from typing import Literal 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: Coord[X, int] = 0
y: Coord[Y, int] = 0
```
Features
- Typed DataArray or Dataset objects can easily be created:
python image = Image.new([[0, 1], [2, 3]], [0, 1], [0, 1]) - NumPy-like filled-data creation is also available:
python image = Image.zeros([2, 2], x=[0, 1], y=[0, 1]) - Support for features by the Python's dataclass.
- Support for static type check by Pyright.
Installation
shell
pip install xarray-dataclasses
Basic usage
xarray-dataclasses uses the Python's dataclass.
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.
Note that the following code is supposed in the examples below.
```python from dataclasses import dataclass from typing import Literal from xarraydataclasses import AsDataArray, AsDataset from xarraydataclasses import Attr, Coord, Data, Name
X = Literal["x"] Y = Literal["y"] ```
Data field
Data field is a field whose value will become the data of a DataArray object or a data variable of a Dataset object.
The type hint Data[TDims, TDtype] fixes the dimensions and the data type of the object.
Here are some examples of how to specify them.
Type hint | Inferred dimensions
--- | ---
Data[tuple[()], ...] | ()
Data[Literal["x"], ...] | ("x",)
Data[tuple[Literal["x"]], ...] | ("x",)
Data[tuple[Literal["x"], Literal["y"]], ...] | ("x", "y")
Type hint | Inferred data type
--- | ---
Data[..., Any] | None
Data[..., None] | None
Data[..., float] | numpy.dtype("float64")
Data[..., numpy.float128] | numpy.dtype("float128")
Data[..., Literal["datetime64[ns]"]] | numpy.dtype("<M8[ns]")
Coordinate field
Coordinate field is a field whose value will become a coordinate of a DataArray or a Dataset object.
The type hint Coord[TDims, TDtype] fixes the dimensions and the data type of the object.
Attribute field
Attribute field is a field whose value will become an attribute of a DataArray or a Dataset object.
The type hint Attr[TAttr] specifies the type of the value, which is used only for static type check.
Name field
Name field is a field whose value will become the name of a DataArray object.
The type hint Name[TName] specifies the type of the value, which is used only for static type check.
DataArray class
DataArray class is a dataclass that defines typed DataArray specifications. Exactly one data field is allowed in a DataArray class. The second and subsequent data fields are just ignored in DataArray creation.
```python @dataclass class Image(AsDataArray): """2D image as DataArray."""
data: Data[tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
units: Attr[str] = "cd / m^2"
name: Name[str] = "luminance"
```
A DataArray object will be created by a class method new():
```python Image.new([[0, 1], [2, 3]], x=[0, 1], y=[0, 1])
NumPy-like class methods (zeros(), ones(), ...) are also available:
```python Image.ones((3, 3))
Dataset class
Dataset class is a dataclass that defines typed Dataset specifications. Multiple data fields are allowed to define the data variables of the object.
```python @dataclass class ColorImage(AsDataset): """2D color image as Dataset."""
red: Data[tuple[X, Y], float]
green: Data[tuple[X, Y], float]
blue: Data[tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
units: Attr[str] = "cd / m^2"
```
A Dataset object will be created by a class method new():
```python ColorImage.new( [[0, 0], [0, 0]], # red [[1, 1], [1, 1]], # green [[2, 2], [2, 2]], # blue )
Advanced usage
Coordof and Dataof type hints
xarray-dataclasses provides advanced type hints, Coordof and Dataof.
Unlike Data and Coord, they specify a dataclass that defines a DataArray class.
This is useful when users want to add metadata to dimensions for plotting.
For example:
```python from xarray_dataclasses import Coordof
@dataclass class XAxis: data: Data[X, int] long_name: Attr[str] = "x axis" units: Attr[str] = "pixel"
@dataclass class YAxis: data: Data[Y, int] long_name: Attr[str] = "y axis" units: Attr[str] = "pixel"
@dataclass class Image(AsDataArray): """2D image as DataArray."""
data: Data[tuple[X, Y], float]
x: Coordof[XAxis] = 0
y: Coordof[YAxis] = 0
```
General data variable names in Dataset creation
Due to the limitation of Python's parameter names, it is not possible to define data variable names that contain white spaces, for example.
In such cases, please define DataArray classes of each data variable so that they have name fields and specify them by Dataof in a Dataset class.
Then the values of the name fields will be used as data variable names.
For example:
```python @dataclass class Red: data: Data[tuple[X, Y], float] name: Name[str] = "Red image"
@dataclass class Green: data: Data[tuple[X, Y], float] name: Name[str] = "Green image"
@dataclass class Blue: data: Data[tuple[X, Y], float] name: Name[str] = "Blue image"
@dataclass class ColorImage(AsDataset): """2D color image as Dataset."""
red: Dataof[Red]
green: Dataof[Green]
blue: Dataof[Blue]
```
```python ColorImage.new( [[0, 0], [0, 0]], [[1, 1], [1, 1]], [[2, 2], [2, 2]], )
Customization of DataArray or Dataset creation
For customization, users can add a special class attribute, __dataoptions__, to a DataArray or Dataset class.
A custom factory for DataArray or Dataset creation is only supported in the current implementation.
```python import xarray as xr from xarray_dataclasses import DataOptions
class Custom(xr.DataArray): """Custom DataArray."""
__slots__ = ()
def custom_method(self) -> bool:
"""Custom method."""
return True
@dataclass class Image(AsDataArray): """2D image as DataArray."""
data: Data[tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
__dataoptions__ = DataOptions(Custom)
image = Image.ones([3, 3]) isinstance(image, Custom) # True image.custom_method() # True ```
DataArray and Dataset creation without shorthands
xarray-dataclasses provides functions, asdataarray and asdataset.
This is useful when users do not want to inherit the mix-in class (AsDataArray or AsDataset) in a DataArray or Dataset dataclass.
For example:
```python from xarray_dataclasses import asdataarray
@dataclass class Image: """2D image as DataArray."""
data: Data[tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
image = asdataarray(Image([[0, 1], [2, 3]], [0, 1], [0, 1])) ```
Owner
- Name: Akio Taniguchi
- Login: astropenguin
- Kind: user
- Location: Nagoya, Japan
- Company: Nagoya University
- Website: https://astropengu.in
- Twitter: astropengu_in
- Repositories: 76
- Profile: https://github.com/astropenguin
Project assistant professor (LMT-FINER)
Citation (CITATION.cff)
# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!
cff-version: 1.2.0
title: xarray-dataclasses
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Akio
family-names: Taniguchi
email: taniguchi.akio@gmail.com
affiliation: Kitami Institute of Technology
orcid: 'https://orcid.org/0000-0002-9695-6183'
identifiers:
- type: doi
value: 10.5281/zenodo.10652375
repository-code: 'https://github.com/astropenguin/xarray-dataclasses'
url: 'https://astropenguin.github.io/xarray-dataclasses/v2.0.0'
abstract: xarray data creation by data classes
keywords:
- python
- dataclasses
- specifications
- typing
- xarray
license: MIT
version: 2.0.0
date-released: '2025-01-01'
GitHub Events
Total
- Create event: 6
- Release event: 2
- Issues event: 14
- Watch event: 11
- Delete event: 3
- Issue comment event: 4
- Push event: 12
- Pull request event: 7
- Fork event: 3
Last Year
- Create event: 6
- Release event: 2
- Issues event: 14
- Watch event: 11
- Delete event: 3
- Issue comment event: 4
- Push event: 12
- Pull request event: 7
- Fork event: 3
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Akio Taniguchi | t****i@a****p | 645 |
| Sohum Banerjea | s****b@g****m | 3 |
| Shaun Cutts | s****n@c****t | 2 |
| dependabot[bot] | 4****] | 1 |
| Matt McCormick | m****t@m****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 79
- Total pull requests: 66
- Average time to close issues: about 2 months
- Average time to close pull requests: 8 days
- Total issue authors: 13
- Total pull request authors: 5
- Average comments per issue: 0.16
- Average comments per pull request: 0.2
- Merged pull requests: 56
- Bot issues: 0
- Bot pull requests: 1
Past Year
- Issues: 10
- Pull requests: 7
- Average time to close issues: 10 days
- Average time to close pull requests: 5 minutes
- Issue authors: 7
- Pull request authors: 2
- Average comments per issue: 0.1
- Average comments per pull request: 0.0
- Merged pull requests: 4
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- astropenguin (61)
- krokosik (2)
- thewtex (2)
- giovp (2)
- dgkuester (1)
- Alexsaphir (1)
- charlesbmi (1)
- JamieMcMillan (1)
- melonora (1)
- freol35241 (1)
- rtbs-dev (1)
- rahulbhatia (1)
- shashwatsahay (1)
Pull Request Authors
- astropenguin (62)
- lolpack (2)
- SohumB (1)
- dependabot[bot] (1)
- thewtex (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- pypi 36,027 last-month
- Total dependent packages: 3
- Total dependent repositories: 8
- Total versions: 28
- Total maintainers: 1
pypi.org: xarray-dataclasses
xarray data creation by data classes
- Homepage: https://github.com/astropenguin/xarray-dataclasses/
- Documentation: https://astropenguin.github.io/xarray-dataclasses/
- License: MIT
-
Latest release: 1.9.1
published about 1 year ago
Rankings
Maintainers (1)
Dependencies
- alabaster 0.7.12 develop
- appnope 0.1.3 develop
- asttokens 2.0.5 develop
- atomicwrites 1.4.0 develop
- attrs 21.4.0 develop
- babel 2.10.1 develop
- backcall 0.2.0 develop
- beautifulsoup4 4.11.1 develop
- black 22.3.0 develop
- certifi 2022.5.18.1 develop
- charset-normalizer 2.0.12 develop
- click 8.1.3 develop
- colorama 0.4.4 develop
- decorator 5.1.1 develop
- docutils 0.17.1 develop
- executing 0.8.3 develop
- idna 3.3 develop
- imagesize 1.3.0 develop
- iniconfig 1.1.1 develop
- ipython 7.34.0 develop
- ipython 8.4.0 develop
- jedi 0.18.1 develop
- jinja2 3.1.2 develop
- markdown-it-py 2.1.0 develop
- markupsafe 2.1.1 develop
- matplotlib-inline 0.1.3 develop
- mdit-py-plugins 0.3.0 develop
- mdurl 0.1.1 develop
- mypy-extensions 0.4.3 develop
- myst-parser 0.17.2 develop
- nodeenv 1.6.0 develop
- packaging 21.3 develop
- parso 0.8.3 develop
- pathspec 0.9.0 develop
- pexpect 4.8.0 develop
- pickleshare 0.7.5 develop
- platformdirs 2.5.2 develop
- pluggy 1.0.0 develop
- prompt-toolkit 3.0.29 develop
- ptyprocess 0.7.0 develop
- pure-eval 0.2.2 develop
- py 1.11.0 develop
- pydata-sphinx-theme 0.8.1 develop
- pygments 2.12.0 develop
- pyparsing 3.0.9 develop
- pyright 1.1.252 develop
- pytest 7.1.2 develop
- pyyaml 6.0 develop
- requests 2.27.1 develop
- snowballstemmer 2.2.0 develop
- soupsieve 2.3.2.post1 develop
- sphinx 4.5.0 develop
- sphinxcontrib-applehelp 1.0.2 develop
- sphinxcontrib-devhelp 1.0.2 develop
- sphinxcontrib-htmlhelp 2.0.0 develop
- sphinxcontrib-jsmath 1.0.1 develop
- sphinxcontrib-qthelp 1.0.3 develop
- sphinxcontrib-serializinghtml 1.1.5 develop
- stack-data 0.2.0 develop
- tomli 2.0.1 develop
- traitlets 5.2.2.post1 develop
- typed-ast 1.5.4 develop
- urllib3 1.26.9 develop
- wcwidth 0.2.5 develop
- importlib-metadata 4.11.4
- morecopy 0.2.4
- numpy 1.22.4
- numpy 1.21.6
- pandas 1.4.2
- pandas 1.3.5
- python-dateutil 2.8.2
- pytz 2022.1
- six 1.16.0
- typing-extensions 3.10.0.2
- xarray 0.20.2
- xarray 2022.3.0
- zipp 3.8.0
- black ^22.3 develop
- ipython --- - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: "^7.32" python: ">=3.7.1, <3.8" - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: "^8.4" python: ">=3.8, <3.11" develop
- myst-parser ^0.17 develop
- pydata-sphinx-theme ^0.8 develop
- pyright ^1.1 develop
- pytest ^7.1 develop
- sphinx ^4.5 develop
- morecopy ^0.2
- numpy --- - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: ">=1.15, <1.22" python: ">=3.7.1, <3.8" - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: "^1.15" python: ">=3.8, <3.11"
- python >=3.7.1, <3.11
- typing-extensions ^3.10
- xarray --- - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: ">=0.18, <0.21" python: ">=3.7.1, <3.8" - !ruby/hash:ActiveSupport::HashWithIndifferentAccess version: ">=0.18, <2023" python: ">=3.8, <3.11"
- actions/checkout v2 composite
- actions/setup-python v2 composite
- peaceiris/actions-gh-pages v3 composite
- actions/checkout v2 composite
- actions/setup-python v2 composite
- actions/checkout v2 composite
- actions/setup-python v2 composite