domain_tuple.py 4.88 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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
16
17
18
19
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

from functools import reduce
Martin Reinecke's avatar
Martin Reinecke committed
20
from .domains.domain import Domain
Martin Reinecke's avatar
Martin Reinecke committed
21

Martin Reinecke's avatar
Martin Reinecke committed
22

Martin Reinecke's avatar
Martin Reinecke committed
23
class DomainTuple(object):
Martin Reinecke's avatar
Martin Reinecke committed
24
25
26
27
28
29
30
31
32
33
34
    """Ordered sequence of Domain objects.

    This class holds a set of :class:`Domain` objects, which together form the
    space on which a :class:`Field` is defined.

    Notes
    -----

    DomainTuples should never be created using the constructor, but rather
    via the factory function :attr:`make`!
    """
35
36
    _tupleCache = {}

Martin Reinecke's avatar
step 1  
Martin Reinecke committed
37
38
39
    def __init__(self, domain, _callingfrommake=False):
        if not _callingfrommake:
            raise NotImplementedError
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
        self._dom = self._parse_domain(domain)
        self._axtuple = self._get_axes_tuple()
        shape_tuple = tuple(sp.shape for sp in self._dom)
        self._shape = reduce(lambda x, y: x + y, shape_tuple, ())
Martin Reinecke's avatar
Martin Reinecke committed
44
        self._size = reduce(lambda x, y: x * y, self._shape, 1)
Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
48
49
50
51
52
53
54
55
56

    def _get_axes_tuple(self):
        i = 0
        res = [None]*len(self._dom)
        for idx, thing in enumerate(self._dom):
            nax = len(thing.shape)
            res[idx] = tuple(range(i, i+nax))
            i += nax
        return res

    @staticmethod
    def make(domain):
Martin Reinecke's avatar
Martin Reinecke committed
57
58
59
60
61
62
63
64
65
66
67
68
        """Returns a DomainTuple matching `domain`.

        This function checks whether a matching DomainTuple already exists.
        If yes, this object is returned, otherwise a new DomainTuple object
        is created and returned.

        Parameters
        ----------
        domain : Domain or tuple of Domain or DomainTuple
            The geometrical structure for which the DomainTuple shall be
            obtained.
        """
Martin Reinecke's avatar
Martin Reinecke committed
69
70
        if isinstance(domain, DomainTuple):
            return domain
71
72
73
74
        domain = DomainTuple._parse_domain(domain)
        obj = DomainTuple._tupleCache.get(domain)
        if obj is not None:
            return obj
Martin Reinecke's avatar
step 1  
Martin Reinecke committed
75
        obj = DomainTuple(domain, _callingfrommake=True)
76
77
        DomainTuple._tupleCache[domain] = obj
        return obj
Martin Reinecke's avatar
Martin Reinecke committed
78
79
80
81
82

    @staticmethod
    def _parse_domain(domain):
        if domain is None:
            return ()
Martin Reinecke's avatar
Martin Reinecke committed
83
        if isinstance(domain, Domain):
Martin Reinecke's avatar
Martin Reinecke committed
84
85
86
87
88
            return (domain,)

        if not isinstance(domain, tuple):
            domain = tuple(domain)
        for d in domain:
Martin Reinecke's avatar
Martin Reinecke committed
89
            if not isinstance(d, Domain):
Martin Reinecke's avatar
Martin Reinecke committed
90
91
                raise TypeError(
                    "Given object contains something that is not an "
Martin Reinecke's avatar
Martin Reinecke committed
92
                    "instance of Domain class.")
Martin Reinecke's avatar
Martin Reinecke committed
93
94
95
96
97
98
99
        return domain

    def __getitem__(self, i):
        return self._dom[i]

    @property
    def shape(self):
Martin Reinecke's avatar
Martin Reinecke committed
100
101
102
103
104
        """tuple of int: number of pixels along each axis

        The shape of the array-like object required to store information
        living on the DomainTuple.
        """
Martin Reinecke's avatar
Martin Reinecke committed
105
106
        return self._shape

107
108
109
110
111
112
113
114
115
116
    @property
    def local_shape(self):
        """tuple of int: number of pixels along each axis on the local task

        The shape of the array-like object required to store information
        living on part of the domain which is stored on the local MPI task.
        """
        from .dobj import local_shape
        return local_shape(self._shape)

Martin Reinecke's avatar
Martin Reinecke committed
117
    @property
Martin Reinecke's avatar
Martin Reinecke committed
118
    def size(self):
Martin Reinecke's avatar
Martin Reinecke committed
119
120
121
122
        """int : total number of pixels.

        Equivalent to the products over all entries in the object's shape.
        """
Martin Reinecke's avatar
Martin Reinecke committed
123
        return self._size
Martin Reinecke's avatar
Martin Reinecke committed
124
125
126

    @property
    def axes(self):
Martin Reinecke's avatar
Martin Reinecke committed
127
        """tuple of tuple of int : axis indices of the underlying domains"""
Martin Reinecke's avatar
Martin Reinecke committed
128
129
130
131
132
133
134
135
136
        return self._axtuple

    def __len__(self):
        return len(self._dom)

    def __hash__(self):
        return self._dom.__hash__()

    def __eq__(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
137
138
139
        if self is x:
            return True
        x = DomainTuple.make(x)
Martin Reinecke's avatar
Martin Reinecke committed
140
        return self is x
Martin Reinecke's avatar
Martin Reinecke committed
141
142

    def __ne__(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
143
        return not self.__eq__(x)
144

Martin Reinecke's avatar
Martin Reinecke committed
145
146
147
148
149
150
151
    def compatibleTo(self, x):
        return self.__eq__(x)

    def subsetOf(self, x):
        return self.__eq__(x)

    def unitedWith(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
152
153
154
155
        if self is x:
            return self
        x = DomainTuple.make(x)
        if self is not x:
Martin Reinecke's avatar
Martin Reinecke committed
156
157
158
            raise ValueError("domain mismatch")
        return self

159
    def __str__(self):
Martin Reinecke's avatar
Martin Reinecke committed
160
161
        res = "DomainTuple, len: " + str(len(self))
        for i in self:
162
163
            res += "\n" + str(i)
        return res