Skip to content

Commit

Permalink
Create test and fix for issue 427 (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpaterno authored Jun 21, 2024
1 parent a0ee415 commit 21b67d6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion firecrown/likelihood/two_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ def compute_theory_vector_real_space(self, tools: ModelingTools) -> TheoryVector
assert self.thetas is not None
assert self.ells_for_xi is not None

self.cells = {}
print(self.source0.parameter_prefix, self.source1.parameter_prefix)
cells_for_xi = self.compute_cells(
self.ells_for_xi, scale0, scale1, tools, tracers0, tracers1
Expand Down Expand Up @@ -724,6 +723,7 @@ def compute_cells(
tracers1: Sequence[Tracer],
) -> npt.NDArray[np.float64]:
"""Compute the power spectrum for the given ells and tracers."""
self.cells = {}
for tracer0 in tracers0:
for tracer1 in tracers1:
pk_name = f"{tracer0.field}:{tracer1.field}"
Expand Down
22 changes: 21 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def fixture_sacc_galaxy_xis_lens0_lens0_no_data():

@pytest.fixture(name="sacc_galaxy_cells_src0_src0_window")
def fixture_sacc_galaxy_cells_src0_src0_window():
"""Fixture for a SACC data without window functions."""
"""Fixture for a SACC data with a window function."""
sacc_data = sacc.Sacc()

z = np.linspace(0, 1.0, 256) + 0.05
Expand All @@ -534,3 +534,23 @@ def fixture_sacc_galaxy_cells_src0_src0_window():
sacc_data.add_covariance(cov)

return sacc_data, z, dndz


@pytest.fixture(name="sacc_galaxy_cells_src0_src0_no_window")
def fixture_sacc_galaxy_cells_src0_src0_no_window():
"""Fixture for a SACC data without a window function."""
sacc_data = sacc.Sacc()

z = np.linspace(0, 1.0, 256) + 0.05
ells = np.unique(np.logspace(1, 3, 10).astype(np.int64))

dndz = np.exp(-0.5 * (z - 0.5) ** 2 / 0.05 / 0.05)
sacc_data.add_tracer("NZ", "src0", z, dndz)

Cells = np.random.normal(size=ells.shape[0])
sacc_data.add_ell_cl("galaxy_shear_cl_ee", "src0", "src0", ells, Cells)

cov = np.diag(np.ones_like(Cells) * 0.01)
sacc_data.add_covariance(cov)

return sacc_data, z, dndz
42 changes: 41 additions & 1 deletion tests/likelihood/gauss_family/statistic/test_two_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def test_tracer_names():


def test_two_point_src0_src0_window(sacc_galaxy_cells_src0_src0_window):
"""This test also makes sure that TwoPoint theory calculations are
repeatable."""
sacc_data, _, _ = sacc_galaxy_cells_src0_src0_window

src0 = WeakLensing(sacc_tracer="src0")
Expand All @@ -93,8 +95,46 @@ def test_two_point_src0_src0_window(sacc_galaxy_cells_src0_src0_window):
assert statistic.window.ells_for_interpolation is not None
assert all(np.isfinite(statistic.window.ells_for_interpolation))

statistic.reset()
statistic.update(ParamsMap())
statistic.compute_theory_vector(tools)
tools.update(ParamsMap())
result1 = statistic.compute_theory_vector(tools)
assert all(np.isfinite(result1))

statistic.reset()
statistic.update(ParamsMap())
tools.update(ParamsMap())
result2 = statistic.compute_theory_vector(tools)
assert np.array_equal(result1, result2)


def test_two_point_src0_src0_no_window(sacc_galaxy_cells_src0_src0_no_window):
"""This test also makes sure that TwoPoint theory calculations are
repeatable."""
sacc_data, _, _ = sacc_galaxy_cells_src0_src0_no_window

src0 = WeakLensing(sacc_tracer="src0")

statistic = TwoPoint("galaxy_shear_cl_ee", src0, src0)
statistic.read(sacc_data)

tools = ModelingTools()
tools.update(ParamsMap())
tools.prepare(pyccl.CosmologyVanillaLCDM())

assert statistic.window is None

statistic.reset()
statistic.update(ParamsMap())
tools.update(ParamsMap())
result1 = statistic.compute_theory_vector(tools)
assert all(np.isfinite(result1))

statistic.reset()
statistic.update(ParamsMap())
tools.update(ParamsMap())
result2 = statistic.compute_theory_vector(tools)
assert np.array_equal(result1, result2)


def test_two_point_src0_src0_no_data_lin(sacc_galaxy_cells_src0_src0_no_data):
Expand Down

0 comments on commit 21b67d6

Please sign in to comment.