rmellipse.arrschema

Exceptions

ValidationError

Common base class for all non-exit exceptions.

Classes

AnnotatedArray

Extension of xr.DataArray that is expected to conform to a specific schema.

ArraySchema

Specialized dict subclass to describe the shape an array.

CoordinateSchema

dict() -> new empty dictionary

Package Contents

class rmellipse.arrschema.AnnotatedArray(*args, **kwargs)

Bases: xarray.DataArray

Extension of xr.DataArray that is expected to conform to a specific schema.

Schema is defined by an ArraySchema class.

schema: ArraySchema
validate()

Validate array data against the schema of this type.

classmethod from_dataarray(array: xarray.DataArray) Self

Cast an array into an annotated array.

Parameters:

array (xr.DataArray) – Array that can be cast into this format.

Returns:

New array.

Return type:

Self

classmethod zeros(attrs: Mapping | None = None, **coords: numpy.ndarray | xarray.DataArray) AnnotatedArray

Generate an empty array of zeros based on the schema.

If extra coordinates are supplied they will be inserted at the first arbitrary dimension specificier in the AnnotatedArray’s schema (’…’). Coordinates with set values can be ignored, and will be automatically inserted.

Zero array is initialized with numpy.zeros.

Parameters:
  • attrs (Mapping | None = None) – Provided metadata to instantiate the AnnotatedArray with. The values in the supplied metadata are shallow copied onto the instantiated AnnotatedArrays’s attrs.

  • **coords (np.ndarray | xr.DataArray) – KeyValue pairs of coordinates. Must include the required coordinates of the AnnotatedArray.

Returns:

Array with supplied coordinates that conforms to the schema.

Return type:

AnnotatedArray

Raises:

KeyError – DESCRIPTION.

classmethod zeros_from(prototype: AnnotatedArray, drop_dims: list[str] | None = None, rename_dims: Mapping | None = None, use_coords: Mapping | None = None, reorder: bool = True, validate: bool = True, attrs: Mapping | None = None, **coords) AnnotatedArray

Generate a new zeros array based on a prototype array.

Dimensions that are mapped from the prototype array to the output array are cast into the correct type. Otherwise, dimensions are inserted in the expected place.

Parameters:
  • prototype (AnnotatedArray) – Array to base the new array off of.

  • drop_dims (list[str], optional) – Drop these dimensions. The default is None.

  • rename_dims (Mapping | dict, optional) – Mapping of dimensions on the prototype array that should be converted to dimensions of this type of array. The default is None.

  • use_coords (Mapping | dict, optional) – Additional dimensions required for the new type, key is the dimension name and value is the new coordinate to use for that dimension.

  • reorder (bool, optional) – Automatically try to reorder dimensions to conform to the specification.

  • validate (bool, optional) – If true, validate after creation. Default is False

  • attrs (Mapping | dict, optional) – If provided, supply metadata to be used as attributes.

  • **coords (Mapping | None, optional) – Keyword version of use_coords. Is merged with ontop of use_coords.

Returns:

zeros – Zeros array in the new format.

Return type:

AnnotatedArray

class rmellipse.arrschema.ArraySchema(shape: tuple[str | int | types.EllipsisType, Ellipsis], dims: tuple[str | types.EllipsisType, Ellipsis], dtype: type | None = None, units: str | None = None, coords: Mapping = {}, attrs: pydantic.BaseModel = None)

Bases: dict

Specialized dict subclass to describe the shape an array.

Initialize an ArraySchema.

Parameters:
  • shape (tuple[str | int | EllipsisType, ...]) – Shape of structure. Ellipses indicate arbitrary dimensions, letters indicate a required dimension of unknown length, and integers indicate a required dimension of a required length.

  • dims (tuple[str | EllipsisType, ...]) – Names assigned to dimensions specified by shape. Any required dimension must be names, and arbitrary dimensions must also be ellipses.

  • dtype (type | None) – Type must be parseable by numpy’s dtype (e.g. f8, c8, u8, etc). None means no datatype requriement, can be Any.

  • units (Mapping, optional) – Mapping of units to the array structure.

  • coords (Mapping, optional) – Mapping of required dimensions to a coordinate space. Must provide at least a dtype and a single unit as a string. Optionally, if the coordinates are fixed (i.e. the row and column indices of stacks of 2-d matrices) then you may specify those coordinates here.

  • attrs_schema (mapping, optional) – JSON Schema for validating metadata attributes.

Returns:

Dictionary conforming to an arrschema specification.

Return type:

dict

Raises:

Exception – If some logical inconsistency or is found, or the provided schema doesn’t follow the specification for an array schema.

validate(arr: AnnotatedArray, attach_schema: bool = True)

Test if array conforms to schema.

Parameters:
  • arr (AnnotatedArray) – Array to check.

  • attach_schema (bool, optional) – If True, the schema is dumped into a string and attatched to the attrs of the input data array. The default is True.

Raises:

ValidationError – If a discrepancy is found between the data and the schema.

Return type:

None.

class rmellipse.arrschema.CoordinateSchema(values: list | None = None, dtype: type | None = None, units: str | None = None)

Bases: dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

Initialize self. See help(type(self)) for accurate signature.

exception rmellipse.arrschema.ValidationError(*args, **kwargs)

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.