Commit 876fde66 authored by Martin Reinecke's avatar Martin Reinecke

introduce and use scalar_weight()

parent 2630bd70
Pipeline #17906 passed with stage
in 3 minutes and 21 seconds
......@@ -126,6 +126,26 @@ class DomainObject(with_metaclass(
raise NotImplementedError(
"There is no generic dim for DomainObject.")
@abc.abstractmethod
def scalar_weight(self):
""" Returns the volume factors of this domain as a floating
point scalar, if the volume factors are all identical, otherwise
returns None.
Returns
-------
float or None
Volume factor
Raises
------
NotImplementedError
If called for this abstract class.
"""
raise NotImplementedError(
"There is no generic scalar_weight method for DomainObject.")
@abc.abstractmethod
def weight(self):
""" Returns the volume factors of this domain, either as a floating
......
......@@ -612,6 +612,20 @@ class Field(object):
domain = self.domain
return Field(domain=domain, val=self._val, dtype=dtype, copy=True)
def scalar_weight(self, spaces=None):
if np.isscalar(spaces):
return self.domain[spaces].scalar_weight()
res = 1.
if spaces is None:
spaces = range(len(self.domain))
for i in spaces:
tmp = self.domain[i].scalar_weight()
if tmp is None:
return None
res *= tmp
return res
def weight(self, power=1, inplace=False, spaces=None):
""" Weights the pixels of `self` with their invidual pixel-volume.
......@@ -680,10 +694,19 @@ class Field(object):
"the NIFTy field class")
# Compute the dot respecting the fact of discrete/continuous spaces
y = self if bare else self.weight(power=1)
fct = 1.
if bare:
y = self
else:
tmp = self.scalar_weight(spaces)
if tmp is None:
y = self.weight(power=1)
else:
y = self
fct = tmp
if spaces is None:
return np.vdot(y.val.reshape(-1), x.val.reshape(-1))
return fct*np.vdot(y.val.reshape(-1), x.val.reshape(-1))
else:
# create a diagonal operator which is capable of taking care of the
# axes-matching
......@@ -693,7 +716,7 @@ class Field(object):
diagonal=diagonal,
copy=False)
dotted = diagonalOperator(x, spaces=spaces)
return dotted.sum(spaces=spaces)
return fct*dotted.sum(spaces=spaces)
def norm(self):
""" Computes the L2-norm of the field values.
......
......@@ -20,5 +20,8 @@ from ..domain_object import DomainObject
class FieldType(DomainObject):
def scalar_weight(self):
return 1.
def weight(self):
return 1.
......@@ -111,6 +111,9 @@ class GLSpace(Space):
def copy(self):
return self.__class__(nlat=self.nlat, nlon=self.nlon)
def scalar_weight(self):
return None
def weight(self):
from pyHealpix import GL_weights
vol = GL_weights(self.nlat, self.nlon)
......
......@@ -106,6 +106,9 @@ class HPSpace(Space):
def copy(self):
return self.__class__(nside=self.nside)
def scalar_weight(self):
return np.pi / (3*self._nside*self._nside)
def weight(self):
return np.pi / (3*self._nside*self._nside)
......
......@@ -116,6 +116,9 @@ class LMSpace(Space):
def copy(self):
return self.__class__(lmax=self.lmax)
def scalar_weight(self):
return 1.
def weight(self):
return 1.
......
......@@ -208,8 +208,11 @@ class PowerSpace(Space):
return self.__class__(harmonic_partner=self.harmonic_partner,
binbounds=self._binbounds)
def scalar_weight(self):
return None
def weight(self):
# MR FIXME: this will probably change to 1 soon
# MR FIXME: this will probably change to 1 soon
return np.asarray(self.rho, dtype=np.float64)
def get_distance_array(self):
......
......@@ -115,6 +115,9 @@ class RGSpace(Space):
distances=self.distances,
harmonic=self.harmonic)
def scalar_weight(self):
return reduce(lambda x, y: x*y, self.distances)
def weight(self):
return reduce(lambda x, y: x*y, self.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