From 2b3dcc86bc3130220ab49483f4d7ea720a6ba35d Mon Sep 17 00:00:00 2001 From: kelle Date: Thu, 31 Jul 2025 11:01:26 -0400 Subject: [PATCH 1/2] upgrade to specutils2.1 --- astrodbkit/spectra.py | 20 ++++++++++---------- astrodbkit/tests/test_spectra.py | 2 +- docs/index.rst | 4 ++-- pyproject.toml | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/astrodbkit/spectra.py b/astrodbkit/spectra.py index 7174184..a22cf9b 100644 --- a/astrodbkit/spectra.py +++ b/astrodbkit/spectra.py @@ -8,7 +8,7 @@ from astropy.nddata import StdDevUncertainty from astropy.units import Unit from astropy.wcs import WCS -from specutils import Spectrum1D +from specutils import Spectrum from specutils.io.parsing_utils import read_fileobj_or_hdulist from specutils.io.registers import data_loader @@ -45,7 +45,7 @@ def identify_spex_prism(origin, *args, **kwargs): return is_spex -@data_loader("Spex Prism", identifier=identify_spex_prism, extensions=["fits"], dtype=Spectrum1D) +@data_loader("Spex Prism", identifier=identify_spex_prism, extensions=["fits"], dtype=Spectrum) def spex_prism_loader(filename, **kwargs): """Open a SpeX Prism file and convert it to a Spectrum1D object""" @@ -72,7 +72,7 @@ def spex_prism_loader(filename, **kwargs): meta = {"header": header} - return Spectrum1D(flux=data, spectral_axis=wave, uncertainty=uncertainty, meta=meta) + return Spectrum(flux=data, spectral_axis=wave, uncertainty=uncertainty, meta=meta) def identify_wcs1d_multispec(origin, *args, **kwargs): @@ -92,7 +92,7 @@ def identify_wcs1d_multispec(origin, *args, **kwargs): ) -@data_loader("wcs1d-multispec", identifier=identify_wcs1d_multispec, extensions=["fits"], dtype=Spectrum1D, priority=10) +@data_loader("wcs1d-multispec", identifier=identify_wcs1d_multispec, extensions=["fits"], dtype=Spectrum, priority=10) def wcs1d_multispec_loader(file_obj, flux_unit=None, hdu=0, verbose=False, **kwargs): """ Loader for multiextension spectra as wcs1d. Adapted from wcs1d_fits_loader @@ -115,7 +115,7 @@ def wcs1d_multispec_loader(file_obj, flux_unit=None, hdu=0, verbose=False, **kwa Returns ------- - :class:`~specutils.Spectrum1D` + :class:`~specutils.Spectrum` """ with read_fileobj_or_hdulist(file_obj, **kwargs) as hdulist: @@ -178,7 +178,7 @@ def wcs1d_multispec_loader(file_obj, flux_unit=None, hdu=0, verbose=False, **kwa # Store header as metadata information meta = {"header": header} - return Spectrum1D(flux=flux_data, spectral_axis=spectral_axis, uncertainty=uncertainty, meta=meta) + return Spectrum(flux=flux_data, spectral_axis=spectral_axis, uncertainty=uncertainty, meta=meta) def load_spectrum(filename: str, spectra_format: str = None, raise_error: bool = False): @@ -189,8 +189,8 @@ def load_spectrum(filename: str, spectra_format: str = None, raise_error: bool = filename Name of the file to read spectra_format - Optional file format, passed to Spectrum1D.read. - In its absense Spectrum1D.read will attempt to determine the format. + Optional file format, passed to Spectrum.read. + In its absense Spectrum.read will attempt to determine the format. raise_error Boolean to control if a failure to read the spectrum should raise an error. """ @@ -208,9 +208,9 @@ def load_spectrum(filename: str, spectra_format: str = None, raise_error: bool = try: if spectra_format is not None: - spec1d = Spectrum1D.read(filename, format=spectra_format) + spec1d = Spectrum.read(filename, format=spectra_format) else: - spec1d = Spectrum1D.read(filename) + spec1d = Spectrum.read(filename) except Exception as e: # pylint: disable=broad-except, invalid-name msg = f"Error loading {filename}: {e}" diff --git a/astrodbkit/tests/test_spectra.py b/astrodbkit/tests/test_spectra.py index 738f452..a7e70ac 100644 --- a/astrodbkit/tests/test_spectra.py +++ b/astrodbkit/tests/test_spectra.py @@ -142,7 +142,7 @@ def test_wcs1d_multispec_loader(mock_fits_open, good_wcs1dmultispec, alt_wcs1dmu assert spectrum.wavelength.unit == Unit("Angstrom") -@mock.patch("astrodbkit.spectra.Spectrum1D.read") +@mock.patch("astrodbkit.spectra.Spectrum.read") def test_load_spectrum(mock_spectrum1d, monkeypatch): _ = load_spectrum("fake_file.txt") mock_spectrum1d.assert_called_with("fake_file.txt") diff --git a/docs/index.rst b/docs/index.rst index 632e23a..ccf6e06 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -328,7 +328,7 @@ General Queries with Transformations ------------------------------------ **AstrodbKit** can convert columns to special types. -Currently, spectra transformations are implemented and the specified column would be converted to a `Spectrum1D` object +Currently, spectra transformations are implemented and the specified column would be converted to a `Spectrum` object using the `specutils package `_. To call this, users can supply the name of the column to convert (by default, none is converted, though .spectra assumes the column name is *spectrum*):: @@ -338,7 +338,7 @@ To call this, users can supply the name of the column to convert db.query(db.Spectra).spectra(fmt='astropy') These three calls will return results from the Spectra table and will attempt to convert the *spectrum* -column to a Spectrum1D object for each row. Multiple columns to convert can also be passed as a list. +column to a Spectrum object for each row. Multiple columns to convert can also be passed as a list. The parameter `spectra_format` can be specified if **specutils** is having trouble determining the type of spectrum. Spectra need to be specified as either URL or paths relative to an environment variable, diff --git a/pyproject.toml b/pyproject.toml index d1566d3..a429ee7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ dependencies = [ "sqlalchemy>=2.0.38", "pandas>=1.0.4", "packaging", - "specutils>=1.0", + "specutils>=2.0", "tqdm", ] From d71b917af3aa170e9c7766195311f94096fe46d1 Mon Sep 17 00:00:00 2001 From: Kelle Cruz Date: Thu, 31 Jul 2025 11:19:57 -0400 Subject: [PATCH 2/2] fix docstring Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- astrodbkit/spectra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astrodbkit/spectra.py b/astrodbkit/spectra.py index a22cf9b..81f7443 100644 --- a/astrodbkit/spectra.py +++ b/astrodbkit/spectra.py @@ -47,7 +47,7 @@ def identify_spex_prism(origin, *args, **kwargs): @data_loader("Spex Prism", identifier=identify_spex_prism, extensions=["fits"], dtype=Spectrum) def spex_prism_loader(filename, **kwargs): - """Open a SpeX Prism file and convert it to a Spectrum1D object""" + """Open a SpeX Prism file and convert it to a Spectrum object""" with fits.open(filename, **kwargs) as hdulist: header = hdulist[0].header