field.py 32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

csongor's avatar
csongor committed
19
20
21
from __future__ import division
import numpy as np

Theo Steininger's avatar
Theo Steininger committed
22
23
from keepers import Versionable,\
                    Loggable
Jait Dixit's avatar
Jait Dixit committed
24

25
from d2o import distributed_data_object,\
26
    STRATEGIES as DISTRIBUTION_STRATEGIES
csongor's avatar
csongor committed
27

28
from nifty.config import nifty_configuration as gc
csongor's avatar
csongor committed
29

30
from nifty.domain_object import DomainObject
31

32
from nifty.spaces.power_space import PowerSpace
csongor's avatar
csongor committed
33

csongor's avatar
csongor committed
34
import nifty.nifty_utilities as utilities
35
36
from nifty.random import Random

csongor's avatar
csongor committed
37

Jait Dixit's avatar
Jait Dixit committed
38
class Field(Loggable, Versionable, object):
theos's avatar
theos committed
39
    # ---Initialization methods---
40

41
    def __init__(self, domain=None, val=None, dtype=None,
42
                 distribution_strategy=None, copy=False):
csongor's avatar
csongor committed
43

44
        self.domain = self._parse_domain(domain=domain, val=val)
45
        self.domain_axes = self._get_axes_tuple(self.domain)
csongor's avatar
csongor committed
46

theos's avatar
theos committed
47
        self.dtype = self._infer_dtype(dtype=dtype,
48
                                       val=val)
49

50
51
52
        self.distribution_strategy = self._parse_distribution_strategy(
                                distribution_strategy=distribution_strategy,
                                val=val)
csongor's avatar
csongor committed
53

54
55
56
57
        if val is None:
            self._val = None
        else:
            self.set_val(new_val=val, copy=copy)
csongor's avatar
csongor committed
58

59
    def _parse_domain(self, domain, val=None):
60
        if domain is None:
61
62
63
64
            if isinstance(val, Field):
                domain = val.domain
            else:
                domain = ()
65
        elif isinstance(domain, DomainObject):
66
            domain = (domain,)
67
68
69
        elif not isinstance(domain, tuple):
            domain = tuple(domain)

csongor's avatar
csongor committed
70
        for d in domain:
71
            if not isinstance(d, DomainObject):
72
73
                raise TypeError(
                    "Given domain contains something that is not a "
74
                    "DomainObject instance.")
csongor's avatar
csongor committed
75
76
        return domain

theos's avatar
theos committed
77
78
79
80
81
82
83
84
85
86
    def _get_axes_tuple(self, things_with_shape, start=0):
        i = start
        axes_list = []
        for thing in things_with_shape:
            l = []
            for j in range(len(thing.shape)):
                l += [i]
                i += 1
            axes_list += [tuple(l)]
        return tuple(axes_list)
87

88
    def _infer_dtype(self, dtype, val):
csongor's avatar
csongor committed
89
        if dtype is None:
90
            try:
91
                dtype = val.dtype
92
            except AttributeError:
Theo Steininger's avatar
Theo Steininger committed
93
94
95
                try:
                    if val is None:
                        raise TypeError
96
                    dtype = np.result_type(val)
Theo Steininger's avatar
Theo Steininger committed
97
                except(TypeError):
98
                    dtype = np.dtype(gc['default_field_dtype'])
theos's avatar
theos committed
99
        else:
100
            dtype = np.dtype(dtype)
101

theos's avatar
theos committed
102
        return dtype
103

104
105
    def _parse_distribution_strategy(self, distribution_strategy, val):
        if distribution_strategy is None:
106
            if isinstance(val, distributed_data_object):
107
                distribution_strategy = val.distribution_strategy
108
            elif isinstance(val, Field):
109
                distribution_strategy = val.distribution_strategy
110
            else:
111
                self.logger.debug("distribution_strategy set to default!")
112
                distribution_strategy = gc['default_distribution_strategy']
113
        elif distribution_strategy not in DISTRIBUTION_STRATEGIES['global']:
114
115
116
            raise ValueError(
                    "distribution_strategy must be a global-type "
                    "strategy.")
117
        return distribution_strategy
118
119

    # ---Factory methods---
120

121
    @classmethod
122
    def from_random(cls, random_type, domain=None, dtype=None,
123
                    distribution_strategy=None, **kwargs):
124
        # create a initially empty field
125
        f = cls(domain=domain, dtype=dtype,
126
                distribution_strategy=distribution_strategy)
127
128
129
130
131
132
133

        # now use the processed input in terms of f in order to parse the
        # random arguments
        random_arguments = cls._parse_random_arguments(random_type=random_type,
                                                       f=f,
                                                       **kwargs)

