feat: cache sparse-path scalars + chunked SparseTerms accumulation (#588) - #589
Merged
Merged
Conversation
…ion (#588) Phase 1 of Discussion PyAutoLabs/13. InterferometerSparseOperator carries data_term and noise_normalization; fast_chi_squared and FitInterferometer.noise_normalization read them when present (fallback unchanged, bit-identical). Add SparseTerms, sparse_terms_from_chunks and Interferometer.apply_sparse_operator_from_chunks (chunked accumulation of W~, dirty image, dirty beam and the scalars, verified against the one-shot path at 1e-15). DatasetInterface(data=None) marks unsubtracted data; the noise real/imag check is a shared helper run per chunk. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Jammy2211
pushed a commit
that referenced
this pull request
Sep 30, 2026
Resolve the append/append conflict in test_interferometer.py by keeping both sides: #586's operated_mapping_matrix_list transform-once test and main's #588/#589 fast_chi_squared data_term tests. interferometer/abstract.py auto-merged: @cached_property on operated_mapping_matrix_list (#586) sits alongside main's noise_map-based override shape check and data=None term-3 path (#588/#589). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1 of GitHub Discussion https://github.com/orgs/PyAutoLabs/discussions/13 ("Streaming visibilities for memory efficiency", HRSAstro). Closes #588.
On the sparse interferometer path the inversion is independent of N_vis, but every likelihood call still reduced over the full visibility arrays twice:
fast_chi_squaredterm 3 (sum(d_r^2/sigma_r^2) + sum(d_i^2/sigma_i^2), a data-only constant) andFitInterferometer.noise_normalization(sum(log 2 pi sigma^2)). This PR:InterferometerSparseOperator(data_term,noise_normalization) whenapply_sparse_operatorbuilds it, and reads them per likelihood call, with the previous reductions as the fallback (results are bit-identical);SparseTerms(per-visibility sums: W~, dirty image, dirty beam, sum of weights, the two scalars, n_vis) with field-wise addition,sparse_terms_from_chunks(...)over(uv_wavelengths, data, noise_map)chunks, andInterferometer.apply_sparse_operator_from_chunks(...);DatasetInterface(data=None)express "nothing was subtracted from the data" so a pixelization-only fit (PyAutoGalaxy PR) never touches an N_vis array per evaluation;check_noise_map_real_imag_equalso it runs once per chunk.Parity witness (1e5 visibilities, 616-pixel mask, 25 chunks of 4096): chunked vs one-shot W~ 5.8e-16 rel, dirty image 3.0e-15 rel, data_term 1.5e-16 rel, noise_normalization 2.7e-16 rel;
fast_chi_squaredwithdata=Nonevs the array path 1.5e-16 rel (exactly 0 on the same operator).Phase 2 (array-free
Interferometer.from_stream, save/aggregator/visualizer contract) is filed as a Mind follow-up. Reference implementation: https://github.com/HRSAstro/pyuvimagesrc/pyuvimage/streaming.py.API Changes
Additive only. New public names:
aa.SparseTerms,sparse_terms_from_chunks,check_noise_map_real_imag_equal,InterferometerSparseOperator.from_sparse_terms,Interferometer.apply_sparse_operator_from_chunks.InterferometerSparseOperatorgains two optional trailing fields (data_term,noise_normalization, defaultNone);from_nufft_precision_operatorgains matching keyword args.DatasetInterfaceacceptsdata=None(only with a sparse operator carryingdata_term);fast_chi_squaredandFitInterferometer.noise_normalizationread the cached scalars when present. No symbol removed, no default changed.See full details below.
Test Plan
pytest test_autoarray— 1756 passed (jax 0.10.2 importable, jax-marked sparse tests ran)inversion/inversion/interferometer,dataset/interferometer,fit,inversion/inversion/test_factory.py) — 181 passednoise_normalization_complex_from/ direct sum; unequal re/im noise in a later chunk raises;apply_sparse_operatorpopulates both scalars;apply_sparse_operator_from_chunksmatchesapply_sparse_operator;fast_chi_squared(data=None)== array path incl. jax;noise_normalizationscalar/fallbackFull API Changes (for automation & release notes)
Added
autoarray.SparseTerms(frozen dataclass;nufft_precision_operator,dirty_image_native,dirty_beam_native,sum_weights,data_term,noise_normalization,n_vis;__add__field-wise) — per-visibility sums accumulated over chunksautoarray.inversion.inversion.interferometer.inversion_interferometer_util.sparse_terms_from_chunks(chunks, *, real_space_mask, transformer_class=None, method="nufft", eps=None, chunk_size=None, chunk_k=2048, use_jax=False, show_progress=False) -> SparseTermsautoarray.inversion.inversion.interferometer.inversion_interferometer_util.check_noise_map_real_imag_equal(noise_map) -> NoneInterferometerSparseOperator.from_sparse_terms(terms, *, real_space_mask, batch_size=128)Interferometer.apply_sparse_operator_from_chunks(chunks, *, batch_size=128, **accumulator_kwargs) -> InterferometerChanged Signature
InterferometerSparseOperator(..., data_term: Optional[float] = None, noise_normalization: Optional[float] = None)— two optional trailing fieldsInterferometerSparseOperator.from_nufft_precision_operator(nufft_precision_operator, dirty_image, *, batch_size=128, data_term=None, noise_normalization=None)DatasetInterface(data=None, ...)now permitted whensparse_operator.data_termis setChanged Behaviour
Interferometer.apply_sparse_operatorcomputesdata_termandnoise_normalizationonce and stores them on the operator (same values as the per-call reductions)AbstractInversionInterferometer.fast_chi_squaredusessparse_operator.data_termfor term 3 whendataset.data is None; raisesInversionExceptionif neither is available; otherwise unchangedFitInterferometer.noise_normalizationreturnssparse_operator.noise_normalizationwhen set andself.noise_map is self.dataset.noise_map; otherwise unchangedAbstractInversionInterferometer.operated_mapping_matrix_listoverride check readsnoise_map.shape[0]instead ofdata.shape[0](same value; hardening fordata=None)Migration
Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code