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

Update spectrum class name from Spectrum1D to Spectrum #1126

Merged
merged 17 commits into from
Mar 28, 2024
Merged
Prev Previous commit
Next Next commit
Update name in fitting and manipulation
rosteen committed Mar 25, 2024
commit 672fcc2c37ef55cd26b642c1b401bee412f50f2e
4 changes: 2 additions & 2 deletions specutils/fitting/continuum.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ def fit_generic_continuum(spectrum, median_window=3, model=Chebyshev1D(3),

Parameters
----------
spectrum : Spectrum1D
spectrum : Spectrum
The spectrum object overwhich the equivalent width will be calculated.
model : list of `~astropy.modeling.Model`
The list of models that contain the initial guess.
@@ -64,7 +64,7 @@ def fit_continuum(spectrum, model=Chebyshev1D(3), fitter=LevMarLSQFitter(),

Parameters
----------
spectrum : Spectrum1D
spectrum : Spectrum
The spectrum object overwhich the equivalent width will be calculated.
model: list of `~astropy.modeling.Model`
The list of models that contain the initial guess.
18 changes: 9 additions & 9 deletions specutils/fitting/fitmodels.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
from scipy.signal import convolve

from ..spectra.spectral_region import SpectralRegion
from ..spectra.spectrum1d import Spectrum1D
from ..spectra.spectrum1d import Spectrum
from ..utils import QuantityModel
from ..analysis import fwhm, gaussian_sigma_width, centroid, warn_continuum_below_threshold
from ..manipulation import extract_region
@@ -24,7 +24,7 @@
# the astropy models but it was determined that this is a decent start as most
# fitting will probably use one of these.
#
# Each method list must take a Spectrum1D object and should return a Quantity.
# Each method list must take a Spectrum object and should return a Quantity.
_parameter_estimators = {
'Gaussian1D': {
'amplitude': lambda s: max(s.flux),
@@ -66,7 +66,7 @@ def estimate_line_parameters(spectrum, model, region=None):

Parameters
----------
spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum object from which we will estimate the model parameters.

model : `~astropy.modeling.Model`
@@ -114,7 +114,7 @@ def find_lines_threshold(spectrum, noise_factor=1):

Parameters
----------
spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum object in which the lines will be found.

noise_factor : float
@@ -169,7 +169,7 @@ def find_lines_derivative(spectrum, flux_threshold=None):

Parameters
----------
spectrum : Spectrum1D
spectrum : Spectrum
The spectrum object over which the equivalent width will be calculated.
flux_threshold : float, `~astropy.units.Quantity` or None
The threshold a pixel must be above to be considered part of a line. If
@@ -267,7 +267,7 @@ def fit_lines(spectrum, model, fitter=fitting.LevMarLSQFitter(calc_uncertainties

Parameters
----------
spectrum : Spectrum1D
spectrum : Spectrum
The spectrum object over which the equivalent width will be calculated.
model: `~astropy.modeling.Model` or list of `~astropy.modeling.Model`
The model or list of models that contain the initial guess.
@@ -454,7 +454,7 @@ def _fit_lines(spectrum, model, fitter=fitting.LevMarLSQFitter(calc_uncertaintie
# TODO: really the spectral region machinery should have the power
# to create a mask, and we'd just use that...
idxarr = np.arange(spectrum.flux.size).reshape(spectrum.flux.shape)
index_spectrum = Spectrum1D(spectral_axis=dispersion,
index_spectrum = Spectrum(spectral_axis=dispersion,
flux=u.Quantity(idxarr, u.Jy, dtype=int))

extracted_regions = extract_region(index_spectrum, window)
@@ -480,7 +480,7 @@ def _fit_lines(spectrum, model, fitter=fitting.LevMarLSQFitter(calc_uncertaintie

input_spectrum = spectrum

spectrum = Spectrum1D(
spectrum = Spectrum(
flux=flux.value * flux_unit,
spectral_axis=dispersion,
wcs=input_spectrum.wcs,
@@ -585,7 +585,7 @@ def _strip_units_from_model(model_in, spectrum, convert=True):
class instantiation.

If convert is False, then we will *not* do the conversion of units
to the units of the Spectrum1D object. Otherwise we will convert.
to the units of the Spectrum object. Otherwise we will convert.
"""

#
8 changes: 4 additions & 4 deletions specutils/manipulation/estimate_uncertainty.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

from astropy import units as u

from .. import Spectrum1D
from .. import Spectrum
from astropy.nddata.nduncertainty import StdDevUncertainty, VarianceUncertainty, InverseVariance
from .extract_spectral_region import extract_region

@@ -18,7 +18,7 @@ def noise_region_uncertainty(spectrum, spectral_region, noise_func=np.std):
Parameters
----------

spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum to which we want to set the uncertainty.

spectral_region : `~specutils.SpectralRegion`
@@ -30,7 +30,7 @@ def noise_region_uncertainty(spectrum, spectral_region, noise_func=np.std):

Returns
-------
spectrum_uncertainty : `~specutils.Spectrum1D`
spectrum_uncertainty : `~specutils.Spectrum`
The ``spectrum``, but with a constant uncertainty set by the result of
the noise region calculation
"""
@@ -59,7 +59,7 @@ def noise_region_uncertainty(spectrum, spectral_region, noise_func=np.std):
raise ValueError('Can not determine correct NDData Uncertainty based on units {} relative to the flux units {}'.format(noise.unit, spectrum.flux.unit))

# Return new specturm with uncertainty set.
return Spectrum1D(flux=spectrum.flux, spectral_axis=spectrum.spectral_axis,
return Spectrum(flux=spectrum.flux, spectral_axis=spectrum.spectral_axis,
uncertainty=uncertainty,
wcs=spectrum.wcs,
velocity_convention=spectrum.velocity_convention,
34 changes: 17 additions & 17 deletions specutils/manipulation/extract_spectral_region.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import numpy as np

from astropy import units as u
from ..spectra import Spectrum1D, SpectralRegion
from ..spectra import Spectrum, SpectralRegion

__all__ = ['extract_region', 'extract_bounding_spectral_region', 'spectral_slab']

@@ -30,12 +30,12 @@ def _subregion_to_edge_pixels(subregion, spectrum):
"""
Calculate and return the left and right indices defined
by the lower and upper bounds and based on the input
`~specutils.spectra.spectrum1d.Spectrum1D`. The left and right indices will
`~specutils.spectra.spectrum1d.Spectrum`. The left and right indices will
be returned.

Parameters
----------
spectrum: `~specutils.spectra.spectrum1d.Spectrum1D`
spectrum: `~specutils.spectra.spectrum1d.Spectrum`
The spectrum object from which the region will be extracted.

Returns
@@ -106,27 +106,27 @@ def _subregion_to_edge_pixels(subregion, spectrum):

def extract_region(spectrum, region, return_single_spectrum=False):
"""
Extract a region from the input `~specutils.Spectrum1D`
Extract a region from the input `~specutils.Spectrum`
defined by the lower and upper bounds defined by the ``region``
instance. The extracted region will be returned as a new
`~specutils.Spectrum1D`.
`~specutils.Spectrum`.

Parameters
----------
spectrum: `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum`
The spectrum object from which the region will be extracted.

region: `~specutils.SpectralRegion`
The spectral region to extract from the original spectrum.

return_single_spectrum: `bool`
If ``region`` has multiple sections, whether to return a single spectrum
instead of multiple `~specutils.Spectrum1D` objects. The returned spectrum
instead of multiple `~specutils.Spectrum` objects. The returned spectrum
will be a unique, concatenated, spectrum of all sub-regions.

Returns
-------
spectrum: `~specutils.Spectrum1D` or list of `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum` or list of `~specutils.Spectrum`
Excised spectrum, or list of spectra if the input region contained multiple
subregions and ``return_single_spectrum`` is `False`.

@@ -146,7 +146,7 @@ def extract_region(spectrum, region, return_single_spectrum=False):
And we calculate ``sub_spectrum = extract_region(spectrum, region)``, then the ``sub_spectrum``
spectral axis will be ``[0.2, 0.3, 0.4, 0.5] * u.um``.

If the ``region`` does not overlap with the ``spectrum`` then an empty Spectrum1D object
If the ``region`` does not overlap with the ``spectrum`` then an empty Spectrum object
will be returned.

"""
@@ -156,7 +156,7 @@ def extract_region(spectrum, region, return_single_spectrum=False):

# If both indices are out of bounds then return an empty spectrum
if left_index == right_index:
empty_spectrum = Spectrum1D(spectral_axis=[]*spectrum.spectral_axis.unit,
empty_spectrum = Spectrum(spectral_axis=[]*spectrum.spectral_axis.unit,
flux=[]*spectrum.flux.unit)
extracted_spectrum.append(empty_spectrum)
else:
@@ -205,7 +205,7 @@ def _get_joined_value(sps, key, unique_inds=None):
# so we'll concatenate that first and track the unique indices
spectral_axis = _get_joined_value(extracted_spectrum, 'spectral_axis')
spectral_axis_unique, unique_inds = np.unique(spectral_axis, return_index=True)
return Spectrum1D(spectral_axis=spectral_axis_unique,
return Spectrum(spectral_axis=spectral_axis_unique,
**{key: _get_joined_value(extracted_spectrum, key, unique_inds)
for key in concat_keys+copy_keys})

@@ -214,14 +214,14 @@ def _get_joined_value(sps, key, unique_inds=None):

def spectral_slab(spectrum, lower, upper):
"""
Extract a slab from the input `~specutils.Spectrum1D`
Extract a slab from the input `~specutils.Spectrum`
defined by the lower and upper bounds defined by the ``region``
instance. The extracted region will be returned as a new
`~specutils.Spectrum1D`.
`~specutils.Spectrum`.

Parameters
----------
spectrum: `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum`
The spectrum object from which the region will be extracted.

lower, upper: `~astropy.units.Quantity`
@@ -230,7 +230,7 @@ def spectral_slab(spectrum, lower, upper):

Returns
-------
spectrum: `~specutils.Spectrum1D` or list of `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum` or list of `~specutils.Spectrum`
Excised spectrum, or list of spectra if the input region contained multiple
subregions.

@@ -255,7 +255,7 @@ def extract_bounding_spectral_region(spectrum, region):

Parameters
----------
spectrum: `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum`
The spectrum object from which the region will be extracted.

region: `~specutils.SpectralRegion`
@@ -264,7 +264,7 @@ def extract_bounding_spectral_region(spectrum, region):

Returns
-------
spectrum: `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum`
Excised spectrum from the bounding region defined by the set of
sub-regions in the input ``region`` instance.

6 changes: 3 additions & 3 deletions specutils/manipulation/manipulation.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ def snr_threshold(spectrum, value, op=operator.gt):

Parameters
----------
spectrum : `~specutils.Spectrum1D`, `~specutils.SpectrumCollection` or `~astropy.nddata.NDData`
spectrum : `~specutils.Spectrum`, `~specutils.SpectrumCollection` or `~astropy.nddata.NDData`
The spectrum object overwhich the S/N threshold will be calculated.

value: ``float``
@@ -31,7 +31,7 @@ def snr_threshold(spectrum, value, op=operator.gt):

Returns
-------
spectrum: `~specutils.Spectrum1D`
spectrum: `~specutils.Spectrum`
Output object with ``spectrum.mask`` set based on threshold.

Notes
@@ -63,7 +63,7 @@ def snr_threshold(spectrum, value, op=operator.gt):
if isinstance(op, str):
op = operator_mapping[op]

# Spectrum1D
# Spectrum
if hasattr(spectrum, 'flux'):
data = spectrum.flux

14 changes: 7 additions & 7 deletions specutils/manipulation/model_replace.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
from astropy.units import Quantity
from astropy.modeling import Fittable1DModel

from ..spectra import Spectrum1D
from ..spectra import Spectrum
from ..utils import QuantityModel
from . import extract_region

@@ -17,7 +17,7 @@ def model_replace(spectrum, replace_region, model=10, extrapolation_treatment='d
Parameters
----------

spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum to be modified.

replace_region : `~specutils.SpectralRegion`
@@ -48,7 +48,7 @@ def model_replace(spectrum, replace_region, model=10, extrapolation_treatment='d

Returns
-------
spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum with the region replaced by spline values, and data
values or zeros outside the spline region. The spectral axis will
have the same units as the spline knots.
@@ -58,14 +58,14 @@ def model_replace(spectrum, replace_region, model=10, extrapolation_treatment='d

>>> import numpy as np
>>> import astropy.units as u
>>> from specutils.spectra.spectrum1d import Spectrum1D
>>> from specutils.spectra.spectrum1d import Spectrum
>>> from specutils.manipulation.model_replace import model_replace
>>> wave_val = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> flux_val = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
>>> input_spectrum = Spectrum1D(spectral_axis=wave_val * u.AA, flux=flux_val * u.mJy)
>>> input_spectrum = Spectrum(spectral_axis=wave_val * u.AA, flux=flux_val * u.mJy)
>>> spline_knots = [3.5, 4.7, 6.8, 7.1] * u.AA
>>> model_replace(input_spectrum, None, spline_knots)
<Spectrum1D(flux=<Quantity [ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.] mJy> (shape=(10,), mean=11.00000 mJy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<Spectrum(flux=<Quantity [ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.] mJy> (shape=(10,), mean=11.00000 mJy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>

"""
if extrapolation_treatment not in ('data_fill', 'zero_fill'):
@@ -147,7 +147,7 @@ def model_replace(spectrum, replace_region, model=10, extrapolation_treatment='d
new_unc = spectrum.uncertainty.__class__(array=out_uncert,
unit=spectrum.uncertainty.unit)

return Spectrum1D(spectral_axis=new_spectral_axis, flux=out, uncertainty=new_unc)
return Spectrum(spectral_axis=new_spectral_axis, flux=out, uncertainty=new_unc)


def _compute_spline_values(spectrum, spline_knots, new_spectral_axis, interpolate_uncertainty):
48 changes: 24 additions & 24 deletions specutils/manipulation/resample.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
from astropy.units import Quantity
from scipy.interpolate import CubicSpline

from ..spectra import Spectrum1D, SpectralAxis
from ..spectra import Spectrum, SpectralAxis

__all__ = ['ResamplerBase', 'FluxConservingResampler',
'LinearInterpolatedResampler', 'SplineInterpolatedResampler']
@@ -31,14 +31,14 @@ def __init__(self, extrapolation_treatment='nan_fill'):

def __call__(self, orig_spectrum, fin_spec_axis):
"""
Return the resulting `~specutils.Spectrum1D` of the resampling.
Return the resulting `~specutils.Spectrum` of the resampling.
"""
return self.resample1d(orig_spectrum, fin_spec_axis)

@abstractmethod
def resample1d(self, orig_spectrum, fin_spec_axis):
"""
Workhorse method that will return the resampled Spectrum1D
Workhorse method that will return the resampled Spectrum
object.
"""
return NotImplemented
@@ -68,15 +68,15 @@ class FluxConservingResampler(ResamplerBase):
>>> import numpy as np
>>> import astropy.units as u
>>> from specutils import Spectrum1D
>>> from specutils import Spectrum
>>> from specutils.manipulation import FluxConservingResampler
>>> input_spectra = Spectrum1D(
>>> input_spectra = Spectrum(
... flux=np.array([1, 3, 7, 6, 20]) * u.mJy,
... spectral_axis=np.array([2, 4, 12, 16, 20]) * u.nm)
>>> resample_grid = [1, 5, 9, 13, 14, 17, 21, 22, 23] *u.nm
>>> fluxc_resample = FluxConservingResampler()
>>> fluxc_resample(input_spectra, resample_grid) # doctest: +FLOAT_CMP
<Spectrum1D(flux=<Quantity [ 1. , 3. , 6. , 7. , 6.25, 10. , 20. , nan, nan] mJy> (shape=(9,), mean=7.60714 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
<Spectrum(flux=<Quantity [ 1. , 3. , 6. , 7. , 6.25, 10. , 20. , nan, nan] mJy> (shape=(9,), mean=7.60714 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
"""

@@ -227,15 +227,15 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
Parameters
----------
orig_spectrum : `~specutils.Spectrum1D`
orig_spectrum : `~specutils.Spectrum`
The original 1D spectrum.
fin_spec_axis : Quantity
The desired spectral axis array.
Returns
-------
resampled_spectrum : `~specutils.Spectrum1D`
An output spectrum containing the resampled `~specutils.Spectrum1D`
resampled_spectrum : `~specutils.Spectrum`
An output spectrum containing the resampled `~specutils.Spectrum`
"""

if isinstance(fin_spec_axis, Quantity):
@@ -298,7 +298,7 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
new_errs = new_errs[np.where(~np.isnan(output_fluxes))]
output_fluxes = output_fluxes[np.where(~np.isnan(output_fluxes))]

resampled_spectrum = Spectrum1D(flux=output_fluxes,
resampled_spectrum = Spectrum(flux=output_fluxes,
spectral_axis=fin_spec_axis,
uncertainty=new_errs,
spectral_axis_index = orig_spectrum.spectral_axis_index)
@@ -326,15 +326,15 @@ class LinearInterpolatedResampler(ResamplerBase):
>>> import numpy as np
>>> import astropy.units as u
>>> from specutils import Spectrum1D
>>> from specutils import Spectrum
>>> from specutils.manipulation import LinearInterpolatedResampler
>>> input_spectra = Spectrum1D(
>>> input_spectra = Spectrum(
... flux=np.array([1, 3, 7, 6, 20]) * u.mJy,
... spectral_axis=np.array([2, 4, 12, 16, 20]) * u.nm)
>>> resample_grid = [1, 5, 9, 13, 14, 17, 21, 22, 23] * u.nm
>>> fluxc_resample = LinearInterpolatedResampler()
>>> fluxc_resample(input_spectra, resample_grid) # doctest: +FLOAT_CMP
<Spectrum1D(flux=<Quantity [ nan, 3.5 , 5.5 , 6.75, 6.5 , 9.5 , nan, nan, nan] mJy> (shape=(9,), mean=6.35000 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
<Spectrum(flux=<Quantity [ nan, 3.5 , 5.5 , 6.75, 6.5 , 9.5 , nan, nan, nan] mJy> (shape=(9,), mean=6.35000 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
"""
def __init__(self, extrapolation_treatment='nan_fill'):
@@ -347,15 +347,15 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
Parameters
----------
orig_spectrum : `~specutils.Spectrum1D`
orig_spectrum : `~specutils.Spectrum`
The original 1D spectrum.
fin_spec_axis : ndarray
The desired spectral axis array.
Returns
-------
resample_spectrum : `~specutils.Spectrum1D`
An output spectrum containing the resampled `~specutils.Spectrum1D`
resample_spectrum : `~specutils.Spectrum`
An output spectrum containing the resampled `~specutils.Spectrum`
"""

fill_val = np.nan # bin_edges=nan_fill case
@@ -382,7 +382,7 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
new_unc = new_unc[np.where(~np.isnan(out_flux))]
out_flux = out_flux[np.where(~np.isnan(out_flux))]

return Spectrum1D(spectral_axis=fin_spec_axis,
return Spectrum(spectral_axis=fin_spec_axis,
flux=out_flux,
uncertainty=new_unc,
spectral_axis_index = orig_spectrum.spectral_axis_index)
@@ -411,15 +411,15 @@ class SplineInterpolatedResampler(ResamplerBase):
>>> import numpy as np
>>> import astropy.units as u
>>> from specutils import Spectrum1D
>>> from specutils import Spectrum
>>> from specutils.manipulation import SplineInterpolatedResampler
>>> input_spectra = Spectrum1D(
>>> input_spectra = Spectrum(
... flux=np.array([1, 3, 7, 6, 20]) * u.mJy,
... spectral_axis=np.array([2, 4, 12, 16, 20]) * u.nm)
>>> resample_grid = [1, 5, 9, 13, 14, 17, 21, 22, 23] * u.nm
>>> fluxc_resample = SplineInterpolatedResampler()
>>> fluxc_resample(input_spectra, resample_grid) # doctest: +FLOAT_CMP
<Spectrum1D(flux=<Quantity [ nan, 3.98808594, 6.94042969, 6.45869141, 5.89921875,
<Spectrum(flux=<Quantity [ nan, 3.98808594, 6.94042969, 6.45869141, 5.89921875,
7.29736328, nan, nan, nan] mJy> (shape=(9,), mean=6.11676 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
"""
@@ -433,15 +433,15 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
Parameters
----------
orig_spectrum : `~specutils.Spectrum1D`
orig_spectrum : `~specutils.Spectrum`
The original 1D spectrum.
fin_spec_axis : Quantity
The desired spectral axis array.
Returns
-------
resample_spectrum : `~specutils.Spectrum1D`
An output spectrum containing the resampled `~specutils.Spectrum1D`
resample_spectrum : `~specutils.Spectrum`
An output spectrum containing the resampled `~specutils.Spectrum`
"""
orig_axis_in_new = orig_spectrum.spectral_axis.to(fin_spec_axis.unit)
flux_spline = CubicSpline(orig_axis_in_new.value, orig_spectrum.flux.value,
@@ -475,7 +475,7 @@ def resample1d(self, orig_spectrum, fin_spec_axis):
new_unc = new_unc[np.where(~np.isnan(out_flux_val))]
out_flux_val = out_flux_val[np.where(~np.isnan(out_flux_val))]

return Spectrum1D(spectral_axis=fin_spec_axis,
return Spectrum(spectral_axis=fin_spec_axis,
flux=out_flux_val*orig_spectrum.flux.unit,
uncertainty=new_unc,
spectral_axis_index = orig_spectrum.spectral_axis_index)
52 changes: 26 additions & 26 deletions specutils/manipulation/smoothing.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
from astropy.utils.exceptions import AstropyUserWarning
from scipy.signal import medfilt

from ..spectra import Spectrum1D
from ..spectra import Spectrum

__all__ = ['convolution_smooth', 'box_smooth', 'gaussian_smooth',
'trapezoid_smooth', 'median_smooth']
@@ -30,17 +30,17 @@ def convolution_smooth(spectrum, kernel):
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the smoothing will be
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the smoothing will be
applied.
kernel : `astropy.convolution.Kernel1D` subclass or array.
The convolution based smoothing kernel - anything that
`astropy.convolution.convolve` accepts.
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which is copy of the one passed in with
the updated flux.
Raises
@@ -49,8 +49,8 @@ def convolution_smooth(spectrum, kernel):
In the case that ``spectrum`` and ``kernel`` are not the correct types.
"""
# Parameter checks
if not isinstance(spectrum, Spectrum1D):
raise ValueError('The spectrum parameter must be a Spectrum1D object')
if not isinstance(spectrum, Spectrum):
raise ValueError('The spectrum parameter must be a Spectrum object')

# Get the flux of the input spectrum
flux = spectrum.flux
@@ -120,21 +120,21 @@ def convolution_smooth(spectrum, kernel):

def box_smooth(spectrum, width):
"""
Smooth a `~specutils.Spectrum1D` instance along the spectral axis
Smooth a `~specutils.Spectrum` instance along the spectral axis
based on a `astropy.convolution.Box1DKernel` kernel.
Parameters
----------
spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum object to which the smoothing will be applied.
width : number
The width of the kernel, in pixels, as defined in
`astropy.convolution.Box1DKernel`
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which a copy of the one passed in with
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which a copy of the one passed in with
the updated flux.
Raises
@@ -157,21 +157,21 @@ def box_smooth(spectrum, width):

def gaussian_smooth(spectrum, stddev):
"""
Smooth a `~specutils.Spectrum1D` instance along the spectral axis
Smooth a `~specutils.Spectrum` instance along the spectral axis
based on a `astropy.convolution.Gaussian1DKernel`.
Parameters
----------
spectrum : `~specutils.Spectrum1D`
spectrum : `~specutils.Spectrum`
The spectrum object to which the smoothing will be applied.
stddev : number
The stddev of the kernel, in pixels, as defined in
`astropy.convolution.Gaussian1DKernel`
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which is copy of the one passed in with
the updated flux.
Raises
@@ -194,22 +194,22 @@ def gaussian_smooth(spectrum, stddev):

def trapezoid_smooth(spectrum, width):
"""
Smooth a `~specutils.Spectrum1D` instance along the spectral axis
Smooth a `~specutils.Spectrum` instance along the spectral axis
based on a `astropy.convolution.Trapezoid1DKernel` kernel.
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the smoothing will be
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the smoothing will be
applied.
width : number
The width of the kernel, in pixels, as defined in
`astropy.convolution.Trapezoid1DKernel`
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which is copy of the one passed in with
the updated flux.
Raises
@@ -237,16 +237,16 @@ def median_smooth(spectrum, width):
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the smoothing will be
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the smoothing will be
applied.
width : number
The width of the median filter in pixels.
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which is copy of the one passed in with
the updated flux.
Raises
@@ -257,8 +257,8 @@ def median_smooth(spectrum, width):
"""
# Parameter checks
if not isinstance(spectrum, Spectrum1D):
raise ValueError('The spectrum parameter must be a Spectrum1D object')
if not isinstance(spectrum, Spectrum):
raise ValueError('The spectrum parameter must be a Spectrum object')

if not isinstance(width, (int, float)) or width <= 0:
raise ValueError("The stddev parameter, {}, must be a number greater "
64 changes: 32 additions & 32 deletions specutils/manipulation/utils.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

from astropy.utils.exceptions import AstropyUserWarning
from astropy.coordinates import SpectralCoord
from ..spectra import Spectrum1D, SpectralRegion
from ..spectra import Spectrum, SpectralRegion

__all__ = ['excise_regions', 'linear_exciser', 'spectrum_from_model']

@@ -12,24 +12,24 @@ def true_exciser(spectrum, region):
"""
Basic spectral excise method where the array elements in the spectral
region defined by the parameter ``region`` (a `~specutils.SpectralRegion`)
will be deleted from all applicable elements of the Spectrum1D object:
will be deleted from all applicable elements of the Spectrum object:
flux, spectral_axis, mask, and uncertainty. If multiple subregions are
defined in ``region``, all the subregions will be excised.
Other methods could be defined by the user to do other types of excision.
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the excision will be applied.
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the excision will be applied.
region : `~specutils.SpectralRegion`
The region of the spectrum to remove.
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` with the region excised.
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` with the region excised.
Raises
------
@@ -66,7 +66,7 @@ def true_exciser(spectrum, region):
new_uncertainty = None

# Return a new object with the regions excised.
return Spectrum1D(flux=new_flux,
return Spectrum(flux=new_flux,
spectral_axis=new_spectral_axis,
uncertainty=new_uncertainty,
mask=new_mask,
@@ -87,16 +87,16 @@ def linear_exciser(spectrum, region):
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the excision will be applied.
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the excision will be applied.
region : `~specutils.SpectralRegion`
The region of the spectrum to replace.
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` with the region excised.
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` with the region excised.
Raises
------
@@ -140,7 +140,7 @@ def linear_exciser(spectrum, region):
new_uncertainty[s:e] = np.sqrt(spectrum.uncertainty[s]**2 + spectrum.uncertainty[e]**2)

# Return a new object with the regions excised.
return Spectrum1D(flux=modified_flux,
return Spectrum(flux=modified_flux,
spectral_axis=spectral_axis,
uncertainty=new_uncertainty,
wcs=spectrum.wcs,
@@ -157,8 +157,8 @@ def excise_regions(spectrum, regions, exciser=true_exciser):
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the excision will be applied.
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the excision will be applied.
regions : list of `~specutils.SpectralRegion`
Each element of the list is a `~specutils.SpectralRegion`. The flux
@@ -175,8 +175,8 @@ def excise_regions(spectrum, regions, exciser=true_exciser):
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which has the regions excised.
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which has the regions excised.
Raises
------
@@ -186,8 +186,8 @@ def excise_regions(spectrum, regions, exciser=true_exciser):
"""

# Parameter checks
if not isinstance(spectrum, Spectrum1D):
raise ValueError('The spectrum parameter must be Spectrum1D object.')
if not isinstance(spectrum, Spectrum):
raise ValueError('The spectrum parameter must be Spectrum object.')

for region in regions:
spectrum = excise_region(spectrum, region, exciser)
@@ -202,8 +202,8 @@ def excise_region(spectrum, region, exciser=true_exciser):
Parameters
----------
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to which the smoothing will be applied.
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to which the smoothing will be applied.
region : `~specutils.SpectralRegion`
A `~specutils.SpectralRegion` object defining the region to excise.
@@ -219,8 +219,8 @@ def excise_region(spectrum, region, exciser=true_exciser):
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` with the region excised.
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` with the region excised.
Raises
------
@@ -230,8 +230,8 @@ def excise_region(spectrum, region, exciser=true_exciser):
"""

# Parameter checks
if not isinstance(spectrum, Spectrum1D):
raise ValueError('The spectrum parameter must be a Spectrum1D object.')
if not isinstance(spectrum, Spectrum):
raise ValueError('The spectrum parameter must be a Spectrum object.')

if not isinstance(region, SpectralRegion):
raise ValueError('The region parameter must be a SpectralRegion object.')
@@ -245,23 +245,23 @@ def excise_region(spectrum, region, exciser=true_exciser):

def spectrum_from_model(model_input, spectrum):
"""
This method will create a `~specutils.Spectrum1D` object
This method will create a `~specutils.Spectrum` object
with the flux defined by calling the input ``model``. All
other parameters for the output `~specutils.Spectrum1D` object
will be the same as the input `~specutils.Spectrum1D` object.
other parameters for the output `~specutils.Spectrum` object
will be the same as the input `~specutils.Spectrum` object.
Parameters
----------
model : `~astropy.modeling.Model`
The input model or compound model from which flux is calculated.
spectrum : `~specutils.Spectrum1D`
The `~specutils.Spectrum1D` object to use as the model template.
spectrum : `~specutils.Spectrum`
The `~specutils.Spectrum` object to use as the model template.
Returns
-------
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with the updated flux.
spectrum : `~specutils.Spectrum`
Output `~specutils.Spectrum` which is copy of the one passed in with the updated flux.
The uncertainty will not be copied as it is not necessarily the same.
"""
@@ -275,7 +275,7 @@ def spectrum_from_model(model_input, spectrum):
else:
flux = model_input(spectrum.spectral_axis.value)*spectrum.flux.unit

return Spectrum1D(flux=flux,
return Spectrum(flux=flux,
spectral_axis=spectrum.spectral_axis,
wcs=spectrum.wcs,
velocity_convention=spectrum.velocity_convention,