Commit dfdfff76 authored by Martin Reinecke's avatar Martin Reinecke

weight() -> dvol() for domain objects

parent 537a2d3d
Pipeline #19131 canceled with stage
......@@ -120,7 +120,7 @@ class DomainObject(with_metaclass(
"There is no generic dim for DomainObject.")
@abc.abstractmethod
def scalar_weight(self):
def scalar_dvol(self):
""" Returns the volume factors of this domain as a floating
point scalar, if the volume factors are all identical, otherwise
returns None.
......@@ -139,8 +139,7 @@ class DomainObject(with_metaclass(
raise NotImplementedError(
"There is no generic scalar_weight method for DomainObject.")
@abc.abstractmethod
def weight(self):
def dvol(self):
""" Returns the volume factors of this domain, either as a floating
point scalar (if the volume factors are all identical) or as a
floating point array with a shape of `self.shape`.
......@@ -157,5 +156,4 @@ class DomainObject(with_metaclass(
If called for this abstract class.
"""
raise NotImplementedError(
"There is no generic weight method for DomainObject.")
return self.scalar_dvol()
......@@ -416,13 +416,13 @@ class Field(object):
def scalar_weight(self, spaces=None):
if np.isscalar(spaces):
return self.domain[spaces].scalar_weight()
return self.domain[spaces].scalar_dvol()
if spaces is None:
spaces = range(len(self.domain))
res = 1.
for i in spaces:
tmp = self.domain[i].scalar_weight()
tmp = self.domain[i].scalar_dvol()
if tmp is None:
return None
res *= tmp
......@@ -458,7 +458,7 @@ class Field(object):
fct = 1.
for ind in spaces:
wgt = self.domain[ind].weight()
wgt = self.domain[ind].dvol()
if np.isscalar(wgt):
fct *= wgt
else:
......
......@@ -20,8 +20,5 @@ from ..domain_object import DomainObject
class FieldType(DomainObject):
def scalar_weight(self):
return 1.
def weight(self):
def scalar_dvol(self):
return 1.
......@@ -62,9 +62,9 @@ class RGRGTransformation(Transformation):
# BUT: the pixel volumes of the domain and codomain are different.
# Hence, in order to produce the same scalar product, power==1.
if self.codomain.harmonic:
fct = self.domain.weight()
fct = self.domain.scalar_dvol()
else:
fct = 1./(self.codomain.weight()*self.domain.dim)
fct = 1./(self.codomain.scalar_dvol()*self.domain.dim)
# Perform the transformation
if issubclass(val.dtype.type, np.complexfloating):
......
......@@ -86,7 +86,7 @@ class GLSpace(Space):
self._nlat = self._parse_nlat(nlat)
self._nlon = self._parse_nlon(nlon)
self._wgt = None
self._dvol = None
# ---Mandatory properties and methods---
......@@ -109,16 +109,16 @@ class GLSpace(Space):
def total_volume(self):
return 4 * np.pi
def scalar_weight(self):
def scalar_dvol(self):
return None
# MR FIXME: this is potentially wasteful, since the return array is
# blown up by a factor of self.nlon
def weight(self):
def dvol(self):
from pyHealpix import GL_weights
if self._wgt is None:
self._wgt = GL_weights(self.nlat, self.nlon)
return np.repeat(self._wgt, self.nlon)
if self._dvol is None:
self._dvol = GL_weights(self.nlat, self.nlon)
return np.repeat(self._dvol, self.nlon)
# ---Added properties and methods---
......
......@@ -104,10 +104,7 @@ class HPSpace(Space):
def total_volume(self):
return 4 * np.pi
def scalar_weight(self):
return np.pi / (3*self._nside*self._nside)
def weight(self):
def scalar_dvol(self):
return np.pi / (3*self._nside*self._nside)
# ---Added properties and methods---
......
......@@ -114,10 +114,7 @@ class LMSpace(Space):
# the individual pixels have a fixed volume of 1.
return np.float64(self.dim)
def scalar_weight(self):
return 1.
def weight(self):
def scalar_dvol(self):
return 1.
def get_k_length_array(self):
......
......@@ -204,10 +204,10 @@ class PowerSpace(Space):
# every power-pixel has a volume of 1
return float(reduce(lambda x, y: x*y, self.pindex.shape))
def scalar_weight(self):
def scalar_dvol(self):
return None
def weight(self):
def dvol(self):
# MR FIXME: this will probably change to 1 soon
return np.asarray(self.rho, dtype=np.float64)
......
......@@ -87,7 +87,7 @@ class RGSpace(Space):
self._harmonic = bool(harmonic)
self._shape = self._parse_shape(shape)
self._distances = self._parse_distances(distances)
self._wgt = float(reduce(lambda x, y: x*y, self._distances))
self._dvol = float(reduce(lambda x, y: x*y, self._distances))
self._dim = int(reduce(lambda x, y: x*y, self._shape))
def __repr__(self):
......@@ -108,13 +108,10 @@ class RGSpace(Space):
@property
def total_volume(self):
return self.dim * self._wgt
return self.dim * self._dvol
def scalar_weight(self):
return self._wgt
def weight(self):
return self._wgt
def scalar_dvol(self):
return self._dvol
def get_k_length_array(self):
if (not self.harmonic):
......
......@@ -41,14 +41,14 @@ CONSTRUCTOR_CONFIGS = [
]
def get_weight_configs():
def get_dvol_configs():
np.random.seed(42)
wgt = [2.0943951, 2.0943951]
# for GLSpace(nlat=2, nlon=3)
weight_0 = np.array(list(itertools.chain.from_iterable(
dvol_0 = np.array(list(itertools.chain.from_iterable(
itertools.repeat(x, 3) for x in wgt)))
return [
[1, weight_0],
[1, dvol_0],
]
......@@ -73,6 +73,6 @@ class GLSpaceFunctionalityTests(unittest.TestCase):
for key, value in expected.items():
assert_equal(getattr(g, key), value)
@expand(get_weight_configs())
def test_weight(self, power, expected):
assert_almost_equal(GLSpace(2).weight(), expected)
@expand(get_dvol_configs())
def test_dvol(self, power, expected):
assert_almost_equal(GLSpace(2).dvol(), expected)
......@@ -72,5 +72,5 @@ class HPSpaceFunctionalityTests(unittest.TestCase):
for key, value in expected.items():
assert_equal(getattr(h, key), value)
def test_weight(self):
assert_almost_equal(HPSpace(2).weight(), np.pi/12)
def test_dvol(self):
assert_almost_equal(HPSpace(2).dvol(), np.pi/12)
......@@ -89,8 +89,8 @@ class LMSpaceFunctionalityTests(unittest.TestCase):
for key, value in expected.items():
assert_equal(getattr(l, key), value)
def test_weight(self):
assert_almost_equal(LMSpace(5).weight(), 1.)
def test_dvol(self):
assert_almost_equal(LMSpace(5).dvol(), 1.)
@expand(get_k_length_array_configs())
def test_k_length_array(self, lmax, expected):
......
......@@ -129,6 +129,6 @@ class PowerSpaceFunctionalityTest(unittest.TestCase):
p = PowerSpace(harmonic_partner=harmonic_partner)
assert_almost_equal(p.get_k_length_array(), expected)
def test_weight(self):
def test_dvol(self):
p = PowerSpace(harmonic_partner=RGSpace(10,harmonic=True))
assert_almost_equal(p.weight(),p.rho)
assert_almost_equal(p.dvol(),p.rho)
......@@ -88,7 +88,7 @@ def get_k_length_array_configs():
]
def get_weight_configs():
def get_dvol_configs():
np.random.seed(42)
return [
[(11, 11), None, False, 1],
......@@ -122,7 +122,7 @@ class RGSpaceFunctionalityTests(unittest.TestCase):
r = RGSpace(shape=shape, distances=distances, harmonic=True)
assert_almost_equal(r.get_k_length_array(), expected)
@expand(get_weight_configs())
def test_weight(self, shape, distances, harmonic, power):
@expand(get_dvol_configs())
def test_dvol(self, shape, distances, harmonic, power):
r = RGSpace(shape=shape, distances=distances, harmonic=harmonic)
assert_almost_equal(r.weight(), np.prod(r.distances)**power)
assert_almost_equal(r.dvol(), np.prod(r.distances)**power)
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