diff --git a/docs/arithmetic.rst b/docs/arithmetic.rst index c7e98650b..aa8b1fbf3 100644 --- a/docs/arithmetic.rst +++ b/docs/arithmetic.rst @@ -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 - (length=49))> diff --git a/docs/manipulation.rst b/docs/manipulation.rst index 300ac9c40..289952a69 100644 --- a/docs/manipulation.rst +++ b/docs/manipulation.rst @@ -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 - (length=49))> + (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` @@ -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 - (length=49))> + (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, @@ -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 - (length=49))> + (length=49))> Resampling ---------- @@ -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 - , mean=76.02860 Jy; spectral_axis: (length=10))> + (shape=(10,), mean=76.02860 Jy); spectral_axis= (length=10))> >>> shift = 12300 * u.AA >>> new_spec = Spectrum1D(spectral_axis=spectrum.spectral_axis + shift, flux=spectrum.flux) >>> new_spec # doctest: +FLOAT_CMP - , mean=76.02860 Jy; spectral_axis: (length=10))> + 1.68011575, 85.30439276] Jy> (shape=(10,), mean=76.02860 Jy); spectral_axis= (length=10))> Replacing a region ------------------ @@ -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 - , mean=11.00000 mJy; spectral_axis: (length=10))> + (shape=(10,), mean=11.00000 mJy); spectral_axis= (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: @@ -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 - , mean=4.40000 mJy; spectral_axis: (length=10))> + (shape=(10,), mean=4.40000 mJy); spectral_axis= (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: @@ -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 - , mean=11.00000 mJy; spectral_axis: (length=10))> + (shape=(10,), mean=11.00000 mJy); spectral_axis= (length=10))> A model fitted over the region can also be used to replace the spectrum flux values: @@ -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 - , mean=2.67887 mJy; spectral_axis: (length=10))> + (shape=(10,), mean=2.67887 mJy); spectral_axis= (length=10))> Reference/API ------------- diff --git a/docs/spectral_cube.rst b/docs/spectral_cube.rst index b1d091e43..8a8d4bd8b 100644 --- a/docs/spectral_cube.rst +++ b/docs/spectral_cube.rst @@ -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 - (length=3); uncertainty: InverseVariance)> + (length=3); uncertainty=InverseVariance)> Spectral slab extraction @@ -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 - (length=3); uncertainty: InverseVariance)> + (length=3); uncertainty=InverseVariance)> Spectral Bounding Region diff --git a/docs/spectrum1d.rst b/docs/spectrum1d.rst index 5108bbf75..f844a5fe5 100644 --- a/docs/spectrum1d.rst +++ b/docs/spectrum1d.rst @@ -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 - (length=3841); uncertainty: InverseVariance)> + (length=3841); uncertainty=InverseVariance)> Note that the same spectrum could be more conveniently downloaded via astroquery, if the user has that package installed: @@ -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 - (length=3841); uncertainty: InverseVariance)> + (length=3841); uncertainty=InverseVariance)> List of Loaders @@ -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 - >> spec.mean(axis='spatial') # doctest: +FLOAT_CMP - , mean=0.49803 Jy; spectral_axis: (shape=(10,), mean=0.49803 Jy); spectral_axis=>> 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 - , mean=8.70833 mJy; spectral_axis: (length=9))> """ + (shape=(9,), mean=8.70833 mJy); spectral_axis= (length=9))> + + """ def _fluxc_resample(self, input_bin_centers, output_bin_centers, input_bin_fluxes, errs): @@ -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 - , mean=6.35000 mJy; spectral_axis: (length=9))> + (shape=(9,), mean=6.35000 mJy); spectral_axis= (length=9))> """ def __init__(self, extrapolation_treatment='nan_fill'): @@ -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 - , mean=6.11676 mJy; spectral_axis: (length=9))> + (shape=(9,), mean=6.11676 mJy); spectral_axis= (length=9))> """ def __init__(self, bin_edges='nan_fill'): diff --git a/specutils/spectra/spectrum1d.py b/specutils/spectra/spectrum1d.py index 98bcfe3a6..7702b58f5 100644 --- a/specutils/spectra/spectrum1d.py +++ b/specutils/spectra/spectrum1d.py @@ -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+':') @@ -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 = "".format(inner_str) diff --git a/specutils/tests/test_spectrum1d.py b/specutils/tests/test_spectrum1d.py index 43cc2be3c..470f6d516 100644 --- a/specutils/tests/test_spectrum1d.py +++ b/specutils/tests/test_spectrum1d.py @@ -427,17 +427,17 @@ def test_repr(): spec_with_wcs = Spectrum1D(spectral_axis=np.linspace(100, 1000, 10) * u.nm, flux=np.random.random(10) * u.Jy) result = repr(spec_with_wcs) - assert result.startswith('