-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic duck array testing - reductions #4972
Draft
keewis
wants to merge
140
commits into
pydata:main
Choose a base branch
from
keewis:duckarray-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
140 commits
Select commit
Hold shift + click to select a range
21696bf
add a initial, tiny draft of the automatic duckarray test machinery
keewis f14ba29
add missing comma
keewis 90f9c41
fix the global marks
keewis aa4a457
don't try to apply marks if marks is None
keewis 9fa2eca
only set pytestmark if the value is not None
keewis 7994bad
skip the module if pint is not installed
keewis c4a35f0
filter UnitStrippedWarnings
keewis 0efbbbb
also test sparse
keewis 73499b5
add a test for the test extractor
keewis 532f213
move the selector parsing code to a new function
keewis f44aafa
also skip the sparse tests
keewis d651438
move the utils tests into a different file
keewis f84894a
don't keep the utils tests in a test group
keewis 0090db5
split apply_marks into two separate functions
keewis ef05c7d
add a mark which attaches marks to test variants
keewis 20334d9
move the duckarray testing module to tests
keewis f7acc0f
move the utils to a separate module
keewis e41a15b
fix the existing tests
keewis 1f095a1
completely isolate the apply_marks tests
keewis 2503af7
add a test for applying marks to test variants
keewis b229645
skip failing test variants
keewis 0723418
fix the import path
keewis 6c4ccb0
rename the duckarray testing module
keewis c4aa05a
use Variable as example
keewis fc97e90
fix the skips
keewis 31e577a
only use dimensionless for cumprod
keewis 8d80212
also test dask wrapped by pint
keewis 7c43e91
add a function to concatenate mappings
keewis b6a90df
add tests for preprocess_marks
keewis a95b5c4
fix the tests
keewis aa7caaa
show the duplicates in the error message
keewis 6415be8
add back support for test marks
keewis de25594
allow passing a list of addition assert functions
keewis 1b0f372
add some notes about the test suite
keewis 706ee54
simplify the extra_assert function
keewis caf6308
Merge branch 'master' into duckarray-tests
keewis cd5aa70
convert to hypothesis
keewis 08d72ed
add a marker to convert label-space parameters
keewis 0649d59
add a dummy expect_error function
keewis c32cb5a
compute actual before expected
keewis 440e0bd
pass a strategy instead of a single dtype
keewis f74a29c
set a default for expect_error
keewis d9346f8
add a test for clip
keewis 75f584a
allow passing a separate "create_label" function
keewis b94c84d
draft the base class hierarchy tailored after pandas' extension array…
keewis 7a150f8
make sure multiple dims are passed as a list
keewis a6eecb8
sort the dtypes differently
keewis dcb9fc0
add a strategy to generate a single axis
keewis 0ee096d
add a function to compute the axes from the dims
keewis 53debb2
move the call of the operation to a hook
keewis 9b2c0a3
remove the arg* methods since they are not reducing anything
keewis a6efbe1
add a context manager to suppress specific warnings
keewis 5d679bf
don't try to reduce along multiple dimensions
keewis 50db3c3
demonstrate the new pattern using pint
keewis 8114d2a
fix the sparse tests
keewis 6afc7c3
Merge branch 'duckarray-testing-baseclasses' into duckarray-tests
keewis 2f084d0
Merge branch 'master' into duckarray-tests
keewis 14349f2
back to only raising for UnitStrippedWarning
keewis 6a658ef
remove the old duckarray testing module
keewis d595cd6
rename the tests
keewis 2b0dcba
add a mark to skip individual test nodes
keewis 6b61900
skip the prod and std tests
keewis b9535a1
skip all sparse tests for now
keewis c675f8d
also skip var
keewis 6e7c538
add a duckarray base class
keewis e0ee7a6
move the strategies to a separate file and add a variable strategy
keewis 3feef1c
add a simple DataArray strategy and use it in the DataArray tests
keewis 2a70c38
use the DataArray reduce tests with pint
keewis 6a18acf
add a simple strategy to create Dataset objects
keewis 835930c
fix the variable strategy
keewis 0a5c487
adjust the dataset strategy
keewis d1184a4
parametrize the dataset strategy
keewis 12b5527
fix some of the pint testing utils
keewis 1f95318
use flatmap to define the data_vars strategy
keewis 9800db5
add tests for dataset reduce
keewis c43f35e
demonstrate the use of the dataset reduce tests using pint
keewis d1b541e
simplify check_reduce
keewis 19d9d96
remove apparently unnecessary skips
keewis 69e0624
skip the tests if hypothesis is missing
keewis c7f6677
update the sparse tests
keewis 396c2ba
add DataArray and Dataset tests for sparse
keewis ead706e
fix attach_units
keewis 3cf9523
rename the test classes
keewis cd132c6
update a few strategies
keewis 1c310b0
fix the strategies and utils
keewis 7f879b0
use allclose instead of identical to compare
keewis ff91be8
don't provide a default for shape
keewis cb286ef
remove the function to generate dimension names
keewis 438f8a5
simplify the generation of the dimension sizes
keewis 01814ff
immediately draw the computed dimension sizes
keewis 0f1222e
convert the sizes to a dict when making sure data vars are not dims
keewis a38a307
align the default maximum number of dimensions
keewis ea3d015
draw the data before passing it to DataArray
keewis afa33ac
directly generate the reduce dimensions
keewis 566627a
Merge branch 'master' into duckarray-tests
keewis 2e0c6bf
disable dim=[] / axis=() because that's not supported by all duckarrays
keewis 01599b7
skip the sparse tests
keewis 259e1d5
typo
keewis 527b17c
use a single dtype for all variables of a dataset
keewis 3437c3d
specify tolerances per dtype
keewis 4866801
abandon the notion of single_dtype=True
keewis 8019a20
limit the values and add dtype specific tolerances
keewis cc75b46
Merge branch 'main' into duckarray-tests
keewis 566470b
Merge branch 'main' into duckarray-tests
keewis b0e94f1
disable bottleneck
keewis e57cd7b
Merge branch 'main' into duckarray-tests
keewis 33f63a7
reduce the maximum number of dims, dim sizes, and variables
keewis 11d41e3
disable bottleneck for the sparse tests
keewis 71a37ba
try activating the sparse tests
keewis 1d98fec
propagate the dtypes
keewis a826249
Merge remote-tracking branch 'upstream/main' into duckarray-tests
dcherian f2cd8a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 21a5838
Merge branch 'main' into duckarray-tests
dcherian c747733
Turn off deadlines
dcherian 3f81995
Disable float16 tests.
dcherian a282686
Use as_numpy in as_dense
dcherian ede0045
Merge branch 'main' into duckarray-tests
dcherian cbf408c
Merge branch 'main' into duckarray-tests
keewis f5b9bdc
move the hypothesis importorskip to before the strategy definitions
keewis ed68dc2
properly filter out float16 dtypes for sparse
keewis 1e4f18e
also filter out complex64 because there seems to be a bug in sparse
keewis da2225f
Merge branch 'main' into duckarray-tests
dcherian 37622c5
Merge branch 'main' into duckarray-tests
dcherian 86377e6
Update xarray/tests/duckarrays/test_sparse.py
dcherian 5af49d8
use the proper base to check the dtypes
keewis 8d0a8c3
Merge branch 'main' into duckarray-tests
TomNicholas 50151a4
make sure the importorskip call is before any hypothesis imports
keewis 707aecb
remove the op parameter to create
keewis 23e4e70
Merge branch 'main' into duckarray-tests
TomNicholas cc7af83
add special test job for duck arrays
keewis 644bf69
limit the github annotations to just the standard env
keewis cae9287
don't run duckarray tests unless explicitly requested
keewis 240cbcf
fix the workflow
keewis aa7d893
one more fix
keewis fa34023
try not pinning the os for duckarray tests
keewis b92f272
explicitly write out the new entries
keewis 4b5f63b
try removing the variable evaluation
keewis 909583c
set the environment file by default
keewis 2bcdc83
don't overwrite the default
keewis 9161a7d
Merge branch 'main' into duckarray-tests
TomNicholas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .reduce import DataArrayReduceTests, DatasetReduceTests, VariableReduceTests | ||
|
||
__all__ = [ | ||
"VariableReduceTests", | ||
"DataArrayReduceTests", | ||
"DatasetReduceTests", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import hypothesis.strategies as st | ||
import numpy as np | ||
import pytest | ||
from hypothesis import given, note, settings | ||
|
||
from ... import assert_identical | ||
from . import strategies | ||
|
||
|
||
class VariableReduceTests: | ||
def check_reduce(self, obj, op, *args, **kwargs): | ||
actual = getattr(obj, op)(*args, **kwargs) | ||
|
||
data = np.asarray(obj.data) | ||
expected = getattr(obj.copy(data=data), op)(*args, **kwargs) | ||
|
||
note(f"actual:\n{actual}") | ||
note(f"expected:\n{expected}") | ||
|
||
assert_identical(actual, expected) | ||
|
||
@staticmethod | ||
def create(shape, dtypes): | ||
return strategies.numpy_array(shape) | ||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
( | ||
"all", | ||
"any", | ||
"cumprod", | ||
"cumsum", | ||
"max", | ||
"mean", | ||
"median", | ||
"min", | ||
"prod", | ||
"std", | ||
"sum", | ||
"var", | ||
), | ||
) | ||
@given(st.data()) | ||
@settings(deadline=None) | ||
def test_reduce(self, method, data): | ||
var = data.draw( | ||
strategies.variable(lambda shape, dtypes: self.create(shape, dtypes)) | ||
) | ||
|
||
reduce_dims = data.draw(strategies.valid_dims(var.dims)) | ||
|
||
self.check_reduce(var, method, dim=reduce_dims) | ||
|
||
|
||
class DataArrayReduceTests: | ||
def check_reduce(self, obj, op, *args, **kwargs): | ||
actual = getattr(obj, op)(*args, **kwargs) | ||
|
||
data = np.asarray(obj.data) | ||
expected = getattr(obj.copy(data=data), op)(*args, **kwargs) | ||
|
||
note(f"actual:\n{actual}") | ||
note(f"expected:\n{expected}") | ||
|
||
assert_identical(actual, expected) | ||
|
||
@staticmethod | ||
def create(op, shape, dtypes): | ||
return strategies.numpy_array(shape, dtypes) | ||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
( | ||
"all", | ||
"any", | ||
"cumprod", | ||
"cumsum", | ||
"max", | ||
"mean", | ||
"median", | ||
"min", | ||
"prod", | ||
"std", | ||
"sum", | ||
"var", | ||
), | ||
) | ||
@given(st.data()) | ||
@settings(deadline=None) | ||
def test_reduce(self, method, data): | ||
arr = data.draw( | ||
strategies.data_array(lambda shape, dtypes: self.create(shape, dtypes)) | ||
) | ||
|
||
reduce_dims = data.draw(strategies.valid_dims(arr.dims)) | ||
|
||
self.check_reduce(arr, method, dim=reduce_dims) | ||
|
||
|
||
class DatasetReduceTests: | ||
def check_reduce(self, obj, op, *args, **kwargs): | ||
actual = getattr(obj, op)(*args, **kwargs) | ||
|
||
data = {name: np.asarray(obj.data) for name, obj in obj.variables.items()} | ||
expected = getattr(obj.copy(data=data), op)(*args, **kwargs) | ||
|
||
note(f"actual:\n{actual}") | ||
note(f"expected:\n{expected}") | ||
|
||
assert_identical(actual, expected) | ||
|
||
@staticmethod | ||
def create(shape, dtypes): | ||
return strategies.numpy_array(shape, dtypes) | ||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
( | ||
"all", | ||
"any", | ||
"cumprod", | ||
"cumsum", | ||
"max", | ||
"mean", | ||
"median", | ||
"min", | ||
"prod", | ||
"std", | ||
"sum", | ||
"var", | ||
), | ||
) | ||
@given(st.data()) | ||
@settings(deadline=None) | ||
def test_reduce(self, method, data): | ||
ds = data.draw( | ||
strategies.dataset( | ||
lambda shape, dtypes: self.create(shape, dtypes), max_size=5 | ||
) | ||
) | ||
|
||
reduce_dims = data.draw(strategies.valid_dims(ds.dims)) | ||
|
||
self.check_reduce(ds, method, dim=reduce_dims) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we rename instances of arrays to be called
arr
instead ofdata
? It took me a pretty long time to realise that this had nothing to do with hypothesis'strategies.data
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, although it would probably be better to call it
numpy_arr
or move it into the.copy
call