diff --git a/firecrown/parameters.py b/firecrown/parameters.py index b5e2d289..58ea0f30 100644 --- a/firecrown/parameters.py +++ b/firecrown/parameters.py @@ -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. diff --git a/tests/likelihood/gauss_family/statistic/source/test_source.py b/tests/likelihood/gauss_family/statistic/source/test_source.py index df70d613..d7e736e8 100644 --- a/tests/likelihood/gauss_family/statistic/source/test_source.py +++ b/tests/likelihood/gauss_family/statistic/source/test_source.py @@ -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): @@ -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 diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 5982bdd5..b7520a3b 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -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) + ) diff --git a/tests/test_pt_systematics.py b/tests/test_pt_systematics.py index a9e0655d..159a0a95 100644 --- a/tests/test_pt_systematics.py +++ b/tests/test_pt_systematics.py @@ -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 @@ -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") @@ -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