kalepy.utils¶
kalepy’s internal, utility functions.
-
kalepy.utils.
add_cov
(data, cov)¶
-
kalepy.utils.
allclose
(xx, yy, msg=None, **kwargs)¶
-
kalepy.utils.
alltrue
(xx, msg=None)¶
-
kalepy.utils.
array_str
(data, num=3, format=':.2e')¶
-
kalepy.utils.
assert_true
(val, msg=None)¶
-
kalepy.utils.
bins
(*args, **kwargs)¶ Calculate np.linspace(*args) and return also centers and widths.
- Returns
xe ((N,) bin edges)
xc ((N-1,) bin centers)
dx ((N-1,) bin widths)
-
kalepy.utils.
bound_indices
(data, bounds, outside=False)¶ Find the indices of the data array that are bounded by the given bounds.
If outside is True, then indices for values outside of the bounds are returned.
-
kalepy.utils.
check_path
(fname)¶ Make sure the given path exists. Create directories as needed.
-
kalepy.utils.
cov_from_var_cor
(var, corr)¶
-
kalepy.utils.
cumsum
(vals, axis=None)¶ Perform a cumulative sum without flattening the input array.
See: https://stackoverflow.com/a/60647166/230468
- Parameters
vals (array_like of scalar) – Input values to sum over.
axis (None or int) – Axis over which to perform the cumulative sum.
- Returns
res – Same shape as input vals
- Return type
ndarray of scalar
-
kalepy.utils.
cumtrapz
(pdf, edges, prepend=True, axis=None)¶ Perform a cumulative integration using the trapezoid rule.
- Parameters
pdf (array_like of scalar) – Input values (e.g. a PDF) to be integrated.
edges ([D,] list of (array_like of scalar)) – Edges defining bins along each dimension. This should be an array/list of edges for each of D dimensions.
prepend (bool) – Whether or not to prepend zero values along the integrated dimensions.
axis (None or int) – Axis/Dimension over which to integrate.
- Returns
cdf – Values integrated over the desired axes. Shape: * If prepend is False, the shape of cdf will be one smaller than the input pdf * in all dimensions integrated over. * If prepend is True, the shape of cdf will match that of the input pdf.
- Return type
ndarray of scalar
-
kalepy.utils.
flatlen
(arr)¶
-
kalepy.utils.
flatten
(arr)¶ Flatten a ND array, whether jagged or not, into a 1D array.
-
kalepy.utils.
histogram
(data, bins=None, weights=None, density=False, probability=False)¶
-
kalepy.utils.
iqcenter
(data, weights=None, axis=None)¶
-
kalepy.utils.
iqrange
(data, log=False, weights=None)¶ Calculate inter-quartile range of the given data.
-
kalepy.utils.
isjagged
(arr)¶ Test if the given array is jagged.
-
kalepy.utils.
jshape
(arr, level=0, printout=False, prepend='', indent=' ')¶ Print the complete shape (even if jagged) of the given array.
-
kalepy.utils.
matrix_invert
(matrix, helper=True)¶
-
kalepy.utils.
meshgrid
(*args, indexing='ij', **kwargs)¶
-
kalepy.utils.
midpoints
(data, scale='lin', frac=0.5, axis=- 1, squeeze=True)¶ Return the midpoints between values in the given array.
-
kalepy.utils.
minmax
(data, positive=False, prev=None, stretch=None, log_stretch=None, limit=None)¶
-
kalepy.utils.
modify_exists
(path_fname)¶ Modify the given filename if it already exists.
-
kalepy.utils.
parse_edges
(data, edges=None, extrema=None, weights=None, params=None, nmin=5, nmax=1000, pad=None, refine=1.0)¶
-
kalepy.utils.
quantiles
(values, percs=None, sigmas=None, weights=None, axis=None, values_sorted=False)¶ Compute weighted quartiles.
Taken from zcode.math.statistics Based on @Alleo answer: http://stackoverflow.com/a/29677616/230468
- Parameters
values ((N,)) – input data
percs ((M,) scalar [0.0, 1.0]) – Desired percentiles of the data.
weights ((N,) or None) – Weighted for each input data point in values.
values_sorted (bool) – If True, then input values are assumed to already be sorted.
- Returns
percs – Array of percentiles of the weighted input data.
- Return type
(M,) float
-
kalepy.utils.
really1d
(arr)¶ Test whether an array_like is really 1D (i.e. not a jagged ND array).
Test whether the input array is uniformly one-dimensional, as apposed to (e.g.) a
ndim == 1
list or array of irregularly shaped sub-lists/sub-arrays. True for an empty list [].- Parameters
arr (array_like) – Array to be tested.
- Returns
Whether arr is purely 1D.
- Return type
bool
-
kalepy.utils.
rem_cov
(data, cov=None)¶
-
kalepy.utils.
run_if
(func, target, *args, otherwise=None, **kwargs)¶
-
kalepy.utils.
run_if_notebook
(func, *args, otherwise=None, **kwargs)¶
-
kalepy.utils.
run_if_script
(func, *args, otherwise=None, **kwargs)¶
-
kalepy.utils.
spacing
(data, scale='log', num=None, dex=10, **kwargs)¶
-
kalepy.utils.
stats
(data, shape=True, sample=3, stats=True)¶
-
kalepy.utils.
stats_str
(data, percs=[0.0, 0.16, 0.5, 0.84, 1.0], ave=False, std=False, weights=None, format=None, log=False, label_log=True)¶ Return a string with the statistics of the given array.
- Parameters
data (ndarray of scalar) – Input data from which to calculate statistics.
percs (array_like of scalars in {0, 100}) – Which percentiles to calculate.
ave (bool) – Include average value in output.
std (bool) – Include standard-deviation in output.
format (str) – Formatting for all numerical output, (e.g. “:.2f”).
log (bool) – Convert values to log10 before printing.
Output –
------ –
out (str) – Single-line string of the desired statistics.
-
kalepy.utils.
trapz_dens_to_mass
(pdf, edges, axis=None)¶ Convert from density to mass, for values on the corner of a grid, using the trapezoid rule.
- Parameters
pdf (array_like) – Density values, computed at the grid edges specified by the edges list-of-lists.
edges (array_like of array_like) – List of edge-locations along each dimension specifying the grid of values at which pdf are located. e.g. [[x0, x1, … xn], [y0, y1, … ym], …] The length of each sub-list in edges, must match the shape of pdf. e.g. if edges is a (3,) list, composed of sub-lists with lengths: [N, M, L,] then the shape of pdf must be (N, M, L,).
axis (int, array_like int, or None) – Along which axes to convert from density to mass.
- Returns
mass – The mass array has as many dimensions as pdf, with each dimension one element shorter. e.g. if the shape of pdf is (N, M, …), then the shape of mass is (N-1, M-1, …).
- Return type
array_like
-
kalepy.utils.
trapz_nd
(data, edges, axis=None)¶