Skip to content

Commit

Permalink
Merge pull request #239 from lpsinger/SpectralUnitsWarning
Browse files Browse the repository at this point in the history
Add SpectralUnitsWarning for more targeted warning filters
  • Loading branch information
karllark authored Sep 13, 2024
2 parents 8871ed6 + 412cbfc commit d2e54ba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
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

__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


@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

0 comments on commit d2e54ba

Please sign in to comment.