Skip to content

Commit

Permalink
Merge pull request #387 from adrn/no-wheels-and-numpy2
Browse files Browse the repository at this point in the history
Compatibility with numpy 2.0
  • Loading branch information
adrn authored Aug 22, 2024
2 parents 7750fee + 0e36291 commit 987f135
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 395 deletions.
138 changes: 46 additions & 92 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,61 @@ on:

jobs:
tests:
name: ${{ matrix.name }}
name: Python ${{ matrix.python-version }} on ${{ matrix.os }} with numpy ${{ matrix.numpy-version }}, no gsl ${{ matrix.gala-nogsl }}, deps ${{ matrix.pip-test-deps }}
runs-on: ${{ matrix.os }}
if: github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'docs only')
strategy:
fail-fast: true
matrix:
python-version: ["3.10", "3.11", "3.12"]
os: ["ubuntu-latest", "macos-latest"]
numpy-version: ["latest"]
gala-nogsl: ["0"]
pip-test-deps: ["test"]
include:

- name: Code style checks
- name: Oldest numpy version supported
os: ubuntu-latest
python: 3.x
toxenv: codestyle
python-version: "3.11"
numpy-version: "1.24"
gala-nogsl: "0"
pip-test-deps: "test"

- name: Python 3.10 with minimal dependencies and coverage
- name: Install without GSL
os: ubuntu-latest
python: '3.10'
toxenv: py310-test-cov

- name: Python 3.9
os: ubuntu-latest
python: '3.9'
toxenv: py39-test

- name: Python 3.10
os: ubuntu-latest
python: '3.10'
toxenv: py310-test

- name: Python 3.11
os: ubuntu-latest
python: '3.11'
toxenv: py311-test

# Has to happen on ubuntu because galpy is finnicky on macOS
- name: Python 3.10 with all optional dependencies
os: ubuntu-latest
python: '3.10'
toxenv: py310-test-extradeps
toxposargs: --durations=50

- name: Python 3.10 without GSL
os: ubuntu-latest
python: '3.10'
toxenv: nogsl

- name: Python 3.9 with oldest supported version of all dependencies
os: ubuntu-latest
python: 3.9
toxenv: py39-test-oldestdeps

# Mac and Windows:
- name: Python 3.10 standard tests (macOS)
os: macos-latest
python: '3.10'
toxenv: py310-test

# - name: Python 3.9 standard tests (Windows)
# os: windows-latest
# python: 3.9
# toxenv: py39-test
python-version: "3.11"
numpy-version: "latest"
gala-nogsl: "1"
pip-test-deps: "test"

- name: With optional dependencies
os: ubuntu-latest # note: galpy install failed on macos here
python-version: "3.11"
numpy-version: "latest"
gala-nogsl: "0"
pip-test-deps: "test,extra"

steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

# For animation tests
- uses: FedericoCarboni/setup-ffmpeg@v3
if: ${{ !startsWith(matrix.os, 'mac') }}
continue-on-error: true
with:
# Not strictly necessary, but it may prevent rate limit
# errors especially on GitHub-hosted macos machines.
github-token: ${{ secrets.GITHUB_TOKEN }}
ffmpeg-version: "6.1.0"
id: setup-ffmpeg

- name: Set up Python ${{ matrix.python }} on ${{ matrix.os }}
if: ${{ !startsWith(matrix.os, 'windows') }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}

# Windows:
# - uses: conda-incubator/setup-miniconda@v2
# if: startsWith(matrix.os, 'windows')
# with:
# auto-update-conda: true
# python-version: ${{ matrix.python-version }}

# - name: Install Python dependencies - Windows
# if: startsWith(matrix.os, 'windows')
# shell: bash -l {0}
# run: |
# conda install -c conda-forge -q gsl python=3.9 libpython
# python -m pip install -e .[test]
# python -m pip install tox

# - name: Run tests - Windows
# if: startsWith(matrix.os, 'windows')
# shell: bash -l {0}
# run: |
# tox ${{ matrix.toxargs }} -e ${{ matrix.toxenv }} ${{ matrix.toxposargs }}

