utilities.py 8.5 KB
Newer Older
1
2
3
4
5
6
7
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.
#
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
Theo Steininger's avatar
Theo Steininger committed
18

Martin Reinecke's avatar
Martin Reinecke committed
19
from builtins import next, range
Theo Steininger's avatar
Theo Steininger committed
20
import numpy as np
21
from itertools import product
Martin Reinecke's avatar
Martin Reinecke committed
22
import abc
23

Martin Reinecke's avatar
Martin Reinecke committed
24

25
26
def get_slice_list(shape, axes):
    """
Theo Steininger's avatar
Theo Steininger committed
27
28
    Helper function which generates slice list(s) to traverse over all
    combinations of axes, other than the selected axes.
Jait Dixit's avatar
Jait Dixit committed
29
30
31
32

    Parameters
    ----------
    shape: tuple
Theo Steininger's avatar
Theo Steininger committed
33
        Shape of the data array to traverse over.
Jait Dixit's avatar
Jait Dixit committed
34
    axes: tuple
Theo Steininger's avatar
Theo Steininger committed
35
        Axes which should not be iterated over.
Jait Dixit's avatar
Jait Dixit committed
36
37
38
39
40
41
42
43
44
45
46

    Yields
    -------
    list
        The next list of indices and/or slice objects for each dimension.

    Raises
    ------
    ValueError
        If shape is empty.
        If axes(axis) does not match shape.
47
    """
Martin Reinecke's avatar
Martin Reinecke committed
48
    if shape is None:
49
        raise ValueError("shape cannot be None.")
50

51
52
    if axes:
        if not all(axis < len(shape) for axis in axes):
53
            raise ValueError("axes(axis) does not match shape.")
54
        axes_select = [0 if x in axes else 1 for x, y in enumerate(shape)]
Jait Dixit's avatar
Jait Dixit committed
55
        axes_iterables = \
Martin Reinecke's avatar
Martin Reinecke committed
56
            [list(range(y)) for x, y in enumerate(shape) if x not in axes]
57
58
59
60
61
        for index in product(*axes_iterables):
            it_iter = iter(index)
            slice_list = [
                next(it_iter)
                if axis else slice(None, None) for axis in axes_select
Jait Dixit's avatar
Jait Dixit committed
62
                ]
63
64
65
            yield slice_list
    else:
        yield [slice(None, None)]
Theo Steininger's avatar
Theo Steininger committed
66

Theo Steininger's avatar
Theo Steininger committed
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def safe_cast(tfunc, val):
    tmp = tfunc(val)
    if val != tmp:
        raise ValueError("value changed during cast")
    return tmp


def parse_spaces(spaces, maxidx):
    maxidx = safe_cast(int, maxidx)
    if spaces is None:
        return tuple(range(maxidx))
    elif np.isscalar(spaces):
        spaces = (safe_cast(int, spaces),)
    else:
        spaces = tuple(safe_cast(int, item) for item in spaces)
    tmp = tuple(set(spaces))
    if tmp[0] < 0 or tmp[-1] >= maxidx:
        raise ValueError("space index out of range")
    if len(tmp) != len(spaces):
        raise ValueError("multiply defined space indices")
    return spaces
Martin Reinecke's avatar
Martin Reinecke committed
89
90


91
92
93
94
95
96
97
98
99
100
101
def infer_space(domain, space):
    if space is None:
        if len(domain) != 1:
            raise ValueError("need a Field with exactly one domain")
        space = 0
    space = int(space)
    if space < 0 or space >= len(domain):
        raise ValueError("space index out of range")
    return space


Martin Reinecke's avatar
Martin Reinecke committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
146
147
148
149
150
def memo(f):
    name = f.__name__

    def wrapped_f(self):
        if not hasattr(self, "_cache"):
            self._cache = {}
        try:
            return self._cache[name]
        except KeyError:
            self._cache[name] = f(self)
            return self._cache[name]
    return wrapped_f


class _DocStringInheritor(type):
    """
    A variation on
    http://groups.google.com/group/comp.lang.python/msg/26f7b4fcb4d66c95
    by Paul McGuire
    """
    def __new__(meta, name, bases, clsdict):
        if not('__doc__' in clsdict and clsdict['__doc__']):
            for mro_cls in (mro_cls for base in bases
                            for mro_cls in base.mro()):
                doc = mro_cls.__doc__
                if doc:
                    clsdict['__doc__'] = doc
                    break
        for attr, attribute in list(clsdict.items()):
            if not attribute.__doc__:
                for mro_cls in (mro_cls for base in bases
                                for mro_cls in base.mro()
                                if hasattr(mro_cls, attr)):
                    doc = getattr(getattr(mro_cls, attr), '__doc__')
                    if doc:
                        if isinstance(attribute, property):
                            clsdict[attr] = property(attribute.fget,
                                                     attribute.fset,
                                                     attribute.fdel,
                                                     doc)
                        else:
                            attribute.__doc__ = doc
                        break
        return super(_DocStringInheritor, meta).__new__(meta, name,
                                                        bases, clsdict)