Martin Reinecke's avatar
Martin Reinecke committed
134
        # extract the distributed_data_object from f and apply the appropriate
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        # random number generator to it
        sample = f.get_val(copy=False)
        generator_function = getattr(Random, random_type)
        sample.apply_generator(
            lambda shape: generator_function(dtype=f.dtype,
                                             shape=shape,
                                             **random_arguments))
        return f

    @staticmethod
    def _parse_random_arguments(random_type, f, **kwargs):

        if random_type == "pm1":
            random_arguments = {}

        elif random_type == "normal":
            mean = kwargs.get('mean', 0)
            std = kwargs.get('std', 1)
            random_arguments = {'mean': mean,
                                'std': std}

        elif random_type == "uniform":
            low = kwargs.get('low', 0)
            high = kwargs.get('high', 1)
            random_arguments = {'low': low,
                                'high': high}

csongor's avatar
csongor committed
162
        else:
163
164
            raise KeyError(
                "unsupported random key '" + str(random_type) + "'.")
csongor's avatar
csongor committed
165

166
        return random_arguments
csongor's avatar
csongor committed
167

168
169
    # ---Powerspectral methods---

Jakob Knollmueller's avatar
Jakob Knollmueller committed
170
    def power_analyze(self, spaces=None, logarithmic=False, nbin=None,
171
                      binbounds=None, decompose_power=True):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
172
        """ Computes the square root power spectrum for a subspace of `self`.
173
174

        Creates a PowerSpace for the space addressed by `spaces` with the given
Jakob Knollmueller's avatar
Jakob Knollmueller committed
175
        binning and computes the power spectrum as a Field over this
176
        PowerSpace. This can only be done if the subspace to  be analyzed is a
Jakob Knollmueller's avatar
Jakob Knollmueller committed
177
178
        harmonic space. The resulting field has the same units as the initial
        field, corresponding to the square root of the power spectrum.
179
180
181
182

        Parameters
        ----------
        spaces : int *optional*
Jakob Knollmueller's avatar
Jakob Knollmueller committed
183
            The subspace for which the powerspectrum shall be computed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
            (default : None).
            if spaces==None : Tries to synthesize for the whole domain
        logarithmic : boolean *optional*
            True if the output PowerSpace should use logarithmic binning.
            {default : False}
        nbin : int *optional*
            The number of bins the resulting PowerSpace shall have
            (default : None).
            if nbin==None : maximum number of bins is used
        binbounds : array-like *optional*
            Inner bounds of the bins (default : None).
            if binbounds==None : bins are inferred. Overwrites nbins and log
        decompose_power : boolean, *optional*
            Whether the analysed signal-space Field is intrinsically real or
Jakob Knollmueller's avatar
Jakob Knollmueller committed
198
            complex and if the power spectrum shall therefore be computed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
            for the real and the imaginary part of the Field separately
            (default : True).

        Raise
        -----
        ValueError
            Raised if
                *len(domain) is != 1 when spaces==None
                *len(spaces) is != 1 if not None
                *the analyzed space is not harmonic

        Returns
        -------
        out : Field
            The output object. It's domain is a PowerSpace and it contains
Jakob Knollmueller's avatar
Jakob Knollmueller committed
214
            the power spectrum of 'self's field.
215
216
217

        See Also
        --------
Jakob Knollmueller's avatar
Jakob Knollmueller committed
218
        power_synthesize, PowerSpace
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

        """

        # check if all spaces in `self.domain` are either harmonic or
        # power_space instances
        for sp in self.domain:
            if not sp.harmonic and not isinstance(sp, PowerSpace):
                self.logger.info(
                    "Field has a space in `domain` which is neither "
                    "harmonic nor a PowerSpace.")

        # check if the `spaces` input is valid
        spaces = utilities.cast_axis_to_tuple(spaces, len(self.domain))
        if spaces is None:
            if len(self.domain) == 1:
                spaces = (0,)
            else:
                raise ValueError(
                    "Field has multiple spaces as domain "
                    "but `spaces` is None.")

        if len(spaces) == 0:
            raise ValueError(
                "No space for analysis specified.")
        elif len(spaces) > 1:
            raise ValueError(
                "Conversion of only one space at a time is allowed.")

        space_index = spaces[0]

        if not self.domain[space_index].harmonic:
            raise ValueError(
                "The analyzed space must be harmonic.")

        # Create the target PowerSpace instance:
        # If the associated signal-space field was real, we extract the
        # hermitian and anti-hermitian parts of `self` and put them
Jakob Knollmueller's avatar
Jakob Knollmueller committed
256
257
        # into the real and imaginary parts of the power spectrum.
        # If it was complex, all the power is put into a real power spectrum.
