nifty_rg.py 76.3 KB
Newer Older
1
2
# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
Marco Selig's avatar
Marco Selig committed
3
##
4
# Copyright (C) 2015 Max-Planck-Society
Marco Selig's avatar
Marco Selig committed
5
##
6
7
# Author: Marco Selig
# Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
Marco Selig's avatar
Marco Selig committed
8
##
9
10
11
12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
Marco Selig's avatar
Marco Selig committed
13
##
14
15
16
17
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
Marco Selig's avatar
Marco Selig committed
18
##
19
20
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Marco Selig's avatar
Marco Selig committed
21
22
23
24
25
26
27
28
29
30

"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  rg
    ..                               /______/

Marco Selig's avatar
Marco Selig committed
31
    NIFTY submodule for regular Cartesian grids.
Marco Selig's avatar
Marco Selig committed
32
33
34

"""
from __future__ import division
Ultimanet's avatar
Ultimanet committed
35

36
import itertools
Marco Selig's avatar
Marco Selig committed
37
import numpy as np
38
import os
39
from scipy.special import erf
Marco Selig's avatar
Marco Selig committed
40
41
42
import pylab as pl
from matplotlib.colors import LogNorm as ln
from matplotlib.ticker import LogFormatter as lf
Ultimanet's avatar
Ultimanet committed
43

44
45
46
from nifty.nifty_core import point_space,\
                             field
import nifty_fft
47
from nifty.keepers import about,\
Ultima's avatar
Ultima committed
48
49
                          global_dependency_injector as gdi,\
                          global_configuration as gc
50
from nifty.nifty_mpi_data import distributed_data_object
51
from nifty.nifty_mpi_data import STRATEGIES as DISTRIBUTION_STRATEGIES
Ultimanet's avatar
Ultimanet committed
52
from nifty.nifty_paradict import rg_space_paradict
53
54
from nifty.nifty_power_indices import rg_power_indices
from nifty.nifty_random import random
Ultima's avatar
Ultima committed
55
import nifty.nifty_utilities as utilities
56

Ultima's avatar
Ultima committed
57
MPI = gdi[gc['mpi_module']]
58
RG_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global']
Ultimanet's avatar
Ultimanet committed
59

Marco Selig's avatar
Marco Selig committed
60

61
class rg_space(point_space):
Marco Selig's avatar
Marco Selig committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    """
        ..      _____   _______
        ..    /   __/ /   _   /
        ..   /  /    /  /_/  /
        ..  /__/     \____  /  space class
        ..          /______/

        NIFTY subclass for spaces of regular Cartesian grids.

        Parameters
        ----------
        num : {int, numpy.ndarray}
            Number of gridpoints or numbers of gridpoints along each axis.
        naxes : int, *optional*
            Number of axes (default: None).
        zerocenter : {bool, numpy.ndarray}, *optional*
            Whether the Fourier zero-mode is located in the center of the grid
            (or the center of each axis speparately) or not (default: True).
        hermitian : bool, *optional*
            Whether the fields living in the space follow hermitian symmetry or
            not (default: True).
        purelyreal : bool, *optional*
            Whether the field values are purely real (default: True).
        dist : {float, numpy.ndarray}, *optional*
            Distance between two grid points along each axis (default: None).
        fourier : bool, *optional*
            Whether the space represents a Fourier or a position grid
            (default: False).

        Notes
        -----
        Only even numbers of grid points per axis are supported.
        The basis transformations between position `x` and Fourier mode `k`
        rely on (inverse) fast Fourier transformations using the
        :math:`exp(2 \pi i k^\dagger x)`-formulation.

        Attributes
        ----------
        para : numpy.ndarray
            One-dimensional array containing information on the axes of the
            space in the following form: The first entries give the grid-points
            along each axis in reverse order; the next entry is 0 if the
            fields defined on the space are purely real-valued, 1 if they are
            hermitian and complex, and 2 if they are not hermitian, but
            complex-valued; the last entries hold the information on whether
            the axes are centered on zero or not, containing a one for each
            zero-centered axis and a zero for each other one, in reverse order.
109
        dtype : numpy.dtype
Marco Selig's avatar
Marco Selig committed
110
111
112
113
114
115
116
117
118
119
120
121
            Data type of the field values for a field defined on this space,
            either ``numpy.float64`` or ``numpy.complex128``.
        discrete : bool
            Whether or not the underlying space is discrete, always ``False``
            for regular grids.
        vol : numpy.ndarray
            One-dimensional array containing the distances between two grid
            points along each axis, in reverse order. By default, the total
            length of each axis is assumed to be one.
        fourier : bool
            Whether or not the grid represents a Fourier basis.
    """
122
    epsilon = 0.0001  # relative precision for comparisons
Marco Selig's avatar
Marco Selig committed
123

124
    def __init__(self, shape, zerocenter=False, complexity=0, distances=None,
Ultima's avatar
Ultima committed
125
                 harmonic=False, datamodel='fftw', fft_module=gc['fft_module'],
126
                 comm=gc['default_comm']):
Marco Selig's avatar
Marco Selig committed
127
128
129
130
131
132
133
134
135
136
137
138
        """
            Sets the attributes for an rg_space class instance.

            Parameters
            ----------
            num : {int, numpy.ndarray}
                Number of gridpoints or numbers of gridpoints along each axis.
            naxes : int, *optional*
                Number of axes (default: None).
            zerocenter : {bool, numpy.ndarray}, *optional*
                Whether the Fourier zero-mode is located in the center of the
                grid (or the center of each axis speparately) or not
Ultimanet's avatar
Ultimanet committed
139
                (default: False).
