sot / mica

Microscope on Chandra aspect
https://sot.github.io/mica
BSD 3-Clause "New" or "Revised" License
3 stars 0 forks source link

DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays #293

Closed javierggt closed 5 months ago

javierggt commented 6 months ago

The following code:

import warnings
warnings.simplefilter("always")
from mica.archive import aca_l0
imgs = aca_l0.get_l0_images(467055635, 467055639, slot=7)

Produces this warning

/Users/javierg/SAO/miniconda3/envs/ska3-flight-2024.0rc8/lib/python3.11/site-packages/numpy/ma/core.py:429: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of -9999 to uint8 will fail in the future.

The exact point where this happens is https://github.com/sot/mica/blob/4.35.0/mica/archive/aca_l0.py#L237:

dat = dat.filled(-9999)

where data is an arbitrary array, but in the specific call that causes the warning during testing the type is

dtype([('TIME', '>f8'), ('QUALITY', '>i4'), ('INTEG', '>f4'), ('IMGFUNC1', 'u1'), ('IMGSTAT', 'u1'), ('IMGSCALE', '>i2'), ('BGDAVG', '>i2'), ('IMGRAW', '>f4', (64,)), ('IMGSIZE', '>i4')])
taldcroft commented 6 months ago

I think something like this will work?

    # Masked array col access ~100 times slower than ndarray so convert
    dat_new = np.empty(dat.shape, dtype=dat.dtype)
    for name in dat.dtype.names:
        # Fill uint8 or int8 with 127 and others with -9999. All columns are numeric.
        dat_new[name] = dat[name].filled(127 if dat[name].itemsize == 1 else -9999)
    dat = dat_new