Covariances¶
- class sacc.covariance.BaseCovariance[source]¶
The abstract base class for covariances in different forms. These are not currently designed to be modified after creation.
The three concrete subclasses that are created are:
FullCovariance - for dense matrices
- BlockDiagonalCovariance - for block diagonal matrices
(those in which some sub-blocks are dense but without correlation between the blocks
DiagonalCovariance - a covariance where the elements are uncorrelated
- Attributes:
- cov_type: string
The type of the covariance (class variable)
Methods
from_hdu
(hdu)Make a covariance object from an astropy FITS HDU object.
make
(cov)Make an appropriate covariance object from the matrix info itself.
- property dense¶
A dense matrix form of the covariance
- Parameters:
- None
- Returns:
- covmat: 2D array
Numpy array of dense form of matrix
- classmethod from_hdu(hdu)[source]¶
Make a covariance object from an astropy FITS HDU object.
The type of the covariance is determined from a keyword in the HDU, and then the corresponding subclass from_hdu method is called.
- Parameters:
- hdu: astropy.fits.ImageHDU instance
An HDU object with covariance info in it
- Returns:
- instance: BaseCovariance
A covariance instance
- property inverse¶
A dense matrix form of the inverse of the covariance matrix
- Returns:
- invC: array
Inverse covariance
- classmethod make(cov)[source]¶
Make an appropriate covariance object from the matrix info itself.
You can pass in a list of covariance blocks for a block-diagonal, covariance a 1D array for a diagonal covariance, or a full matrix.
A different subclass is returned for each of these cases.
- Parameters:
- cov: list[array] or array
If a list, the total length of all the arrays in it should equal n. If an array, it should be either 1D of length n or 2D of shape (n x n).
- n: int
length of the data vector to which this covariance applies
- class sacc.covariance.BlockDiagonalCovariance(blocks)[source]¶
A covariance subclass representing block diagonal covariances
Block diagonal covariances have sub-blocks that are full dense matrices, but without correlations between the blocks. This feature can be taken advantage of when doing matrix operations like multiplication or inversion.
- Parameters:
- blocks: list[arrays]
list of sub-blocks of the matrix
- block_sizes: list[int]
list of sizes n of each the n x n sub-blocks
- size: int
overall total size of the matrix
- Attributes:
dense
A dense matrix form of the covariance
inverse
A dense matrix form of the inverse of the covariance matrix
Methods
from_hdu
(hdu)Read a covariance object from a loaded FITS HDU.
get_block
(indices)Read a (not necessarily contiguous) sublock of the matrix
keeping_indices
(indices)Return a new instance with only the specified elements retained.
make
(cov)Make an appropriate covariance object from the matrix info itself.
to_hdu
()Write a FITS HDU from the data, ready to be saved.
- classmethod from_hdu(hdu)[source]¶
Read a covariance object from a loaded FITS HDU.
- Parameters:
- hdu: FITS HDU object as read in by astropy.
- Returns:
- cov: BlockDiagonalCovariance
Loaded covariance object
- get_block(indices)[source]¶
Read a (not necessarily contiguous) sublock of the matrix
- Parameters:
- indices: array
An array of integer indices, which must be in ascending order
- Returns:
- cov: array
A full (dense) 2x2 array of the submatrix.
- keeping_indices(indices)[source]¶
Return a new instance with only the specified elements retained.
This method will try to return another BlockDiagonalCovariance if it can, but otherwise will revert to a full one: if the mask passed in is of a boolean type or if it is integers in it can remain block diagonal
- Parameters:
- indices: array or list
Either an array or list of integer indices, or a boolean array of the same size (1D) as the matrix. Specifies rows/cols to keep in the new matrix.
- Returns:
- cov: FullCovariance or BlockDiagonalCovariance
A covariance with only the corresponding data points remaining
- class sacc.covariance.DiagonalCovariance(variances)[source]¶
A covariance subclass representing covariances that are purely diagonal.
- Parameters:
- size: int
The size of the matrix
- diag: array
The diagonal terms in the covariance (i.e. the variances)
- Attributes:
dense
A dense matrix form of the covariance
inverse
A dense matrix form of the inverse of the covariance matrix
Methods
from_hdu
(hdu)Load a covariance object from the data in the HDU
get_block
(indices)Read a (not necessarily contiguous) sublock of the matrix
keeping_indices
(indices)Return a new DiagonalCovariance with only the specified indices retained.
make
(cov)Make an appropriate covariance object from the matrix info itself.
to_hdu
()Make an astropy FITS HDU object with this covariance in it.
- classmethod from_hdu(hdu)[source]¶
Load a covariance object from the data in the HDU
- Parameters:
- hdu: astropy.fits.BinTableHDU instance
- Returns:
- cov: DiagonalCovariance
Loaded covariance object
- get_block(indices)[source]¶
Read a (not necessarily contiguous) sublock of the matrix
- Parameters:
- indices: array
An array of integer indices, which should be in ascending order (for consistency with the block diagonal interface)
- Returns:
- cov: array
A full (dense) 2x2 array of the submatrix.
- keeping_indices(indices)[source]¶
Return a new DiagonalCovariance with only the specified indices retained.
- Parameters:
- indices: array or list
Either an array or list of integer indices, or a boolean array of the same size (1D) as the matrix. Specifies rows/cols to keep in the new matrix.
- Returns:
- cov: DiagonalCovariance
A covariance with only the corresponding data points remaining
- class sacc.covariance.FullCovariance(covmat)[source]¶
A covariance subclass representing a full matrix with correlations anywhere. Represented as an n x n matrix.
- Attributes:
- size: int
the length of the corresponding data vector
- covmat: 2D array
The matrix itself, of shape (size x size)
Methods
from_hdu
(hdu)Load a covariance object from the data in the HDU
get_block
(indices)Read a (not necessarily contiguous) sublock of the matrix
keeping_indices
(indices)Return a new instance with only the specified indices retained.
make
(cov)Make an appropriate covariance object from the matrix info itself.
to_hdu
()Make an astropy FITS HDU object with this covariance in it.
- classmethod from_hdu(hdu)[source]¶
Load a covariance object from the data in the HDU
- Parameters:
- hdu: astropy.fits.ImageHDU instance
- Returns:
- cov: FullCovariance
Loaded covariance object
- get_block(indices)[source]¶
Read a (not necessarily contiguous) sublock of the matrix
- Parameters:
- indices: array
An array of integer indices
- Returns:
- block: array
a 2D array of the relevant sub-block of the matrix
- keeping_indices(indices)[source]¶
Return a new instance with only the specified indices retained.
- Parameters:
- indices: array or list
Either an array or list of integer indices, or a boolean array of the same size (1D) as the matrix. Specifies rows/cols to keep in the new matrix.
- Returns:
- cov: FullCovariance
A covariance with only the corresponding data points remaining