258
259
260
261
262
263
264
265
266
267
268
269
270

        distribution_strategy = \
            self.val.get_axes_local_distribution_strategy(
                self.domain_axes[space_index])

        harmonic_domain = self.domain[space_index]
        power_domain = PowerSpace(harmonic_partner=harmonic_domain,
                                  distribution_strategy=distribution_strategy,
                                  logarithmic=logarithmic, nbin=nbin,
                                  binbounds=binbounds)

        # extract pindex and rho from power_domain
        pindex = power_domain.pindex
Jakob Knollmueller's avatar
Jakob Knollmueller committed
271
        rho = power_domain.rho
272
273
274
275
276
277
278
279

        if decompose_power:
            hermitian_part, anti_hermitian_part = \
                harmonic_domain.hermitian_decomposition(
                                            self.val,
                                            axes=self.domain_axes[space_index])

            [hermitian_power, anti_hermitian_power] = \
Jakob Knollmueller's avatar
Jakob Knollmueller committed
280
                [self._calculate_power_spectrum(
281
282
                                            x=part,
                                            pindex=pindex,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
283
                                            rho=rho,
284
285
286
                                            axes=self.domain_axes[space_index])
                 for part in [hermitian_part, anti_hermitian_part]]

Jakob Knollmueller's avatar
Jakob Knollmueller committed
287
288
            power_spectrum = hermitian_power + 1j * anti_hermitian_power

289
        else:
Jakob Knollmueller's avatar
Jakob Knollmueller committed
290
            power_spectrum = self._calculate_power_spectrum(
291
292
                                            x=self.val,
                                            pindex=pindex,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
293
                                            rho=rho,
294
295
296
297
298
                                            axes=self.domain_axes[space_index])

        # create the result field and put power_spectrum into it
        result_domain = list(self.domain)
        result_domain[space_index] = power_domain
Jakob Knollmueller's avatar
Jakob Knollmueller committed
299
        result_dtype = power_spectrum.dtype
300
301
302
303

        result_field = self.copy_empty(
                   domain=result_domain,
                   dtype=result_dtype,
Jakob Knollmueller's avatar
Jakob Knollmueller committed
304
305
                   distribution_strategy=power_spectrum.distribution_strategy)
        result_field.set_val(new_val=power_spectrum, copy=False)
306
307
308

        return result_field

Jakob Knollmueller's avatar
Jakob Knollmueller committed
309
    def _calculate_power_spectrum(self, x, pindex, rho, axes=None):
310
311
312
313
314
315
316
317
318
        fieldabs = abs(x)
        fieldabs **= 2

        if axes is not None:
            pindex = self._shape_up_pindex(
                                    pindex=pindex,
                                    target_shape=x.shape,
                                    target_strategy=x.distribution_strategy,
                                    axes=axes)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
319
        power_spectrum = pindex.bincount(weights=fieldabs,
320
321
322
323
324
325
326
327
328
329
330
331
332
                                         axis=axes)
        if axes is not None:
            new_rho_shape = [1, ] * len(power_spectrum.shape)
            new_rho_shape[axes[0]] = len(rho)
            rho = rho.reshape(new_rho_shape)
        power_spectrum /= rho

        power_spectrum **= 0.5
        return power_spectrum

    def _shape_up_pindex(self, pindex, target_shape, target_strategy, axes):
        if pindex.distribution_strategy not in \
                DISTRIBUTION_STRATEGIES['global']:
333
            raise ValueError("pindex's distribution strategy must be "
334
335
336
337
338
339
                             "global-type")

        if pindex.distribution_strategy in DISTRIBUTION_STRATEGIES['slicing']:
            if ((0 not in axes) or
                    (target_strategy is not pindex.distribution_strategy)):
                raise ValueError(
340
                    "A slicing distributor shall not be reshaped to "
341
342
343
344
345
346
347
348
349
350
351
352
353
                    "something non-sliced.")

        semiscaled_shape = [1, ] * len(target_shape)
        for i in axes:
            semiscaled_shape[i] = target_shape[i]
        local_data = pindex.get_local_data(copy=False)
        semiscaled_local_data = local_data.reshape(semiscaled_shape)
        result_obj = pindex.copy_empty(global_shape=target_shape,
                                       distribution_strategy=target_strategy)
        result_obj.set_full_data(semiscaled_local_data, copy=False)

        return result_obj

354
    def power_synthesize(self, spaces=None, real_power=True,
355
                         real_signal=False, mean=None, std=None):
356

357
358
359
        # check if the `spaces` input is valid
        spaces = utilities.cast_axis_to_tuple(spaces, len(self.domain))

Theo Steininger's avatar
Theo Steininger committed
360
361
362
        if spaces is None:
            spaces = range(len(self.domain))

363
364
365
366
367
        for power_space_index in spaces:
            power_space = self.domain[power_space_index]
            if not isinstance(power_space, PowerSpace):
                raise ValueError("A PowerSpace is needed for field "
                                 "synthetization.")
