code.rst 21.6 KB
Newer Older
1

Martin Reinecke's avatar
Martin Reinecke committed
2
=============
3
4
Code Overview
=============
5

Martin Reinecke's avatar
Martin Reinecke committed
6
7
8
9

Executive summary
=================

10
11
The fundamental building blocks required for IFT computations are best
recognized from a large distance, ignoring all technical details.
12

13
14
From such a perspective,

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
15
- IFT problems largely consist of the combination of several high dimensional
16
  *minimization* problems.
Philipp Arras's avatar
Philipp Arras committed
17
- Within NIFTy, *operators* are used to define the characteristic equations and
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
18
  properties of the problems.
Martin Reinecke's avatar
Martin Reinecke committed
19
20
- The equations are built mostly from the application of *linear operators*,
  but there may also be nonlinear functions involved.
21
- The unknowns in the equations represent either continuous physical *fields*,
Martin Reinecke's avatar
Martin Reinecke committed
22
23
24
25
  or they are simply individual measured *data points*.
- Discretized *fields* have geometrical information (like locations and volume
  elements) associated with every entry; this information is called the field's
  *domain*.
26
27
28
29
30
31
32
33
34
35
36
37
38

In the following sections, the concepts briefly presented here will be
discussed in more detail; this is done in reversed order of their introduction,
to avoid forward references.


Domains
=======


Abstract base class
-------------------

Martin Reinecke's avatar
Martin Reinecke committed
39
.. currentmodule:: nifty7.domains.domain
40

Martin Reinecke's avatar
Martin Reinecke committed
41
One of the fundamental building blocks of the NIFTy7 framework is the *domain*.
42
Its required capabilities are expressed by the abstract :py:class:`Domain` class.
43
44
45
46
47
48
49
50
51
52
53
54
A domain must be able to answer the following queries:

- its total number of data entries (pixels), which is accessible via the
  :attr:`~Domain.size` property
- the shape of the array that is supposed to hold these data entries
  (obtainable by means of the :attr:`~Domain.shape` property)
- equality comparison to another :class:`Domain` instance


Unstructured domains
--------------------

Martin Reinecke's avatar
Martin Reinecke committed
55
.. currentmodule:: nifty7.domains.unstructured_domain
56

57
58
59
60
61
62
63
64
65
66
67
Domains can be either *structured* (i.e. there is geometrical information
associated with them, like position in space and volume factors),
or *unstructured* (meaning that the data points have no associated manifold).

Unstructured domains can be described by instances of NIFTy's
:class:`UnstructuredDomain` class.


Structured domains
------------------

Martin Reinecke's avatar
Martin Reinecke committed
68
.. currentmodule:: nifty7.domains.structured_domain
69

70
71
72
73
74
In contrast to unstructured domains, these domains have an assigned geometry.
NIFTy requires them to provide the volume elements of their grid cells.
The additional methods are specified in the abstract class
:class:`StructuredDomain`:

Martin Reinecke's avatar
Martin Reinecke committed
75
- The properties :attr:`~StructuredDomain.scalar_dvol`,
76
77
78
79
  :attr:`~StructuredDomain.dvol`, and  :attr:`~StructuredDomain.total_volume`
  provide information about the domain's pixel volume(s) and its total volume.
- The property :attr:`~StructuredDomain.harmonic` specifies whether a domain
  is harmonic (i.e. describes a frequency space) or not
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
80
- If (and only if) the domain is harmonic, the methods
81
82
83
84
85
86
87
88
  :meth:`~StructuredDomain.get_k_length_array`,
  :meth:`~StructuredDomain.get_unique_k_lengths`, and
  :meth:`~StructuredDomain.get_fft_smoothing_kernel_function` provide absolute
  distances of the individual grid cells from the origin and assist with
  Gaussian convolution.

NIFTy comes with several concrete subclasses of :class:`StructuredDomain`:

Martin Reinecke's avatar
Martin Reinecke committed
89
.. currentmodule:: nifty7.domains
90

