flipcosmo 1.0.0__py3-none-any.whl → 1.2.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 (113) hide show
  1. docs/conf.py +154 -0
  2. flip/__init__.py +4 -11
  3. flip/covariance/__init__.py +7 -8
  4. flip/covariance/analytical/__init__.py +11 -0
  5. flip/covariance/{adamsblake17plane → analytical/adamsblake17}/coefficients.py +1 -1
  6. flip/covariance/{adamsblake17plane → analytical/adamsblake17}/fisher_terms.py +1 -1
  7. flip/covariance/{adamsblake17 → analytical/adamsblake17}/flip_terms.py +0 -1
  8. flip/covariance/{adamsblake17 → analytical/adamsblake17plane}/coefficients.py +1 -1
  9. flip/covariance/{adamsblake17 → analytical/adamsblake17plane}/fisher_terms.py +1 -1
  10. flip/covariance/{adamsblake17plane → analytical/adamsblake17plane}/flip_terms.py +0 -1
  11. flip/covariance/{adamsblake17plane → analytical/adamsblake17plane}/generator.py +103 -19
  12. flip/covariance/{adamsblake20 → analytical/adamsblake20}/coefficients.py +1 -1
  13. flip/covariance/{adamsblake20 → analytical/adamsblake20}/fisher_terms.py +1 -1
  14. flip/covariance/{adamsblake20 → analytical/adamsblake20}/flip_terms.py +0 -1
  15. flip/covariance/{carreres23 → analytical/carreres23}/coefficients.py +1 -4
  16. flip/covariance/{ravouxnoanchor25 → analytical/carreres23}/fisher_terms.py +1 -1
  17. flip/covariance/{carreres23 → analytical/carreres23}/flip_terms.py +0 -1
  18. flip/covariance/analytical/carreres23/generator.py +198 -0
  19. flip/covariance/analytical/genericzdep/__init__.py +5 -0
  20. flip/covariance/analytical/genericzdep/coefficients.py +53 -0
  21. flip/covariance/analytical/genericzdep/flip_terms.py +99 -0
  22. flip/covariance/{lai22 → analytical/lai22}/coefficients.py +2 -3
  23. flip/covariance/{lai22 → analytical/lai22}/fisher_terms.py +1 -1
  24. flip/covariance/{lai22 → analytical/lai22}/flip_terms.py +0 -1
  25. flip/covariance/{lai22 → analytical/lai22}/generator.py +263 -58
  26. flip/covariance/{lai22 → analytical/lai22}/symbolic.py +55 -19
  27. flip/covariance/{ravouxcarreres → analytical/ravouxcarreres}/coefficients.py +1 -1
  28. flip/covariance/{ravouxcarreres → analytical/ravouxcarreres}/fisher_terms.py +1 -1
  29. flip/covariance/{ravouxcarreres → analytical/ravouxcarreres}/flip_terms.py +0 -1
  30. flip/covariance/{ravouxnoanchor25 → analytical/ravouxnoanchor25}/coefficients.py +3 -2
  31. flip/covariance/{carreres23 → analytical/ravouxnoanchor25}/fisher_terms.py +1 -1
  32. flip/covariance/{ravouxnoanchor25 → analytical/ravouxnoanchor25}/flip_terms.py +0 -9
  33. flip/covariance/{rcrk24 → analytical/rcrk24}/coefficients.py +6 -6
  34. flip/covariance/{rcrk24 → analytical/rcrk24}/fisher_terms.py +7 -9
  35. flip/covariance/{rcrk24 → analytical/rcrk24}/flip_terms.py +0 -8
  36. flip/covariance/contraction.py +82 -40
  37. flip/covariance/cov_utils.py +89 -81
  38. flip/covariance/covariance.py +172 -141
  39. flip/covariance/emulators/__init__.py +1 -1
  40. flip/covariance/emulators/generator.py +73 -3
  41. flip/covariance/emulators/gpmatrix.py +40 -1
  42. flip/covariance/emulators/nnmatrix.py +57 -1
  43. flip/covariance/emulators/skgpmatrix.py +125 -0
  44. flip/covariance/fisher.py +307 -0
  45. flip/{fit_utils.py → covariance/fit_utils.py} +185 -10
  46. flip/{fitter.py → covariance/fitter.py} +151 -125
  47. flip/covariance/generator.py +82 -106
  48. flip/{likelihood.py → covariance/likelihood.py} +286 -64
  49. flip/{plot_utils.py → covariance/plot_utils.py} +79 -4
  50. flip/covariance/symbolic.py +89 -44
  51. flip/data/__init__.py +1 -1
  52. flip/data/data_density.parquet +0 -0
  53. flip/data/data_velocity.parquet +0 -0
  54. flip/data/{grid_window_m.parquet → data_window_density.parquet} +0 -0
  55. flip/{gridding.py → data/gridding.py} +125 -130
  56. flip/data/load_data_test.py +102 -0
  57. flip/data/power_spectrum_mm.txt +2 -2
  58. flip/data/power_spectrum_mt.txt +2 -2
  59. flip/data/power_spectrum_tt.txt +2 -2
  60. flip/data/test_covariance_reference_values.json +145 -0
  61. flip/data/test_e2e_reference_values.json +14 -0
  62. flip/data_vector/basic.py +118 -101
  63. flip/data_vector/cosmo_utils.py +18 -0
  64. flip/data_vector/galaxypv_vectors.py +58 -94
  65. flip/data_vector/snia_vectors.py +60 -3
  66. flip/data_vector/vector_utils.py +47 -1
  67. flip/power_spectra/class_engine.py +36 -1
  68. flip/power_spectra/cosmoprimo_engine.py +37 -2
  69. flip/power_spectra/generator.py +47 -25
  70. flip/power_spectra/models.py +30 -31
  71. flip/power_spectra/pyccl_engine.py +36 -1
  72. flip/simulation/__init__.py +0 -0
  73. flip/utils.py +62 -91
  74. flipcosmo-1.2.1.dist-info/METADATA +78 -0
  75. flipcosmo-1.2.1.dist-info/RECORD +109 -0
  76. {flipcosmo-1.0.0.dist-info → flipcosmo-1.2.1.dist-info}/WHEEL +1 -1
  77. flipcosmo-1.2.1.dist-info/top_level.txt +7 -0
  78. scripts/flip_compute_correlation_model.py +70 -0
  79. scripts/flip_compute_power_spectra.py +50 -0
  80. scripts/flip_fisher_forecast_velocity.py +70 -0
  81. scripts/flip_fisher_rcrk24.py +164 -0
  82. scripts/flip_launch_minuit_density_fit.py +91 -0
  83. scripts/flip_launch_minuit_full_fit.py +117 -0
  84. scripts/flip_launch_minuit_velocity_fit.py +78 -0
  85. scripts/flip_launch_minuit_velocity_fit_full.py +107 -0
  86. scripts/flip_launch_minuit_velocity_fit_interpolation.py +93 -0
  87. test/refresh_reference_values.py +43 -0
  88. test/test_covariance_assembly.py +102 -0
  89. test/test_covariance_reference_values.py +125 -0
  90. test/test_covariance_utils.py +34 -0
  91. test/test_e2e_density.py +50 -0
  92. test/test_e2e_joint.py +65 -0
  93. test/test_e2e_velocity.py +53 -0
  94. test/test_likelihood_inversions.py +31 -0
  95. flip/covariance/carreres23/generator.py +0 -132
  96. flip/data/density_data.parquet +0 -0
  97. flip/data/velocity_data.parquet +0 -0
  98. flip/fisher.py +0 -190
  99. flipcosmo-1.0.0.dist-info/METADATA +0 -32
  100. flipcosmo-1.0.0.dist-info/RECORD +0 -82
  101. flipcosmo-1.0.0.dist-info/top_level.txt +0 -1
  102. /flip/{config.py → _config.py} +0 -0
  103. /flip/covariance/{adamsblake17 → analytical/adamsblake17}/__init__.py +0 -0
  104. /flip/covariance/{adamsblake17plane → analytical/adamsblake17plane}/__init__.py +0 -0
  105. /flip/covariance/{adamsblake20 → analytical/adamsblake20}/__init__.py +0 -0
  106. /flip/covariance/{carreres23 → analytical/carreres23}/__init__.py +0 -0
  107. /flip/covariance/{lai22 → analytical/lai22}/__init__.py +0 -0
  108. /flip/covariance/{lai22 → analytical/lai22}/h_terms.py +0 -0
  109. /flip/covariance/{ravouxcarreres → analytical/ravouxcarreres}/__init__.py +0 -0
  110. /flip/covariance/{ravouxcarreres → analytical/ravouxcarreres}/flip_terms_lmax.py +0 -0
  111. /flip/covariance/{ravouxnoanchor25 → analytical/ravouxnoanchor25}/__init__.py +0 -0
  112. /flip/covariance/{rcrk24 → analytical/rcrk24}/__init__.py +0 -0
  113. {flipcosmo-1.0.0.dist-info → flipcosmo-1.2.1.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,198 @@
1
+ import multiprocessing as mp
2
+ from functools import partial
3
+
4
+ import numpy as np
5
+ from scipy.special import spherical_jn
6
+
7
+ from flip.covariance import cov_utils
8
+
9
+
10
+ def angle_between(ra_0, ra_1, dec_0, dec_1):
11
+ """Compute cos of the angle between two directions.
12
+
13
+ Args:
14
+ ra_0: Right ascension of first object (radians).
15
+ ra_1: Right ascension of second object (radians).
16
+ dec_0: Declination of first object (radians).
17
+ dec_1: Declination of second object (radians).
18
+
19
+ Returns:
20
+ Cosine of the angle between the two directions.
21
+ """
22
+ cos_alpha = np.cos(ra_1 - ra_0) * np.cos(dec_0) * np.cos(dec_1) + np.sin(
23
+ dec_0
24
+ ) * np.sin(dec_1)
25
+ return cos_alpha
26
+
27
+
28
+ def separation(r_0, r_1, cos_alpha):
29
+ """Compute comoving separation given distances and angular cosine.
30
+
31
+ Args:
32
+ r_0: Comoving distance of first object.
33
+ r_1: Comoving distance of second object.
34
+ cos_alpha: Cosine of the angular separation.
35
+
36
+ Returns:
37
+ Scalar separation ``|r_0 - r_1|`` generalized to non-colinear case.
38
+ """
39
+ return np.sqrt(r_0**2 + r_1**2 - 2 * r_0 * r_1 * cos_alpha)
40
+
41
+
42
+ def window(r_0, r_1, cos_alpha, sep, j0kr, j2kr):
43
+ """Wide-angle window function using bisector definition.
44
+
45
+ Args:
46
+ r_0: Comoving distance of first object.
47
+ r_1: Comoving distance of second object.
48
+ cos_alpha: Cosine of angle between directions.
49
+ sep: Comoving separation.
50
+ j0kr: Spherical Bessel ``j_0(k r)`` evaluated on grid.
51
+ j2kr: Spherical Bessel ``j_2(k r)`` evaluated on grid.
52
+
53
+ Returns:
54
+ Window values per k contributing to the vv covariance integral.
55
+ """
56
+ win = 1 / 3 * (j0kr - 2 * j2kr) * cos_alpha
57
+ win += j2kr * r_0 * r_1 / sep**2 * (1 - cos_alpha**2)
58
+ return win
59
+
60
+
61
+ def intp(win, k, pk):
62
+ """Integrate window times power spectrum over k.
63
+
64
+ Args:
65
+ win: Window array per k (shape compatible with k).
66
+ k: Wavenumber grid.
67
+ pk: Power spectrum values at k.
68
+
69
+ Returns:
70
+ Scalar integral value via trapezoidal rule.
71
+ """
72
+ pint = win.T * pk
73
+ return np.trapz(pint, x=k)
74
+
75
+
76
+ def covariance_vv(
77
+ ra_in,
78
+ dec_in,
79
+ rcomov_in,
80
+ k_in,
81
+ pk_in,
82
+ grid_window_in=None,
83
+ size_batch=100_000,
84
+ number_worker=8,
85
+ ):
86
+ """Compute Carreres23 vv covariance using bisector wide-angle.
87
+
88
+ Applies optional grid window to the velocity power spectrum and integrates
89
+ per pair in batches using multiprocessing.
90
+
91
+ Args:
92
+ ra_in: Right ascensions (radians).
93
+ dec_in: Declinations (radians).
94
+ rcomov_in: Comoving distances.
95
+ k_in: Wavenumber grid.
96
+ pk_in: Velocity power spectrum values at ``k_in``.
97
+ grid_window_in: Optional window to apply to ``pk_in``.
98
+ size_batch: Number of pairs per batch for processing.
99
+ number_worker: Number of parallel workers.
100
+
101
+ Returns:
102
+ Flattened covariance vector: variance at index 0, then upper triangle.
103
+ """
104
+ N = len(ra_in)
105
+
106
+ if grid_window_in is not None:
107
+ pk = pk_in * grid_window_in**2
108
+ else:
109
+ pk = pk_in
110
+
111
+ n_task = int((N * (N + 1)) / 2) - N
112
+
113
+ batches = []
114
+ for n in range(0, n_task, size_batch):
115
+ brange = np.arange(n, np.min((n + size_batch, n_task)))
116
+ i_list, j_list = cov_utils.compute_i_j(N, brange)
117
+ r_comovi, rai, deci = rcomov_in[i_list], ra_in[i_list], dec_in[i_list]
118
+ r_comovj, raj, decj = rcomov_in[j_list], ra_in[j_list], dec_in[j_list]
119
+ batches.append([rai, raj, deci, decj, r_comovi, r_comovj])
120
+
121
+ with mp.Pool(number_worker) as pool:
122
+ func = partial(compute_coef, k_in, pk)
123
+ pool_results = pool.map(func, batches)
124
+ values = np.concatenate(pool_results)
125
+
126
+ var_val = np.trapz(pk / 3, x=k_in)
127
+ cov = np.insert(values, 0, var_val)
128
+ cov = 100**2 / (2 * np.pi**2) * cov
129
+ return cov
130
+
131
+
132
+ def compute_coef(k, pk, coord):
133
+ """Compute covariance coefficient for a single pair.
134
+
135
+ Args:
136
+ k: Wavenumber grid.
137
+ pk: Power spectrum values at k.
138
+ coord: Tuple/list ``(ra_i, ra_j, dec_i, dec_j, r_i, r_j)``.
139
+
140
+ Returns:
141
+ Scalar integral value contributing to the covariance.
142
+ """
143
+ cos = angle_between(coord[0], coord[1], coord[2], coord[3])
144
+ sep = separation(coord[4], coord[5], cos)
145
+ ksep = np.outer(k, sep)
146
+ j0 = spherical_jn(0, ksep)
147
+ j2 = spherical_jn(2, ksep)
148
+ res = window(coord[4], coord[5], cos, sep, j0, j2)
149
+ res = intp(res, k, pk)
150
+ return res
151
+
152
+
153
+ def generate_covariance(
154
+ model_kind,
155
+ power_spectrum_dict,
156
+ coordinates_density=False,
157
+ coordinates_velocity=None,
158
+ **kwargs,
159
+ ):
160
+ """Generate Carreres23 covariance for velocity-only model.
161
+
162
+ Wide-angle definition uses the bisector. Only ``vv`` block is produced.
163
+
164
+ Args:
165
+ model_kind: Must be ``"velocity"``.
166
+ power_spectrum_dict: Dict with ``{"vv": [(k,), (pk,)]}`` entries.
167
+ coordinates_density: Ignored; kept for interface compatibility.
168
+ coordinates_velocity: Tuple ``(ra, dec, rcom)`` for velocity tracers.
169
+ **kwargs: Forwarded to ``covariance_vv`` (e.g., batching/workers/window).
170
+
171
+ Returns:
172
+ Tuple ``(covariance_dict, number_densities, number_velocities, los_definition)``.
173
+ """
174
+ assert model_kind == "velocity"
175
+ cov_utils.check_generator_need(
176
+ model_kind,
177
+ coordinates_density,
178
+ coordinates_velocity,
179
+ )
180
+ number_densities = None
181
+ number_velocities = len(coordinates_velocity[0])
182
+ cov_vv = covariance_vv(
183
+ coordinates_velocity[0],
184
+ coordinates_velocity[1],
185
+ coordinates_velocity[2],
186
+ power_spectrum_dict["vv"][0][0],
187
+ power_spectrum_dict["vv"][0][1],
188
+ **kwargs,
189
+ )
190
+
191
+ los_definition = "bisector"
192
+
193
+ return (
194
+ {"vv": np.array([cov_vv])},
195
+ number_densities,
196
+ number_velocities,
197
+ los_definition,
198
+ )
@@ -0,0 +1,5 @@
1
+ _variant = [None]
2
+
3
+ _free_par = {"sigv": "velocity@all"}
4
+
5
+ _coordinate_keys = ["ra", "dec", "rcom_zobs", "zobs"]
@@ -0,0 +1,53 @@
1
+ from ..._config import __use_jax__
2
+
3
+ if __use_jax__:
4
+ try:
5
+ import jax.numpy as jnp
6
+
7
+ jax_installed = True
8
+
9
+ except ImportError:
10
+ import numpy as jnp
11
+
12
+ jax_installed = False
13
+ else:
14
+ import numpy as jnp
15
+
16
+ jax_installed = False
17
+
18
+
19
+ def get_coefficients(
20
+ parameter_values_dict,
21
+ model_kind,
22
+ variant=None,
23
+ covariance_prefactor_dict=None,
24
+ ):
25
+ coefficients_dict = {}
26
+
27
+ redshift_velocity = covariance_prefactor_dict["redshift_velocity"]
28
+ f_z = covariance_prefactor_dict["f_z"]
29
+ E_z = covariance_prefactor_dict["E_z"]
30
+ D_growth_z = covariance_prefactor_dict["D_growth_z"]
31
+
32
+ coefficients_dict["vv"] = get_cov_matrix_prefactor(
33
+ redshift_velocity, f_z, E_z, D_growth_z
34
+ )
35
+ return coefficients_dict
36
+
37
+
38
+ def get_cov_matrix_prefactor(redshift, f_z, E_z, D_growth_z):
39
+ Ai_z = f_z * D_growth_z * E_z / (1 + redshift)
40
+ Aij_z = jnp.outer(Ai_z, Ai_z)
41
+ Delta_D_growth_squared = jnp.subtract.outer(D_growth_z, D_growth_z) ** 2
42
+
43
+ return [
44
+ Aij_z * jnp.ones_like(Delta_D_growth_squared),
45
+ Aij_z * Delta_D_growth_squared,
46
+ Aij_z * 1 / 2 * Delta_D_growth_squared**2,
47
+ ]
48
+
49
+
50
+ def get_diagonal_coefficients(parameter_values_dict, model_kind):
51
+ coefficients_dict = {}
52
+ coefficients_dict["vv"] = parameter_values_dict["sigv"] ** 2
53
+ return coefficients_dict
@@ -0,0 +1,99 @@
1
+ import mpmath
2
+ import numpy
3
+ import scipy
4
+
5
+
6
+ def set_backend(module):
7
+ global np, erf
8
+ if module == "numpy":
9
+ np = numpy
10
+ erf = scipy.special.erf
11
+ elif module == "mpmath":
12
+ np = mpmath.mp
13
+ erf = mpmath.erf
14
+
15
+
16
+ set_backend("numpy")
17
+
18
+
19
+ def M_vv_0_0_0(kNL):
20
+ def func(k):
21
+ return (10000 / 9) / k**2
22
+
23
+ return func
24
+
25
+
26
+ def N_vv_0_0_0(theta, phi):
27
+ return 3 * np.cos(theta)
28
+
29
+
30
+ def M_vv_0_2_0(kNL):
31
+ def func(k):
32
+ return (10000 / 9) / k**2
33
+
34
+ return func
35
+
36
+
37
+ def N_vv_0_2_0(theta, phi):
38
+ return (9 / 2) * np.cos(2 * phi) + (3 / 2) * np.cos(theta)
39
+
40
+
41
+ def M_vv_1_0_0(kNL):
42
+ def func(k):
43
+ return (10000 / 9) / kNL**2
44
+
45
+ return func
46
+
47
+
48
+ def N_vv_1_0_0(theta, phi):
49
+ return 3 * np.cos(theta)
50
+
51
+
52
+ def M_vv_1_2_0(kNL):
53
+ def func(k):
54
+ return (10000 / 9) / kNL**2
55
+
56
+ return func
57
+
58
+
59
+ def N_vv_1_2_0(theta, phi):
60
+ return (9 / 2) * np.cos(2 * phi) + (3 / 2) * np.cos(theta)
61
+
62
+
63
+ def M_vv_2_0_0(kNL):
64
+ def func(k):
65
+ return (10000 / 9) * k**2 / kNL**4
66
+
67
+ return func
68
+
69
+
70
+ def N_vv_2_0_0(theta, phi):
71
+ return 3 * np.cos(theta)
72
+
73
+
74
+ def M_vv_2_2_0(kNL):
75
+ def func(k):
76
+ return (10000 / 9) * k**2 / kNL**4
77
+
78
+ return func
79
+
80
+
81
+ def N_vv_2_2_0(theta, phi):
82
+ return (9 / 2) * np.cos(2 * phi) + (3 / 2) * np.cos(theta)
83
+
84
+
85
+ dictionary_terms = {"vv": ["0", "1", "2"]}
86
+ dictionary_lmax = {"vv": [2, 2, 2]}
87
+ dictionary_subterms = {
88
+ "vv_0_0": 1,
89
+ "vv_0_1": 0,
90
+ "vv_0_2": 1,
91
+ "vv_1_0": 1,
92
+ "vv_1_1": 0,
93
+ "vv_1_2": 1,
94
+ "vv_2_0": 1,
95
+ "vv_2_1": 0,
96
+ "vv_2_2": 1,
97
+ }
98
+ multi_index_model = False
99
+ regularize_M_terms = None
@@ -1,13 +1,12 @@
1
1
  import numpy as np
2
-
3
- from flip.covariance.lai22.flip_terms import dictionary_terms
2
+ from flip.covariance.analytical.lai22.flip_terms import dictionary_terms
4
3
 
5
4
 
6
5
  def get_coefficients(
7
6
  parameter_values_dict,
8
7
  model_kind,
9
8
  variant=None,
10
- redshift_dict=None,
9
+ covariance_prefactor_dict=None,
11
10
  ):
12
11
  coefficients_dict = {}
13
12
  if model_kind in ["density", "full", "density_velocity"]:
@@ -5,7 +5,7 @@ def get_partial_derivative_coefficients(
5
5
  model_kind,
6
6
  parameter_values_dict,
7
7
  variant=None,
8
- redshift_dict=None,
8
+ covariance_prefactor_dict=None,
9
9
  ):
10
10
  if model_kind == "density":
11
11
  return get_partial_derivative_coefficients_density(
@@ -15999,5 +15999,4 @@ dictionary_subterms = {
15999
15999
  "vv_0_0_2": 1,
16000
16000
  }
16001
16001
  multi_index_model = True
16002
- redshift_dependent_model = False
16003
16002
  regularize_M_terms = None