Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Fix NumPy v2 compatibility issue #736

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ jobs:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: [3.9, '3.10', 3.11]
python-version: ['3.10', 3.11, 3.12]
exclude:
- os: windows-latest
python-version: 3.12

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -52,6 +55,7 @@ jobs:
- name: Setup QuantEcon
shell: bash -l {0}
run: |
python -m ensurepip --upgrade
python -m pip install -U pip
pip install .[testing]

Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/ci_np2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: conda-build (NumPy v2)

on: [push]

jobs:
build-linux:
runs-on: ubuntu-latest
strategy:
max-parallel: 5

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12'
- name: Add conda to system path
run: |
# $CONDA is an environment variable pointing to the root of the miniconda directory
echo $CONDA/bin >> $GITHUB_PATH
- name: Install dependencies
run: |
conda env update --file environment_np2.yml --name base
- name: Conda info
shell: bash -l {0}
run: |
conda info
conda list
- name: Test with pytest
run: |
conda install pytest
pytest
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
- ipython
- flake8
- requests
- urllib3>=2
- flit
- chardet # python>3.9,osx
- pytest
18 changes: 18 additions & 0 deletions environment_np2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: qe
channels:
- conda-forge
- defaults
dependencies:
- coverage
- numpy>=2
- scipy
- pandas
- numba
- sympy
- ipython
- flake8
- requests
- urllib3>=2
- flit
- chardet # python>3.9,osx
- pytest
7 changes: 6 additions & 1 deletion quantecon/markov/gth_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import numpy as np
from numba import jit

from ..util.compat import copy_if_needed


def gth_solve(A, overwrite=False, use_jit=True):
r"""
This routine computes the stationary distribution of an irreducible
Expand Down Expand Up @@ -52,7 +55,9 @@ def gth_solve(A, overwrite=False, use_jit=True):
Simulation, Princeton University Press, 2009.

"""
A1 = np.array(A, dtype=float, copy=not overwrite, order='C')
copy = copy_if_needed if overwrite else True

A1 = np.array(A, dtype=float, copy=copy, order='C')
# `order='C'` is for use with Numba <= 0.18.2
# See issue github.com/numba/numba/issues/1103

Expand Down
7 changes: 7 additions & 0 deletions quantecon/markov/tests/test_gth_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def test_matrices_with_C_F_orders():
assert_array_equal(computed_F, stationary_dist)


def test_unable_to_avoid_copy():
A = np.array([[0, 1], [0, 1]]) # dtype=int
stationary_dist = [0., 1.]
x = gth_solve(A, overwrite=True)
assert_array_equal(x, stationary_dist)


def test_raises_value_error_non_2dim():
"""Test with non 2dim input"""
assert_raises(ValueError, gth_solve, np.array([0.4, 0.6]))
Expand Down
23 changes: 23 additions & 0 deletions quantecon/util/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Utilities for compatibility

"""
from typing import Optional
import numpy as np


# From scipy/_lib/_util.py

copy_if_needed: Optional[bool]

if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
copy_if_needed = None
elif np.lib.NumpyVersion(np.__version__) < "1.28.0":
copy_if_needed = False
else:
# 2.0.0 dev versions, handle cases where copy may or may not exist
try:
np.array([1]).__array__(copy=None) # type: ignore[call-overload]
copy_if_needed = None
except TypeError:
copy_if_needed = False
Loading