Skip to content

fix: Avoid repeated extinction law construction in redden!/deredden! - #48

Merged
icweaver merged 3 commits into
JuliaAstro:mainfrom
Akshat1000Sharma:calling-functions-from-DustExtinction.jl
Jun 27, 2026
Merged

icweaver merged 3 commits into
JuliaAstro:mainfrom
Akshat1000Sharma:calling-functions-from-DustExtinction.jl

Conversation

@Akshat1000Sharma

Copy link
Copy Markdown
Contributor

This PR improves the implementation of redden! and deredden! in src/transforms/redden.jl.

Previously the implementation broadcasted directly over the extinction law type:

@. f = DustExtinction.redden(law, s, f; Rv, Av)

When law is provided as a Type (e.g. the default DustExtinction.CCM89), the broadcast dispatch calls the type-based method of DustExtinction.redden for every wavelength element. This leads to constructing the extinction law instance (e.g. CCM89(Rv=3.1)) repeatedly.

This PR instead constructs the extinction law instance once and then applies the broadcast using that instance:

law_instance = law isa DustExtinction.ExtinctionLaw ? law : law(Rv=Rv)
@. f = DustExtinction.redden(law_instance, s, f; Av)

The same change is applied to deredden!.

Benefits

  • Avoids repeated construction of extinction law objects
  • Keeps the public API unchanged
  • Continues delegating reddening calculations to DustExtinction.jl

Future work

There is ongoing upstream work adding in-place versions of these functions in DustExtinction.jl:

JuliaAstro/DustExtinction.jl#84

Once those functions (redden! / deredden!) are available in a released version, Spectra.jl could simplify this further by delegating directly to those implementations. (Once that happens, it will also fix #45 )

Testing

All existing tests (224/224) pass successfully.

Instantiate the DustExtinction law once before broadcasting instead of passing the law type directly to `DustExtinction.redden` /`DustExtinction.deredden`. This avoids repeated construction of the extinction law when broadcasting over spectral elements while keeping the public API unchanged.
@codecov

codecov Bot commented Mar 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.38%. Comparing base (9905292) to head (c0a91a6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #48      +/-   ##
==========================================
- Coverage   90.74%   90.38%   -0.36%     
==========================================
  Files           8        8              
  Lines         162      156       -6     
==========================================
- Hits          147      141       -6     
  Misses         15       15              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

icweaver added a commit to JuliaAstro/DustExtinction.jl that referenced this pull request Jun 27, 2026
@icweaver

Copy link
Copy Markdown
Member

Thanks for the contribution! I've upstreamed the instance logic to DustExtinction.jl to make the changes here a bit more general. Should be good to go after the next version there is registered JuliaRegistries/General#159484

@icweaver icweaver changed the title Avoid repeated extinction law construction in redden!/deredden! fix: Avoid repeated extinction law construction in redden!/deredden! Jun 27, 2026
@icweaver
icweaver merged commit e3b1ef6 into JuliaAstro:main Jun 27, 2026
6 of 11 checks passed
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.

To-do: Use redden! and deredden!

2 participants