Optimize stack_images: pre-allocate + fill, add get_image(..., out=)#35
Open
YanLogovskiy wants to merge 3 commits intoneuro-ml:masterfrom
Open
Optimize stack_images: pre-allocate + fill, add get_image(..., out=)#35YanLogovskiy wants to merge 3 commits intoneuro-ml:masterfrom
YanLogovskiy wants to merge 3 commits intoneuro-ml:masterfrom
Conversation
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.
Reduce peak memory during DICOM series stacking by pre-allocating a single buffer and filling it slice-by-slice instead of building a list of arrays and calling
np.stack. Add optionaloutargument toget_imageso callers can write directly into the result buffer.Changes
dicom_csv.misc.stack_imagesnp.stack(list(map(get_image, series)), axis)with one pre-allocated array and slice-by-slice fill.axis in (0, 1, 2)(includingaxis=-1): allocate once withnp.empty(...), copy the first slice withnp.copyto, then fill the rest withget_image(series[i], ..., out=out[...]).np.stack.dicom_csv.misc.get_imageout: Optional[np.ndarray] = None.outvianp.copyto(out, array)andoutis returned.outare unchanged (backward compatible).Scripts
scripts/benchmark_stack_images.py— benchmark fordcmread,pixel_array,get_image,stack_images(with breakdown forget_image_loopvsnp_stack); supports--path(real DICOM) and--synthetic(temp series).scripts/generate_compressed_dataset.py— generate synthetic series with optional compression (RLE, JPEG-LS) for benchmarking on compressed data.Cleanup
partialimport inmisc.py.Rationale
np.stackresult (same size), so peak usage duringstack_imageswas about double the series size. The new path uses one buffer and fills it viaout=, avoiding the list and the extra full-size allocation.stack_imagescontract.Impact
Memory (per series):
At 1500 studies/day, peak memory during
stack_imagesis reduced by this amount per series; with multiple parallel workers, the saving scales with the number of concurrent series.CPU: No measurable speedup in current benchmarks; most time is in N
get_imagecalls (pixel_array + rescale) and copying into the buffer. The gain is in memory and in a cleaner path for future optimizations (e.g. direct decode into buffer).Backward compatibility
stack_images(series, axis=-1, to_color_space=None)signature unchanged.get_image(instance, to_color_space=None)withoutoutbehaves as before.get_image(..., out=...)is optional and does not affect existing callers.How to verify
python scripts/benchmark_stack_images.py --synthetic --slices 500 --rows 512 --cols 512 --runs 2stack_images(series, axis=k)matchesnp.stack([get_image(ds) for ds in series], axis=k)for axis -1, 0, 1, 2.