pycontrails.utils.types¶
Convienence types.
Module Attributes
Array like (np.ndarray, xr.DataArray) |
|
Array or Float (np.ndarray, float) |
|
Array like input (np.ndarray, xr.DataArray, float) |
|
Datetime like input (datetime, pd.Timestamp, np.datetime64) |
Functions
|
Apply |
|
Extend a numpy universal function operating on arrays of floats. |
|
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)
- has_default()¶
- class pycontrails.utils.types.ArrayOrFloat¶
Array or Float (np.ndarray, float)
alias of TypeVar(‘ArrayOrFloat’, ~numpy.ndarray, float)
- has_default()¶
- class pycontrails.utils.types.ArrayScalarLike¶
Array like input (np.ndarray, xr.DataArray, float)
alias of TypeVar(‘ArrayScalarLike’, ~numpy.ndarray, ~xarray.core.dataarray.DataArray, float)
- has_default()¶
- 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)
- has_default()¶
- pycontrails.utils.types.apply_nan_mask_to_arraylike(arr, nan_mask)¶
Apply
nan_masktoarrwhile maintaining the type.The parameter
arrshould have afloatdtype.This function is tested against
xr.DataArray,pd.Series, andnp.ndarraytypes.- Parameters:
arr (
ArrayLike) – Array withnp.float64entriesnan_mask (
numpy.ndarray) – Boolean array of the same shape asarr
- Returns:
ArrayLike– Arrayarrwith values innan_maskset tonp.nan. Thearris mutated in place if it is anp.ndarray. Forxr.DataArray, a copy is returned.
Notes
When
arris axr.DataArray, this function keeps anyattrsfromarrin 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 objecttype_ (
Type[_Object]) – Type of variable. Can be a tuple of typeserror_message (
str, optional) – Custom error message
- Returns:
_Object– Returns the input object ensured to betype_- Raises:
ValueError – Raises ValueError if
objis nottype_