plima 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. plima/__init__.py +1 -0
  2. plima/__pycache__/__init__.cpython-312.pyc +0 -0
  3. plima/_version.py +24 -0
  4. plima/backends/__init__.py +1 -0
  5. plima/backends/__pycache__/__init__.cpython-312.pyc +0 -0
  6. plima/backends/ccl/__init__.py +1 -0
  7. plima/backends/ccl/__pycache__/__init__.cpython-312.pyc +0 -0
  8. plima/backends/ccl/__pycache__/builder.cpython-312.pyc +0 -0
  9. plima/backends/ccl/__pycache__/halo_model.cpython-312.pyc +0 -0
  10. plima/backends/ccl/__pycache__/la.cpython-312.pyc +0 -0
  11. plima/backends/ccl/__pycache__/nla.cpython-312.pyc +0 -0
  12. plima/backends/ccl/__pycache__/tatt.cpython-312.pyc +0 -0
  13. plima/backends/ccl/builder.py +294 -0
  14. plima/backends/ccl/halo_model.py +471 -0
  15. plima/backends/ccl/la.py +69 -0
  16. plima/backends/ccl/nla.py +69 -0
  17. plima/backends/ccl/tatt.py +314 -0
  18. plima/models/__init__.py +19 -0
  19. plima/models/__pycache__/__init__.cpython-312.pyc +0 -0
  20. plima/models/__pycache__/_discovery.cpython-312.pyc +0 -0
  21. plima/models/__pycache__/halo_model.cpython-312.pyc +0 -0
  22. plima/models/__pycache__/la.cpython-312.pyc +0 -0
  23. plima/models/__pycache__/model_registry.cpython-312.pyc +0 -0
  24. plima/models/__pycache__/nla.cpython-312.pyc +0 -0
  25. plima/models/__pycache__/tatt.cpython-312.pyc +0 -0
  26. plima/models/_discovery.py +27 -0
  27. plima/models/halo_model.py +91 -0
  28. plima/models/la.py +408 -0
  29. plima/models/model_registry.py +88 -0
  30. plima/models/nla.py +399 -0
  31. plima/models/tatt.py +283 -0
  32. plima/utils/__init__.py +1 -0
  33. plima/utils/__pycache__/__init__.cpython-312.pyc +0 -0
  34. plima/utils/__pycache__/constants.cpython-312.pyc +0 -0
  35. plima/utils/__pycache__/converters.cpython-312.pyc +0 -0
  36. plima/utils/__pycache__/types.cpython-312.pyc +0 -0
  37. plima/utils/__pycache__/validators.cpython-312.pyc +0 -0
  38. plima/utils/constants.py +24 -0
  39. plima/utils/converters.py +65 -0
  40. plima/utils/types.py +15 -0
  41. plima/utils/validators.py +170 -0
  42. plima-0.1.1.dist-info/METADATA +91 -0
  43. plima-0.1.1.dist-info/RECORD +46 -0
  44. plima-0.1.1.dist-info/WHEEL +5 -0
  45. plima-0.1.1.dist-info/licenses/LICENSE +21 -0
  46. plima-0.1.1.dist-info/top_level.txt +1 -0