# Mac:
- name: Setup Mac - GSL
if: startsWith(matrix.os, 'mac')
Expand All @@ -127,18 +79,20 @@ jobs:
sudo apt-get install gsl-bin libgsl0-dev build-essential
sudo apt-get install libhdf5-serial-dev # TODO: remove when h5py has 3.11 wheels
# Any *nix:
- name: Install Python dependencies - nix
if: ${{ !startsWith(matrix.os, 'windows') }}
run: python -m pip install --upgrade tox codecov
- name: Install package and dependencies
run: python -m pip install -e ".[${{ matrix.pip-test-deps }}]"
env:
GALA_NOGSL: ${{ matrix.gala-nogsl }}

- name: Run tests - nix
if: ${{ !startsWith(matrix.os, 'windows') }}
run: tox -e ${{ matrix.toxenv }} -- ${{ matrix.toxposargs }}
- name: Update versions if testing min versions
if: matrix.numpy-version != 'latest'
run: |
python -m pip install numpy~=${{ matrix.numpy-version }}
# Coverage:
- name: Upload coverage report to codecov
uses: codecov/codecov-action@v4
if: steps.check_files.outputs.files_exists == 'true' && runner.os == 'Linux'
with:
file: ./coverage.xml # optional
- name: Run tests
run: >-
python -m pytest -ra --cov --cov-report=xml --cov-report=term
--durations=20 .
- name: Upload coverage report
uses: codecov/[email protected]
12 changes: 6 additions & 6 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- ["1", "cp39-* cp310-*"]
- ["2", "cp311-* cp312-*"]
os:
- "macos-14"
# - "macos-14"
- "ubuntu-latest"

steps:
Expand All @@ -42,12 +42,12 @@ jobs:
# with:
# platforms: all

# Mac:
# Mac: disable wheels on mac because of GSL issues
# NOTE: need to install pipx explicitly for macos-14
- name: Setup Mac
if: runner.os == 'macOS'
run: |
brew install gsl pipx
# - name: Setup Mac
# if: runner.os == 'macOS'
# run: |
# brew install gsl pipx

# Ubuntu:
- name: Setup Linux
Expand Down
6 changes: 6 additions & 0 deletions gala/_compat_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import numpy as np
from packaging.version import Version

# See: https://github.com/astropy/astropy/pull/16181
NUMPY_LT_2_0 = Version(np.__version__) < Version("2.0.0")
COPY_IF_NEEDED = False if NUMPY_LT_2_0 else None
8 changes: 5 additions & 3 deletions gala/dynamics/representation_nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import astropy.units as u
import numpy as np

from gala._compat_utils import COPY_IF_NEEDED

__all__ = ["NDCartesianRepresentation", "NDCartesianDifferential"]


Expand Down Expand Up @@ -62,7 +64,7 @@ def _apply(self, method, *args, **kwargs):
apply_method = operator.methodcaller(method, *args, **kwargs)
return self.__class__(
[apply_method(getattr(self, component)) for component in self.components],
copy=False,
copy=COPY_IF_NEEDED,
)


Expand Down Expand Up @@ -163,7 +165,7 @@ def get_xyz(self, xyz_axis=0):
for name in self.attr_classes
]
xs_value = np.concatenate(components, axis=xyz_axis)
return u.Quantity(xs_value, unit=unit, copy=False)
return u.Quantity(xs_value, unit=unit, copy=COPY_IF_NEEDED)

xyz = property(get_xyz)

Expand Down Expand Up @@ -260,6 +262,6 @@ def get_d_xyz(self, xyz_axis=0):
for name in self.components
]
xs_value = np.concatenate(components, axis=xyz_axis)
return u.Quantity(xs_value, unit=unit, copy=False)
return u.Quantity(xs_value, unit=unit, copy=COPY_IF_NEEDED)

d_xyz = property(get_d_xyz)
Loading

0 comments on commit 987f135

Please sign in to comment.