log_rg_space.py 4.85 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
17

Martin Reinecke's avatar
Martin Reinecke committed
18
from functools import reduce
Philipp Arras's avatar
Philipp Arras committed
19

20
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
21

22
23
24
25
26
from ..field import Field
from .structured_domain import StructuredDomain


class LogRGSpace(StructuredDomain):
Philipp Arras's avatar
Docs    
Philipp Arras committed
27
    '''Represents a logarithmic Cartesian grid.
28
29
30
31
32
33

    Parameters
    ----------
    shape : int or tuple of int
        Number of grid points or numbers of gridpoints along each axis.
    bindistances : float or tuple of float
Philipp Frank's avatar
docs    
Philipp Frank committed
34
35
        Logarithmic distance between two grid points along each axis.
        Equidistant spacing of bins on logarithmic scale is assumed.
36
    t_0 : float or tuple of float
Philipp Arras's avatar
Docs    
Philipp Arras committed
37
        Coordinate of pixel ndim*(1,).
38
39
    harmonic : bool, optional
        Whether the space represents a grid in position or harmonic space.
Philipp Arras's avatar
Philipp Arras committed
40
        Default: False.
Philipp Arras's avatar
Docs    
Philipp Arras committed
41
    '''
42
43
44
45
46
47
48
49
50
51
52
    _needed_for_hash = ['_shape', '_bindistances', '_t_0', '_harmonic']

    def __init__(self, shape, bindistances, t_0, harmonic=False):
        self._harmonic = bool(harmonic)

        if np.isscalar(shape):
            shape = (shape,)
        self._shape = tuple(int(i) for i in shape)

        self._bindistances = tuple(bindistances)
        self._t_0 = tuple(t_0)
53
54
        if min(self._bindistances) <= 0:
            raise ValueError('Non-positive bindistances encountered')
55

56
57
        self._dim = int(reduce(lambda x, y: x*y, self._shape))
        self._dvol = float(reduce(lambda x, y: x*y, self._bindistances))
58
59
60
61
62
63
64
65
66

    @property
    def harmonic(self):
        return self._harmonic

    @property
    def shape(self):
        return self._shape

Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
67
    @property
68
69
70
71
72
73
74
75
76
77
78
79
80
    def scalar_dvol(self):
        return self._dvol

    @property
    def bindistances(self):
        return np.array(self._bindistances)

    @property
    def size(self):
        return np.prod(self._shape)

    @property
    def t_0(self):
Philipp Arras's avatar
Docs    
Philipp Arras committed
81
        """np.ndarray : array of coordinates of pixel ndim*(1,)."""
82
83
84
        return np.array(self._t_0)

    def __repr__(self):
85
86
        return ("LogRGSpace(shape={}, bindistances={}, t_0={}, harmonic={})".format(
            self.shape, self.bindistances, self.t_0, self.harmonic))
87
88

    def get_default_codomain(self):
Philipp Arras's avatar
Docs    
Philipp Arras committed
89
90
91
92
93
94
95
96
97
        """Returns a :class:`LogRGSpace` object representing the (position or
        harmonic) partner domain of `self`, depending on `self.harmonic`. The
        `bindistances` are transformed and `t_0` stays the same.

        Returns
        -------
        LogRGSpace
            The parter domain
        """
98
        codomain_bindistances = 1./(self.bindistances*self.shape)
Philipp Haim's avatar
Philipp Haim committed
99
        return LogRGSpace(self.shape, codomain_bindistances, self._t_0, not self.harmonic)
100
101

    def get_k_length_array(self):
Philipp Arras's avatar
Docs    
Philipp Arras committed
102
        """Generates array of distances to origin of the space.
Philipp Frank's avatar
docs    
Philipp Frank committed
103
104
105

        Returns
        -------
Philipp Arras's avatar
Docs    
Philipp Arras committed
106
107
108
109
        numpy.ndarray
            Distances to origin of the space. If any index of the array is
            zero then the distance is np.nan if self.harmonic True.
            The dtype is float64, the shape is `self.shape`.
Philipp Frank's avatar
docs    
Philipp Frank committed
110
111
112

        Raises
        ------
Philipp Arras's avatar
Docs    
Philipp Arras committed
113
114
        NotImplementedError
            If `self.harmonic` is False.
Philipp Frank's avatar
docs    
Philipp Frank committed
115
        """
116
117
118
119
120
121
        if not self.harmonic:
            raise NotImplementedError
        ks = self.get_k_array()
        return Field.from_global_data(self, np.linalg.norm(ks, axis=0))

    def get_k_array(self):
Philipp Arras's avatar
Docs    
Philipp Arras committed
122
        """Generates coordinates of the space.
Philipp Frank's avatar
docs    
Philipp Frank committed
123
124
125

        Returns
        -------
Philipp Arras's avatar
Docs    
Philipp Arras committed
126
127
128
129
130
        numpy.ndarray
            Coordinates of the space. If one index of the array is zero the
            corresponding coordinate is -np.inf (np.nan) if self.harmonic is
            False (True).
            The dtype is float64 and shape: `(len(self.shape),) + self.shape`.
Philipp Frank's avatar
docs    
Philipp Frank committed
131
        """
132
133
134
135
136
        ndim = len(self.shape)
        k_array = np.zeros((ndim,) + self.shape)
        dist = self.bindistances
        for i in range(ndim):
            ks = np.zeros(self.shape[i])
Martin Reinecke's avatar
Martin Reinecke committed
137
138
            ks[1:] = np.minimum(self.shape[i] - 1 - np.arange(self.shape[i]-1),
                                np.arange(self.shape[i]-1)) * dist[i]
139
140
141
142
143
            if self.harmonic:
                ks[0] = np.nan
            else:
                ks[0] = -np.inf
                ks[1:] += self.t_0[i]
Martin Reinecke's avatar
Martin Reinecke committed
144
145
            k_array[i] += ks.reshape((1,)*i + (self.shape[i],)
                                     + (1,)*(ndim-i-1))
146
        return k_array