code.rst 16.8 KB
 Mihai Baltac committed Feb 12, 2018 1   Martin Reinecke committed Feb 19, 2018 2 =============  Martin Reinecke committed Feb 17, 2018 3 4 Code Overview =============  Mihai Baltac committed Feb 12, 2018 5   Martin Reinecke committed Feb 19, 2018 6 7 8 9  Executive summary =================  Martin Reinecke committed Feb 17, 2018 10 11 The fundamental building blocks required for IFT computations are best recognized from a large distance, ignoring all technical details.  Mihai Baltac committed Feb 12, 2018 12   Martin Reinecke committed Feb 17, 2018 13 14 From such a perspective,  Martin Reinecke committed Jul 02, 2018 15 - IFT problems largely consist of the combination of several high dimensional  Sebastian Hutschenreuter committed Jul 02, 2018 16  *minimization* problems.  Philipp Arras committed Jan 07, 2019 17 - Within NIFTy, *operators* are used to define the characteristic equations and  Martin Reinecke committed Jul 02, 2018 18  properties of the problems.  Martin Reinecke committed Feb 19, 2018 19 20 - The equations are built mostly from the application of *linear operators*, but there may also be nonlinear functions involved.  Martin Reinecke committed Feb 17, 2018 21 - The unknowns in the equations represent either continuous physical *fields*,  Martin Reinecke committed Feb 19, 2018 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*.  Martin Reinecke committed Feb 17, 2018 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 -------------------  Philipp Arras committed Jan 10, 2019 39 40 .. currentmodule:: nifty5.domains.domain  Philipp Arras committed Jun 18, 2018 41 One of the fundamental building blocks of the NIFTy5 framework is the *domain*.  Philipp Arras committed Jan 10, 2019 42 Its required capabilities are expressed by the abstract :py:class:Domain class.  Martin Reinecke committed Feb 17, 2018 43 A domain must be able to answer the following queries:  Philipp Arras committed Jan 10, 2019 44 m  Martin Reinecke committed Feb 17, 2018 45 46 47 48 49 50 51 52 53 54 55  - 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 --------------------  Philipp Arras committed Jan 10, 2019 56 57 .. currentmodule:: nifty5.domains.unstructured_domain  Martin Reinecke committed Feb 17, 2018 58 59 60 61 62 63 64 65 66 67 68 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 ------------------  Philipp Arras committed Jan 10, 2019 69 70 .. currentmodule:: nifty5.domains.structured_domain  Martin Reinecke committed Feb 17, 2018 71 72 73 74 75 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 committed Feb 19, 2018 76 - The properties :attr:~StructuredDomain.scalar_dvol,  Martin Reinecke committed Feb 17, 2018 77 78 79 80 81 82 83 84 85 86 87 88 89  :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 - Iff the domain is harmonic, the methods :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:  Philipp Arras committed Jan 10, 2019 90 91 .. currentmodule:: nifty5.domains  Philipp Arras committed Jan 11, 2019 92 - :class:~rg_space.RGSpace represents a regular Cartesian grid with an arbitrary  Martin Reinecke committed Feb 17, 2018 93  number of dimensions, which is supposed to be periodic in each dimension.  Philipp Arras committed Jan 11, 2019 94 95 - :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  Martin Reinecke committed Feb 17, 2018 96  contains spherical harmonic coefficients.  Philipp Arras committed Jan 11, 2019 97 - :class:~power_space.PowerSpace is used to describe one-dimensional power spectra.  Martin Reinecke committed Feb 17, 2018 98   Philipp Arras committed Jan 11, 2019 99 100 101 102 Among these, :class:~rg_space.RGSpace can 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. nonharmonic), and :class:~lm_space.LMSpace is always harmonic.  Martin Reinecke committed Feb 17, 2018 103 104 105 106 107 108  Combinations of domains ======================= The fundamental classes described above are often sufficient to specify the  Philipp Arras committed Jan 07, 2019 109 110 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 committed Jan 07, 2019 111 More sophisticated operators also require a set of several such fields.  Martin Reinecke committed Feb 17, 2018 112 113 Some examples are:  Philipp Arras committed Jan 11, 2019 114 115 116 - 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).  Philipp Arras committed Jan 07, 2019 117 - a polarized field, which could be modeled as a product of any structured  Martin Reinecke committed Feb 19, 2018 118  domain (representing location) with a four-element  Philipp Arras committed Jan 11, 2019 119  :class:~unstructured_domain.UnstructuredDomain holding Stokes I, Q, U and V components.  Martin Reinecke committed Aug 08, 2018 120 121 122 - a model for the sky emission, which holds both the current realization (on a harmonic domain) and a few inferred model parameters (e.g. on an unstructured grid).  Martin Reinecke committed Feb 17, 2018 123   Philipp Arras committed Jan 11, 2019 124 .. currentmodule:: nifty5  Martin Reinecke committed Feb 17, 2018 125   Philipp Arras committed Jan 11, 2019 126 127 128 129 Consequently, NIFTy defines a class called :class:~domain_tuple.DomainTuple holding a sequence of :class:~domains.domain.Domain objects, which is used to specify full field domains. In principle, a :class:~domain_tuple.DomainTuple can even be empty, which implies that the field living on it is a scalar.  Martin Reinecke committed Feb 19, 2018 130   Philipp Arras committed Jan 11, 2019 131 132 133 134 135 136 137 138 A :class:~domain_tuple.DomainTuple supports iteration and indexing, and also provides the properties :attr:~domain_tuple.DomainTuple.shape, :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 class.  Martin Reinecke committed Feb 17, 2018 139 140 141 142  Fields ======  Martin Reinecke committed Aug 08, 2018 143 144 145 Fields on a single DomainTuple ------------------------------  Philipp Arras committed Jan 11, 2019 146 A :class:~field.Field object consists of the following components:  Martin Reinecke committed Feb 17, 2018 147   Philipp Arras committed Jan 11, 2019 148 - a domain in form of a :class:~domain_tuple.DomainTuple object  Martin Reinecke committed Feb 17, 2018 149 150 151 - a data type (e.g. numpy.float64) - an array containing the actual values  Martin Reinecke committed Dec 21, 2018 152 Usually, the array is stored in the form of a numpy.ndarray, but for very  Martin Reinecke committed Feb 19, 2018 153 154 155 156 157 158 159 160 161 162 163 164 165 166 resource-intensive tasks NIFTy also provides an alternative storage method to be used with distributed memory processing. Fields support a wide range of arithmetic operations, either involving two fields with equal domains, or a field and a scalar. Contractions (like summation, integration, minimum/maximum, computation of statistical moments) can be carried out either over an entire field (producing a scalar result) or over sub-domains (resulting in a field living on a smaller domain). Scalar products of two fields can also be computed easily. 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 distribution.  Martin Reinecke committed Jul 05, 2018 167 168 169 170 171 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 committed Feb 19, 2018 172   Philipp Arras committed Jan 07, 2019 173 Fields defined on a MultiDomain  Martin Reinecke committed Jan 09, 2019 174 -------------------------------  Martin Reinecke committed Aug 08, 2018 175   Philipp Arras committed Jan 11, 2019 176 177 178 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 committed Aug 08, 2018 179 180 181 182 183  Operators =========  Philipp Arras committed Jan 11, 2019 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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, 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 committed Aug 08, 2018 205   Martin Reinecke committed Feb 17, 2018 206   Martin Reinecke committed Jan 09, 2019 207 208 209 Advanced operators ------------------  Philipp Arras committed Jan 11, 2019 210 NIFTy provides a library of commonly employed operators which can be used for  Martin Reinecke committed Jan 09, 2019 211 212 213 specific inference problems. Currently these are: - :class:AmplitudeOperator, which returns a smooth power spectrum.  Philipp Arras committed Jan 11, 2019 214 215 216 217 - :class:InverseGammaOperator, which models point sources which are distributed according to a inverse-gamma distribution. - :class:CorrelatedField, which models a diffuse log-normal field. It takes an amplitude operator to specify the correlation structure of the field.  Martin Reinecke committed Jan 09, 2019 218 219   Martin Reinecke committed Feb 17, 2018 220 221 222 Linear Operators ================  Philipp Arras committed Jan 11, 2019 223 A linear operator (represented by NIFTy5's abstract :class:operators.linear_operator.LinearOperator  Martin Reinecke committed Aug 08, 2018 224 225 class) is derived from Operator and can be interpreted as an (implicitly defined) matrix. Since its operation is linear, it can provide some  Philipp Arras committed Jan 11, 2019 226 additional functionality which is not available for the more generic :class:operators.operator.Operator  Martin Reinecke committed Aug 08, 2018 227 class.  Martin Reinecke committed Feb 17, 2018 228   Martin Reinecke committed Feb 18, 2018 229   Martin Reinecke committed Jan 09, 2019 230 231 Linear Operator basics ----------------------  Martin Reinecke committed Feb 18, 2018 232   Martin Reinecke committed Feb 17, 2018 233 234 There are four basic ways of applying an operator :math:A to a field :math:f:  Martin Reinecke committed Aug 08, 2018 235 236 237 238 - direct application: :math:A\cdot f - adjoint application: :math:A^\dagger \cdot f - inverse application: :math:A^{-1}\cdot f - adjoint inverse application: :math:(A^\dagger)^{-1}\cdot f  Martin Reinecke committed Feb 17, 2018 239   Martin Reinecke committed Aug 08, 2018 240 241 (Because of the linearity, inverse adjoint and adjoint inverse application are equivalent.)  Martin Reinecke committed Feb 17, 2018 242   Martin Reinecke committed Jan 09, 2019 243 244 These different actions of a linear operator Op on a field f can be invoked in various ways:  Martin Reinecke committed Feb 17, 2018 245 246 247 248 249 250 251 252 253 254 255 256 257  - 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 :attr:~LinearOperator.capability property. If needed, the set of supported operations can be enhanced by iterative inversion methods; for example, an operator defining direct and adjoint multiplication could be  Martin Reinecke committed Feb 19, 2018 258 enhanced by this approach to support the complete set. This functionality is  Martin Reinecke committed Feb 17, 2018 259 260 261 262 263 264 265 266 267 268 269 270 271 272 provided by NIFTy's :class:InversionEnabler class, which is itself a linear operator. Direct multiplication and adjoint inverse multiplication transform a field living on the operator's :attr:~LinearOperator.domain to one living on the operator's :attr:~LinearOperator.target, whereas adjoint multiplication and inverse multiplication transform from :attr:~LinearOperator.target to :attr:~LinearOperator.domain. Operators with identical domain and target can be derived from :class:EndomorphicOperator; typical examples for this category are the :class:ScalingOperator, which simply multiplies its input by a scalar value, and :class:DiagonalOperator, which multiplies every value of its input field with potentially different values. Further operator classes provided by NIFTy are  Martin Reinecke committed Feb 19, 2018 273 274 - :class:HarmonicTransformOperator for transforms from a harmonic domain to its counterpart in position space, and their adjoint  Martin Reinecke committed Feb 17, 2018 275 - :class:PowerDistributor for transforms from a :class:PowerSpace to  Martin Reinecke committed Feb 19, 2018 276  an associated harmonic domain, and their adjoint  Martin Reinecke committed Feb 17, 2018 277 278 279 280 - :class:GeometryRemover, which transforms from structured domains to unstructured ones. This is typically needed when building instrument response operators.  Martin Reinecke committed Feb 18, 2018 281   Martin Reinecke committed Feb 19, 2018 282 283 Syntactic sugar ---------------  Martin Reinecke committed Feb 18, 2018 284   Martin Reinecke committed Jun 19, 2018 285 Nifty5 allows simple and intuitive construction of altered and combined  Martin Reinecke committed Feb 17, 2018 286 operators.  Philipp Arras committed Jan 11, 2019 287 288 As an example, if A, B and C are of type :class:operators.linear_operator.LinearOperator and f1 and f2 are of type :class:~field.Field, writing::  Martin Reinecke committed Feb 17, 2018 289   Martin Reinecke committed Aug 08, 2018 290  X = A(B.inverse(A.adjoint)) + C  Martin Reinecke committed Feb 17, 2018 291 292 293 294 295 296 297 298 299 300 301 302 303  f2 = X(f1) will perform the operation suggested intuitively by the notation, checking domain compatibility while building the composed operator. The combined operator infers its domain and target from its constituents, as well as the set of operations it can support. 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. .. _minimization:  Martin Reinecke committed Feb 19, 2018 304   Martin Reinecke committed Feb 17, 2018 305 306 307 Minimization ============  Martin Reinecke committed Feb 18, 2018 308 309 310 311 312 313 Most problems in IFT are solved by (possibly nested) minimizations of high-dimensional functions, which are often nonlinear. Energy functionals ------------------  Martin Reinecke committed Feb 17, 2018 314   Philipp Arras committed Jun 18, 2018 315 In NIFTy5 such functions are represented by objects of type :class:Energy.  Martin Reinecke committed Feb 18, 2018 316 317 These hold the prescription how to calculate the function's :attr:~Energy.value, :attr:~Energy.gradient and  Martin Reinecke committed Jul 03, 2018 318 (optionally) :attr:~Energy.metric at any given :attr:~Energy.position  Martin Reinecke committed Feb 19, 2018 319 in parameter space.  Martin Reinecke committed Feb 18, 2018 320 Function values are floating-point scalars, gradients have the form of fields  Martin Reinecke committed Jul 03, 2018 321 living on the energy's position domain, and metrics are represented by  Martin Reinecke committed Feb 18, 2018 322 linear operator objects.  Martin Reinecke committed Feb 17, 2018 323   Martin Reinecke committed Feb 19, 2018 324 325 Energies are classes that typically have to be provided by the user when tackling new IFT problems.  Philipp Arras committed Jun 18, 2018 326 Some examples of concrete energy classes delivered with NIFTy5 are  Martin Reinecke committed Jul 03, 2018 327 :class:QuadraticEnergy (with position-independent metric, mainly used with  Philipp Arras committed Jun 18, 2018 328 conjugate gradient minimization) and :class:~nifty5.library.WienerFilterEnergy.  Martin Reinecke committed Feb 17, 2018 329   Martin Reinecke committed Feb 19, 2018 330 331 332  Iteration control -----------------  Martin Reinecke committed Feb 18, 2018 333 334  Iterative minimization of an energy reqires some means of  Martin Reinecke committed Feb 19, 2018 335 checking the quality of the current solution estimate and stopping once  Martin Reinecke committed Feb 18, 2018 336 337 338 it is sufficiently accurate. In case of numerical problems, the iteration needs to be terminated as well, returning a suitable error description.  Philipp Arras committed Jun 18, 2018 339 In NIFTy5, this functionality is encapsulated in the abstract  Martin Reinecke committed Feb 18, 2018 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 :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 solved; NIFTy provides one concrete sub-class of :class:IterationController called :class:GradientNormController, which should be appropriate in many circumstances, but users have complete freedom to implement custom sub-classes for their specific applications. Minimization algorithms -----------------------  Martin Reinecke committed Feb 19, 2018 356 357 358 359 360 361 362 363 364 365 366 367 368 All minimization algorithms in NIFTy inherit from the abstract :class:Minimizer class, which presents a minimalistic interface consisting only of a :meth:~Minimizer.__call__ method taking an :class:Energy object and optionally a preconditioning operator, and returning the energy at the discovered minimum and a status code. For energies with a quadratic form (i.e. which can be expressed by means of a :class:QuadraticEnergy object), an obvious choice of algorithm is the :class:ConjugateGradient minimizer. A similar algorithm suited for nonlinear problems is provided by :class:NonlinearCG.  Martin Reinecke committed Feb 19, 2018 369 Many minimizers for nonlinear problems can be characterized as  Martin Reinecke committed Feb 19, 2018 370 371 372 373 374 375 376 377 378  - first deciding on a direction for the next step - then finding a suitable step length along this direction, resulting in the next energy estimate. This family of algorithms is encapsulated in NIFTy's :class:DescentMinimizer class, which currently has three concrete implementations: :class:SteepestDescent, :class:VL_BFGS, and :class:RelaxedNewton. Of these algorithms, only :class:RelaxedNewton requires the energy object to  Martin Reinecke committed Jul 03, 2018 379 provide a :attr:~Energy.metric property, the others only need energy  Martin Reinecke committed Feb 19, 2018 380 381 values and gradients.  Martin Reinecke committed Feb 19, 2018 382 383 384 385 386 The flexibility of NIFTy's design allows using externally provided minimizers. With only small effort, adapters for two SciPy minimizers were written; they are available under the names :class:NewtonCG and :class:L_BFGS_B.  Martin Reinecke committed Feb 19, 2018 387 388 389 390  Application to operator inversion ---------------------------------  Martin Reinecke committed Feb 18, 2018 391 392 393 394 395 396 397 398 399 400 It is important to realize that the machinery presented here cannot only be used for minimizing IFT Hamiltonians, 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 :math:D = \left(R^\dagger N^{-1} R + S^{-1}\right)^{-1}, which must be applied when calculating a Wiener filter. Only its inverse application is straightforward; to use it in forward direction, we make use of NIFTy's :class:InversionEnabler class, which internally performs a  Martin Reinecke committed Feb 19, 2018 401 minimization of a :class:QuadraticEnergy by means of the  Martin Reinecke committed Feb 18, 2018 402 :class:ConjugateGradient algorithm.