Philipp Arras's avatar
Philipp Arras committed
91
- :class:`~rg_space.RGSpace` represents a regular Cartesian grid with an arbitrary
92
  number of dimensions, which is supposed to be periodic in each dimension.
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
93
- :class:`~log_rg_space.LogRGSpace` implements a Cartesian grid with logarithmically
94
  spaced bins and an arbitrary number of dimensions.
Philipp Arras's avatar
Philipp Arras committed
95
96
- :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
97
  contains spherical harmonic coefficients.
Philipp Arras's avatar
Philipp Arras committed
98
- :class:`~power_space.PowerSpace` is used to describe one-dimensional power spectra.
99

100
101
102
Among these, :class:`~rg_space.RGSpace` and :class:`~log_rg_space.LogRGSpace` can
be harmonic or not (depending on constructor arguments),
:class:`~gl_space.GLSpace`, :class:`~hp_space.HPSpace`,
Philipp Arras's avatar
Philipp Arras committed
103
104
and :class:`~power_space.PowerSpace` are pure position domains (i.e.
nonharmonic), and :class:`~lm_space.LMSpace` is always harmonic.
105
106
107
108
109
110


Combinations of domains
=======================

The fundamental classes described above are often sufficient to specify the
111
112
domain of a field. In some cases, however, it will be necessary to define the
field on a product of elementary domains instead of a single one.
Philipp Arras's avatar
Philipp Arras committed
113
More sophisticated operators also require a set of several such fields.
114
115
Some examples are:

Philipp Arras's avatar
Philipp Arras committed
116
117
118
- sky emission depending on location and energy. This could be represented by a
  product of an :class:`~hp_space.HPSpace` (for location) with an
  :class:`~rg_space.RGSpace` (for energy).
119
- a polarized field, which could be modelled as a product of any structured
Martin Reinecke's avatar
Martin Reinecke committed
120
  domain (representing location) with a four-element
Philipp Arras's avatar
Philipp Arras committed
121
  :class:`~unstructured_domain.UnstructuredDomain` holding Stokes I, Q, U and V components.
122
- a model for the sky emission, which holds both the current realisation
Martin Reinecke's avatar
Martin Reinecke committed
123
124
  (on a harmonic domain) and a few inferred model parameters (e.g. on an
  unstructured grid).
125

Martin Reinecke's avatar
Martin Reinecke committed
126
.. currentmodule:: nifty7
127

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
128
Consequently, NIFTy defines a class called :class:`~domain_tuple.DomainTuple`
129
130
holding a sequence of :class:`~domains.domain.Domain` objects. The full domain is
specified as the product of all elementary domains. Thus, an instance of
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
131
:class:`~domain_tuple.DomainTuple` would be suitable to describe the first two
132
examples above. In principle, a :class:`~domain_tuple.DomainTuple`
Philipp Arras's avatar
Philipp Arras committed
133
can even be empty, which implies that the field living on it is a scalar.
Martin Reinecke's avatar
Martin Reinecke committed
134

Philipp Arras's avatar
Philipp Arras committed
135
A :class:`~domain_tuple.DomainTuple` supports iteration and indexing, and also
136
provides the properties :attr:`~domain_tuple.DomainTuple.shape` and
Philipp Arras's avatar
Philipp Arras committed
137
138
139
140
141
:attr:`~domain_tuple.DomainTuple.size` in analogy to the elementary
:class:`~domains.domain.Domain`.

An aggregation of several :class:`~domain_tuple.DomainTuple` s, each member
identified by a name, is described by the :class:`~multi_domain.MultiDomain`
142
143
144
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
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
145
last of the above examples.
146
147
148
149

Fields
======

Martin Reinecke's avatar
Martin Reinecke committed
150
151
152
Fields on a single DomainTuple
------------------------------

Philipp Arras's avatar
Philipp Arras committed
153
A :class:`~field.Field` object consists of the following components:
154

Philipp Arras's avatar
Philipp Arras committed
155
- a domain in form of a :class:`~domain_tuple.DomainTuple` object
156
157
158
- a data type (e.g. numpy.float64)
- an array containing the actual values

