astropy: Unexpected results from sigma_clipped_stats for large np.float32 input arrays
Description
I’m processing 63 megapixel FITS images using sigma_clipped_stats, and have run into behavior where the endianness of the 32-bit float data is apparently affecting the correctness of the output.
Specifically, data loaded directly out of the fits file dtype('>f4') is working correctly. But if I perform numerical operations on the data–or explicitly cast it–such that it becomes dtype('<f4') (which is the native float32 on x64: dtype('<f4')==dtype('float32')), I get what appear to be incorrect results.
This only happens with large arrays. I haven’t determined the exact cutoff, but (4096,4096) appears to work correctly. (6388,9576) does not.
Expected behavior
I expect astropy.stats.sigma_clipped_stats(np.array(data,'>f4')) and astropy.stats.sigma_clipped_stats(np.array(data,'<f4')) to produce identical, or at least substantially similar results.
(It appears that there’s also a difference in precision calculated in these two cases–while odd, this is not the issue I’m reporting.)
How to Reproduce
This can be reproduced by generating a random uniform [0-1] array with np.random.random. The mean and median should be approximately 0.5, and the stddev should be approximately 0.288.
The sample code generates arrays of two sizes, and processes them in each endianness. The first three lines have output values that I expect. The last line (larger array, ‘<f4’) gives very different results.
import numpy as np
import astropy, astropy.stats
rand_data = np.random.random(size=(1024,1024))
print(astropy.stats.sigma_clipped_stats(np.array(rand_data,'>f4'), sigma=3, maxiters=5))
print(astropy.stats.sigma_clipped_stats(np.array(rand_data,'<f4'), sigma=3, maxiters=5))
rand_data = np.random.random(size=(6388,9576))
print(astropy.stats.sigma_clipped_stats(np.array(rand_data,'>f4'), sigma=3, maxiters=5))
print(astropy.stats.sigma_clipped_stats(np.array(rand_data,'<f4'), sigma=3, maxiters=5))
When I ran this, I got:
(0.4994579, 0.4990998, 0.2887525)
(0.49945005774497986, 0.4990997910499573, 0.28869783878326416)
(0.4999373, 0.49990413, 0.28868327)
(0.2742652893066406, 0.4999041259288788, 0.3462343215942383)
Versions
Windows-10-10.0.19045-SP0
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
astropy 6.1.4
Numpy 1.24.0
pyerfa 2.0.1.4
Scipy 1.11.2
Matplotlib 3.8.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (16 by maintainers)
Regarding the timings, calling
np.nanmedianwas never much slower thanbn.nanmedianwhen called just with an array, the biggest difference (and this is used instats) is when using theaxiskeyword argument:Note this was reported at the time of #11492: https://github.com/pydata/bottleneck/issues/379 But bottleneck is lacking maintainers since a few years, we had other issues and discussed removing it in #11492. I think we should remove it, slower and correct is better than faster and incorrect 😉, and people can still pass their mean/std func if they want to.
@larrybradley
For the array sizes and input range I tested, float64 results between numpy and bottleneck agree to many decimal places.
Well at least now they are aware. Thanks @prutschman-iv for reporting upstream !