Module for DataArray accessor classes.
xarray_custom.accessor.
add_accessors
Add unique and common accessors to a DataArray class.
cls – DataArray class to which accessors are added.
name – Name of a common accessor. If not specified, only an unique accessor is added to the class.
The same DataArray class as the input.
Module for providing the base class of DataArray classes.
xarray_custom.bases.
DataArrayClassBase
Bases: object
object
Create a custom DataArray from data and coordinates.
(dims=None, dtype=None) No description.
data – Values of the DataArray. Its shape must match class dims. If class dtype is defined, it will be casted to that type. If it cannot be casted, a ValueError will be raised.
dims
dtype
ValueError
name – Name of the DataArray. Default is class name.
name
attrs – Attributes of the DataArray. Default is class attrs.
attrs
**coords – Coordinates of the DataArray defined by the class.
Custom DataArray.
accessor
desc
empty
Create a custom DataArray filled with uninitialized values.
shape – Shape of the DataArray. It must match class dims.
dtype – Datatype of the DataArray. Default is 64-bit float. It is ignored if class dtype is defined.
order – Order of data in memory. Either 'C' (row-major; C-style) or 'F' (column-major; Fortran-style) is accepted.
'C'
'F'
Custom DataArray filled with uninitialized values.
full
Create a custom DataArray filled with fill_value.
fill_value
fill_value – Scalar value to fill the custom DataArray.
Custom DataArray filled with fill_value.
ones
Create a custom DataArray filled with ones.
Custom DataArray filled with ones.
zeros
Create a custom DataArray filled with zeros.
Custom DataArray filled with zeros.
Module for creating custom DataArray classes.
This module provides functions which help to create a custom DataArray class with fixed dimensions, datatype, and coordinates. Two functions are available:
dataarrayclass: Class decorator to construct a custom DataArray class.
dataarrayclass
coord: Create a DataArray class for the definition of a coordinate.
coord
Examples
To create a custom DataArray class to represent images:
@dataarrayclass class Image: """DataArray class to represent images.""" accessor = 'img' dims = 'x', 'y' dtype = float x: coord('x', int) = 0 y: coord('y', int) = 0 def normalize(self): return self / self.max()
The code style is similar to that of Python’s dataclass. A DataArray is then created using the class:
image = Image([[0, 1], [2, 3]], x=[0, 1], y=[0, 1]) print(image) # <xarray.DataArray (x: 2, y: 2)> # array([[0., 1.], # [2., 3.]]) # Coordinates: # * x (x) int64 0 1 # * y (y) int64 0 1
Because dims, dtype, and coordinates are pre-defined, it is much easier to create a DataArray with given data. Custom methods can be used via an accessor:
normalized = image.img.normalize() print(normalized) # <xarray.DataArray (x: 2, y: 2)> # array([[0. , 0.33333333], # [0.66666667, 1. ]]) # Coordinates: # * x (x) int64 0 1 # * y (y) int64 0 1
Like NumPy, several special class methods are available to create a DataArray filled with some values:
ones = Image.ones((2, 2)) print(ones) # <xarray.DataArray (x: 2, y: 2)> # array([[1., 1.], # [1., 1.]]) # Coordinates: # * x (x) int64 0 0 # * y (y) int64 0 0
Inheriting a custom DataArray class is possible to create a derivative DataArray class:
class WeightedImage(Image): accessor = 'wimg' w: coord(('x', 'y'), float) = 1.0 zeros = Weightedimage.zeros((2, 2)) print(zeros) # <xarray.DataArray (x: 2, y: 2)> # array([[1., 1.], # [1., 1.]]) # Coordinates: # * x (x) int64 0 0 # * y (y) int64 0 0 # w (x, y) float64 1.0 1.0 1.0 1.0
xarray_custom.dataclasses.
Create a DataArray class for the definition of a coordinate.
dims – Dimensions of the coordinate.
dtype – Datatype of the coordinate. Default is None, which means that an input of any datatype is accepted.
None
desc – Short description of the coordinate.
DataArray class for the coordinate.
Class decorator to construct a custom DataArray class.
A class can define properties of DataArray to be created. The following class variables are accepted (see examples).
dims (str or tuple of str): Name(s) of dimension(s).
dtype (str or type): Datatype of DataArray.
alternatively define it by a docstring (__doc__).
__doc__
accessor (str): Name of accessor (if necessary).
The coordinates of DataArray can also be defined as class variables using the coord function (see examples).
Finally users can define custom instance methods which can be used by an accessor whose name is defined by accessor.
cls – Class to be decorated.
Decorated class as a DataArray class.
Module for making docstrings of functions updatable.
xarray_custom.docstring.
updatable_doc
Decorator for making the docstring of a function updatable.
A decorated function has the set method, by which an update function for the docstring is registered to the function.
set
func – A function whose docstring should be updatable.
Decorated function with the copy and set methods.
copy
Copy the decorated function and return a new one.
Set an update function to the decorated function.
Examples:
@updatable_doc def func(): """Docstring.""" pass # set an updater function func.set(lambda doc: doc.replace(".", "!")) # show the help of the decorated function help(func) # -> Docstring!
Module for special class methods of DataArray classes.
xarray_custom.special.
add_classmethods
Add special class methods to a DataArray class.
updater – Function to update docstrings of the class methods. Its input must be a docstring (str) and its output must be the updated docstring (str). If not specified (by default), any docstrings will not be updated.
str
Module for type hints of xarray DataArray.
This module provides type hints of xarray DataArray which are intended to be used other modules of the package.
xarray_custom.typing.
Attrs
The central part of internal API.
This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.
alias of Mapping
Dims
alias of Union[Sequence[Hashable], Hashable]
Dtype
alias of Union[type, str]
Name
alias of Hashable
Shape
alias of Union[Tuple[int], int]
Module for utilities which help to create custom DataArray classes.
Currently this module only provides include class decorator which can include a custom DataArray definition written in a file.
include
include: Class decorator to include a custom DataArray definition in a file.
xarray_custom.utils.
Class decorator to include a custom DataArray definition in a file.
File format of either JSON, TOML, or YAML is accepted. The following key=value pairs can be included if available.
key=value
dims=<array of string>: Dimensions of the DataArray.
dims=<array of string>
dtype=<string>: Datatype of the DataArray.
dtype=<string>
desc=<string>: Short description of the DataArray.
desc=<string>
coords=<map of coord>: Definition of coordinates (coords). Each coord is a map which can have the following key=value pairs.
coords=<map of coord>
dims=<array of string>: Dimensions of a coordinate.
dtype=<string>: Datatype of a coordinate.
desc=<string>: Short description of a coordinate.
default=<any>: Default value of a coordinate.
default=<any>
path – Path or filename of the file.
Decorator to include the definition.
If a definition is written in dataarray.toml:
dataarray.toml
# dataarray.toml dims = [ "x", "y" ] dtype = "float" desc = "DataArray class to represent images." [coords.x] dims = "x" dtype = "int" default = 0 [coords.y] dims = "y" dtype = "int" default = 0
then the following two class definitions are equivalent:
@include('dataarray.toml') @dataarrayclass class Image: pass
@dataarrayclass class Image: """DataArray class to represent images.""" dims = 'x', 'y' dtype = float x: coord('x', int) = 0 y: coord('y', int) = 0