Martin Reinecke's avatar
Martin Reinecke committed
159
Usually, the array is stored in the form of a ``numpy.ndarray``, but for very
Martin Reinecke's avatar
Martin Reinecke committed
160
161
162
resource-intensive tasks NIFTy also provides an alternative storage method to
be used with distributed memory processing.

163
164
Fields support a wide range of arithmetic operations, either involving
two fields of equal domains or a field and a scalar. Arithmetic operations are
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
165
166
performed point-wise, and the returned field has the same domain as the input field(s).
Available operators are addition ("+"), subtraction ("-"),
167
168
multiplication ("*"), division ("/"), floor division ("//") and
exponentiation ("**"). Inplace operators ("+=", etc.) are not supported.
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
169
Further, boolean operators, performing a point-wise comparison of a field with
170
171
172
173
174
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.

Martin Reinecke's avatar
Martin Reinecke committed
175
176
Contractions (like summation, integration, minimum/maximum, computation of
statistical moments) can be carried out either over an entire field (producing
Philipp Arras's avatar
Philipp Arras committed
177
a scalar result) or over sub-domains (resulting in a field defined on a smaller
178
domain). Scalar products of two fields can also be computed easily as well.
Martin Reinecke's avatar
Martin Reinecke committed
179
See the documentation of :class:`~field.Field` for details.
Martin Reinecke's avatar
Martin Reinecke committed
180
181
182

There is also a set of convenience functions to generate fields with constant
values or fields filled with random numbers according to a user-specified
183
distribution: :attr:`~sugar.full`, :attr:`~sugar.from_random`.
Martin Reinecke's avatar
Martin Reinecke committed
184

Martin Reinecke's avatar
Martin Reinecke committed
185
186
187
188
189
Like almost all NIFTy objects, fields are immutable: their value or any other
attribute cannot be modified after construction. To manipulate a field in ways
that are not covered by the provided standard operations, its data content must
be extracted first, then changed, and a new field has to be created from the
result.
Martin Reinecke's avatar
Martin Reinecke committed
190

191
Fields defined on a MultiDomain
Martin Reinecke's avatar
Martin Reinecke committed
192
-------------------------------
Martin Reinecke's avatar
Martin Reinecke committed
193

Philipp Arras's avatar
Philipp Arras committed
194
195
196
The :class:`~multi_field.MultiField` class can be seen as a dictionary of
individual :class:`~field.Field` s, each identified by a name, which is defined
on a :class:`~multi_domain.MultiDomain`.
Martin Reinecke's avatar
Martin Reinecke committed
197
198
199
200
201


Operators
=========

Philipp Arras's avatar
Philipp Arras committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
All transformations between different NIFTy fields are expressed in the form of
:class:`~operators.operator.Operator` objects. The interface of this class is
rather minimalistic: it has a property called
:attr:`~operators.operator.Operator.domain` which returns a
:class:`~domain_tuple.DomainTuple` or :class:`~multi_domain.MultiDomain` object
specifying the structure of the :class:`~field.Field` or
:class:`~multi_field.MultiField` it expects as input, another property
:attr:`~operators.operator.Operator.target` describing its output, and finally
an overloaded :attr:`~operators.operator.Operator.apply` method, which can take:

- a :class:`~field.Field`/:class:`~multi_field.MultiField` object, in which case
  it returns the transformed :class:`~field.Field`/:class:`~multi_field.MultiField`.
- a :class:`~linearization.Linearization` object, in which case it returns the
  transformed :class:`~linearization.Linearization`.

This is the interface that all objects derived from
:class:`~operators.operator.Operator` must implement. In addition,
:class:`~operators.operator.Operator` objects can be added/subtracted,
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
220
221
222
multiplied, chained (via the :attr:`__call__` method or the `@` operator) and
support point-wise application of functions like :class:`exp()`, :class:`log()`,
:class:`sqrt()`, :class:`conjugate()`.
Martin Reinecke's avatar
Martin Reinecke committed
223

