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, np.float64, 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, ~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
toarr
while maintaining the type.The parameter
arr
should have afloat
dtype
.This function is tested against
xr.DataArray
,pd.Series
, andnp.ndarray
types.- Parameters:
arr (
ArrayLike
) – Array withnp.float64
entriesnan_mask (
numpy.ndarray
) – Boolean array of the same shape asarr
- Returns:
ArrayLike
– Arrayarr
with values innan_mask
set tonp.nan
. Thearr
is mutated in place if it is anp.ndarray
. Forxr.DataArray
, a copy is returned.
Notes
When
arr
is axr.DataArray
, this function keeps anyattrs
fromarr
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 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
obj
is nottype_