power_space.py 4.18 KB
Newer Older
theos's avatar
theos committed
1
2
# -*- coding: utf-8 -*-

theos's avatar
theos committed
3
4
import numpy as np

5
6
from power_index_factory import PowerIndexFactory

7
from nifty.config import about
8
from nifty.spaces.space import Space
9
from nifty.spaces.rg_space import RGSpace
10
from nifty.nifty_utilities import cast_axis_to_tuple
theos's avatar
theos committed
11
12
13


class PowerSpace(Space):
14
15
16

    # ---Overwritten properties and methods---

17
18
    def __init__(self, harmonic_domain=RGSpace((1,)),
                 distribution_strategy='not',
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
                 log=False, nbin=None, binbounds=None,
                 dtype=np.dtype('float')):

        super(PowerSpace, self).__init__(dtype)
        self._ignore_for_hash += ['_pindex', '_kindex', '_rho']

        if not isinstance(harmonic_domain, Space):
            raise ValueError(about._errors.cstring(
                "ERROR: harmonic_domain must be a Space."))
        if not harmonic_domain.harmonic:
            raise ValueError(about._errors.cstring(
                "ERROR: harmonic_domain must be a harmonic space."))
        self._harmonic_domain = harmonic_domain

        power_index = PowerIndexFactory.get_power_index(
                        domain=self.harmonic_domain,
35
                        distribution_strategy=distribution_strategy,
36
37
38
39
40
41
42
43
44
45
46
47
                        log=log,
                        nbin=nbin,
                        binbounds=binbounds)

        config = power_index['config']
        self._log = config['log']
        self._nbin = config['nbin']
        self._binbounds = config['binbounds']

        self._pindex = power_index['pindex']
        self._kindex = power_index['kindex']
        self._rho = power_index['rho']
48
49
        self._pundex = power_index['pundex']
        self._k_array = power_index['k_array']
50

51
52
53
54
55
56
    def pre_cast(self, x, axes=None):
        if callable(x):
            return x(self.kindex)
        else:
            return x

57
58
59
60
61
    # ---Mandatory properties and methods---

    @property
    def harmonic(self):
        return True
62

63
64
    @property
    def shape(self):
65
        return self.kindex.shape
66

67
68
69
70
71
72
73
    @property
    def dim(self):
        return self.shape[0]

    @property
    def total_volume(self):
        # every power-pixel has a volume of 1
74
75
76
        return reduce(lambda x, y: x*y, self.pindex.shape)

    def copy(self):
77
        distribution_strategy = self.pindex.distribution_strategy
78
        return self.__class__(harmonic_domain=self.harmonic_domain,
79
                              distribution_strategy=distribution_strategy,
80
81
82
83
                              log=self.log,
                              nbin=self.nbin,
                              binbounds=self.binbounds,
                              dtype=self.dtype)
84

85
    def weight(self, x, power=1, axes=None, inplace=False):
86
87
88
89
90
91
92
93
94
95
        total_shape = x.shape

        axes = cast_axis_to_tuple(axes, len(total_shape))
        if len(axes) != 1:
            raise ValueError(about._errors.cstring(
                "ERROR: axes must be of length 1."))

        reshaper = [1, ] * len(total_shape)
        reshaper[axes[0]] = self.shape[0]

96
        weight = self.rho.reshape(reshaper)
97
98
        if power != 1:
            weight = weight ** power
99
100
101
102
103
104

        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x*weight
105
106
107

        return result_x

108
    def get_distance_array(self, distribution_strategy):
theos's avatar
theos committed
109
        raise NotImplementedError(about._errors.cstring(
110
111
            "ERROR: There is no get_distance_array implementation for "
            "PowerSpace."))
theos's avatar
theos committed
112
113
114
115
116

    def get_smoothing_kernel_function(self, sigma):
        raise NotImplementedError(about._errors.cstring(
            "ERROR: There is no smoothing function for PowerSpace."))

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    # ---Added properties and methods---

    @property
    def harmonic_domain(self):
        return self._harmonic_domain

    @property
    def log(self):
        return self._log

    @property
    def nbin(self):
        return self._nbin

    @property
    def binbounds(self):
        return self._binbounds

    @property
    def pindex(self):
        return self._pindex

    @property
    def kindex(self):
        return self._kindex

    @property
    def rho(self):
        return self._rho
146

147
148
149
150
151
152
153
    @property
    def pundex(self):
        return self._pundex

    @property
    def k_array(self):
        return self._k_array