224

Martin Reinecke's avatar
Martin Reinecke committed
225
226
227
Advanced operators
------------------

Philipp Arras's avatar
Philipp Arras committed
228
NIFTy provides a library of commonly employed operators which can be used for
Martin Reinecke's avatar
Martin Reinecke committed
229
230
specific inference problems. Currently these are:

Martin Reinecke's avatar
Martin Reinecke committed
231
.. currentmodule:: nifty7.library
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
232

233
- :class:`~smooth_linear_amplitude.SLAmplitude`, which returns a smooth power spectrum.
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
234
- :class:`~inverse_gamma_operator.InverseGammaOperator`, which models point sources which are
Philipp Arras's avatar
Philipp Arras committed
235
  distributed according to a inverse-gamma distribution.
236
237
- :class:`~correlated_fields.CorrelatedField`, which models a diffuse field whose correlation
  structure is described by an amplitude operator.
Martin Reinecke's avatar
Martin Reinecke committed
238
239


240
241
242
Linear Operators
================

Martin Reinecke's avatar
Martin Reinecke committed
243
.. currentmodule:: nifty7.operators
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
244

Martin Reinecke's avatar
Martin Reinecke committed
245
A linear operator (represented by NIFTy7's abstract
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
246
247
248
249
250
:class:`~linear_operator.LinearOperator` class) is derived from
:class:`~operator.Operator` and can be interpreted as an (implicitly defined)
matrix. Since its operation is linear, it can provide some additional
functionality which is not available for the more generic
:class:`~operator.Operator` class.
251

Martin Reinecke's avatar
Martin Reinecke committed
252

Martin Reinecke's avatar
Martin Reinecke committed
253
254
Linear Operator basics
----------------------
Martin Reinecke's avatar
Martin Reinecke committed
255

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
256
There are four basic ways of applying an operator :math:`A` to a field :math:`s`:
257

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
258
259
260
261
- direct application: :math:`A(s)`
- adjoint application: :math:`A^\dagger (s)`
- inverse application: :math:`A^{-1} (s)`
- adjoint inverse application: :math:`(A^\dagger)^{-1} (s)`
262

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
263
264
Note: The inverse of the adjoint of a linear map and the adjoint of the inverse
of a linear map (if all those exist) are the same.
265

Martin Reinecke's avatar
Martin Reinecke committed
266
267
These different actions of a linear operator ``Op`` on a field ``f`` can be
invoked in various ways:
268
269
270
271
272
273
274
275

- direct multiplication: ``Op(f)`` or ``Op.times(f)`` or ``Op.apply(f, Op.TIMES)``
- adjoint multiplication: ``Op.adjoint_times(f)`` or ``Op.apply(f, Op.ADJOINT_TIMES)``
- inverse multiplication: ``Op.inverse_times(f)`` or ``Op.apply(f, Op.INVERSE_TIMES)``
- adjoint inverse multiplication: ``Op.adjoint_inverse_times(f)`` or ``Op.apply(f, Op.ADJOINT_INVERSE_TIMES)``

Operator classes defined in NIFTy may implement an arbitrary subset of these
four operations. This subset can be queried using the
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
276
:attr:`~linear_operator.LinearOperator.capability` property.
277
278

If needed, the set of supported operations can be enhanced by iterative
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
279
280
281
282
inversion methods; for example, an operator defining direct and adjoint
multiplication could be enhanced by this approach to support the complete set.
This functionality is provided by NIFTy's
:class:`~inversion_enabler.InversionEnabler` class, which is itself a linear
283
284
operator.

Martin Reinecke's avatar
Martin Reinecke committed
285
.. currentmodule:: nifty7.operators.operator
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
286

287
Direct multiplication and adjoint inverse multiplication transform a field
Philipp Arras's avatar
Philipp Arras committed
288
289
290
291
defined on the operator's :attr:`~Operator.domain` to one defined on the
operator's :attr:`~Operator.target`, whereas adjoint multiplication and inverse
multiplication transform from :attr:`~Operator.target` to
:attr:`~Operator.domain`.
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
292

