Skip to content

Commit 25842b1

Browse files
committed
Capturing warnings.
1 parent 0541359 commit 25842b1

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

firecrown/app/sacc/_load.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Load command for SACC files."""
22

33
import dataclasses
4+
import contextlib
5+
import io
6+
import warnings
47
from pathlib import Path
58
from typing import Annotated
69

@@ -41,7 +44,14 @@ def _load_sacc_file(self) -> None:
4144
try:
4245
if not self.sacc_file.exists():
4346
raise typer.BadParameter(f"SACC file not found: {self.sacc_file}")
44-
self.sacc_data = factories.load_sacc_data(self.sacc_file.as_posix())
47+
48+
with (
49+
contextlib.redirect_stdout(io.StringIO()),
50+
contextlib.redirect_stderr(io.StringIO()),
51+
warnings.catch_warnings(),
52+
):
53+
warnings.simplefilter("ignore")
54+
self.sacc_data = factories.load_sacc_data(self.sacc_file.as_posix())
4555
except Exception as e:
4656
self.console.print(f"[bold red]Failed to load SACC file:[/bold red] {e}")
4757
raise

tests/likelihood/gauss_family/test_const_gaussianPM.py

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ def test_compute_chisq_fails_before_read(trivial_stats):
214214
"""Note that the error message from the direct call to compute_chisq notes
215215
that update() must be called; this can only be called after read()."""
216216
likelihood = ConstGaussianPM(statistics=trivial_stats)
217-
with pytest.raises(
218-
AssertionError,
219-
match=re.escape("update() must be called before compute_chisq()"),
217+
with (
218+
pytest.raises(
219+
AssertionError,
220+
match=re.escape("update() must be called before compute_chisq()"),
221+
),
220222
):
221223
_ = likelihood.compute_chisq(ModelingTools())
222224

@@ -225,7 +227,11 @@ def test_compute_chisq(trivial_stats, sacc_data_for_trivial_stat, trivial_params
225227
likelihood = ConstGaussianPM(statistics=trivial_stats)
226228
likelihood.read(sacc_data_for_trivial_stat)
227229
likelihood.update(trivial_params)
228-
assert likelihood.compute_chisq(ModelingTools()) == 2.0
230+
with pytest.warns(
231+
UserWarning,
232+
match=re.escape("inverse covariance correction has not yet been computed."),
233+
):
234+
assert likelihood.compute_chisq(ModelingTools()) == 2.0
229235

230236

231237
def test_deprecated_compute(trivial_stats, sacc_data_for_trivial_stat, trivial_params):
@@ -256,7 +262,11 @@ def compute(
256262
likelihood.compute_theory_vector = compute_theory_vector # type: ignore
257263
likelihood.compute = compute # type: ignore
258264

259-
assert likelihood.compute_chisq(ModelingTools()) == 2.0
265+
with pytest.warns(
266+
UserWarning,
267+
match=re.escape("inverse covariance correction has not yet been computed."),
268+
):
269+
assert likelihood.compute_chisq(ModelingTools()) == 2.0
260270

261271

262272
def test_required_parameters(trivial_stats, sacc_data_for_trivial_stat, trivial_params):
@@ -282,7 +292,11 @@ def test_reset(trivial_stats, sacc_data_for_trivial_stat, trivial_params):
282292
likelihood.read(sacc_data_for_trivial_stat)
283293
likelihood.update(trivial_params)
284294
assert not trivial_stats[0].computed_theory_vector
285-
assert likelihood.compute_loglike(ModelingTools()) == -1.0
295+
with pytest.warns(
296+
UserWarning,
297+
match=re.escape("inverse covariance correction has not yet been computed."),
298+
):
299+
assert likelihood.compute_loglike(ModelingTools()) == -1.0
286300
assert trivial_stats[0].computed_theory_vector
287301
likelihood.reset()
288302
assert not trivial_stats[0].computed_theory_vector
@@ -307,7 +321,11 @@ def test_using_good_sacc(
307321
likelihood.read(sacc_data_for_trivial_stat)
308322
params = firecrown.parameters.ParamsMap(mean=10.5)
309323
likelihood.update(params)
310-
chisq = likelihood.compute_chisq(tools_with_vanilla_cosmology)
324+
with pytest.warns(
325+
UserWarning,
326+
match=re.escape("inverse covariance correction has not yet been computed."),
327+
):
328+
chisq = likelihood.compute_chisq(tools_with_vanilla_cosmology)
311329
assert isinstance(chisq, float)
312330
assert chisq > 0.0
313331

@@ -331,15 +349,23 @@ def test_make_realization_chisq(
331349
likelihood.read(sacc_data_for_trivial_stat)
332350
params = firecrown.parameters.ParamsMap(mean=10.5)
333351
likelihood.update(params)
334-
likelihood.compute_chisq(tools_with_vanilla_cosmology)
352+
with pytest.warns(
353+
UserWarning,
354+
match=re.escape("inverse covariance correction has not yet been computed."),
355+
):
356+
likelihood.compute_chisq(tools_with_vanilla_cosmology)
335357

336358
new_sacc = likelihood.make_realization(sacc_data_for_trivial_stat)
337359

338360
new_likelihood = ConstGaussianPM(statistics=[TrivialStatistic()])
339361
new_likelihood.read(new_sacc)
340362
params = firecrown.parameters.ParamsMap(mean=10.5)
341363
new_likelihood.update(params)
342-
chisq = new_likelihood.compute_chisq(tools_with_vanilla_cosmology)
364+
with pytest.warns(
365+
UserWarning,
366+
match=re.escape("inverse covariance correction has not yet been computed."),
367+
):
368+
chisq = new_likelihood.compute_chisq(tools_with_vanilla_cosmology)
343369

344370
# The new likelihood chisq is distributed as a chi-squared with 3 degrees of
345371
# freedom. We want to check that the new chisq is within the 1-10^-6 quantile
@@ -358,7 +384,11 @@ def test_make_realization_chisq_mean(
358384
likelihood.read(sacc_data_for_trivial_stat)
359385
params = firecrown.parameters.ParamsMap(mean=10.5)
360386
likelihood.update(params)
361-
likelihood.compute_chisq(tools_with_vanilla_cosmology)
387+
with pytest.warns(
388+
UserWarning,
389+
match=re.escape("inverse covariance correction has not yet been computed."),
390+
):
391+
likelihood.compute_chisq(tools_with_vanilla_cosmology)
362392

363393
chisq_list = []
364394
for _ in range(1000):
@@ -368,7 +398,11 @@ def test_make_realization_chisq_mean(
368398
new_likelihood.read(new_sacc)
369399
params = firecrown.parameters.ParamsMap(mean=10.5)
370400
new_likelihood.update(params)
371-
chisq = new_likelihood.compute_chisq(tools_with_vanilla_cosmology)
401+
with pytest.warns(
402+
UserWarning,
403+
match=re.escape("inverse covariance correction has not yet been computed."),
404+
):
405+
chisq = new_likelihood.compute_chisq(tools_with_vanilla_cosmology)
372406
chisq_list.append(chisq)
373407

374408
# The new likelihood chisq is distributed as a chi-squared with 3 degrees of
@@ -387,7 +421,11 @@ def test_make_realization_data_vector(
387421
likelihood.read(sacc_data_for_trivial_stat)
388422
params = firecrown.parameters.ParamsMap(mean=10.5)
389423
likelihood.update(params)
390-
likelihood.compute_chisq(tools_with_vanilla_cosmology)
424+
with pytest.warns(
425+
UserWarning,
426+
match=re.escape("inverse covariance correction has not yet been computed."),
427+
):
428+
likelihood.compute_chisq(tools_with_vanilla_cosmology)
391429

392430
data_vector_list = []
393431
for _ in range(1000):
@@ -430,7 +468,11 @@ def test_make_realization_no_noise(
430468
likelihood.read(sacc_data_for_trivial_stat)
431469
params = firecrown.parameters.ParamsMap(mean=10.5)
432470
likelihood.update(params)
433-
likelihood.compute_chisq(tools_with_vanilla_cosmology)
471+
with pytest.warns(
472+
UserWarning,
473+
match=re.escape("inverse covariance correction has not yet been computed."),
474+
):
475+
likelihood.compute_chisq(tools_with_vanilla_cosmology)
434476

435477
new_sacc = likelihood.make_realization(sacc_data_for_trivial_stat, add_noise=False)
436478

@@ -561,8 +603,8 @@ def __init__(self, statistic):
561603
self.statistic = statistic
562604

563605

564-
@pytest.fixture
565-
def minimal_const_gaussian_PM():
606+
@pytest.fixture(name="minimal_const_gaussian_PM")
607+
def fixture_minimal_const_gaussian_PM() -> ConstGaussianPM:
566608
# Create minimal valid statistics for the class.
567609
z = np.array([0.1, 0.2, 0.3])
568610
dndz = np.array([1.0, 2.0, 3.0])
@@ -601,6 +643,9 @@ def minimal_const_gaussian_PM():
601643
return likelihood
602644

603645

646+
# pylint: disable=protected-access
647+
648+
604649
def test_precomputed_warning(minimal_const_gaussian_PM):
605650
# Check that running the precomputation twice gives a warning.
606651
minimal_const_gaussian_PM._generate_maps()
@@ -818,3 +863,6 @@ def __init__(self):
818863

819864
with pytest.raises(StopIteration, match="missing attributes"):
820865
likelihood._collect_data_vectors()
866+
867+
868+
# pylint enable=protected-access

0 commit comments

Comments
 (0)