Commit 1046cb1d authored by Martin Reinecke's avatar Martin Reinecke

Merge branch 'NIFTy_5' into adjust_variances

parents 5c4d0aba c26a26ea
Pipeline #47210 passed with stages
in 8 minutes and 12 seconds
...@@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y \ ...@@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y \
# more optional NIFTy dependencies # more optional NIFTy dependencies
&& pip3 install pyfftw \ && pip3 install pyfftw \
&& pip3 install git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git \ && pip3 install git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git \
&& pip3 install git+https://gitlab.mpcdf.mpg.de/ift/nifty_gridder.git \
&& pip3 install jupyter \ && pip3 install jupyter \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
......
...@@ -52,6 +52,8 @@ Optional dependencies: ...@@ -52,6 +52,8 @@ Optional dependencies:
- [pyFFTW](https://pypi.python.org/pypi/pyFFTW) for faster Fourier transforms - [pyFFTW](https://pypi.python.org/pypi/pyFFTW) for faster Fourier transforms
- [pyHealpix](https://gitlab.mpcdf.mpg.de/ift/pyHealpix) (for harmonic - [pyHealpix](https://gitlab.mpcdf.mpg.de/ift/pyHealpix) (for harmonic
transforms involving domains on the sphere) transforms involving domains on the sphere)
- [nifty_gridder](https://gitlab.mpcdf.mpg.de/ift/nifty_gridder) (for radio
interferometry responses)
- [mpi4py](https://mpi4py.scipy.org) (for MPI-parallel execution) - [mpi4py](https://mpi4py.scipy.org) (for MPI-parallel execution)
- [matplotlib](https://matplotlib.org/) (for field plotting) - [matplotlib](https://matplotlib.org/) (for field plotting)
...@@ -97,6 +99,10 @@ Support for spherical harmonic transforms is added via: ...@@ -97,6 +99,10 @@ Support for spherical harmonic transforms is added via:
pip3 install --user git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git pip3 install --user git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git
Support for the radio interferometry gridder is added via:
pip3 install git+https://gitlab.mpcdf.mpg.de/ift/nifty_gridder.git
MPI support is added via: MPI support is added via:
sudo apt-get install python3-mpi4py sudo apt-get install python3-mpi4py
......
from time import time
import matplotlib.pyplot as plt
import numpy as np
import nifty5 as ift
ift.fft.enable_fftw()
np.random.seed(40)
N0s, a0s, b0s, c0s = [], [], [], []
N1s, a1s, b1s, c1s = [], [], [], []
for ii in range(10, 23):
nu = 1024
nv = 1024
N = int(2**ii)
print('N = {}'.format(N))
uv = np.random.rand(N, 2) - 0.5
vis = np.random.randn(N) + 1j*np.random.randn(N)
uvspace = ift.RGSpace((nu, nv))
visspace = ift.UnstructuredDomain(N)
img = np.random.randn(nu*nv)
img = img.reshape((nu, nv))
img = ift.from_global_data(uvspace, img)
t0 = time()
GM = ift.GridderMaker(uvspace, eps=1e-7)
idx = GM.getReordering(uv)
uv = uv[idx]
vis = vis[idx]
vis = ift.from_global_data(visspace, vis)
op = GM.getFull(uv).adjoint
t1 = time()
op(img).to_global_data()
t2 = time()
op.adjoint(vis).to_global_data()
t3 = time()
N0s.append(N)
a0s.append(t1 - t0)
b0s.append(t2 - t1)
c0s.append(t3 - t2)
t0 = time()
op = ift.NFFT(uvspace, uv)
t1 = time()
op(img).to_global_data()
t2 = time()
op.adjoint(vis).to_global_data()
t3 = time()
N1s.append(N)
a1s.append(t1 - t0)
b1s.append(t2 - t1)
c1s.append(t3 - t2)
print('Measure rest operator')
sc = ift.StatCalculator()
op = GM.getRest().adjoint
for _ in range(10):
t0 = time()
res = op(img)
sc.add(time() - t0)
t_fft = sc.mean
print('FFT shape', res.shape)
plt.scatter(N0s, a0s, label='Gridder mr')
plt.scatter(N1s, a1s, marker='^', label='NFFT')
plt.legend()
# no idea why this is necessary, but if it is omitted, the range is wrong
plt.ylim(min(a0s+a1s), max(a0s+a1s))
plt.ylabel('time [s]')
plt.title('Initialization')
plt.loglog()
plt.savefig('bench0.png')
plt.close()
plt.scatter(N0s, b0s, color='k', marker='^', label='Gridder mr times')
plt.scatter(N1s, b1s, color='r', marker='^', label='NFFT times')
plt.scatter(N0s, c0s, color='k', label='Gridder mr adjoint times')
plt.scatter(N1s, c1s, color='r', label='NFFT adjoint times')
plt.axhline(sc.mean, label='FFT')
plt.axhline(sc.mean + np.sqrt(sc.var))
plt.axhline(sc.mean - np.sqrt(sc.var))
plt.legend()
plt.ylabel('time [s]')
plt.title('Apply')
plt.loglog()
plt.savefig('bench1.png')
plt.close()
...@@ -45,13 +45,6 @@ def make_random_mask(): ...@@ -45,13 +45,6 @@ def make_random_mask():
return mask.to_global_data() return mask.to_global_data()
def mask_to_nan(mask, field):
# Set masked pixels to nan for plotting
masked_data = field.local_data.copy()
masked_data[mask.local_data == 0] = np.nan
return ift.from_local_data(field.domain, masked_data)
if __name__ == '__main__': if __name__ == '__main__':
np.random.seed(42) np.random.seed(42)
...@@ -64,7 +57,7 @@ if __name__ == '__main__': ...@@ -64,7 +57,7 @@ if __name__ == '__main__':
if mode == 0: if mode == 0:
# One-dimensional regular grid # One-dimensional regular grid
position_space = ift.RGSpace([1024]) position_space = ift.RGSpace([1024])
mask = np.ones(position_space.shape) mask = np.zeros(position_space.shape)
elif mode == 1: elif mode == 1:
# Two-dimensional regular grid with checkerboard mask # Two-dimensional regular grid with checkerboard mask
position_space = ift.RGSpace([128, 128]) position_space = ift.RGSpace([128, 128])
...@@ -101,23 +94,22 @@ if __name__ == '__main__': ...@@ -101,23 +94,22 @@ if __name__ == '__main__':
# Build instrument response consisting of a discretization, mask # Build instrument response consisting of a discretization, mask
# and harmonic transformaion # and harmonic transformaion
# Data is defined on a geometry-free space, thus the geometry is removed
GR = ift.GeometryRemover(position_space)
# Masking operator to model that parts of the field have not been observed # Masking operator to model that parts of the field have not been observed
mask = ift.Field.from_global_data(position_space, mask) mask = ift.Field.from_global_data(position_space, mask)
Mask = ift.DiagonalOperator(mask) Mask = ift.MaskOperator(mask)
# The response operator consists of # The response operator consists of
# - an harmonic transform (to get to image space) # - a harmonic transform (to get to image space)
# - the application of the mask # - the application of the mask
# - the removal of geometric information # - the removal of geometric information
# The removal of geometric information is included in the MaskOperator
# it can also be implemented with a GeometryRemover
# Operators can be composed either with parenthesis # Operators can be composed either with parenthesis
R = GR(Mask(HT)) R = Mask(HT)
# or with @ # or with @
R = GR @ Mask @ HT R = Mask @ HT
data_space = GR.target data_space = R.target
# Set the noise covariance N # Set the noise covariance N
noise = 5. noise = 5.
...@@ -144,16 +136,17 @@ if __name__ == '__main__': ...@@ -144,16 +136,17 @@ if __name__ == '__main__':
filename = "getting_started_1_mode_{}.png".format(mode) filename = "getting_started_1_mode_{}.png".format(mode)
if rg and len(position_space.shape) == 1: if rg and len(position_space.shape) == 1:
plot.add( plot.add(
[HT(MOCK_SIGNAL), GR.adjoint(data), [HT(MOCK_SIGNAL), Mask.adjoint(data),
HT(m)], HT(m)],
label=['Mock signal', 'Data', 'Reconstruction'], label=['Mock signal', 'Data', 'Reconstruction'],
alpha=[1, .3, 1]) alpha=[1, .3, 1])
plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals') plot.add(Mask.adjoint(Mask(HT(m - MOCK_SIGNAL))), title='Residuals')
plot.output(nx=2, ny=1, xsize=10, ysize=4, name=filename) plot.output(nx=2, ny=1, xsize=10, ysize=4, name=filename)
else: else:
plot.add(HT(MOCK_SIGNAL), title='Mock Signal') plot.add(HT(MOCK_SIGNAL), title='Mock Signal')
plot.add(mask_to_nan(mask, (GR(Mask)).adjoint(data)), title='Data') plot.add(Mask.adjoint(data), title='Data')
plot.add(HT(m), title='Reconstruction') plot.add(HT(m), title='Reconstruction')
plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals') plot.add(Mask.adjoint(Mask(HT(m) - HT(MOCK_SIGNAL))),
title='Residuals')
plot.output(nx=2, ny=2, xsize=10, ysize=10, name=filename) plot.output(nx=2, ny=2, xsize=10, ysize=10, name=filename)
print("Saved results as '{}'.".format(filename)) print("Saved results as '{}'.".format(filename))
...@@ -77,7 +77,7 @@ The additional methods are specified in the abstract class ...@@ -77,7 +77,7 @@ The additional methods are specified in the abstract class
provide information about the domain's pixel volume(s) and its total volume. provide information about the domain's pixel volume(s) and its total volume.
- The property :attr:`~StructuredDomain.harmonic` specifies whether a domain - The property :attr:`~StructuredDomain.harmonic` specifies whether a domain
is harmonic (i.e. describes a frequency space) or not is harmonic (i.e. describes a frequency space) or not
- Iff the domain is harmonic, the methods - If (and only if) the domain is harmonic, the methods
:meth:`~StructuredDomain.get_k_length_array`, :meth:`~StructuredDomain.get_k_length_array`,
:meth:`~StructuredDomain.get_unique_k_lengths`, and :meth:`~StructuredDomain.get_unique_k_lengths`, and
:meth:`~StructuredDomain.get_fft_smoothing_kernel_function` provide absolute :meth:`~StructuredDomain.get_fft_smoothing_kernel_function` provide absolute
...@@ -90,13 +90,16 @@ NIFTy comes with several concrete subclasses of :class:`StructuredDomain`: ...@@ -90,13 +90,16 @@ NIFTy comes with several concrete subclasses of :class:`StructuredDomain`:
- :class:`~rg_space.RGSpace` represents a regular Cartesian grid with an arbitrary - :class:`~rg_space.RGSpace` represents a regular Cartesian grid with an arbitrary
number of dimensions, which is supposed to be periodic in each dimension. number of dimensions, which is supposed to be periodic in each dimension.
- :class:`~log_rg_space.LogRGSpace` implements a Cartesian grid with logarithmically
spaced bins and an arbitrary number of dimensions.
- :class:`~hp_space.HPSpace` and :class:`~gl_space.GLSpace` describe pixelisations of the - :class:`~hp_space.HPSpace` and :class:`~gl_space.GLSpace` describe pixelisations of the
2-sphere; their counterpart in harmonic space is :class:`~lm_space.LMSpace`, which 2-sphere; their counterpart in harmonic space is :class:`~lm_space.LMSpace`, which
contains spherical harmonic coefficients. contains spherical harmonic coefficients.
- :class:`~power_space.PowerSpace` is used to describe one-dimensional power spectra. - :class:`~power_space.PowerSpace` is used to describe one-dimensional power spectra.
Among these, :class:`~rg_space.RGSpace` can be harmonic or not (depending on Among these, :class:`~rg_space.RGSpace` and :class:`~log_rg_space.LogRGSpace` can
constructor arguments), :class:`~gl_space.GLSpace`, :class:`~hp_space.HPSpace`, be harmonic or not (depending on constructor arguments),
:class:`~gl_space.GLSpace`, :class:`~hp_space.HPSpace`,
and :class:`~power_space.PowerSpace` are pure position domains (i.e. and :class:`~power_space.PowerSpace` are pure position domains (i.e.
nonharmonic), and :class:`~lm_space.LMSpace` is always harmonic. nonharmonic), and :class:`~lm_space.LMSpace` is always harmonic.
...@@ -113,18 +116,20 @@ Some examples are: ...@@ -113,18 +116,20 @@ Some examples are:
- sky emission depending on location and energy. This could be represented by a - sky emission depending on location and energy. This could be represented by a
product of an :class:`~hp_space.HPSpace` (for location) with an product of an :class:`~hp_space.HPSpace` (for location) with an
:class:`~rg_space.RGSpace` (for energy). :class:`~rg_space.RGSpace` (for energy).
- a polarized field, which could be modeled as a product of any structured - a polarized field, which could be modelled as a product of any structured
domain (representing location) with a four-element domain (representing location) with a four-element
:class:`~unstructured_domain.UnstructuredDomain` holding Stokes I, Q, U and V components. :class:`~unstructured_domain.UnstructuredDomain` holding Stokes I, Q, U and V components.
- a model for the sky emission, which holds both the current realization - a model for the sky emission, which holds both the current realisation
(on a harmonic domain) and a few inferred model parameters (e.g. on an (on a harmonic domain) and a few inferred model parameters (e.g. on an
unstructured grid). unstructured grid).
.. currentmodule:: nifty5 .. currentmodule:: nifty5
Consequently, NIFTy defines a class called :class:`~domain_tuple.DomainTuple` Consequently, NIFTy defines a class called :class:`~domain_tuple.DomainTuple`
holding a sequence of :class:`~domains.domain.Domain` objects, which is used to holding a sequence of :class:`~domains.domain.Domain` objects. The full domain is
specify full field domains. In principle, a :class:`~domain_tuple.DomainTuple` specified as the product of all elementary domains. Thus, an instance of
:class:`~domain_tuple.DomainTuple` would be suitable to describe the first two
examples above. In principle, a :class:`~domain_tuple.DomainTuple`
can even be empty, which implies that the field living on it is a scalar. can even be empty, which implies that the field living on it is a scalar.
A :class:`~domain_tuple.DomainTuple` supports iteration and indexing, and also A :class:`~domain_tuple.DomainTuple` supports iteration and indexing, and also
...@@ -134,7 +139,10 @@ provides the properties :attr:`~domain_tuple.DomainTuple.shape` and ...@@ -134,7 +139,10 @@ provides the properties :attr:`~domain_tuple.DomainTuple.shape` and
An aggregation of several :class:`~domain_tuple.DomainTuple` s, each member An aggregation of several :class:`~domain_tuple.DomainTuple` s, each member
identified by a name, is described by the :class:`~multi_domain.MultiDomain` identified by a name, is described by the :class:`~multi_domain.MultiDomain`
class. class. In contrast to a :class:`~domain_tuple.DomainTuple` a
:class:`~multi_domain.MultiDomain` is a collection and does not define the
product space of its elements. It would be the adequate space to use in the
last of the above examples.
Fields Fields
====== ======
...@@ -152,12 +160,22 @@ Usually, the array is stored in the form of a ``numpy.ndarray``, but for very ...@@ -152,12 +160,22 @@ Usually, the array is stored in the form of a ``numpy.ndarray``, but for very
resource-intensive tasks NIFTy also provides an alternative storage method to resource-intensive tasks NIFTy also provides an alternative storage method to
be used with distributed memory processing. be used with distributed memory processing.
Fields support a wide range of arithmetic operations, either involving two Fields support a wide range of arithmetic operations, either involving
fields with equal domains, or a field and a scalar. two fields of equal domains or a field and a scalar. Arithmetic operations are
performed point-wise, and the returned field has the same domain as the input field(s).
Available operators are addition ("+"), subtraction ("-"),
multiplication ("*"), division ("/"), floor division ("//") and
exponentiation ("**"). Inplace operators ("+=", etc.) are not supported.
Further, boolean operators, performing a point-wise comparison of a field with
either another field of equal domain or a scalar, are available as well. These
include equals ("=="), not equals ("!="), less ("<"), less or equal ("<="),
greater (">") and greater or equal (">=). The domain of the field returned equals
that of the input field(s), while the stored data is of boolean type.
Contractions (like summation, integration, minimum/maximum, computation of Contractions (like summation, integration, minimum/maximum, computation of
statistical moments) can be carried out either over an entire field (producing statistical moments) can be carried out either over an entire field (producing
a scalar result) or over sub-domains (resulting in a field defined on a smaller a scalar result) or over sub-domains (resulting in a field defined on a smaller
domain). Scalar products of two fields can also be computed easily. domain). Scalar products of two fields can also be computed easily as well.
See the documentation of :class:`~field.Field` for details. See the documentation of :class:`~field.Field` for details.
There is also a set of convenience functions to generate fields with constant There is also a set of convenience functions to generate fields with constant
...@@ -215,8 +233,8 @@ specific inference problems. Currently these are: ...@@ -215,8 +233,8 @@ specific inference problems. Currently these are:
- :class:`~smooth_linear_amplitude.SLAmplitude`, which returns a smooth power spectrum. - :class:`~smooth_linear_amplitude.SLAmplitude`, which returns a smooth power spectrum.
- :class:`~inverse_gamma_operator.InverseGammaOperator`, which models point sources which are - :class:`~inverse_gamma_operator.InverseGammaOperator`, which models point sources which are
distributed according to a inverse-gamma distribution. distributed according to a inverse-gamma distribution.
- :class:`~correlated_fields.CorrelatedField`, which models a diffuse log-normal field. It takes an - :class:`~correlated_fields.CorrelatedField`, which models a diffuse field whose correlation
amplitude operator to specify the correlation structure of the field. structure is described by an amplitude operator.
Linear Operators Linear Operators
...@@ -351,13 +369,34 @@ tackling new IFT problems. An example of concrete energy classes delivered with ...@@ -351,13 +369,34 @@ tackling new IFT problems. An example of concrete energy classes delivered with
NIFTy5 is :class:`~minimization.quadratic_energy.QuadraticEnergy` (with NIFTy5 is :class:`~minimization.quadratic_energy.QuadraticEnergy` (with
position-independent metric, mainly used with conjugate gradient minimization). position-independent metric, mainly used with conjugate gradient minimization).
For MGVI, NIFTy provides the :class:`~energy.Energy` subclass
:class:`~minimization.metric_gaussian_kl.MetricGaussianKL`,
which computes the sampled estimated of the KL divergence, its gradient and the
Fisher metric. The constructor of
:class:`~minimization.metric_gaussian_kl.MetricGaussianKL` requires an instance
of :class:`~operators.energy_operators.StandardHamiltonian`, an operator to
compute the negative log-likelihood of the problem in standardized coordinates
at a given position in parameter space.
Finally, the :class:`~operators.energy_operators.StandardHamiltonian`
can be constructed from the likelihood, represented by an
:class:`~operators.energy_operators.EnergyOperator` instance.
Several commonly used forms of the likelihoods are already provided in
NIFTy, such as :class:`~operators.energy_operators.GaussianEnergy`,
:class:`~operators.energy_operators.PoissonianEnergy`,
:class:`~operators.energy_operators.InverseGammaLikelihood` or
:class:`~operators.energy_operators.BernoulliEnergy`, but the user
is free to implement any likelihood customized to the problem at hand.
The demo code `demos/getting_started_3.py` illustrates how to set up an energy
functional for MGVI and minimize it.
Iteration control Iteration control
----------------- -----------------
.. currentmodule:: nifty5.minimization.iteration_controllers .. currentmodule:: nifty5.minimization.iteration_controllers
Iterative minimization of an energy reqires some means of Iterative minimization of an energy requires some means of
checking the quality of the current solution estimate and stopping once checking the quality of the current solution estimate and stopping once
it is sufficiently accurate. In case of numerical problems, the iteration needs it is sufficiently accurate. In case of numerical problems, the iteration needs
to be terminated as well, returning a suitable error description. to be terminated as well, returning a suitable error description.
...@@ -370,12 +409,12 @@ the minimization or return the current estimate indicating convergence or ...@@ -370,12 +409,12 @@ the minimization or return the current estimate indicating convergence or
failure. failure.
Sensible stopping criteria can vary significantly with the problem being Sensible stopping criteria can vary significantly with the problem being
solved; NIFTy provides one concrete sub-class of :class:`IterationController` solved; NIFTy provides a concrete sub-class of :class:`IterationController`
called :class:`GradientNormController`, which should be appropriate in many called :class:`GradientNormController`, which should be appropriate in many
circumstances, but users have complete freedom to implement custom circumstances. A full list of the available :class:`IterationController` s
in NIFTy can be found below, but users have complete freedom to implement custom
:class:`IterationController` sub-classes for their specific applications. :class:`IterationController` sub-classes for their specific applications.
Minimization algorithms Minimization algorithms
----------------------- -----------------------
...@@ -407,10 +446,12 @@ generally usable concrete implementations: ...@@ -407,10 +446,12 @@ generally usable concrete implementations:
:class:`~descent_minimizers.VL_BFGS`. Of these algorithms, only :class:`~descent_minimizers.VL_BFGS`. Of these algorithms, only
:class:`~descent_minimizers.NewtonCG` requires the energy object to provide :class:`~descent_minimizers.NewtonCG` requires the energy object to provide
a :attr:`~energy.Energy.metric` property, the others only need energy values and a :attr:`~energy.Energy.metric` property, the others only need energy values and
gradients. gradients. Further available descent minimizers are
:class:`~descent_minimizers.RelaxedNewton`
and :class:`~descent_minimizers.SteepestDescent`.
The flexibility of NIFTy's design allows using externally provided minimizers. The flexibility of NIFTy's design allows using externally provided minimizers.
With only small effort, adapters for two SciPy minimizers were written; they are With only small effort, adaptors for two SciPy minimizers were written; they are
available under the names :class:`~scipy_minimizer.ScipyCG` and available under the names :class:`~scipy_minimizer.ScipyCG` and
:class:`~scipy_minimizer.L_BFGS_B`. :class:`~scipy_minimizer.L_BFGS_B`.
...@@ -438,3 +479,16 @@ This is accomplished by minimizing a suitable ...@@ -438,3 +479,16 @@ This is accomplished by minimizing a suitable
with the :class:`~minimization.conjugate_gradient.ConjugateGradient` with the :class:`~minimization.conjugate_gradient.ConjugateGradient`
algorithm. An example is provided in algorithm. An example is provided in
:func:`~library.wiener_filter_curvature.WienerFilterCurvature`. :func:`~library.wiener_filter_curvature.WienerFilterCurvature`.
Posterior analysis and visualization
------------------------------------
After the minimization of an energy functional has converged, samples can be drawn
from the posterior distribution at the current position to investigate the result.
The probing module offers class called :class:`~probing.StatCalculator`
which allows to evaluate the :attr:`~probing.StatCalculator.mean` and the unbiased
variance :attr:`~probing.StatCalculator.var` of these samples.
Fields can be visualized using the :class:`~plot.Plot` class, which invokes
matplotlib for plotting.
...@@ -35,6 +35,24 @@ Support for spherical harmonic transforms is added via:: ...@@ -35,6 +35,24 @@ Support for spherical harmonic transforms is added via::
pip3 install --user git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git pip3 install --user git+https://gitlab.mpcdf.mpg.de/ift/pyHealpix.git
Support for the radio interferometry gridder is added via:
pip3 install git+https://gitlab.mpcdf.mpg.de/ift/nifty_gridder.git
MPI support is added via:: MPI support is added via::
sudo apt-get install python3-mpi4py sudo apt-get install python3-mpi4py
NIFTy documentation is provided by Sphinx. To build the documentation::
sudo apt-get install python3-sphinx-rtd-theme dvipng
cd <nifty_directory>
sh docs/generate.sh
To view the documentation in firefox::
firefox docs/build/index.html
(Note: Make sure that you reinstall nifty after each change since sphinx
imports nifty from the Python path.)
Discretization and Volume in NIFTy Discretisation and Volume in NIFTy
================================== ==================================
.. note:: Some of this discussion is rather technical and may be skipped in a first read-through. .. note:: Some of this discussion is rather technical and may be skipped in a first read-through.
...@@ -160,15 +160,21 @@ Often, log-likelihoods contain integrals over the quantity of interest :math:`s` ...@@ -160,15 +160,21 @@ Often, log-likelihoods contain integrals over the quantity of interest :math:`s`
\int_\Omega \text{d}x\, s(x) \approx \sum_i s^i\int_{\Omega_i}\text{d}x\, 1 \int_\Omega \text{d}x\, s(x) \approx \sum_i s^i\int_{\Omega_i}\text{d}x\, 1
Here the domain of the integral :math:`\Omega = \dot{\bigcup_q} \; \Omega_i` is the disjoint union over smaller :math:`\Omega_i`, e.g. the pixels of the space, and :math:`s_i` is the discretized field value on the :math:`i`-th pixel. Here the domain of the integral :math:`\Omega = \dot{\bigcup_q} \; \Omega_i` is the disjoint union over smaller :math:`\Omega_i`, e.g. the pixels of the space, and :math:`s_i` is the discretised field value on the :math:`i`-th pixel.
This introduces the weighting :math:`V_i=\int_{\Omega_i}\text{d}x\, 1`, also called the volume factor, a property of the space. This introduces the weighting :math:`V_i=\int_{\Omega_i}\text{d}x\, 1`, also called the volume factor, a property of the space.
NIFTy aids you in constructing your own log-likelihood by providing methods like :func:`~field.Field.weight`, which weights all pixels of a field with their corresponding volume. NIFTy aids you in constructing your own log-likelihood by providing methods like :func:`~field.Field.weight`, which weights all pixels of a field with their corresponding volume.
An integral over a :class:`~field.Field` :code:`s` can be performed by calling :code:`s.weight(1).sum()`, which is equivalent to :code:`s.integrate()`. An integral over a :class:`~field.Field` :code:`s` can be performed by calling :code:`s.weight(1).sum()`, which is equivalent to :code:`s.integrate()`.
Volume factors are also applied automatically in the following places: Volume factors are also applied automatically in the following places:
- :class:`~operators.harmonic_operators.FFTOperator` as well as all other harmonic operators. Here the zero mode of the transformed field is the integral over the original field, thus the whole field is weighted once. - :class:`~operators.harmonic_operators.FFTOperator` as well as all other harmonic operators.
- some response operators, such as the :class:`~library.los_response.LOSResponse`. In this operator a line integral is descritized, so a 1-dimensional volume factor is applied. Here the zero mode of the transformed field is the integral over the original field, thus the whole field is weighted once.
- In :class:`~library.correlated_fields.CorrelatedField` as well :class:`~library.correlated_fields.MfCorrelatedField`, the field is multiplied by the square root of the total volume in configuration space. This ensures that the same field reconstructed over a larger domain has the same variance in position space in the limit of infinite resolution. It also ensures that power spectra in NIFTy behave according to the definition of a power spectrum, namely the power of a k-mode is the expectation of the k-mode square, divided by the volume of the space. - Some response operators, such as the :class:`~library.los_response.LOSResponse`.
In this operator a line integral is discretised, so a 1-dimensional volume factor is applied.
- In :class:`~library.correlated_fields.CorrelatedField` as well as :class:`~library.correlated_fields.MfCorrelatedField`.
Both describe fields with a smooth, a priori unknown correlation structure specified by a power spectrum.
The field is multiplied by the square root of the total volume of it domain's harmonic counterpart.
This ensures that the same power spectrum can be used regardless of the chosen resolution, provided the total volume of the space remains the same.
It also guarantees that the power spectra in NIFTy behave according to their definition, i.e. the power of a mode :math:`s_k` is the expectation value of that mode squared, divided by the volume of its space :math:`P(k) = \left\langle s_k^2 \right\rangle / V_k`.
Note that in contrast to some older versions of NIFTy, the dot product :code:`s.vdot(t)` of fields does **not** apply a volume factor, but instead just sums over the field components, Note that in contrast to some older versions of NIFTy, the dot product :code:`s.vdot(t)` of fields does **not** apply a volume factor, but instead just sums over the field components,
......
...@@ -74,7 +74,8 @@ from .minimization.metric_gaussian_kl import MetricGaussianKL ...@@ -74,7 +74,8 @@ from .minimization.metric_gaussian_kl import MetricGaussianKL
from .sugar import * from .sugar import *
from .plot import Plot from .plot import Plot
from .library.smooth_linear_amplitude import SLAmplitude, CepstrumOperator from .library.smooth_linear_amplitude import (
SLAmplitude, LinearSLAmplitude, CepstrumOperator)
from .library.inverse_gamma_operator import InverseGammaOperator from .library.inverse_gamma_operator import InverseGammaOperator
from .library.los_response import LOSResponse from .library.los_response import LOSResponse
from .library.dynamic_operator import (dynamic_operator, from .library.dynamic_operator import (dynamic_operator,
...@@ -86,6 +87,7 @@ from .library.correlated_fields import CorrelatedField, MfCorrelatedField ...@@ -86,6 +87,7 @@ from .library.correlated_fields import CorrelatedField, MfCorrelatedField
from .library.adjust_variances import (make_adjust_variances_hamiltonian, from .library.adjust_variances import (make_adjust_variances_hamiltonian,
do_adjust_variances) do_adjust_variances)
from .library.nfft import NFFT from .library.nfft import NFFT
from .library.gridder import GridderMaker
from . import extra from . import extra
......
...@@ -149,7 +149,7 @@ def ensure_default_distributed(arr): ...@@ -149,7 +149,7 @@ def ensure_default_distributed(arr):
def absmax(arr): def absmax(arr):
return np.linalg.norm(arr.rehape(-1), ord=np.inf) return np.linalg.norm(arr.reshape(-1), ord=np.inf)
def norm(arr, ord=2): def norm(arr, ord=2):
......
...@@ -33,7 +33,8 @@ def _assert_allclose(f1, f2, atol, rtol): ...@@ -33,7 +33,8 @@ def _assert_allclose(f1, f2, atol, rtol):
_assert_allclose(val, f2[key], atol=atol, rtol=rtol) _assert_allclose(val, f2[key], atol=atol, rtol=rtol)
def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol): def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol,
only_r_linear):
needed_cap = op.TIMES | op.ADJOINT_TIMES needed_cap = op.TIMES | op.ADJOINT_TIMES
if (op.capability & needed_cap) != needed_cap: if (op.capability & needed_cap) != needed_cap:
return return
...@@ -41,6 +42,8 @@ def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol): ...@@ -41,6 +42,8 @@ def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol):
f2 = from_random("normal", op.target, dtype=target_dtype) f2 = from_random("normal", op.target, dtype=target_dtype)
res1 = f1.vdot(op.adjoint_times(f2)) res1 = f1.vdot(op.adjoint_times(f2))
res2 = op.times(f1).vdot(f2) res2 = op.times(f1).vdot(f2)
if only_r_linear:
res1, res2 = res1.real, res2.real
np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol) np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)
...@@ -57,8 +60,10 @@ def _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol): ...@@ -57,8 +60,10 @@ def _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol):
_assert_allclose(res, foo, atol=atol, rtol=rtol) _assert_allclose(res, foo, atol=atol, rtol=rtol)
def _full_implementation(op, domain_dtype, target_dtype, atol, rtol): def _full_implementation(op, domain_dtype, target_dtype, atol, rtol,
_adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol) only_r_linear):
_adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol,
only_r_linear)
_inverse_implementation(op, domain_dtype, target_dtype, atol, rtol) _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol)
...@@ -72,7 +77,7 @@ def _check_linearity(op, domain_dtype, atol, rtol): ...@@ -72,7 +77,7 @@ def _check_linearity(op, domain_dtype, atol, rtol):
def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64, def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
atol=0, rtol=1e-7): atol=0, rtol=1e-7, only_r_linear=False):
""" """
Checks an operator for algebraic consistency of its capabilities. Checks an operator for algebraic consistency of its capabilities.
...@@ -98,15 +103,21 @@ def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64, ...@@ -98,15 +103,21 @@ def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
Relative tolerance for the check. If atol is specified, Relative tolerance for the check. If atol is specified,
then satisfying any tolerance will let the check pass. then satisfying any tolerance will let the check pass.
Default: 0. Default: 0.
only_r_linear: bool
set to True if the operator is only R-linear, not C-linear.
This will relax the adjointness test accordingly.
""" """
if not isinstance(op, LinearOperator): if not isinstance(op, LinearOperator):
raise TypeError('This test tests only linear operators.') raise TypeError('This test tests only linear operators.')
_check_linearity(op, domain_dtype, atol, rtol) _check_linearity(op, domain_dtype, atol, rtol)
_full_implementation(op, domain_dtype, target_dtype, atol, rtol) _full_implementation(op, domain_dtype, target_dtype, atol, rtol,
_full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol) only_r_linear)
_full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol) _full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol,
only_r_linear)
_full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol,
only_r_linear)
_full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol, _full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol,
rtol) rtol, only_r_linear)
def _get_acceptable_location(op, loc, lin):