Skip to content

Commit

Permalink
Merge 8742f9e into 5d4c3fb
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnperera committed Jul 25, 2019
2 parents 5d4c3fb + 8742f9e commit 3f8a5ec
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Each of the width analysis functions are applied to this spectrum below:
>>> gaussian_fwhm(noisy_gaussian) # doctest: +FLOAT_CMP
<Quantity 1.81144683 GHz>
>>> fwhm(noisy_gaussian) # doctest: +FLOAT_CMP
<Quantity 1.90954774 GHz>
<Quantity 1.91686888 GHz>
>>> fwzi(noisy_gaussian) # doctest: +FLOAT_CMP
<Quantity 89.99999455 GHz>
Expand Down
2 changes: 1 addition & 1 deletion docs/fitting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Then estimate the line parameters it it for a Gaussian line profile::
amplitude mean stddev
Jy um um
------------------ ---------------- ------------------
1.1845669151078486 4.57517271067525 0.3015075376884422
1.1845669151078486 4.57517271067525 0.3199324375933905


If an `~astropy.modeling.Model` is used that does not have the predefined
Expand Down
49 changes: 37 additions & 12 deletions specutils/analysis/width.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,51 @@ def _compute_gaussian_sigma_width(spectrum, regions=None):


def _compute_single_fwhm(flux, spectral_axis):
"""
This is a helper function for the above `fwhm()` method.
"""

argmax = np.argmax(flux)
halfval = flux[argmax] / 2
# The .value attribute is used here as the following algorithm does not
# use any array operations and would otherwise introduce a relatively
# significant overhead factor. Two-point linear interpolation is used to
# acheive sub-pixel precision.
flux_value = flux.value
spectral_value = spectral_axis.value

left = flux[:argmax] <= halfval
right = flux[argmax+1:] <= halfval
argmax = flux_value.argmax()
halfval = flux_value[argmax] / 2
left = flux_value[:argmax] < halfval
right = flux_value[argmax + 1:] < halfval

# Highest signal at the first point
if np.sum(left) == 0:
l_idx = 0
i0 = np.nonzero(left)[0]
if i0.size == 0:
left_value = spectral_value[0]
else:
l_idx = np.where(left == True)[0][-1]
i0 = i0[-1]
i1 = i0 + 1
left_flux = flux_value[i0]
left_spectral = spectral_value[i0]
left_value = ((halfval - left_flux)
* (spectral_value[i1] - left_spectral)
/ (flux_value[i1] - left_flux)
+ left_spectral)

# Highest signal at the last point
if np.sum(right) == 0:
r_idx = len(flux)-1
i1 = np.nonzero(right)[0]
if i1.size == 0:
right_value = spectral_value[-1]
else:
r_idx = np.where(right == True)[0][0] + argmax

return spectral_axis[r_idx] - spectral_axis[l_idx]
i1 = i1[0] + argmax + 1
i0 = i1 - 1
left_flux = flux_value[i0]
left_spectral = spectral_value[i0]
right_value = ((halfval - left_flux)
* (spectral_value[i1] - left_spectral)
/ (flux_value[i1] - left_flux)
+ left_spectral)

return spectral_axis.unit * (right_value - left_value)


def _compute_fwhm(spectrum, regions=None):
Expand Down
6 changes: 5 additions & 1 deletion specutils/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ def test_fwhm():

spectrum = Spectrum1D(spectral_axis=wavelengths, flux=flux)
result = fwhm(spectrum)
assert result == 0.9090909090909092*u.um
# Note that this makes a little more sense than the previous version;
# since the maximum value occurs at wavelength=1, and the half-value of
# flux (0.5) occurs at exactly wavelength=2, the result should be
# exactly 1 (2 - 1).
assert result == 1.0*u.um

# Highest point at the last point
wavelengths = np.linspace(1, 10, 100) * u.um
Expand Down
10 changes: 5 additions & 5 deletions specutils/tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_single_peak_estimate():

assert np.isclose(g_init.amplitude.value, 3.354169257846847)
assert np.isclose(g_init.mean.value, 6.218588636687762)
assert np.isclose(g_init.stddev.value, 1.608040201005025)
assert np.isclose(g_init.stddev.value, 1.6339001193853715)

assert g_init.amplitude.unit == u.Jy
assert g_init.mean.unit == u.um
Expand All @@ -120,7 +120,7 @@ def test_single_peak_estimate():

assert np.isclose(g_init.amplitude.value, 3.354169257846847)
assert np.isclose(g_init.x_0.value, 6.218588636687762)
assert np.isclose(g_init.fwhm.value, 1.608040201005025)
assert np.isclose(g_init.fwhm.value, 1.6339001193853715)

assert g_init.amplitude.unit == u.Jy
assert g_init.x_0.unit == u.um
Expand All @@ -134,8 +134,8 @@ def test_single_peak_estimate():

assert np.isclose(g_init.amplitude_L.value, 3.354169257846847)
assert np.isclose(g_init.x_0.value, 6.218588636687762)
assert np.isclose(g_init.fwhm_L.value, 1.1370561305512321)
assert np.isclose(g_init.fwhm_G.value, 1.1370561305512321)
assert np.isclose(g_init.fwhm_L.value, 1.1553418541989058)
assert np.isclose(g_init.fwhm_G.value, 1.1553418541989058)

assert g_init.amplitude_L.unit == u.Jy
assert g_init.x_0.unit == u.um
Expand All @@ -158,7 +158,7 @@ def test_single_peak_estimate():

assert np.isclose(g_init.amplitude.value, 3.354169257846847)
assert np.isclose(g_init.x_0.value, 6.218588636687762)
assert np.isclose(g_init.stddev.value, 1.608040201005025)
assert np.isclose(g_init.stddev.value, 1.6339001193853715)

assert g_init.amplitude.unit == u.Jy
assert g_init.x_0.unit == u.um
Expand Down

0 comments on commit 3f8a5ec

Please sign in to comment.