Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpectralUnitsWarning for more targeted warning filters #239

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/dust_extinction/fit_extinction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extinction curve for the LMC outside of the LMC2 supershell region

from dust_extinction.averages import G03_LMCAvg
from dust_extinction.shapes import FM90
from dust_extinction.warnings import SpectralUnitsWarning

# get an observed extinction curve to fit
g03_model = G03_LMCAvg()
Expand All @@ -49,9 +50,9 @@ extinction curve for the LMC outside of the LMC2 supershell region
# use the initialized model as the starting point

# ignore some warnings
# UserWarning is to avoid the units of x warning
# SpectralUnitsWarning is to avoid the units of x warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
warnings.simplefilter("ignore", category=SpectralUnitsWarning)
g03_fit = fit(fm90_init, x[gindxs].value, y[gindxs])

# plot the observed data, initial guess, and final fit
Expand Down Expand Up @@ -100,6 +101,7 @@ between data points.

from dust_extinction.averages import GCC09_MWAvg
from dust_extinction.shapes import P92
from dust_extinction.warnings import SpectralUnitsWarning

# get an observed extinction curve to fit
g09_model = GCC09_MWAvg()
Expand Down Expand Up @@ -134,11 +136,11 @@ between data points.
# accuracy set to avoid warning the fit may have failed

# ignore some warnings
# UserWarning is to avoid the units of x warning
# SpectralUnitsWarning is to avoid the units of x warning
# AstropyWarning ignored to avoid the "fit may have been unsuccessful" warning
# fit is fine, but this means the build of the docs fails
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
warnings.simplefilter("ignore", category=SpectralUnitsWarning)
warnings.simplefilter("ignore", category=AstropyWarning)
p92_fit = fit(p92_init, x.value, y, weights=1.0 / y_unc)

Expand Down
5 changes: 4 additions & 1 deletion dust_extinction/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from scipy.special import comb
import astropy.units as u

from .warnings import SpectralUnitsWarning
karllark marked this conversation as resolved.
Show resolved Hide resolved

__all__ = ["_get_x_in_wavenumbers", "_test_valid_x_range", "_smoothstep"]


Expand All @@ -28,7 +30,8 @@ def _get_x_in_wavenumbers(in_x):
# check if in_x is an astropy quantity, if not issue a warning
if not isinstance(in_x, u.Quantity):
warnings.warn(
"x has no units, assuming x units are inverse microns", UserWarning
"x has no units, assuming x units are inverse microns",
SpectralUnitsWarning
)

# convert to wavenumbers (1/micron) if x input in units
Expand Down
4 changes: 3 additions & 1 deletion dust_extinction/tests/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ave_models,
grain_models,
)
from ..warnings import SpectralUnitsWarning
karllark marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize("model", all_models)
Expand All @@ -22,7 +23,8 @@ def test_nounits_warning(model):
x = np.arange(ext.x_range[0], ext.x_range[1], 0.1)

with pytest.warns(
UserWarning, match="x has no units, assuming x units are inverse microns"
SpectralUnitsWarning,
match="x has no units, assuming x units are inverse microns"
):
ext(x)

Expand Down
2 changes: 2 additions & 0 deletions dust_extinction/warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class SpectralUnitsWarning(UserWarning):
pass
Loading