pypeit.containers.bspline module
FITS-serializable wrappers for the BSpline and
BSpline2D classes.
Each container inherits from both DataContainer (for FITS
I/O) and its respective algorithmic base class (for all fitting and evaluation
methods). The dual inheritance requires a specific construction order:
__init__ must run first to establish the
attribute-enforcement mechanism, after which the base-class constructor runs against
the already-prepared __dict__.
Note
The breakpoints property defined on
BSpline is a read-only data descriptor that
delegates to self.knots.breakpoints. Using breakpoints as a datamodel
key would create an irresolvable naming conflict (the property intercepts all
attribute-level reads, making the stored value invisible). The breakpoint
array is therefore stored under the distinct key bkpt_full, and the
property continues to serve all algorithmic code via self.knots.
- class pypeit.containers.bspline.BSpline2DContainer(x=None, knots=None, nord=4)[source]
Bases:
DataContainer,BSpline2DFITS-serializable wrapper for
BSpline2D.Inherits all fitting and evaluation methods from
BSpline2Dand adds FITS I/O viaDataContainer. Instances can be used wherever a plainBSpline2Dis expected.The datamodel attributes are:
Version: 2.0.0
Attribute
Type
Array Type
Description
basisPolynomial basis matrix, shape (N, npoly); stored only when fit was called with a numpy array basis (funcname is None).
bkpt_fullFull padded knot vector stored for serialization.
bkpt_gpmActive-breakpoint mask; True where a breakpoint participates in the fit.
coeffFitted 2D B-spline coefficients, shape (nc, npoly).
funcnamestr
Polynomial family name (e.g.
legendre); None when fit was called with an array basis.icoeffInverse-covariance diagonal, shape (nc, npoly).
nordint
Order of the B-spline.
npolyint
Number of polynomial basis functions in the second dimension.
xmaxfloat
Upper normalisation bound for the polynomial basis coordinate.
xminfloat
Lower normalisation bound for the polynomial basis coordinate.
The
basisdatamodel field is populated only whenfit()was called with anumpy.ndarraybasisargument (i.e., whenfuncnameisNoneafter fitting). Storing the basis enables evaluation viavalue()without re-supplying it after loading from a file. When the fit was performed with a string basis (e.g.'legendre'),basisisNoneand the caller must supplybasis_xtovalue().- Parameters:
x (
numpy.ndarray, optional) – Independent variable used to build the initial breakpoint grid.knots (
Knotsornumpy.ndarrayor None, optional) – Knot specification.nord (int, optional) – Order of the B-spline (default 4).
See also
- _bundle()[source]
Overrides the base class method used to bundle the datamodel for FITS serialization.
Captures any in-place changes to
knots.breakpointsinbkpt_fulland, for array-basis fits (funcname is None), copiesPtobasisso that the polynomial basis matrix can be recovered on load via_validate().Beyond that, the method uses the base class method and sets the FITS extension name to
'BSPLINE2D'.
- _validate()[source]
Overrides the base class validation method called during instantiation.
Specifically, this method rebuilds
knotsfrombkpt_fulland restorePfrombasiswhen they are not set by the constructor.This extends the
BSplineContainerlogic with an additional step for array-basis fits: whenbasisis populated butPisNone,Pis restored frombasisso thatvalue()can be called immediately after any dict-based instantiation (e.g.from_file(),from_bspline2d()) without re-supplying the basis array.
- copy()[source]
Return a deep copy as a new
BSpline2DContainer.The design-matrix cache is not copied (the copy starts with a cold cache). The polynomial basis
Pis copied when it exists: for array-basis fits (funcname is None) it is already restored frombasisby_validate(); for string-basis fits it is copied explicitly.- Return type:
- datamodel = {'basis': {'atype': <class 'numpy.floating'>, 'descr': 'Polynomial basis matrix, shape (N, npoly); stored only when fit was called with a numpy array basis (funcname is None).', 'otype': <class 'numpy.ndarray'>}, 'bkpt_full': {'atype': <class 'numpy.floating'>, 'descr': 'Full padded knot vector stored for serialization.', 'otype': <class 'numpy.ndarray'>}, 'bkpt_gpm': {'atype': <class 'numpy.bool'>, 'descr': 'Active-breakpoint mask; True where a breakpoint participates in the fit.', 'otype': <class 'numpy.ndarray'>}, 'coeff': {'atype': <class 'numpy.floating'>, 'descr': 'Fitted 2D B-spline coefficients, shape (nc, npoly).', 'otype': <class 'numpy.ndarray'>}, 'funcname': {'descr': 'Polynomial family name (e.g. ``legendre``); None when fit was called with an array basis.', 'otype': <class 'str'>}, 'icoeff': {'atype': <class 'numpy.floating'>, 'descr': 'Inverse-covariance diagonal, shape (nc, npoly).', 'otype': <class 'numpy.ndarray'>}, 'nord': {'descr': 'Order of the B-spline.', 'otype': <class 'int'>}, 'npoly': {'descr': 'Number of polynomial basis functions in the second dimension.', 'otype': <class 'int'>}, 'xmax': {'descr': 'Upper normalisation bound for the polynomial basis coordinate.', 'otype': <class 'float'>}, 'xmin': {'descr': 'Lower normalisation bound for the polynomial basis coordinate.', 'otype': <class 'float'>}}
Provides the class data model. This is undefined in the abstract class and should be overwritten in the derived classes.
The format of the
datamodelneeded for each implementation of aDataContainerderived class is as follows.The datamodel itself is a class attribute (i.e., it is a member of the class, not just of an instance of the class). The datamodel is a dictionary of dictionaries: Each key of the datamodel dictionary provides the name of a given datamodel element, and the associated item (dictionary) for the datamodel element provides the type and description information for that datamodel element. For each datamodel element, the dictionary item must provide:
otype: This is the type of the object for this datamodel item. E.g., for a float or a numpy.ndarray, you would setotype=floatandotype=np.ndarray, respectively.descr: This provides a text description of the datamodel element. This is used to construct the datamodel tables in the pypeit documentation.
If the object type is a numpy.ndarray, you should also provide the
atypekeyword that sets the type of the data contained within the array. E.g., for a floating point array containing an image, your datamodel could be simply:datamodel = {'image' : dict(otype=np.ndarray, atype=float, descr='My image')}
More advanced examples are given in the top-level module documentation.
Currently,
datamodelcomponents are restricted to haveotypethat aretuple,int,float,numpy.integer,numpy.floating, numpy.ndarray, or astropy.table.Table objects. E.g.,datamodelvalues forotypecannot bedict.
- fit(x, y, ivar=None, basis='legendre', npoly=1, basis_x=None, xmin=None, xmax=None, reset_knots=False)[source]
Fit a weighted least-squares 2D B-spline.
This delegates entirely to
fit(), but updatesbkpt_full, as necessary. See documentation forfit().
- classmethod from_bspline2d(spl)[source]
Construct a
BSpline2DContainerfrom an existingBSpline2D.- Parameters:
spl (
BSpline2D) – Source B-spline. Itsbreakpointsmust not beNone.- Returns:
A new container wrapping a copy of the spline state. The
basisdatamodel field is populated only whenspl.funcname is Noneandspl.P is not None(array-basis fit); otherwise it isNone.- Return type:
- internals = ['knots', 'x', 'yfit', '_cached_design', 'P', 'basis_x']
A list of strings identifying a set of internal attributes that are not part of the datamodel.
- version = '2.0.0'
Provides the string representation of the class version.
This is currently put to minimal use so far, but will used for I/O verification in the future.
Each derived class should provide a version to guard against data model changes during development.
- class pypeit.containers.bspline.BSplineContainer(x=None, knots=None, nord=4)[source]
Bases:
DataContainer,BSplineFITS-serializable wrapper for
BSpline.Inherits all fitting and evaluation methods from
BSplineand adds FITS I/O viaDataContainer. Instances can be used wherever a plainBSplineis expected.The datamodel attributes are:
Version: 2.0.0
Attribute
Type
Array Type
Description
bkpt_fullFull padded knot vector stored for serialization.
bkpt_gpmActive-breakpoint mask; True where a breakpoint participates in the fit.
coeffFitted B-spline coefficients, shape (nc,).
icoeffInverse-covariance diagonal, shape (nc,).
nordint
Order of the B-spline.
- Parameters:
x (
numpy.ndarray, optional) – Independent variable used to build the initial breakpoint grid. Passed directly toBSpline.knots (
Knotsornumpy.ndarrayor None, optional) – Knot specification. Passed directly toBSpline.nord (int, optional) – Order of the B-spline (default 4).
See also
- _bundle()[source]
Overrides the base class method used to bundle the datamodel for FITS serialization.
The method ensures any in-place changes to
knots.breakpoints(e.g. from afit()call withreset_knots=True) are captured inbkpt_fullbefore the datamodel attributes are packaged for writing.Beyond that, the method uses the base class method and sets the FITS extension name to
'BSPLINE'.
- _validate()[source]
Overrides the base class validation method called during instantiation.
Specifically, this method rebuilds
knotsfrombkpt_fullwhen knots were not set by the constructor.knotsis aninternal(not stored in the datamodel), so it isNoneafter any dict-based instantiation (e.g.from_file(),from_bspline()). This override rebuilds it from the storedbkpt_fullarray so that all fitting and evaluation methods work without further intervention.bkpt_gpmis initialized to all-Truewhen it is absent from the source data.
- copy()[source]
Return a deep copy as a new
BSplineContainer.The design-matrix cache is not copied (the copy starts with a cold cache), matching the behavior of
pypeit.core.bspline.BSpline.copy().- Return type:
- datamodel = {'bkpt_full': {'atype': <class 'numpy.floating'>, 'descr': 'Full padded knot vector stored for serialization.', 'otype': <class 'numpy.ndarray'>}, 'bkpt_gpm': {'atype': <class 'numpy.bool'>, 'descr': 'Active-breakpoint mask; True where a breakpoint participates in the fit.', 'otype': <class 'numpy.ndarray'>}, 'coeff': {'atype': <class 'numpy.floating'>, 'descr': 'Fitted B-spline coefficients, shape (nc,).', 'otype': <class 'numpy.ndarray'>}, 'icoeff': {'atype': <class 'numpy.floating'>, 'descr': 'Inverse-covariance diagonal, shape (nc,).', 'otype': <class 'numpy.ndarray'>}, 'nord': {'descr': 'Order of the B-spline.', 'otype': <class 'int'>}}
Provides the class data model. This is undefined in the abstract class and should be overwritten in the derived classes.
The format of the
datamodelneeded for each implementation of aDataContainerderived class is as follows.The datamodel itself is a class attribute (i.e., it is a member of the class, not just of an instance of the class). The datamodel is a dictionary of dictionaries: Each key of the datamodel dictionary provides the name of a given datamodel element, and the associated item (dictionary) for the datamodel element provides the type and description information for that datamodel element. For each datamodel element, the dictionary item must provide:
otype: This is the type of the object for this datamodel item. E.g., for a float or a numpy.ndarray, you would setotype=floatandotype=np.ndarray, respectively.descr: This provides a text description of the datamodel element. This is used to construct the datamodel tables in the pypeit documentation.
If the object type is a numpy.ndarray, you should also provide the
atypekeyword that sets the type of the data contained within the array. E.g., for a floating point array containing an image, your datamodel could be simply:datamodel = {'image' : dict(otype=np.ndarray, atype=float, descr='My image')}
More advanced examples are given in the top-level module documentation.
Currently,
datamodelcomponents are restricted to haveotypethat aretuple,int,float,numpy.integer,numpy.floating, numpy.ndarray, or astropy.table.Table objects. E.g.,datamodelvalues forotypecannot bedict.
- fit(x, y, ivar=None, reset_knots=False)[source]
Fit a weighted least-squares B-spline.
This delegates entirely to
fit(), but updatesbkpt_full, as necessary. See documentation forfit().
- classmethod from_bspline(spl)[source]
Construct a
BSplineContainerfrom an existingBSpline.- Parameters:
spl (
BSpline) – Source B-spline. Itsbreakpointsmust not beNone.- Returns:
A new container wrapping a copy of the spline state. The transient internals
xandyfitare not copied; callfit()or usecopy()if those are needed.- Return type:
- internals = ['knots', 'x', 'yfit', '_cached_design']
A list of strings identifying a set of internal attributes that are not part of the datamodel.
- version = '2.0.0'
Provides the string representation of the class version.
This is currently put to minimal use so far, but will used for I/O verification in the future.
Each derived class should provide a version to guard against data model changes during development.