Skip to content

Commit

Permalink
Fix tests for numpy 2.0 (#917)
Browse files Browse the repository at this point in the history
* remove NaNs for numpy 2.0

* More NaNs to fix

* Bump python testing versions up (mostly to py311)

* add py310 test for newest casa versions

* Add casadata fix

* Add a py312 min dep test

* Fix workflow syntax

* '3.10' to avoid 3.1 version check

* Remove ptp and nansum methods that were removed in ndarray as methods in numpy 2

* Force numpy<2 for tests with CASA

* Don't think we need to set the env variable in setup

* Force creation of the data folder?

* Missed updating nansum

* Attempt to ignore casadata preamble on non-casa builds (esp mac and windows)

* Fix yml structure

* Fix logic

* Try adding manual update cmds for casadata

* Add casaconfig install

* only pass str to casa tasks

* Check on desc dict structure for on-going test failure

* try passing dict to beam kwarg in setrestoringbeam

* TEMP: print out keys in the desc dict

* Extra open/close?

* add another temp print line to figure out what's going wrong with setting the beams

* setrestoringbeam: hitting a type error?

* Clean up unneeded code for ia.setrestoringbeam
  • Loading branch information
e-koch authored Oct 13, 2024
1 parent 8ae6705 commit 9919364
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 51 deletions.
80 changes: 50 additions & 30 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ jobs:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
python-version: 3.9
name: Py3.9 mindeps
toxenv: py39-test
- os: ubuntu-latest
python-version: '3.10'
name: Py3.10 mindeps
Expand All @@ -28,9 +24,13 @@ jobs:
name: Py3.11 mindeps
toxenv: py311-test
- os: ubuntu-latest
python-version: 3.9
name: Py3.9 no visualization + coverage
toxenv: py39-test-noviz-cov
python-version: 3.12
name: Py3.12 mindeps
toxenv: py312-test
- os: ubuntu-latest
python-version: 3.11
name: Py3.11 no visualization + coverage
toxenv: py311-test-noviz-cov
- os: ubuntu-latest
python-version: 3.8
name: Py3.8 mindeps
Expand All @@ -44,47 +44,51 @@ jobs:
name: Py3.9 all
toxenv: py39-test-viz-noviz
- os: ubuntu-latest
python-version: 3.9
name: Py3.9 dev
toxenv: py39-test-dev
python-version: 3.11
name: Py3.11 dev
toxenv: py311-test-dev
- os: ubuntu-latest
python-version: 3.9
name: Py3.9 viz
toxenv: py39-test-viz
python-version: 3.11
name: Py3.11 viz
toxenv: py311-test-viz
#- os: ubuntu-latest
# python-version: 3.9
# name: Py3.9 viz w/yt & glue
# toxenv: py39-test-viz-viz_extra
- os: ubuntu-latest
python-version: 3.9
name: Py3.9 all dev
toxenv: py39-test-viz-noviz-dev
python-version: 3.11
name: Py3.11 all dev
toxenv: py311-test-viz-noviz-dev
- os: ubuntu-latest
python-version: 3.8
name: Py3.8 mindeps and CASA
toxenv: py38-test-casa
- os: ubuntu-latest
python-version: 3.9
name: Py3.9, noviz, dev
toxenv: py39-test-noviz-dev
python-version: '3.10'
name: Py3.10 mindeps and CASA
toxenv: py310-test-casa
- os: ubuntu-latest
python-version: 3.11
name: Py3.11, noviz, dev
toxenv: py311-test-noviz-dev
- os: ubuntu-latest
python-version: 3.8
name: Py3.8 noviz (except CASA)
toxenv: py38-test-noviz
- os: macos-latest
python-version: 3.9
name: Py3.9 noviz (except CASA) MacOS X
python-version: 3.11
name: Py3.11 noviz (except CASA) MacOS X
toxenv: py39-test-noviz
- os: windows-latest
python-version: 3.9
name: Py3.9, noviz Windows
toxenv: py39-test-noviz-dev
python-version: 3.11
name: Py3.11, noviz Windows
toxenv: py311-test-noviz-dev
- os: windows-latest
python-version: 3.9
name: Py3.9, noviz, dev; Windows
toxenv: py39-test-noviz-dev
python-version: 3.11
name: Py3.11, noviz, dev; Windows
toxenv: py311-test-noviz-dev
- os: ubuntu-latest
python-version: 3.9
python-version: 3.11
name: Documentation
toxenv: build_docs

Expand All @@ -95,9 +99,25 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install testing dependencies
run: python -m pip install tox
- name: Run tests with ${{ matrix.name }}
run: python -m pip install tox codecov


- name: Run tests with ${{ matrix.name }} (optionally with CASA)
if: ${{ !contains(matrix.toxenv,'-casa') }}
run: tox -v -e ${{ matrix.toxenv }}
- name: Run tests with ${{ matrix.name }} (no CASA)
if: ${{ contains(matrix.toxenv,'-casa') }}
run: |
pip install casaconfig
mkdir ${GITHUB_WORKSPACE}/.casa
mkdir ${GITHUB_WORKSPACE}/.casa/data
chmod 777 -R ${GITHUB_WORKSPACE}/.casa
echo "datapath=['${GITHUB_WORKSPACE}/.casa/data']; measurespath=datapath[0]; measures_auto_update=True; data_auto_update=True" > $GITHUB_WORKSPACE/config.py
pip install casadata
# https://casadocs.readthedocs.io/en/stable/notebooks/external-data.html#Populating-the-Data-Directory-Manually
python -m casaconfig --update-all
python -m casaconfig --current-data
CASASITECONFIG=$GITHUB_WORKSPACE/config.py tox -v -e ${{ matrix.toxenv }}
- name: Upload coverage to codecov
if: ${{ contains(matrix.toxenv,'-cov') }}
uses: codecov/codecov-action@v4
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/analysis_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def fourier_shift(x, shift, axis=0, add_pad=False, pad_size=None):

# Check if there are all NaNs before shifting
if nanmask.all():
return np.array([np.NaN] * pad_mask.size)
return np.array([np.nan] * pad_mask.size)

nonan_shift = _fourier_shifter(pad_nonan, shift, axis)
if shift_mask:
mask_shift = _fourier_shifter(pad_mask, shift, axis) > 0.5
nonan_shift[mask_shift] = np.NaN
nonan_shift[mask_shift] = np.nan

return nonan_shift

Expand Down
14 changes: 7 additions & 7 deletions spectral_cube/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,15 @@ def data_vda_beams_image(tmp_path):
hdul.writeto(tmp_path / 'vda_beams.fits')
from casatools import image
ia = image()
ia.fromfits(infile=tmp_path / 'vda_beams.fits',
outfile=tmp_path / 'vda_beams.image',
ia.fromfits(infile=str(tmp_path / 'vda_beams.fits'),
outfile=str(tmp_path / 'vda_beams.image'),
overwrite=True)
for (bmaj, bmin, bpa, chan, pol) in beams.data:
ia.setrestoringbeam(major={'unit': 'arcsec', 'value': bmaj},
minor={'unit': 'arcsec', 'value': bmin},
pa={'unit': 'deg', 'value': bpa},
channel=chan,
polarization=pol)
ia.setrestoringbeam(beam={'major': {'unit': 'arcsec', 'value': float(bmaj)},
'minor': {'unit': 'arcsec', 'value': float(bmin)},
'positionangle': {'unit': 'deg', 'value': float(bpa)}},
channel=int(chan),
polarization=int(pol))
ia.close()
return tmp_path / 'vda_beams.image'

Expand Down
5 changes: 5 additions & 0 deletions spectral_cube/io/casa_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def load_casa_image(filename, skipdata=False, memmap=True,
elif 'beams' in beam_:
bdict = beam_['beams']

# NOTE: temp to check failing test
print(desc['_keywords_']['coords'])
print(list(desc['_keywords_']['coords'].keys()))


stokes_params = desc['_keywords_']['coords']['stokes1']['stokes']

nbeams = len(bdict)
Expand Down
2 changes: 1 addition & 1 deletion spectral_cube/lower_dimensional_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ def __getattribute__(self, attrname):
# `Quantity`, but for now this approach works.... we just have
# to add more functions to this list.
if attrname in ('min', 'max', 'std', 'mean', 'sum', 'cumsum',
'nansum', 'ptp', 'var'):
'var'):
return getattr(self.quantity, attrname)
else:
return super(BaseOneDSpectrum, self).__getattribute__(attrname)
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def _argmaxmin_world(self, axis, method, **kwargs):

# Compute whether the mask has any valid data along `axis`
collapsed_mask = self.mask.include().any(axis=axis)
out[~collapsed_mask] = np.NaN
out[~collapsed_mask] = np.nan

# Return a Projection.
new_wcs = wcs_utils.drop_axis(self._wcs, np2wcs[axis])
Expand Down Expand Up @@ -3274,7 +3274,7 @@ def spectral_interpolate(self, spectral_grid,
newmask[outslice,iy,ix] = interped
else:
newmask[:, iy, ix] = False
newcube[:, iy, ix] = np.NaN
newcube[:, iy, ix] = np.nan

update_function()

Expand Down
6 changes: 3 additions & 3 deletions spectral_cube/tests/test_analysis_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def test_shift():
# With part masked
masked_spectrum = true_spectrum.copy()
mask = np.abs(spectral_axis.value) <= 30
masked_spectrum[~mask] = np.NaN
masked_spectrum[~mask] = np.nan

rolled_mask = np.roll(mask, 10)
rolled_masked_spectrum = rolled_spectrum.copy()
rolled_masked_spectrum[~rolled_mask] = np.NaN
rolled_masked_spectrum[~rolled_mask] = np.nan

shift_spectrum = fourier_shift(masked_spectrum, 10)

Expand Down Expand Up @@ -413,7 +413,7 @@ def test_stacking_noisy(use_dask):

# cov = fit_g.fit_info['param_cov']
# if cov is None:
# cov = np.zeros((3, 3)) * np.NaN
# cov = np.zeros((3, 3)) * np.nan
# parvals = g_fit.parameters

# parerrs = np.sqrt(np.diag(cov))
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_spectral_interpolate_with_mask(data_522_delta, use_dask):
# The output makes CDELT3 > 0 (reversed spectral axis) so the masked
# portion are the final 2 channels.
np.testing.assert_almost_equal(result.filled_data[:].value,
[0.0, 0.5, np.NaN, np.NaN])
[0.0, 0.5, np.nan, np.nan])


def test_spectral_interpolate_reversed(data_522_delta, use_dask):
Expand Down Expand Up @@ -718,7 +718,7 @@ def test_1d_slices(data_255_delta, use_dask):
reason='https://github.com/numpy/numpy/issues/20699')
@pytest.mark.parametrize('method',
('min', 'max', 'std', 'mean', 'sum', 'cumsum',
'nansum', 'ptp', 'var'),
'var'),
)
def test_1d_slice_reductions(method, data_255_delta, use_dask):

Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_reproject(use_memmap, data_adv, use_dask):
result = cube.reproject(header_out, use_memmap=use_memmap)