Martin Reinecke's avatar
Martin Reinecke committed
293
.. currentmodule:: nifty7.operators
294
295

Operators with identical domain and target can be derived from
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
296
297
298
299
300
301
:class:`~endomorphic_operator.EndomorphicOperator`. Typical examples for this
category are the :class:`~scaling_operator.ScalingOperator`, which simply
multiplies its input by a scalar value, and
:class:`~diagonal_operator.DiagonalOperator`, which multiplies every value of
its input field with potentially different values.

Martin Reinecke's avatar
Martin Reinecke committed
302
.. currentmodule:: nifty7
303
304
305

Further operator classes provided by NIFTy are

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
306
307
308
309
310
311
312
313
314
- :class:`~operators.harmonic_operators.HarmonicTransformOperator` for
  transforms from a harmonic domain to its counterpart in position space, and
  their adjoint
- :class:`~operators.distributors.PowerDistributor` for transforms from a
  :class:`~domains.power_space.PowerSpace` to an associated harmonic domain, and
  their adjoint.
- :class:`~operators.simple_linear_operators.GeometryRemover`, which transforms
  from structured domains to unstructured ones. This is typically needed when
  building instrument response operators.
315

Martin Reinecke's avatar
Martin Reinecke committed
316

Martin Reinecke's avatar
Martin Reinecke committed
317
318
Syntactic sugar
---------------
Martin Reinecke's avatar
Martin Reinecke committed
319

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
320
NIFTy allows simple and intuitive construction of altered and combined
321
operators.
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
322
As an example, if ``A``, ``B`` and ``C`` are of type :class:`~operators.linear_operator.LinearOperator`
Philipp Arras's avatar
Philipp Arras committed
323
and ``f1`` and ``f2`` are of type :class:`~field.Field`, writing::
324

Martin Reinecke's avatar
Martin Reinecke committed
325
    X = A(B.inverse(A.adjoint)) + C
326
327
    f2 = X(f1)

Martin Reinecke's avatar
Martin Reinecke committed
328
.. currentmodule:: nifty7.operators.linear_operator
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
329

330
331
332
333
334
will perform the operation suggested intuitively by the notation, checking
domain compatibility while building the composed operator.
The properties :attr:`~LinearOperator.adjoint` and
:attr:`~LinearOperator.inverse` return a new operator which behaves as if it
were the original operator's adjoint or inverse, respectively.
335
336
337
338
339
340
The combined operator infers its domain and target from its constituents,
as well as the set of operations it can support.
Instantiating operator adjoints or inverses by :attr:`~LinearOperator.adjoint`
and similar methods is to be distinguished from the instant application of
operators performed by :attr:`~LinearOperator.adjoint_times` and similar
methods.
341
342
343
344


.. _minimization:

Martin Reinecke's avatar
Martin Reinecke committed
345

346
347
348
Minimization
============

Martin Reinecke's avatar
Martin Reinecke committed
349
350
351
Most problems in IFT are solved by (possibly nested) minimizations of
high-dimensional functions, which are often nonlinear.

Martin Reinecke's avatar
Martin Reinecke committed
352
.. currentmodule:: nifty7.minimization
Martin Reinecke's avatar
Martin Reinecke committed
353
354
355

Energy functionals
------------------
356

Martin Reinecke's avatar
Martin Reinecke committed
357
In NIFTy7 such functions are represented by objects of type
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
358
359
360
361
:class:`~energy.Energy`. These hold the prescription how to calculate the
function's :attr:`~energy.Energy.value`, :attr:`~energy.Energy.gradient` and
(optionally) :attr:`~energy.Energy.metric` at any given
:attr:`~energy.Energy.position` in parameter space. Function values are
362
floating-point scalars, gradients have the form of fields defined on the energy's
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
363
364
position domain, and metrics are represented by linear operator objects.