plima/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """Python Library for Intrinsic alignment Models and Amplitudes."""
plima/_version.py ADDED
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.1.1'
22
+ __version_tuple__ = version_tuple = (0, 1, 1)
23
+
24
+ __commit_id__ = commit_id = None
@@ -0,0 +1 @@
1
+ """Backend integrations for PLIMA."""
@@ -0,0 +1 @@
1
+ """CCL backend helpers for PLIMA intrinsic alignment models."""
@@ -0,0 +1,294 @@
1
+ """Builder for CCL intrinsic alignment backend objects.
2
+
3
+ This module provides one public entry point for building PLIMA intrinsic
4
+ alignment models for CCL.
5
+
6
+ The PLIMA model registry is the source of truth for model names and aliases.
7
+ This builder resolves models through ``plima.models.model_registry`` instead
8
+ of keeping a second alias registry.
9
+
10
+ LA and NLA models use native CCL IA bias tuples.
11
+
12
+ TATT and halo model IA use custom CCL Pk2D spectra.
13
+
14
+ The forecasting code should branch on the returned ``mode`` value instead of
15
+ checking model names directly.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from collections.abc import Callable
21
+ from inspect import signature
22
+ from typing import Any, Literal, TypedDict
23
+
24
+ from numpy.typing import ArrayLike
25
+
26
+ from plima.backends.ccl.halo_model import make_ccl_halo_model_power_spectra
27
+ from plima.backends.ccl.la import make_ccl_la_ia_bias
28
+ from plima.backends.ccl.nla import make_ccl_nla_ia_bias
29
+ from plima.backends.ccl.tatt import make_ccl_tatt_power_spectra
30
+ from plima.models.model_registry import get_model, list_model_aliases
31
+ from plima.utils.types import ModelFunction
32
+
33
+ __all__ = [
34
+ "CCLIAMode",
35
+ "CCLIAModel",
36
+ "build_ccl_ia_model",
37
+ ]
38
+
39
+
40
+ CCLIAMode = Literal["native_bias", "pk2d"]
41
+ CCLIABuilder = Callable[..., "CCLIAModel"]
42
+
43
+
44
+ class CCLIAModel(TypedDict):
45
+ """Dictionary returned by the CCL IA builder."""
46
+
47
+ model_name: str
48
+ canonical_model_name: str
49
+ family: str
50
+ backend: str
51
+ mode: CCLIAMode
52
+ ia_bias: tuple[Any, Any] | None
53
+ spectra: dict[str, Any]
54
+ metadata: dict[str, Any]
55
+
56
+
57
+ def _normalize_model_name(model_name: str) -> str:
58
+ """Return a normalized model name."""
59
+ return model_name.strip().lower()
60
+
61
+
62
+ def _canonical_model_name(model_name: str) -> str:
63
+ """Return the canonical registered model name for a model or alias."""
64
+ normalized_name = _normalize_model_name(model_name)
65
+ aliases_by_model = list_model_aliases()
66
+
67
+ if normalized_name in aliases_by_model:
68
+ return normalized_name
69
+
70
+ for canonical_name, aliases in aliases_by_model.items():
71
+ if normalized_name in aliases:
72
+ return canonical_name
73
+
74
+ return normalized_name
75
+
76
+
77
+ def _model_family(model_function: ModelFunction) -> str:
78
+ """Return the PLIMA model family from the model function module."""
79
+ return model_function.__module__.rsplit(".", maxsplit=1)[-1]
80
+
81
+
82
+ def _model_accepts_z(model_function: ModelFunction) -> bool:
83
+ """Return whether a registered model function accepts ``z``."""
84
+ parameters = signature(model_function).parameters
85
+
86
+ return "z" in parameters
87
+
88
+
89
+ def _evaluate_amplitude_model(
90
+ model_function: ModelFunction,
91
+ z: ArrayLike,
92
+ **kwargs: Any,
93
+ ) -> Any:
94
+ """Evaluate a registered amplitude model."""
95
+ if _model_accepts_z(model_function):
96
+ return model_function(z, **kwargs)
97
+
98
+ return model_function(**kwargs)
99
+
100
+
101
+ def _native_bias_model(
102
+ *,
103
+ model_name: str,
104
+ canonical_model_name: str,
105
+ family: str,
106
+ ia_bias: tuple[Any, Any],
107
+ amplitude: Any,
108
+ ) -> CCLIAModel:
109
+ """Return a standard native CCL IA bias model dictionary."""
110
+ return {
111
+ "model_name": model_name,
112
+ "canonical_model_name": canonical_model_name,
113
+ "family": family,
114
+ "backend": "ccl",
115
+ "mode": "native_bias",
116
+ "ia_bias": ia_bias,
117
+ "spectra": {},
118
+ "metadata": {
119
+ "uses_native_ccl_ia": True,
120
+ "uses_pk2d": False,
121
+ "amplitude": amplitude,
122
+ },
123
+ }
124
+
125
+
126
+ def _pk2d_model(
127
+ *,
128
+ model_name: str,
129
+ canonical_model_name: str,
130
+ family: str,
131
+ spectra: dict[str, Any],
132
+ raw_backend_result: dict[str, Any],
133
+ ) -> CCLIAModel:
134
+ """Return a standard custom CCL Pk2D model dictionary."""
135
+ return {
136
+ "model_name": model_name,
137
+ "canonical_model_name": canonical_model_name,
138
+ "family": family,
139
+ "backend": "ccl",
140
+ "mode": "pk2d",
141
+ "ia_bias": None,
142
+ "spectra": spectra,
143
+ "metadata": {
144
+ "uses_native_ccl_ia": False,
145
+ "uses_pk2d": True,
146
+ "raw_backend_result": raw_backend_result,
147
+ },
148
+ }
149
+
150
+
151
+ def _build_la_model(
152
+ *,
153
+ cosmo: Any,
154
+ model_name: str,
155
+ canonical_model_name: str,
156
+ model_function: ModelFunction,
157
+ z: ArrayLike,
158
+ **kwargs: Any,
159
+ ) -> CCLIAModel:
160
+ """Build a native CCL IA bias model for LA."""
161
+ del cosmo
162
+
163
+ amplitude = _evaluate_amplitude_model(model_function, z, **kwargs)
164
+ ia_bias = make_ccl_la_ia_bias(z, amplitude=amplitude)
165
+
166
+ return _native_bias_model(
167
+ model_name=model_name,
168
+ canonical_model_name=canonical_model_name,
169
+ family="la",
170
+ ia_bias=ia_bias,
171
+ amplitude=amplitude,
172
+ )
173
+
174
+
175
+ def _build_nla_model(
176
+ *,
177
+ cosmo: Any,
178
+ model_name: str,
179
+ canonical_model_name: str,
180
+ model_function: ModelFunction,
181
+ z: ArrayLike,
182
+ **kwargs: Any,
183
+ ) -> CCLIAModel:
184
+ """Build a native CCL IA bias model for NLA."""
185
+ del cosmo
186
+
187
+ amplitude = _evaluate_amplitude_model(model_function, z, **kwargs)
188
+ ia_bias = make_ccl_nla_ia_bias(z, amplitude=amplitude)
189
+
190
+ return _native_bias_model(
191
+ model_name=model_name,
192
+ canonical_model_name=canonical_model_name,
193
+ family="nla",
194
+ ia_bias=ia_bias,
195
+ amplitude=amplitude,
196
+ )
197
+
198
+
199
+ def _build_tatt_model(
200
+ *,
201
+ cosmo: Any,
202
+ model_name: str,
203
+ canonical_model_name: str,
204
+ model_function: ModelFunction,
205
+ z: ArrayLike,
206
+ **kwargs: Any,
207
+ ) -> CCLIAModel:
208
+ """Build custom CCL Pk2D spectra for TATT."""
209
+ del model_function
210
+
211
+ result = make_ccl_tatt_power_spectra(cosmo, z, **kwargs)
212
+
213
+ return _pk2d_model(
214
+ model_name=model_name,
215
+ canonical_model_name=canonical_model_name,
216
+ family="tatt",
217
+ spectra=result["spectra"],
218
+ raw_backend_result=result,
219
+ )
220
+
221
+
222
+ def _build_halo_model(
223
+ *,
224
+ cosmo: Any,
225
+ model_name: str,
226
+ canonical_model_name: str,
227
+ model_function: ModelFunction,
228
+ z: ArrayLike,
229
+ **kwargs: Any,
230
+ ) -> CCLIAModel:
231
+ """Build custom CCL Pk2D spectra for halo model IA."""
232
+ del model_function
233
+
234
+ result = make_ccl_halo_model_power_spectra(cosmo, z, **kwargs)
235
+
236
+ return _pk2d_model(
237
+ model_name=model_name,
238
+ canonical_model_name=canonical_model_name,
239
+ family="halo_model",
240
+ spectra=result["spectra"],
241
+ raw_backend_result=result,
242
+ )
243
+
244
+
245
+ _CCL_FAMILY_BUILDERS: dict[str, CCLIABuilder] = {
246
+ "la": _build_la_model,
247
+ "nla": _build_nla_model,
248
+ "tatt": _build_tatt_model,
249
+ "halo_model": _build_halo_model,
250
+ }
251
+
252
+
253
+ def build_ccl_ia_model(
254
+ cosmo: Any,
255
+ model_name: str,
256
+ z: ArrayLike,
257
+ **kwargs: Any,
258
+ ) -> CCLIAModel:
259
+ """Build CCL backend objects for a registered PLIMA IA model.
260
+
261
+ Args:
262
+ cosmo: CCL cosmology object. This is used by models that build custom
263
+ Pk2D spectra. It is accepted for all models so the public API stays
264
+ stable.
265
+ model_name: PLIMA model name or alias.
266
+ z: Redshift values used by the IA model.
267
+ **kwargs: Model keyword arguments.
268
+
269
+ Returns:
270
+ Standard CCL IA model dictionary.
271
+
272
+ Raises:
273
+ ValueError: If the registered model family has no CCL builder.
274
+ KeyError: If ``model_name`` is not registered in PLIMA.
275
+ """
276
+ normalized_name = _normalize_model_name(model_name)
277
+ canonical_name = _canonical_model_name(normalized_name)
278
+ model_function = get_model(normalized_name)
279
+ family = _model_family(model_function)
280
+
281
+ try:
282
+ builder = _CCL_FAMILY_BUILDERS[family]
283
+ except KeyError as error:
284
+ msg = f"Model family {family!r} has no CCL builder."
285
+ raise ValueError(msg) from error
286
+
287
+ return builder(
288
+ cosmo=cosmo,
289
+ model_name=normalized_name,
290
+ canonical_model_name=canonical_name,
291
+ model_function=model_function,
292
+ z=z,
293
+ **kwargs,
294
+ )