class NiftyMeta(_DocStringInheritor, abc.ABCMeta):
    pass
Martin Reinecke's avatar
Martin Reinecke committed
151
152
153
154
155
156
157


def hartley(a, axes=None):
    # Check if the axes provided are valid given the shape
    if axes is not None and \
            not all(axis < len(a.shape) for axis in axes):
        raise ValueError("Provided axes do not match array shape")
158
    if np.issubdtype(a.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
159
        raise TypeError("Hartley transform requires real-valued arrays.")
Martin Reinecke's avatar
Martin Reinecke committed
160
161
162
163

    from pyfftw.interfaces.numpy_fft import rfftn
    tmp = rfftn(a, axes=axes)

Martin Reinecke's avatar
Martin Reinecke committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    def _fill_array(tmp, res, axes):
        if axes is None:
            axes = tuple(range(tmp.ndim))
        lastaxis = axes[-1]
        ntmplast = tmp.shape[lastaxis]
        slice1 = [slice(None)]*lastaxis + [slice(0, ntmplast)]
        np.add(tmp.real, tmp.imag, out=res[slice1])

        def _fill_upper_half(tmp, res, axes):
            lastaxis = axes[-1]
            nlast = res.shape[lastaxis]
            ntmplast = tmp.shape[lastaxis]
            nrem = nlast - ntmplast
            slice1 = [slice(None)]*lastaxis + [slice(ntmplast, None)]
            slice2 = [slice(None)]*lastaxis + [slice(nrem, 0, -1)]
            for i in axes[:-1]:
                slice1[i] = slice(1, None)
                slice2[i] = slice(None, 0, -1)
            np.subtract(tmp[slice2].real, tmp[slice2].imag, out=res[slice1])
            for i, ax in enumerate(axes[:-1]):
                dim1 = [slice(None)]*ax + [slice(0, 1)]
                axes2 = axes[:i] + axes[i+1:]
                _fill_upper_half(tmp[dim1], res[dim1], axes2)

        _fill_upper_half(tmp, res, axes)
        return res
Martin Reinecke's avatar
Martin Reinecke committed
190

Martin Reinecke's avatar
Martin Reinecke committed
191
    return _fill_array(tmp, np.empty_like(a), axes)
Martin Reinecke's avatar
Martin Reinecke committed
192
193
194
195
196
197
198
199


# Do a real-to-complex forward FFT and return the _full_ output array
def my_fftn_r2c(a, axes=None):
    # Check if the axes provided are valid given the shape
    if axes is not None and \
            not all(axis < len(a.shape) for axis in axes):
        raise ValueError("Provided axes do not match array shape")
200
    if np.issubdtype(a.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
201
202
203
204
        raise TypeError("Transform requires real-valued input arrays.")

    from pyfftw.interfaces.numpy_fft import rfftn
    tmp = rfftn(a, axes=axes)
Martin Reinecke's avatar
Martin Reinecke committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

    def _fill_complex_array(tmp, res, axes):
        if axes is None:
            axes = tuple(range(tmp.ndim))
        lastaxis = axes[-1]
        ntmplast = tmp.shape[lastaxis]
        slice1 = [slice(None)]*lastaxis + [slice(0, ntmplast)]
        res[slice1] = tmp

        def _fill_upper_half_complex(tmp, res, axes):
            lastaxis = axes[-1]
            nlast = res.shape[lastaxis]
            ntmplast = tmp.shape[lastaxis]
            nrem = nlast - ntmplast
            slice1 = [slice(None)]*lastaxis + [slice(ntmplast, None)]
            slice2 = [slice(None)]*lastaxis + [slice(nrem, 0, -1)]
            for i in axes[:-1]:
                slice1[i] = slice(1, None)
                slice2[i] = slice(None, 0, -1)
            # np.conjugate(tmp[slice2], out=res[slice1])
            res[slice1] = np.conjugate(tmp[slice2])
            for i, ax in enumerate(axes[:-1]):
                dim1 = [slice(None)]*ax + [slice(0, 1)]
                axes2 = axes[:i] + axes[i+1:]
                _fill_upper_half_complex(tmp[dim1], res[dim1], axes2)

        _fill_upper_half_complex(tmp, res, axes)
        return res

    return _fill_complex_array(tmp, np.empty_like(a, dtype=tmp.dtype), axes)
Martin Reinecke's avatar
Martin Reinecke committed
235
236


Martin Reinecke's avatar
Martin Reinecke committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def general_axpy(a, x, y, out):
    if x.domain != y.domain or x.domain != out.domain:
        raise ValueError("Incompatible domains")

    if out is x:
        if a != 1.:
            out *= a
        out += y
    elif out is y:
        if a != 1.:
            out += a*x
        else:
            out += x
    else:
        out.copy_content_from(y)
        if a != 1.:
            out += a*x
        else:
            out += x
    return out