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)

Most upvoted comments

Regarding the timings, calling np.nanmedian was never much slower than bn.nanmedian when called just with an array, the biggest difference (and this is used in stats) is when using the axis keyword argument:

In [1]: import numpy as np

In [2]: import bottleneck as bn

In [3]: array = np.random.random((256, 256, 256))

In [4]: array[array < 0.1] = np.nan

In [5]: %timeit np.nanmedian(array, axis=1)
792 ms ± 13.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [6]: %timeit bn.nanmedian(array, axis=1)
155 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

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

Do float (float64) arrays show this same problem?

For the array sizes and input range I tested, float64 results between numpy and bottleneck agree to many decimal places.

I’m surprised they haven’t fixed this issue for the past 3.5 years.

Well at least now they are aware. Thanks @prutschman-iv for reporting upstream !