API reference
earthcarekit.utils.np_array_utils
earthcarekit.utils.np_array_utils
Utilities based on numpy.
all_same
Check if all elements in the input array are the same.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Input array or array-like object to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all elements in the array are the same, False otherwise. |
Source code in earthcarekit/utils/np_array_utils/_check.py
bins_to_centers
Converts bin edges to bin centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
ArrayLike
|
Array of N+1 bin edges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Array of N bin centers. |
Source code in earthcarekit/utils/np_array_utils/_bin.py
centers_to_bins
Estimates bin edges from bin centers, assuming edges lie halfway between centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
ArrayLike
|
Array of N bin centers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Array of N+1 bin edges. |
Source code in earthcarekit/utils/np_array_utils/_bin.py
circular_nanmean
Compute the circular mean of angles in degrees, ignoring NaNs.
Source code in earthcarekit/utils/np_array_utils/_circular.py
clamp
Limits given values to a range between a minimum and maximum value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Input array or array-like object to be clamped. |
required |
min
|
float
|
Minimum limit. |
required |
max
|
float
|
Maximum limit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Clampled array. |
Source code in earthcarekit/utils/np_array_utils/_clamp.py
coarsen_mean
Downsamples a array by averaging every n adjacient elements together, discarding residual elements at the end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Input array or array-like object to downsample. |
required |
n
|
int
|
Number of elements to be averaged together. |
required |
axis
|
int
|
The axis along which the array |
0
|
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: The downsampled array. |
Source code in earthcarekit/utils/np_array_utils/_coarsen_mean.py
flatten_array
Flatten a nested sequence of array-likes into a 1D numpy.array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence
|
ArrayLike
|
Sequence of array-like objects (may contain lists, tuples, arrays, or non-iterable elements). |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: Flattened 1D array. |
Source code in earthcarekit/utils/np_array_utils/_flatten.py
get_number_range
get_number_range(
start: float, end: float, freq: float | None = None, periods: int | None = None
) -> NDArray[floating | integer]
Generates a sequence of numbers based on frequency or number of periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
float
|
A number defining the frequency of sampled values in the sequence. |
None
|
periods
|
int
|
A number of defining the number of evenly spaced values to sample. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
number_range |
ndarray[floating | integer]
|
A sequence of numbers, either sampled by frequency or evenly spaced n times. |
Source code in earthcarekit/utils/np_array_utils/_misc.py
isascending
Check whether a sequence is initially ascending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lats
|
ArrayLike
|
Input sequence (e.g., |
required |
raise_error
|
bool
|
If True, raises ValueError if the sequence is too short (< 2). Defaults to False. |
False
|
result_constant
|
bool
|
If True, a constant sequence counts as acending. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
is_ascending |
bool
|
True if the sequence is ascending, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If given |
Source code in earthcarekit/utils/np_array_utils/_check.py
ismonotonic
ismonotonic(
a: ArrayLike,
strict: bool = False,
mode: Literal["any", "increasing", "decreasing"] = "any",
raise_error: bool = False,
ignore_nans: bool = True,
)
Check whether a sequence is monotonic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Input sequence (e.g., |
required |
strict
|
bool
|
If True, checks for strictly increasing or decreasing sequences. If False, allows equal adjacent elements. Defaults to False. |
False
|
mode
|
Literal['any', 'increasing', 'decreasing']
|
Direction of monotonicity to check. Defaults to 'any'. - 'any': Checks if the sequence is either increasing or decreasing, depending on the initial difference of the first two elements. - 'increasing': Checks only for increasing order. - 'decreasing': Checks only for decreasing order. |
'any'
|
raise_error
|
bool
|
If True, raises ValueError if the sequence is not monotonic. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
is_monotonic |
bool
|
True if the sequence is monotonic according to the specified parameters, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If given |
Source code in earthcarekit/utils/np_array_utils/_check.py
isndarray
Returns True if input has type numpy.ndarray and also checks if dtype is lower/equal
in type hierarchy if given (i.e. returns True if input.dtype is subtype of dtype).
Source code in earthcarekit/utils/np_array_utils/_check.py
lookup_value_by_number
Returns the value corresponding to the number closest to a given number, using interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
float
|
A single number to look up. |
required |
numbers
|
NDArray
|
A series of of monotonically increasing numbers. |
required |
values
|
NDArray[Any]
|
A series of values corresponding to each number in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
v |
Any
|
The value from |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in earthcarekit/utils/np_array_utils/_misc.py
normalize
Normalizes a list or array of numbers to a specified range [vmin, vmax], preserving NaNs.
The input is linearly scaled such that the minimum non-NaN value maps to vmin
and the maximum to vmax. NaN values are preserved in their original positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ArrayLike
|
A sequence of numeric values, possibly containing NaNs. |
required |
vmin
|
float
|
The minimum value of the normalized output range. Defaults to 0. |
0
|
vmax
|
float
|
The maximum value of the normalized output range. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
NDArray
|
A |
NDArray
|
and NaNs preserved. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in earthcarekit/utils/np_array_utils/_normalize.py
pad_true_sequence
Pads all sequences of True values occuring in an array with n True values before and after the sequence (while keeping the original size).
Source code in earthcarekit/utils/np_array_utils/_true_sequence.py
pad_true_sequence_2d
Pads all sequences of True values occuring along one axis in an 2d array with n True values before and after the sequence (while keeping the original size).
Source code in earthcarekit/utils/np_array_utils/_true_sequence.py
rebin_lerp
rebin_lerp(
v: ArrayLike,
axis0_coords: ArrayLike,
rebin_index: ArrayLike | None = None,
bin_edges: ArrayLike | None = None,
bin_centers: ArrayLike | None = None,
) -> NDArray
Rebin 1D or 2D arrays along the first axis (0) by linearly interpolating the two samples around a bin center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
ArrayLike
|
1D or 2D array to be rebinned. |
required |
axis0_coords
|
ArrayLike
|
Array of reference monotonic values. |
required |
rebin_index
|
ArrayLike | None
|
Array of non-decreasing indecies mapping values in |
None
|
bin_edges
|
ArrayLike | None
|
Array of N+1 bin edges. Defaults to None. |
None
|
bin_centers
|
ArrayLike | None
|
Array of N bin centers. Defaults to None. |
None
|
ignore_nans
|
bool
|
If True, NaNs are ignored during interpolation. If False, bins containing NaN return NaN. Defaults to True. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Along axis 0 rebinned version of original array |
Source code in earthcarekit/utils/np_array_utils/_rebin_lerp.py
rebin_mean
rebin_mean(
v: ArrayLike,
rebin_index: ArrayLike | None = None,
axis0_coords: ArrayLike | None = None,
bin_edges: ArrayLike | None = None,
bin_centers: ArrayLike | None = None,
ignore_nans: bool = True,
) -> NDArray
Rebin 1D or 2D arrays along the first axis (0) by averaging all samples that fall within a bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
ArrayLike
|
1D or 2D array to be rebinned. |
required |
rebin_index
|
ArrayLike | None
|
Array of non-decreasing indecies mapping values in |
None
|
axis0_coords
|
ArrayLike | None
|
Array of reference monotonic values used to derive |
None
|
bin_edges
|
ArrayLike | None
|
Array of N+1 bin edges. Ignored if |
None
|
bin_centers
|
ArrayLike | None
|
Array of N bin centers. Ignored if |
None
|
ignore_nans
|
bool
|
If True, NaNs are ignored during averaging. If False, bins containing NaN return NaN. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Along axis 0 rebinned version of original array |
Source code in earthcarekit/utils/np_array_utils/_rebin_mean.py
rebin_median
rebin_median(
v: ArrayLike,
rebin_index: ArrayLike | None = None,
axis0_coords: ArrayLike | None = None,
bin_edges: ArrayLike | None = None,
bin_centers: ArrayLike | None = None,
ignore_nans: bool = True,
) -> NDArray
Rebin 1D or 2D arrays along the first axis (0) by finding the median out of samples falling within a bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
ArrayLike
|
1D or 2D array to be rebinned. |
required |
rebin_index
|
ArrayLike | None
|
Array of non-decreasing indecies mapping values in |
None
|
axis0_coords
|
ArrayLike | None
|
Array of reference monotonic values used to derive |
None
|
bin_edges
|
ArrayLike | None
|
Array of N+1 bin edges. Ignored if |
None
|
bin_centers
|
ArrayLike | None
|
Array of N bin centers. Ignored if |
None
|
ignore_nans
|
bool
|
If True, NaNs are ignored during median search. If False, bins containing NaN return NaN. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Along axis 0 rebinned version of original array |
Source code in earthcarekit/utils/np_array_utils/_rebin_median.py
shift_true_sequence
Offsets all sequences of True values occuring in an array (while keeping the original size).
Source code in earthcarekit/utils/np_array_utils/_true_sequence.py
wrap_to_interval
Wrap values in a to the interval [min, max).