Skip to content
forked from pydata/xarray

Commit f8a9c81

Browse files
committed
Merge remote-tracking branch 'upstream/main' into groupby-reduce
* upstream/main: Only run asv benchmark when labeled (pydata#5893) Add asv benchmark jobs to CI (pydata#5796) Remove use of deprecated `kind` argument in `CFTimeIndex` tests (pydata#5723) Single matplotlib import (pydata#5794) Check jupyter nbs with black in pre-commit (pydata#5891)
2 parents a12ba89 + fdabf3b commit f8a9c81

21 files changed

+420
-177
lines changed

.github/workflows/benchmarks.yml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Benchmark
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize, labeled]
6+
workflow_dispatch:
7+
8+
jobs:
9+
benchmark:
10+
if: ${{ contains( github.event.pull_request.labels.*.name, 'run-benchmark') && github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }}
11+
name: Linux
12+
runs-on: ubuntu-20.04
13+
env:
14+
ASV_DIR: "./asv_bench"
15+
16+
steps:
17+
# We need the full repo to avoid this issue
18+
# https://github.com/actions/checkout/issues/23
19+
- uses: actions/checkout@v2
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Setup Miniconda
24+
uses: conda-incubator/setup-miniconda@v2
25+
with:
26+
# installer-url: https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
27+
installer-url: https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh
28+
29+
- name: Setup some dependencies
30+
shell: bash -l {0}
31+
run: |
32+
pip install asv
33+
sudo apt-get update -y
34+
35+
- name: Run benchmarks
36+
shell: bash -l {0}
37+
id: benchmark
38+
env:
39+
OPENBLAS_NUM_THREADS: 1
40+
MKL_NUM_THREADS: 1
41+
OMP_NUM_THREADS: 1
42+
ASV_FACTOR: 1.5
43+
ASV_SKIP_SLOW: 1
44+
run: |
45+
set -x
46+
# ID this runner
47+
asv machine --yes
48+
echo "Baseline: ${{ github.event.pull_request.base.sha }} (${{ github.event.pull_request.base.label }})"
49+
echo "Contender: ${GITHUB_SHA} (${{ github.event.pull_request.head.label }})"
50+
# Use mamba for env creation
51+
# export CONDA_EXE=$(which mamba)
52+
export CONDA_EXE=$(which conda)
53+
# Run benchmarks for current commit against base
54+
ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR"
55+
asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \
56+
| sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \
57+
| tee benchmarks.log
58+
# Report and export results for subsequent steps
59+
if grep "Traceback \|failed\|PERFORMANCE DECREASED" benchmarks.log > /dev/null ; then
60+
exit 1
61+
fi
62+
working-directory: ${{ env.ASV_DIR }}
63+
64+
- name: Add instructions to artifact
65+
if: always()
66+
run: |
67+
cp benchmarks/README_CI.md benchmarks.log .asv/results/
68+
working-directory: ${{ env.ASV_DIR }}
69+
70+
- uses: actions/upload-artifact@v2
71+
if: always()
72+
with:
73+
name: asv-benchmark-results-${{ runner.os }}
74+
path: ${{ env.ASV_DIR }}/.asv/results

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ repos:
1616
rev: 21.9b0
1717
hooks:
1818
- id: black
19+
- id: black-jupyter
1920
- repo: https://github.com/keewis/blackdoc
2021
rev: v0.3.4
2122
hooks:

