Skip to content

Commit

Permalink
Test with numpy 1.*
Browse files Browse the repository at this point in the history
  • Loading branch information
adityagoel4512 committed Jun 24, 2024
1 parent 9bbccd7 commit a64ec67
Show file tree
Hide file tree
Showing 6 changed files with 851 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- py310
- py311
- py312
- np1x
steps:
- name: Checkout branch
uses: actions/checkout@v4
Expand Down
848 changes: 824 additions & 24 deletions pixi.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ python = "3.10.*"
python = "3.11.*"
[feature.py312.dependencies]
python = "3.12.*"
[feature.np1x.dependencies]
python = "3.11.*"
numpy = "1.*"

[environments]
default = ["test", "lint"]
py310 = ["py310", "test"]
py311 = ["py311", "test"]
py312 = ["py312", "test"]
np1x = ["np1x", "test"]
docs = ["docs"]
build = ["build"]
lint = { features = ["lint"], no-default-feature = true }
10 changes: 7 additions & 3 deletions tests/ndonnx/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ndonnx.additional as nda
from ndonnx import _data_types as dtypes

from .utils import run
from .utils import get_numpy_array_api_namespace, run


def numpy_to_graph_input(arr, eager=False):
Expand Down Expand Up @@ -365,8 +365,9 @@ def test_matrix_transpose():
b = ndx.matrix_transpose(a)

model = ndx.build({"a": a}, {"b": b})
npx = get_numpy_array_api_namespace()
np.testing.assert_equal(
np.matrix_transpose(np.reshape(np.arange(3 * 2 * 3), (3, 2, 3))),
npx.matrix_transpose(npx.reshape(npx.arange(3 * 2 * 3), (3, 2, 3))),
run(model, {"a": np.arange(3 * 2 * 3, dtype=np.int64).reshape(3, 2, 3)})["b"],
)

Expand All @@ -376,8 +377,11 @@ def test_matrix_transpose_attribute():
b = a.mT

model = ndx.build({"a": a}, {"b": b})
npx = get_numpy_array_api_namespace()
expected = npx.reshape(npx.arange(3 * 2 * 3), (3, 2, 3)).mT

np.testing.assert_equal(
np.reshape(np.arange(3 * 2 * 3), (3, 2, 3)).mT,
expected,
run(model, {"a": np.arange(3 * 2 * 3, dtype=np.int64).reshape(3, 2, 3)})["b"],
)

Expand Down
9 changes: 6 additions & 3 deletions tests/ndonnx/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ndonnx as ndx
import ndonnx.additional as nda

from .utils import run
from .utils import get_numpy_array_api_namespace, run


def testfill_null():
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_reduce_ops_none_filling(fn_name, default_value):
)
def test_unary_none_propagation(fn_name, args, kwargs):
fn = getattr(ndx, fn_name)
np_fn = getattr(np, fn_name)
# If testing with NumPy 1.x, we need to get the function from the numpy.array_api

a = ndx.array(shape=(1, 3), dtype=ndx.nfloat32)
b = fn(a, *args)
Expand All @@ -114,7 +114,10 @@ def test_unary_none_propagation(fn_name, args, kwargs):
inp_a = np.ma.masked_array([[0, -2.0, 3.0]], mask=[[1, 0, 0]], dtype=np.float32)
ret_b = run(model, {"a": inp_a})["b"]
missing_a = inp_a.mask
expected_b = np_fn(np.ma.filled(inp_a, np.nan), *args, **kwargs)
npx = get_numpy_array_api_namespace()
np_fn = getattr(npx, fn_name)
inp_a = npx.asarray(np.ma.filled(inp_a, np.nan))
expected_b = np_fn(inp_a, *args, **kwargs)
np.testing.assert_almost_equal(
np.ma.masked_array(expected_b, mask=missing_a),
ret_b,
Expand Down
9 changes: 9 additions & 0 deletions tests/ndonnx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,12 @@ def _get_dtypes(
output_schema[name] = _get_dtype(schema["type_name"], version)

return input_schema, output_schema


def get_numpy_array_api_namespace():
if np.__version__ < "2":
import numpy.array_api as npx

return npx
else:
return np

0 comments on commit a64ec67

Please sign in to comment.