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
@@ -8,7 +8,7 @@ import numpy as np
8
8
  from flip.covariance import cov_utils
9
9
  from flip.utils import create_log
10
10
 
11
- from ..config import __use_jax__
11
+ from .._config import __use_jax__
12
12
 
13
13
  if __use_jax__:
14
14
  try:
@@ -22,7 +22,6 @@ if __use_jax__:
22
22
 
23
23
  jax_installed = False
24
24
  else:
25
-
26
25
  import numpy as jnp
27
26
 
28
27
  jax_installed = False
@@ -36,7 +35,19 @@ def _read_free_par(
36
35
  model_kind,
37
36
  variant=None,
38
37
  ):
39
- _free_par = importlib.import_module(f"flip.covariance.{model_name}")._free_par
38
+ """Read free parameter names for a given model kind and variant.
39
+
40
+ Args:
41
+ model_name (str): Covariance model package name.
42
+ model_kind (str): Kind string (`density`, `velocity`, `density_velocity`, `full`).
43
+ variant (str, optional): Model variant; defaults to `baseline`.
44
+
45
+ Returns:
46
+ list[str]: Unique free parameter names used by the model and variant.
47
+ """
48
+ _free_par = importlib.import_module(
49
+ f"flip.covariance.analytical.{model_name}"
50
+ )._free_par
40
51
  model_kind = model_kind.split("_")
41
52
 
42
53
  if variant is None:
@@ -63,7 +74,22 @@ def compute_covariance_sum(
63
74
  kind="full",
64
75
  parameter_values_dict=None,
65
76
  ):
66
-
77
+ """Compose the total covariance matrix from blocks and coefficients.
78
+
79
+ Assembles `gg`, `gv`, and `vv` blocks with optional diagonal terms and adds
80
+ data variance. Supports special cases like `density_velocity` (no cross-term).
81
+
82
+ Args:
83
+ covariance_dict (dict): Covariance blocks keyed by `gg/gv/vv`.
84
+ coefficients_dict (dict): Coefficient arrays per block.
85
+ coefficients_dict_diagonal (dict): Diagonal noise terms per block.
86
+ vector_variance (array-like): Data variance vector or matrix.
87
+ kind (str): Model kind.
88
+ parameter_values_dict (dict, optional): Values for callable covariances (emulator).
89
+
90
+ Returns:
91
+ array-like: Total covariance matrix `C`.
92
+ """
67
93
  if kind == "density":
68
94
  keys = ["gg"]
69
95
  elif kind == "velocity":
