nifty_paradict.py 6.74 KB
Newer Older
ultimanet's avatar
ultimanet committed
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
"""
Created on Thu Apr  2 21:29:30 2015

@author: steininger
"""

import numpy as np
9
from nifty.config import about
ultimanet's avatar
ultimanet committed
10
11


12
class space_paradict(object):
Ultima's avatar
Ultima committed
13

ultimanet's avatar
ultimanet committed
14
15
16
17
    def __init__(self, **kwargs):
        self.parameters = {}
        for key in kwargs:
            self[key] = kwargs[key]
18

19
    def __eq__(self, other):
Ultima's avatar
Ultima committed
20
21
        return (isinstance(other, self.__class__) and
                self.__dict__ == other.__dict__)
22
23

    def __ne__(self, other):
24
        return not self.__eq__(other)
25

26
27
    def __repr__(self):
        return self.parameters.__repr__()
28

ultimanet's avatar
ultimanet committed
29
    def __setitem__(self, key, arg):
30
        if(np.isscalar(arg)):
Ultima's avatar
Ultima committed
31
            arg = np.array([arg], dtype=np.int)
32
        else:
Ultima's avatar
Ultima committed
33
            arg = np.array(arg, dtype=np.int)
34

ultimanet's avatar
ultimanet committed
35
        self.parameters.__setitem__(key, arg)
36

ultimanet's avatar
ultimanet committed
37
    def __getitem__(self, key):
38
39
        return self.parameters.__getitem__(key)

Ultima's avatar
Ultima committed
40
41
42
43
44
45
46
47
48
49
    def __hash__(self):
        result_hash = 0
        for (key, item) in self.parameters.items():
            try:
                temp_hash = hash(item)
            except TypeError:
                temp_hash = hash(tuple(item))
            result_hash ^= temp_hash * hash(key)
        return result_hash

ultimanet's avatar
ultimanet committed
50

51
class rg_space_paradict(space_paradict):
Ultima's avatar
Ultima committed
52

53
54
    def __init__(self, shape, complexity, zerocenter):
        self.ndim = len(np.array(shape).flatten())
Ultima's avatar
Ultima committed
55
        space_paradict.__init__(
56
            self, shape=shape, complexity=complexity, zerocenter=zerocenter)
57

ultimanet's avatar
ultimanet committed
58
    def __setitem__(self, key, arg):
59
        if key not in ['shape', 'complexity', 'zerocenter']:
Ultima's avatar
Ultima committed
60
61
            raise ValueError(about._errors.cstring(
                "ERROR: Unsupported rg_space parameter"))
62

63
64
65
66
67
        if key == 'shape':
            temp = np.array(arg, dtype=np.int).flatten()
            if np.any(temp < 0):
                raise ValueError("ERROR: negative number in shape.")
            temp = list(temp)
ultimanet's avatar
ultimanet committed
68
            if len(temp) != self.ndim:
Ultima's avatar
Ultima committed
69
70
71
                raise ValueError(about._errors.cstring(
                    "ERROR: Number of dimensions does not match the init " +
                    "value."))
72
        elif key == 'complexity':
ultimanet's avatar
ultimanet committed
73
            temp = int(arg)
74
75
76
            if temp not in [0, 1, 2]:
                raise ValueError(about._errors.cstring(
                    "ERROR: Unsupported complexity parameter: " + str(temp)))
ultimanet's avatar
ultimanet committed
77
78
79
80
81
        elif key == 'zerocenter':
            temp = np.empty(self.ndim, dtype=bool)
            temp[:] = arg
            temp = list(temp)
        self.parameters.__setitem__(key, temp)
82

Ultima's avatar
Ultima committed
83

84
class nested_space_paradict(space_paradict):
Ultima's avatar
Ultima committed
85

ultimanet's avatar
ultimanet committed
86
87
    def __init__(self, ndim):
        self.ndim = np.int(ndim)
88
        space_paradict.__init__(self)
Ultima's avatar
Ultima committed
89

ultimanet's avatar
ultimanet committed
90
91
    def __setitem__(self, key, arg):
        if not isinstance(key, int):
Ultima's avatar
Ultima committed
92
93
            raise ValueError(about._errors.cstring(
                "ERROR: Unsupported point_space parameter"))
ultimanet's avatar
ultimanet committed
94
        if key >= self.ndim or key < 0:
Ultima's avatar
Ultima committed
95
96
            raise ValueError(about._errors.cstring(
                "ERROR: Nestindex out of bounds"))
97
        temp = list(np.array(arg, dtype=int).flatten())
ultimanet's avatar
ultimanet committed
98
        self.parameters.__setitem__(key, temp)
99
100


101
class lm_space_paradict(space_paradict):
Ultima's avatar
Ultima committed
102

103
    def __init__(self, lmax, mmax):
104
        space_paradict.__init__(self, lmax=lmax)
Ultima's avatar
Ultima committed
105
106
        if mmax is None:
            mmax = -1
107
108
        self['mmax'] = mmax

ultimanet's avatar
ultimanet committed
109
110
    def __setitem__(self, key, arg):
        if key not in ['lmax', 'mmax']:
Ultima's avatar
Ultima committed
111
112
            raise ValueError(about._errors.cstring(
                "ERROR: Unsupported rg_space parameter"))