368
369
370

        # create the result domain
        result_domain = list(self.domain)
371
372
        for power_space_index in spaces:
            power_space = self.domain[power_space_index]
373
374
            harmonic_partner = power_space.harmonic_partner
            result_domain[power_space_index] = harmonic_partner
375
376
377

        # create random samples: one or two, depending on whether the
        # power spectrum is real or complex
378
        if real_power:
379
            result_list = [None]
380
381
        else:
            result_list = [None, None]
382

383
384
        result_list = [self.__class__.from_random(
                             'normal',
385
386
387
                             mean=mean,
                             std=std,
                             domain=result_domain,
388
                             dtype=np.complex,
389
                             distribution_strategy=self.distribution_strategy)
390
391
392
393
394
395
                       for x in result_list]

        # from now on extract the values from the random fields for further
        # processing without killing the fields.
        # if the signal-space field should be real, hermitianize the field
        # components
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

        spec = self.val.get_full_data()
        for power_space_index in spaces:
            spec = self._spec_to_rescaler(spec, result_list, power_space_index)
        local_rescaler = spec

        result_val_list = [x.val for x in result_list]

        # apply the rescaler to the random fields
        result_val_list[0].apply_scalar_function(
                                            lambda x: x * local_rescaler.real,
                                            inplace=True)

        if not real_power:
            result_val_list[1].apply_scalar_function(
                                            lambda x: x * local_rescaler.imag,
                                            inplace=True)

414
        if real_signal:
415
            for power_space_index in spaces:
416
417
                harmonic_partner = result_domain[power_space_index]
                result_val_list = [harmonic_partner.hermitian_decomposition(
418
419
420
421
422
                                    result_val,
                                    axes=result.domain_axes[power_space_index],
                                    preserve_gaussian_variance=True)[0]
                                   for (result, result_val)
                                   in zip(result_list, result_val_list)]
423
424
425
426
427
428
429

        # store the result into the fields
        [x.set_val(new_val=y, copy=False) for x, y in
            zip(result_list, result_val_list)]

        if real_power:
            result = result_list[0]
430
        else:
431
432
433
434
435
436
            result = result_list[0] + 1j*result_list[1]

        return result

    def _spec_to_rescaler(self, spec, result_list, power_space_index):
        power_space = self.domain[power_space_index]
437
438
439

        # weight the random fields with the power spectrum
        # therefore get the pindex from the power space
440
        pindex = power_space.pindex
441
442
443
444
445
446
447
        # take the local data from pindex. This data must be compatible to the
        # local data of the field given the slice of the PowerSpace
        local_distribution_strategy = \
            result_list[0].val.get_axes_local_distribution_strategy(
                result_list[0].domain_axes[power_space_index])

        if pindex.distribution_strategy is not local_distribution_strategy:
448
            self.logger.warn(
449
                "The distribution_stragey of pindex does not fit the "
450
451
452
453
454
455
456
457
458
459
                "slice_local distribution strategy of the synthesized field.")

        # Now use numpy advanced indexing in order to put the entries of the
        # power spectrum into the appropriate places of the pindex array.
        # Do this for every 'pindex-slice' in parallel using the 'slice(None)'s
        local_pindex = pindex.get_local_data(copy=False)

        local_blow_up = [slice(None)]*len(self.shape)
        local_blow_up[self.domain_axes[power_space_index][0]] = local_pindex
        # here, the power_spectrum is distributed into the new shape
460
461
        local_rescaler = spec[local_blow_up]
        return local_rescaler
462

theos's avatar
theos committed
463
    # ---Properties---
464

theos's avatar
theos committed
465
    def set_val(self, new_val=None, copy=False):
466
467
        new_val = self.cast(new_val)
        if copy:
theos's avatar
theos committed
468
469
            new_val = new_val.copy()
        self._val = new_val
theos's avatar
theos committed
470
        return self
csongor's avatar
csongor committed
471

472
    def get_val(self, copy=False):
473
474
475
        if self._val is None:
            self.set_val(None)

476
        if copy:
theos's avatar
theos committed
477
            return self._val.copy()
478
        else:
theos's avatar
theos committed
479
            return self._val
csongor's avatar
csongor committed
480

theos's avatar
theos committed
481
482
    @property
    def val(self):
483
        return self.get_val(copy=False)
csongor's avatar
csongor committed
484

theos's avatar
theos committed
485
486
    @val.setter
    def val(self, new_val):
487
        self.set_val(new_val=new_val, copy=False)
csongor's avatar
csongor committed
488

489
490
    @property
    def shape(self):
491
        shape_tuple = tuple(sp.shape for sp in self.domain)
