Skip to content

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

Open
Akshat1000Sharma wants to merge 1 commit into
JuliaAstro:mainfrom
Akshat1000Sharma:calling-functions-from-DustExtinction.jl
Open

Avoid repeated extinction law construction in redden!/deredden!#48
Akshat1000Sharma wants to merge 1 commit into
JuliaAstro:mainfrom
Akshat1000Sharma:calling-functions-from-DustExtinction.jl

Conversation

@Akshat1000Sharma
Copy link
Copy Markdown

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
Copy link
Copy Markdown

codecov Bot commented Mar 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.85%. Comparing base (58828d3) to head (db9daea).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #48      +/-   ##
==========================================
+ Coverage   90.74%   90.85%   +0.11%     
==========================================
  Files           8        8              
  Lines         162      164       +2     
==========================================
+ Hits          147      149       +2     
  Misses         15       15              

☔ View full report in Codecov by Sentry.
📢 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.

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!

1 participant