Skip to content

Commit

Permalink
Merge branch 'main' into fix_ska_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaBreitman authored Feb 12, 2025
2 parents ca8ae8f + c79681f commit 001838e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.3
rev: v0.9.6
hooks:
# Run the linter.
- id: ruff
Expand Down
30 changes: 30 additions & 0 deletions tests/test_sensitivity.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
"""Test the sensitivity module."""

import warnings
from functools import wraps

import numpy as np
import pytest
from astropy import units
from astropy.cosmology.units import littleh
from spiceypy.utils.exceptions import SpiceUNKNOWNFRAME

from py21cmsense import GaussianBeam, Observation, Observatory, PowerSpectrum, theory
from py21cmsense.sensitivity import Sensitivity


def skip_on(exception, reason="Default reason"):
# Func below is the real decorator and will receive the test function as param
def decorator_func(f):
@wraps(f)
def wrapper(*args, **kwargs):
try:
# Try to run the test
return f(*args, **kwargs)
except exception:
# If exception of given type happens
# just swallow it and raise pytest.Skip with given reason
pytest.skip(reason)

return wrapper

return decorator_func


@pytest.fixture(scope="module")
def bm():
return GaussianBeam(150.0 * units.MHz, dish_size=14 * units.m)
Expand Down Expand Up @@ -44,6 +64,7 @@ def test_units(observation):
assert ps.horizon_limit(10).to("littleh/Mpc").unit == littleh / units.Mpc


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_sensitivity_2d(observation):
ps = PowerSpectrum(observation=observation)
sense_thermal = ps.calculate_sensitivity_2d(thermal=True, sample=False)
Expand All @@ -54,6 +75,7 @@ def test_sensitivity_2d(observation):
ps.calculate_sensitivity_2d(thermal=False, sample=False)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_sensitivity_2d_grid(observation, caplog):
ps = PowerSpectrum(observation=observation)
sense_ungridded = ps.calculate_sensitivity_2d(thermal=True, sample=True)
Expand All @@ -65,6 +87,7 @@ def test_sensitivity_2d_grid(observation, caplog):
assert sense.shape == (9, len(ps.k1d) - 1)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_sensitivity_1d_binned(observation):
ps = PowerSpectrum(observation=observation)
assert np.all(ps.calculate_sensitivity_1d() == ps.calculate_sensitivity_1d_binned(ps.k1d))
Expand All @@ -73,23 +96,27 @@ def test_sensitivity_1d_binned(observation):
assert len(sense1d_sample) == len(kbins)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_plots(observation):
# this is a dumb test, just checking that it doesn't error.
ps = PowerSpectrum(observation=observation)
sense2d = ps.calculate_sensitivity_2d()
ps.plot_sense_2d(sense2d)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_sensitivity_optimistic(observation):
ps = PowerSpectrum(observation=observation, foreground_model="optimistic")
assert ps.horizon_limit(10.0) > ps.horizon_limit(5.0)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_sensitivity_foreground_free(observation):
ps = PowerSpectrum(observation=observation, foreground_model="foreground_free")
assert ps.horizon_limit(10.0) == 0


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_infs_in_trms(observation):
# default dumb layout should have lots of infs..
assert np.any(np.isinf(observation.Trms))
Expand Down Expand Up @@ -131,6 +158,7 @@ def test_load_yaml_bad():
)


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_systematics_mask(observation):
ps = PowerSpectrum(
observation=observation,
Expand All @@ -139,6 +167,7 @@ def test_systematics_mask(observation):
assert len(ps.calculate_sensitivity_2d()) == 0


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_track(observatory):
"""Test that setting `track` is the same as setting lst_bin_width."""
obs1 = Observation(observatory=observatory, lst_bin_size=1 * units.hour)
Expand All @@ -156,6 +185,7 @@ def test_clone(observation):
assert ps2 == ps


@skip_on(SpiceUNKNOWNFRAME, "Unknown FRAME (flaky exception)")
def test_at_freq(observation):
ps = PowerSpectrum(observation=observation, theory_model=theory.EOS2016Bright())
ps2 = ps.at_frequency(0.9 * observation.frequency)
Expand Down

0 comments on commit 001838e

Please sign in to comment.