492
493
494
495
        try:
            global_shape = reduce(lambda x, y: x + y, shape_tuple)
        except TypeError:
            global_shape = ()
csongor's avatar
csongor committed
496

497
        return global_shape
csongor's avatar
csongor committed
498

499
500
    @property
    def dim(self):
501
        dim_tuple = tuple(sp.dim for sp in self.domain)
theos's avatar
theos committed
502
503
504
505
        try:
            return reduce(lambda x, y: x * y, dim_tuple)
        except TypeError:
            return 0
csongor's avatar
csongor committed
506

507
508
    @property
    def dof(self):
theos's avatar
theos committed
509
510
511
512
513
514
515
516
        dof = self.dim
        if issubclass(self.dtype.type, np.complexfloating):
            dof *= 2
        return dof

    @property
    def total_volume(self):
        volume_tuple = tuple(sp.total_volume for sp in self.domain)
517
        try:
theos's avatar
theos committed
518
            return reduce(lambda x, y: x * y, volume_tuple)
519
        except TypeError:
Theo Steininger's avatar
Theo Steininger committed
520
            return 0.
521

theos's avatar
theos committed
522
    # ---Special unary/binary operations---
523

csongor's avatar
csongor committed
524
525
526
    def cast(self, x=None, dtype=None):
        if dtype is None:
            dtype = self.dtype
527
528
        else:
            dtype = np.dtype(dtype)
529

530
531
        casted_x = x

532
        for ind, sp in enumerate(self.domain):
533
            casted_x = sp.pre_cast(casted_x,
534
535
536
                                   axes=self.domain_axes[ind])

        casted_x = self._actual_cast(casted_x, dtype=dtype)
537
538

        for ind, sp in enumerate(self.domain):
539
540
            casted_x = sp.post_cast(casted_x,
                                    axes=self.domain_axes[ind])
541

542
        return casted_x
csongor's avatar
csongor committed
543

theos's avatar
theos committed
544
    def _actual_cast(self, x, dtype=None):
545
        if isinstance(x, Field):
csongor's avatar
csongor committed
546
547
548
549
550
            x = x.get_val()

        if dtype is None:
            dtype = self.dtype

551
        return_x = distributed_data_object(
552
553
554
                            global_shape=self.shape,
                            dtype=dtype,
                            distribution_strategy=self.distribution_strategy)
555
556
        return_x.set_full_data(x, copy=False)
        return return_x
theos's avatar
theos committed
557

558
    def copy(self, domain=None, dtype=None, distribution_strategy=None):
theos's avatar
theos committed
559
        copied_val = self.get_val(copy=True)
560
561
562
563
        new_field = self.copy_empty(
                                domain=domain,
                                dtype=dtype,
                                distribution_strategy=distribution_strategy)
theos's avatar
theos committed
564
565
        new_field.set_val(new_val=copied_val, copy=False)
        return new_field
csongor's avatar
csongor committed
566

567
    def copy_empty(self, domain=None, dtype=None, distribution_strategy=None):
theos's avatar
theos committed
568
569
        if domain is None:
            domain = self.domain
csongor's avatar
csongor committed
570
        else:
theos's avatar
theos committed
571
            domain = self._parse_domain(domain)
csongor's avatar
csongor committed
572

theos's avatar
theos committed
573
574
575
576
        if dtype is None:
            dtype = self.dtype
        else:
            dtype = np.dtype(dtype)
csongor's avatar
csongor committed
577

578
579
        if distribution_strategy is None:
            distribution_strategy = self.distribution_strategy
csongor's avatar
csongor committed
580

theos's avatar
theos committed
581
582
583
584
585
586
587
588
589
590
        fast_copyable = True
        try:
            for i in xrange(len(self.domain)):
                if self.domain[i] is not domain[i]:
                    fast_copyable = False
                    break
        except IndexError:
            fast_copyable = False

        if (fast_copyable and dtype == self.dtype and
591
                distribution_strategy == self.distribution_strategy):
theos's avatar
theos committed
592
593
594
595
            new_field = self._fast_copy_empty()
        else:
            new_field = Field(domain=domain,
                              dtype=dtype,
596
                              distribution_strategy=distribution_strategy)
theos's avatar
theos committed
597
        return new_field
csongor's avatar
csongor committed
598

theos's avatar
theos committed
599
600
601
602
603
604
605
    def _fast_copy_empty(self):
        # make an empty field
        new_field = EmptyField()
        # repair its class
        new_field.__class__ = self.__class__
        # copy domain, codomain and val
        for key, value in self.__dict__.items():
606
            if key != '_val':
theos's avatar
theos committed
607
608
609
610
611
612
                new_field.__dict__[key] = value
            else:
                new_field.__dict__[key] = self.val.copy_empty()
        return new_field

    def weight(self, power=1, inplace=False, spaces=None):
