Skip to content

Speed up synthetic photometry by tabulating grid spectra and bandpasses - #174

Merged
jluastro merged 5 commits into
MovingUniverseLab:devfrom
wei-lingfeng:synphot-speedups
Nov 13, 2026
Merged

jluastro merged 5 commits into
MovingUniverseLab:devfrom
wei-lingfeng:synphot-speedups

Conversation

@wei-lingfeng

@wei-lingfeng wei-lingfeng commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Since the move to synphot, generating isochrone photometry takes about 4.7× longer: 554 s instead of 117 s for a 1547-star IsochronePhot in four HST filters. Two causes:

  1. Synthetic photometry (fixed here): interpolated grid spectra and stsynphot bandpasses are trees of synphot models, every node carrying a FITS header. A spectrum interpolated from 8 grid spectra is a 580-node tree, and an HST bandpass is a product of its throughput components. synphot merges and deep-copies all that metadata in every arithmetic operation and Observation, about 1,100 header copies per 10 magnitudes, which dominated photometry.
  2. Reading grid spectra (fixed upstream): stsynphot.grid_to_spec re-reads its up-to-8 grid files on every call, and each synphot read costs about 21 ms against about 4 ms in pysynphot. Cache grid spectra read by grid_to_spec spacetelescope/stsynphot_refactor#244 caches the grid spectra; it is merged but not yet released.

Changes

  • synphot_bridge.tabulate() returns the same spectrum or bandpass as one Empirical1D table on its own waveset. Negative values are kept, not clipped, and the top-level meta is kept. A bandpass becomes a plain SpectralElement, because stsynphot's ObservationSpectralElement cannot be built without an obsmode.
  • atmospheres.grid_to_spec() wraps stsynphot.grid_to_spec with it, and all grid lookups in atmospheres.py use the wrapper.
  • get_filter_info() returns the tabulated bandpass, after the Vega zero point has been computed as before.

Results

Magnitudes are unchanged: identical to the last bit for 30 test stars in four HST filters, and every column of a full isochrone is identical.

Per star Before After
Photometry, per filter ~34 ms ~4.7 ms
Full IsochronePhot (1547 stars, 4 filters) Time
dev 554 s
This PR 410 s
This PR + stsynphot_refactor#244 (measured on 155 stars: 38 s → 10 s) ~110 s, about the old pysynphot speed

The speedup in this PR does not depend on the stsynphot version: it works with the stsynphot that dev installs and with the current PyPI release. A 42-star IsochronePhot (four HST filters), with magnitudes identical in all four cases:

stsynphot 1.6.0 (PyPI) stsynphot installed by dev (with #244)
dev 20.8 s 14.9 s
This PR 13.1 s 5.7 s

Tests

All fast tests in test_imf.py, test_atmospheres.py, test_synthetic.py and test_reddening.py pass locally, as do the slow test_isochrone, test_iso_wave and test_IsochronePhot, which compare against the stored reference data. CI currently fails on dev itself, because the runner has no model grids (Missing MISTv1/v1.2-synthpop/).

stsynphot returns interpolated grid spectra and HST bandpasses as trees of
synphot models (a 580-node tree for a spectrum interpolated from 8 grid
spectra), each node carrying a FITS header. synphot merges and deep-copies
that metadata in every arithmetic operation and Observation, ~1,100 header
copies per 10 magnitudes, which dominated synthetic photometry.

- synphot_bridge.tabulate() returns the same spectrum or bandpass as one
  Empirical1D table on its own waveset (negative values kept, top-level
  meta kept). A bandpass becomes a plain SpectralElement.
- atmospheres.grid_to_spec() wraps stsynphot.grid_to_spec with it; all
  grid lookups in atmospheres.py use the wrapper.
- get_filter_info() returns the tabulated bandpass, after the Vega zero
  point is computed as before.

Photometry per star and filter: ~34 ms -> ~4.7 ms. A full IsochronePhot
(1547 stars, 4 HST filters) takes 410 s instead of 554 s and is identical in
every column; the rest of the time is now reading grid spectra in stsynphot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jluastro

Copy link
Copy Markdown
Contributor

These changes should be pushed in stsynphot and I have an issue submitted there. I would suggest submitting a pull request there first and see if we can get it pushed through quickly.

I'll note that the suggested changes remove units (I think) from the calculations; but this has been very useful in catching other types of photometric errors.

@wei-lingfeng

Copy link
Copy Markdown
Contributor Author

Yes, I have already submitted a pull request to stsynphot spacetelescope/stsynphot_refactor#244. Let's see if it gets merged. But the units are not dropped: tabulate() builds the Empirical1D table from Quantities (waveset in Å, flux in synphot's internal PHOTLAM), the same way stsynphot builds its grid spectra. It only drops the nested model tree and the per-node FITS headers, which are what get deep-copied on every operation. I checked raw against tabulated grid spectra:

  • calls return PHOTLAM, flux_unit='flam' returns FLAM, and integrate() returns ph / (s cm²);
  • unit mistakes raise the same errors (IncompatibleSources, UnitConversionError);
  • fluxes are identical (difference 0.0), and magnitudes agree;
  • out-of-range behaviour and DisjointError are unchanged.

wei-lingfeng and others added 3 commits November 10, 2026 13:08
stsynphot_refactor#244 (grid-spectrum cache) is merged but 1.7.0 is not
on PyPI yet. Point pyproject.toml, the docs requirements, the install
instructions and the Dockerfile at the merge commit instead of the
pull request branch on the fork.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This reverts commit a23aedd. Installing stsynphot is out of scope for
this pull request; keep dev's setup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wei-lingfeng

Copy link
Copy Markdown
Contributor Author

This is ready to merge. GitHub reports no conflicts with dev. I merged the current dev (including the SPISEA version check, 01d4dfb) into this branch locally and ran the full test suite: 88 passed, 2 skipped (the COSMIC integration tests; the optional cosmic package is not installed here). The slowest test, test_IsochronePhot, took 149 s, compared with 253 s on a branch without this change.

The PR does not change the installation: it works with the synphot/stsynphot versions dev already installs.

The failing "Fast tests" check is not caused by this PR. Test collection stops with "Missing MISTv1/v1.2-synthpop/" because the CI runner has no SPISEA model grids. Dev's own CI fails the same way.

This PR and #173 do not conflict, so they can be merged in either order.

Keep the tabulating grid_to_spec wrapper with the narrower
ParameterOutOfBounds exception from dev, and use the wrapper in the
new FlameSkimmer loaders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jluastro
jluastro merged commit bc3de60 into MovingUniverseLab:dev Nov 13, 2026
1 check failed
mwhosek pushed a commit that referenced this pull request Nov 14, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mwhosek added a commit that referenced this pull request Nov 14, 2026
@wei-lingfeng
wei-lingfeng deleted the synphot-speedups branch November 18, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants