Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

specmangle

CI status Documentation Status

specmangle applies a smooth, wavelength-dependent multiplicative correction to an observed astronomical spectrum such that synthetic broadband photometry based on that spectrum matches contemporaneous observations.

The package is deliberately narrow in scope, and uses existing Astropy-ecosystem data structures and APIs:

Installation

Published releases are available on PyPI. The package can be installed by running

pip install specmangle

For development:

git clone https://github.com/cgobat/specmangle.git
cd specmangle
python -m pip install -e .

Basic use

import astropy.units as u
from astropy.nddata import StdDevUncertainty
from astropy.table import Table
from specutils import Spectrum

from specmangle import mangle

spectrum = Spectrum(
    spectral_axis=wavelength * u.AA,
    flux=flux * u.erg / u.s / u.cm**2 / u.AA,
    uncertainty=StdDevUncertainty(
        flux_error,
        unit=u.erg / u.s / u.cm**2 / u.AA,
    ),
)

photometry = Table(
    rows=[
        ("SLOAN/SDSS.g", 18.42, 0.03),
        ("SLOAN/SDSS.r", 18.17, 0.02),
        ("SLOAN/SDSS.i", 18.05, 0.04),
    ],
    names=("band", "mag", "mag_err"),
)

result = mangle(spectrum, photometry)

mangled = result.spectrum
correction = result.correction
print(result.photometry)

The input photometry table must contain:

  • band: passband identifier, normally an SVO FPS filter ID
  • mag: AB magnitude

It may additionally contain:

  • mag_err: 1-sigma magnitude uncertainty. Masked values are treated as missing uncertainties.

Additional columns are preserved in result.photometry, which adds used, coverage, skip_reason, synthetic_mag, synthetic_mag_err, and residual columns. Bands that do not meet min_coverage are excluded from the fit and reported explicitly in these diagnostic columns.

Mangling model

The correction is positive and multiplicative. It is represented by a natural cubic spline in log wavelength and log flux scale, with one free anchor at the pivot wavelength of each unique passband. The correction is held constant beyond the bluest and reddest anchors rather than extrapolating the spline.

The fit minimizes the difference between the mangled spectrum's synthetic AB magnitudes and the supplied photometry. When uncertainties are available, photometric uncertainties and synthetic-photometry uncertainties propagated from the spectrum are combined in quadrature.

A nonzero smoothness can be supplied to penalize changes in slope between neighboring spline anchors:

result = mangle(spectrum, photometry, smoothness=0.1)

The default is smoothness=0.0, so no additional regularization is imposed.

The fitted correction can be evaluated on any wavelength grid:

correction = result.correction_at(new_wavelength)

History & heritage

The term "spectral mangling" is commonly used for smooth wavelength-dependent corrections that are applied to a spectrum to make it reproduce contemporaneous broadband photometry. The approach has roots in earlier spectral "warping" methods (e.g. Tonry et al. 2003), was formalized under the "mangling" nomenclature by Hsiao et al. (2007) and Conley et al. (2008), and was later applied directly to spectrophotometric calibration of observed core-collapse supernova spectra by Vincenzi et al. (2019).

Spectral uncertainties

specmangle supports the same uncertainty representations supported by specutils for arithmetic propagation:

  • astropy.nddata.StdDevUncertainty;
  • astropy.nddata.VarianceUncertainty;
  • astropy.nddata.InverseVariance.

For synthetic photometry, uncertainties are converted to standard deviations and spectral samples are assumed to be statistically independent. Masked spectral samples are omitted.

The uncertainty stored on result.spectrum is the input spectral uncertainty scaled by the best-fit mangling correction. Uncertainty in the correction itself is returned separately as result.correction_uncertainty, because errors in the fitted correction are correlated across wavelength.

Bandpasses

By default, values in the band column are passed to the SVO Filter Profile Service:

from specmangle import Bandpass

bandpass = Bandpass.from_svo("SLOAN/SDSS.g")

Custom response curves can be supplied instead:

import astropy.units as u
from specmangle import Bandpass, mangle

custom = Bandpass(
    filter_id="my/filter",
    wavelength=filter_wavelength * u.AA,
    transmission=filter_transmission,
    detector_type="photon",
)

result = mangle(
    spectrum,
    photometry,
    bandpasses={"my/filter": custom},
)

The synthetic-photometry calculation distinguishes photon-counting and energy-counting response curves. SVO's DetectorType metadata is used automatically for SVO filters.

Passband coverage

By default, at least 98% of the passband's AB-reference signal must fall within the wavelength range covered by the valid spectrum samples. This prevents a synthetic magnitude from silently relying on a substantially incomplete bandpass.

The threshold can be changed with min_coverage:

result = mangle(spectrum, photometry, min_coverage=0.95)

Direct synthetic photometry

For the common/general case of computing an AB magnitude directly from an SVO FPS identifier:

from specmangle import spectrum_to_magnitude

mag = spectrum_to_magnitude(spectrum, "SLOAN/SDSS.r")

If the input Spectrum includes an uncertainty, the function returns (mag, mag_err). Both values carry Astropy magnitude units. An already loaded Bandpass can be supplied instead of an SVO identifier.

The lower-level synthetic-photometry result is also exposed when coverage and other diagnostics are useful:

from specmangle import Bandpass, synthetic_ab_magnitude

bandpass = Bandpass.from_svo("SLOAN/SDSS.r")
synthetic = synthetic_ab_magnitude(spectrum, bandpass)

print(synthetic.magnitude)
print(synthetic.uncertainty)
print(synthetic.coverage)

Result object

mangle returns a MangleResult containing:

  • spectrum: the mangled specutils.Spectrum
  • correction: multiplicative correction evaluated at each input spectral sample
  • correction_at(wavelength): evaluate the fitted correction at arbitrary wavelengths
  • correction_uncertainty: estimated 1-sigma uncertainty in that correction, when identifiable
  • anchor_wavelengths: spline-anchor pivot wavelengths
  • log_correction_parameters: fitted log-correction values at the anchors
  • parameter_covariance: fitted parameter covariance when absolute uncertainties are available
  • photometry: the input table with synthetic-photometry diagnostics appended
  • bandpasses: the bandpasses used in the fit
  • optimizer: the underlying scipy.optimize.OptimizeResult

Metadata

Release files for specmangle 0.1.0b2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for specmangle 0.1.0b2
File Size Uploaded Provenance
specmangle-0.1.0b2.tar.gz 17.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for specmangle 0.1.0b2
File Interpreter ABI Platform
specmangle-0.1.0b2-py3-none-any.whl Python 3 none any Details

Total release size: 33.9 kB

Release files / specmangle-0.1.0b2.tar.gz

Download URL specmangle-0.1.0b2.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
675622227f8b7151421f11de2a8aae56cd2891162bf13b5acc8e6450d2db529c
BLAKE2b-256 checksum
How to use checksums
cb6e6a6fd295055959e9c7b8f0bf320e0981e6e86d57b5c4349605154ba24e00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / specmangle-0.1.0b2-py3-none-any.whl

Download URL specmangle-0.1.0b2-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c85ebecd7e4342a6da64ca1262cc055b634e9c9bfd7428fd144aadf3d9197274
BLAKE2b-256 checksum
How to use checksums
b0df0279cb7a0f770bbef3734cb8ef89f8aa139a3f9207596594c26c89f2a6e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Security for specmangle 0.1.0b2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for specmangle-0.1.0b2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page