Skip to content

Commit

Permalink
More tests, splitting metadata test file.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitenti committed Jul 11, 2024
1 parent 51c9719 commit 0b115ea
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 169 deletions.
101 changes: 100 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
from firecrown.parameters import ParamsMap
from firecrown.connector.mapping import MappingCosmoSIS, mapping_builder
from firecrown.modeling_tools import ModelingTools
from firecrown.metadata.two_point import TracerNames
from firecrown.metadata.two_point import (
TracerNames,
Galaxies,
InferredGalaxyZDist,
Window,
TwoPointXY,
TwoPointCWindow,
)
import firecrown.likelihood.weak_lensing as wl
import firecrown.likelihood.number_counts as nc

Expand Down Expand Up @@ -153,6 +160,98 @@ def fixture_tools_with_vanilla_cosmology():
return result


@pytest.fixture(
name="harmonic_bin_1",
params=[Galaxies.COUNTS, Galaxies.SHEAR_E],
)
def make_harmonic_bin_1(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 5 bins."""
x = InferredGalaxyZDist(
bin_name="bin_1",
z=np.linspace(0, 1, 5),
dndz=np.array([0.1, 0.5, 0.2, 0.3, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="harmonic_bin_2",
params=[Galaxies.COUNTS, Galaxies.SHEAR_E],
)
def make_harmonic_bin_2(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 3 bins."""
x = InferredGalaxyZDist(
bin_name="bin_2",
z=np.linspace(0, 1, 3),
dndz=np.array([0.1, 0.5, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="real_bin_1",
params=[Galaxies.COUNTS, Galaxies.SHEAR_T],
)
def make_real_bin_1(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 5 bins."""
x = InferredGalaxyZDist(
bin_name="bin_1",
z=np.linspace(0, 1, 5),
dndz=np.array([0.1, 0.5, 0.2, 0.3, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="real_bin_2",
params=[Galaxies.COUNTS, Galaxies.SHEAR_T],
)
def make_real_bin_2(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 3 bins."""
x = InferredGalaxyZDist(
bin_name="bin_2",
z=np.linspace(0, 1, 3),
dndz=np.array([0.1, 0.5, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(name="window_1")
def make_window_1() -> Window:
"""Generate a Window object with 100 ells."""
ells = np.array(np.linspace(0, 100, 100), dtype=np.int64)
ells_for_interpolation = np.array(np.linspace(0, 100, 100), dtype=np.int64)
weights = np.ones(400).reshape(-1, 4)

window = Window(
ells=ells,
weights=weights,
ells_for_interpolation=ells_for_interpolation,
)
return window


@pytest.fixture(name="two_point_cwindow_1")
def make_two_point_cwindow_1(
window_1: Window,
harmonic_bin_1: InferredGalaxyZDist,
harmonic_bin_2: InferredGalaxyZDist,
) -> TwoPointCWindow:
"""Generate a TwoPointCWindow object with 100 ells."""
xy = TwoPointXY(
x=harmonic_bin_1,
y=harmonic_bin_2,
x_measurement=list(harmonic_bin_1.measurements)[0],
y_measurement=list(harmonic_bin_2.measurements)[0],
)
two_point = TwoPointCWindow(XY=xy, window=window_1)
return two_point


@pytest.fixture(name="cluster_sacc_data")
def fixture_cluster_sacc_data() -> sacc.Sacc:
"""Return a Sacc object with cluster data."""
Expand Down
212 changes: 44 additions & 168 deletions tests/metadata/test_metadata_two_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,104 +29,11 @@
type_to_sacc_string_harmonic as harmonic,
type_to_sacc_string_real as real,
Window,
TwoPointMeasurement,
)
from firecrown.likelihood.source import SourceGalaxy
from firecrown.likelihood.two_point import TwoPoint


@pytest.fixture(
name="harmonic_bin_1",
params=[Galaxies.COUNTS, Galaxies.SHEAR_E],
)
def make_harmonic_bin_1(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 5 bins."""
x = InferredGalaxyZDist(
bin_name="bin_1",
z=np.linspace(0, 1, 5),
dndz=np.array([0.1, 0.5, 0.2, 0.3, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="harmonic_bin_2",
params=[Galaxies.COUNTS, Galaxies.SHEAR_E],
)
def make_harmonic_bin_2(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 3 bins."""
x = InferredGalaxyZDist(
bin_name="bin_2",
z=np.linspace(0, 1, 3),
dndz=np.array([0.1, 0.5, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="real_bin_1",
params=[Galaxies.COUNTS, Galaxies.SHEAR_T],
)
def make_real_bin_1(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 5 bins."""
x = InferredGalaxyZDist(
bin_name="bin_1",
z=np.linspace(0, 1, 5),
dndz=np.array([0.1, 0.5, 0.2, 0.3, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(
name="real_bin_2",
params=[Galaxies.COUNTS, Galaxies.SHEAR_T],
)
def make_real_bin_2(request) -> InferredGalaxyZDist:
"""Generate an InferredGalaxyZDist object with 3 bins."""
x = InferredGalaxyZDist(
bin_name="bin_2",
z=np.linspace(0, 1, 3),
dndz=np.array([0.1, 0.5, 0.4]),
measurements={request.param},
)
return x


@pytest.fixture(name="window_1")
def make_window_1() -> Window:
"""Generate a Window object with 100 ells."""
ells = np.array(np.linspace(0, 100, 100), dtype=np.int64)
ells_for_interpolation = np.array(np.linspace(0, 100, 100), dtype=np.int64)
weights = np.ones(400).reshape(-1, 4)

window = Window(
ells=ells,
weights=weights,
ells_for_interpolation=ells_for_interpolation,
)
return window


@pytest.fixture(name="two_point_cwindow_1")
def make_two_point_cwindow_1(
window_1: Window,
harmonic_bin_1: InferredGalaxyZDist,
harmonic_bin_2: InferredGalaxyZDist,
) -> TwoPointCWindow:
"""Generate a TwoPointCWindow object with 100 ells."""
xy = TwoPointXY(
x=harmonic_bin_1,
y=harmonic_bin_2,
x_measurement=list(harmonic_bin_1.measurements)[0],
y_measurement=list(harmonic_bin_2.measurements)[0],
)
two_point = TwoPointCWindow(XY=xy, window=window_1)
return two_point


def test_order_enums():
assert compare_enums(CMB.CONVERGENCE, Clusters.COUNTS) < 0
assert compare_enums(Clusters.COUNTS, CMB.CONVERGENCE) > 0
Expand Down Expand Up @@ -306,6 +213,50 @@ def test_two_point_xy_gal_gal():
assert xy.y_measurement == Galaxies.COUNTS


def test_two_point_xy_gal_gal_invalid_x_measurement():
x = InferredGalaxyZDist(
bin_name="bname1",
z=np.linspace(0, 1, 100),
dndz=np.ones(100),
measurements={Galaxies.SHEAR_E},
)
y = InferredGalaxyZDist(
bin_name="bname2",
z=np.linspace(0, 1, 100),
dndz=np.ones(100),
measurements={Galaxies.COUNTS},
)
with pytest.raises(
ValueError,
match="Measurement Galaxies.COUNTS not in the measurements of bname1.",
):
TwoPointXY(
x=x, y=y, x_measurement=Galaxies.COUNTS, y_measurement=Galaxies.COUNTS
)


def test_two_point_xy_gal_gal_invalid_y_measurement():
x = InferredGalaxyZDist(
bin_name="bname1",
z=np.linspace(0, 1, 100),
dndz=np.ones(100),
measurements={Galaxies.COUNTS},
)
y = InferredGalaxyZDist(
bin_name="bname2",
z=np.linspace(0, 1, 100),
dndz=np.ones(100),
measurements={Galaxies.SHEAR_E},
)
with pytest.raises(
ValueError,
match="Measurement Galaxies.COUNTS not in the measurements of bname2.",
):
TwoPointXY(
x=x, y=y, x_measurement=Galaxies.COUNTS, y_measurement=Galaxies.COUNTS
)


def test_two_point_xy_cmb_gal():
x = InferredGalaxyZDist(
bin_name="bname1",
Expand Down Expand Up @@ -907,78 +858,3 @@ def test_two_point_from_metadata_cells_unsupported_type(wl_factory, nc_factory):
match="Measurement .* not supported!",
):
TwoPoint.from_metadata_harmonic([cells], wl_factory, nc_factory)


def test_two_point_measurement():

data = np.array([1, 2, 3, 4, 5])
indices = np.array([1, 2, 3, 4, 5])
covariance_name = "cov"
measure = TwoPointMeasurement(
data=data, indices=indices, covariance_name=covariance_name
)
assert_array_equal(measure.data, data)
assert_array_equal(measure.indices, indices)
assert measure.covariance_name == covariance_name


def test_two_point_measurement_invalid_data():
data = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
indices = np.array([1, 2, 3, 4, 5])
covariance_name = "cov"
with pytest.raises(
ValueError,
match="Data should be a 1D array.",
):
TwoPointMeasurement(data=data, indices=indices, covariance_name=covariance_name)


def test_two_point_measurement_invalid_indices():
data = np.array([1, 2, 3, 4, 5])
indices = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
covariance_name = "cov"
with pytest.raises(
ValueError,
match="Data and indices should have the same shape.",
):
TwoPointMeasurement(data=data, indices=indices, covariance_name=covariance_name)


def test_two_point_measurement_eq():
data = np.array([1, 2, 3, 4, 5])
indices = np.array([1, 2, 3, 4, 5])
covariance_name = "cov"
measure_1 = TwoPointMeasurement(
data=data, indices=indices, covariance_name=covariance_name
)
measure_2 = TwoPointMeasurement(
data=data, indices=indices, covariance_name=covariance_name
)
assert measure_1 == measure_2


def test_two_point_measurement_neq():
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
indices = np.array([1, 2, 3, 4, 5])
covariance_name = "cov"
measure_1 = TwoPointMeasurement(
data=data,
indices=indices,
covariance_name=covariance_name,
)
measure_2 = TwoPointMeasurement(data=data, indices=indices, covariance_name="cov2")
assert measure_1 != measure_2
measure_3 = TwoPointMeasurement(
data=data, indices=indices, covariance_name=covariance_name
)
measure_4 = TwoPointMeasurement(
data=data, indices=indices + 1, covariance_name=covariance_name
)
assert measure_3 != measure_4
measure_5 = TwoPointMeasurement(
data=data, indices=indices, covariance_name=covariance_name
)
measure_6 = TwoPointMeasurement(
data=data + 1.0, indices=indices, covariance_name=covariance_name
)
assert measure_5 != measure_6
Loading

0 comments on commit 0b115ea

Please sign in to comment.