Commit 4c2a4012 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

tweak DomainObject comparisons

parent fd036abf
...@@ -43,8 +43,7 @@ class DomainObject(with_metaclass( ...@@ -43,8 +43,7 @@ class DomainObject(with_metaclass(
""" """
def __init__(self): def __init__(self):
# _global_id is used in the Versioning module from keepers self._needed_for_hash = []
self._ignore_for_hash = ['_global_id']
@abc.abstractmethod @abc.abstractmethod
def __repr__(self): def __repr__(self):
...@@ -53,11 +52,8 @@ class DomainObject(with_metaclass( ...@@ -53,11 +52,8 @@ class DomainObject(with_metaclass(
def __hash__(self): def __hash__(self):
# Extract the identifying parts from the vars(self) dict. # Extract the identifying parts from the vars(self) dict.
result_hash = 0 result_hash = 0
for key in sorted(vars(self).keys()): for key in self._needed_for_hash:
item = vars(self)[key] result_hash ^= hash(vars(self)[key])
if key in self._ignore_for_hash or key == '_ignore_for_hash':
continue
result_hash ^= item.__hash__() ^ int(hash(key)//117)
return result_hash return result_hash
def __eq__(self, x): def __eq__(self, x):
...@@ -74,18 +70,14 @@ class DomainObject(with_metaclass( ...@@ -74,18 +70,14 @@ class DomainObject(with_metaclass(
True if `self` and x describe the same manifold. True if `self` and x describe the same manifold.
""" """
if self is x: # shortcut for simple case
if isinstance(x, type(self)):
for key in list(vars(self).keys()):
item1 = vars(self)[key]
if key in self._ignore_for_hash or key == '_ignore_for_hash':
continue
item2 = vars(x)[key]
if item1 != item2:
return False
return True return True
else: if not isinstance(x, type(self)):
return False return False
for key in self._needed_for_hash:
if vars(self)[key] != vars(x)[key]:
return False
return True
def __ne__(self, x): def __ne__(self, x):
return not self.__eq__(x) return not self.__eq__(x)
......
...@@ -94,6 +94,7 @@ class GLSpace(Space): ...@@ -94,6 +94,7 @@ class GLSpace(Space):
"The module pyHealpix is needed but not available.") "The module pyHealpix is needed but not available.")
super(GLSpace, self).__init__() super(GLSpace, self).__init__()
self._needed_for_hash += ["_nlat", "_nlon"]
self._nlat = self._parse_nlat(nlat) self._nlat = self._parse_nlat(nlat)
self._nlon = self._parse_nlon(nlon) self._nlon = self._parse_nlon(nlon)
......
...@@ -80,7 +80,7 @@ class HPSpace(Space): ...@@ -80,7 +80,7 @@ class HPSpace(Space):
def __init__(self, nside): def __init__(self, nside):
super(HPSpace, self).__init__() super(HPSpace, self).__init__()
self._needed_for_hash += ["_nside"]
self._nside = self._parse_nside(nside) self._nside = self._parse_nside(nside)
# ---Mandatory properties and methods--- # ---Mandatory properties and methods---
...@@ -94,7 +94,7 @@ class HPSpace(Space): ...@@ -94,7 +94,7 @@ class HPSpace(Space):
@property @property
def shape(self): def shape(self):
return (np.int(12 * self.nside * self.nside),) return (self.dim,)
@property @property
def dim(self): def dim(self):
......
...@@ -87,6 +87,7 @@ class LMSpace(Space): ...@@ -87,6 +87,7 @@ class LMSpace(Space):
def __init__(self, lmax): def __init__(self, lmax):
super(LMSpace, self).__init__() super(LMSpace, self).__init__()
self._needed_for_hash += ["_lmax"]
self._lmax = self._parse_lmax(lmax) self._lmax = self._parse_lmax(lmax)
# ---Mandatory properties and methods--- # ---Mandatory properties and methods---
......
...@@ -87,8 +87,8 @@ class PowerSpace(Space): ...@@ -87,8 +87,8 @@ class PowerSpace(Space):
def __init__(self, harmonic_partner, distribution_strategy=None, def __init__(self, harmonic_partner, distribution_strategy=None,
volume_type='rho', binbounds=None): volume_type='rho', binbounds=None):
super(PowerSpace, self).__init__() super(PowerSpace, self).__init__()
self._ignore_for_hash += ['_pindex', '_kindex', '_rho', self._needed_for_hash += ['_harmonic_partner', '_binbounds',
'_volume_weight'] 'volume_type']
if distribution_strategy is None: if distribution_strategy is None:
distribution_strategy = gc['default_distribution_strategy'] distribution_strategy = gc['default_distribution_strategy']
......
...@@ -75,10 +75,10 @@ class RGSpace(Space): ...@@ -75,10 +75,10 @@ class RGSpace(Space):
# ---Overwritten properties and methods--- # ---Overwritten properties and methods---
def __init__(self, shape, distances=None, harmonic=False): def __init__(self, shape, distances=None, harmonic=False):
self._harmonic = bool(harmonic)
super(RGSpace, self).__init__() super(RGSpace, self).__init__()
self._needed_for_hash += ["_distances", "_shape", "_harmonic"]
self._harmonic = bool(harmonic)
self._shape = self._parse_shape(shape) self._shape = self._parse_shape(shape)
self._distances = self._parse_distances(distances) self._distances = self._parse_distances(distances)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment