Covariances

class sacc.covariance.BaseCovariance[source]

The abstract base class for covariances in different forms.

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.
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

A covariance instance

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.

Attributes:
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

Methods

from_hdu(hdu) Read a covariance object from a loaded FITS HDU.
get_block(self, indices) Read a (not necessarily contiguous) sublock of the matrix
inverted(self) Return the inverse of the covariance matrix, as a dense array.
make(cov) Make an appropriate covariance object from the matrix info itself.
masked(self, mask) Return a new matrix with only the masked elements retained.
to_hdu(self) 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(self, 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.

inverted(self)[source]

Return the inverse of the covariance matrix, as a dense array.

Returns:
invC: array

Inverse covariance

masked(self, mask)[source]

Return a new matrix with only the masked 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:
mask: 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

to_hdu(self)[source]

Write a FITS HDU from the data, ready to be saved.

The data in the HDU is stored as a single 1 x size image, and the header contains the information needed to reconstruct it.

Parameters:
None
Returns:
hdu: astropy.fits.ImageHDU object

HDU containing data and metadata

class sacc.covariance.DiagonalCovariance(variances)[source]

A covariance subclass representing covariances that are purely diagonal.

Attributes:
size: int

The size of the matrix

diag: array

The diagonal terms in the covariance (i.e. the variances)

Methods

from_hdu(hdu) Load a covariance object from the data in the HDU
get_block(self, indices) Read a (not necessarily contiguous) sublock of the matrix
inverted(self) Return the inverse of the covariance matrix, as a dense array.
make(cov) Make an appropriate covariance object from the matrix info itself.
masked(self, mask) Return a new DiagonalCovariance with only the masked elements retained.
to_hdu(self) 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(self, 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.

inverted(self)[source]

Return the inverse of the covariance matrix, as a dense array.

Returns:
invC: array

Inverse covariance

masked(self, mask)[source]

Return a new DiagonalCovariance with only the masked elements retained.

Parameters:
mask: 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

to_hdu(self)[source]

Make an astropy FITS HDU object with this covariance in it. In this can a binary table HDU is created.

Parameters:
None
Returns:
hdu: astropy.fits.BinTableHDU instance

HDU that can be used to reconstruct the object.

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(self, indices) Read a (not necessarily contiguous) sublock of the matrix
inverted(self) Return the inverse of the covariance matrix, as a dense array.
make(cov) Make an appropriate covariance object from the matrix info itself.
masked(self, mask) Return a new matrix with only the masked elements retained.
to_hdu(self) 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(self, 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

inverted(self)[source]

Return the inverse of the covariance matrix, as a dense array.

Returns:
invC: array

Inverse covariance

masked(self, mask)[source]

Return a new matrix with only the masked elements retained.

Parameters:
mask: 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

to_hdu(self)[source]

Make an astropy FITS HDU object with this covariance in it. This is represented as an image.

Parameters:
None
Returns:
hdu: astropy.fits.ImageHDU instance

HDU that can be used to reconstruct the object.