lib.data_vector.covariance_matrix.MockCovarianceMatrix

class lib.data_vector.covariance_matrix.MockCovarianceMatrix(covariance, first, second=None, attrs=None)

Bases: lib.data_vector.covariance_matrix.CovarianceMatrix

Class representing a covariance matrix built from mocks.

Initialize covariance matrix.

Parameters
  • covariance (array) – 2D array holding covariance.

  • first (DataVector) – Data vector for first coordinate in covariance.

  • second (DataVector) – Data vector for second coordinate in covariance.

  • attrs (dict) – Dictionary of other attributes.

Methods

copy

Return shallow copy of self.

deepcopy

from_data

Estimate covariance matrix from list of data vectors.

from_files

Estimate covariance matrix from list of data vector files.

from_state

Instantiate and initalize class with state dictionary.

get_corrcoef

Return correlation matrix.

get_cov

Return covariance.

get_header_txt

Dump header:

get_index

Return indices corresponding to input selections (including current view).

get_invcov

Return inverse covariance.

get_std

Return standard deviation.

get_title_label

Return title, as module.class.

get_x

Return x-coordinates of the data vectors for the two dimensions.

get_y

Return mean y-coordinates for the two dimensions.

is_mpi_broadcast

is_mpi_gathered

is_mpi_root

is_mpi_scattered

load

Load class in numpy binary format from disk.

load_auto

Load covariance matrix.

load_txt

Load CovarianceMatrix from disk.

log_critical

log_debug

log_error

log_info

log_warning

noview

Reset view.

plot

Plot covariance matrix.

read_header_txt

Read and decode header.

read_title_label

Decode title line, splitting module.class into (module, class).

rebin

Rebin projection of name proj (list or single projection name).

save

Save class to disk.

save_auto

Write covariance matrix to disk.

save_txt

set_new_edges

Set new edges for projection of name proj (list or single projection name).

view

Set covariance matrix view, i.e. ensemble of selections to apply.

Attributes

cov

Return covariance matrix.

kwview

Current view selections.

logger

mpiattrs

MPI attributes

mpicomm

mpiroot

mpistate

shape

Return shape, i.e. tuple of length along the two dimensions.

x

Return data vectors for first and second dimensions.

copy()

Return shallow copy of self.

property cov

Return covariance matrix.

classmethod from_data(*list_data)

Estimate covariance matrix from list of data vectors.

classmethod from_files(reader, *filenames, **kwargs)

Estimate covariance matrix from list of data vector files.

Parameters
  • reader (callable) – Function that takes in a file name (string) and kwargs as input and returns a DataVector instance.

  • filenames (list) – List of data vector file names.

  • kwargs (dict) – Arguments for reader.

Returns

covariance

Return type

MockCovarianceMatrix

classmethod from_state(state, mpiroot=0, mpicomm=None)

Instantiate and initalize class with state dictionary.

get_corrcoef(*args, **kwargs)

Return correlation matrix.

get_cov(*args, **kwargs)

Return covariance.

get_header_txt(comments='#', ignore_json_errors=True)

Dump header:

  • items in attrs

  • _kwargs_view (current view selections)

Parameters
  • comments (string, default='#') – String to be prepended to the header lines.

  • ignore_json_errors (bool, default=True) – When trying to dump attrs using json, ignore errors.

Returns

header – List of strings (lines).

Return type

list

get_index(*args, permissive=True, concatenate=True, **kwargs)

Return indices corresponding to input selections (including current view).

Example

cov.get_index(xlim=(0.1,0.2),proj=0) will return index to obtain covariance of monopole between 0.1 and 0.2. cov.get_index(proj=(0,2)) will return indices to obtain covariance of monopole and quadrupole.

Parameters
  • permissive (bool, default=True) – If True, include projections which match input proj (in kwargs) for non-None attributes. For example ProjectionName(space=’power’,mode=’multipole’,proj=0) would match ell_0 (space not specified).

  • concatenate (bool, default=True) – If True, return index in full cov. Else, return list of (proj, index, start) tuples, where proj is projection name, index is the index array (with zero being the first index when no x-selection), and start the first index of the projection in full cov.

  • args (list) – Dictionary(ies) of selections (i.e. list of proj and xlim) for first and second dimensions.

  • kwargs (dict) – Dictionary of selections (i.e. list of proj and xlim).

Returns

indices – Indices in full cov for first and second dimensions, or list of (proj, index, start) tuples.

Return type

list

get_invcov(*args, block=True, inv=<function inv>, **kwargs)

Return inverse covariance.

Parameters
  • block (bool, default=False) – Invert matrix per block.

  • inv (callable, default=np.linalg.inv) – Function that inverts input matrix.

  • args (list) – Arguments for get_cov.

  • kwargs (list) – Arguments for get_cov.

get_std(*args, **kwargs)

Return standard deviation.

classmethod get_title_label()

