Skip to content

Commit

Permalink
Test with numpy 2.0 rc1 (#1114)
Browse files Browse the repository at this point in the history
* Test with numpy 2.0 rc1

* Remove Python 3.8 from numpy 2.0 tests

* specify dtype

* fix new style np.unique output with squeeze

* fix doctest failures due to np.float64(...) print

* add separate numpy2 workflow
  • Loading branch information
kinnala authored Apr 7, 2024
1 parent c5edf42 commit 03a55f6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
mypy skfem
- name: Run pytest
run: |
pytest
pytest
42 changes: 42 additions & 0 deletions .github/workflows/numpy2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: tests with numpy 2

on:
pull_request:
push:
branches: master

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9, '3.10', 3.11, 3.12]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install --pre numpy==2.0.0rc1
pip install .
- name: Run flake8
run: |
flake8 skfem
- name: Run sphinx build
run: |
sphinx-build -W -a -b html docs docs/_build
- name: Run sphinx doctests
run: |
sphinx-build -W -a -b doctest docs docs/_build
- name: Run mypy
run: |
mypy skfem
- name: Run pytest
run: |
pytest -k 'not TestEx09 and not TestEx32 and not TestEx49'
3 changes: 2 additions & 1 deletion docs/examples/ex41.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
on mixed meshes is work-in-progress.
"""
import numpy as np
from pathlib import Path
from skfem import *
from skfem.models import laplace, unit_load
Expand All @@ -35,7 +36,7 @@
A = asm(laplace, basis)
f = asm(unit_load, basis)

y = solve(*condense(A, f, D=out[0]['boundary']['line']))
y = solve(*condense(A, f, D=out[0]['boundary']['line'].astype(np.int64)))

def visualize():
from skfem.visuals.matplotlib import plot, draw
Expand Down
4 changes: 2 additions & 2 deletions docs/gettingstarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ Thus, it makes sense to verify that the error is small.
... uh = w['uh']
... u = np.sin(np.pi * x) * np.sin(np.pi * y) / (2. * np.pi ** 2)
... return (uh - u) ** 2
>>> round(error.assemble(Vh, uh=Vh.interpolate(x)), 9)
1.069e-06
>>> str(round(error.assemble(Vh, uh=Vh.interpolate(x)), 9))
'1.069e-06'
15 changes: 12 additions & 3 deletions skfem/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,16 @@ def _remove_duplicate_nodes(p, t):
tmp = np.ascontiguousarray(p.T)
tmp, ixa, ixb = np.unique(tmp.view([('', tmp.dtype)] * tmp.shape[1]),
return_index=True, return_inverse=True)
return p[:, ixa], ixb[t]
return p[:, ixa], Mesh._squeeze_if(ixb[t])

@staticmethod
def _squeeze_if(arr):
# Workaround for the additional dimension introduced in
# numpy 2.0 for the output of np.unique when using
# return_index=True
if len(arr.shape) > 2:
return arr.squeeze(axis=2)
return arr

def __iter__(self):
return iter((self.doflocs, self.t))
Expand All @@ -609,8 +618,8 @@ def __matmul__(self, other):
return_index=True, return_inverse=True)
p = p[:, ixa]
return [
cls(p, ixb[self.t]),
*[type(m)(p, ixb[m.t + self.p.shape[1]])
cls(p, self._squeeze_if(ixb[self.t])),
*[type(m)(p, self._squeeze_if(ixb[m.t + self.p.shape[1]]))
for i, m in enumerate(other)],
]
raise NotImplementedError
Expand Down
4 changes: 2 additions & 2 deletions skfem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ def _init_bc(A: spmatrix,
if I is None and D is None:
raise Exception("Either I or D must be given!")
elif I is None and D is not None:
I = np.setdiff1d(np.arange(A.shape[0]), D)
I = np.setdiff1d(np.arange(A.shape[0], dtype=np.int64), D)
elif D is None and I is not None:
D = np.setdiff1d(np.arange(A.shape[0]), I)
D = np.setdiff1d(np.arange(A.shape[0], dtype=np.int64), I)
else:
raise Exception("Give only I or only D!")

Expand Down

0 comments on commit 03a55f6

Please sign in to comment.