slicing_transformation.py 2.05 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
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import abc
import numpy as np

import nifty.nifty_utilities as utilities
from transformation import Transformation


class SlicingTransformation(Transformation):

    def transform(self, val, axes=None, **kwargs):

        return_shape = np.array(val.shape)
        return_shape[list(axes)] = self.codomain.shape
        return_shape = tuple(return_shape)

        return_val = None

        for slice_list in utilities.get_slice_list(val.shape, axes):
            if return_val is None:
38
                return_val = val.copy_empty(global_shape=return_shape)
39

Theo Steininger's avatar
Theo Steininger committed
40
            data = val.get_data(slice_list, copy=False)
41
42
            data = data.get_full_data()

43
            data = self._transformation_of_slice(data, **kwargs)
44

Theo Steininger's avatar
Theo Steininger committed
45
            return_val.set_data(data=data, to_key=slice_list, copy=False)
46
47
48

        return return_val

49
50
51
52
53
54
55
56
57
58
59
    def _combine_complex_result(self, resultReal, resultImag):
        # construct correct complex dtype
        one = resultReal.dtype.type(1)
        result_dtype = np.dtype(type(one + 1j))

        result = np.empty_like(resultReal, dtype=result_dtype)
        result.real = resultReal
        result.imag = resultImag

        return result

60
61
62
    @abc.abstractmethod
    def _transformation_of_slice(self, inp):
        raise NotImplementedError