labcore.utils.num#
num.py
Tools for numerical operations.
Functions
|
reshape an array to a target shape. |
|
Check if two numpy arrays are equal, content-wise. |
|
Given an array of center coordinates, return the array of bounding vertices for the mesh that is defined by the coordinates. |
|
Given a 2d array of coordinates, return the array of bounding vertices for the mesh that is defined by the coordinates. |
|
Remove invalid rows and columns from 2d data. |
|
Remove rows/cols from a 2d array. |
|
Get row and col idxs that are completely invalid in a 2d array. |
|
Find the period with which the values in an array change direction. |
|
Try to determine order and shape of a set of axes data (such as flattened meshgrid data). |
|
Try to find missing vertices in a 2d meshgrid, where xx and yy are the x and y coordinates of each point. |
|
|
|
Get idxs where full rows/cols are invalid in any of the input arrays. |
|
Get the largest numerical type present in an array. :param arr: input array :param include_integers: whether to include int as possible return. if |
- labcore.utils.num.array1d_to_meshgrid(arr: List | ndarray, target_shape: Tuple[int, ...], copy: bool = True) ndarray[source]#
reshape an array to a target shape.
If target shape is larger than the array, fill with invalids (
nanfor float and complex dtypes,Noneotherwise). If target shape is smaller than the array, cut off the end.- Parameters:
arr – input array
target_shape – desired output shape
copy – whether to make a copy before the operation.
- Returns:
re-shaped array.
- labcore.utils.num.arrays_equal(a: ndarray, b: ndarray, rtol: float | None = None) bool[source]#
Check if two numpy arrays are equal, content-wise.
Perform the following checks: * actual equality of elements * approximate equality to a certain degree of relative accuracy (for floats) * invalid entries (capturing both
Noneandnp.nan; all treated asequally invalid).
Element-wise comparison is
Trueif any of the conditions areTrue.- Parameters:
a – 1st numpy array
b – 2nd numpy array
rtol – relative uncertainty tolerance. see numpy.isclose.
- Returns:
True, if all element-wise checks are True. False otherwise.
- Raises:
ValueError if shapes of a and b don’t match
- labcore.utils.num.centers2edges_1d(arr: ndarray) ndarray[source]#
Given an array of center coordinates, return the array of bounding vertices for the mesh that is defined by the coordinates. This is useful for methods like pcolor(mesh).
To illustrate: if x are the centers, we return the inferred o’s:
o-x-o-x-o-x-o-x–o–x–o
They are equidistantly spaced between the centers, such that the centers are in the middle between the vertices.
- labcore.utils.num.centers2edges_2d(centers: ndarray) ndarray[source]#
Given a 2d array of coordinates, return the array of bounding vertices for the mesh that is defined by the coordinates. This is useful for methods like pcolor(mesh). Done by very simple linear interpolation.
To illustrate: if x are the centers, we return the inferred o’s:
`` o o o o
x—x—x
- o | o | o | o
x—x—x
o o o o ``
- labcore.utils.num.crop2d(x: ndarray, y: ndarray, *arr: ndarray) Tuple[ndarray, ...][source]#
Remove invalid rows and columns from 2d data.
Determine invalid areas from the x and y coordinates, and then crop the invalid rows/columns from all input data.
- Parameters:
x – 1st dim coordinates (2d meshgrid-like)
y – 2nd dim coordinates (2d meshgrid-like)
arr – other arrays to crop.
- Returns:
all arrays (incl. x and y), cropped.
- labcore.utils.num.crop2d_from_xy(arr: ndarray, xs: ndarray, ys: ndarray) ndarray[source]#
Remove rows/cols from a 2d array.
- Parameters:
arr – input array.
xs – list of 1st-dimension indices to remove.
ys – list of 2nd-dimension indices to remove.
- Returns:
remaining array.
- Raises:
ValueErrorif input is not a 2d ndarray.
- labcore.utils.num.crop2d_rows_cols(arr: ndarray) Tuple[ndarray, ndarray][source]#
Get row and col idxs that are completely invalid in a 2d array.
- Parameters:
arr – input array
- Returns:
the x (outer) and y (inner) indices at which the data ontains only invalid entries.
- Raises:
ValueErrorif input is not a 2d ndarray.
- labcore.utils.num.find_direction_period(vals: ndarray, ignore_last: bool = False) float | None[source]#
Find the period with which the values in an array change direction.
- Parameters:
vals – the axes values (1d array)
ignore_last – if True, we’ll ignore the last value when determining if the period is unique (useful for incomplete data),
- Returns:
None if we could not determine a unique period. The period, i.e., the number of elements after which the more common direction is changed.
- labcore.utils.num.guess_grid_from_sweep_direction(**axes: ndarray) None | Tuple[List[str], Tuple[int, ...]][source]#
Try to determine order and shape of a set of axes data (such as flattened meshgrid data).
Analyzes the periodicity (in sweep direction) of the given set of axes values, and use that information to infer the shape of the dataset, and the order of the axes, given from slowest to fastest.
- Parameters:
axes – all axes values as keyword args, given as 1d numpy arrays.
- Returns:
None, if we cannot infer a shape that makes sense. Sorted list of axes names, and shape tuple for the dataset.
- Raises:
ValueError for incorrect input
- labcore.utils.num.interp_meshgrid_2d(xx: ndarray, yy: ndarray) Tuple[ndarray, ndarray][source]#
Try to find missing vertices in a 2d meshgrid, where xx and yy are the x and y coordinates of each point. This is just done by simple linear interpolation (using pandas).
i.e.: if xx = [[0, 0], [1, nan]], yy = [[0, 1], [0, nan]] this will return [[0, 0], [1, 1]], [[0, 1], [0, 1]].
- labcore.utils.num.joint_crop2d_rows_cols(*arr: ndarray) Tuple[ndarray, ndarray][source]#
Get idxs where full rows/cols are invalid in any of the input arrays. Uses
crop2d_rows_colsfor each, then joins indices.- Parameters:
arr – input 2d arrays.
- Returns:
x/y indices with invalid rows/cols.
- labcore.utils.num.largest_numtype(arr: ndarray, include_integers: bool = True) None | type[source]#
Get the largest numerical type present in an array. :param arr: input array :param include_integers: whether to include int as possible return.
if
False, return will be float, if there’s only integers in the the data.- Returns:
type if possible. None if no numeric data in array.