Skip to content

Warn when MartInverter runs on the methods of the instrument, and why - #52

Merged
roytsmart merged 2 commits into
mainfrom
feature/slow-path-warning
Nov 20, 2026
Merged

roytsmart merged 2 commits into
mainfrom
feature/slow-path-warning

Conversation

@roytsmart

@roytsmart roytsmart commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

MartInverter now warns when an inversion runs on the image and backproject methods of the instrument, in place of its sparse matrices, and says why:

UserWarning: This inversion runs on the `image` and `backproject` methods of the instrument,
which is slower than on its sparse matrices: the instrument provides none, since its sensor
responds differently from pixel to pixel.

An inversion which runs on the matrices gives no warning.

Why

The inverter leaves the matrices for the methods without a word, and the methods are much slower: on the model of the 2019 flight of ESIS, four iterations took 1.0 s on the matrices and 17.7 s on the methods, and on the MART tutorial of esis an iteration takes 0.13 s on the matrices and 10.3 s on the methods.
An inversion which leaves the matrices, for a reason which nothing says, can look like one which hangs, and the reason is often a detail of the instrument or of the guess which is easy to change once it is known.

Changes

  • Each place which gives up on the matrices gives its reason, in place of None:
    • MartInverter._arrays: a subclass which replaces a merit function, images which are not a stack of frames, and images or a guess which cannot be expressed as plain arrays, such as a guess in units of energy for matrices which do not take one,
    • AbstractLinearInstrument._operators_new, IdealInstrument._factors and OptikaInstrument._factors: whatever of the instrument cannot be expressed as its matrices, such as weights with an axis of their own, a grid in a unit which cannot be converted to that of the sensor, a quantum yield which varies with wavelength, or a sensor whose response varies from pixel to pixel or is not proportional to the light.
  • AbstractInstrument._operators_reason gives the reason that an instrument provides no matrices, in a precision, or in the one they are kept in. An instrument which cannot provide them keeps its reason where it would keep them, so the reason costs nothing once they have been asked for.
  • unaligned, among the helpers of the operators: the reason that aligned cannot express a named array as a plain array, such as has the axis 'time', which is not one of ('channel', 'x'). aligned makes its checks through it, so the two cannot drift apart. vertices gives the reason of the component of a grid which cannot be expressed.
  • MartInverter.__call__ gives the warning, as a UserWarning which points at the call of the inverter, as soon as the inversion is known to run on the methods, before any work on them.
  • The error which refuses single precision, which only the matrices provide, gives the same reason, in place of a list of the reasons it could be, and so does the error for images with more than one element along an axis of the instrument or of the scene. MartInverter refuses dtype=None, which numpy.dtype takes for double precision and _operators for the precision kept.

The warning is a UserWarning. A category of its own would let a caller silence it alone, but it would be a new public name of the package, so it is left out here.

Review

The review of this pull request found that the reason of the precision kept was lost for dtype=None, that the reason of the vertices of a grid named one cause for several, that the warning came after the backprojection of a frame on the methods, and that the error for images which are not frames blamed the instrument.
The second commit fixes those.

Tests

The tests of each place which gives up on the matrices now check the reason it gives:

  • the tests of the operators of IdealInstrument and of OptikaInstrument, for every instrument which provides none, check _operators_reason, and that an instrument which provides them gives none,
  • test_aligned_unsupported checks what unaligned says of each array which aligned cannot express, and test_unaligned that it says nothing of one which it can,
  • the tests of MartInverter on the methods of the instrument check the warning and its reason, for each reason which the inverter or the instrument can give, and the tests of single precision check the reason in the error.

Four tests are new:

  • test_optika_operators_response_axis: the reason of an instrument whose sensor responds along an axis of its own,
  • test__call__operators_silent: an inversion on the matrices gives no warning,
  • test__call__operators_undefined: an instrument which defines no matrices is inverted on its methods, with a warning which says so, and the warning of the limit of the iterations still follows it,
  • test__call__axis_scene_many: images with more than one element along an axis of the scene are refused, with the reason that they cannot run on the matrices.

Run with this warning turned into an error, so that no inversion gives it where a test does not expect it, 598 tests pass, with every line covered.

🤖 Generated with Claude Code

https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn

`MartInverter` runs on the sparse matrices of the instrument when it
can, and otherwise on the `image` and `backproject` methods of the
instrument, without a word. The methods are much slower: on the model
of the 2019 flight of ESIS, four iterations took 1.0 s on the matrices
and 17.7 s on the methods, and on the MART tutorial of esis an
iteration takes 0.13 s on the matrices and 10.3 s on the methods. An
inversion which leaves the matrices, for a reason which nothing says,
can look like one which hangs.

It now warns when it runs on the methods, with the reason that it
cannot run on the matrices, such as

    This inversion runs on the `image` and `backproject` methods of the
    instrument, which is slower than on its sparse matrices: the
    instrument provides none, since its sensor responds differently
    from pixel to pixel.

Each place which gives up on the matrices gives its reason, in place
of `None`. The inverter gives one for a subclass which replaces a
merit function, for images which are not a stack of frames, and for
images or a guess which cannot be expressed as plain arrays. The
instrument gives one for whatever of it cannot be expressed as its
matrices: its weights, the vertices of its grids, its quantum yield or
the response of its sensor, its read noise. `_operators_reason` hands
it on.
The reason that an array cannot be expressed as a plain array, such as
"has the axis 'time', which is not one of ('channel', 'x')", comes from
`unaligned`, the twin of `aligned` among the helpers of the operators.

The warning is a `UserWarning`, which points at the call of the
inverter. The error which refuses single precision where the matrices
cannot be used gives the same reason, in place of a list of the
reasons it could be.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##              main       #52    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           29        29            
  Lines         4292      4437   +145     
==========================================
+ Hits          4292      4437   +145     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Follows the review of the pull request.

`aligned` makes its checks through `unaligned`, so what `unaligned` says
of an array is why `aligned` refused it, and the two cannot drift apart.
`vertices` gives the reason that a component of a grid cannot be
expressed as a plain array, so an instrument whose scene is in a unit
which cannot be converted to that of its sensor says so, where it was
said to have a grid with an axis of its own.

`_operators_reason` takes `None` for the precision that the matrices are
kept in, as `_operators` does, and gives the reason of that precision,
where it gave "it does not define any". `MartInverter` refuses
`dtype=None`, which `numpy.dtype` takes for double precision, and which
reached `_operators` as the precision kept.

The warning comes as soon as the inversion is known to leave the
matrices, before the backprojection of the first frame on the methods
of the instrument, which can take long for a large one. The error for
images with more than one element along an axis of the instrument or
of the scene gives the reason that the inversion left the matrices,
where it said that the instrument cannot be expressed as them, which is
not the reason for images with an axis of the scene.

The reason of a sensor whose ratio of the variance to the signal strays
too far keeps the digits of a tolerance below a tenth of a percent, and
the tests which count the warnings of an inversion count those of the
inverter alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn
@roytsmart
roytsmart merged commit 44a2f61 into main Nov 20, 2026
12 checks passed
@roytsmart
roytsmart deleted the feature/slow-path-warning branch November 20, 2026 22:10
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