Skip to content

Commit

Permalink
Replace distutils.LooseVersion with packaging.version
Browse files Browse the repository at this point in the history
This is for Python 3.12 compatibility
  • Loading branch information
olebole committed Feb 8, 2024
1 parent c548cf3 commit 22acc38
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 25 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install_requires =
dask[array]
joblib
casa-formats-io
packaging

[options.extras_require]
test =
Expand Down
1 change: 0 additions & 1 deletion spectral_cube/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import print_function, absolute_import, division

import os
from distutils.version import LooseVersion
from astropy.units.equivalencies import pixel_scale

# Import casatools and casatasks here if available as they can otherwise
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/lower_dimensional_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ def reproject(self, header, order='bilinear'):
" installed.")

# Need version > 0.2 to work with cubes
from distutils.version import LooseVersion
if LooseVersion(version) < "0.3":
from packaging.version import Version, parse
if parse(version) < Version("0.3"):
raise Warning("Requires version >=0.3 of reproject. The current "
"version is: {}".format(version))

Expand Down
3 changes: 1 addition & 2 deletions spectral_cube/np_compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import print_function, absolute_import, division

import numpy as np
from distutils.version import LooseVersion

def allbadtonan(function):
"""
Expand All @@ -14,7 +13,7 @@ def f(data, axis=None, keepdims=None):
result = function(data, axis=axis)
else:
result = function(data, axis=axis, keepdims=keepdims)
if LooseVersion(np.__version__) >= LooseVersion('1.9.0') and hasattr(result, '__len__'):
if hasattr(result, '__len__'):
if axis is None:
if np.all(np.isnan(data)):
return np.nan
Expand Down
9 changes: 4 additions & 5 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
doppler_beta, doppler_gamma, doppler_z)
from .io.core import SpectralCubeRead, SpectralCubeWrite

from distutils.version import LooseVersion
from packaging.version import Version, parse


__all__ = ['BaseSpectralCube', 'SpectralCube', 'VaryingResolutionSpectralCube']
Expand Down Expand Up @@ -2343,7 +2343,7 @@ def to_yt(self, spectral_factor=1.0, nprocs=None, **kwargs):
import yt

if (('dev' in yt.__version__) or
(LooseVersion(yt.__version__) >= LooseVersion('3.0'))):
(parse(yt.__version__) >= Version('3.0'))):

# yt has updated their FITS data set so that only the SpectralCube
# variant takes spectral_factor
Expand Down Expand Up @@ -2659,11 +2659,10 @@ def reproject(self, header, order='bilinear', use_memmap=False,

reproj_kwargs = kwargs
# Need version > 0.2 to work with cubes, >= 0.5 for memmap
from distutils.version import LooseVersion
if LooseVersion(version) < "0.5":
if parse(version) < Version("0.5"):
raise Warning("Requires version >=0.5 of reproject. The current "
"version is: {}".format(version))
elif LooseVersion(version) >= "0.6":
elif parse(version) >= Version("0.6"):
pass # no additional kwargs, no warning either
else:
reproj_kwargs['independent_celestial_slices'] = True
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
FunctionMask, CompositeMask)
from ..masks import is_broadcastable_and_smaller, dims_to_skip, view_of_subset

from distutils.version import LooseVersion
from packaging.version import Version, parse


def test_spectral_cube_mask():
Expand Down Expand Up @@ -392,7 +392,7 @@ def test_flat_mask(data_adv, use_dask):
assert np.all(np.isnan(mcube.sum(axis=0)[~mask_array]))


@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.7'),
@pytest.mark.skipif(parse(np.__version__) < Version('1.7'),
reason='Numpy <1.7 does not support multi-slice indexing.')
def test_flat_mask_spectral(data_adv, use_dask):
cube, data = cube_and_raw(data_adv, use_dask=use_dask)
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_moments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import print_function, absolute_import, division

import warnings
from distutils.version import LooseVersion
from packaging.version import Version, parse

import pytest
import numpy as np
Expand Down Expand Up @@ -78,7 +78,7 @@ def moment_cube():
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)))

if LooseVersion(astropy.__version__[:3]) >= LooseVersion('1.0'):
if parse(astropy.__version__[:3]) >= Version('1.0'):
# The relative error is slightly larger on astropy-dev
# There is no obvious reason for this.
rtol = 2e-7
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

# The comparison of Quantities in test_memory_usage
# fail with older versions of numpy
from distutils.version import LooseVersion
from packaging.version import Version, parse

NPY_VERSION_CHECK = LooseVersion(np.version.version) >= "1.13"
NPY_VERSION_CHECK = parse(np.version.version) >= Version("1.13")

from .test_moments import moment_cube
from .helpers import assert_allclose
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 @@ -18,9 +18,9 @@

# The comparison of Quantities in test_memory_usage
# fail with older versions of numpy
from distutils.version import LooseVersion
from packaging.version import Version, parse

NPY_VERSION_CHECK = LooseVersion(np.version.version) >= "1.13"
NPY_VERSION_CHECK = parse(np.version.version) >= Version("1.13")

from radio_beam import beam, Beam

Expand Down
8 changes: 4 additions & 4 deletions spectral_cube/tests/test_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import itertools
import warnings
import mmap
from distutils.version import LooseVersion
import sys
from packaging.version import Version, parse

import pytest

Expand Down Expand Up @@ -65,7 +65,7 @@
try:
import yt
YT_INSTALLED = True
YT_LT_301 = LooseVersion(yt.__version__) < LooseVersion('3.0.1')
YT_LT_301 = parse(yt.__version__) < Version('3.0.1')
except ImportError:
YT_INSTALLED = False
YT_LT_301 = False
Expand All @@ -81,7 +81,7 @@
from radio_beam import Beam, Beams
from radio_beam.utils import BeamError

NUMPY_LT_19 = LooseVersion(np.__version__) < LooseVersion('1.9.0')
NUMPY_LT_19 = parse(np.__version__) < Version('1.9.0')


def cube_and_raw(filename, use_dask=None):
Expand Down Expand Up @@ -978,7 +978,7 @@ def test_read_write_rountrip(tmpdir, data_adv, use_dask):
assert cube.shape == cube.shape
assert_allclose(cube._data, cube2._data)
if (((hasattr(_wcs, '__version__')
and LooseVersion(_wcs.__version__) < LooseVersion('5.9'))
and parse(_wcs.__version__) < Version('5.9'))
or not hasattr(_wcs, '__version__'))):
# see https://github.com/astropy/astropy/pull/3992 for reasons:
# we should upgrade this for 5.10 when the absolute accuracy is
Expand Down
4 changes: 2 additions & 2 deletions spectral_cube/tests/test_subcubes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import print_function, absolute_import, division

import pytest
from distutils.version import LooseVersion
from packaging.version import Version, parse

from astropy import units as u
from astropy import wcs
Expand All @@ -15,7 +15,7 @@
try:
import regions
regionsOK = True
REGIONS_GT_03 = LooseVersion(regions.__version__) >= LooseVersion('0.3')
REGIONS_GT_03 = parse(regions.__version__) >= Version('0.3')
except ImportError:
regionsOK = REGIONS_GT_03 = False

Expand Down
2 changes: 1 addition & 1 deletion spectral_cube/tests/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_proj_imshow(data_vda_jybeam_lower, use_dask):
plt = pytest.importorskip('matplotlib.pyplot')
cube, data = cube_and_raw(data_vda_jybeam_lower, use_dask=use_dask)
mom0 = cube.moment0()
if LooseVersion(plt.matplotlib.__version__) < LooseVersion('2.1'):
if parse(plt.matplotlib.__version__) < Version('2.1'):
# imshow is now only compatible with more recent versions of matplotlib
# (apparently 2.0.2 was still incompatible)
plt.imshow(mom0.value)
Expand Down

0 comments on commit 22acc38

Please sign in to comment.