erlab.utils.array¶
Utility functions for working with numpy and xarray.
Functions
|
Apply a function to a DataArray, Dataset, or DataTree. |
|
Decorate a function to broadcast all DataArray arguments. |
|
Decorate a function to check if the first argument is a 2D DataArray. |
|
Decorate a function to check if the first argument has no NaNs. |
|
Decorate a function to check if all dims in the first argument are uniform. |
|
Calculate the effective number of decimal places for a given step size. |
|
Ensure all data has the same set of coordinate names. |
|
Check if the given dimensions of a DataArray have uniform spacing. |
|
Check if an array is monotonic. |
|
Check if the given array is uniformly spaced. |
|
Return (min, max) for DataArrays, with efficient handling for dask. |
|
Sort the coordinates of a DataArray in the given order. |
|
Convert an array to native endianness. |
|
Drop all-NaN coordinates from the edges of a DataArray. |
|
Return the set of dimensions that are uniformly spaced. |
|
Compute digits needed to represent floating point values uniquely. |
- erlab.utils.array.apply_dataarray_func(data, func, **kwargs)[source]¶
Apply a function to a DataArray, Dataset, or DataTree.
- erlab.utils.array.broadcast_args(func)[source]¶
Decorate a function to broadcast all DataArray arguments.
This decorator automatically broadcasts all DataArray args and kwargs to the same shape, and only passes pure NumPy arrays to the decorated function.
If the decorated function returns a NumPy array with the same shape as the broadcasted DataArray arguments, a new DataArray will be created with the same coordinates and dimensions as the broadcasted DataArray. In this case, the attributes will be taken from the first DataArray that appears in the arguments.
This is useful when working with functions that only accept pure NumPy arrays, or always returns a NumPy array, such as numba jit-compiled functions.
Note
When used on numba functions in nopython mode, the decorated function will no longer be able to be called from another function compiled in nopython mode.
The decorated function will not be able to accept DataArray arguments.
Dask-backed DataArray arguments are applied lazily so that the decorated function still receives NumPy arrays.
- erlab.utils.array.check_arg_2d_darr(func)[source]¶
Decorate a function to check if the first argument is a 2D DataArray.
- erlab.utils.array.check_arg_has_no_nans(func)[source]¶
Decorate a function to check if the first argument has no NaNs.
- erlab.utils.array.check_arg_uniform_dims(func)[source]¶
Decorate a function to check if all dims in the first argument are uniform.
- erlab.utils.array.effective_decimals(step_or_coord)[source]¶
Calculate the effective number of decimal places for a given step size.
This function determines the number of decimal places required to approximately represent a value in a linearly spaced array, given its step size. We assume that rounding to a decimal an order of magnitude smaller than the step size to be a good approximation.
- Parameters:
step – The step size for which to calculate the effective number of decimal places, or a coordinate array in which case the step size is calculated as the difference of the first two elements.
- Returns:
int– The effective number of decimal places, calculated as the order of magnitude ofstepplus one.If the step size is zero, a default value of 3 is returned.
- Return type:
- erlab.utils.array.ensure_same_coord_names(data_list)[source]¶
Ensure all data has the same set of coordinate names.
This function modifies the provided list in place by adding missing coordinates with NaN values to each DataArray, Dataset, or DataTree in the list.
All inputs must be of the same type: either all DataArrays, all Datasets, or all DataTrees.
This function is used by the data loading utilities to ensure that all files in a single load operation have the same set of coordinate names. This is required because some endstations produce files with missing header entries, possibly due to a bug in the data acquisition software.
- erlab.utils.array.is_dims_uniform(darr, dims=None, **kwargs)[source]¶
Check if the given dimensions of a DataArray have uniform spacing.
- erlab.utils.array.is_monotonic(arr, strict=False)[source]¶
Check if an array is monotonic.
- Parameters:
arr (
array-like) – The input array.strict (
bool, optional) – IfTrue, the array must be strictly monotonic, i.e., either strictly increasing or strictly decreasing. IfFalse, the array can be non-decreasing or non-increasing.
- Returns:
- Return type:
- erlab.utils.array.is_uniform_spaced(arr, rtol=1.0e-5, atol=1.0e-8)[source]¶
Check if the given array is uniformly spaced.
Constant arrays are also considered as uniformly spaced.
- Parameters:
arr (
array-like) – The input array.rtol (
float, optional) – Relative tolerance passed tonumpy.isclose().atol (
float, optional) – Absolute tolerance passed tonumpy.isclose().
- Returns:
bool–Trueif the array is uniformly spaced and one-dimensional,Falseotherwise.- Return type:
Examples
>>> is_uniform_spaced([1, 2, 3, 4]) True >>> is_uniform_spaced([1, 2, 3, 5]) False
- erlab.utils.array.minmax_darr(darr, *, skipna=True)[source]¶
Return (min, max) for DataArrays, with efficient handling for dask.
- erlab.utils.array.sort_coord_order(darr, keys=None, *, dims_first=True)[source]¶
Sort the coordinates of a DataArray in the given order.
By default, DataArray represents the coordinates in the order they are given in the constructor. The order may become mixed up after performing operations; This function sorts them so that they are more easily readable.
This has been raised as an issue in xarray, but it seems like it will not be implemented in the near future.
- Parameters:
darr (
DataArray) – The DataArray to sort.keys (
Iterable[Hashable] |None, default:None) – The order in which to sort the coordinates. If not provided, the coordinates will retain their original order. Ifkeysis not provided anddims_firstis False, this function will return the DataArray as is.dims_first (
bool, default:True) – IfTrue, the dimensions will come first in the sorted DataArray. The order of the dimensions will not respect the order given inkeys, but will be sorted in the order they appear in the DataArray. IfFalse, everything will be sorted in the order given inkeys.
- Returns:
darr– The sorted DataArray.- Return type:
- erlab.utils.array.to_native_endian(arr)[source]¶
Convert an array to native endianness.
Some Igor Pro files may contain data in big-endian format, which may be incompatible with numba functions. This function converts the array to native endianness.
- Parameters:
arr (
array-like) – The input array.- Returns:
array– The array in native endianness.- Return type:
- erlab.utils.array.trim_na(darr, dims=None)[source]¶
Drop all-NaN coordinates from the edges of a DataArray.
- erlab.utils.array.uniform_dims(darr, **kwargs)[source]¶
Return the set of dimensions that are uniformly spaced.
- Parameters:
darr (
DataArray) – The input xarray DataArray.**kwargs – Additional keyword arguments passed to
is_uniform_spaced.
- Returns:
dims– A set of dimensions that are uniformly spaced.- Return type: