Skip to content

Add utu.spectrum.contribution_function_line(), the contribution function of an observed line - #19

Merged
roytsmart merged 2 commits into
mainfrom
feature/contribution-function-line
Nov 13, 2026
Merged

roytsmart merged 2 commits into
mainfrom
feature/contribution-function-line

Conversation

@roytsmart

@roytsmart roytsmart commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Adds utu.spectrum.contribution_function_line(), the contribution function of an observed line, named by its ion and its wavelength. Also requires fiasco 0.9, which lets the ratio of protons to electrons be given to each ion as it is built.

Why

contribution_function() returns every transition of an ion and leaves picking one out to the caller. Two papers now do that picking themselves, each with its own rule:

  • the ESIS-II science paper matches within 0.5 mÅ and sums the blends;
  • a figure in progress for the ESIS instrument paper matches exactly.

This function gives both of them one rule.

What it does

  • Lines are given by ion and wavelength, which broadcast against each other. Several lines are an ion and a wavelength for each along the same axis, or one ion and several wavelengths. Each line is the sum of every transition of its ion within tolerance of its wavelength, so a blend is counted whole.
  • Tolerance: the default is 0.01 mÅ.
    • CHIANTI lists some wavelengths to 0.1 mÅ, and many pairs of transitions 0.5 mÅ apart (142 pairs in Mg X, 133 in Si XII, 98 in O VII). A tolerance of 0.5 mÅ would take in such a neighbor or not depending on rounding.
    • No two distinct wavelengths of one ion in the database are closer than 0.1 mÅ. So 0.01 mÅ gathers every transition listed at the wavelength and none beside it, as long as the wavelength is given as CHIANTI lists it.
    • Every line both papers use is listed exactly, with no other transition within 2 mÅ.
  • Checked before anything is solved:
    • a wavelength with no transition raises ValueError before any ion is solved;
    • so do a temperature that is empty or has another axis, size_chunk below 1, lines along the temperature axis, and a density along the lines or along the temperature and another axis.
  • The result is laid out as contribution_function() lays it out, so it can be handed to synthesize(). The inputs are the wavelengths, a copy of their own, and the outputs are the contribution functions along axis_temperature and the axes of the lines. Two differences, since this is new API:
    • a scalar density is the same density at every temperature and adds no axis;
    • a density on an axis of its own keeps the name of that axis, where contribution_function() calls it _density.
  • No lines at all is an empty result with the right shape and unit.
  • Cost:
    • an ion with more than one of the lines is built and solved once, even under two names, "O 5" and "O V";
    • the ratio of protons to electrons is computed once if it isn't given;
    • size_chunk solves the temperatures a few at a time to bound the memory of an ion with hundreds of levels, which the ESIS-II paper needs for Si XII in CI, and it leaves the result unchanged.

fiasco 0.9

fiasco 0.9.0 (wtbarnes/fiasco#471) makes the ratio of protons to electrons an argument of fiasco.Ion, which next_ion() passes on. Under 0.8, the ion that fiasco builds to recombine from computed its own ratio, walking the whole database, once per call and once per chunk. Measured on 21 temperatures with the full database, with the ratio given:

fiasco 0.8.3 fiasco 0.9.0
Mg X contribution function 25 s 3.6 s
Si XII contribution function 25 s 3.6 s
fiasco.proton_electron_ratio() 19 s 2.7 s

The ratios and contribution functions of O V, Mg X and Si XII are bit-identical between the two versions.

fiasco 0.9 also computes the ratio as an ion is built unless it is given one, and the ratio is now a property, so the old priming through __dict__ does nothing. So:

  • contribution_function() sets a ratio it is given through the property, which also reaches the ion it recombines from. Its docstring now says to give the ratio to fiasco.Ion instead.
  • contribution_function_line() and lines() give the ratio to each ion as it is built. lines() broadcasts it against the temperature first, so that a ratio that doesn't fit fails, rather than as each ion is built, where a failure means the ion is skipped.
  • ions() gives each ion a ratio it never reads. Without one, listing the 495 ions of the database would walk it once per ion, about 21 minutes.

The requirement is fiasco~=0.9.0. esis and both papers require fiasco~=0.8.2, so they keep utu 0.6 until they raise it.

Tests

Sixteen new tests in test_lines.py, all comparing whole arrays with exact equality:

  • Three lines (two of O V, one of Mg X) match the sum of the transitions listed at their wavelengths, picked out of contribution_function() by hand. The second is a blend of two O V transitions both listed at 122.133 Å, and the test checks that it really does match two.
  • Chunks of 1 and 4 temperatures (the last chunk short) give the same result as one chunk, with the ratio given with its axis in one case and without it in the other.
  • A single line given as a plain string and quantity, without the ratio, matches the first of the three.
  • The lines of O V on either side of the line of Mg X, one of them named "O V", and one ion given once for two wavelengths, match the three lines, and each ion is solved once. The inputs can be written.
  • A scalar density adds no axis and matches the same density along the temperature.
  • No lines gives an empty result without the database.
  • A density on its own axis keeps its name and matches contribution_function().
  • A wavelength with no transition raises, as do the six invalid arguments above.
  • A ratio given to contribution_function() replaces the ion's, and the ion it recombines from has it too.

The existing tests now give the ratio to fiasco.Ion. All 24 tests in test_lines.py and the rest of the suite pass locally with fiasco 0.9.0 against the full CHIANTI database. Ruff and black pass. The docstring example uses only O V and Mg X, which the docs' small database includes, and it runs locally.

🤖 Generated with Claude Code

https://claude.ai/code/session_01BYjDL98znSud1yFh9chnkP

…ction of an observed line

`contribution_function()` returns every transition of an ion and leaves
picking one out to the caller. The new function takes the lines by their
ion and wavelength, along axes of their own, and sums the contribution
functions of every transition of the ion within a tolerance of each
wavelength, half a milliangstrom by default, so that a blend is counted
whole. A wavelength with no transition there is an error.

The result is laid out as `contribution_function()` lays it out, the
wavelengths as the inputs and the contribution functions along the
temperature and the lines as the outputs, so that it can be handed to
`synthesize()`.

An ion with more than one of the lines is built once. The ratio of
protons to electrons is computed once if it is not given, and
`size_chunk` solves the temperatures a few at a time to bound the memory
of an ion with hundreds of levels, which changes nothing in the result.

The tests check each line against the sum of the transitions picked out
of `contribution_function()` by hand, exactly, including a blend of two
transitions of O V at 122.133 angstroms, and that chunks, a density on
its own axis, a single line given as plain values, and the ratio given
with or without its axis all agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BYjDL98znSud1yFh9chnkP
@read-the-docs-community

read-the-docs-community Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (08f29ee) to head (f2bc310).

Additional details and impacted files
@@            Coverage Diff             @@
##              main       #19    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           21        21            
  Lines         1537      1698   +161     
==========================================
+ Hits          1537      1698   +161     

☔ 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.

…line()`

fiasco 0.9.0 takes the ratio of protons to electrons as an argument of
`fiasco.Ion`, and `next_ion()` passes it on, so the ion that fiasco
builds to recombine from no longer walks the whole database. It also
computes the ratio as an ion is built unless it is given one, and the
ratio is now a property, so priming it through `__dict__` does nothing.

- `contribution_function()` sets a ratio it is given through the
  property, which also reaches the ion it recombines from.
- `contribution_function_line()` and `lines()` give the ratio to each
  ion as it is built. `lines()` broadcasts it against the temperature
  first, so that a ratio which does not fit fails rather than skipping
  every ion.
- `ions()` gives each ion a ratio it never reads, so that listing the
  database does not walk it once for each of five hundred ions.

From the review of `contribution_function_line()`:

- The default tolerance is a hundredth of a milliangstrom, not half of
  one. CHIANTI lists some wavelengths to a tenth of a milliangstrom,
  and many pairs of transitions half a milliangstrom apart, so half a
  milliangstrom could take in a neighbor or not depending on rounding.
  No two distinct wavelengths of one ion in the database are closer than
  a tenth of a milliangstrom.
- Every line is matched to its transitions before any ion is solved, so
  that a wavelength with no transition fails at once.
- The lines are grouped by the ion rather than by its name, so that
  "O 5" and "O V" are solved once.
- The result is allocated before anything is solved, so that no lines
  at all is an empty result rather than outputs of None.
- A scalar density is the same density at every temperature and adds
  no axis, and a density on an axis of its own keeps the name of that
  axis. A density along the lines, or along the temperature and another
  axis, is an error.
- An empty temperature is an error of its own.
- The inputs of the result are a copy, which can be written.
- The docstring says what the arguments of fiasco may not do under
  `size_chunk`, and names the line of Mg X in the example correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BYjDL98znSud1yFh9chnkP
@roytsmart
roytsmart merged commit 91a4742 into main Nov 13, 2026
13 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.

1 participant