Skip to content

Commit

Permalink
Give useful error message when trying to pass decreasing a array to C…
Browse files Browse the repository at this point in the history
…alculatorCosmology (#864)

* Give useful error message when trying to pass decreasing a array.

* Check Pk2D too.
  • Loading branch information
tilmantroester committed Mar 18, 2021
1 parent 438bcdf commit b2a6e7c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pyccl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,11 @@ def _init_pklin(self, pk_linear):
"and 'delta_matter:delta_matter' "
"(at least)")

# Check that `a` is a monotonically increasing array.
if not np.array_equal(pk_linear['a'], np.sort(pk_linear['a'])):
raise ValueError("Input scale factor array in `pk_linear` is not "
"monotonically increasing.")

# needed for high-z extrapolation
self.compute_growth()

Expand Down Expand Up @@ -1314,6 +1319,11 @@ def _init_pknl(self, pk_nonlin, has_nonlin_model):
if (('a' not in pk_nonlin) or ('k' not in pk_nonlin)):
raise ValueError("`pk_nonlin` must contain keys "
"'a' and 'k' (at least)")
# Check that `a` is a monotonically increasing array.
if not np.array_equal(pk_nonlin['a'], np.sort(pk_nonlin['a'])):
raise ValueError("Input scale factor array in `pk_nonlin` is not "
"monotonically increasing.")

if ((not has_nonlin_model) and
('delta_matter:delta_matter' not in pk_nonlin)):
raise ValueError("`pk_nonlin` must contain key "
Expand Down
5 changes: 5 additions & 0 deletions pyccl/pk2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def __init__(self, pkfunc=None, a_arr=None, lk_arr=None, pk_arr=None,
raise ValueError("If you do not provide a function, "
"you must provide arrays")

# Check that `a` is a monotonically increasing array.
if not np.array_equal(a_arr, np.sort(a_arr)):
raise ValueError("Input scale factor array in `a_arr` is not "
"monotonically increasing.")

pkflat = pk_arr.flatten()
# Check dimensions make sense
if (len(a_arr)*len(lk_arr) != len(pkflat)):
Expand Down
4 changes: 4 additions & 0 deletions pyccl/tests/test_pk2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def test_pk2d_init():
assert_raises(
ValueError, ccl.Pk2D, a_arr=aarr, lk_arr=lkarr, pk_arr=pkarr[1:])

# Scale factor is not monotonically increasing
assert_raises(
ValueError, ccl.Pk2D, a_arr=aarr[::-1], lk_arr=lkarr, pk_arr=pkarr)


def test_pk2d_smoke():
"""Make sure it works once."""
Expand Down
16 changes: 16 additions & 0 deletions pyccl/tests/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ def test_input_linpower_raises():
n_s=0.965, sigma8=0.8,
pk_linear=np.pi)

# a not increasing
with pytest.raises(ValueError):
ccl.CosmologyCalculator(
Omega_c=0.27, Omega_b=0.05, h=0.7,
n_s=0.965, sigma8=0.8,
pk_linear={'a': a_arr[::-1], 'k': k_arr,
'delta_matter:delta_matter': pk_arr})

# Dm x Dm not present
with pytest.raises(ValueError):
ccl.CosmologyCalculator(
Expand Down Expand Up @@ -527,6 +535,14 @@ def test_input_nonlin_raises():
pk_nonlin={'a': a_arr, 'kk': k_arr,
'delta_matter;delta_matter': pk_arr})

# a not increasing
with pytest.raises(ValueError):
ccl.CosmologyCalculator(
Omega_c=0.27, Omega_b=0.05, h=0.7,
n_s=0.965, sigma8=0.8,
pk_nonlin={'a': a_arr[::-1], 'k': k_arr,
'delta_matter:delta_matter': pk_arr})

# delta_matter:delta_matter not present
with pytest.raises(ValueError):
ccl.CosmologyCalculator(
Expand Down

0 comments on commit b2a6e7c

Please sign in to comment.