613
        if inplace:
csongor's avatar
csongor committed
614
615
616
617
            new_field = self
        else:
            new_field = self.copy_empty()

618
        new_val = self.get_val(copy=False)
csongor's avatar
csongor committed
619

620
        spaces = utilities.cast_axis_to_tuple(spaces, len(self.domain))
csongor's avatar
csongor committed
621
        if spaces is None:
theos's avatar
theos committed
622
            spaces = range(len(self.domain))
csongor's avatar
csongor committed
623

624
        for ind, sp in enumerate(self.domain):
theos's avatar
theos committed
625
626
627
628
629
            if ind in spaces:
                new_val = sp.weight(new_val,
                                    power=power,
                                    axes=self.domain_axes[ind],
                                    inplace=inplace)
630
631

        new_field.set_val(new_val=new_val, copy=False)
csongor's avatar
csongor committed
632
633
        return new_field

634
635
636
637
638
    def dot(self, x=None, spaces=None, bare=False):

        if not isinstance(x, Field):
            raise ValueError("The dot-partner must be an instance of " +
                             "the NIFTy field class")
theos's avatar
theos committed
639

Martin Reinecke's avatar
Martin Reinecke committed
640
        # Compute the dot respecting the fact of discrete/continuous spaces
theos's avatar
theos committed
641
642
643
644
645
        if bare:
            y = self
        else:
            y = self.weight(power=1)

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
        if spaces is None:
            x_val = x.get_val(copy=False)
            y_val = y.get_val(copy=False)
            result = (x_val.conjugate() * y_val).sum()
            return result
        else:
            # create a diagonal operator which is capable of taking care of the
            # axes-matching
            from nifty.operators.diagonal_operator import DiagonalOperator
            diagonal = y.val.conjugate()
            diagonalOperator = DiagonalOperator(domain=y.domain,
                                                diagonal=diagonal,
                                                copy=False)
            dotted = diagonalOperator(x, spaces=spaces)
            return dotted.sum(spaces=spaces)
theos's avatar
theos committed
661

662
    def norm(self, q=2):
csongor's avatar
csongor committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
        """
            Computes the Lq-norm of the field values.

            Parameters
            ----------
            q : scalar
                Parameter q of the Lq-norm (default: 2).

            Returns
            -------
            norm : scalar
                The Lq-norm of the field values.

        """
677
        if q == 2:
678
            return (self.dot(x=self)) ** (1 / 2)
csongor's avatar
csongor committed
679
        else:
680
            return self.dot(x=self ** (q - 1)) ** (1 / q)
csongor's avatar
csongor committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

    def conjugate(self, inplace=False):
        """
            Computes the complex conjugate of the field.

            Returns
            -------
            cc : field
                The complex conjugated field.

        """
        if inplace:
            work_field = self
        else:
            work_field = self.copy_empty()

697
        new_val = self.get_val(copy=False)
theos's avatar
theos committed
698
        new_val = new_val.conjugate()
699
        work_field.set_val(new_val=new_val, copy=False)
csongor's avatar
csongor committed
700
701
702

        return work_field

theos's avatar
theos committed
703
    # ---General unary/contraction methods---
704

theos's avatar
theos committed
705
706
    def __pos__(self):
        return self.copy()
707

theos's avatar
theos committed
708
709
710
711
    def __neg__(self):
        return_field = self.copy_empty()
        new_val = -self.get_val(copy=False)
        return_field.set_val(new_val, copy=False)
csongor's avatar
csongor committed
712
713
        return return_field

theos's avatar
theos committed
714
715
716
717
718
    def __abs__(self):
        return_field = self.copy_empty()
        new_val = abs(self.get_val(copy=False))
        return_field.set_val(new_val, copy=False)
        return return_field
csongor's avatar
csongor committed
719

720
    def _contraction_helper(self, op, spaces):
theos's avatar
theos committed
721
722
723
724
725
        # build a list of all axes
        if spaces is None:
            spaces = xrange(len(self.domain))
        else:
            spaces = utilities.cast_axis_to_tuple(spaces, len(self.domain))
csongor's avatar
csongor committed
726

727
        axes_list = tuple(self.domain_axes[sp_index] for sp_index in spaces)
728
729

        try:
theos's avatar
theos committed
730
            axes_list = reduce(lambda x, y: x+y, axes_list)
731
        except TypeError:
theos's avatar
theos committed
732
            axes_list = ()
csongor's avatar
csongor committed
733

theos's avatar
theos committed
734
735
736
        # perform the contraction on the d2o
        data = self.get_val(copy=False)
        data = getattr(data, op)(axis=axes_list)
csongor's avatar
csongor committed
737