Marco Selig's avatar
Marco Selig committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            hermitian : bool, *optional*
                Whether the fields living in the space follow hermitian
                symmetry or not (default: True).
            purelyreal : bool, *optional*
                Whether the field values are purely real (default: True).
            dist : {float, numpy.ndarray}, *optional*
                Distance between two grid points along each axis
                (default: None).
            fourier : bool, *optional*
                Whether the space represents a Fourier or a position grid
                (default: False).

            Returns
            -------
            None
        """
156

157
        self.paradict = rg_space_paradict(shape=shape,
158
159
                                          complexity=complexity,
                                          zerocenter=zerocenter)
160
        # set dtype
161
        if self.paradict['complexity'] == 0:
162
            self.dtype = np.dtype('float64')
Marco Selig's avatar
Marco Selig committed
163
        else:
164
            self.dtype = np.dtype('complex128')
165
166
167

        # set datamodel
        if datamodel not in ['np'] + RG_DISTRIBUTION_STRATEGIES:
168
            about.warnings.cprint("WARNING: datamodel set to default.")
169
            self.datamodel = \
Ultima's avatar
Ultima committed
170
                gc['default_distribution_strategy']
171
172
173
        else:
            self.datamodel = datamodel

174
        # set volume/distances
175
176
177
178
179
        naxes = len(self.paradict['shape'])
        if distances is None:
            distances = 1 / np.array(self.paradict['shape'], dtype=np.float)
        elif np.isscalar(distances):
            distances = np.ones(naxes, dtype=np.float) * distances
Marco Selig's avatar
Marco Selig committed
180
        else:
181
182
183
184
            distances = np.array(distances, dtype=np.float)
            if np.size(distances) == 1:
                distances = distances * np.ones(naxes, dtype=np.float)
            if np.size(distances) != naxes:
185
                raise ValueError(about._errors.cstring(
186
187
188
                    "ERROR: size mismatch ( " + str(np.size(distances)) +
                    " <> " + str(naxes) + " )."))
        if np.any(distances <= 0):
189
            raise ValueError(about._errors.cstring(
190
                "ERROR: nonpositive distance(s)."))
Marco Selig's avatar
Marco Selig committed
191

192
        self.distances = tuple(distances)
193
194
195
196
        self.harmonic = bool(harmonic)
        self.discrete = False

        self.comm = self._parse_comm(comm)
Ultima's avatar
Ultima committed
197

198
199
        # Initializes the fast-fourier-transform machine, which will be used
        # to transform the space
Ultima's avatar
Ultima committed
200
        if not gc.validQ('fft_module', fft_module):
201
            about.warnings.cprint("WARNING: fft_module set to default.")
Ultima's avatar
Ultima committed
202
203
            fft_module = gc['fft_module']
        self.fft_machine = nifty_fft.fft_factory(fft_module)
204
205
206
207

        # Initialize the power_indices object which takes care of kindex,
        # pindex, rho and the pundex for a given set of parameters
        if self.harmonic:
208
209
            self.power_indices = rg_power_indices(
                    shape=self.get_shape(),
210
                    dgrid=distances,
211
212
213
214
                    zerocentered=self.paradict['zerocenter'],
                    comm=self.comm,
                    datamodel=self.datamodel,
                    allowed_distribution_strategies=RG_DISTRIBUTION_STRATEGIES)
215

216
217
    @property
    def para(self):
218
        temp = np.array(self.paradict['shape'] +
219
220
                        [self.paradict['complexity']] +
                        self.paradict['zerocenter'], dtype=int)
221
        return temp
222

223
224
    @para.setter
    def para(self, x):
225
        self.paradict['shape'] = x[:(np.size(x) - 1) // 2]
226
227
        self.paradict['zerocenter'] = x[(np.size(x) + 1) // 2:]
        self.paradict['complexity'] = x[(np.size(x) - 1) // 2]
Ultimanet's avatar
Ultimanet committed
228

Ultima's avatar
Ultima committed
229
230
231
232
233
234
235
236
    def __hash__(self):
        result_hash = 0
        for (key, item) in vars(self).items():
            if key in ['fft_machine', 'power_indices']:
                continue
            result_hash ^= item.__hash__() * hash(key)
        return result_hash

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    # __identiftier__ returns an object which contains all information needed
    # to uniquely identify a space. It returns a (immutable) tuple which
    # therefore can be compared.
    # The rg_space version of __identifier__ filters out the vars-information
    # which is describing the rg_space's structure
    def _identifier(self):
        # Extract the identifying parts from the vars(self) dict.
        temp = [(ii[0],
                 ((lambda x: tuple(x) if
                  isinstance(x, np.ndarray) else x)(ii[1])))
                for ii in vars(self).iteritems()
                if ii[0] not in ['fft_machine', 'power_indices', 'comm']]
        temp.append(('comm', self.comm.__hash__()))
        # Return the sorted identifiers as a tuple.
        return tuple(sorted(temp))
Ultimanet's avatar
Ultimanet committed
252

253
    def copy(self):
254
        return rg_space(shape=self.paradict['shape'],
255
256
                        complexity=self.paradict['complexity'],
                        zerocenter=self.paradict['zerocenter'],
257
                        distances=self.distances,
258
                        harmonic=self.harmonic,
259
260
                        datamodel=self.datamodel,
                        comm=self.comm)
261
262

    def get_shape(self):
263
        return tuple(self.paradict['shape'])
Marco Selig's avatar
Marco Selig committed
264

265
266
267
268
    def _cast_to_d2o(self, x, dtype=None, hermitianize=True, **kwargs):
        casted_x = super(rg_space, self)._cast_to_d2o(x=x,
                                                      dtype=dtype,
                                                      **kwargs)
Ultima's avatar
Ultima committed
269
270
        if x is not None and hermitianize and \
           self.paradict['complexity'] == 1 and not casted_x.hermitian:
271
272
273
274
            about.warnings.cflush(
                 "WARNING: Data gets hermitianized. This operation is " +
                 "extremely expensive\n")
            casted_x = utilities.hermitianize(casted_x)
Marco Selig's avatar
Marco Selig committed
275

276
        return casted_x
277

278
279
280
281
    def _cast_to_np(self, x, dtype=None, hermitianize=True, **kwargs):
        casted_x = super(rg_space, self)._cast_to_np(x=x,
                                                     dtype=dtype,
                                                     **kwargs)
Ultima's avatar
Ultima committed
282
        if x is not None and hermitianize and self.paradict['complexity'] == 1:
283
284
285
286
            about.warnings.cflush(
                 "WARNING: Data gets hermitianized. This operation is " +
                 "extremely expensive\n")
            casted_x = utilities.hermitianize(casted_x)
Marco Selig's avatar
Marco Selig committed
287

288
        return casted_x
ultimanet's avatar
ultimanet committed
289

290
    def enforce_power(self, spec, size=None, kindex=None, codomain=None,
Ultima's avatar
Ultima committed
291
                      **kwargs):
Marco Selig's avatar
Marco Selig committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
        """
            Provides a valid power spectrum array from a given object.

            Parameters
            ----------
            spec : {float, list, numpy.ndarray, nifty.field, function}
                Fiducial power spectrum from which a valid power spectrum is to
                be calculated. Scalars are interpreted as constant power
                spectra.

            Returns
            -------
            spec : numpy.ndarray
                Valid power spectrum.

            Other parameters
            ----------------
            size : int, *optional*
                Number of bands the power spectrum shall have (default: None).
            kindex : numpy.ndarray, *optional*
                Scale of each band.
            codomain : nifty.space, *optional*
                A compatible codomain for power indexing (default: None).
            log : bool, *optional*
