Make Instrument indexable via the na.Indexable mixin#61
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Documentation preview available at https://esis-mission.github.io/esis-pr-previews/61-feature/select-channel |
|
Cross-referencing a parallel approach to the same problem: sun-data/named-arrays#180 adds a generic One important behavioral difference worth reconciling: Worth deciding whether |
|
Follow-up: the With that in, |
1911ef5 to
bdc6985
Compare
|
Reworked per @roytsmart's review: instead of the standalone The per-leaf missing-axis filtering the old helper did is no longer needed thanks to #181 ( Since this needs Net diff is now just |
AbstractInstrument now inherits named_arrays.Indexable, giving every
instrument a recursive named __getitem__/isel/shape that indexes its fields
(rebuilding dataclasses via dataclasses.replace, so the result is a new
object and cached properties such as `system` are dropped).
A single channel can then be selected generically with
instrument.isel(channel=...) (equivalently instrument[{axis_channel: ...}]),
which extends to any future vectorized selection without a bespoke method
per axis.
Requires named-arrays >= 1.6 (na.Indexable, #180/#181); pin bumped accordingly.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
bdc6985 to
47abccd
Compare
|
Per discussion, dropped The ESIS-side change is now a single line (inherit |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Summary
Adds a reusable way to reduce a multi-channel
esis.optics.Instrumentto a single channel.esis.optics.dataclass_select(instance, item)— a recursive helper that indexes everynamed_arrays.AbstractArrayfield of a dataclass along a named axis. It recurses into nested dataclasses and intodict/list/tuplecontainers (e.g. the ruling spacingcoefficientsdict), and leaves arrays that don't carry the indexed axis untouched (some named-array types such asFunctionArrayraise on a missing axis, so the index is filtered to axes each array actually has).AbstractInstrument.select_channel(channel)— a copy-safe method that deep-copies the instrument, drops any cachedsystem, and usesdataclass_selectto eliminate the channel axis.This generalizes the manual per-channel indexing in
esis.flights.f1.optics.design_singleand the ad-hoccopy.deepcopyloop used to model/fit individual channels, and is broadly useful for single-channel modeling, inversion, and diagnostics.The pattern follows
dataclass_selectfrom the siblingmulti-slit-solar-explorerrepo, extended to handle dict/list/tuple containers and the missing-axis case.Tests
esis/optics/_select_test.py:dataclass_select(axis reduction, missing-axis pass-through, dict/nested recursion, non-array fields untouched);select_channeltest over all four channels covering the dict-stored ruling coefficient, copy-safety (original left unmodified), channel-axis removal, and that the reduced instrument still resolves into an optical system.Plus
test_select_channeladded toAbstractTestAbstractInstrument.pytest esis/opticspasses (191 tests);ruffandblackclean.🤖 Generated with Claude Code