ultimanet's avatar
ultimanet committed
113
114

        if key == 'lmax':
115
116
            temp = np.int(arg)
            if temp < 1:
Ultima's avatar
Ultima committed
117
118
119
120
121
122
                raise ValueError(about._errors.cstring(
                    "ERROR: lmax: nonpositive number."))
            # exception lmax == 2 (nside == 1)
            if (temp % 2 == 0) and (temp > 2):
                about.warnings.cprint(
                    "WARNING: unrecommended parameter (lmax <> 2*n+1).")
ultimanet's avatar
ultimanet committed
123
124
            try:
                if temp < self['mmax']:
Ultima's avatar
Ultima committed
125
126
                    about.warnings.cprint(
                        "WARNING: mmax parameter set to lmax.")
ultimanet's avatar
ultimanet committed
127
128
                    self['mmax'] = temp
                if (temp != self['mmax']):
Ultima's avatar
Ultima committed
129
130
                    about.warnings.cprint(
                        "WARNING: unrecommended parameter set (mmax <> lmax).")
ultimanet's avatar
ultimanet committed
131
132
133
            except:
                pass
        elif key == 'mmax':
134
            temp = int(arg)
ultimanet's avatar
ultimanet committed
135
            if (temp < 1) or(temp > self['lmax']):
Ultima's avatar
Ultima committed
136
137
                about.warnings.cprint(
                    "WARNING: mmax parameter set to default.")
138
                temp = self['lmax']
ultimanet's avatar
ultimanet committed
139
            if(temp != self['lmax']):
Ultima's avatar
Ultima committed
140
141
                about.warnings.cprint(
                    "WARNING: unrecommended parameter set (mmax <> lmax).")
142

ultimanet's avatar
ultimanet committed
143
144
        self.parameters.__setitem__(key, temp)

Ultima's avatar
Ultima committed
145

146
class gl_space_paradict(space_paradict):
Ultima's avatar
Ultima committed
147

148
    def __init__(self, nlat, nlon):
149
        space_paradict.__init__(self, nlat=nlat)
Ultima's avatar
Ultima committed
150
151
        if nlon is None:
            nlon = -1
ultimanet's avatar
ultimanet committed
152
        self['nlon'] = nlon
153

ultimanet's avatar
ultimanet committed
154
155
    def __setitem__(self, key, arg):
        if key not in ['nlat', 'nlon']:
Ultima's avatar
Ultima committed
156
157
            raise ValueError(about._errors.cstring(
                "ERROR: Unsupported rg_space parameter"))
ultimanet's avatar
ultimanet committed
158
159
160

        if key == 'nlat':
            temp = int(arg)
Ultima's avatar
Ultima committed
161
162
163
164
165
166
            if(temp < 1):
                raise ValueError(about._errors.cstring(
                    "ERROR: nlat: nonpositive number."))
            if (temp % 2 != 0):
                raise ValueError(about._errors.cstring(
                    "ERROR: invalid parameter (nlat <> 2n)."))
ultimanet's avatar
ultimanet committed
167
168
            try:
                if temp < self['mmax']:
Ultima's avatar
Ultima committed
169
170
                    about.warnings.cprint(
                        "WARNING: mmax parameter set to lmax.")
ultimanet's avatar
ultimanet committed
171
172
                    self['mmax'] = temp
                if (temp != self['mmax']):
Ultima's avatar
Ultima committed
173
174
                    about.warnings.cprint(
                        "WARNING: unrecommended parameter set (mmax <> lmax).")
ultimanet's avatar
ultimanet committed
175
176
177
            except:
                pass
        elif key == 'nlon':
178
            temp = int(arg)
ultimanet's avatar
ultimanet committed
179
            if (temp < 1):
Ultima's avatar
Ultima committed
180
181
182
183
184
185
                about.warnings.cprint(
                    "WARNING: nlon parameter set to default.")
                temp = 2 * self['nlat'] - 1
            if(temp != 2 * self['nlat'] - 1):
                about.warnings.cprint(
                    "WARNING: unrecommended parameter set (nlon <> 2*nlat-1).")
ultimanet's avatar
ultimanet committed
186
187
188
        self.parameters.__setitem__(key, temp)


189
class hp_space_paradict(space_paradict):
Ultima's avatar
Ultima committed
190

ultimanet's avatar
ultimanet committed
191
    def __init__(self, nside):
192
        space_paradict.__init__(self, nside=nside)
Ultima's avatar
Ultima committed
193

ultimanet's avatar
ultimanet committed
194
195
    def __setitem__(self, key, arg):
        if key not in ['nside']:
Ultima's avatar
Ultima committed
196
197
            raise ValueError(about._errors.cstring(
                "ERROR: Unsupported hp_space parameter"))
198

ultimanet's avatar
ultimanet committed
199
        temp = int(arg)
Ultima's avatar
Ultima committed
200
201
202
203
        # if(not hp.isnsideok(nside)):
        if ((temp & (temp - 1)) != 0) or (temp < 2):
            raise ValueError(about._errors.cstring(
                "ERROR: invalid parameter ( nside <> 2**n )."))
204
        self.parameters.__setitem__(key, temp)