316
317
318
                Flag specifying if the spectral binning is performed on
                logarithmic scale or not; if set, the number of used bins is
                set automatically (if not given otherwise); by default no
319
                binning is done (default: None).
Marco Selig's avatar
Marco Selig committed
320
            nbin : integer, *optional*
321
                Number of used spectral bins; if given `log` is set to
322
323
                ``False``; iintegers below the minimum of 3 induce an automatic
                setting; by default no binning is done (default: None).
Marco Selig's avatar
Marco Selig committed
324
325
326
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
327
                (default: None).
Marco Selig's avatar
Marco Selig committed
328
        """
329
330
331
332
333
334
335

        # Setting up the local variables: kindex
        # The kindex is only necessary if spec is a function or if
        # the size is not set explicitly
        if kindex is None and (size is None or callable(spec)):
            # Determine which space should be used to get the kindex
            if self.harmonic:
336
337
                kindex_supply_space = self
            else:
338
339
                # Check if the given codomain is compatible with the space
                try:
340
341
342
                    assert(self.check_codomain(codomain))
                    kindex_supply_space = codomain
                except(AssertionError):
343
344
345
346
                    about.warnings.cprint("WARNING: Supplied codomain is " +
                                          "incompatible. Generating a " +
                                          "generic codomain. This can " +
                                          "be expensive!")
347
                    kindex_supply_space = self.get_codomain()
Ultima's avatar
Ultima committed
348

349
            kindex = kindex_supply_space.\
Ultima's avatar
Ultima committed
350
                power_indices.get_index_dict(**kwargs)['kindex']
351

352
353
354
        return self._enforce_power_helper(spec=spec,
                                          size=size,
                                          kindex=kindex)
355

356
    def check_codomain(self, codomain):
Marco Selig's avatar
Marco Selig committed
357
        """
358
            Checks whether a given codomain is compatible to the space or not.
Marco Selig's avatar
Marco Selig committed
359
360
361

            Parameters
            ----------
362
363
            codomain : nifty.space
                Space to be checked for compatibility.
Marco Selig's avatar
Marco Selig committed
364
365
366

            Returns
            -------
367
368
            check : bool
                Whether or not the given codomain is compatible to the space.
Marco Selig's avatar
Marco Selig committed
369
        """
370
371
        if codomain is None:
            return False
372

373
        if not isinstance(codomain, rg_space):
374
375
            raise TypeError(about._errors.cstring(
                "ERROR: The given codomain must be a nifty rg_space."))
376

377
378
        if self.datamodel is not codomain.datamodel:
            return False
379

380
381
382
        if self.comm is not codomain.comm:
            return False

383
        # check number of number and size of axes
384
385
        if not np.all(np.array(self.paradict['shape']) ==
                      np.array(codomain.paradict['shape'])):
386
            return False
Ultima's avatar
Ultima committed
387

388
389
390
        # check harmonic flag
        if self.harmonic == codomain.harmonic:
            return False
Ultima's avatar
Ultima committed
391

392
393
394
395
        # check complexity-type
        # prepare the shorthands
        dcomp = self.paradict['complexity']
        cocomp = codomain.paradict['complexity']
Ultima's avatar
Ultima committed
396

397
398
399
400
401
402
403
404
405
406
407
408
409
410
        # Case 1: if the domain is copmleteley complex
        # -> the codomain must be complex, too
        if dcomp == 2:
            if cocomp != 2:
                return False
        # Case 2: domain is hermitian
        # -> codmomain can be real. If it is marked as hermitian or even
        # fully complex, a warning is raised
        elif dcomp == 1:
            if cocomp > 0:
                about.warnings.cprint("WARNING: Unrecommended codomain! " +
                                      "The domain is hermitian, hence the " +
                                      "codomain should be restricted to " +
                                      "real values!")
Ultima's avatar
Ultima committed
411

412
413
414
415
416
417
418
419
420
421
        # Case 3: domain is real
        # -> codmain should be hermitian
        elif dcomp == 0:
            if cocomp == 2:
                about.warnings.cprint("WARNING: Unrecommended codomain! " +
                                      "The domain is real, hence the " +
                                      "codomain should be restricted to " +
                                      "hermitian configurations!")
            elif cocomp == 0:
                return False
Ultima's avatar
Ultima committed
422

423
424
        # Check if the distances match, i.e. dist'=1/(num*dist)
        if not np.all(
425
                np.absolute(np.array(self.paradict['shape']) *
426
427
428
                            np.array(self.distances) *
                            np.array(codomain.distances) - 1) < self.epsilon):
            return False
Ultima's avatar
Ultima committed
429

430
        return True
431

432
    def get_codomain(self, cozerocenter=None, **kwargs):
Marco Selig's avatar
Marco Selig committed
433
        """