Return title, as module.class.

get_x(concatenate=False, *args, **kwargs)

Return x-coordinates of the data vectors for the two dimensions.

get_y(concatenate=True, *args, **kwargs)

Return mean y-coordinates for the two dimensions.

property kwview

Current view selections.

classmethod load(filename, mpiroot=0, mpicomm=None)

Load class in numpy binary format from disk. If the loaded state contains __class__ and that exists in cls._registry, return instance of cls._registry[__class__] (instead of cls).

classmethod load_auto(filename, *args, **kwargs)

Load covariance matrix.

Note

Returned covariance matrix, if saved with a CovarianceMatrix-inherited class, will be an instance of that class.

Parameters
  • filename (string) – File name of covariance matrix. If ends with ‘.txt’, calls load_txt() Else (numpy binary format), calls load()

  • args (list) – Arguments for load function.

  • kwargs (dict) – Other arguments for load function.

classmethod load_txt(filename, data=None, comments='#', usecols=None, skip_rows=0, max_rows=None, mapping_header=None, columns=None, mapping_proj=None, attrs=None, **kwargs)

Load CovarianceMatrix from disk.

Note

If previously saved using save_txt(), loading the CovarianceMatrix only requires filename. In this case, the returned instance will be of the class that was used to create it (e.g. MockCovarianceMatrix) - not necessarily CovarianceMatrix.

Parameters
  • filename (string) – File name to read in.

  • data (tuple, DataVector) – Data vector(s) matching the two dimensions of the covariance matrix. If provided, ‘x’ columns of the covariance file are expected to be indices of the data vector.

  • comments (string, default='#') – Characters used to indicate the start of a comment.

  • usecols (list, default=None) – Which columns to read, with 0 being the first. If None, reads all columns. Last column must be the covariance value.

  • skip_rows (int, default=0) – Skip the first skip_rows lines, including comments.

  • max_rows (int, default=None) – Read max_rows lines of content after skip_rows lines. The default is to read all the lines.

  • mapping_header (dict, default=None) – Dictionary holding key:regex mapping or (regex, type) to provide the type. The corresponding values, read in the header, will be saved in the attrs dictionary.

  • columns (list, default=None) – Column names corresponding to usecols. Can be a tuple of column lists for two different data vectors. Columns ‘x’ and ‘y’ are used as x- and y-coordinates for each data vector.

  • mapping_proj (dict, list, default=None) – List of projection names (considered of the same size), or dictionary holding a mapping from projection specifier (e.g. ‘ell_0’) to the number of points for this projection (e.g. {'ell_0':10, 'ell_2':4} for a matrix of total size 14 x 14).

  • attrs (dict, default=None) – Attributes to save in the attrs dictionary.

  • kwargs (dict) – Other arguments for BinnedProjection.__init__().

Returns

data

Return type

DataVector

property mpiattrs

MPI attributes

noview()

Reset view.

plot(style='corr', data_styles=None, **kwargs_style)

Plot covariance matrix. See plotting.MatrixPlotStyle.

classmethod read_header_txt(file, comments='#', mapping_header=None, pattern_header=None, ignore_json_errors=True)

Read and decode header.

Parameters
  • file (list, iterator) – List of lines.

  • comments (string, default='#') – Characters used to indicate the start of a header line.

  • mapping_header (dict, default=None) – Dictionary holding key:regex mapping or (regex, type) to provide the type. Type can be unspecified (or None), in which case decoded will be tried with json, a string corresponding to __builtins__, or a callable.

  • pattern_header (string, default=None) – A regex pattern with groups corresponding to key:value.

  • ignore_json_errors (bool, default=True) – When trying to decode header values using json, ignore errors.

Returns

attrs

Return type

dict

classmethod read_title_label(line)

Decode title line, splitting module.class into (module, class). It loads module, then if class is in _registry, return corresponding class. Else return None.

rebin(proj, *args, **kwargs)

Rebin projection of name proj (list or single projection name). See BinnedStatistic.rebin() for other arguments.

save(filename)

Save class to disk.

save_auto(filename, *args, **kwargs)

Write covariance matrix to disk.

Parameters
  • filename (string) – File name of covariance matrix. If ends with ‘.txt’, calls save_txt() Else (numpy binary format), calls save()

  • args (list) – Arguments for save function.

  • kwargs (dict) – Other arguments for save function.

set_new_edges(proj, *args, **kwargs)

Set new edges for projection of name proj (list or single projection name). See BinnedStatistic.set_new_edges() for other arguments.

property shape

Return shape, i.e. tuple of length along the two dimensions.

view(*args, **kwargs)

Set covariance matrix view, i.e. ensemble of selections to apply. This will apply to output of get_ methods.

Example

cov.view(xlim=(0.1,0.2)).get_cov(proj=0) will return monopole covariance between 0.1 and 0.2.

property x

Return data vectors for first and second dimensions.