Martin Reinecke's avatar
Martin Reinecke committed
365
.. currentmodule:: nifty7
366

Martin Reinecke's avatar
Martin Reinecke committed
367
Energies are classes that typically have to be provided by the user when
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
368
tackling new IFT problems. An example of concrete energy classes delivered with
Martin Reinecke's avatar
Martin Reinecke committed
369
NIFTy7 is :class:`~minimization.quadratic_energy.QuadraticEnergy` (with
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
370
position-independent metric, mainly used with conjugate gradient minimization).
371

Philipp Arras's avatar
Philipp Arras committed
372
373
374
375
For MGVI and GeoVI, NIFTy provides :func:`~minimization.kl_energies.MetricGaussianKL`
and :func:`~minimization.kl_energies.GeoMetricKL` that instantiate objects
containing the sampled estimate of the KL divergence, its gradient and the
Fisher metric. These constructors require an instance
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
376
377
378
379
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`
Philipp Frank's avatar
Philipp Frank committed
380
can be constructed from the likelihood, represented by a
Philipp Arras's avatar
Philipp Arras committed
381
:class:`~operators.energy_operators.LikelihoodEnergyOperator` instance.
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
382
383
384
Several commonly used forms of the likelihoods are already provided in
NIFTy, such as :class:`~operators.energy_operators.GaussianEnergy`,
:class:`~operators.energy_operators.PoissonianEnergy`,
Philipp Arras's avatar
Philipp Arras committed
385
:class:`~operators.energy_operators.InverseGammaEnergy` or
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
386
387
388
389
: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.
390
391


Martin Reinecke's avatar
Martin Reinecke committed
392
393
394

Iteration control
-----------------
Martin Reinecke's avatar
Martin Reinecke committed
395

Martin Reinecke's avatar
Martin Reinecke committed
396
.. currentmodule:: nifty7.minimization.iteration_controllers
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
397

398
Iterative minimization of an energy requires some means of
Martin Reinecke's avatar
Martin Reinecke committed
399
checking the quality of the current solution estimate and stopping once
Martin Reinecke's avatar
Martin Reinecke committed
400
401
402
it is sufficiently accurate. In case of numerical problems, the iteration needs
to be terminated as well, returning a suitable error description.

Martin Reinecke's avatar
Martin Reinecke committed
403
In NIFTy7, this functionality is encapsulated in the abstract
Martin Reinecke's avatar
Martin Reinecke committed
404
405
406
407
408
409
410
:class:`IterationController` class, which is provided with the initial energy
object before starting the minimization, and is updated with the improved
energy after every iteration. Based on this information, it can either continue
the minimization or return the current estimate indicating convergence or
failure.

Sensible stopping criteria can vary significantly with the problem being
411
solved; NIFTy provides a concrete sub-class of :class:`IterationController`
Martin Reinecke's avatar
Martin Reinecke committed
412
called :class:`GradientNormController`, which should be appropriate in many
413
414
circumstances. A full list of the available :class:`IterationController` s
in NIFTy can be found below, but users have complete freedom to implement custom
415
:class:`IterationController` sub-classes for their specific applications.
Martin Reinecke's avatar
Martin Reinecke committed
416
417
418
419

Minimization algorithms
-----------------------

Martin Reinecke's avatar
Martin Reinecke committed
420
.. currentmodule:: nifty7.minimization
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
421

Martin Reinecke's avatar
Martin Reinecke committed
422
All minimization algorithms in NIFTy inherit from the abstract
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
423
:class:`~minimizer.Minimizer` class, which presents a minimalistic interface
Philipp Arras's avatar
Philipp Arras committed
424
consisting only of a :meth:`~minimizer.Minimizer.__call__` method taking an
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
425
426
:class:`~energy.Energy` object and optionally a preconditioning operator, and
returning the energy at the discovered minimum and a status code.
Martin Reinecke's avatar
Martin Reinecke committed
427

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
428
429
430
For energies with a quadratic form (i.e. which can be expressed by means of a
:class:`~quadratic_energy.QuadraticEnergy` object), an obvious choice of
algorithm is the :class:`~conjugate_gradient.ConjugateGradient` minimizer.
Martin Reinecke's avatar
Martin Reinecke committed
431
432

A similar algorithm suited for nonlinear problems is provided by
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
433
:class:`~nonlinear_cg.NonlinearCG`.
Martin Reinecke's avatar
Martin Reinecke committed
434

Martin Reinecke's avatar
Martin Reinecke committed
435
Many minimizers for nonlinear problems can be characterized as
Martin Reinecke's avatar
Martin Reinecke committed
436

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
437
438
- First deciding on a direction for the next step.
- Then finding a suitable step length along this direction, resulting in the
Martin Reinecke's avatar
Martin Reinecke committed
439
440
  next energy estimate.

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
441
442
This family of algorithms is encapsulated in NIFTy's
:class:`~descent_minimizers.DescentMinimizer` class, which currently has three
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
443
generally usable concrete implementations:
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
444
445
:class:`~descent_minimizers.NewtonCG`, :class:`~descent_minimizers.L_BFGS` and
:class:`~descent_minimizers.VL_BFGS`. Of these algorithms, only
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
446
:class:`~descent_minimizers.NewtonCG` requires the energy object to provide
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
447
a :attr:`~energy.Energy.metric` property, the others only need energy values and
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
448
449
gradients. Further available descent minimizers are
:class:`~descent_minimizers.RelaxedNewton`
450
and :class:`~descent_minimizers.SteepestDescent`.
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
451
452

The flexibility of NIFTy's design allows using externally provided minimizers.
453
With only small effort, adaptors for two SciPy minimizers were written; they are
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
454
available under the names :class:`~scipy_minimizer.ScipyCG` and
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
455
:class:`~scipy_minimizer.L_BFGS_B`.
Martin Reinecke's avatar
Martin Reinecke committed
456

Martin Reinecke's avatar
Martin Reinecke committed
457
458
459
460

Application to operator inversion
---------------------------------

Martin Reinecke's avatar
Martin Reinecke committed
461
.. currentmodule:: nifty7
Martin Reinecke's avatar
Martin Reinecke committed
462

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
463
464
465
466
The machinery presented here cannot only be used for minimizing functionals
derived from IFT, but also for the numerical inversion of linear operators, if
the desired application mode is not directly available. A classical example is
the information propagator whose inverse is defined as:
Martin Reinecke's avatar
Martin Reinecke committed
467

Martin Reinecke's avatar
Martin Reinecke committed
468
:math:`D^{-1} = \left(R^\dagger N^{-1} R + S^{-1}\right)`.
Martin Reinecke's avatar
Martin Reinecke committed
469

Philipp Arras's avatar
Docs pa    
Philipp Arras committed
470
It needs to be applied in forward direction in order to calculate the Wiener
471
472
filter solution, but only its inverse application is straightforward.
To use it in forward direction, we make use of NIFTy's
Philipp Arras's avatar
Docs pa    
Philipp Arras committed
473
:class:`~operators.inversion_enabler.InversionEnabler` class, which internally
Martin Reinecke's avatar
Martin Reinecke committed
474
applies the (approximate) inverse of the given operator :math:`x = Op^{-1} (y)` by
475
476
477
478
479
solving the equation :math:`y = Op (x)` for :math:`x`.
This is accomplished by minimizing a suitable
:class:`~minimization.quadratic_energy.QuadraticEnergy`
with the :class:`~minimization.conjugate_gradient.ConjugateGradient`
algorithm. An example is provided in
Philipp Arras's avatar
Philipp Arras committed
480
:func:`~library.wiener_filter_curvature.WienerFilterCurvature`.
481

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
482

483
Posterior analysis and visualization
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
484
------------------------------------
485
486
487
488
489

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
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
490
variance :attr:`~probing.StatCalculator.var` of these samples.
491
492
493

Fields can be visualized using the :class:`~plot.Plot` class, which invokes
matplotlib for plotting.