assert result.shape == (cube.shape[0], 5, 4)

# empirically, this is how close we can get after https://github.com/astropy/astropy/pull/14508
tolerance = 1e-12

Expand Down Expand Up @@ -346,7 +346,7 @@ def test_spectral_interpolate_with_mask(data_522_delta, use_dask):
# The output makes CDELT3 > 0 (reversed spectral axis) so the masked
# portion are the final 2 channels.
np.testing.assert_almost_equal(result[:, 0, 0].value,
[0.0, 0.5, np.NaN, np.NaN])
[0.0, 0.5, np.nan, np.nan])

assert cube.wcs.wcs.compare(orig_wcs.wcs)

Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py{38,39,310,311}-test{,-dev,-noviz,-viz,-viz_extra,-cov,-noopenfiles}
py{38,39,310,311,312}-test{,-dev,-noviz,-viz,-viz_extra,-cov,-noopenfiles}
build_docs
codestyle
requires =
Expand All @@ -14,7 +14,7 @@ passenv = HOME,DISPLAY,LC_ALL,LC_CTYPE,ON_TRAVIS
changedir = .tmp/{envname}
description = run tests with pytest
deps =
casa: numpy
casa: numpy<2.0.0
casa: scipy
casa: matplotlib
casa: casatools
Expand Down

0 comments on commit 9919364

Please sign in to comment.