434
435
436
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  either a shifted grid or a Fourier conjugate
            grid.
Marco Selig's avatar
Marco Selig committed
437
438
439

            Parameters
            ----------
440
441
442
443
444
            coname : string, *optional*
                String specifying a desired codomain (default: None).
            cozerocenter : {bool, numpy.ndarray}, *optional*
                Whether or not the grid is zerocentered for each axis or not
                (default: None).
Marco Selig's avatar
Marco Selig committed
445
446
447

            Returns
            -------
448
449
            codomain : nifty.rg_space
                A compatible codomain.
Marco Selig's avatar
Marco Selig committed
450

451
452
453
454
455
456
            Notes
            -----
            Possible arguments for `coname` are ``'f'`` in which case the
            codomain arises from a Fourier transformation, ``'i'`` in which
            case it arises from an inverse Fourier transformation.If no
            `coname` is given, the Fourier conjugate grid is produced.
Marco Selig's avatar
Marco Selig committed
457
        """
458
459
460
461
462
463
464
465
        naxes = len(self.get_shape())
        # Parse the cozerocenter input
        if(cozerocenter is None):
            cozerocenter = self.paradict['zerocenter']
        # if the input is something scalar, cast it to a boolean
        elif(np.isscalar(cozerocenter)):
            cozerocenter = bool(cozerocenter)
        # if it is not a scalar...
Marco Selig's avatar
Marco Selig committed
466
        else:
467
468
469
470
471
472
473
474
475
476
477
            # ...cast it to a numpy array of booleans
            cozerocenter = np.array(cozerocenter, dtype=np.bool)
            # if it was a list of length 1, extract the boolean
            if(np.size(cozerocenter) == 1):
                cozerocenter = np.asscalar(cozerocenter)
            # if the length of the input does not match the number of
            # dimensions, raise an exception
            elif(np.size(cozerocenter) != naxes):
                raise ValueError(about._errors.cstring(
                    "ERROR: size mismatch ( " +
                    str(np.size(cozerocenter)) + " <> " + str(naxes) + " )."))
Marco Selig's avatar
Marco Selig committed
478

479
        # Set up the initialization variables
480
481
482
        shape = self.paradict['shape']
        distances = 1 / (np.array(self.paradict['shape']) *
                         np.array(self.distances))
483
        datamodel = self.datamodel
484
        comm = self.comm
485
        complexity = {0: 1, 1: 0, 2: 2}[self.paradict['complexity']]
486
        harmonic = bool(not self.harmonic)
Marco Selig's avatar
Marco Selig committed
487

488
        new_space = rg_space(shape,
489
490
                             zerocenter=cozerocenter,
                             complexity=complexity,
491
                             distances=distances,
492
                             harmonic=harmonic,
493
494
                             datamodel=datamodel,
                             comm=comm)
495
        return new_space
Marco Selig's avatar
Marco Selig committed
496

497
    def get_random_values(self, **kwargs):
Marco Selig's avatar
Marco Selig committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
        """
            Generates random field values according to the specifications given
            by the parameters, taking into account possible complex-valuedness
            and hermitian symmetry.

            Returns
            -------
            x : numpy.ndarray
                Valid field values.

            Other parameters
            ----------------
            random : string, *optional*
                Specifies the probability distribution from which the random
                numbers are to be drawn.
                Supported distributions are:

                - "pm1" (uniform distribution over {+1,-1} or {+1,+i,-1,-i}
516
517
                - "gau" (normal distribution with zero-mean and a given
                    standard
Marco Selig's avatar
Marco Selig committed
518
519
520
521
522
523
524
525
526
527
                    deviation or variance)
                - "syn" (synthesizes from a given power spectrum)
                - "uni" (uniform distribution over [vmin,vmax[)

                (default: None).
            dev : float, *optional*
                Standard deviation (default: 1).
            var : float, *optional*
                Variance, overriding `dev` if both are specified
                (default: 1).
528
529
            spec : {scalar, list, numpy.ndarray, nifty.field, function},
                *optional*
Marco Selig's avatar
Marco Selig committed
530
531
532
533
534
535
536
                Power spectrum (default: 1).
            pindex : numpy.ndarray, *optional*
                Indexing array giving the power spectrum index of each band
                (default: None).
            kindex : numpy.ndarray, *optional*
                Scale of each band (default: None).
            codomain : nifty.rg_space, *optional*
Ultimanet's avatar
Ultimanet committed
537
                A compatible codomain (default: None).
Marco Selig's avatar
Marco Selig committed
538
            log : bool, *optional*
539
540
                Flag specifying if the spectral binning is performed on
                    logarithmic
Marco Selig's avatar
Marco Selig committed
541
542
543
544
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer, *optional*
545
546
                Number of used spectral bins; if given `log` is set to
                    ``False``;
Marco Selig's avatar
Marco Selig committed
547
548
549
550
551
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
552
                (default: None).
Ultimanet's avatar
Ultimanet committed
553
            vmin : float, *optional*
Marco Selig's avatar
Marco Selig committed
554
555
556
557
                Lower limit for a uniform distribution (default: 0).
            vmax : float, *optional*
                Upper limit for a uniform distribution (default: 1).
        """
558
        # Parse the keyword arguments
559
        arg = random.parse_arguments(self, **kwargs)
560

Ultima's avatar
Ultima committed
561
562
        # Should the output be hermitianized?
        hermitianizeQ = (self.paradict['complexity'] == 1)
Ultimanet's avatar
Ultimanet committed
563

564
        # Case 1: uniform distribution over {-1,+1}/{1,i,-1,-i}
Ultima's avatar
Ultima committed
565
566
        if arg['random'] == 'pm1' and not hermitianizeQ:
            sample = super(rg_space, self).get_random_values(**arg)
567

Ultima's avatar
Ultima committed
568
        elif arg['random'] == 'pm1' and hermitianizeQ:
569
            sample = self.get_random_values(random='uni', vmin=-1, vmax=1)
Ultima's avatar
Ultima committed
570

571
            if issubclass(sample.dtype.type, np.complexfloating):
Ultima's avatar
Ultima committed
572
573
574
575
576
                temp_data = sample.copy()
                sample[temp_data.real >= 0.5] = 1
                sample[(temp_data.real >= 0) * (temp_data.real < 0.5)] = -1
                sample[(temp_data.real < 0) * (temp_data.imag >= 0)] = 1j
                sample[(temp_data.real < 0) * (temp_data.imag < 0)] = -1j
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
                # Set the mirroring invariant points to real values
                product_list = []
                for s in self.get_shape():
                    # if the particular dimension has even length, set
                    # also the middle of the array to a real value
                    if s % 2 == 0:
                        product_list += [[0, s/2]]
                    else:
                        product_list += [[0]]

                for i in itertools.product(*product_list):
                    sample[i] = {1: 1,
                                 -1: -1,
                                 1j: 1,
                                 -1j: -1}[sample[i]]
Ultimanet's avatar
Ultimanet committed
592
            else:
Ultima's avatar
Ultima committed
593
594
                sample[sample >= 0] = 1
                sample[sample < 0] = -1
595
596

        # Case 2: normal distribution with zero-mean and a given standard
597
        #         deviation or variance
Ultima's avatar
Ultima committed
598
599
        elif arg['random'] == 'gau':
            sample = super(rg_space, self).get_random_values(**arg)
600

601
            if hermitianizeQ:
Ultimanet's avatar
Ultimanet committed
602
                sample = utilities.hermitianize(sample)
Ultimanet's avatar
Ultimanet committed
603

604
        # Case 3: uniform distribution
Ultima's avatar
Ultima committed
605
606
        elif arg['random'] == "uni" and not hermitianizeQ:
            sample = super(rg_space, self).get_random_values(**arg)
607

Ultima's avatar
Ultima committed
608
        elif arg['random'] == "uni" and hermitianizeQ:
609
610
611
612
613
            # For a hermitian uniform sample, generate a gaussian one
            # and then convert it to a uniform one
            sample = self.get_random_values(random='gau')
            # Use the cummulative of the gaussian, the error function in order
            # to transform it to a uniform distribution.
614
            if issubclass(sample.dtype.type, np.complexfloating):
Ultima's avatar
Ultima committed
615
                def temp_erf(x):
616
                    return erf(x.real) + 1j * erf(x.imag)
Ultimanet's avatar
Ultimanet committed
617
            else:
Ultima's avatar
Ultima committed
618
                def temp_erf(x):
619
                    return erf(x / np.sqrt(2))
Ultima's avatar
Ultima committed
620
621
622
623
624
625
626
627

            if self.datamodel == 'np':
                sample = temp_erf(sample)
            elif self.datamodel in RG_DISTRIBUTION_STRATEGIES:
                sample.apply_scalar_function(function=temp_erf, inplace=True)
            else:
                raise NotImplementedError(about._errors.cstring(
                    "ERROR: function is not implemented for given datamodel."))
628
629

            # Shift and stretch the uniform distribution into the given limits
630
            # sample = (sample + 1)/2 * (vmax-vmin) + vmin
Ultima's avatar
Ultima committed
631
632
            vmin = arg['vmin']
            vmax = arg['vmax']
633
634
            sample *= (vmax - vmin) / 2.
            sample += 1 / 2. * (vmax + vmin)
Marco Selig's avatar
Marco Selig committed
635

Ultima's avatar
Ultima committed
636
637
638
639
        elif(arg['random'] == "syn"):
            spec = arg['spec']
            kpack = arg['kpack']
            harmonic_domain = arg['harmonic_domain']
Ultima's avatar
Ultima committed
640
641
642
643
644
            lnb_dict = {}
            for name in ('log', 'nbin', 'binbounds'):
                if arg[name] != 'default':
                    lnb_dict[name] = arg[name]

645
646
647
            # Check whether there is a kpack available or not.
            # kpack is only used for computing kdict and extracting kindex
            # If not, take kdict and kindex from the fourier_domain
648
            if kpack is None:
Ultimanet's avatar
Ultimanet committed
649
                power_indices =\
Ultima's avatar
Ultima committed
650
                    harmonic_domain.power_indices.get_index_dict(**lnb_dict)
651

Ultimanet's avatar
Ultimanet committed
652
653
654
655
656
657
                kindex = power_indices['kindex']
                kdict = power_indices['kdict']
                kpack = [power_indices['pindex'], power_indices['kindex']]
            else:
                kindex = kpack[1]
                kdict = harmonic_domain.power_indices.\
658
659
660
661
662
663
664
665
                    _compute_kdict_from_pindex_kindex(kpack[0], kpack[1])

            # draw the random samples
            # Case 1: self is a harmonic space
            if self.harmonic:
                # subcase 1: self is real
                # -> simply generate a random field in fourier space and
                # weight the entries accordingly to the powerspectrum
Ultimanet's avatar
Ultimanet committed
666
                if self.paradict['complexity'] == 0:
Ultima's avatar
Ultima committed
667
668
669
                    sample = self.get_random_values(random='gau',
                                                    mean=0,
                                                    std=1)
670
671
672
673
674
                # subcase 2: self is hermitian but probably complex
                # -> generate a real field (in position space) and transform
                # it to harmonic space -> field in harmonic space is
                # hermitian. Now weight the modes accordingly to the
                # powerspectrum.
Ultimanet's avatar
Ultimanet committed
675
676
                elif self.paradict['complexity'] == 1:
                    temp_codomain = self.get_codomain()
Ultima's avatar
Ultima committed
677
678
679
                    sample = temp_codomain.get_random_values(random='gau',
                                                             mean=0,
                                                             std=1)
680
681
682
683
684
685

                    # In order to get the normalisation right, the sqrt
                    # of self.dim must be divided out.
                    # Furthermore, the normalisation in the fft routine
                    # must be undone
                    # TODO: Insert explanation
686
                    sqrt_of_dim = np.sqrt(self.get_dim())
Ultimanet's avatar
Ultimanet committed
687
688
689
                    sample /= sqrt_of_dim
                    sample = temp_codomain.calc_weight(sample, power=-1)

690
                    # tronsform the random field to harmonic space
Ultimanet's avatar
Ultimanet committed
691
                    sample = temp_codomain.\
692
693
694
695
                        calc_transform(sample, codomain=self)

                    # ensure that the kdict and the harmonic_sample have the
                    # same distribution strategy
Ultima's avatar
Ultima committed
696
697
698
699
700
                    try:
                        assert(kdict.distribution_strategy ==
                               sample.distribution_strategy)
                    except AttributeError:
                        pass
701
702
703
704

                # subcase 3: self is fully complex
                # -> generate a complex random field in harmonic space and
                # weight the modes accordingly to the powerspectrum
Ultimanet's avatar
Ultimanet committed
705
                elif self.paradict['complexity'] == 2:
Ultima's avatar
Ultima committed
706
707
708
                    sample = self.get_random_values(random='gau',
                                                    mean=0,
                                                    std=1)
709

710
                # apply the powerspectrum renormalization
Ultima's avatar
Ultima committed
711
712
713
714
715
716
                if self.datamodel == 'np':
                    rescaler = np.sqrt(spec[np.searchsorted(kindex, kdict)])
                    sample *= rescaler
                elif self.datamodel in RG_DISTRIBUTION_STRATEGIES:
                    # extract the local data from kdict
                    local_kdict = kdict.get_local_data()
717
                    print ('local_kdict', local_kdict)
Ultima's avatar
Ultima committed
718
719
                    rescaler = np.sqrt(
                        spec[np.searchsorted(kindex, local_kdict)])
720
721
                    print ('rescaler', rescaler)
                    print ('sample', sample.distribution_strategy)
Ultima's avatar
Ultima committed
722
723
724
725
726
727
                    sample.apply_scalar_function(lambda x: x * rescaler,
                                                 inplace=True)
                else:
                    raise NotImplementedError(about._errors.cstring(
                        "ERROR: function is not implemented for given " +
                        "datamodel."))
728
            # Case 2: self is a position space
Ultimanet's avatar
Ultimanet committed
729
            else:
730
731
                # get a suitable codomain
                temp_codomain = self.get_codomain()
Ultimanet's avatar
Ultimanet committed
732

733
734
735
                # subcase 1: self is a real space.
                # -> generate a hermitian sample with the codomain in harmonic
                # space and make a fourier transformation.
Ultimanet's avatar
Ultimanet committed
736
                if self.paradict['complexity'] == 0:
737
                    # check that the codomain is hermitian
Ultimanet's avatar
Ultimanet committed
738
                    assert(temp_codomain.paradict['complexity'] == 1)
739
740
741
742

                # subcase 2: self is hermitian but probably complex
                # -> generate a real-valued random sample in fourier space
                # and transform it to real space
Ultimanet's avatar
Ultimanet committed
743
                elif self.paradict['complexity'] == 1:
744
745
                    # check that the codomain is real
                    assert(temp_codomain.paradict['complexity'] == 0)
Ultimanet's avatar
Ultimanet committed
746

747
748
749
750
751
752
                # subcase 3: self is fully complex
                # -> generate a complex-valued random sample in fourier space
                # and transform it to real space
                elif self.paradict['complexity'] == 2:
                    # check that the codomain is real
                    assert(temp_codomain.paradict['complexity'] == 2)
Ultimanet's avatar
Ultimanet committed
753

754
755
                # Get a hermitian/real/complex sample in harmonic space from
                # the codomain
Ultima's avatar
Ultima committed
756
757
758
759
760
761
762
763
                sample = temp_codomain.get_random_values(random='syn',
                                                         pindex=kpack[0],
                                                         kindex=kpack[1],
                                                         spec=spec,
                                                         codomain=self,
                                                         log=log,
                                                         nbin=nbin,
                                                         binbounds=binbounds)
764

765
                # Perform a fourier transform
Ultima's avatar
Ultima committed
766
                sample = temp_codomain.calc_transform(sample, codomain=self)
Ultimanet's avatar
Ultimanet committed
767
768

            if self.paradict['complexity'] == 1:
Ultima's avatar
Ultima committed
769
770
771
772
                try:
                    sample.hermitian = True
                except AttributeError:
                    pass
773

Ultimanet's avatar
Ultimanet committed
774
775
        else:
            raise KeyError(about._errors.cstring(
Ultima's avatar
Ultima committed
776
                "ERROR: unsupported random key '" + str(arg['random']) + "'."))
Marco Selig's avatar
Marco Selig committed
777

778
        return sample
Marco Selig's avatar
Marco Selig committed
779

Ultimanet's avatar
Ultimanet committed
780
    def calc_weight(self, x, power=1):
Marco Selig's avatar
Marco Selig committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
        """
            Weights a given array with the pixel volumes to a given power.

            Parameters
            ----------
            x : numpy.ndarray
                Array to be weighted.
            power : float, *optional*
                Power of the pixel volumes to be used (default: 1).

            Returns
            -------
            y : numpy.ndarray
                Weighted array.
        """
796
797
        # weight
        x = x * self.get_weight(power=power)
Ultimanet's avatar
Ultimanet committed
798
        return x
Marco Selig's avatar
Marco Selig committed
799

800
    def get_weight(self, power=1):
801
        return np.prod(self.distances)**power
802

803
    def calc_dot(self, x, y):
Marco Selig's avatar
Marco Selig committed
804
        """
805
806
            Computes the discrete inner product of two given arrays of field
            values.
Marco Selig's avatar
Marco Selig committed
807
808
809
810
811
812
813
814
815
816
817
818
819

            Parameters
            ----------
            x : numpy.ndarray
                First array
            y : numpy.ndarray
                Second array

            Returns
            -------
            dot : scalar
                Inner product of the two arrays.
        """
820
821
        x = self.cast(x)
        y = self.cast(y)
822

823
824
        if self.datamodel == 'np':
            result = np.vdot(x, y)
825
        elif self.datamodel in RG_DISTRIBUTION_STRATEGIES:
826
827
828
829
830
            result = x.vdot(y)
        else:
            raise NotImplementedError(about._errors.cstring(
                "ERROR: function is not implemented for given datamodel."))

831
        if np.isreal(result):
832
            result = np.asscalar(np.real(result))
Ultimanet's avatar
Ultimanet committed
833
        if self.paradict['complexity'] != 2:
834
835
            if (np.absolute(result.imag) >
                    self.epsilon**2 * np.absolute(result.real)):
Ultimanet's avatar
Ultimanet committed
836
837
                about.warnings.cprint(
                    "WARNING: Discarding considerable imaginary part.")
838
            result = np.asscalar(np.real(result))
839
        return result
Marco Selig's avatar
Marco Selig committed
840

841
    def calc_transform(self, x, codomain=None, **kwargs):
Marco Selig's avatar
Marco Selig committed
842
843
844
845
846
847
848
849
        """
            Computes the transform of a given array of field values.

            Parameters
            ----------
            x : numpy.ndarray
                Array to be transformed.
            codomain : nifty.rg_space, *optional*
850
                codomain space to which the transformation shall map
Marco Selig's avatar
Marco Selig committed
851
852
853
854
855
856
857
                (default: None).

            Returns
            -------
            Tx : numpy.ndarray
                Transformed array
        """
858
        x = self.cast(x)
859

860
        if codomain is None:
Ultimanet's avatar
Ultimanet committed
861
            codomain = self.get_codomain()
862
863

        # Check if the given codomain is suitable for the transformation
864
        if not self.check_codomain(codomain):
865
            raise ValueError(about._errors.cstring(
866
                "ERROR: unsupported codomain."))
867

868
        if codomain.harmonic:
869
            # correct for forward fft
870
            x = self.calc_weight(x, power=1)
871
872
873

        # Perform the transformation
        Tx = self.fft_machine.transform(val=x, domain=self, codomain=codomain,
874
875
                                        **kwargs)

876
        if not codomain.harmonic:
877
            # correct for inverse fft
Ultimanet's avatar
Ultimanet committed
878
879
            Tx = codomain.calc_weight(Tx, power=-1)

880
881
882
        # when the codomain space is purely real, the result of the
        # transformation must be corrected accordingly. Using the casting
        # method of codomain is sufficient
883
        # TODO: Let .transform  yield the correct dtype
884
        Tx = codomain.cast(Tx)
885

886
887
        return Tx

Ultimanet's avatar
Ultimanet committed
888
    def calc_smooth(self, x, sigma=0, codomain=None):
Marco Selig's avatar
Marco Selig committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
        """
            Smoothes an array of field values by convolution with a Gaussian
            kernel.

            Parameters
            ----------
            x : numpy.ndarray
                Array of field values to be smoothed.
            sigma : float, *optional*
                Standard deviation of the Gaussian kernel, specified in units
                of length in position space; for testing: a sigma of -1 will be
                reset to a reasonable value (default: 0).

            Returns
            -------
            Gx : numpy.ndarray
                Smoothed array.
        """

908
        # Check sigma
Ultimanet's avatar
Ultimanet committed
909
        if sigma == 0:
Ultima's avatar
Ultima committed
910
            return self.unary_operation(x, op='copy')
Ultimanet's avatar
Ultimanet committed
911
912
913
        elif sigma == -1:
            about.infos.cprint(
                "INFO: Resetting sigma to sqrt(2)*max(dist).")
914
            sigma = np.sqrt(2) * np.max(self.distances)
915
        elif(sigma < 0):
Marco Selig's avatar
Marco Selig committed
916
            raise ValueError(about._errors.cstring("ERROR: invalid sigma."))
Ultimanet's avatar
Ultimanet committed
917

918
        # if a codomain was given...
919
        if codomain is not None:
920
            # ...check if it was suitable
Ultimanet's avatar
Ultimanet committed
921
922
            if not self.check_codomain(codomain):
                raise ValueError(about._errors.cstring(
923
924
                    "ERROR: the given codomain is not a compatible!"))
        else:
Ultimanet's avatar
Ultimanet committed
925
926
            codomain = self.get_codomain()

927
928
929
930
        x = self.calc_transform(x, codomain=codomain)
        x = codomain._calc_smooth_helper(x, sigma)
        x = codomain.calc_transform(x, codomain=self)
        return x
931

932
933
    def _calc_smooth_helper(self, x, sigma):
        # multiply the gaussian kernel, etc...
934
935
936
937
938
939

        # Cast the input
        x = self.cast(x)

        # if x is hermitian it remains hermitian during smoothing
        if self.datamodel in RG_DISTRIBUTION_STRATEGIES:
940
            remeber_hermitianQ = x.hermitian
Ultimanet's avatar
Ultimanet committed
941

942
943
944
945
        # Define the Gaussian kernel function
        gaussian = lambda x: np.exp(-2. * np.pi**2 * x**2 * sigma**2)

        # Define the variables in the dialect of the legacy smoothing.py
946
947
        nx = np.array(self.get_shape())
        dx = 1 / nx / self.distances
948
        # Multiply the data along each axis with suitable the gaussian kernel
Ultimanet's avatar
Ultimanet committed
949
        for i in range(len(nx)):
950
951
            # Prepare the exponent
            dk = 1. / nx[i] / dx[i]
Ultimanet's avatar
Ultimanet committed
952
            nk = nx[i]
953
            k = -0.5 * nk * dk + np.arange(nk) * dk
Ultimanet's avatar
Ultimanet committed
954
955
            if self.paradict['zerocenter'][i] == False:
                k = np.fft.fftshift(k)
956
            # compute the actual kernel vector
Ultimanet's avatar
Ultimanet committed
957
            gaussian_kernel_vector = gaussian(k)
958
959
            # blow up the vector to an array of shape (1,.,1,len(nk),1,.,1)
            blown_up_shape = [1, ] * len(nx)
Ultimanet's avatar
Ultimanet committed
960
961
962
            blown_up_shape[i] = len(gaussian_kernel_vector)
            gaussian_kernel_vector =\
                gaussian_kernel_vector.reshape(blown_up_shape)
963
964
            # apply the blown-up gaussian_kernel_vector
            x = x*gaussian_kernel_vector
965

966
        try:
967
            x.hermitian = remeber_hermitianQ
968
969
        except AttributeError:
            pass
970

Ultimanet's avatar
Ultimanet committed
971
        return x
Marco Selig's avatar
Marco Selig committed
972

973
    def calc_power(self, x, **kwargs):
Marco Selig's avatar
Marco Selig committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
        """
            Computes the power of an array of field values.

            Parameters
            ----------
            x : numpy.ndarray
                Array containing the field values of which the power is to be
                calculated.

            Returns
            -------
            spec : numpy.ndarray
                Power contained in the input array.

            Other parameters
            ----------------
            pindex : numpy.ndarray, *optional*
                Indexing array assigning the input array components to
                components of the power spectrum (default: None).
            rho : numpy.ndarray, *optional*
                Number of degrees of freedom per band (default: None).
            codomain : nifty.space, *optional*
                A compatible codomain for power indexing (default: None).
            log : bool, *optional*
998
999
                Flag specifying if the spectral binning is performed on
                logarithmic
Marco Selig's avatar
Marco Selig committed
1000
1001
1002
1003
                scale or not; if set, the number of used bins is set
                automatically (if not given otherwise); by default no binning
                is done (default: None).
            nbin : integer, *optional*
1004
1005
                Number of used spectral bins; if given `log` is set to
                ``False``;
Marco Selig's avatar
Marco Selig committed
1006
1007
1008
1009
1010
                integers below the minimum of 3 induce an automatic setting;
                by default no binning is done (default: None).
            binbounds : {list, array}, *optional*
                User specific inner boundaries of the bins, which are preferred
                over the above parameters; by default no binning is done
1011
                (default: None).
Marco Selig's avatar
Marco Selig committed
1012
1013

        """
Ultimanet's avatar
Ultimanet committed
1014
1015
        x = self.cast(x)

1016
        # If self is a position space, delegate calc_power to its codomain.
1017
        if not self.harmonic:
Marco Selig's avatar
Marco Selig committed
1018
            try:
1019
                codomain = kwargs['codomain']
Ultimanet's avatar
Ultimanet committed
1020
1021
            except(KeyError):
                codomain = self.get_codomain()
1022

Ultimanet's avatar
Ultimanet committed
1023
1024
1025
            y = self.calc_transform(x, codomain)
            kwargs.update({'codomain': self})
            return codomain.calc_power(y, **kwargs)
1026
1027
1028
1029
1030

        # If some of the pindex, kindex or rho arrays are given explicitly,
        # favor them over those from the self.power_indices dictionary.
        # As the default value in kwargs.get(key, default) does NOT evaluate
        # lazy, a distinction of cases is necessary. Otherwise the
Ultima's avatar
Ultima committed
1031
1032
        # powerindices might be computed, although not needed
        if 'pindex' in kwargs and 'rho' in kwargs:
Ultimanet's avatar
Ultimanet committed
1033
1034
1035
            pindex = kwargs.get('pindex')
            rho = kwargs.get('rho')
        else:
Ultima's avatar
Ultima committed
1036
            power_indices = self.power_indices.get_index_dict(**kwargs)
Ultimanet's avatar
Ultimanet committed
1037
1038
            pindex = kwargs.get('pindex', power_indices['pindex'])
            rho = kwargs.get('rho', power_indices['rho'])
1039

Ultimanet's avatar
Ultimanet committed
1040
        fieldabs = abs(x)**2
1041
        power_spectrum = np.zeros(rho.shape)
1042

1043
        if self.datamodel == 'np':
1044
1045
            power_spectrum = np.bincount(pindex.flatten(),
                                         weights=fieldabs.flatten())
1046
        elif self.datamodel in RG_DISTRIBUTION_STRATEGIES:
1047
            power_spectrum = pindex.bincount(weights=fieldabs)
1048
1049
1050
        else:
            raise NotImplementedError(about._errors.cstring(
                "ERROR: function is not implemented for given datamodel."))
1051
1052

        # Divide out the degeneracy factor
Ultimanet's avatar
Ultimanet committed
1053
1054
        power_spectrum /= rho
        return power_spectrum
Marco Selig's avatar
Marco Selig committed
1055
1056
1057
1058
1059
1060