Repository navigation
Expand file tree
/
Copy pathtest_lensing.py
More file actions
148 lines (126 loc) · 4.57 KB
/
Copy pathtest_lensing.py
File metadata and controls
148 lines (126 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from __future__ import annotations
import typing
from typing import TYPE_CHECKING
import pytest
import array_api_extra as xpx
import glass
if TYPE_CHECKING:
from types import ModuleType
from typing import Never
from pytest_benchmark.fixture import BenchmarkFixture
from glass._types import FloatArray, UnifiedGenerator
from glass.cosmology import Cosmology
@pytest.mark.skipif(
not hasattr(glass, "multi_plane_matrix"),
reason="glass.multi_plane_matrix not implemented",
)
@pytest.mark.stable
def test_multi_plane_matrix(
benchmark: BenchmarkFixture,
cosmo: Cosmology,
urng: UnifiedGenerator,
xp: ModuleType,
) -> None:
"""Regression tests for add_window and add_plane with a multi_plane_matrix."""
# Use this over the fixture to allow us to add many more windows
shells = [
glass.RadialWindow(
xp.arange(i, i + 3, dtype=xp.float64),
xp.asarray([0.0, 1.0, 0.0]),
float(i + 1),
)
for i in range(1_000)
]
mat = glass.multi_plane_matrix(shells, cosmo)
deltas = urng.random((len(shells), 10))
xpx.testing.assert_equal(mat, xp.tril(mat))
xpx.testing.assert_equal(xp.triu(mat, k=1), xp.asarray(0.0), check_shape=False)
def setup_shells_and_deltas() -> tuple[
tuple[
glass.MultiPlaneConvergence,
list[glass.RadialWindow],
FloatArray,
],
dict[Never, Never],
]:
"""Run setup a generator with zip before each benchmark run."""
convergence = glass.MultiPlaneConvergence(cosmo)
return (convergence, shells, deltas), {}
def multi_plane_matrix_add_window(
convergence: type[glass.MultiPlaneConvergence],
shells: list[type[glass.RadialWindow]],
deltas: FloatArray,
) -> type[glass.MultiPlaneConvergence]:
"""Call add_window repeatedly, to be benchmarked."""
for i in range(min(len(shells), deltas.shape[0])):
shell = shells[i]
delta = deltas[i, ...]
convergence.add_window(delta, shell) # ty: ignore[missing-argument]
return convergence
actual_convergence = benchmark.pedantic(
multi_plane_matrix_add_window,
setup=setup_shells_and_deltas,
rounds=500,
)
assert actual_convergence.kappa.shape[0] == 10
for x in actual_convergence.kappa:
assert x is not None
@pytest.mark.skipif(
not hasattr(glass, "multi_plane_weights"),
reason="glass.multi_plane_weights not implemented",
)
@pytest.mark.stable
def test_multi_plane_weights(
benchmark: BenchmarkFixture,
cosmo: Cosmology,
urng: UnifiedGenerator,
xp: ModuleType,
) -> None:
"""Regression tests for add_window and add_plane with a multi_plane_weights."""
# Use this over the fixture to allow us to add many more windows
shells = [
glass.RadialWindow(
xp.arange(i, i + 3, dtype=xp.float64),
xp.asarray([0.0, 1.0, 0.0]),
float(i + 1),
)
for i in range(500)
]
w_in = xp.eye(len(shells))
deltas = urng.random((len(shells), 10))
weights = urng.random((len(shells), 3))
w_out = glass.multi_plane_weights(w_in, shells, cosmo)
xpx.testing.assert_equal(w_out, xp.triu(w_out, 1))
xpx.testing.assert_equal(xp.tril(w_out), xp.asarray(0.0), check_shape=False)
def setup_shells_deltas_and_weights() -> tuple[
tuple[
glass.MultiPlaneConvergence,
zip[tuple[glass.RadialWindow, FloatArray, FloatArray]],
],
dict[Never, Never],
]:
"""Run setup a generator with zip before each benchmark run."""
convergence = glass.MultiPlaneConvergence(cosmo)
return (
convergence,
typing.cast(
"zip[tuple[glass.RadialWindow, FloatArray, FloatArray]]",
zip(shells, deltas, weights, strict=False),
),
), {}
def multi_plane_weights_add_window(
convergence: type[glass.MultiPlaneConvergence],
zipped: tuple[list[type[glass.RadialWindow]], FloatArray, FloatArray],
) -> type[glass.MultiPlaneConvergence]:
"""Call add_window repeatedly, to be benchmarked."""
for shell, delta, _ in zipped:
convergence.add_window(delta, shell) # ty: ignore[missing-argument]
return convergence
actual_convergence = benchmark.pedantic(
multi_plane_weights_add_window,
setup=setup_shells_deltas_and_weights,
rounds=100,
)
assert actual_convergence.kappa.shape[0] == 10
for x in actual_convergence.kappa:
assert x is not None