theos's avatar
theos committed
738
739
740
        # check if the result is scalar or if a result_field must be constr.
        if np.isscalar(data):
            return data
csongor's avatar
csongor committed
741
        else:
theos's avatar
theos committed
742
743
744
            return_domain = tuple(self.domain[i]
                                  for i in xrange(len(self.domain))
                                  if i not in spaces)
745

theos's avatar
theos committed
746
747
748
749
            return_field = Field(domain=return_domain,
                                 val=data,
                                 copy=False)
            return return_field
csongor's avatar
csongor committed
750

751
752
    def sum(self, spaces=None):
        return self._contraction_helper('sum', spaces)
csongor's avatar
csongor committed
753

754
755
    def prod(self, spaces=None):
        return self._contraction_helper('prod', spaces)
csongor's avatar
csongor committed
756

757
758
    def all(self, spaces=None):
        return self._contraction_helper('all', spaces)
csongor's avatar
csongor committed
759

760
761
    def any(self, spaces=None):
        return self._contraction_helper('any', spaces)
csongor's avatar
csongor committed
762

763
764
    def min(self, spaces=None):
        return self._contraction_helper('min', spaces)
csongor's avatar
csongor committed
765

766
767
    def nanmin(self, spaces=None):
        return self._contraction_helper('nanmin', spaces)
csongor's avatar
csongor committed
768

769
770
    def max(self, spaces=None):
        return self._contraction_helper('max', spaces)
csongor's avatar
csongor committed
771

772
773
    def nanmax(self, spaces=None):
        return self._contraction_helper('nanmax', spaces)
csongor's avatar
csongor committed
774

775
776
    def mean(self, spaces=None):
        return self._contraction_helper('mean', spaces)
csongor's avatar
csongor committed
777

778
779
    def var(self, spaces=None):
        return self._contraction_helper('var', spaces)
csongor's avatar
csongor committed
780

781
782
    def std(self, spaces=None):
        return self._contraction_helper('std', spaces)
csongor's avatar
csongor committed
783

theos's avatar
theos committed
784
    # ---General binary methods---
csongor's avatar
csongor committed
785

theos's avatar
theos committed
786
    def _binary_helper(self, other, op, inplace=False):
csongor's avatar
csongor committed
787
        # if other is a field, make sure that the domains match
788
        if isinstance(other, Field):
theos's avatar
theos committed
789
790
791
792
793
            try:
                assert len(other.domain) == len(self.domain)
                for index in xrange(len(self.domain)):
                    assert other.domain[index] == self.domain[index]
            except AssertionError:
794
795
                raise ValueError(
                    "domains are incompatible.")
theos's avatar
theos committed
796
            other = other.get_val(copy=False)
csongor's avatar
csongor committed
797

theos's avatar
theos committed
798
799
        self_val = self.get_val(copy=False)
        return_val = getattr(self_val, op)(other)
csongor's avatar
csongor committed
800
801
802
803

        if inplace:
            working_field = self
        else:
804
            working_field = self.copy_empty(dtype=return_val.dtype)
csongor's avatar
csongor committed
805

theos's avatar
theos committed
806
        working_field.set_val(return_val, copy=False)
csongor's avatar
csongor committed
807
808
809
        return working_field

    def __add__(self, other):
theos's avatar
theos committed
810
        return self._binary_helper(other, op='__add__')
811

812
    def __radd__(self, other):
theos's avatar
theos committed
813
        return self._binary_helper(other, op='__radd__')
csongor's avatar
csongor committed
814
815

    def __iadd__(self, other):
theos's avatar
theos committed
816
        return self._binary_helper(other, op='__iadd__', inplace=True)
csongor's avatar
csongor committed
817
818

    def __sub__(self, other):
theos's avatar
theos committed
819
        return self._binary_helper(other, op='__sub__')
csongor's avatar
csongor committed
820
821

    def __rsub__(self, other):
theos's avatar
theos committed
822
        return self._binary_helper(other, op='__rsub__')
csongor's avatar
csongor committed
823
824

    def __isub__(self, other):
theos's avatar
theos committed
825
        return self._binary_helper(other, op='__isub__', inplace=True)
csongor's avatar
csongor committed
826
827

    def __mul__(self, other):
theos's avatar
theos committed
828
        return self._binary_helper(other, op='__mul__')
829

830
    def __rmul__(self, other):
theos's avatar
theos committed
831
        return self._binary_helper(other, op='__rmul__')
csongor's avatar
csongor committed
832
833

    def __imul__(self, other):
theos's avatar
theos committed
834
        return self._binary_helper(other, op='__imul__', inplace=True)
csongor's avatar
csongor committed
835
836

    def __div__(self, other):
theos's avatar
theos committed
837
        return self._binary_helper(other, op='__div__')
