pycontrails.utils.types

Convienence types.

Module Attributes

ArrayLike

Array like (np.ndarray, xr.DataArray)

ArrayOrFloat

Array or Float (np.ndarray, float)

ArrayScalarLike

Array like input (np.ndarray, xr.DataArray, np.float64, float)

DatetimeLike

Datetime like input (datetime, pd.Timestamp, np.datetime64)

Functions

apply_nan_mask_to_arraylike(arr, nan_mask)

Apply nan_mask to arr while maintaining the type.

support_arraylike(func)

Extend a numpy universal function operating on arrays of floats.

type_guard(obj, type_[, error_message])

Shortcut utility to type guard a variable with custom error message.

class pycontrails.utils.types.ArrayLike

Array like (np.ndarray, xr.DataArray)

alias of TypeVar(‘ArrayLike’, ~numpy.ndarray, ~xarray.core.dataarray.DataArray, ~xarray.core.dataarray.DataArray | ~numpy.ndarray)

class pycontrails.utils.types.ArrayOrFloat

Array or Float (np.ndarray, float)

alias of TypeVar(‘ArrayOrFloat’, ~numpy.ndarray[~typing.Any, ~numpy.dtype[~numpy.float64]], float, float | ~numpy.ndarray[~typing.Any, ~numpy.dtype[~numpy.float64]])

class pycontrails.utils.types.ArrayScalarLike

Array like input (np.ndarray, xr.DataArray, np.float64, float)

alias of TypeVar(‘ArrayScalarLike’, ~numpy.ndarray, ~xarray.core.dataarray.DataArray, ~numpy.float64, float, ~numpy.ndarray | float, ~xarray.core.dataarray.DataArray | ~numpy.ndarray)

class pycontrails.utils.types.DatetimeLike

Datetime like input (datetime, pd.Timestamp, np.datetime64)

alias of TypeVar(‘DatetimeLike’, ~datetime.datetime, ~pandas._libs.tslibs.timestamps.Timestamp, ~numpy.datetime64, str)

pycontrails.utils.types.apply_nan_mask_to_arraylike(arr, nan_mask)

Apply nan_mask to arr while maintaining the type.

The parameter arr should have a float dtype.

This function is tested against xr.DataArray, pd.Series, and np.ndarray types.

Parameters:
  • arr (ArrayLike) – Array with np.float64 entries

  • nan_mask (numpy.ndarray) – Boolean array of the same shape as arr

Returns:

ArrayLike – Array arr with values in nan_mask set to np.nan. The arr is mutated in place if it is a np.ndarray. For xr.DataArray, a copy is returned.

Notes

When arr is a xr.DataArray, this function keeps any attrs from arr in the returned instance.

pycontrails.utils.types.support_arraylike(func)

Extend a numpy universal function operating on arrays of floats.

This decorator allows func to support any ArrayScalarLike parameter and keeps the return type consistent with the parameter.

Parameters:

func (Callable[[ArrayScalarLike], np.ndarray]) – A numpy ufunc taking in a single array with float-like dtype. This decorator assumes func returns a numpy array.

Returns:

Callable[[ArrayScalarLike], ArrayScalarLike] – Extended function.

See also

-

pycontrails.utils.types.type_guard(obj, type_, error_message=None)

Shortcut utility to type guard a variable with custom error message.

Parameters:
  • obj (Any) – Any variable object

  • type_ (Type[_Object]) – Type of variable. Can be a tuple of types

  • error_message (str, optional) – Custom error message

Returns:

_Object – Returns the input object ensured to be type_

Raises:

ValueError – Raises ValueError if obj is not type_