@@ -77,25 +103,19 @@ def compute_covariance_sum(
77
103
 
78
104
  for k in keys:
79
105
  if parameter_values_dict is not None:
80
- covariance_sum_[k] = jnp.sum(
81
- jnp.stack(
82
- [
83
- coefficients_dict[k][i] * cov(parameter_values_dict)
84
- for i, cov in enumerate(covariance_dict[k])
85
- ]
86
- ),
87
- axis=0,
106
+ covariance_evaluated = np.array(
107
+ [cov(parameter_values_dict) for i, cov in enumerate(covariance_dict[k])]
88
108
  )
89
109
  else:
90
- covariance_sum_[k] = jnp.sum(
91
- jnp.stack(
92
- [
93
- coefficients_dict[k][i] * cov
94
- for i, cov in enumerate(covariance_dict[k])
95
- ]
96
- ),
97
- axis=0,
98
- )
110
+ covariance_evaluated = covariance_dict[k]
111
+
112
+ coefficient_3d = jnp.atleast_3d(jnp.atleast_2d(coefficients_dict[k].T).T)
113
+
114
+ covariance_sum_[k] = jnp.sum(
115
+ coefficient_3d * covariance_evaluated,
116
+ axis=0,
117
+ )
118
+
99
119
  if k in coefficients_dict_diagonal:
100
120
  covariance_sum_[k] += coefficients_dict_diagonal[k] * jnp.eye(
101
121
  covariance_sum_[k].shape[0]
@@ -140,7 +160,6 @@ class CovMatrix:
140
160
  los_definition (str): Definition of the line-of-sight.
141
161
  covariance_dict (dict): Dictionary containing covariance matrices.
142
162
  matrix_form (bool): Indicates if covariance is in matrix form.
143
- redshift_dict (dict): Dictionary of redshift information.
144
163
  variant (str): Variant of the model.
145
164
  number_densities (int): Number of density bins.
146
165
  number_velocities (int): Number of velocity bins.
@@ -177,40 +196,26 @@ class CovMatrix:
177
196
  los_definition=None,
178
197
  covariance_dict=None,
179
198
  matrix_form=False,
180
- redshift_dict=None,
181
199
  variant=None,
182
200
  coefficients=None,
183
201
  number_densities=None,
184
202
  number_velocities=None,
185
203
  emulator_flag=False,
186
204
  ):
187
- """
188
- Initialize the covariance model with specified parameters.
205
+ """Initialize the covariance model.
189
206
 
190
207
  Args:
191
- model_name (str, optional): Name of the covariance model to use.
192
- model_kind (str, optional): Type or kind of the model.
193
- free_par (Any, optional): Free parameters for the model.
194
- los_definition (Any, optional): Definition of the line-of-sight.
195
- covariance_dict (dict, optional): Dictionary containing covariance data.
196
- matrix_form (bool, optional): If True, use matrix form for computations.
197
- redshift_dict (dict, optional): Dictionary containing redshift information.
198
- variant (Any, optional): Variant of the model to use.
199
- number_densities (Any, optional): Number densities for the model.
200
- number_velocities (Any, optional): Number velocities for the model.
201
-
202
- Attributes:
203
- model_name (str): Name of the covariance model.
204
- model_kind (str): Type or kind of the model.
205
- free_par (Any): Free parameters for the model.
206
- los_definition (Any): Definition of the line-of-sight.
207
- covariance_dict (dict): Dictionary containing covariance data.
208
- matrix_form (bool): Indicates if matrix form is used.
209
- redshift_dict (dict): Dictionary containing redshift information.
210
- variant (Any): Variant of the model.
211
- coefficients (module): Imported coefficients module for the model.
212
- compute_covariance_sum (callable): Function to compute covariance sum.
213
- compute_covariance_sum_jit (callable): JIT-compiled function to compute covariance sum.
208
+ model_name (str, optional): Name of the covariance model.
209
+ model_kind (str, optional): Kind (`density`, `velocity`, `density_velocity`, `full`).
210
+ free_par (list[str], optional): Free parameters names for the model.
211
+ los_definition (str, optional): Line-of-sight definition.
212
+ covariance_dict (dict, optional): Covariance blocks.
213
+ matrix_form (bool, optional): Whether blocks are in matrix form.
214
+ variant (str, optional): Model variant.
215
+ coefficients (module, optional): Coefficients provider module.
216
+ number_densities (int, optional): Density block size.
217
+ number_velocities (int, optional): Velocity block size.
218
+ emulator_flag (bool, optional): Whether covariances are emulator callables.
214
219
  """
215
220
  self.model_name = model_name
216
221
  self.model_kind = model_kind
@@ -218,7 +223,6 @@ class CovMatrix:
218
223
  self.los_definition = los_definition
219
224
  self.covariance_dict = covariance_dict
220
225
  self.matrix_form = matrix_form
221
- self.redshift_dict = redshift_dict
222
226
  self.variant = variant
223
227
  self.coefficients = coefficients
224
228
  self.compute_covariance_sum = None
@@ -242,33 +246,27 @@ class CovMatrix:
242
246
  variant=None,
243
247
  **kwargs,
244
248
  ):
245
- """
246
- The init_from_flip function is a function that initializes the covariance matrix from the flip code.
247
- It takes as input:
248
- - model_name: name of the model used to generate the covariance matrix (e.g., 'lai22')
249
- - model_kind: kind of data used to generate the covariance matrix (e.g., 'density' or 'velocity')
250
- - power_spectrum_dict: dictionary containing all information about power spectrum, including k and P(k) values, redshift, etc...
251
- It is generated by calling getPowerSpectrumDict() in
249
+ """Initialize covariance from flip code generator.
252
250
 
253
251
  Args:
254
- cls: Indicate that the function is a class method
255
- model_name: Determine which model to use for the covariance matrix
256
- model_kind: Determine the kind of model to be used
257
- power_spectrum_dict: Pass the power spectrum of the model
258
- coordinates_density: Specify the coordinates of the density field
259
- coordinates_velocity: Define the velocity coordinates of the covariance matrix
260
- additional_parameters_values: Pass the values of additional parameters to the flip code
261
- **kwargs: Pass a variable number of keyword arguments to the function
252
+ model_name (str): Covariance model package.
253
+ model_kind (str): Kind (`density`, `velocity`, `density_velocity`, `full`).
254
+ power_spectrum_dict (dict): Power spectrum inputs for the model.
255
+ coordinates_density (array-like, optional): Density coordinates.
256
+ coordinates_velocity (array-like, optional): Velocity coordinates.
257
+ additional_parameters_values (tuple, optional): Extra parameters for generator.
258
+ los_definition (str): LOS choice; defaults to `bisector`.
259
+ variant (str): Model variant.
260
+ **kwargs: Extra generator options.
262
261
 
263
262
  Returns:
264
- A covariancematrix object
265
-
263
+ CovMatrix: Initialized covariance matrix in matrix form.
266
264
  """
267
265
  begin = time.time()
268
266
  from flip.covariance import generator as generator_flip
269
267
 
270
268
  _available_variants = importlib.import_module(
271
- f"flip.covariance.{model_name}"
269
+ f"flip.covariance.analytical.{model_name}"
272
270
  )._variant
273
271
  if variant not in _available_variants:
274
272
  raise ValueError(
@@ -278,14 +276,13 @@ class CovMatrix:
278
276
  free_par = _read_free_par(model_name, model_kind, variant=variant)
279
277
 
280
278
  coefficients = importlib.import_module(
281
- f"flip.covariance.{model_name}.coefficients"
279
+ f"flip.covariance.analytical.{model_name}.coefficients"
282
280
  )
283
281
 
284
282
  (
285
283
  covariance_dict,
286
284
  number_densities,
287
285
  number_velocities,
288
- redshift_dict,
289
286
  ) = generator_flip.generate_covariance(
290
287
  model_name,
291
288
  model_kind,
@@ -307,7 +304,6 @@ class CovMatrix:
307
304
  los_definition=los_definition,
308
305
  covariance_dict=covariance_dict,
309
306
  matrix_form=False,
310
- redshift_dict=redshift_dict,
311
307
  variant=variant,
312
308
  coefficients=coefficients,
313
309
  number_densities=number_densities,
@@ -326,35 +322,28 @@ class CovMatrix:
326
322
  variant=None,
327
323
  **kwargs,
328
324
  ):
329
- """
330
- The init_from_generator function is a helper function that allows the user to initialize
331
- a Covariance object from a generator. The init_from_generator function takes in as arguments:
332
- - cls: the class of the object being initialized (Covariance)
333
- - model_name: name of covariance model used to generate covariance matrix (e.g., 'lai22')
334
- - model_kind: kind of covariance matrix generated ('density' or 'velocity')
335
- - power spectrum dictionary containing keys for each redshift bin and values corresponding to
336
- power spectra at those red
325
+ """Initialize covariance from a model-local generator implementation.
337
326
 
338
327
  Args:
339
- cls: Refer to the class itself
340
- model_name: Specify the kind of model used to generate the covariance matrix
341
- model_kind: Determine which model to use
342
- power_spectrum_dict: Pass the power spectrum to the generate_* functions
343
- coordinates_velocity: Generate the velocity covariance matrix
344
- coordinates_density: Generate the density field
345
- additional_parameters_values: Pass additional parameters to the generator function
346
- **kwargs: Pass a variable number of keyword arguments to the function
347
- : Generate the covariance matrix from a given model
328
+ model_name (str): Covariance model package.
329
+ model_kind (str): Kind (`density`, `velocity`, `density_velocity`, `full`).
330
+ power_spectrum_dict (dict): Power spectrum inputs.
331
+ coordinates_velocity (array-like, optional): Velocity coordinates.
332
+ coordinates_density (array-like, optional): Density coordinates.
333
+ additional_parameters_values (tuple, optional): Extra generator params.
334
+ variant (str): Model variant.
335
+ **kwargs: Extra generator options.
348
336
 
349
337
  Returns:
350
- An object of the class covariancematrix
351
-
338
+ CovMatrix: Initialized covariance matrix in matrix form.
352
339
  """
353
340
  begin = time.time()
354
- generator = importlib.import_module(f"flip.covariance.{model_name}.generator")
341
+ generator = importlib.import_module(
342
+ f"flip.covariance.analytical.{model_name}.generator"
343
+ )
355
344
 
356
345
  _available_variants = importlib.import_module(
357
- f"flip.covariance.{model_name}"
346
+ f"flip.covariance.analytical.{model_name}"
358
347
  )._variant
359
348
  if variant not in _available_variants:
360
349
  raise ValueError(
@@ -364,7 +353,7 @@ class CovMatrix:
364
353
  free_par = _read_free_par(model_name, model_kind, variant=variant)
365
354
 
366
355
  coefficients = importlib.import_module(
367
- f"flip.covariance.{model_name}.coefficients"
356
+ f"flip.covariance.analytical.{model_name}.coefficients"
368
357
  )
369
358
 
370
359
  (
@@ -372,7 +361,6 @@ class CovMatrix:
372
361
  number_densities,
373
362
  number_velocities,
374
363
  los_definition,
375
- redshift_dict,
376
364
  ) = generator.generate_covariance(
377
365
  model_kind,
378
366
  power_spectrum_dict,
@@ -391,7 +379,6 @@ class CovMatrix:
391
379
  los_definition=los_definition,
392
380
  covariance_dict=covariance_dict,
393
381
  matrix_form=False,
394
- redshift_dict=redshift_dict,
395
382
  variant=variant,
396
383
  coefficients=coefficients,
397
384
  number_densities=number_densities,
@@ -408,7 +395,19 @@ class CovMatrix:
408
395
  parameter_names,
409
396
  **kwargs,
410
397
  ):
398
+ """Initialize covariance from an emulator over precomputed covariances.
399
+
400
+ Args:
401
+ emulator_model_name (str): Emulator model name.
402
+ model_kind (str): Kind (`density`, `velocity`, `density_velocity`, `full`).
403
+ covariance_list (list[CovMatrix]): Base covariances forming the grid.
404
+ emulator_parameter_values (array-like): Emulator parameter vector.
405
+ parameter_names (list[str]): Names aligned with emulator parameters.
406
+ **kwargs: Extra emulator options.
411
407
 
408
+ Returns:
409
+ CovMatrix: Covariance matrix with emulator-backed blocks.
410
+ """
412
411
  begin = time.time()
413
412
  from flip.covariance.emulators import generator as generator_emulators
414
413
 
@@ -431,7 +430,6 @@ class CovMatrix:
431
430
  los_definition=covariance_list[0].los_definition,
432
431
  covariance_dict=emulator_covariance_dict,
433
432
  matrix_form=False,
434
- redshift_dict=covariance_list[0].redshift_dict,
435
433
  variant=covariance_list[0].variant,
436
434
  coefficients=covariance_list[0].coefficients,
437
435
  number_densities=covariance_list[0].number_densities,
@@ -445,8 +443,20 @@ class CovMatrix:
445
443
  filename,
446
444
  file_format,
447
445
  ):
446
+ """Load a CovMatrix from file.
447
+
448
+ Args:
449
+ filename (str): Path without extension.
450
+ file_format (str): One of `pickle`, `npz` (parquet not yet implemented).
451
+
452
+ Returns:
453
+ CovMatrix: Loaded covariance matrix.
454
+
455
+ Raises:
456
+ NotImplementedError: For parquet reading.
457
+ """
448
458
  if file_format == "parquet":
449
- raise NotImplementedError(f"Reading from parquet not implemented yet")
459
+ raise NotImplementedError("Reading from parquet not implemented yet")
450
460
  if file_format == "pickle":
451
461
  with open(f"{filename}.pickle", "rb") as file_read:
452
462
  class_attrs_dictionary = pickle.load(file_read)
@@ -461,19 +471,10 @@ class CovMatrix:
461
471
 
462
472
  @property
463
473
  def kind(self):
464
- """
465
- The kind function is used to determine the kind of covariance model that will be computed.
466
- The options are:
467
- - velocity: The covariance model is computed for velocity only.
468
- - density: The covariance model is computed for density only.
469
- - density_velocity: The covariance model is computed for both velocity and density, without cross-term (i.e., the covariances between velocities and densities are zero). This option should be used when computing a full 3D tomography in which we want to compute a separate 1D tomography along each axis (x, y, z
470
-
471
- Args:
472
- self: Represent the instance of the class
474
+ """Return and log the covariance model kind.
473
475
 
474
476
  Returns:
475
- The kind of the model
476
-
477
+ str: One of `velocity`, `density`, `density_velocity`, `full`.
477
478
  """
478
479
  if self.model_kind == "velocity":
479
480
  log.add("The covariance model is computed for velocity")
@@ -491,15 +492,10 @@ class CovMatrix:
491
492
 
492
493
  @property
493
494
  def loaded(self):
494
- """
495
- The loaded function checks if the covariance matrix is loaded.
496
-
497
- Args:
498
- self: Refer to the object itself
495
+ """Check that required covariance blocks are present for the kind.
499
496
 
500
497
  Returns:
501
- A boolean
502
-
498
+ bool: True if the covariance has necessary blocks, False otherwise.
503
499
  """
504
500
  if self.model_kind == "density":
505
501
  if "gg" in self.covariance_dict.keys():
@@ -532,6 +528,11 @@ class CovMatrix:
532
528
  return False
533
529
 
534
530
  def init_compute_covariance_sum(self):
531
+ """Prepare functions to compute covariance sums.
532
+
533
+ Ensures matrix form, binds coefficient accessors, and defines (optionally
534
+ JIT-compiled) functions combining blocks and data variance.
535
+ """
535
536
  if not self.matrix_form and not self.emulator_flag:
536
537
  self.compute_matrix_covariance()
537
538
 
@@ -539,7 +540,6 @@ class CovMatrix:
539
540
  self.coefficients.get_coefficients,
540
541
  model_kind=self.model_kind,
541
542
  variant=self.variant,
542
- redshift_dict=self.redshift_dict,
543
543
  )
544
544
 
545
545
  get_diagonal_coefficients = partial(
@@ -553,13 +553,33 @@ class CovMatrix:
553
553
  kind=self.model_kind,
554
554
  )
555
555
 
556
- def _compute_covariance_sum(parameter_values_dict, vector_variance):
557
- coefficients_dict = get_coefficients(
558
- parameter_values_dict=parameter_values_dict
559
- )
556
+ def _compute_covariance_sum(
557
+ parameter_values_dict,
558
+ vector_variance,
559
+ covariance_prefactor_dict=None,
560
+ ):
561
+ """Compute total covariance given parameters and data variance.
562
+
563
+ Args:
564
+ parameter_values_dict (dict): Parameter values for coefficients.
565
+ vector_variance (array-like): Data variance vector or matrix.
566
+ covariance_prefactor_dict (dict, optional): Prefactors per block.
567
+
568
+ Returns:
569
+ array-like: Total covariance matrix.
570
+ """
571
+ coefficients_dict = {
572
+ k: jnp.array(v)
573
+ for k, v in get_coefficients(
574
+ parameter_values_dict=parameter_values_dict,
575
+ covariance_prefactor_dict=covariance_prefactor_dict,
576
+ ).items()
577
+ }
578
+
560
579
  coefficients_dict_diagonal = get_diagonal_coefficients(
561
- parameter_values_dict=parameter_values_dict
580
+ parameter_values_dict=parameter_values_dict,
562
581
  )
582
+
563
583
  covariance_sum = compute_covariance_sum_fun(
564
584
  coefficients_dict=coefficients_dict,
565
585
  coefficients_dict_diagonal=coefficients_dict_diagonal,
@@ -580,25 +600,32 @@ class CovMatrix:
580
600
  self,
581
601
  parameter_values_dict,
582
602
  vector_variance,
603
+ covariance_prefactor_dict=None,
583
604
  ):
605
+ """Return eigenvalues of the covariance sum for diagnostics.
606
+
607
+ Args:
608
+ parameter_values_dict (dict): Parameter values.
609
+ vector_variance (array-like): Data variance.
610
+ covariance_prefactor_dict (dict, optional): Prefactors per block.
611
+
612
+ Returns:
613
+ numpy.ndarray: Eigenvalues of `C`.
614
+ """
584
615
  covariance_sum = self.compute_covariance_sum(
585
616
  parameter_values_dict,
586
617
  vector_variance,
618
+ covariance_prefactor_dict=covariance_prefactor_dict,
587
619
  )
588
620
  return np.linalg.eigvals(covariance_sum)
589
621
 
590
622
  # CR - the two next functions should be more general (covariance_type[0] != covariance_type[1] case or not)
591
623
 
592
624
  def compute_matrix_covariance(self, verbose=True):
593
- """
594
- The compute_matrix_covariance function takes the covariance matrix and fills in all of the missing values.
595
-
596
- Args:
597
- self: Bind the method to the object
598
-
599
- Returns:
600
- A dictionary with the full covariance matrices for each redshift bin
625
+ """Convert flat covariance vectors to full matrix blocks.
601
626
 
627
+ For each block (`gg`, `gv`, `vv`), reconstruct the 2D matrices from their
628
+ flattened representation and set `matrix_form=True`.
602
629
  """
603
630
  if self.matrix_form:
604
631
  if verbose:
@@ -645,12 +672,15 @@ class CovMatrix:
645
672
  new_cov[i] = cov_utils.return_matrix_covariance(
646
673
  self.covariance_dict[key][i]
647
674
  )
648
- self.covariance_dict[key] = new_cov
675
+ self.covariance_dict[key] = jnp.array(new_cov)
649
676
 
650
677
  self.matrix_form = True
651
678
 
652
679
  def compute_flat_covariance(self, verbose=True):
680
+ """Convert full matrix blocks back to flat vector representation.
653
681
 
682
+ Updates `covariance_dict` blocks and sets `matrix_form=False`.
683
+ """
654
684
  if not self.matrix_form:
655
685
  if verbose:
656
686
  log.add("Flat covariance already computed")
@@ -695,17 +725,11 @@ class CovMatrix:
695
725
  filename,
696
726
  file_format,
697
727
  ):
698
- """
699
- The write function writes the covariance matrix to a file.
728
+ """Write the covariance matrix to disk.
700
729
 
701
730
  Args:
702
- self: Represent the instance of the class
703
- filename: Specify the name of the file to be written
704
- : Specify the name of the file in which we want to save our covariance matrix
705
-
706
- Returns:
707
- Nothing
708
-
731
+ filename (str): Output path without extension.
732
+ file_format (str): One of `parquet`, `pickle`, `npz`.
709
733
  """
710
734
  class_attrs_dictionary = {
711
735
  key: eval(f"self.{key}", {"self": self}) for key in vars(self)
@@ -739,10 +763,18 @@ class CovMatrix:
739
763
  np.savez(f"{filename}.npz", **class_attrs_dictionary)
740
764
 
741
765
  def mask(self, mask_vel=None, mask_dens=None):
766
+ """Return a masked copy of the covariance restricting indices.
742
767
 
743
- Ng = self.number_densities
744
- Nv = self.number_velocities
768
+ Args:
769
+ mask_vel (array-like, optional): Boolean mask for velocity indices.
770
+ mask_dens (array-like, optional): Boolean mask for density indices.
745
771
 
772
+ Returns:
773
+ CovMatrix: Masked covariance object.
774
+
775
+ Raises:
776
+ ValueError: If no mask is provided or sizes mismatch.
777
+ """
746
778
  if mask_vel is None and mask_dens is None:
747
779
  raise ValueError("No mask set")
748
780
 
@@ -820,6 +852,5 @@ class CovMatrix:
820
852
  matrix_form=self.matrix_form,
821
853
  number_densities=np.sum(mask_dens),
822
854
  number_velocities=np.sum(mask_vel),
823
- redshift_dict=self.redshift_dict,
824
855
  variant=self.variant,
825
856
  )
@@ -1,3 +1,3 @@
1
1
  """Init file of the flip.covariance.emulators package."""
2
2
 
3
- from . import generator, gpmatrix, nnmatrix
3
+ from . import generator, gpmatrix, nnmatrix, skgpmatrix