csongor's avatar
csongor committed
838
839

    def __rdiv__(self, other):
theos's avatar
theos committed
840
        return self._binary_helper(other, op='__rdiv__')
csongor's avatar
csongor committed
841
842

    def __idiv__(self, other):
theos's avatar
theos committed
843
        return self._binary_helper(other, op='__idiv__', inplace=True)
844

csongor's avatar
csongor committed
845
    def __pow__(self, other):
theos's avatar
theos committed
846
        return self._binary_helper(other, op='__pow__')
csongor's avatar
csongor committed
847
848

    def __rpow__(self, other):
theos's avatar
theos committed
849
        return self._binary_helper(other, op='__rpow__')
csongor's avatar
csongor committed
850
851

    def __ipow__(self, other):
theos's avatar
theos committed
852
        return self._binary_helper(other, op='__ipow__', inplace=True)
csongor's avatar
csongor committed
853
854

    def __lt__(self, other):
theos's avatar
theos committed
855
        return self._binary_helper(other, op='__lt__')
csongor's avatar
csongor committed
856
857

    def __le__(self, other):
theos's avatar
theos committed
858
        return self._binary_helper(other, op='__le__')
csongor's avatar
csongor committed
859
860
861
862
863

    def __ne__(self, other):
        if other is None:
            return True
        else:
theos's avatar
theos committed
864
            return self._binary_helper(other, op='__ne__')
csongor's avatar
csongor committed
865
866
867
868
869

    def __eq__(self, other):
        if other is None:
            return False
        else:
theos's avatar
theos committed
870
            return self._binary_helper(other, op='__eq__')
csongor's avatar
csongor committed
871
872

    def __ge__(self, other):
theos's avatar
theos committed
873
        return self._binary_helper(other, op='__ge__')
csongor's avatar
csongor committed
874
875

    def __gt__(self, other):
theos's avatar
theos committed
876
877
878
879
880
881
882
883
884
885
886
887
888
        return self._binary_helper(other, op='__gt__')

    def __repr__(self):
        return "<nifty_core.field>"

    def __str__(self):
        minmax = [self.min(), self.max()]
        mean = self.mean()
        return "nifty_core.field instance\n- domain      = " + \
               repr(self.domain) + \
               "\n- val         = " + repr(self.get_val()) + \
               "\n  - min.,max. = " + str(minmax) + \
               "\n  - mean = " + str(mean)
csongor's avatar
csongor committed
889

Jait Dixit's avatar
Jait Dixit committed
890
891
892
    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Theo Steininger's avatar
Theo Steininger committed
893
894
895
        hdf5_group.attrs['dtype'] = self.dtype.name
        hdf5_group.attrs['distribution_strategy'] = self.distribution_strategy
        hdf5_group.attrs['domain_axes'] = str(self.domain_axes)
896
        hdf5_group['num_domain'] = len(self.domain)
Jait Dixit's avatar
Jait Dixit committed
897

Theo Steininger's avatar
Theo Steininger committed
898
899
900
901
        if self._val is None:
            ret_dict = {}
        else:
            ret_dict = {'val': self.val}
Jait Dixit's avatar
Jait Dixit committed
902
903
904
905
906
907
908

        for i in range(len(self.domain)):
            ret_dict['s_' + str(i)] = self.domain[i]

        return ret_dict

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
909
    def _from_hdf5(cls, hdf5_group, repository):
Jait Dixit's avatar
Jait Dixit committed
910
911
912
913
914
915
        # create empty field
        new_field = EmptyField()
        # reset class
        new_field.__class__ = cls
        # set values
        temp_domain = []
916
        for i in range(hdf5_group['num_domain'][()]):
Theo Steininger's avatar
Theo Steininger committed
917
            temp_domain.append(repository.get('s_' + str(i), hdf5_group))
Jait Dixit's avatar
Jait Dixit committed
918
919
        new_field.domain = tuple(temp_domain)

Theo Steininger's avatar
Theo Steininger committed
920
        exec('new_field.domain_axes = ' + hdf5_group.attrs['domain_axes'])
Theo Steininger's avatar
Theo Steininger committed
921
922
923
924
925
926

        try:
            new_field._val = repository.get('val', hdf5_group)
        except(KeyError):
            new_field._val = None

Theo Steininger's avatar
Theo Steininger committed
927
928
929
        new_field.dtype = np.dtype(hdf5_group.attrs['dtype'])
        new_field.distribution_strategy =\
            hdf5_group.attrs['distribution_strategy']
Jait Dixit's avatar
Jait Dixit committed
930
931

        return new_field
932

Theo Steininger's avatar
Theo Steininger committed
933

934
class EmptyField(Field):
csongor's avatar
csongor committed
935
936
    def __init__(self):
        pass