domain.py 2.71 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.
18 19

import abc
Martin Reinecke's avatar
Martin Reinecke committed
20
from ..utilities import NiftyMeta
Martin Reinecke's avatar
Martin Reinecke committed
21
from future.utils import with_metaclass
Martin Reinecke's avatar
Martin Reinecke committed
22
import numpy as np
23 24


Martin Reinecke's avatar
Martin Reinecke committed
25
class Domain(with_metaclass(
Martin Reinecke's avatar
Martin Reinecke committed
26
        NiftyMeta, type('NewBase', (object,), {}))):
Martin Reinecke's avatar
Martin Reinecke committed
27
    """The abstract class repesenting a (structured or unstructured) domain.
28
    """
29

Martin Reinecke's avatar
Martin Reinecke committed
30
    def __init__(self):
31
        self._needed_for_hash = []
32

33 34 35 36
    @abc.abstractmethod
    def __repr__(self):
        raise NotImplementedError

37
    def __hash__(self):
Martin Reinecke's avatar
Martin Reinecke committed
38 39 40 41 42 43 44
        """Returns a hash value for the object.

        Notes
        -----
            Only members that are explicitly added to
            :py:attr:`._needed_for_hash` will be used for hashing.
        """
45
        result_hash = 0
46 47
        for key in self._needed_for_hash:
            result_hash ^= hash(vars(self)[key])
48 49 50
        return result_hash

    def __eq__(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
51
        """Checks whether two domains are equal.
Theo Steininger's avatar
Theo Steininger committed
52

53 54
        Parameters
        ----------
Martin Reinecke's avatar
Martin Reinecke committed
55
        x : Domain
Martin Reinecke's avatar
Martin Reinecke committed
56
            The domain `self` is compared to.
Theo Steininger's avatar
Theo Steininger committed
57

58 59
        Returns
        -------
Martin Reinecke's avatar
Martin Reinecke committed
60
        bool : True iff `self` and x describe the same domain.
61
        """
Martin Reinecke's avatar
Martin Reinecke committed
62 63
        if self is x:  # shortcut for simple case
            return True
Martin Reinecke's avatar
Martin Reinecke committed
64
        if not isinstance(x, type(self)):
65
            return False
66 67 68
        for key in self._needed_for_hash:
            if vars(self)[key] != vars(x)[key]:
                return False
Martin Reinecke's avatar
Martin Reinecke committed
69
        return True
70 71

    def __ne__(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
72
        """Returns the opposite of :py:meth:`.__eq__()`"""
73 74 75 76
        return not self.__eq__(x)

    @abc.abstractproperty
    def shape(self):
Martin Reinecke's avatar
Martin Reinecke committed
77
        """tuple of ints: number of pixels along each axis
Theo Steininger's avatar
Theo Steininger committed
78

Martin Reinecke's avatar
Martin Reinecke committed
79 80
        The shape of the array-like object required to store information
        living on the domain.
81
        """
Martin Reinecke's avatar
Martin Reinecke committed
82
        raise NotImplementedError
83 84

    @abc.abstractproperty
Martin Reinecke's avatar
Martin Reinecke committed
85
    def size(self):
Martin Reinecke's avatar
Martin Reinecke committed
86
        """int: total number of pixels.
87

Martin Reinecke's avatar
Martin Reinecke committed
88
        Equivalent to the products over all entries in the domain's shape.
89
        """
Martin Reinecke's avatar
Martin Reinecke committed
90
        raise NotImplementedError