Interpolation Schemes¶
Interpolators goven how the model parameters are interpolated across the field of view.
The Interp base class¶
-
class
piff.
Interp
[source]¶ The base class for interpolating a set of data vectors across the field of view.
In general, the interpolator is agnostic as to the meaning of the parameter vectors. These parameter vectors are passed as simple numpy arrays. They are imbued meaning by a Model instance. Thus, the same interpolators may be used with many different Model types.
The principal ways that interpolators will differ are:
Which properties of the star are used for their interpolation.
What functional form (or algorithm) is used to interpolate between measurements.
Whether the interpolator assumes each sample has a non-degenerate parameter fit, vs getting a differential quadratic form for chisq from each sample.
The answer to #3 is given in a boolean property degenerate_points.
This is essentially an abstract base class intended to define the methods that should be implemented by any derived class.
-
getProperties
(star)[source]¶ Extract the appropriate properties to use as the independent variables for the interpolation.
The base class implementation returns the field position (u,v) as a 1d numpy array.
- Parameters
star – A Star instance from which to extract the properties to use.
- Returns
A numpy vector of these properties.
-
initialize
(stars, logger=None)[source]¶ Initialize both the interpolator to some state prefatory to any solve iterations and initialize the stars for use with this interpolator.
The nature of the initialization is specific to the derived classes.
The base class implentation calls interpolateList, which will set the stars to have the right type object in its star.fit.params attribute.
- Parameters
stars – A list of Star instances to use to initialize.
logger – A logger object for logging debug info. [default: None]
- Returns
a new list of Star instances
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position.
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance holding the interpolated parameters
-
interpolateList
(stars, logger=None)[source]¶ Perform the interpolation for a list of stars.
The base class just calls interpolate(star) for each star in the list, but in many cases, this may be more efficiently done with a matrix operation, so we make it available for derived classes to override.
- Parameters
stars – A list of Star instances to interpolate.
logger – A logger object for logging debug info. [default: None]
- Returns
a list of new Star instances with interpolated parameters
-
classmethod
parseKwargs
(config_interp, logger=None)[source]¶ Parse the interp field of a configuration dict and return the kwargs to use for initializing an instance of the class.
The base class implementation just returns the kwargs as they are, but derived classes might want to override this if they need to do something more sophisticated with them.
- Parameters
config_interp – The interpolator field of the configuration dict, config[‘interp’]
logger – A logger object for logging debug info. [default: None]
- Returns
a kwargs dict to pass to the initializer
-
classmethod
process
(config_interp, logger=None)[source]¶ Parse the interp field of the config dict.
- Parameters
config_interp – The configuration dict for the interp field.
logger – A logger object for logging debug info. [default: None]
- Returns
an Interp instance
-
property
property_names
¶ List of properties used by this interpolant.
-
classmethod
read
(fits, extname)[source]¶ Read an Interp from a FITS file.
- Parameters
fits – An open fitsio.FITS object
extname – The name of the extension with the interpolator information.
- Returns
an interpolator built with a information in the FITS file.
-
solve
(stars, logger=None)[source]¶ Solve for the interpolation coefficients given some data.
- Parameters
stars – A list of Star instances to interpolate between
logger – A logger object for logging debug info. [default: None]
-
write
(fits, extname)[source]¶ Write an Interp to a FITS file.
Note: this only writes the initialization kwargs to the fits extension, not the parameters.
The base class implemenation works if the class has a self.kwargs attribute and these are all simple values (str, float, or int).
However, the derived class will need to implement _finish_write to write the solution parameters to a binary table.
- Parameters
fits – An open fitsio.FITS object
extname – The name of the extension to write the interpolator information.
Mean interpolation¶
-
class
piff.
Mean
(logger=None)[source]¶ The simplest possible interpolation scheme. It just finds the mean of the parameter vectors and uses that at every position.
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position.
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance holding the interpolated parameters
-
Polynomial interpolation¶
-
class
piff.
Polynomial
(order=None, orders=None, poly_type='poly', logger=None)[source]¶ An interpolator that uses scipy curve_fit command to fit a polynomial surface to each parameter passed in independently.
- Parameters
order – The maximum order in the polynomial. i.e. the maximum value of i+j where p(u,v) = sum c_{ij} x^i y^j. [required, unless orders is given]
orders – Optionally, a list of orders, one for each parameter to be interpolated. This list should be the same length as the number of parameters that will be given to interpolate.
poly_type – A string, one of the keys in the polynomial_types dictionary. By default these are “poly” (ordinary polynomials), “chebyshev”, “legendre”, “laguerre”, “hermite”. To add more you can add a key to polynomial_types with the value of a function with the signature of numpy.polynomial.polynomial.polyval2d
-
initialize
(stars, logger=None)[source]¶ Initialization is just solving the interpolator with current stars. This then calls interpolateList, which will set the stars to have the right type of object in its star.fit.params attribute.
- Parameters
stars – A list of Star instances to use to initialize.
logger – A logger object for logging debug info. [default: None]
- Returns
a new list of Star instances
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position.
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance holding the interpolated parameters
-
solve
(stars, logger=None)[source]¶ Solve for the interpolation coefficients given some data, using the scipy.optimize.curve_fit routine, which uses Levenberg-Marquardt to find the least-squares solution.
This currently assumes that our positions pos are just u and v.
- Parameters
stars – A list of Star instances to use for the interpolation.
logger – A logger object for logging debug info. [default: None]
Interpolation using basis functions¶
-
class
piff.
BasisInterp
[source]¶ An Interp class that works whenever the interpolating functions are linear sums of basis functions. Does things the “slow way” to be stable to degenerate fits to individual stars, instead of fitting to parameter sets produced by single stars.
First time coding this we will assume that each element of the PSF parameter vector p is a linear combination of the same set of basis functions across the focal plane,
\[p_i = \sum_{j} q_{ij} K_j(u,v,other stellar params).\]The property degenerate_points is set to True to indicate that this interpolator uses the alpha/beta quadratic form of chisq for each sample, rather than assuming that a best-fit parameter vector is available at every sample.
Internally we’ll store the interpolation coefficients in a 2d array of dimensions (nparams, nbases)
Note: This is an abstract base class. The concrete class you probably want to use is BasisPolynomial.
-
basis
(star)[source]¶ Return 1d array of polynomial basis values for this star
- Parameters
star – A Star instance
- Returns
1d numpy array with values of u^i v^j for 0<i+j<=order
-
constant
(value=1.0)[source]¶ Return 1d array of coefficients that represent a polynomial with constant value.
- Parameters
value – The value to use as the constant term. [default: 1.]
- Returns
1d numpy array with values of u^i v^j for 0<i+j<=order
-
initialize
(stars, logger=None)[source]¶ Initialize both the interpolator to some state prefatory to any solve iterations and initialize the stars for use with this interpolator.
This class will initialize everything to have constant PSF parameter vector taken from the first Star in the list.
- Parameters
stars – A list of Star instances to use to initialize.
logger – A logger object for logging debug info. [default: None]
- Returns
A new list of Stars which have their parameters initialized.
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position.
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance holding the interpolated parameters
-
solve
(stars, logger=None)[source]¶ Solve for the interpolation coefficients given some data. The StarFit element of each Star in the list is assumed to hold valid alpha and beta members specifying depending of chisq on differential changes to its parameter vector.
- Parameters
stars – A list of Star instances to interpolate between
logger – A logger object for logging debug info. [default: None]
-
-
class
piff.
BasisPolynomial
(order, keys=('u', 'v'), max_order=None, use_qr=False, logger=None)[source]¶ A version of the Polynomial interpolator that works with BasisModels and can use the quadratic form of the chisq information it calculates. It works better than the regular Polynomial interpolator when there is missing or degenerate information.
The order is the highest power of a key to be used. This can be the same for all keys or you may provide a list of separate order values to be used for each key. (e.g. you may want to use 2nd order in the positions, but only 1st order in the color).
All combinations of powers of keys that have total order <= max_order are used. The maximum order is normally the maximum order of any given key’s order, but you may specify a larger value. (e.g. to use 1, x, y, xy, you would specify order=1, max_order=2.)
- Parameters
order – The order to use for each key. Can be a single value (applied to all keys) or an array matching number of keys.
keys – List of keys for properties that will be used as the polynomial arguments. [default: (‘u’,’v’)]
max_order – The maximum total order to use for cross terms between keys. [default: None, which uses the maximum value of any individual key’s order]
use_qr – Use QR decomposition for the solution rather than the more direct least squares solution. QR decomposition requires more memory than the default and is somewhat slower (nearly a factor of 2); however, it is significantly less susceptible to numerical errors from high condition matrices. Therefore, it may be preferred for some use cases. [default: False]
logger – A logger object for logging debug info. [default: None]
-
basis
(star)[source]¶ Return 1d array of polynomial basis values for this star
- Parameters
star – A Star instance
- Returns
1d numpy array with values of u^i v^j for 0<i+j<=order
-
constant
(value=1.0)[source]¶ Return 1d array of coefficients that represent a polynomial with constant value.
- Parameters
value – The value to use as the constant term. [default: 1.]
- Returns
1d numpy array with values of u^i v^j for 0<i+j<=order
-
getProperties
(star)[source]¶ Extract the appropriate properties to use as the independent variables for the interpolation.
The base class implementation returns the field position (u,v) as a 1d numpy array.
- Parameters
star – A Star instance from which to extract the properties to use.
- Returns
A numpy vector of these properties.
-
property
property_names
¶ List of properties used by this interpolant.
K-Nearest Neighbors¶
-
class
piff.
kNNInterp
(keys=('u', 'v'), n_neighbors=15, weights='uniform', algorithm='auto', p=2, logger=None)[source]¶ An interpolator that uses sklearn KNeighborsRegressor to interpolate a single surface
- Parameters
keys – A list of star attributes to interpolate from [default: (‘u’, ‘v’)]
n_neighbors – Number of neighbors used for interpolation. [default: 15]
weights – Weight function used in prediction. Possible values are ‘uniform’, ‘distance’, and a callable function which accepts an array of distances and returns an array of the same shape containing the weights. [default: ‘uniform’]
algorithm – Algorithm used to compute nearest neighbors. Possible values are ‘ball_tree’, ‘kd_tree’, ‘brute’, and ‘auto’, which tries to determine the best choice. [default: ‘auto’]
p – Power parameter of distance metrice. p=2 is default euclidean distance, p=1 is manhattan. [default: 2]
logger – A logger object for logging debug info. [default: None]
-
getProperties
(star, logger=None)[source]¶ Extract the appropriate properties to use as the independent variables for the interpolation.
Take self.keys from star.data
- Parameters
star – A Star instances from which to extract the properties to use.
- Returns
A np vector of these properties.
-
initialize
(stars, logger=None)[source]¶ Initialize both the interpolator to some state prefatory to any solve iterations and initialize the stars for use with this interpolator.
- Parameters
stars – A list of Star instances to interpolate between
logger – A logger object for logging debug info. [default: None]
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position. Calls interpolateList because sklearn prefers list input anyways
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance with its StarFit member holding the interpolated parameters
-
interpolateList
(star_list, logger=None)[source]¶ Perform the interpolation for a list of stars.
- Parameters
star_list – A list of Star instances to which to interpolate.
logger – A logger object for logging debug info. [default: None]
- Returns
a list of new Star instances with interpolated parameters
-
property
property_names
¶ List of properties used by this interpolant.
Gaussian process interpolation¶
-
class
piff.
GPInterp
(keys=('u', 'v'), kernel='RBF(1)', optimizer='isotropic', normalize=True, l0=3000.0, white_noise=0.0, n_neighbors=4, average_fits=None, nbins=20, min_sep=None, max_sep=None, rows=None, logger=None)[source]¶ An interpolator that models the underlying field as a Gaussian process.
Gaussian process regression, also known as “Kriging,” assumes that the parameters are drawn from a multi-dimensional Gaussian random field across the (u, v) space. It requires an estimate of the spatial covariance function of p, commonly referred to as the kernel.
The interpolation estimate at an arbitrary location (u, v) is the minimum variance unbiased estimate from the Gaussian distribution at that location conditioned on the values of the parameters measured at all the PSF stars.
The implemention of this class use the treegp module. (https://github.com/PFLeget/treegp) It can use any of the kernels defined in scikit-learn (https://scikit-learn.org/) to define the covariance matrix as well as a custom VonKarman kernel defined in treegp. The default kernel is the so-called “squared exponential” or “radial basis function” kernel, known as RBF in scikit-learn.
The default behavior involves measuring the radial two-point correlation function with TreeCorr (https://github.com/rmjarvis/TreeCorr) and then fitting the hyper-parameters of the kernel that best fits this measurement. This can be done either isotropically or anisotropically. There are also options to use the traditional maximum likelihood optimization or no optimization if preferred. See the
optimizer
parameter below.- Parameters
keys – A list of keys for properties that will be interpolated. Must be 2 properties, which can be used to calculate a 2-point correlation function. [default: (‘u’,’v’)]
kernel – A string that can be evaled to make a
sklearn.gaussian_process.kernels.Kernel
object. Could be also a list of Kernels objects (one per PSF param). The reprs of sklear Kernels will work, as well as the repr of a custom treegp VonKarman object. [default: ‘RBF(1)’]optimizer –
Indicates which techniques to use for optimizing the kernel. Four options are available:
”isotropic” = use an isotropic radial 2-point correlation function estimated by TreeCorr.
”anisotropic” = use an anisotropic two-dimensional 2-point correlation function estimated by TreeCorr.
”likelihood” = use the classical Gaussian process maximum likelihood method.
”none” = don’t do any kernal optimization.
[default: “isotropic”]
rows – A list of integer which indicates which rows of Star.fit.param need to be interpolated using GPs. [default: None, which means all rows]
normalize – Whether to normalize the interpolation parameters to have a mean of 0. Normally, the parameters being interpolated are not mean 0, so you would want this to be True, but if your parameters have an a priori mean of 0, then subtracting off the realized mean would be invalid. [default: True]
white_noise – A float value that indicate the ammount of white noise that you want to use during the gp interpolation. This is an additional uncorrelated noise added in quadrature to the measured uncertainties of the PSF parameters. This should be given as a “sigma” value, not a variance. [default: 0.]
nbins – Number of bins (in each direction using a 2D correlation function) used in TreeCorr to compute the 2-point correlation function. Used only if optimizer is “isotropic” or “anisotropic”. [default: 20]
min_sep – Minimum separation between pairs when computing 2-point correlation function in arcsec (or more generally the same units as the keys). Compute automaticaly if it is not given. Used only if optimizer is “isotropic” or “anisotropic”. [default: None]
max_sep – Maximum separation between pairs when computing 2-point correlation function in arcsec (or more generally the same units as the keys). Compute automaticaly if it is not given. Used only if optimizer is “isotropic” or “anisotropic”. [default: None]
l0 – Initial guess for correlation length when optimzer is “anisotropic” in arcsec (or more generally the same units as the keys). [default: 3000.]
average_fits – A fits file that have the spatial average functions of PSF parameters build in it. Build using meanify and piff output across different exposures. See meanify documentation for details. [default: None]
n_neighbors – Number of neighbors to use for interpolating the spatial average using a KNeighbors interpolation. Used only if average_fits is not None. [defaulf: 4]
logger – A logger object for logging debug info. [default: None]
-
getProperties
(star, logger=None)[source]¶ Extract the appropriate properties to use as the independent variables for the interpolation.
Take self.keys from star.data
- Parameters
star – A Star instances from which to extract the properties to use.
- Returns
A np vector of these properties.
-
initialize
(stars, logger=None)[source]¶ Initialize both the interpolator to some state prefatory to any solve iterations and initialize the stars for use with this interpolator.
- Parameters
stars – A list of Star instances to interpolate between
logger – A logger object for logging debug info. [default: None]
-
interpolate
(star, logger=None)[source]¶ Perform the interpolation to find the interpolated parameter vector at some position.
- Parameters
star – A Star instance to which one wants to interpolate
logger – A logger object for logging debug info. [default: None]
- Returns
a new Star instance with its StarFit member holding the interpolated parameters
-
interpolateList
(stars, logger=None)[source]¶ Perform the interpolation for a list of stars.
- Parameters
star_list – A list of Star instances to which to interpolate.
logger – A logger object for logging debug info. [default: None]
- Returns
a list of new Star instances with interpolated parameters
-
property
property_names
¶ List of properties used by this interpolant.