From 1763f1ca2b53e98c81c3fdba658e26314c56d0c0 Mon Sep 17 00:00:00 2001 From: Lukas Platz <lplatz@mpa-garching.mpg.de> Date: Tue, 29 Oct 2019 18:11:36 +0100 Subject: [PATCH] move total_volume functionality to DomainTuple --- nifty5/domain_tuple.py | 27 +++++++++++++++++++++++---- nifty5/field.py | 18 ++++-------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/nifty5/domain_tuple.py b/nifty5/domain_tuple.py index e2fee3006..b0199a831 100644 --- a/nifty5/domain_tuple.py +++ b/nifty5/domain_tuple.py @@ -19,6 +19,8 @@ from functools import reduce from . import utilities from .domains.domain import Domain +import numpy as np + class DomainTuple(object): """Ordered sequence of Domain objects. @@ -125,11 +127,28 @@ class DomainTuple(object): """ return self._size - @property - def total_volume(self): + def total_volume(self, spaces=None): + """Returns the total volume of `self` or of a subspace of it. + + Parameters + ---------- + spaces : int, tuple of int or None + Indices of the sub-domains of the domain to be considered. + If `None`, the total volume of the whole domain is returned. + + Returns + ------- + float + the total volume of the requested (sub-)domain. + """ + if np.isscalar(spaces): + return self._dom[spaces].total_volume + + if spaces is None: + spaces = range(len(self._dom)) res = 1. - for d in self._dom: - res *= d.total_volume + for i in spaces: + res *= self._dom[i].total_volume return res @property diff --git a/nifty5/field.py b/nifty5/field.py index 6958421fe..c43b93d28 100644 --- a/nifty5/field.py +++ b/nifty5/field.py @@ -260,30 +260,20 @@ class Field(object): return res def total_volume(self, spaces=None): - """Returns the total volume of a sub-domain of `self`. + """Returns the total volume of the field's domain or of a subspace of it. Parameters ---------- spaces : int, tuple of int or None Indices of the sub-domains of the field's domain to be considered. - If `None`, the entire domain is used. + If `None`, the total volume of the whole domain is returned. Returns ------- float - the total volume of the requested sub-domain. + the total volume of the requested (sub-)domain. """ - if spaces is None: - return self._domain.total_volume - - if np.isscalar(spaces): - return self._domain[spaces].total_volume - - # tuple of spaces given - res = 1. - for i in spaces: - res *= self._domain[i].total_volume - return res + return self._domain.total_volume(spaces) def weight(self, power=1, spaces=None): """Weights the pixels of `self` with their invidual pixel volumes. -- GitLab