Skip to content
Home » Fast Sigma Clipped Averaging with astro-sca

Fast Sigma Clipped Averaging with astro-sca

Southern Pinwheel Galaxy (M83)
Andromeda Galaxy - Messier 31

One of the most effective ways to improve the quality of astrophotography images is through image averaging.  While this method is effective in increasing the signal to noise ratio (SNR), it does not necessarily do well for eliminating outlier data. In astrophotography, outliers are typically seen as trails from meteors, aircraft, or satellites. Stuck pixels not screened out in-camera or during raw conversion can also cause trailing artifacts due to tracking errors.

A more effective approach for handling outlier data is sigma-clipped averaging. This method iteratively computes the mean and standard deviation for each RGB channel, rejecting pixel values that deviate beyond a specified number of standard deviations from the mean. By filtering out these statistical outliers, sigma clipping helps preserve genuine signal while suppressing anomalies. 

One of the main drawbacks of sigma-clipped averaging is that is it computationally intensive. The method requires storing all image arrays in memory for each iteration of the clipping process, which becomes especially demanding with large stacks of high-resolution images. Previous implementations that I have tried—such as those using Astropy sigma_clip or custom scripts on DaVinci—were often prohibitively slow, taking many hours to complete. These approaches were also prone to forced termination (SIGKILL) after extended runtimes, likely due to excessive memory usage.

Program astro-sca

To address these issues, I developed astro-sca – a fast, scalable sigma-clipped averaging progam written in Python. It utilizes NumPy and OpenCV (cv2) for high-speed array-based image processing that also replaces slow, per-pixel operations with array masking techniques, resulting in much greater efficiency.

Instead of holding all image arrays in memory, astro-sca stores them in external temporary files, enabling virtually unlimited stack sizes. While in-memory processing is slightly faster for small stacks, it becomes inefficient for large datasets due to memory swapping overhead. On systems with fast internal SSDs, astro-sca’s external file approach not only avoids memory issues but can also deliver better overall performance and speed with large image stacks.

In practice, astro-sca significantly outperforms other solutions I’ve used. For example, it processed a stack of thirty-four 45MP images in just 3 minutes and 14 seconds on a MacBook Pro M4 with 16 GB of RAM. A second test—using 120 cropped images at 17MP each—completed in 4 minutes and 4 seconds. Performance will naturally vary based on different machine hardware. Machines with Internal magnetic hard disk drives require too much acquisition time and are thus not recommended for use with astro-sca.

Temporary Data Storage Considerations
A temporary directory for the individual color channel array files is created in the same location as the input images. The additional temporary storage required is approximately equal to the total size of the original input files. If the internal SSD lacks sufficient space, an external USB SSD can be specified using the path variable (described below). This allows for virtually unlimited storage capacity, though it may result in slower execution times due to reduced I/O performance. Temporary files and directories are automatically deleted by astro-sca—either during execution when they are no longer needed, or upon exit in the event of a trapped error. For runs aborted by the user, the residual temporary directory (‘/npytmp’) and any files it contains may be safely deleted manually.

astro-sca Input Parameters

fbase – The desired base file name for the output file. Suffixes ‘-avg.tif’ or ‘-sca.tif’ will be added depending on which type of averaging is performed. Input files must be star-aligned TIF files of data type uint16.

path – Path to the directory where the input files are located. It is also where the output file(s) will be written.

averaging_method – 1 – Simple Average,  2 – Sigma Clipped Average,  3 – Both Simple and Sigma Clipped Averages

fn_filter_method – Filtering method that can be applied to choose which file names to process. 
0 – No filter – All TIF files in path will be read in and checked for processing.
1 – Starts with fn_filter – All tif files starting with the string ‘fn_filter’ will be read in and checked for processing.
2 – Contains fn_filter – All tif files that contain the string ‘fn_filter‘ will be read in and checked for processig.

fn_filter
– String used to filter on filenames according to ‘fn_filter_method”.
I often use option 2 – Contains ‘fn_filter‘  with fn_filter = “.reg”. This is an easy way to filter on just the registered and aligned Deep Sky Stacker (DSS) files which all contain “.reg” in the filename.

niter – Number of iterations for sigma clipped average. I have found 3 iterations to be good in most cases.

nsig – Number of sigmas around center for clipping. A value of 2.2 is a good default that seems to work well.

min_images – minimum number of images for averaging.  Set to 3 as default.

Running astro-sca

The program is a single Python file and is available in the astrophotography software downloads page. It is provided here free and open source according to the license agreement in the downloads page.

The program was developed using PyCharm. I highly recommend this IDE to load and run astro-sca for first time and even experienced users. It is reliable and easy to use. In order to run astro-sca, a number of imports are required. Modules os, sys, shutil, and time are all part of the Python standard library so they should import directly. CV2 and NumPy, however, must be installed if not already done so by the user. This can be done either with pip install via a console window or from within PyCharm by going to PyCharm “settings…”. Under your project name, find “Python Interpreter” and then click the “+” (install) symbol. This will pull up a window where you can search for CV2 and NumPy to install. There are a number of internet resources that can help with these installations or debug any issues.

As I have not yet written a GUI or implemented an input file method for parameter input, the program parameters must be modified directly in the “user modifiable variables” section near the top of the program code. Comments in the code and the information provided above should easily guide the user. Beyond that, no programming knowledge of Python is required to run astro-sca.

I have only run astro-sca on Mac OS but it should work fine on Windows provided that the ‘path‘ variable is properly set for the correct Windows path syntax. I am working on a Win 11 VMware virtual machine but have so far found it to be too unstable to load and verify astro-sca in Windows. Any feedback on issues or modifications needed with running this code on Windows are welcome via an email or in the comments.

Download

astro-sca can be downloaded here on the Astrophotography Software Page.

Leave a Reply