Skip to content

Commit

Permalink
[skip-ci] Add alignment benchmarks (#7738)
Browse files Browse the repository at this point in the history
* [skip-ci] Add alignment benchmarks

* [skip-ci] Add random selection

* Apply suggestions from code review

Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>

---------

Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
  • Loading branch information
dcherian and Illviljan authored Apr 7, 2023
1 parent 551de70 commit 8e899ad
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion asv_bench/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"distributed": [""],
"flox": [""],
"numpy_groupies": [""],
"sparse": [""]
"sparse": [""],
"cftime": [""]
},


Expand Down
54 changes: 54 additions & 0 deletions asv_bench/benchmarks/alignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np

import xarray as xr

from . import parameterized, requires_dask

ntime = 365 * 30
nx = 50
ny = 50

rng = np.random.default_rng(0)


class Align:
def setup(self, *args, **kwargs):
data = rng.standard_normal((ntime, nx, ny))
self.ds = xr.Dataset(
{"temperature": (("time", "x", "y"), data)},
coords={
"time": xr.date_range("2000", periods=ntime),
"x": np.arange(nx),
"y": np.arange(ny),
},
)
self.year = self.ds.time.dt.year
self.idx = np.unique(rng.integers(low=0, high=ntime, size=ntime // 2))
self.year_subset = self.year.isel(time=self.idx)

@parameterized(["join"], [("outer", "inner", "left", "right", "exact", "override")])
def time_already_aligned(self, join):
xr.align(self.ds, self.year, join=join)

@parameterized(["join"], [("outer", "inner", "left", "right")])
def time_not_aligned(self, join):
xr.align(self.ds, self.year[-100:], join=join)

@parameterized(["join"], [("outer", "inner", "left", "right")])
def time_not_aligned_random_integers(self, join):
xr.align(self.ds, self.year_subset, join=join)


class AlignCFTime(Align):
def setup(self, *args, **kwargs):
super().setup()
self.ds["time"] = xr.date_range("2000", periods=ntime, calendar="noleap")
self.year = self.ds.time.dt.year
self.year_subset = self.year.isel(time=self.idx)


class AlignDask(Align):
def setup(self, *args, **kwargs):
requires_dask()
super().setup()
self.ds = self.ds.chunk({"time": 100})

0 comments on commit 8e899ad

Please sign in to comment.