API reference#
earthcarekit.utils.numpy
Utilities based on numpy.
Notes#
This module does not depend on other internal modules.
all_in
#
Check if all elements in subset are present in set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
ArrayLike
|
The list to check. |
required |
set
|
ArrayLike
|
The list to check against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all elements of |
Source code in earthcarekit/utils/numpy/_check.py
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/numpy/_check.py
asarray_or_none
#
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/numpy/_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/numpy/_bin.py
circular_nanmean
#
Compute the circular mean of angles in degrees, ignoring NaNs.
Source code in earthcarekit/utils/numpy/_circular.py
clamp
#
Limits given values to a range between a minimum and maximum value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Scalar | ArrayLike
|
Input array or array-like object to be clamped. |
required |
min
|
float
|
Minimum limit. |
required |
max
|
float
|
Maximum limit. |
required |
Returns:
| Type | Description |
|---|---|
|
Scalar | NDArray: Clampled scalar or array. |
Examples:
>>> import numpy as np
>>> import earthcarekit.utils.numpy as np_utils
>>> x1 = np_utils.clamp(1.2, 0, 1)
>>> print(x1)
1.0
>>> x2 = np_utils.clamp([1.0, 1.2, 0.8, -0.1], 0, 1)
>>> print(x2)
[1. 1. 0.8 0. ]
>>> x3 = np_utils.clamp(
... np.datetime64("2025-02-23"),
... np.datetime64("2025-01-01"),
... np.datetime64("2025-01-02"),
... )
>>> print(x3)
2025-01-02
>>> x4 = np_utils.clamp(
... np.timedelta64(70, "m"), np.timedelta64(0, "h"), np.timedelta64(1, "h")
... )
>>> print(x4)
60 minutes
Source code in earthcarekit/utils/numpy/_clamp.py
coarsen_mean
#
Downsample an 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/numpy/_coarsen_mean.py
downsample
#
Downsample an array by selecting evenly spaced samples along one axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Input array or array-like object to downsample. |
required |
n
|
int
|
Number of samples to select. |
required |
axis
|
int
|
The axis along which the array |
0
|
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: The downsampled array. |
Source code in earthcarekit/utils/numpy/_downsample.py
flatten_array
#
Flatten a nested sequence of array-likes into a 1D numpy.array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
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/numpy/_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/numpy/_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/numpy/_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/numpy/_check.py
isndarray
#
Returns True if a has type numpy.ndarray and also checks if dtype is lower/equal
in type hierarchy if given (i.e. returns True if a.dtype is subtype of dtype).
Source code in earthcarekit/utils/numpy/_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/numpy/_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 |
|---|---|---|---|
a
|
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/numpy/_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/numpy/_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/numpy/_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
|
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
Along axis 0 rebinned version of original array |
Source code in earthcarekit/utils/numpy/_rebin/_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/numpy/_rebin/_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/numpy/_rebin/_rebin_median.py
rescale
#
Linearly map values from one range to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Scalar1 | ArrayLike
|
Scalar or array-like values to be rescaled. |
required |
src_min
|
Scalar1
|
Lower bound of the source range. |
required |
src_max
|
Scalar1
|
Upper bound of the source range. |
required |
dst_min
|
Scalar2
|
Lower bound of the destination range. |
required |
dst_max
|
Scalar2
|
Upper bound of the destination range. |
required |
Returns:
| Type | Description |
|---|---|
|
Scalar2 | NDArray: Rescaled scalar or array. The retrun type matches the type of the destination range. |
Examples:
>>> import numpy as np
>>> import earthcarekit.utils.numpy as np_utils
>>> x1 = np_utils.rescale(3, 0, 10, 0, 200)
>>> print(x1)
60.0
>>> x2 = np_utils.rescale([3], 0, 10, 0, 200)
>>> print(x2)
[60.]
>>> x3 = np_utils.rescale(
... [0, 0.5, 1],
... 0,
... 1,
... np.datetime64("2025-01-01 00:00:00"),
... np.datetime64("2025-01-02 00:00:00"),
... )
>>> print(x3)
['2025-01-01T00:00:00' '2025-01-01T12:00:00' '2025-01-02T00:00:00']
>>> x4 = np_utils.rescale(
... np.timedelta64(15, "m"),
... np.timedelta64(0, "h"),
... np.timedelta64(1, "h"),
... 0,
... 1,
... )
>>> print(x4)
0.25
Source code in earthcarekit/utils/numpy/_rescale.py
shift_true_sequence
#
Offsets all sequences of True values occuring in an array (while keeping the original size).
Source code in earthcarekit/utils/numpy/_true_sequence.py
wrap_to_interval
#
Wrap values in a to the interval [min, max).