From c4d70b527e946fc542dd7df51fee7521c2a5dc58 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 15:59:48 +0000 Subject: [PATCH 01/38] Import all functions --- tests/test_shells.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index 33259bcf..e399ac2f 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -3,9 +3,22 @@ from glass import ( RadialWindow, + combine, # noqa: F401 + cubic_windows, # noqa: F401 + density_weight, # noqa: F401 + distance_grid, # noqa: F401 + distance_weight, # noqa: F401 + linear_windows, # noqa: F401 partition, + redshift_grid, # noqa: F401 restrict, tophat_windows, + volume_weight, # noqa: F401 +) +from glass.shells import ( # noqa: F401 + partition_lstsq, + partition_nnls, + partition_restrict, ) From 43cfd668133b47917b4ed04fd04ba2cef2f4f9b6 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 16:03:05 +0000 Subject: [PATCH 02/38] Add to current docstringa --- tests/test_shells.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index e399ac2f..f0e07ee5 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -23,6 +23,7 @@ def test_tophat_windows() -> None: + """Add unit tests for :func:`tophat_windows`.""" zb = np.array([0.0, 0.1, 0.2, 0.5, 1.0, 2.0]) dz = 0.005 @@ -44,6 +45,7 @@ def test_tophat_windows() -> None: def test_restrict() -> None: + """Add unit tests for :func:`restrict`.""" # Gaussian test function z = np.linspace(0.0, 5.0, 1000) f = np.exp(-(((z - 2.0) / 0.5) ** 2) / 2) @@ -75,6 +77,7 @@ def test_restrict() -> None: @pytest.mark.parametrize("method", ["lstsq", "nnls", "restrict"]) def test_partition(method: str) -> None: + """Add unit tests for :func:`partition`.""" shells = [ RadialWindow(np.array([0.0, 1.0]), np.array([1.0, 0.0]), 0.0), RadialWindow(np.array([0.0, 1.0, 2.0]), np.array([0.0, 1.0, 0.0]), 0.5), From 3fc27820912f66228d133ab96c1e261e072db73b Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 16:09:52 +0000 Subject: [PATCH 03/38] Add placeholders for all tests --- tests/test_shells.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index f0e07ee5..0384a131 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -22,6 +22,18 @@ ) +def test_distance_weight() -> None: + """Add unit tests for :func:`distance_weight`.""" + + +def test_volume_weight() -> None: + """Add unit tests for :func:`volume_weight`.""" + + +def test_density_weight() -> None: + """Add unit tests for :func:`density_weight`.""" + + def test_tophat_windows() -> None: """Add unit tests for :func:`tophat_windows`.""" zb = np.array([0.0, 0.1, 0.2, 0.5, 1.0, 2.0]) @@ -44,6 +56,14 @@ def test_tophat_windows() -> None: assert all(np.all(w.wa == 1) for w in ws) +def test_linear_windows() -> None: + """Add unit tests for :func:`linear_windows`.""" + + +def test_cubic_windows() -> None: + """Add unit tests for :func:`cubic_windows`.""" + + def test_restrict() -> None: """Add unit tests for :func:`restrict`.""" # Gaussian test function @@ -98,3 +118,27 @@ def test_partition(method: str) -> None: assert part.shape == (len(shells), 3, 2) np.testing.assert_allclose(part.sum(axis=0), np.trapezoid(fz, z)) + + +def test_partition_lstsq() -> None: + """Add unit tests for :func:`partition_lstsq`.""" + + +def test_partition_nnls() -> None: + """Add unit tests for :func:`partition_nnls`.""" + + +def test_partition_restrict() -> None: + """Add unit tests for :func:`partition_restrict`.""" + + +def test_redshift_grid() -> None: + """Add unit tests for :func:`redshift_grid`.""" + + +def test_distance_grid() -> None: + """Add unit tests for :func:`distance_grid`.""" + + +def test_combine() -> None: + """Add unit tests for :func:`combine`.""" From 596f8d4ce301538c21e07cf9d3caf80698c94777 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 16:19:51 +0000 Subject: [PATCH 04/38] Add failing test --- glass/shells.py | 2 +- tests/test_shells.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/glass/shells.py b/glass/shells.py index 23925c23..01166e5e 100644 --- a/glass/shells.py +++ b/glass/shells.py @@ -744,7 +744,7 @@ def redshift_grid( elif dz is None and num is not None: z = np.linspace(zmin, zmax, num + 1) else: - msg = 'exactly one of "dz" or "num" must be given' + msg = "exactly one of 'dz' or 'num' must be given" raise ValueError(msg) return z diff --git a/tests/test_shells.py b/tests/test_shells.py index 0384a131..138f1b7c 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -10,7 +10,7 @@ distance_weight, # noqa: F401 linear_windows, # noqa: F401 partition, - redshift_grid, # noqa: F401 + redshift_grid, restrict, tophat_windows, volume_weight, # noqa: F401 @@ -134,6 +134,13 @@ def test_partition_restrict() -> None: def test_redshift_grid() -> None: """Add unit tests for :func:`redshift_grid`.""" + zmin = 0 + zmax = 1 + + # check error raised + + with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + redshift_grid(zmin, zmax, dz=0.2, num=5) def test_distance_grid() -> None: From 61dc081e63f10d12640bdc78764264cbd6972a73 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 16:28:47 +0000 Subject: [PATCH 05/38] gh-450: move all `pytest.fixture` into `conftest.py` This makes them available to all tests. I need `cosmo` for use in #449. Have also added `scope="session"` so they are only computed once when testing rather than multiple times. --- tests/conftest.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_lensing.py | 34 ---------------------------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 43489682..ab4ae201 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,45 @@ import numpy as np +import numpy.typing as npt import pytest +from cosmology import Cosmology + +from glass import RadialWindow + + +@pytest.fixture(scope="session") +def cosmo() -> Cosmology: + class MockCosmology: + @property + def omega_m(self) -> float: + return 0.3 + + def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5 + + def xm( + self, + z: npt.NDArray[np.float64], + z2: npt.NDArray[np.float64] | None = None, + ) -> npt.NDArray[np.float64]: + if z2 is None: + return np.array(z) * 1000 + return (np.array(z2) - np.array(z)) * 1000 + + return MockCosmology() + @pytest.fixture(scope="session") def rng() -> np.random.Generator: return np.random.default_rng(seed=42) + + +@pytest.fixture(scope="session") +def shells() -> list[RadialWindow]: + return [ + RadialWindow(np.array([0.0, 1.0, 2.0]), np.array([0.0, 1.0, 0.0]), 1.0), + RadialWindow(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.0]), 2.0), + RadialWindow(np.array([2.0, 3.0, 4.0]), np.array([0.0, 1.0, 0.0]), 3.0), + RadialWindow(np.array([3.0, 4.0, 5.0]), np.array([0.0, 1.0, 0.0]), 4.0), + RadialWindow(np.array([4.0, 5.0, 6.0]), np.array([0.0, 1.0, 0.0]), 5.0), + ] diff --git a/tests/test_lensing.py b/tests/test_lensing.py index d0c2fb72..cdb343f5 100644 --- a/tests/test_lensing.py +++ b/tests/test_lensing.py @@ -4,7 +4,6 @@ import healpix import numpy as np -import numpy.typing as npt import pytest from glass import ( @@ -19,39 +18,6 @@ from cosmology import Cosmology -@pytest.fixture -def shells() -> list[RadialWindow]: - return [ - RadialWindow(np.array([0.0, 1.0, 2.0]), np.array([0.0, 1.0, 0.0]), 1.0), - RadialWindow(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.0]), 2.0), - RadialWindow(np.array([2.0, 3.0, 4.0]), np.array([0.0, 1.0, 0.0]), 3.0), - RadialWindow(np.array([3.0, 4.0, 5.0]), np.array([0.0, 1.0, 0.0]), 4.0), - RadialWindow(np.array([4.0, 5.0, 6.0]), np.array([0.0, 1.0, 0.0]), 5.0), - ] - - -@pytest.fixture -def cosmo() -> Cosmology: - class MockCosmology: - @property - def omega_m(self) -> float: - return 0.3 - - def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: - return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5 - - def xm( - self, - z: npt.NDArray[np.float64], - z2: npt.NDArray[np.float64] | None = None, - ) -> npt.NDArray[np.float64]: - if z2 is None: - return np.array(z) * 1000 - return (np.array(z2) - np.array(z)) * 1000 - - return MockCosmology() - - @pytest.mark.parametrize("usecomplex", [True, False]) def test_deflect_nsew(usecomplex: bool) -> None: # noqa: FBT001 d = 5.0 From 6552260717db198f9a74aca931627d0564d3a2a9 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 10:49:00 +0000 Subject: [PATCH 06/38] Test `distance_weight` --- tests/test_shells.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 138f1b7c..00267c2e 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -1,13 +1,15 @@ import numpy as np import pytest +from cosmology import Cosmology + from glass import ( RadialWindow, combine, # noqa: F401 cubic_windows, # noqa: F401 density_weight, # noqa: F401 distance_grid, # noqa: F401 - distance_weight, # noqa: F401 + distance_weight, linear_windows, # noqa: F401 partition, redshift_grid, @@ -22,8 +24,22 @@ ) -def test_distance_weight() -> None: +def test_distance_weight(cosmo: Cosmology) -> None: """Add unit tests for :func:`distance_weight`.""" + z = np.linspace(0, 1, 6) + + # check shape + + w = distance_weight(z, cosmo) + np.testing.assert_array_equal(w.shape, z.shape) + + # check first value is 1 + + np.testing.assert_array_equal(w[0], 1) + + # check values are decreasing + + np.testing.assert_array_less(w[1:], w[:-1]) def test_volume_weight() -> None: From c4b810712b5b3536e0f02349c0d5e415c5f75086 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 10:53:14 +0000 Subject: [PATCH 07/38] Test `volume_weight` --- tests/test_shells.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 00267c2e..ebe2e803 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -15,7 +15,7 @@ redshift_grid, restrict, tophat_windows, - volume_weight, # noqa: F401 + volume_weight, ) from glass.shells import ( # noqa: F401 partition_lstsq, @@ -42,8 +42,22 @@ def test_distance_weight(cosmo: Cosmology) -> None: np.testing.assert_array_less(w[1:], w[:-1]) -def test_volume_weight() -> None: +def test_volume_weight(cosmo: Cosmology) -> None: """Add unit tests for :func:`volume_weight`.""" + z = np.linspace(0, 1, 6) + + # check shape + + w = volume_weight(z, cosmo) + np.testing.assert_array_equal(w.shape, z.shape) + + # check first value is 0 + + np.testing.assert_array_equal(w[0], 0) + + # check values are increasing + + np.testing.assert_array_less(w[:-1], w[1:]) def test_density_weight() -> None: From 9a80d85aced0a18cd7d92ca634c49f1f8bea5702 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 11:17:33 +0000 Subject: [PATCH 08/38] Add warning for mock --- tests/test_shells.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index ebe2e803..242f49fc 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -60,8 +60,9 @@ def test_volume_weight(cosmo: Cosmology) -> None: np.testing.assert_array_less(w[:-1], w[1:]) -def test_density_weight() -> None: +def test_density_weight(cosmo: Cosmology) -> None: """Add unit tests for :func:`density_weight`.""" + # AttributeError: 'MockCosmology' object has no attribute 'rho_m_z' def test_tophat_windows() -> None: From 371300b3d38e8e95c39798f06855acbd7e886533 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 11:25:53 +0000 Subject: [PATCH 09/38] Test `redshift_grid` --- tests/test_shells.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 242f49fc..ea2a9eb7 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -168,10 +168,27 @@ def test_redshift_grid() -> None: zmin = 0 zmax = 1 + # check num input + + num = 5 + z = redshift_grid(zmin, zmax, num=5) + np.testing.assert_array_equal(len(z), num + 1) + + # check dz input + + dz = 0.2 + z = redshift_grid(zmin, zmax, dz=dz) + np.testing.assert_array_equal(len(z), np.ceil((zmax - zmin) / dz) + 1) + + # check dz for spacing which results in a max value above zmax + + z = redshift_grid(zmin, zmax, dz=0.3) + np.testing.assert_array_less(zmax, z[-1]) + # check error raised with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): - redshift_grid(zmin, zmax, dz=0.2, num=5) + redshift_grid(zmin, zmax, dz=dz, num=num) def test_distance_grid() -> None: From 2863c9a0043dec9b64fb4a2a5c91994da86ffbfd Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 14:13:53 +0000 Subject: [PATCH 10/38] Trigger error --- tests/test_shells.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index ea2a9eb7..5851414c 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -10,7 +10,7 @@ density_weight, # noqa: F401 distance_grid, # noqa: F401 distance_weight, - linear_windows, # noqa: F401 + linear_windows, partition, redshift_grid, restrict, @@ -89,6 +89,8 @@ def test_tophat_windows() -> None: def test_linear_windows() -> None: """Add unit tests for :func:`linear_windows`.""" + with pytest.raises(ValueError, match="nodes must have at least 3 entries"): + linear_windows([]) def test_cubic_windows() -> None: From 2f3a57e04ff78e1d7a0538d6edda07558d7bbdd6 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 14:27:50 +0000 Subject: [PATCH 11/38] Test for warning --- tests/test_shells.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index 5851414c..9b84684f 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -1,5 +1,6 @@ import numpy as np import pytest +import warnings from cosmology import Cosmology @@ -89,9 +90,18 @@ def test_tophat_windows() -> None: def test_linear_windows() -> None: """Add unit tests for :func:`linear_windows`.""" + # check error raised + with pytest.raises(ValueError, match="nodes must have at least 3 entries"): linear_windows([]) + # check warning issued + + with pytest.warns( + UserWarning, match="first triangular window does not start at z=0" + ): + linear_windows([0.1, 0.2, 0.3]) + def test_cubic_windows() -> None: """Add unit tests for :func:`cubic_windows`.""" From b7bf08ac9c2ce6919448dd8feab2d229601e704a Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 14:40:17 +0000 Subject: [PATCH 12/38] Check spacing of grid --- tests/test_shells.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 9b84684f..b8e5691e 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -1,6 +1,5 @@ import numpy as np import pytest -import warnings from cosmology import Cosmology @@ -90,6 +89,20 @@ def test_tophat_windows() -> None: def test_linear_windows() -> None: """Add unit tests for :func:`linear_windows`.""" + dz = 1e-2 + zgrid = [ + 0.0, + 0.20224358, + 0.42896272, + 0.69026819, + 1.0, + ] + + # check spacing of redshift grid + + ws = linear_windows(zgrid) + np.testing.assert_allclose(dz, np.diff(ws[0].za).mean(), atol=1e-2) + # check error raised with pytest.raises(ValueError, match="nodes must have at least 3 entries"): From 0826305e073415c4cd0a5d18bd75a85593a4c5f3 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:14:21 +0000 Subject: [PATCH 13/38] Check shapes --- tests/test_shells.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index b8e5691e..f78e0317 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -103,6 +103,10 @@ def test_linear_windows() -> None: ws = linear_windows(zgrid) np.testing.assert_allclose(dz, np.diff(ws[0].za).mean(), atol=1e-2) + # check number of windows + + np.testing.assert_array_equal(len(ws), len(zgrid) - 2) + # check error raised with pytest.raises(ValueError, match="nodes must have at least 3 entries"): From 5be24bdfa401bd198db76c44472c449b4bf8be0d Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:19:07 +0000 Subject: [PATCH 14/38] Check `zeff` --- tests/test_shells.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index f78e0317..86481502 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -107,6 +107,10 @@ def test_linear_windows() -> None: np.testing.assert_array_equal(len(ws), len(zgrid) - 2) + # check values of zeff + + np.testing.assert_array_equal([w.zeff for w in ws], zgrid[1:-1]) + # check error raised with pytest.raises(ValueError, match="nodes must have at least 3 entries"): From 9402362f1909dda7ddd537591f13a91b4bb86b75 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:33:53 +0000 Subject: [PATCH 15/38] Pass in weight function --- tests/test_shells.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_shells.py b/tests/test_shells.py index 86481502..f9628cfa 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -111,6 +111,15 @@ def test_linear_windows() -> None: np.testing.assert_array_equal([w.zeff for w in ws], zgrid[1:-1]) + # check weight function + + ws = linear_windows( + zgrid, + weight=lambda _: 0, # type: ignore[arg-type, return-value] + ) + for w in ws: + np.testing.assert_array_equal(w.wa, np.zeros_like(w.wa)) + # check error raised with pytest.raises(ValueError, match="nodes must have at least 3 entries"): From 21aedbecced9696484fa6311650ad3010fbd98ca Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:34:54 +0000 Subject: [PATCH 16/38] Fix linting --- tests/test_shells.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index f9628cfa..34db4e71 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -111,11 +111,11 @@ def test_linear_windows() -> None: np.testing.assert_array_equal([w.zeff for w in ws], zgrid[1:-1]) - # check weight function + # check weight function input ws = linear_windows( zgrid, - weight=lambda _: 0, # type: ignore[arg-type, return-value] + weight=lambda _: 0, # type: ignore[arg-type, return-value] ) for w in ws: np.testing.assert_array_equal(w.wa, np.zeros_like(w.wa)) From 85c4b8faa5af877846472f7fe5e66fcab4a424f9 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:39:58 +0000 Subject: [PATCH 17/38] Test cubic window --- tests/test_shells.py | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 34db4e71..5450cfac 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -6,7 +6,7 @@ from glass import ( RadialWindow, combine, # noqa: F401 - cubic_windows, # noqa: F401 + cubic_windows, density_weight, # noqa: F401 distance_grid, # noqa: F401 distance_weight, @@ -115,7 +115,7 @@ def test_linear_windows() -> None: ws = linear_windows( zgrid, - weight=lambda _: 0, # type: ignore[arg-type, return-value] + weight=lambda _: 0, # type: ignore[arg-type, return-value] ) for w in ws: np.testing.assert_array_equal(w.wa, np.zeros_like(w.wa)) @@ -135,6 +135,48 @@ def test_linear_windows() -> None: def test_cubic_windows() -> None: """Add unit tests for :func:`cubic_windows`.""" + dz = 1e-2 + zgrid = [ + 0.0, + 0.20224358, + 0.42896272, + 0.69026819, + 1.0, + ] + + # check spacing of redshift grid + + ws = cubic_windows(zgrid) + np.testing.assert_allclose(dz, np.diff(ws[0].za).mean(), atol=1e-2) + + # check number of windows + + np.testing.assert_array_equal(len(ws), len(zgrid) - 2) + + # check values of zeff + + np.testing.assert_array_equal([w.zeff for w in ws], zgrid[1:-1]) + + # check weight function input + + ws = cubic_windows( + zgrid, + weight=lambda _: 0, # type: ignore[arg-type, return-value] + ) + for w in ws: + np.testing.assert_array_equal(w.wa, np.zeros_like(w.wa)) + + # check error raised + + with pytest.raises(ValueError, match="nodes must have at least 3 entries"): + cubic_windows([]) + + # check warning issued + + with pytest.warns( + UserWarning, match="first cubic spline window does not start at z=0" + ): + cubic_windows([0.1, 0.2, 0.3]) def test_restrict() -> None: From f810c4da59b3e71345330c9f45333009d1e35c88 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 15:48:19 +0000 Subject: [PATCH 18/38] Add attribute error --- tests/test_shells.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 5450cfac..8e6b7e34 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -275,8 +275,9 @@ def test_redshift_grid() -> None: redshift_grid(zmin, zmax, dz=dz, num=num) -def test_distance_grid() -> None: +def test_distance_grid(cosmo: Cosmology) -> None: """Add unit tests for :func:`distance_grid`.""" + # AttributeError: 'MockCosmology' object has no attribute 'dc_inv' def test_combine() -> None: From 019caf1c7add1229680af2e1a6f1e466704468a6 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 22 Nov 2024 17:39:27 +0000 Subject: [PATCH 19/38] Remove other partition tests --- tests/test_shells.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 8e6b7e34..cd1723ba 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -17,11 +17,6 @@ tophat_windows, volume_weight, ) -from glass.shells import ( # noqa: F401 - partition_lstsq, - partition_nnls, - partition_restrict, -) def test_distance_weight(cosmo: Cosmology) -> None: @@ -235,18 +230,6 @@ def test_partition(method: str) -> None: np.testing.assert_allclose(part.sum(axis=0), np.trapezoid(fz, z)) -def test_partition_lstsq() -> None: - """Add unit tests for :func:`partition_lstsq`.""" - - -def test_partition_nnls() -> None: - """Add unit tests for :func:`partition_nnls`.""" - - -def test_partition_restrict() -> None: - """Add unit tests for :func:`partition_restrict`.""" - - def test_redshift_grid() -> None: """Add unit tests for :func:`redshift_grid`.""" zmin = 0 From beb8e124c54df7df3d9790bdb957fe6a309fd406 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 16:14:56 +0000 Subject: [PATCH 20/38] Improve mock object --- tests/conftest.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index ab4ae201..9c3934cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,9 +12,16 @@ def cosmo() -> Cosmology: class MockCosmology: @property def omega_m(self) -> float: + """Matter density parameter at redshift 0.""" return 0.3 + @property + def rho_c(self) -> float: + """Critical density at redshift 0 in Msol Mpc-3.""" + return 3e4 + def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + """Standardised Hubble function :math:`E(z) = H(z)/H_0`.""" return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5 def xm( @@ -22,10 +29,24 @@ def xm( z: npt.NDArray[np.float64], z2: npt.NDArray[np.float64] | None = None, ) -> npt.NDArray[np.float64]: + """ + Dimensionless transverse comoving distance. + + :math:`x_M(z) = d_M(z)/d_H` + """ if z2 is None: return np.array(z) * 1000 return (np.array(z2) - np.array(z)) * 1000 + def dc_inv(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + """Inverse function for the comoving distance in Mpc.""" + return 1 / (self.xm(z) / 1000) + + def rho_m_z(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + """Redshift-dependent matter density in Msol Mpc-3.""" + rho_crit_0 = 3e4 + return rho_crit_0 * self.omega_m * (1 + z) ** 3 + return MockCosmology() From a6ed9cd27169f25b9c8549ace865c41eb7fc03e5 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 16:21:25 +0000 Subject: [PATCH 21/38] Use `rho_c` --- tests/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9c3934cd..b6425e6d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -44,8 +44,7 @@ def dc_inv(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: def rho_m_z(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: """Redshift-dependent matter density in Msol Mpc-3.""" - rho_crit_0 = 3e4 - return rho_crit_0 * self.omega_m * (1 + z) ** 3 + return self.rho_c * self.omega_m * (1 + z) ** 3 return MockCosmology() From c82a16fac637a77f908c0d6470b835fdd027ff3a Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 16:38:13 +0000 Subject: [PATCH 22/38] Test density weight --- tests/test_shells.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index cd1723ba..cb9d4280 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -7,7 +7,7 @@ RadialWindow, combine, # noqa: F401 cubic_windows, - density_weight, # noqa: F401 + density_weight, distance_grid, # noqa: F401 distance_weight, linear_windows, @@ -57,7 +57,20 @@ def test_volume_weight(cosmo: Cosmology) -> None: def test_density_weight(cosmo: Cosmology) -> None: """Add unit tests for :func:`density_weight`.""" - # AttributeError: 'MockCosmology' object has no attribute 'rho_m_z' + z = np.linspace(0, 1, 6) + + # check shape + + w = density_weight(z, cosmo) + np.testing.assert_array_equal(w.shape, z.shape) + + # check first value is 0 + + np.testing.assert_array_equal(w[0], 0) + + # check values are increasing + + np.testing.assert_array_less(w[:-1], w[1:]) def test_tophat_windows() -> None: From 49f58d2c682749b17394a65e1d06efa3678e1414 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 17:30:06 +0000 Subject: [PATCH 23/38] gh-456: call `redshift_grid` within `distance_grid` Closes #456. Discovered in #449. The contents of the two functions are near identical so makes sense to combine. --- glass/shells.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/glass/shells.py b/glass/shells.py index 23925c23..e8586f64 100644 --- a/glass/shells.py +++ b/glass/shells.py @@ -784,13 +784,7 @@ def distance_grid( """ xmin, xmax = cosmo.dc(zmin), cosmo.dc(zmax) - if dx is not None and num is None: - x = np.arange(xmin, np.nextafter(xmax + dx, xmax), dx) - elif dx is None and num is not None: - x = np.linspace(xmin, xmax, num + 1) - else: - msg = 'exactly one of "dx" or "num" must be given' - raise ValueError(msg) + x = redshift_grid(xmin, xmax, dz=dx, num=num) return cosmo.dc_inv(x) # type: ignore[no-any-return] From 4b0ecf494d0c32eb586f2eb6e85d219e45cb1c3c Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 17:49:07 +0000 Subject: [PATCH 24/38] Add `dc` to fixture --- tests/conftest.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b6425e6d..af6cde69 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -35,17 +35,25 @@ def xm( :math:`x_M(z) = d_M(z)/d_H` """ if z2 is None: - return np.array(z) * 1000 - return (np.array(z2) - np.array(z)) * 1000 - - def dc_inv(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: - """Inverse function for the comoving distance in Mpc.""" - return 1 / (self.xm(z) / 1000) + return np.array(z) * 1_000 + return (np.array(z2) - np.array(z)) * 1_000 def rho_m_z(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: """Redshift-dependent matter density in Msol Mpc-3.""" return self.rho_c * self.omega_m * (1 + z) ** 3 + def dc( + self, + z: npt.NDArray[np.float64], + z2: npt.NDArray[np.float64] | None = None, + ) -> npt.NDArray[np.float64]: + """Comoving distance :math:`d_c(z)` in Mpc.""" + return self.xm(z) / 1_000 if z2 is None else self.xm(z, z2) / 1_000 + + def dc_inv(self, dc: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + """Inverse function for the comoving distance in Mpc.""" + return 1_000 * (1 / dc) + return MockCosmology() From 8778997efd7f0c933566698332346d415064e85b Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Mon, 25 Nov 2024 18:07:53 +0000 Subject: [PATCH 25/38] Add distance grid --- tests/conftest.py | 2 +- tests/test_shells.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index af6cde69..01ea3686 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -52,7 +52,7 @@ def dc( def dc_inv(self, dc: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: """Inverse function for the comoving distance in Mpc.""" - return 1_000 * (1 / dc) + return 1_000 * (1 / (dc + np.finfo(float).eps)) return MockCosmology() diff --git a/tests/test_shells.py b/tests/test_shells.py index cb9d4280..69f5f3de 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -8,7 +8,7 @@ combine, # noqa: F401 cubic_windows, density_weight, - distance_grid, # noqa: F401 + distance_grid, distance_weight, linear_windows, partition, @@ -267,13 +267,42 @@ def test_redshift_grid() -> None: # check error raised + with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + redshift_grid(zmin, zmax) + with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): redshift_grid(zmin, zmax, dz=dz, num=num) def test_distance_grid(cosmo: Cosmology) -> None: """Add unit tests for :func:`distance_grid`.""" - # AttributeError: 'MockCosmology' object has no attribute 'dc_inv' + zmin = 0 + zmax = 1 + + # check num input + + num = 5 + x = distance_grid(cosmo, zmin, zmax, num=5) + np.testing.assert_array_equal(len(x), num + 1) + + # check dz input + + dx = 0.2 + x = distance_grid(cosmo, zmin, zmax, dx=dx) + np.testing.assert_array_equal(len(x), np.ceil((zmax - zmin) / dx) + 1) + + # check decrease in distance + + x = distance_grid(cosmo, zmin, zmax, dx=0.3) + np.testing.assert_array_less(x[1:], x[:-1]) + + # check error raised + + with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + distance_grid(cosmo, zmin, zmax) + + with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + distance_grid(cosmo, zmin, zmax, dx=dx, num=num) def test_combine() -> None: From 5eec94d13accb501fba43200d069797ef2a63f1f Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 23 Jan 2025 13:46:52 +0000 Subject: [PATCH 26/38] Fix tests --- tests/test_shells.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 69f5f3de..cd8c31d7 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -267,10 +267,16 @@ def test_redshift_grid() -> None: # check error raised - with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + with pytest.raises( + ValueError, + match="exactly one of grid step size or number of steps must be given", + ): redshift_grid(zmin, zmax) - with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + with pytest.raises( + ValueError, + match="exactly one of grid step size or number of steps must be given", + ): redshift_grid(zmin, zmax, dz=dz, num=num) @@ -298,10 +304,16 @@ def test_distance_grid(cosmo: Cosmology) -> None: # check error raised - with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + with pytest.raises( + ValueError, + match="exactly one of grid step size or number of steps must be given", + ): distance_grid(cosmo, zmin, zmax) - with pytest.raises(ValueError, match="exactly one of 'dz' or 'num' must be given"): + with pytest.raises( + ValueError, + match="exactly one of grid step size or number of steps must be given", + ): distance_grid(cosmo, zmin, zmax, dx=dx, num=num) From 2b58281e1cdd1599976b1cc0514894b11454f276 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:34:27 +0000 Subject: [PATCH 27/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index cd8c31d7..f89c8c59 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -252,7 +252,7 @@ def test_redshift_grid() -> None: num = 5 z = redshift_grid(zmin, zmax, num=5) - np.testing.assert_array_equal(len(z), num + 1) + assert len(z) == num + 1 # check dz input From 57333d368ff7b8e8469d144a5feaf2d9b6c5b894 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:34:36 +0000 Subject: [PATCH 28/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index f89c8c59..1568ab58 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -263,7 +263,7 @@ def test_redshift_grid() -> None: # check dz for spacing which results in a max value above zmax z = redshift_grid(zmin, zmax, dz=0.3) - np.testing.assert_array_less(zmax, z[-1]) + assert z[-1] == zmax # check error raised From b73421be28ec31e7feb14ec73d9fea9499050d88 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:34:43 +0000 Subject: [PATCH 29/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 1568ab58..ee2bd996 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -30,7 +30,7 @@ def test_distance_weight(cosmo: Cosmology) -> None: # check first value is 1 - np.testing.assert_array_equal(w[0], 1) + assert w[0] == 1 # check values are decreasing From 4b26970a4cd60163db0c2dfe218e6837b956c4ae Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:34:49 +0000 Subject: [PATCH 30/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index ee2bd996..7c8a08bc 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -289,7 +289,7 @@ def test_distance_grid(cosmo: Cosmology) -> None: num = 5 x = distance_grid(cosmo, zmin, zmax, num=5) - np.testing.assert_array_equal(len(x), num + 1) + assert len(x) == num + 1 # check dz input From 33cd6030a118cf83f5d921e4bf92bedebee8a913 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:34:56 +0000 Subject: [PATCH 31/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 7c8a08bc..1c03893d 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -258,7 +258,7 @@ def test_redshift_grid() -> None: dz = 0.2 z = redshift_grid(zmin, zmax, dz=dz) - np.testing.assert_array_equal(len(z), np.ceil((zmax - zmin) / dz) + 1) + assert len(z) == np.ceil((zmax - zmin) / dz) + 1 # check dz for spacing which results in a max value above zmax From dc8b30cfae2d76aeffecd0c315d61ba39f6ef0cb Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:35:04 +0000 Subject: [PATCH 32/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 1c03893d..176a23c4 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -295,7 +295,7 @@ def test_distance_grid(cosmo: Cosmology) -> None: dx = 0.2 x = distance_grid(cosmo, zmin, zmax, dx=dx) - np.testing.assert_array_equal(len(x), np.ceil((zmax - zmin) / dx) + 1) + assert len(x) == np.ceil((zmax - zmin) / dx) + 1 # check decrease in distance From 02c3a8ae8fb09ccc41abbe4d24914e737020979c Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:35:10 +0000 Subject: [PATCH 33/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 176a23c4..89b0c210 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -66,7 +66,7 @@ def test_density_weight(cosmo: Cosmology) -> None: # check first value is 0 - np.testing.assert_array_equal(w[0], 0) + assert w[0] == 0 # check values are increasing From 90080cb915cfda241b2d8e05ca37168a0c3815ef Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:35:18 +0000 Subject: [PATCH 34/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 89b0c210..ef16e23f 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -48,7 +48,7 @@ def test_volume_weight(cosmo: Cosmology) -> None: # check first value is 0 - np.testing.assert_array_equal(w[0], 0) + assert w[0] == 0 # check values are increasing From 69922094136fc501c24455c4ffe1149c5f4f58af Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:35:25 +0000 Subject: [PATCH 35/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index ef16e23f..781b4fff 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -159,7 +159,7 @@ def test_cubic_windows() -> None: # check number of windows - np.testing.assert_array_equal(len(ws), len(zgrid) - 2) + assert len(ws) == len(zgrid) - 2 # check values of zeff From 4732f71ce20c5513134cdfd4e6d1211d6960fa02 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:35:34 +0000 Subject: [PATCH 36/38] Update tests/test_shells.py Co-authored-by: Saransh Chopra --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 781b4fff..dd9b3032 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -113,7 +113,7 @@ def test_linear_windows() -> None: # check number of windows - np.testing.assert_array_equal(len(ws), len(zgrid) - 2) + assert len(ws) == len(zgrid) - 2 # check values of zeff From f962dce5368724fed2554c79b3ba68d181f63820 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:36:56 +0000 Subject: [PATCH 37/38] Fix indent --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index dd9b3032..968ed3b1 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -289,7 +289,7 @@ def test_distance_grid(cosmo: Cosmology) -> None: num = 5 x = distance_grid(cosmo, zmin, zmax, num=5) - assert len(x) == num + 1 + assert len(x) == num + 1 # check dz input From 1dc87af34d00a9ed79e353e1896f9c5f8ea3ef70 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 24 Jan 2025 15:40:58 +0000 Subject: [PATCH 38/38] Fix tests --- tests/test_shells.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_shells.py b/tests/test_shells.py index 968ed3b1..8f87bdf8 100644 --- a/tests/test_shells.py +++ b/tests/test_shells.py @@ -263,7 +263,7 @@ def test_redshift_grid() -> None: # check dz for spacing which results in a max value above zmax z = redshift_grid(zmin, zmax, dz=0.3) - assert z[-1] == zmax + assert zmax < z[-1] # check error raised