Commit be80ac86 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

remove marco_binbounds(), introduce useful_binbounds()

parent 3102f53d
......@@ -122,6 +122,35 @@ class PowerSpace(Space):
np.log(float(last_bound)),
nbin-1, base=np.e)
@staticmethod
def useful_binbounds(space, logarithmic, nbin=None):
if not (isinstance(space, Space) and space.harmonic):
raise ValueError("first argument must be a harmonic space.")
if logarithmic is None and nbin is None:
return None
nbin = None if nbin is None else int(nbin)
logarithmic = bool(logarithmic)
dists = space.get_unique_distances()
if len(dists) < 3:
raise ValueError("Space does not have enough unique k lengths")
lbound = 0.5*(dists[0]+dists[1])
rbound = 0.5*(dists[-2]+dists[-1])
dists[0] = lbound
dists[-1] = rbound
if logarithmic:
dists = np.log(dists)
binsz_min = np.max(np.diff(dists))
nbin_max = int((dists[-1]-dists[0])/binsz_min)+2
if nbin is None:
nbin = nbin_max
assert nbin >= 3, "nbin must be at least 3"
if nbin > nbin_max:
raise ValueError("nbin is too large")
if logarithmic:
return PowerSpace.logarithmic_binbounds(nbin, lbound, rbound)
else:
return PowerSpace.linear_binbounds(nbin, lbound, rbound)
def __init__(self, harmonic_partner, distribution_strategy=None,
binbounds=None):
super(PowerSpace, self).__init__()
......
......@@ -18,9 +18,8 @@
from builtins import str
from parameterized import parameterized
from nifty import Space, RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace
from nifty import RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace
from nifty.config import dependency_injector as gdi
import numpy as np
def custom_name_func(testcase_func, param_num, param):
......@@ -46,35 +45,3 @@ def generate_spaces():
def generate_harmonic_spaces():
spaces = [RGSpace(4, harmonic=True), LMSpace(5)]
return spaces
def marco_binbounds(space, logarithmic, nbin=None):
"""Only for testing purposes. DO NOT USE IN REAL LIFE!"""
if logarithmic is None and nbin is None:
return None
if not (isinstance(space, Space) and space.harmonic):
raise ValueError("space must be a harmonic space.")
logarithmic = bool(logarithmic)
if nbin is not None:
nbin = int(nbin)
assert nbin >= 3, "nbin must be at least 3"
# equidistant binning (linear or log)
# MR FIXME: this needs to improve
kindex = space.get_unique_distances()
if (logarithmic):
k = np.r_[0, np.log(kindex[1:])]
else:
k = kindex
dk = np.max(k[2:] - k[1:-1]) # minimum dk to avoid empty bins
if(nbin is None):
nbin = int((k[-1] - 0.5 * (k[2] + k[1])) /
dk - 0.5) # maximal nbin
else:
nbin = min(int(nbin), int(
(k[-1] - 0.5 * (k[2] + k[1])) / dk + 2.5))
dk = (k[-1] - 0.5 * (k[2] + k[1])) / (nbin - 2.5)
bb = np.r_[0.5 * (3 * k[1] - k[2]),
0.5 * (k[1] + k[2]) + dk * np.arange(nbin-2)]
if(logarithmic):
bb = np.exp(bb)
return bb
......@@ -21,14 +21,15 @@ import numpy as np
import nifty as ift
from numpy.testing import assert_allclose
from itertools import product
from test.common import expand, marco_binbounds
from test.common import expand
class LaplaceOperatorTests(unittest.TestCase):
@expand(product([None, False, True], [False, True], [10, 100, 1000]))
def test_Laplace(self, log1, log2, sz):
s = ift.RGSpace(sz, harmonic=True)
p = ift.PowerSpace(s, binbounds=marco_binbounds(s, logarithmic=log1))
p = ift.PowerSpace(s, binbounds=ift.PowerSpace.useful_binbounds(s,
logarithmic=log1))
L = ift.LaplaceOperator(p, logarithmic=log2)
arr = np.random.random(p.shape[0])
fp = ift.Field(p, val=arr)
......
......@@ -18,8 +18,7 @@
import unittest
import numpy as np
from numpy.testing import assert_equal, assert_approx_equal,\
assert_allclose
from numpy.testing import assert_approx_equal, assert_allclose
from nifty import Field,\
RGSpace,\
......@@ -27,7 +26,7 @@ from nifty import Field,\
SmoothingOperator
from itertools import product
from test.common import expand, marco_binbounds
from test.common import expand
def _get_rtol(tp):
......@@ -36,18 +35,19 @@ def _get_rtol(tp):
else:
return 1e-5
class SmoothingOperator_Tests(unittest.TestCase):
spaces = [RGSpace(128)]
@expand(product(spaces, [0., .5, 5.], [True, False]))
def test_property(self, space, sigma, log_distances):
op = SmoothingOperator.make(space, sigma=sigma,
log_distances=log_distances)
log_distances=log_distances)
if op.domain[0] != space:
raise TypeError
if op.unitary != False:
if op.unitary:
raise ValueError
if op.self_adjoint != True:
if not op.self_adjoint:
raise ValueError
if op.sigma != sigma:
raise ValueError
......@@ -57,7 +57,7 @@ class SmoothingOperator_Tests(unittest.TestCase):
@expand(product(spaces, [0., .5, 5.], [True, False]))
def test_adjoint_times(self, space, sigma, log_distances):
op = SmoothingOperator.make(space, sigma=sigma,
log_distances=log_distances)
log_distances=log_distances)
rand1 = Field.from_random('normal', domain=space)
rand2 = Field.from_random('normal', domain=space)
tt1 = rand1.vdot(op.times(rand2))
......@@ -67,7 +67,7 @@ class SmoothingOperator_Tests(unittest.TestCase):
@expand(product(spaces, [0., .5, 5.], [False]))
def test_times(self, space, sigma, log_distances):
op = SmoothingOperator.make(space, sigma=sigma,
log_distances=log_distances)
log_distances=log_distances)
rand1 = Field(space, val=0.)
rand1.val[0] = 1.
tt1 = op.times(rand1)
......@@ -76,7 +76,7 @@ class SmoothingOperator_Tests(unittest.TestCase):
@expand(product(spaces, [0., .5, 5.], [True, False]))
def test_inverse_adjoint_times(self, space, sigma, log_distances):
op = SmoothingOperator.make(space, sigma=sigma,
log_distances=log_distances)
log_distances=log_distances)
rand1 = Field.from_random('normal', domain=space)
rand2 = Field.from_random('normal', domain=space)
tt1 = rand1.vdot(op.inverse_times(rand2))
......@@ -112,8 +112,8 @@ class SmoothingOperator_Tests(unittest.TestCase):
def test_smooth_irregular1(self, sz, log, sigma, tp):
tol = _get_rtol(tp)
sp = RGSpace(sz, harmonic=True)
ps = PowerSpace(sp, binbounds=marco_binbounds(
sp, logarithmic=log, nbin=sz))
ps = PowerSpace(sp, binbounds=PowerSpace.useful_binbounds(
sp, logarithmic=log))
smo = SmoothingOperator.make(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
dtype=tp)
......@@ -126,7 +126,8 @@ class SmoothingOperator_Tests(unittest.TestCase):
def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp):
tol = _get_rtol(tp)
sp = RGSpace([sz1, sz2], harmonic=True)
ps = PowerSpace(sp, binbounds=marco_binbounds(sp, logarithmic=log))
ps = PowerSpace(sp, binbounds=PowerSpace.useful_binbounds(
sp, logarithmic=log))
smo = SmoothingOperator.make(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
dtype=tp)
......
......@@ -25,7 +25,7 @@ from d2o import distributed_data_object
from numpy.testing import assert_, assert_equal, assert_almost_equal,\
assert_raises
from nifty import PowerSpace, RGSpace, Space, LMSpace
from test.common import expand, marco_binbounds
from test.common import expand
from itertools import product, chain
# needed to check wether fftw is available
from nifty import dependency_injector as gdi
......@@ -70,14 +70,14 @@ CONSTRUCTOR_CONFIGS = [
}],
[RGSpace((8,), harmonic=True), 'not', True, None, None, {
'harmonic': True,
'shape': (2,),
'dim': 2,
'shape': (4,),
'dim': 4,
'total_volume': 8.0,
'harmonic_partner': RGSpace((8,), harmonic=True),
'binbounds': (0.70710678118654757,),
'binbounds': (0.5, 1.3228756555322954, 3.5),
'pindex': distributed_data_object([0, 1, 1, 1, 1, 1, 1, 1]),
'kindex': np.array([0., 2.28571429]),
'rho': np.array([1, 7]),
'kindex': np.array([0., 1., 2.5, 4.]),
'rho': np.array([1, 2, 4, 1]),
}],
]
......@@ -131,8 +131,8 @@ class PowerSpaceConsistencyCheck(unittest.TestCase):
raise SkipTest
p = PowerSpace(harmonic_partner=harmonic_partner,
distribution_strategy=distribution_strategy,
binbounds=marco_binbounds(harmonic_partner,
logarithmic, nbin))
binbounds=PowerSpace.useful_binbounds(
harmonic_partner, logarithmic, nbin))
assert_equal(p.pindex.flatten().bincount(), p.rho,
err_msg='rho is not equal to pindex degeneracy')
......@@ -149,13 +149,13 @@ class PowerSpaceFunctionalityTest(unittest.TestCase):
with assert_raises(expected['error']):
PowerSpace(harmonic_partner=harmonic_partner,
distribution_strategy=distribution_strategy,
binbounds=marco_binbounds(harmonic_partner,
logarithmic, nbin))
binbounds=PowerSpace.useful_binbounds(
harmonic_partner, logarithmic, nbin))
else:
p = PowerSpace(harmonic_partner=harmonic_partner,
distribution_strategy=distribution_strategy,
binbounds=marco_binbounds(harmonic_partner,
logarithmic, nbin))
binbounds=PowerSpace.useful_binbounds(
harmonic_partner, logarithmic, nbin))
for key, value in expected.items():
if isinstance(value, np.ndarray):
assert_almost_equal(getattr(p, key), value)
......
Supports Markdown
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