Skip to content

Commit

Permalink
Went back to = over :, other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rosteen committed Feb 12, 2024
1 parent 4019868 commit 160c09e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 56 deletions.
8 changes: 4 additions & 4 deletions docs/arithmetic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Arithmetic support includes addition, subtract, multiplication, and division.
>>> spec2 = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm, flux=rng.random(49)*u.Jy)
>>> spec3 = spec1 + spec2
>>> spec3 # doctest: +FLOAT_CMP
<Spectrum1D(flux: shape (49,), mean=0.91592 Jy; spectral_axis: <SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
<Spectrum1D(flux=[0.8559405665668484 ... 0.9711264429515736] Jy (shape=(49,), mean=0.91592 Jy); spectral_axis=<SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
[ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
Expand Down
26 changes: 13 additions & 13 deletions docs/manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ along the spectral dimension.
>>> spec1_gsmooth = gaussian_smooth(spec1, stddev=3)
>>> spec1_tsmooth = trapezoid_smooth(spec1, width=3)
>>> gaussian_smooth(spec1, stddev=3) # doctest: +FLOAT_CMP
<Spectrum1D(flux: shape (49,), mean=0.48009 Jy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
<Spectrum1D(flux=[0.25860917267578276 ... 0.15868783272966752] Jy (shape=(49,), mean=0.48009 Jy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
Each of the specific smoothing methods create the appropriate `astropy.convolution.convolve`
kernel and then call a helper function :func:`~specutils.manipulation.convolution_smooth`
Expand All @@ -75,7 +75,7 @@ that takes the spectrum and an astropy 1D kernel. So, one could also do:
>>> spec1 = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
... flux=np.random.default_rng(12345).random(49) * u.Jy)
>>> convolution_smooth(spec1, box1d_kernel) # doctest: +FLOAT_CMP
<Spectrum1D(flux: shape (49,), mean=0.49378 Jy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
<Spectrum1D(flux=[0.1813647873923075 ... 0.1201562712204726] Jy (shape=(49,), mean=0.49378 Jy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
In this case, the ``spec1_bsmooth2`` result should be equivalent to the ``spec1_bsmooth`` in
the section above (assuming the flux data of the input ``spec`` is the same). Note that,
Expand Down Expand Up @@ -107,7 +107,7 @@ method applys the median filter across the flux.
>>> spec1 = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
... flux=np.random.default_rng(12345).random(49) * u.Jy)
>>> median_smooth(spec1, width=3) # doctest: +FLOAT_CMP
<Spectrum1D(flux: shape (49,), mean=0.48620 Jy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
<Spectrum1D(flux=[0.22733602246716966 ... 0.005022333717131788] Jy (shape=(49,), mean=0.48620 Jy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 47. 48. 49.] nm> (length=49))>
Resampling
----------
Expand Down Expand Up @@ -333,16 +333,16 @@ the ``spectral_axis``. Therefore one can use a construct like this:
>>> flux = 100 * np.abs(np.random.default_rng(42).standard_normal(10)) * u.Jy
>>> spectrum = Spectrum1D(spectral_axis=wavelengths, flux=flux)
>>> spectrum # doctest: +FLOAT_CMP
<Spectrum1D(flux: <Quantity [ 30.47170798, 103.99841062, 75.04511958, 94.05647164,
195.10351887, 130.21795069, 12.78404032, 31.62425923,
1.68011575, 85.30439276] Jy>, mean=76.02860 Jy; spectral_axis: <SpectralAxis [0. 1. 2. ... 7. 8. 9.] um> (length=10))>
<Spectrum1D(flux=<Quantity [ 30.47170798, 103.99841062, 75.04511958, 94.05647164,
195.10351887, 130.21795069, 12.78404032, 31.62425923,
1.68011575, 85.30439276] Jy> (shape=(10,), mean=76.02860 Jy); spectral_axis=<SpectralAxis [0. 1. 2. ... 7. 8. 9.] um> (length=10))>
>>> shift = 12300 * u.AA
>>> new_spec = Spectrum1D(spectral_axis=spectrum.spectral_axis + shift, flux=spectrum.flux)
>>> new_spec # doctest: +FLOAT_CMP
<Spectrum1D(flux: <Quantity [ 30.47170798, 103.99841062, 75.04511958, 94.05647164,
<Spectrum1D(flux=<Quantity [ 30.47170798, 103.99841062, 75.04511958, 94.05647164,
195.10351887, 130.21795069, 12.78404032, 31.62425923,
1.68011575, 85.30439276] Jy>, mean=76.02860 Jy; spectral_axis: <SpectralAxis [ 1.23 2.23 3.23 ... 8.23 9.23 10.23] um> (length=10))>
1.68011575, 85.30439276] Jy> (shape=(10,), mean=76.02860 Jy); spectral_axis=<SpectralAxis [ 1.23 2.23 3.23 ... 8.23 9.23 10.23] um> (length=10))>
Replacing a region
------------------
Expand All @@ -364,7 +364,7 @@ with the spline knots:
>>> spline_knots = [3.5, 4.7, 6.8, 7.1] * u.AA
>>> result = model_replace(input_spectrum, None, model=spline_knots)
>>> result
<Spectrum1D(flux: <Quantity [ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.] mJy>, mean=11.00000 mJy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<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))>
The default behavior is to keep the data outside the replaced region unchanged.
Alternatively, the spectrum outside the replaced region can be filled with zeros:
Expand All @@ -374,7 +374,7 @@ Alternatively, the spectrum outside the replaced region can be filled with zeros
>>> spline_knots = [3.5, 4.7, 6.8, 7.1] * u.AA
>>> result = model_replace(input_spectrum, None, model=spline_knots, extrapolation_treatment='zero_fill')
>>> result
<Spectrum1D(flux: <Quantity [ 0., 0., 0., 8., 10., 12., 14., 0., 0., 0.] mJy>, mean=4.40000 mJy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<Spectrum1D(flux=<Quantity [ 0., 0., 0., 8., 10., 12., 14., 0., 0., 0.] mJy> (shape=(10,), mean=4.40000 mJy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
One can define the spline knots by providing an instance of `~specutils.SpectralRegion`,
and the number of knots to be evenly spread along the region:
Expand All @@ -385,7 +385,7 @@ and the number of knots to be evenly spread along the region:
>>> region = SpectralRegion(3.5*u.AA, 7.1*u.AA)
>>> result = model_replace(input_spectrum, region, model=4)
>>> result
<Spectrum1D(flux: <Quantity [ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.] mJy>, mean=11.00000 mJy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<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))>
A model fitted over the region can also be used to replace the spectrum flux values:

Expand All @@ -400,8 +400,8 @@ A model fitted over the region can also be used to replace the spectrum flux val
>>> region = SpectralRegion(3.5*u.AA, 7.1*u.AA)
>>> result = model_replace(input_spectrum, region, model=fitted_model)
>>> result # doctest: +FLOAT_CMP
<Spectrum1D(flux: <Quantity [1. , 1.1 , 0.9 , 4.40801804, 9.58271877,
5.61238054, 0.88556096, 1. , 1.2 , 1.1 ] mJy>, mean=2.67887 mJy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<Spectrum1D(flux=<Quantity [1. , 1.1 , 0.9 , 4.40801804, 9.58271877,
5.61238054, 0.88556096, 1. , 1.2 , 1.1 ] mJy> (shape=(10,), mean=2.67887 mJy); spectral_axis=<SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
Reference/API
-------------
Expand Down
20 changes: 10 additions & 10 deletions docs/spectral_cube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ Print the contents of 3 spectral axis points in a 3x3 spaxel array:
.. code-block:: python
>>> sc[30:33,30:33,2000:2003] # doctest: +REMOTE_DATA
<Spectrum1D(flux: shape (3, 3, 3), mean=0.54165 1e-17 erg / (Angstrom s spaxel cm2); spectral_axis: <SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
[5.73984286e-07 5.74116466e-07 5.74248676e-07] m> (length=3); uncertainty: InverseVariance)>
<Spectrum1D(flux=[[[0.4892023205757141 ... 0.5994223356246948]]] 1e-17 erg / (Angstrom s spaxel cm2) (shape=(3, 3, 3), mean=0.54165 1e-17 erg / (Angstrom s spaxel cm2)); spectral_axis=<SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
[5.73984286e-07 5.74116466e-07 5.74248676e-07] m> (length=3); uncertainty=InverseVariance)>
Spectral slab extraction
Expand All @@ -70,11 +70,11 @@ spectral regions from the cube.
>>> ss.shape # doctest: +REMOTE_DATA
(74, 74, 3)
>>> ss[30:33,30:33,::] # doctest: +REMOTE_DATA
<Spectrum1D(flux: shape (3, 3, 3), mean=0.83004 1e-17 erg / (Angstrom s spaxel cm2); spectral_axis: <SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
[5.00034537e-07 5.00149688e-07 5.00264865e-07] m> (length=3); uncertainty: InverseVariance)>
<Spectrum1D(flux=[[[0.6103081107139587 ... 0.936118483543396]]] 1e-17 erg / (Angstrom s spaxel cm2) (shape=(3, 3, 3), mean=0.83004 1e-17 erg / (Angstrom s spaxel cm2)); spectral_axis=<SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
[5.00034537e-07 5.00149688e-07 5.00264865e-07] m> (length=3); uncertainty=InverseVariance)>
Spectral Bounding Region
Expand Down
10 changes: 5 additions & 5 deletions docs/spectrum1d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ a loader.
>>> import urllib
>>> specs = urllib.request.urlopen('https://data.sdss.org/sas/dr14/sdss/spectro/redux/26/spectra/0751/spec-0751-52251-0160.fits') # doctest: +REMOTE_DATA
>>> Spectrum1D.read(specs, format="SDSS-III/IV spec") # doctest: +REMOTE_DATA
<Spectrum1D(flux: shape (3841,), mean=51.88042 1e-17 erg / (Angstrom s cm2); spectral_axis: <SpectralAxis [3799.2686 3800.1426 3801.0188 ... 9193.905 9196.0205 9198.141 ] Angstrom> (length=3841); uncertainty: InverseVariance)>
<Spectrum1D(flux=[30.59662628173828 ... 51.70271682739258] 1e-17 erg / (Angstrom s cm2) (shape=(3841,), mean=51.88042 1e-17 erg / (Angstrom s cm2)); spectral_axis=<SpectralAxis [3799.2686 3800.1426 3801.0188 ... 9193.905 9196.0205 9198.141 ] Angstrom> (length=3841); uncertainty=InverseVariance)>
Note that the same spectrum could be more conveniently downloaded via
astroquery, if the user has that package installed:
Expand All @@ -74,7 +74,7 @@ astroquery, if the user has that package installed:
>>> from astroquery.sdss import SDSS # doctest: +REMOTE_DATA
>>> specs = SDSS.get_spectra(plate=751, mjd=52251, fiberID=160, data_release=14) # doctest: +REMOTE_DATA
>>> Spectrum1D.read(specs[0], format="SDSS-III/IV spec") # doctest: +REMOTE_DATA
<Spectrum1D(flux: shape (3841,), mean=51.88042 1e-17 erg / (Angstrom s cm2); spectral_axis: <SpectralAxis [3799.2686 3800.1426 3801.0188 ... 9193.905 9196.0205 9198.141 ] Angstrom> (length=3841); uncertainty: InverseVariance)>
<Spectrum1D(flux=[30.59662628173828 ... 51.70271682739258] 1e-17 erg / (Angstrom s cm2) (shape=(3841,), mean=51.88042 1e-17 erg / (Angstrom s cm2)); spectral_axis=<SpectralAxis [3799.2686 3800.1426 3801.0188 ... 9193.905 9196.0205 9198.141 ] Angstrom> (length=3841); uncertainty=InverseVariance)>


List of Loaders
Expand Down Expand Up @@ -326,7 +326,7 @@ value will apply to the lower bound input.
>>> lower = [SpectralCoord(4.9, unit=u.um), SkyCoord(ra=205, dec=26, unit=u.deg)]
>>> upper = [SpectralCoord(4.9, unit=u.um), SkyCoord(ra=205.5, dec=27.5, unit=u.deg)]
>>> spec.crop(lower, upper) # doctest: +IGNORE_WARNINGS +FLOAT_CMP
<Spectrum1D(flux: shape (10, 5, 1), mean=0.49653 Jy; spectral_axis: <SpectralAxis
<Spectrum1D(flux=[[[0.708612359963129 ... 0.6345714580773677]]] Jy (shape=(10, 5, 1), mean=0.49653 Jy); spectral_axis=<SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
Expand Down Expand Up @@ -356,8 +356,8 @@ spectral axis, or 'spatial', which will collapse along all non-spectral axes.
.. code-block:: python
>>> spec.mean(axis='spatial') # doctest: +FLOAT_CMP
<Spectrum1D(flux: <Quantity [0.37273938, 0.53843905, 0.61351648, 0.57311623, 0.44339915,
0.66084728, 0.45881921, 0.38715911, 0.39967185, 0.53257671] Jy>, mean=0.49803 Jy; spectral_axis: <SpectralAxis
<Spectrum1D(flux=<Quantity [0.37273938, 0.53843905, 0.61351648, 0.57311623, 0.44339915,
0.66084728, 0.45881921, 0.38715911, 0.39967185, 0.53257671] Jy> (shape=(10,), mean=0.49803 Jy); spectral_axis=<SpectralAxis
(observer to target:
radial_velocity=0.0 km / s
redshift=0.0)
Expand Down
2 changes: 1 addition & 1 deletion specutils/manipulation/model_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def model_replace(spectrum, replace_region, model=10, extrapolation_treatment='d
>>> input_spectrum = Spectrum1D(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>, mean=11.00000 mJy; spectral_axis: <SpectralAxis [ 1. 2. 3. ... 8. 9. 10.] Angstrom> (length=10))>
<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))>
"""
if extrapolation_treatment not in ('data_fill', 'zero_fill'):
Expand Down
10 changes: 6 additions & 4 deletions specutils/manipulation/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class FluxConservingResampler(ResamplerBase):
>>> 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 [ nan, 3. , 6. , 7. , 6.25, 10. , 20. , nan, nan] mJy>, mean=8.70833 mJy; spectral_axis: <SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))> """
<Spectrum1D(flux=<Quantity [ nan, 3. , 6. , 7. , 6.25, 10. , 20. , nan, nan] mJy> (shape=(9,), mean=8.70833 mJy); spectral_axis=<SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
"""

def _fluxc_resample(self, input_bin_centers, output_bin_centers,
input_bin_fluxes, errs):
Expand Down Expand Up @@ -322,7 +324,7 @@ class LinearInterpolatedResampler(ResamplerBase):
>>> 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>, mean=6.35000 mJy; spectral_axis: <SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
<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))>
"""
def __init__(self, extrapolation_treatment='nan_fill'):
Expand Down Expand Up @@ -398,8 +400,8 @@ class SplineInterpolatedResampler(ResamplerBase):
>>> 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,
7.29736328, nan, nan, nan] mJy>, mean=6.11676 mJy; spectral_axis: <SpectralAxis [ 1. 5. 9. ... 21. 22. 23.] nm> (length=9))>
<Spectrum1D(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))>
"""
def __init__(self, bin_edges='nan_fill'):
Expand Down
23 changes: 14 additions & 9 deletions specutils/spectra/spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def _format_array_summary(self, label, array):
array_str = np.array2string(array, threshold=8, prefix=label)
if len(array) >= 1:
mean = np.nanmean(array)
s = f"{label} {array_str} {array.unit}, mean={mean:.5f}"
s = f"{label}{array_str} {array.unit}, mean={mean:.5f}"
return s
else:
return "{:17} [ ], mean= n/a".format(label+':')
Expand All @@ -737,32 +737,37 @@ def __str__(self):
result += "(length={})\n".format(len(self.spectral_axis))

# Add Flux information
result += self._format_array_summary('Flux:', self.flux) + '\n'
result += self._format_array_summary('Flux=', self.flux) + '\n'

# Add information about spectral axis
result += self._format_array_summary('Spectral Axis:', self.spectral_axis)
result += self._format_array_summary('Spectral Axis=', self.spectral_axis)

# Add information about uncertainties if available
if self.uncertainty:
result += (f'\nUncertainty: {type(self.uncertainty).__name__} '
result += (f'\nUncertainty={type(self.uncertainty).__name__} '
f'({np.array2string(self.uncertainty.array, threshold=8)}'
f' {self.uncertainty.unit})')

return result

def __repr__(self):
flux_str = "flux="
if (self.flux.ndim == 1 and self.flux.size <= 10) or self.flux.size <= 20:
flux_str = repr(self.flux)
flux_str += repr(self.flux)
else:
flux_str = f" shape {self.flux.shape}"
flux_summary = f"{self.flux.value.flat[0]} ... {self.flux.value.flat[-1]}"
flux_str = flux_str + "[" * self.flux.ndim + flux_summary + "]" * self.flux.ndim
flux_str += f" {self.flux.unit}"

flux_str += f" (shape={self.flux.shape}, mean={np.nanmean(self.flux):.5f}); "
spectral_axis_str = (repr(self.spectral_axis).split("[")[0] +
np.array2string(self.spectral_axis, threshold=8) +
f" {self.spectral_axis.unit}>")
inner_str = (f"flux: {flux_str}, mean={np.nanmean(self.flux):.5f}; "
f"spectral_axis: {spectral_axis_str} (length={len(self.spectral_axis)})")
spectral_axis_str = f"spectral_axis={spectral_axis_str} (length={len(self.spectral_axis)})"
inner_str = (flux_str + spectral_axis_str)

if self.uncertainty is not None:
inner_str += f"; uncertainty: {self.uncertainty.__class__.__name__}"
inner_str += f"; uncertainty={self.uncertainty.__class__.__name__}"

result = "<Spectrum1D({})>".format(inner_str)

Expand Down
Loading

0 comments on commit 160c09e

Please sign in to comment.