asv_bench/benchmarks/README_CI.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Benchmark CI
2+
3+
<!-- Author: @jaimergp -->
4+
<!-- Last updated: 2021.07.06 -->
5+
<!-- Describes the work done as part of https://github.com/scikit-image/scikit-image/pull/5424 -->
6+
7+
## How it works
8+
9+
The `asv` suite can be run for any PR on GitHub Actions (check workflow `.github/workflows/benchmarks.yml`) by adding a `run-benchmark` label to said PR. This will trigger a job that will run the benchmarking suite for the current PR head (merged commit) against the PR base (usually `main`).
10+
11+
We use `asv continuous` to run the job, which runs a relative performance measurement. This means that there's no state to be saved and that regressions are only caught in terms of performance ratio (absolute numbers are available but they are not useful since we do not use stable hardware over time). `asv continuous` will:
12+
13+
* Compile `scikit-image` for _both_ commits. We use `ccache` to speed up the process, and `mamba` is used to create the build environments.
14+
* Run the benchmark suite for both commits, _twice_ (since `processes=2` by default).
15+
* Generate a report table with performance ratios:
16+
* `ratio=1.0` -> performance didn't change.
17+
* `ratio<1.0` -> PR made it slower.
18+
* `ratio>1.0` -> PR made it faster.
19+
20+
Due to the sensitivity of the test, we cannot guarantee that false positives are not produced. In practice, values between `(0.7, 1.5)` are to be considered part of the measurement noise. When in doubt, running the benchmark suite one more time will provide more information about the test being a false positive or not.
21+
22+
## Running the benchmarks on GitHub Actions
23+
24+
1. On a PR, add the label `run-benchmark`.
25+
2. The CI job will be started. Checks will appear in the usual dashboard panel above the comment box.
26+
3. If more commits are added, the label checks will be grouped with the last commit checks _before_ you added the label.
27+
4. Alternatively, you can always go to the `Actions` tab in the repo and [filter for `workflow:Benchmark`](https://github.com/scikit-image/scikit-image/actions?query=workflow%3ABenchmark). Your username will be assigned to the `actor` field, so you can also filter the results with that if you need it.
28+
29+
## The artifacts
30+
31+
The CI job will also generate an artifact. This is the `.asv/results` directory compressed in a zip file. Its contents include:
32+
33+
* `fv-xxxxx-xx/`. A directory for the machine that ran the suite. It contains three files:
34+
* `<baseline>.json`, `<contender>.json`: the benchmark results for each commit, with stats.
35+
* `machine.json`: details about the hardware.
36+
* `benchmarks.json`: metadata about the current benchmark suite.
37+
* `benchmarks.log`: the CI logs for this run.
38+
* This README.
39+
40+
## Re-running the analysis
41+
42+
Although the CI logs should be enough to get an idea of what happened (check the table at the end), one can use `asv` to run the analysis routines again.
43+
44+
1. Uncompress the artifact contents in the repo, under `.asv/results`. This is, you should see `.asv/results/benchmarks.log`, not `.asv/results/something_else/benchmarks.log`. Write down the machine directory name for later.
45+
2. Run `asv show` to see your available results. You will see something like this:
46+
47+
```
48+
$> asv show
49+
50+
Commits with results:
51+
52+
Machine : Jaimes-MBP
53+
Environment: conda-py3.9-cython-numpy1.20-scipy
54+
55+
00875e67
56+
57+
Machine : fv-az95-499
58+
Environment: conda-py3.7-cython-numpy1.17-pooch-scipy
59+
60+
8db28f02
61+
3a305096
62+
```
63+
64+
3. We are interested in the commits for `fv-az95-499` (the CI machine for this run). We can compare them with `asv compare` and some extra options. `--sort ratio` will show largest ratios first, instead of alphabetical order. `--split` will produce three tables: improved, worsened, no changes. `--factor 1.5` tells `asv` to only complain if deviations are above a 1.5 ratio. `-m` is used to indicate the machine ID (use the one you wrote down in step 1). Finally, specify your commit hashes: baseline first, then contender!
65+
66+
```
67+
$> asv compare --sort ratio --split --factor 1.5 -m fv-az95-499 8db28f02 3a305096
68+
69+
Benchmarks that have stayed the same:
70+
71+
before after ratio
72+
[8db28f02] [3a305096]
73+
<ci-benchmark-check~9^2>
74+
n/a n/a n/a benchmark_restoration.RollingBall.time_rollingball_ndim
75+
1.23±0.04ms 1.37±0.1ms 1.12 benchmark_transform_warp.WarpSuite.time_to_float64(<class 'numpy.float64'>, 128, 3)
76+
5.07±0.1μs 5.59±0.4μs 1.10 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(<class 'numpy.float32'>, (192, 192, 192), (192, 192, 192))
77+
1.23±0.02ms 1.33±0.1ms 1.08 benchmark_transform_warp.WarpSuite.time_same_type(<class 'numpy.float32'>, 128, 3)
78+
9.45±0.2ms 10.1±0.5ms 1.07 benchmark_rank.Rank3DSuite.time_3d_filters('majority', (32, 32, 32))
79+
23.0±0.9ms 24.6±1ms 1.07 benchmark_interpolation.InterpolationResize.time_resize((80, 80, 80), 0, 'symmetric', <class 'numpy.float64'>, True)
80+
38.7±1ms 41.1±1ms 1.06 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(<class 'numpy.float32'>, (2048, 2048), (192, 192, 192))
81+
4.97±0.2μs 5.24±0.2μs 1.05 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(<class 'numpy.float32'>, (2048, 2048), (2048, 2048))
82+
4.21±0.2ms 4.42±0.3ms 1.05 benchmark_rank.Rank3DSuite.time_3d_filters('gradient', (32, 32, 32))
83+
84+
...
85+
```
86+
87+
If you want more details on a specific test, you can use `asv show`. Use `-b pattern` to filter which tests to show, and then specify a commit hash to inspect:
88+
89+
```
90+
$> asv show -b time_to_float64 8db28f02
91+
92+
Commit: 8db28f02 <ci-benchmark-check~9^2>
93+
94+
benchmark_transform_warp.WarpSuite.time_to_float64 [fv-az95-499/conda-py3.7-cython-numpy1.17-pooch-scipy]
95+
ok
96+
=============== ============= ========== ============= ========== ============ ========== ============ ========== ============
97+
-- N / order
98+
--------------- --------------------------------------------------------------------------------------------------------------
99+
dtype_in 128 / 0 128 / 1 128 / 3 1024 / 0 1024 / 1 1024 / 3 4096 / 0 4096 / 1 4096 / 3
100+
=============== ============= ========== ============= ========== ============ ========== ============ ========== ============
101+
numpy.uint8 2.56±0.09ms 523±30μs 1.28±0.05ms 130±3ms 28.7±2ms 81.9±3ms 2.42±0.01s 659±5ms 1.48±0.01s
102+
numpy.uint16 2.48±0.03ms 530±10μs 1.28±0.02ms 130±1ms 30.4±0.7ms 81.1±2ms 2.44±0s 653±3ms 1.47±0.02s
103+
numpy.float32 2.59±0.1ms 518±20μs 1.27±0.01ms 127±3ms 26.6±1ms 74.8±2ms 2.50±0.01s 546±10ms 1.33±0.02s
104+
numpy.float64 2.48±0.04ms 513±50μs 1.23±0.04ms 134±3ms 30.7±2ms 85.4±2ms 2.55±0.01s 632±4ms 1.45±0.01s
105+
=============== ============= ========== ============= ========== ============ ========== ============ ========== ============
106+
started: 2021-07-06 06:14:36, duration: 1.99m
107+
```
108+
109+
## Other details
110+
111+
### Skipping slow or demanding tests
112+
113+
To minimize the time required to run the full suite, we trimmed the parameter matrix in some cases and, in others, directly skipped tests that ran for too long or require too much memory. Unlike `pytest`, `asv` does not have a notion of marks. However, you can `raise NotImplementedError` in the setup step to skip a test. In that vein, a new private function is defined at `benchmarks.__init__`: `_skip_slow`. This will check if the `ASV_SKIP_SLOW` environment variable has been defined. If set to `1`, it will raise `NotImplementedError` and skip the test. To implement this behavior in other tests, you can add the following attribute:
114+
115+
```python
116+
from . import _skip_slow # this function is defined in benchmarks.__init__
117+
118+
def time_something_slow():
119+
pass
120+
121+
time_something.setup = _skip_slow
122+
```

asv_bench/benchmarks/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import os
23

34
import numpy as np
45

@@ -46,3 +47,21 @@ def randint(low, high=None, size=None, frac_minus=None, seed=0):
4647
x.flat[inds] = -1
4748

4849
return x
50+
51+
52+
def _skip_slow():
53+
"""
54+
Use this function to skip slow or highly demanding tests.
55+
56+
Use it as a `Class.setup` method or a `function.setup` attribute.
57+
58+
Examples
59+
--------
60+
>>> from . import _skip_slow
61+
>>> def time_something_slow():
62+
... pass
63+
...
64+
>>> time_something.setup = _skip_slow
65+
"""
66+
if os.environ.get("ASV_SKIP_SLOW", "0") == "1":
67+
raise NotImplementedError("Skipping this test...")

asv_bench/benchmarks/combine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Combine:
99
def setup(self):
1010
"""Create 4 datasets with two different variables"""
1111

12-
t_size, x_size, y_size = 100, 900, 800
12+
t_size, x_size, y_size = 50, 450, 400
1313
t = np.arange(t_size)
1414
data = np.random.randn(t_size, x_size, y_size)
1515

asv_bench/benchmarks/dataarray_missing.py

+63-57
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import xarray as xr
44

5-
from . import randn, requires_dask
6-
7-
try:
8-
import dask # noqa: F401
9-
except ImportError:
10-
pass
5+
from . import parameterized, randn, requires_dask
116

127

138
def make_bench_data(shape, frac_nan, chunks):
@@ -21,54 +16,65 @@ def make_bench_data(shape, frac_nan, chunks):
2116
return da
2217

2318

24-
def time_interpolate_na(shape, chunks, method, limit):
25-
if chunks is not None:
26-
requires_dask()
27-
da = make_bench_data(shape, 0.1, chunks=chunks)
28-
actual = da.interpolate_na(dim="time", method="linear", limit=limit)
29-
30-
if chunks is not None:
31-
actual = actual.compute()
32-
33-
34-
time_interpolate_na.param_names = ["shape", "chunks", "method", "limit"]
35-
time_interpolate_na.params = (
36-
[(3650, 200, 400), (100, 25, 25)],
37-
[None, {"x": 25, "y": 25}],
38-
["linear", "spline", "quadratic", "cubic"],
39-
[None, 3],
40-
)
41-
42-
43-
def time_ffill(shape, chunks, limit):
44-
45-
da = make_bench_data(shape, 0.1, chunks=chunks)
46-
actual = da.ffill(dim="time", limit=limit)
47-
48-
if chunks is not None:
49-
actual = actual.compute()
50-
51-
52-
time_ffill.param_names = ["shape", "chunks", "limit"]
53-
time_ffill.params = (
54-
[(3650, 200, 400), (100, 25, 25)],
55-
[None, {"x": 25, "y": 25}],
56-
[None, 3],
57-
)
58-
59-
60-
def time_bfill(shape, chunks, limit):
61-
62-
da = make_bench_data(shape, 0.1, chunks=chunks)
63-
actual = da.bfill(dim="time", limit=limit)
64-
65-
if chunks is not None:
66-
actual = actual.compute()
67-
68-
69-
time_bfill.param_names = ["shape", "chunks", "limit"]
70-
time_bfill.params = (
71-
[(3650, 200, 400), (100, 25, 25)],
72-
[None, {"x": 25, "y": 25}],
73-
[None, 3],
74-
)
19+
def requires_bottleneck():
20+
try:
21+
import bottleneck # noqa: F401
22+
except ImportError:
23+
raise NotImplementedError()
24+
25+
26+
class DataArrayMissingInterpolateNA:
27+
def setup(self, shape, chunks, limit):
28+
if chunks is not None:
29+
requires_dask()
30+
self.da = make_bench_data(shape, 0.1, chunks)
31+
32+
@parameterized(
33+
["shape", "chunks", "limit"],
34+
(
35+
[(365, 75, 75)],
36+
[None, {"x": 25, "y": 25}],
37+
[None, 3],
38+
),
39+
)
40+
def time_interpolate_na(self, shape, chunks, limit):
41+
actual = self.da.interpolate_na(dim="time", method="linear", limit=limit)
42+
43+
if chunks is not None:
44+
actual = actual.compute()
45+
46+
47+
class DataArrayMissingBottleneck:
48+
def setup(self, shape, chunks, limit):
49+
requires_bottleneck()
50+
if chunks is not None:
51+
requires_dask()
52+
self.da = make_bench_data(shape, 0.1, chunks)
53+
54+
@parameterized(
55+
["shape", "chunks", "limit"],
56+
(
57+
[(365, 75, 75)],
58+
[None, {"x": 25, "y": 25}],
59+
[None, 3],
60+
),
61+
)
62+
def time_ffill(self, shape, chunks, limit):
63+
actual = self.da.ffill(dim="time", limit=limit)
64+
65+
if chunks is not None:
66+
actual = actual.compute()
67+
68+
@parameterized(
69+
["shape", "chunks", "limit"],
70+
(
71+
[(365, 75, 75)],
72+
[None, {"x": 25, "y": 25}],
73+
[None, 3],
74+
),
75+
)
76+
def time_bfill(self, shape, chunks, limit):
77+
actual = self.da.ffill(dim="time", limit=limit)
78+
79+
if chunks is not None:
80+
actual = actual.compute()

0 commit comments

Comments
 (0)