Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for nc.LinearBiasSystematic #467

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firecrown/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ def __init__(self, derived_parameters: Sequence[DerivedParameter]):
for derived_parameter in derived_parameters:
self.add_required_parameter(derived_parameter)

def __len__(self) -> int:
"""Return the number of parameters contained."""
return len(self.derived_parameters)

def __add__(self, other: None | DerivedParameterCollection):
"""Add two DerivedParameterCollection objects.
Expand Down
6 changes: 6 additions & 0 deletions tests/likelihood/gauss_family/statistic/source/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def test_weak_lensing_source_init(
source.update(ParamsMap())
ts = source.get_tracers(tools_with_vanilla_cosmology)
assert len(ts) == 1
dp = source.get_derived_parameters()
assert dp is not None
assert len(dp) == 0


def test_weak_lensing_source_create_ready(sacc_galaxy_cells_src0_src0):
Expand Down Expand Up @@ -263,6 +266,9 @@ def test_number_counts_source_init(
assert_allclose(source.tracer_args.z, z)
assert_allclose(source.tracer_args.dndz, dndz)
source.update(ParamsMap(lens0_bias=1.1))
dp = source.get_derived_parameters()
assert dp is not None
assert len(dp) == 0
ts = source.get_tracers(tools_with_vanilla_cosmology)
assert len(ts) == 1

Expand Down
16 changes: 16 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,19 @@ def test_required_parameters_get_default_values_no_default3():
)
with pytest.raises(ValueError, match="Parameter name3 has no default value"):
_ = rp.get_default_values()


def test_add_required_parameter():
coll = DerivedParameterCollection([])
coll.add_required_parameter(
DerivedParameter(section="barnyard", name="cow", val=1.0)
)
assert len(coll) == 1

with pytest.raises(
ValueError,
match="RequiredParameter named barnyard--cow is already present",
):
coll.add_required_parameter(
DerivedParameter(section="barnyard", name="cow", val=3.14)
)
42 changes: 42 additions & 0 deletions tests/test_pt_systematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import numpy as np
import numpy.typing as npt
import pyccl as ccl
import pyccl.nl_pt as pt
import sacc
Expand All @@ -23,6 +24,7 @@
from firecrown.likelihood.gaussian import ConstGaussian
from firecrown.modeling_tools import ModelingTools
from firecrown.ccl_factory import CCLFactory, PoweSpecAmplitudeParameter
import firecrown.parameters as fcp


@pytest.fixture(name="weak_lensing_source")
Expand Down Expand Up @@ -368,3 +370,43 @@ def test_pt_mixed_systematics(sacc_data):

assert np.allclose(cl_gG, cells_gG, atol=0, rtol=1e-7)
assert np.allclose(cl_gI, cells_gI, atol=0, rtol=1e-7)


def test_linear_bias_systematic(tools_with_vanilla_cosmology: ModelingTools):
a = nc.LinearBiasSystematic("xxx")
assert isinstance(a, nc.LinearBiasSystematic)
assert a.parameter_prefix == "xxx"
assert a.alphag is None
assert a.alphaz is None
assert a.z_piv is None
assert not a.is_updated()
a.update(fcp.ParamsMap({"xxx_alphag": 1.0, "xxx_alphaz": 2.0, "xxx_z_piv": 1.5}))
assert a.is_updated()
assert a.alphag == 1.0
assert a.alphaz == 2.0
assert a.z_piv == 1.5

orig_nca = nc.NumberCountsArgs(
z=np.array([0.5, 1.0]),
dndz=np.array([5.0, 4.0]),
bias=np.array([1.0, 1.0]),
mag_bias=(np.array([2.0, 3.0]), np.array([4.0, 5.0])),
has_pt=False,
has_hm=False,
b_2=(np.array([5.0, 6.0]), np.array([6.0, 7.0])),
b_s=(np.array([7.0, 8.0]), np.array([8.0, 9.0])),
)

nca = a.apply(tools_with_vanilla_cosmology, orig_nca)
# Answer values determined by code inspection and hand calculation.
expected_bias: npt.NDArray[np.float64] = np.array([0.27835299, 0.39158961])
assert nca.bias is not None # needed for mypy
new_bias: npt.NDArray[np.float64] = nca.bias # needed for mypy
assert np.allclose(expected_bias, new_bias)

a.reset()
assert not a.is_updated()
assert a.parameter_prefix == "xxx"
assert a.alphag is None
assert a.alphaz is None
assert a.z_piv is None
Loading