From 5e42cb29f4d30b74fb5c05ac4d9dfcc692e3b72d Mon Sep 17 00:00:00 2001 From: Martin Reinecke <martin@mpa-garching.mpg.de> Date: Wed, 25 Oct 2017 21:49:59 +0200 Subject: [PATCH] rearranging --- nifty/__init__.py | 4 +-- nifty/data_objects/numpy_do.py | 9 +++++-- nifty/{ => data_objects}/random.py | 0 nifty/field.py | 7 +++--- nifty/spaces/power_space.py | 2 +- nifty/sugar.py | 40 +++++++++++++++++++----------- 6 files changed, 38 insertions(+), 24 deletions(-) rename nifty/{ => data_objects}/random.py (100%) diff --git a/nifty/__init__.py b/nifty/__init__.py index 2f6ad33dc..8595f2c0c 100644 --- a/nifty/__init__.py +++ b/nifty/__init__.py @@ -8,8 +8,6 @@ from .domain_tuple import DomainTuple from .domain_object import DomainObject -from .random import Random - from .basic_arithmetics import * from .nifty_utilities import * @@ -32,6 +30,6 @@ from . import plotting from . import library -from .data_objects import numpy_do as dobj +from . import dobj from .memoization import memo diff --git a/nifty/data_objects/numpy_do.py b/nifty/data_objects/numpy_do.py index f311a1657..49f6da1c3 100644 --- a/nifty/data_objects/numpy_do.py +++ b/nifty/data_objects/numpy_do.py @@ -3,8 +3,13 @@ import numpy as np from numpy import ndarray as data_object from numpy import full, empty, sqrt, ones, zeros, vdot, abs, bincount, exp, log -from ..nifty_utilities import cast_iseq_to_tuple, get_slice_list -from functools import reduce +from .random import Random + def from_object(object, dtype=None, copy=True): return np.array(object, dtype=dtype, copy=copy) + + +def from_random(random_type, shape, dtype=np.float64, **kwargs): + generator_function = getattr(Random, random_type) + return generator_function(dtype=dtype, shape=shape, **kwargs) diff --git a/nifty/random.py b/nifty/data_objects/random.py similarity index 100% rename from nifty/random.py rename to nifty/data_objects/random.py diff --git a/nifty/field.py b/nifty/field.py index 76b50259c..c7f371955 100644 --- a/nifty/field.py +++ b/nifty/field.py @@ -20,7 +20,6 @@ from __future__ import division, print_function from builtins import range import numpy as np from . import nifty_utilities as utilities -from .random import Random from .domain_tuple import DomainTuple from functools import reduce from . import dobj @@ -184,9 +183,9 @@ class Field(object): """ domain = DomainTuple.make(domain) - generator_function = getattr(Random, random_type) - return Field(domain=domain, val=generator_function(dtype=dtype, - shape=domain.shape, **kwargs)) + return Field(domain=domain, + val=dobj.from_random(random_type, dtype=dtype, + shape=domain.shape, **kwargs)) # ---Properties--- diff --git a/nifty/spaces/power_space.py b/nifty/spaces/power_space.py index 01c3543eb..07f2f910c 100644 --- a/nifty/spaces/power_space.py +++ b/nifty/spaces/power_space.py @@ -141,7 +141,7 @@ class PowerSpace(Space): harmonic_partner=self.harmonic_partner, k_length_array=k_length_array, binbounds=binbounds) - temp_rho = dobj.bincount(temp_pindex.ravel()) + temp_rho = np.bincount(temp_pindex.ravel()) assert not np.any(temp_rho == 0), "empty bins detected" temp_k_lengths = np.bincount(temp_pindex.ravel(), weights=k_length_array.ravel()) \ diff --git a/nifty/sugar.py b/nifty/sugar.py index 6aaefeb0e..d5542282b 100644 --- a/nifty/sugar.py +++ b/nifty/sugar.py @@ -17,14 +17,15 @@ # and financially supported by the Studienstiftung des deutschen Volkes. import numpy as np - from . import Space,\ - PowerSpace,\ - Field,\ - ComposedOperator,\ - DiagonalOperator,\ - FFTOperator,\ - sqrt + PowerSpace,\ + Field,\ + ComposedOperator,\ + DiagonalOperator,\ + PowerProjectionOperator,\ + FFTOperator,\ + sqrt,\ + DomainTuple from . import nifty_utilities as utilities __all__ = ['power_analyze', @@ -223,16 +224,16 @@ def create_power_field(domain, power_spectrum, dtype=None): power_domain = PowerSpace(domain) fp = Field(power_domain, val=power_spectrum(power_domain.k_lengths), dtype=dtype) - f = power_synthesize_special(fp) + P = PowerProjectionOperator(domain, power_domain) + f = P.adjoint_times(fp) if not issubclass(fp.dtype.type, np.complexfloating): f = f.real - f **= 2 return f -def create_power_operator(domain, power_spectrum, dtype=None): +def create_power_operator(domain, power_spectrum, space=None, dtype=None): """ Creates a diagonal operator with the given power spectrum. Constructs a diagonal operator that lives over the specified domain. @@ -241,9 +242,10 @@ def create_power_operator(domain, power_spectrum, dtype=None): ---------- domain : DomainObject Domain over which the power operator shall live. - power_spectrum : callable - A method that implements the square root of a power spectrum as a - function of k. + power_spectrum : callable of Field + An object that implements the power spectrum as a function of k. + space : int + the domain index on which the power operator will work dtype : type *optional* dtype that the field holding the power spectrum shall use (default : None). @@ -254,8 +256,18 @@ def create_power_operator(domain, power_spectrum, dtype=None): DiagonalOperator : An operator that implements the given power spectrum. """ + domain = DomainTuple.make(domain) + if space is None: + if len(domain)!=1: + raise ValueError("space keyword must be set") + else: + space = 0 + space = int(space) return DiagonalOperator( - create_power_field(domain, power_spectrum, dtype).weight(1)) + create_power_field(domain[space], + power_spectrum, dtype).weight(1), + domain=domain, + spaces=space) def generate_posterior_sample(mean, covariance): -- GitLab