kalepy.utils module

kalepy’s internal, utility functions.

kalepy.utils.add_cov(data, cov)
kalepy.utils.array_str(data, num=3, format=':.2e')
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.centroids(edges, data)

Calculate the centroids (centers of mass) of each cell in the given grid.

Parameters:
edges(D,) array_like of array_like
data(…) ndarray of scalar
Returns:
comsndarray (D, …)
kalepy.utils.cov_keep_vars(matrix, keep, reflect=None)
kalepy.utils.cumsum(vals, axis=None)

Perform a cumulative sum without flattening the input array.

See: https://stackoverflow.com/a/60647166/230468

Parameters:
valsarray_like of scalar

Input values to sum over.

axisNone or int

Axis over which to perform the cumulative sum.

Returns:
resndarray of scalar

Same shape as input vals

kalepy.utils.cumtrapz(pdf, edges, prepend=True, axis=None)

Perform a cumulative integration using the trapezoid rule.

Parameters:
pdfarray_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.

prependbool

Whether or not to prepend zero values along the integrated dimensions.

axisNone or int

Axis/Dimension over which to integrate.

Returns:
cdfndarray of scalar

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.

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.iqrange(data, log=False, weights=None)

Calculate inter-quartile range of the given data.

kalepy.utils.isinteger(val, iterable=True)

Test whether the given variable is an integer (i.e. numbers.integral subclass).

Parameters:
valobject,

Variable to test.

iterablebool,

Allow argument to be an iterable.

Returns:
boolwhether or not the input is an integer, or integer iterable
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(arr, log=False, axis=-1, squeeze=False)

Return the midpoints between values in the given array.

If the given array is N-dimensional, midpoints are calculated from the last dimension.

Parameters:
arrndarray of scalars,

Input array.

logbool or None,

Find midpoints in log-space

axisint, sequence, or None,

The axis about which to find the midpoints. If None, find the midpoints along all axes. If a sequence (tuple, list, or array), take the midpoints along each specified axis.

Returns:
midsndarray of floats,

The midpoints of the input array. The resulting shape will be the same as the input array arr, except that mids.shape[axis] == arr.shape[axis]-1.

kalepy.utils.minmax(data, positive=False, prev=None, stretch=None, log_stretch=None, limit=None)
kalepy.utils.parse_edges(data, edges=None, extrema=None, weights=None, params=None, nmin=5, nmax=1000, pad=None, refine=1.0, bw=None)
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

Desired percentiles of the data, within range [0.0, 1.0].

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(M,) float

Array of percentiles of the weighted input data.

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:
arrarray_like

Array to be tested.

Returns:
bool

Whether arr is purely 1D.

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:
datandarray of scalar

Input data from which to calculate statistics.

percsarray_like of scalars in {0, 100}

Which percentiles to calculate.

avebool

Include average value in output.

stdbool

Include standard-deviation in output.

formatstr

Formatting for all numerical output, (e.g. “:.2f”).

logbool

Convert values to log10 before printing.

Returns:
outstr

Single-line string of the desired statistics.

kalepy.utils.subdivide(xx, num=1, log=False)

Subdivide the giving array (e.g. bin edges) by the given factor.

Parameters:
xx(X,) array_like of scalar,

Input array to be subdivided.

numint,

Subdivide each bin by this factor. Subdividing “once” (i.e. num=1) produces 2x number of bins. In general the number of output bins is X * (num + 1).

logbool,

Subdivide evenly in log-space, instead of linear space (e.g. [0, 10.0] ==> [0.0, 3.16, 10.0])

Returns:
div(X * num+1,) ndarray of float

Subdivided array with a number of elements equal to the length of the input array ‘X’ times one plus the subdivision factor num.

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:
pdfarray_like

Density values, computed at the grid edges specified by the edges list-of-lists.

edgesarray_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,).

axisint, array_like int, or None

Along which axes to convert from density to mass. If None, apply to all axes.

Returns:
massarray_like

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, …).

kalepy.utils.trapz_nd(data, edges, axis=None)