Skip to content

Commit d2584a4

Browse files
committed
1 parent 3767e78 commit d2584a4

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

.github/workflows/check.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: check
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
12+
standard:
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
runs-on: [ubuntu-latest, macos-latest, windows-latest]
18+
19+
defaults:
20+
run:
21+
shell: bash -l {0}
22+
23+
name: ${{ matrix.runs-on }} • x64 ${{ matrix.args }}
24+
runs-on: ${{ matrix.runs-on }}
25+
26+
steps:
27+
28+
- name: Basic GitHub action setup
29+
uses: actions/checkout@v3
30+
31+
- name: Set conda environment
32+
uses: mamba-org/provision-with-micromamba@main
33+
with:
34+
environment-file: environment-dev.yml
35+
environment-name: myenv
36+
cache-env: true
37+
extra-specs: |
38+
prettytable
39+
setuptools_scm
40+
scikit-build
41+
xsimd
42+
43+
- name: Set dummy version
44+
run: echo "SETUPTOOLS_SCM_PRETEND_VERSION=0.0" >> $GITHUB_ENV
45+
46+
- name: Build and install Python module
47+
working-directory: python-module
48+
run: |
49+
SKBUILD_CONFIGURE_OPTIONS="-DUSE_XSIMD=1" python -m pip install . -v
50+
51+
- name: Run Python tests
52+
working-directory: python-module
53+
run: python -m unittest discover tests

python-module/module/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ PYBIND11_MODULE(_xt, m)
4646
m.doc() = "Python bindings of xtensor";
4747

4848
m.def("mean", [](const xt::pyarray<double>& a) -> xt::pyarray<double> { return xt::mean(a); });
49+
m.def("average", [](const xt::pyarray<double>& a, const xt::pyarray<double>& w) -> xt::pyarray<double> { return xt::average(a, w); });
50+
m.def("average", [](const xt::pyarray<double>& a, const xt::pyarray<double>& w, const std::vector<ptrdiff_t>& axes) -> xt::pyarray<double> { return xt::average(a, w, axes); });
4951

5052
m.def("flip", [](const xt::pyarray<double>& a, ptrdiff_t axis) -> xt::pyarray<double> { return xt::flip(a, axis); });
5153

python-module/tests/test_a.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import unittest
2-
import timeit
3-
import warnings
42

53
import xt
64
import numpy as np
@@ -19,11 +17,24 @@ def test_mean(self):
1917

2018
self.assertTrue(np.allclose(n, x))
2119

22-
n = timeit.timeit(lambda: np.mean(a), number=10)
23-
x = timeit.timeit(lambda: xt.mean(a), number=10)
20+
def test_average(self):
2421

25-
if x / n > 1.1:
26-
warnings.warn(f"efficiency xt.mean {x / n:.2e}")
22+
a = np.random.random([103, 102, 101])
23+
w = np.random.random([103, 102, 101])
24+
n = np.average(a, weights=w)
25+
x = xt.average(a, w)
26+
27+
self.assertTrue(np.allclose(n, x))
28+
29+
def test_average_axes(self):
30+
31+
a = np.random.random([103, 102, 101])
32+
w = np.random.random([103, 102, 101])
33+
axis = int(np.random.randint(0, high=3))
34+
n = np.average(a, weights=w, axis=(axis,))
35+
x = xt.average(a, w, [axis])
36+
37+
self.assertTrue(np.allclose(n, x))
2738

2839
def test_flip(self):
2940

@@ -34,12 +45,6 @@ def test_flip(self):
3445

3546
self.assertTrue(np.allclose(n, x))
3647

37-
n = timeit.timeit(lambda: np.flip(a, axis), number=10)
38-
x = timeit.timeit(lambda: xt.flip(a, axis), number=10)
39-
40-
if x / n > 1.1:
41-
warnings.warn(f"efficiency xt.flip {x / n:.2e}")
42-
4348
def test_cos(self):
4449

4550
a = np.random.random([103, 102, 101])
@@ -48,13 +53,6 @@ def test_cos(self):
4853

4954
self.assertTrue(np.allclose(n, x))
5055

51-
n = timeit.timeit(lambda: np.cos(a), number=10)
52-
x = timeit.timeit(lambda: xt.cos(a), number=10)
53-
54-
if x / n > 1.1:
55-
warnings.warn(f"efficiency xt.cos {x / n:.2e}")
56-
57-
5856

5957
if __name__ == "__main__":
6058

0 commit comments

Comments
 (0)