nifty8.minimization.sample_list module#

class ResidualSampleList(mean, residuals, neg, comm=None)[source]#

Bases: SampleListBase

__init__(mean, residuals, neg, comm=None)[source]#

Store samples in terms of a mean and a residual deviation thereof.

Parameters:
  • mean (nifty8.field.Field or nifty8.multi_field.MultiField) – Mean of the sample list.

  • residuals (list of nifty8.field.Field or list of nifty8.multi_field.MultiField) – List of residuals from the mean. If it is a list of MultiField, the domain of the residuals can be a subdomain of the domain of mean. This results in adding just a zero in respective MultiField entries.

  • neg (list of bool) – This list has to have the same length as residuals. If an entry is True, the respective residual is subtracted and not added.

  • comm (MPI communicator or None) – If not None, samples can be gathered across multiple MPI tasks. If None, ResidualSampleList is not a distributed object.

at(mean)[source]#

Instantiate ResidualSampleList with the same residuals as self.

The mean is updated.

Note

If self.domain is a MultiDomain, the old and new mean are combined with union beforehand. This means that only the multi field entries present in mean are updated.

Returns:

Sample list with updated mean.

Return type:

ResidualSampleList

classmethod load(file_name_base, comm=None)[source]#

Deserialize SampleList from files on disk.

Parameters:
  • file_name_base (str) – File name of the input file without extension. The actual file name will have the extension “.pickle”.

  • comm (MPI communicator or None) – If not None, each MPI task reads its own input file.

Note

file_name_base needs to be the same string that has been used for saving the SampleListBase.

classmethod load_mean(file_name_base)[source]#
local_item(i)[source]#

Return ith local sample.

save(file_name_base, overwrite=False)[source]#

Serialize SampleList and write it to disk.

Parameters:
  • file_name_base (str) – File name of the output file without extension. The actual file name will have the extension “.pickle”.

  • overwrite (bool) – Existing files are overwritten.

Note

If the instance of SampleListBase is distributed, each MPI task writes its own file.

class SampleList(samples, comm=None, domain=None)[source]#

Bases: SampleListBase

__init__(samples, comm=None, domain=None)[source]#

Store samples as a plain list.

This is a minimalist implementation of SampleListBase. It just serves as a (potentially MPI-distributed) wrapper of a list of samples.

Parameters:
  • samples (list of nifty8.field.Field or list of nifty8.multi_field.MultiField) – List of samples.

  • comm (MPI communicator or None) – If not None, samples can be gathered across multiple MPI tasks. If None, ResidualSampleList is not a distributed object.

  • domain (DomainTuple, MultiDomain or None) – Sets the domain of the SampleList. If samples is non-empty and domain is not None, domain has to coincide with the domain of the samples. Default: None.

classmethod load(file_name_base, comm=None)[source]#

Deserialize SampleList from files on disk.

Parameters:
  • file_name_base (str) – File name of the input file without extension. The actual file name will have the extension “.pickle”.

  • comm (MPI communicator or None) – If not None, each MPI task reads its own input file.

Note

file_name_base needs to be the same string that has been used for saving the SampleListBase.

local_item(i)[source]#

Return ith local sample.

save(file_name_base, overwrite=False)[source]#

Serialize SampleList and write it to disk.

Parameters:
  • file_name_base (str) – File name of the output file without extension. The actual file name will have the extension “.pickle”.

  • overwrite (bool) – Existing files are overwritten.

Note

If the instance of SampleListBase is distributed, each MPI task writes its own file.

class SampleListBase(comm, domain, n_local_samples)[source]#

Bases: object

Base class for storing lists of fields representing samples.

This class suits as a base class for storing lists of in most cases posterior samples. It is intended to be used to hold the minimization state of an inference run and comes with a variety of convenience functions like computing the mean or standard deviation of the output of a given operator over the sample list.

Parameters:
  • comm (MPI communicator or None) – If not None, SampleListBase can gather samples across multiple MPI tasks. If None, SampleListBase is not a distributed object.

  • domain (Domainoid (can be DomainTuple, MultiDomain, dict, Domain or list of Domains)) – The domain on which the samples are defined.

  • n_local_samples (int) – Number of samples present on the current MPI task.

Note

A class inheriting from SampleListBase needs to call the constructor of SampleListBase and needs to implement n_local_samples and local_item().

