-
Notifications
You must be signed in to change notification settings - Fork 48
ENH: Adding tests for tril and triu
#442
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
Changes from all commits
5ca8a3b
409ac69
64e6b97
84be72a
5dd3b5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -392,6 +392,74 @@ def test_eye(n_rows, n_cols, kw): | |
| raise | ||
|
|
||
|
|
||
|
|
||
| @given(x=hh.arrays(dtype=hh.numeric_dtypes, shape=hh.matrix_shapes()), data=st.data()) | ||
| def test_tril(x, data): | ||
| n, m = x.shape[-2:] | ||
| k = data.draw( | ||
| st.integers(min_value=-max(n, m, 1), max_value=max(n, m, 1)), | ||
| label="k", | ||
| ) | ||
| repro_snippet = ph.format_snippet(f"xp.tril({x!r}, k={k!r})") | ||
| try: | ||
| out = xp.tril(x, k=k) | ||
| ph.assert_dtype("tril", in_dtype=x.dtype, out_dtype=out.dtype) | ||
| ph.assert_shape("tril", out_shape=out.shape, expected=x.shape) | ||
| zero = xp.asarray(0, dtype=out.dtype) | ||
| expected_elements = [ | ||
| x[idx] if idx[-1] <= idx[-2] + k | ||
| else zero | ||
| for idx in sh.ndindex(x.shape) | ||
| ] | ||
| if expected_elements: | ||
| expected = xp.stack(expected_elements) | ||
| else: | ||
| # xp.stack does not accept an empty sequence, and the dtype cannot | ||
| # be inferred when there are no elements, so use out.dtype explicitly. | ||
| expected = xp.asarray([], dtype=out.dtype) | ||
| expected = xp.reshape(expected, x.shape) | ||
| ph.assert_array_elements( | ||
| "tril", | ||
| out=out, | ||
| expected=expected, | ||
| ) | ||
|
Comment on lines
+409
to
+425
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've moved the I think this brings us closer to how
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing that fails right now is running for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it still fail if you add
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. It takes 13 min to complete the test on my machine. |
||
| except Exception as exc: | ||
| ph.add_note(exc, repro_snippet) | ||
| raise | ||
|
|
||
|
|
||
| @given(x=hh.arrays(dtype=hh.numeric_dtypes, shape=hh.matrix_shapes()), data=st.data()) | ||
| def test_triu(x, data): | ||
| n, m = x.shape[-2:] | ||
| k = data.draw( | ||
| st.integers(min_value=-max(n, m, 1), max_value=max(n, m, 1)), | ||
| label="k", | ||
| ) | ||
| repro_snippet = ph.format_snippet(f"xp.triu({x!r}, k={k!r})") | ||
| try: | ||
| out = xp.triu(x, k=k) | ||
| ph.assert_dtype("triu", in_dtype=x.dtype, out_dtype=out.dtype) | ||
| ph.assert_shape("triu", out_shape=out.shape, expected=x.shape) | ||
| zero = xp.asarray(0, dtype=out.dtype) | ||
| expected_elements = [ | ||
| x[idx] if idx[-1] >= idx[-2] + k | ||
| else zero | ||
| for idx in sh.ndindex(x.shape) | ||
| ] | ||
| if expected_elements: | ||
| expected = xp.stack(expected_elements) | ||
| else: | ||
| expected = xp.asarray([], dtype=out.dtype) | ||
| expected = xp.reshape(expected, x.shape) | ||
| ph.assert_array_elements( | ||
| "triu", | ||
| out=out, | ||
| expected=expected, | ||
| ) | ||
| except Exception as exc: | ||
| ph.add_note(exc, repro_snippet) | ||
| raise | ||
|
|
||
| default_unsafe_dtypes = [xp.uint64] | ||
| if dh.default_int == xp.int32: | ||
| default_unsafe_dtypes.extend([xp.uint32, xp.int64]) | ||
|
|
||
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.
To double-check: this branching is needed even if if
expected_elementsis an empty list because 1)xp.stackdoes not accept an empty list, and even if it did, 2) it has no way to guessdtype=out.dtype. Would be nice to add a comment, since it's one of things which goes against numpy finger (and eye) memory.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.
Agreed.