Repository navigation
Combine in bands, opening each image once - #193
Merged
Merged
Conversation
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
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.
Combinerhands the list of files toccdproc.combine, which callsCCDData.readon every file for every chunk of the image it works on. Thenumber 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 fileonce (
fits.open(..., memmap=False)in anExitStack), reads one band ofrows at a time with
hdu.section, and combines each band with accdproc.Combinerset up the wayccdproc.combinesets it up (scaling,then min/max clipping, then sigma clipping). The band height comes from the
same arithmetic
ccdproc.combineuses for its chunks, somem_limitmeanswhat 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
Combinerfalls back toccdproc.combine, whenever itcannot promise the same behaviour: a
MASK,UNCERTorPSFextension inany file, no usable
BUNIT, units or shapes that differ, an image that isnot 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,
masterkeyword, unit and WCS of thewritten file are the same.
Measurements
Real 4096 x 4096 float32 frames, through the
Combinerwidget, defaultmem_limitof 5e7, macOS, warm page cache:(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.medianof a whole frame) and is the same before and after.Tests
reducer/tests/test_astro_gui.py, the first tests ofCombiner. They checkthat the banded result is bitwise identical to
ccdproc.combinewith the samearguments for average and median, for
uint16andfloat32inputs, withsigma 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.readis never called; each ofthe 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(fora scaling function) and then
hdu.sectionfails in astropy 7.1 for an integerimage with a
BSCALEand aBLANK, so the whole image is read as a sectiontoo.
🤖 Generated with Claude Code
https://claude.ai/code/session_01FHBWdw5Yp7xBnwbyqi1naU