property MPI_master#
__init__(comm, domain, n_local_samples)[source]#
average(op=None)[source]#

Compute average over all potentially distributed samples.

Parameters:

op (callable or None) – Callable that is applied to each item in the SampleListBase before it is averaged.

Note

Calling this function involves MPI communication if comm != None.

Note

Calling this function involves allocating arrays for all samples and averaging them afterwards. If the number of local samples is big and op is not None, this leads to much temporary memory usage. If the output of op is just a Field or MultiField, sample_stat() can be used in order to compute the average memory efficiently.

property comm#

The communicator used for the SampleListBase.

Type:

MPI communicator or None

property domain#

the domain on which the samples are defined.

Type:

DomainTuple or MultiDomain

iterator(op=None)[source]#

Return iterator over all potentially distributed samples.

Parameters:

op (callable or None) – Callable that is applied to each item in the SampleListBase before it is returned. Can be an Operator or any other callable that takes a Field as an input. Default: None.

Note

Calling this function involves MPI communication if comm != None.

classmethod load(file_name_base, comm=None)[source]#

Deserialize SampleList from files on disk.

Parameters:
  • file_name_base (str) – File name of the input file without extension. The actual file name will have the extension “.pickle”.

  • comm (MPI communicator or None) – If not None, each MPI task reads its own input file.

Note

file_name_base needs to be the same string that has been used for saving the SampleListBase.

local_item(i)[source]#

Return ith local sample.

local_iterator()[source]#

Return an iterator over all local samples.

property n_local_samples#

Number of local samples.

Type:

int

property n_samples#

Return number of samples across all MPI tasks.

sample_stat(op=None)[source]#

Compute mean and variance of samples after applying op.

Parameters:

op (callable or None) – Callable that is applied to each item in the SampleListBase before it is used to compute mean and variance.

Returns:

A tuple with two items: the mean and the variance.

Return type:

tuple

save(file_name_base, overwrite=False)[source]#

Serialize SampleList and write it to disk.

Parameters:
  • file_name_base (str) – File name of the output file without extension. The actual file name will have the extension “.pickle”.

  • overwrite (bool) – Existing files are overwritten.

Note

If the instance of SampleListBase is distributed, each MPI task writes its own file.

save_to_fits(file_name_base, op=None, samples=False, mean=False, std=False, overwrite=False)[source]#

Write sample list to FITS file.

This function writes properties of a sample list to a FITS file. This is supported if and only if the target of op is a two-dimensional RGSpace.

Parameters:
  • file_name_base (str) – File name base of output FITS file, i.e. without .fits extension.

  • op (callable or None) – Callable that is applied to each item in the SampleListBase before it is returned. Can be an Operator or any other callable that takes a Field as an input. Default: None.

  • samples (bool) – If True, samples are written into hdf5 file.

  • mean (bool) – If True, mean of samples is written into hdf5 file.

  • std (bool) – If True, standard deviation of samples is written into hdf5 file.

  • overwrite (bool) – If True, a potentially existing file with the same file name as file_name, is overwritten.

save_to_hdf5(file_name, op=None, samples=False, mean=False, std=False, overwrite=False)[source]#

Write sample list to HDF5 file.

This function writes sample lists to HDF5 files that contain two groups: samples and stats. samples contain the sublabels 0, 1, … that number the labels and stats contains the sublabels mean and standard deviation. If self.domain is an instance of MultiDomain, these sublabels refer themselves to subgroups. For Field, the sublabel refers to an HDF5 data set.

If quanitities are not requested (e.g. by setting mean=False), the respective sublabels are not present in the HDF5 file.

If op is an Operator, the operator string representation, its domain and its target are written to the attributes of the HDF5 file.

Parameters:
  • file_name (str) – File name of output hdf5 file.

  • op (callable or None) – Callable that is applied to each item in the SampleListBase before it is returned. Can be an Operator or any other callable that takes a Field as an input. Default: None.

  • samples (bool) – If True, samples are written into hdf5 file.

  • mean (bool) – If True, mean of samples is written into hdf5 file.

  • std (bool) – If True, standard deviation of samples is written into hdf5 file.

  • overwrite (bool) – If True, a potentially existing file with the same file name as file_name, is overwritten.