mathtools
Mathematical utility objects and functions.
- _spline_cubic_natural(fp, dxp)[source]
Constructs the second derivative array with natural boundary conditions.
As coded in Numerical Recipes in C Chaper 3.3, but specialized for equally spaced input data.
- Parameters:
fp (ndarray) – 1D data points array to interpolate.
dxp (float) – Spacing of x points.
- Returns:
y2p – Second derivative array. Same size as
fp.- Return type:
ndarray
- _spline_cubic_notaknot(fp, dxp)[source]
Constructs the second derivative array with not-a-knot boundary conditions:
\[y''_0 - 2 y''_1 + y''_2 = 0\]- Parameters:
fp (ndarray) – 1D data points array to interpolate.
dxp (float) – Spacing of x points.
- Returns:
y2p – Second derivative array. Same size as
fp.- Return type:
ndarray
- block_covariance_of_square(mean, var, cov)[source]
Return the block covariance of x^2, i.e. \(<x_i^2 x_j^2> - <x_i^2><x_j^2>\). Compatible with
blockdimargument ofSubsampleCov.get_mean_n_cov().\[\begin{split}Cov[x_i^2, x_j^2] &= Var[x_i] Var[x_j] + Cov(x_i, x_j)^2 \\ &+ 2 Cov(x_i, x_j) E[x_i] E[x_j] \\ &+ Var[x_i] E[x_j]^2 + Var[x_j] E[x_i]^2\end{split}\]
- fft_gaussian_smooth(x, sigma_pix=20, mode='edge')[source]
My Gaussian smoother using FFTs. Input array is padded with edge values at the boundary by default. Pad size is
3*sigma_pix.- Parameters:
x (
ndarray) – 1D array to smooth.sigma_pix (float, default: 20) – Smoothing Gaussian sigma in terms of number of pixels.
mode (str) – Padding method. See
numpy.pad()for options.
- Returns:
y – Smoothed x values. Same size as x.
- Return type:
- get_median_outlier_mask(flux, ivar, esigma=3.5, nwindow=11)[source]
Calculates median[i] of flux and sigma = 1.4826 * MAD[i] based on moving median filter of size
nwindow. Returns a boolean array to mask pixels ifabs(f[i] - median[i]) > esigma * max(n[i], sigma[i]).
- get_smooth_ivar(ivar, sigma_pix=20, esigma=3.5)[source]
Smoothing
ivarvalues to reduce signal-noise coupling.Smoothing is done on
error=1/sqrt(ivar), while replacingivar=0and outliers inerrorvalues with the median. These replaced values are put back in in the final result.- Parameters:
- Returns:
ivar2 – Smoothed ivar values. Outliers and masked values are put back in.
- Return type:
- class FastLinear1DInterp(xp0, dxp, fp, copy=False, ep=None)[source]
Bases:
objectFast interpolator class for equally spaced data. Out of domain points are linearly extrapolated without producing any warnings or errors.
Uses
_fast_eval_interp1d_lin().Example:
one_interp = FastLinear1DInterp(0., 1., np.ones(3)) one_interp(5) # = 1
- Parameters:
- class FastCubic1DInterp(xp0, dxp, fp, copy=False, ep=None, bc_type='not-a-knot')[source]
Bases:
objectFast cubic spline for equally spaced data. Out of domain points are extrapolated without producing any warnings or errors.
This class supports natural, not-a-knot boundary conditions. Uses
_spline_cubic_natural(),_spline_cubic_notaknot(), and_fast_eval_interp1d_cubic().- Parameters:
xp0 (float) – Initial x point for interpolation data.
dxp (float) – Spacing of x points.
fp (
ndarray) – Function calculated at interpolation points.copy (bool, default: False) – Copy input data, specifically fp.
ep (
ndarray, optional) – Error on fp points. Not used! Bookkeeping purposes only.bc_type (str, default: 'not-a-knot') – Boundary condition type. Other option is ‘natural’. See
scipy.interpolate.CubicSpline.
- class SubsampleCov(ndata, nsamples, istart=0)[source]
Bases:
objectUtility class to store all subsamples with weights and calculate the delete-one Jackknife covariance matrix.
Usage:
subsampler = SubsampleCov(ndata, nsamples) for measurement, weights in SomeDataAfterFunction: # You can do this more than nsamples times subsampler.add_measurement(measurement, weights) mean, covariance = subsampler.get_mean_n_cov()
Warning
You cannot call
add_measurement()after callingget_mean(),get_mean_n_cov(), orget_mean_n_var().- Parameters:
ndata (int or tuple(int)) – Size or shape of the data vector. If tuple, it should be
(nset, size1d). For example, 3 quantities share the same weights, data vector shape should passndata=(3, size1d).nsamples (int) – Number of samples. You can add more measurements then this.
istart (int, default: 0) – Start index for the subsampling array
- _is_normalized
If the weights are normalized. Keeps track if
_normalize()is called.- Type:
- covariance
Covariance. 2D arrays of shape
(ndata, ndata)or 3D arrays of shape(nblock, blockdim, blockdim).- Type:
list(
ndarray) or None
- add_measurement(xvec, wvec)[source]
Adds a measurement to the sample.
You can call this function more then
nsamplestimes. The provided measurement should be weighted, but unnormalized, i.e.xvec=wi*xi. After a mean or covariance is obtained, you cannot add more measurements.- Parameters:
- Raises:
RuntimeError – If the object is normalized by calling
_normalize, get_mean, get_mean_n_covandget_mean_n_var.
- allreduce(comm, inplace)[source]
Sums statistics from all MPI process.
Note
Call this with
inplace=MPI.IN_PLACE.- Parameters:
comm (MPI.COMM_WORLD) – MPI comm object for Allreduce
inplace (BufSpec) – MPI.IN_PLACE
- get_mean()[source]
Get the mean of all subsamples.
Warning
You cannot call
add_measurement()after calling this unless youreset().- Returns:
mean – Mean.
- Return type:
- get_mean_n_cov(indices=None, blockdim=None, bias_correct=False)[source]
Get the mean and covariance of the mean using delete-one Jackknife.
Also sets
meanandcovariance.Warning
You cannot call
add_measurement()after calling this unless youreset().- Parameters:
- Returns:
- get_mean_n_var(bias_correct=False)[source]
Get the mean and variance of themean (i.e. diagonal of the covariance) using delete-one Jackknife.
Warning
You cannot call
add_measurement()after calling this unless youreset().