Skip to content

Combine in bands, opening each image once - #193

Merged
mwcraig merged 3 commits into
mainfrom
banded-combine
Sep 20, 2026
Merged

mwcraig merged 3 commits into
mainfrom
banded-combine

Conversation

@mwcraig

@mwcraig mwcraig commented Sep 19, 2026

Copy link
Copy Markdown
Owner

Combiner hands the list of files to ccdproc.combine, which calls
CCDData.read on every file for every chunk of the image it works on. The
number of chunks is proportional to the number of files, so the time grows as
the square of the number of images. Students combine 10 frames per filter,
sometimes 40, and at 40 one filter took almost four minutes. The upstream
issue is astropy/ccdproc#1012; this is the fix on the reducer side.

  • New private reducer.astro_gui._combine_in_bands. It opens each file
    once (fits.open(..., memmap=False) in an ExitStack), reads one band of
    rows at a time with hdu.section, and combines each band with a
    ccdproc.Combiner set up the way ccdproc.combine sets it up (scaling,
    then min/max clipping, then sigma clipping). The band height comes from the
    same arithmetic ccdproc.combine uses for its chunks, so mem_limit means
    what it meant before. Every pixel of the result depends only on that pixel
    in the inputs (and, with scaling, on one number per whole image, found in a
    first pass that reads one image at a time), so the result is identical.

  • It declines, and Combiner falls back to ccdproc.combine, whenever it
    cannot promise the same behaviour: a MASK, UNCERT or PSF extension in
    any file, no usable BUNIT, units or shapes that differ, an image that is
    not a 2D image in the first HDU, or too many files to have open at once
    (EMFILE). In those cases nothing changes, including the errors raised.

  • Nothing else changes. The keyword arguments the widget builds are passed
    through as they were, and the header, master keyword, unit and WCS of the
    written file are the same.

Measurements

Real 4096 x 4096 float32 frames, through the Combiner widget, default
mem_limit of 5e7, macOS, warm page cache:

10 images: before / after 40 images: before / after
average 15.6 s, 475 MB / 1.2 s, 343 MB 227.8 s, 403 MB / 4.2 s, 333 MB
median not measured / 5.0 s, 326 MB
median, 3-sigma clip 26.9 s, 509 MB / 4.8 s, 378 MB not measured / 15.6 s, 384 MB
median, sigma and min/max clip, scaled by median 28.1 s, 636 MB / 6.9 s, 614 MB not measured / 25.4 s, 601 MB

(Memory is peak RSS of the process.) The FITS file written was identical, in
every header card and every pixel, for the three 10-image cases and for the
40-image average. The roughly 600 MB peak with scaling is the scaling function
itself (1 / np.ma.median of a whole frame) and is the same before and after.

Tests

reducer/tests/test_astro_gui.py, the first tests of Combiner. They check
that the banded result is bitwise identical to ccdproc.combine with the same
arguments for average and median, for uint16 and float32 inputs, with
sigma clipping, min/max clipping, scaling by a function or an array, and all
of them together; that the band height does not matter, down to one row; that
each file is opened exactly once and CCDData.read is never called; each of
the cases in which it declines; and that the file the widget writes is the
same, header and pixels, as the one it writes when forced down the old path.

One of them covers a bug found while reviewing this: reading hdu.data (for
a scaling function) and then hdu.section fails in astropy 7.1 for an integer
image with a BSCALE and a BLANK, so the whole image is read as a section
too.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FHBWdw5Yp7xBnwbyqi1naU

mwcraig and others added 3 commits September 19, 2026 09:56
Combiner handed the list of files to ccdproc.combine, which calls
CCDData.read on every file for every chunk of the image it works on. The
number of chunks grows with the number of files, so the time grew as the
square of it: 40 images that are 4096 x 4096 took 228 seconds with the
default memory limit.

Add _combine_in_bands, which opens each file once, reads a band of rows
at a time with hdu.section, and combines each band with a
ccdproc.Combiner set up the way ccdproc.combine sets it up. The band
height comes from the same arithmetic ccdproc uses for its chunks. The
same 40 images now take 4 seconds, the file that is written is identical
in every header card and pixel, and peak memory is no higher.

It declines, and Combiner falls back to ccdproc.combine, for images with
a mask, uncertainty or PSF extension, without a usable BUNIT, with units
or shapes that differ, whose image is not a 2D image in the first HDU,
or when there are too many files to have open at once. The whole image
needed by a scaling function is read as a section as well, because
reading hdu.data leaves astropy unable to read a section of an integer
image that has a BSCALE and a BLANK.

Add the first tests of Combiner, which compare the banded result with
ccdproc.combine for each method and each kind of clipping and scaling,
and the file the widget writes with the one it wrote before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHBWdw5Yp7xBnwbyqi1naU
astropy warns each time it makes a WCS from a header it has to fix, a
deprecated RADECSYS keyword for example, and ccdproc.combine makes a WCS
for every file for every chunk of the image. Combining 10 images that are
4096 x 4096 sent 1,620 warnings to the notebook for each filter, and 40
images about 25,000, which was enough to freeze the browser and the
server behind it. Issuing them also cost time: the 10 image combine took
22.6 seconds in a kernel with the warnings and 15.4 without.

Combiner.action now ignores FITSFixedWarning while it works, whichever
way the images are combined, and puts the warning filters back when it
is done.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHBWdw5Yp7xBnwbyqi1naU
Images that have been shifted to line them up have NaN where there is no
data. Sigma clipping leaves those pixels out of the combined image, which
is what is wanted, but astropy warns that it clipped invalid values each
time it is called: once for each filter when combining in bands, and once
for each chunk with ccdproc.combine, 121 times for 10 images that are
4096 x 4096. Where every image is NaN, ccdproc also divides zero by zero
when it works out the uncertainty, and numpy warns about that.

Combiner.action now ignores those two warnings, matched by message and
category, along with FITSFixedWarning. The combined image is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHBWdw5Yp7xBnwbyqi1naU
@mwcraig
mwcraig merged commit 208645e into main Sep 20, 2026
2 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