From 4a29896837abb3906833f9d5d0696e061698c74d Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Wed, 11 Mar 2020 17:28:42 -0400 Subject: [PATCH] Fix test failures with older Numpy/Astropy. Make integrate methods in models.py consistent. Add tests. --- synphot/models.py | 25 +++++++++++++++++-------- synphot/spectrum.py | 2 +- synphot/tests/test_integrator.py | 8 ++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/synphot/models.py b/synphot/models.py index 8b125792..fb173867 100644 --- a/synphot/models.py +++ b/synphot/models.py @@ -136,8 +136,10 @@ def evaluate(x, temperature): return bbflux.value def integrate(self, *args): - return (const.sigma_sb * (u.Quantity(self.temperature, u.K) ** 4) / - math.pi) # per steradian + with u.add_enabled_equivalencies(u.temperature()): + t = u.Quantity(self.temperature, u.K) + + return (const.sigma_sb * t ** 4 / math.pi) # per steradian class BlackBodyNorm1D(BlackBody1D): @@ -225,7 +227,10 @@ def sampleset(self, step=0.01, minimal=False): def integrate(self, *args): # TODO: Remove unit hardcoding when we use model with units natively. - return self.amplitude * (self.width * u.AA) + with u.add_enabled_equivalencies(u.spectral()): + w = u.Quantity(self.width, u.AA) + + return self.amplitude * w class ConstFlux1D(_models.Const1D): @@ -290,8 +295,9 @@ def integrate(self, x): wav_unit = u.Hz with u.add_enabled_equivalencies(u.spectral()): x = u.Quantity(x, wav_unit) + amp = u.Quantity(self.amplitude, self._flux_unit) - return (max(x) - min(x)) * (self.amplitude * self._flux_unit) + return (max(x) - min(x)) * amp class Empirical1D(Tabular1D): @@ -474,9 +480,12 @@ class Gaussian1D(BaseGaussian1D): ``sampleset`` defined. """ - # TODO: Remove unit hardcoding when we use model with units natively. def integrate(self, *args): - return self.amplitude * (self.stddev * u.AA) * self._sqrt_2_pi + # TODO: Remove unit hardcoding when we use model with units natively. + with u.add_enabled_equivalencies(u.spectral()): + stddev = u.Quantity(self.stddev, u.AA) + + return self.amplitude * stddev * self._sqrt_2_pi # TODO: Deprecate this? @@ -559,8 +568,8 @@ def __init__(self, *args, **kwargs): self.meta['expr'] = 'em({0:g}, {1:g}, {2:g}, {3})'.format( self.mean.value, fwhm, total_flux, u_str) - # TODO: Remove unit hardcoding when we use model with units natively. def integrate(self, *args): + # TODO: Remove unit hardcoding when we use model with units natively. return super(GaussianFlux1D, self).integrate(*args) * units.PHOTLAM @@ -592,8 +601,8 @@ def sampleset(self, factor_step=0.05, **kwargs): return np.asarray(w) - # TODO: Remove unit hardcoding when we use model with units natively. def integrate(self, x): + # TODO: Remove unit hardcoding when we use model with units natively. with u.add_enabled_equivalencies(u.spectral()): x = u.Quantity(x, u.AA) x_0 = u.Quantity(self.x_0, u.AA) diff --git a/synphot/spectrum.py b/synphot/spectrum.py index 99d60c65..8f161f71 100644 --- a/synphot/spectrum.py +++ b/synphot/spectrum.py @@ -1539,7 +1539,7 @@ def check_overlap(self, other, wavelengths=None, threshold=0.01): other.model.is_tapered() or not isinstance(other.model, (Empirical1D, CompoundModel))) and - np.allclose(other(x1[::x1.size - 1]), 0)): + np.allclose(other(x1[::x1.size - 1]).value, 0)): result = 'full' # Check if the lack of overlap is significant. diff --git a/synphot/tests/test_integrator.py b/synphot/tests/test_integrator.py index 55c86948..82f5f5e6 100644 --- a/synphot/tests/test_integrator.py +++ b/synphot/tests/test_integrator.py @@ -157,6 +157,14 @@ def test_bandpass_RickerWavelet1D(): assert_quantity_allclose(bp.equivwidth(integration_type='analytical'), ans, rtol=5e-3) + with pytest.raises(NotImplementedError, match='Partial analytic'): + bp.equivwidth(integration_type='analytical', + wavelengths=np.arange(4900, 5200) * u.AA) + + with pytest.raises(NotImplementedError, match='Partial analytic'): + bp.equivwidth(integration_type='analytical', + wavelengths=np.arange(4800, 5100) * u.AA) + def test_source_RickerWavelet1D(): pytest.xfail(