sciris.sc_math

Extensions to Numpy, including finding array elements and smoothing data.

Highlights:

Functions

approx

Determine whether two scalars (or an array and a scalar) approximately match.

cat

Like np.concatenate(), but takes anything and returns an array.

convolve

Like np.convolve(), but always returns an array the size of the first array (equivalent to mode='same'), and solves the boundary problem present in np.convolve() by adjusting the edges by the weight of the convolution kernel.

count

Count the number of matching elements.

dataindex

Take an array of data and return either the first or last (or some other) non-NaN entry.

fillnans

Alias for sc.sanitize(..., replacenans=True) with nearest interpolation (or a specified value).

findfirst

Alias for findinds(..., first=True).

findinds

Find matches even if two things aren't eactly equal (e.g.

findlast

Alias for findinds(..., last=True).

findnearest

Return the index of the nearest match in series to value -- like findinds, but always returns an object with the same type as value (i.e.

gauss1d

Gaussian 1D smoothing kernel.

gauss2d

Gaussian 2D smoothing kernel.

getvaliddata

Return the data value indices that are valid based on the validity of the input data.

getvalidinds

Return the indices that are valid based on the validity of the input data from an arbitrary number of 1-D vector inputs.

inclusiverange

Like np.arange()/np.linspace(), but includes the start and stop points.

isprime

Determine if a number is prime.

linregress

Simple linear regression returning the line of best fit and R value.

normalize

Rescale an array between a minimum value and a maximum value.

normsum

Multiply a list or array by some normalizing factor so that its sum is equal to the total.

numdigits

Count the number of digits in a number (or list of numbers).

perturb

Define an array of numbers uniformly perturbed with a mean of 1.

randround

Round a float, list, or array probabilistically to the nearest integer.

rmnans

Sanitize input to remove NaNs.

rolling

Alias to pandas' rolling() (window) method to smooth a series.

safedivide

Handle divide-by-zero and divide-by-nan elegantly.

sanitize

Sanitize input to remove NaNs.

smooth

Very simple function to smooth a 1D or 2D array.

smoothinterp

Smoothly interpolate over values and keep end points.