Skip to content

TST: Have array assert_eq check scalars - #2681

Merged
mrocklin merged 3 commits into
dask:masterfrom
TomAugspurger:array_eq-scalar
Sep 21, 2017
Merged

mrocklin merged 3 commits into
dask:masterfrom
TomAugspurger:array_eq-scalar

Conversation

@TomAugspurger

Copy link
Copy Markdown
Member

Closes #2680

@TomAugspurger

Copy link
Copy Markdown
Member Author

I didn't see any tests for assert_eq itself, so I made a test_testing.py module. Expanding that would be a good project for someone new looking to contribute to dask. I'll make an issue if / when this is merged so I can reference the module (same for the other assert_eq implementations).

Comment thread dask/array/tests/test_testing.py Outdated

def test_assert_eq_empty_passes():
# Treat empty arrays as equivalent
assert_eq(np.array([[], []]), np.array([]))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was the intention of that code block original, based on the comment # Treat all empty arrays as equivalent. Does that sound right to you @mrocklin?

@TomAugspurger

Copy link
Copy Markdown
Member Author

Caught at least one bug I think 😬

pytest dask/array/tests/test_linalg.py::test_norm_2dim --pdb -x

[25] > /Users/taugspurger/Envs/dask-dev/lib/python3.6/site-packages/dask/dask/array/tests/test_linalg.py(536)test_norm_2dim()
-> assert_eq(a_r, d_r)
(Pdb++) a_r
array([[ 5.04874206]])
(Pdb++) d_r.compute()
5.0487420596453054
(Pdb++) shape, chunks, axis, norm, keepdims
((5, 6), (2, 2), None, 'nuc', True)

I'll open another issue if that's not a quick fix.

@mrocklin

mrocklin commented Sep 14, 2017 via email

Copy link
Copy Markdown
Member

@TomAugspurger

Copy link
Copy Markdown
Member Author

Fixed the linalg bugs in ff63420. keepdims was being ignored in a few cases.

I don't have strong memories about what was going on with empty arrays.

I'll see what removing that check breaks.

@TomAugspurger

Copy link
Copy Markdown
Member Author

Just one failure after removing that clause:

2452         assert_eq(da.ones((5, 5), chunks=(2, 3))[:, :0] + 0,
2453  ->               np.ones((5, 5))[:0] + 0)

which looks to be an error in the test? If so, I'll just remove the empty array special casing.

@TomAugspurger

Copy link
Copy Markdown
Member Author

OK, the empty array special casing is gone, and everything is passing for me locally.

@TomAugspurger

TomAugspurger commented Sep 15, 2017 •

Copy link
Copy Markdown
Member Author

Hmm, this failure looks real: https://travis-ci.org/dask/dask/jobs/275665665#L7745 (you need the --runslow flag

cc @jakirkham. Does that look like a bug in the test, or is it in the code?

 113         usvt = np.dot(u, np.dot(np.diag(s), vt))
 114
 115         tol = 0.2
 116         assert_eq(np.linalg.norm(mat - usvt),
 117                   np.linalg.norm(mat),
 118  ->               rtol=tol, atol=tol)  # average accuracy check

edit: Sorry, I should have done git blame. It looks like @mrocklin or @marianotepper may be able to answer better. It's this section:

def test_svd_compressed():
m, n = 2000, 250
r = 10
np.random.seed(4321)
mat1 = np.random.randn(m, r)
mat2 = np.random.randn(r, n)
mat = mat1.dot(mat2)
data = da.from_array(mat, chunks=(500, 50))
u, s, vt = svd_compressed(data, r, seed=4321, n_power_iter=2)
u, s, vt = da.compute(u, s, vt)
usvt = np.dot(u, np.dot(np.diag(s), vt))
tol = 0.2
assert_eq(np.linalg.norm(mat - usvt),
np.linalg.norm(mat),
rtol=tol, atol=tol) # average accuracy check

Comment thread dask/array/linalg.py Outdated

r = svd(x)[1].sum()
if keepdims:
r = r[None, None]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just pass keepdims=keepdims to sum.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that only prevents 1 dimension from being removed. We need the output to be 2 dimensions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although maybe all of these could be replaced by a single check just before returning r?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't. That would be a bug.

With NumPy...

In [1]: np.arange(6).reshape(2, 3).sum(keepdims=True)
Out[1]: array([[15]])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think it's the slice that removes a dimension, and then the reduction that removes the second.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakirkham jakirkham Sep 15, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I see what you mean now. The middle argument is a vector. So we need to add another dimension to it in order to get a matrix back. It's a strange case to have keepdims in, but it make sense what NumPy is doing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding one singleton dimension before the reduction function with these svd cases and then calling the reduction function with keepdims did the trick for me.

Comment thread dask/array/linalg.py Outdated
elif len(axis) == 2 and ord == 2:
r = svd(x)[1].max()
if keepdims:
r = r[None, None]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just pass keepdims=keepdims to max.

Comment thread dask/array/linalg.py Outdated
elif len(axis) == 2 and ord == -2:
r = svd(x)[1].min()
if keepdims:
r = r[None, None]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just pass keepdims=keepdims to min.

@jakirkham

Copy link
Copy Markdown
Member

Thanks for the assist with handling keepdims in norm. Sorry to drop the ball on that.

All of the methods being called there (sum, max, and min) should also take keepdims. So I think we can just forward that flag on and avoid a branch. Otherwise thanks for catching those keepdims issues with matrices. Guessing you have added tests already that caught this issue.

@jakirkham

Copy link
Copy Markdown
Member

Sorry, I should have done git blame. It looks like @mrocklin or @marianotepper may be able to answer better.

Ah ok. Yeah didn't really touch the svd code. Just used it. :)

@jakirkham

Copy link
Copy Markdown
Member

Glad that you are working on fixing this. Also @bmerry may be interested in this too.

Comment thread dask/array/utils.py Outdated
assert allclose(a, b, **kwargs)
assert a.shape == b.shape
assert allclose(a, b, **kwargs)
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this suppose to return something (not None)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe True like the return below?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll return True to match the below.

@TomAugspurger

TomAugspurger commented Sep 15, 2017 •

Copy link
Copy Markdown
Member Author

I'm pretty confused about why the new test_assert_eq_checks_scalars failed to raise here. https://travis-ci.org/dask/dask/jobs/275845288#L7788 Am I missing something obvious?

@TomAugspurger

Copy link
Copy Markdown
Member Author

I'm still stumped by the non-svd failure. Does https://travis-ci.org/dask/dask/jobs/276491069#L7791 make any sense (with these debug statements)?

Looking into the SVD stuff later today.

@bmerry

bmerry commented Sep 18, 2017

Copy link
Copy Markdown
Contributor

I'm still stumped by the non-svd failure. Does https://travis-ci.org/dask/dask/jobs/276491069#L7791 make any sense (with these debug statements)?

The common feature seems to be PYTHONOPTIMIZE=2, which I assume turns on the -O flag to Python. That normally disables assert statements. Apparently pytest does some magic with assert statements in test modules, but presumably it's not taking effect in this utility module. I don't have much experience with pytest so I'm not sure how to turn on the magic.

@TomAugspurger

Copy link
Copy Markdown
Member Author

@bmerry thanks! I think that's it: https://travis-ci.org/dask/dask/jobs/276491069#L720

Here's the related issue: #1388

I'm skipping that test when optimize is enabled.

Comment thread dask/array/tests/test_testing.py Outdated
from dask.array.utils import assert_eq


@pytest.mark.skipif(sys.flags.optimize == 2, reason="Assertions disabled.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be sys.flags.optimize. Assertions are disabled even at level 1 (it just happens that Travis only tests with level 2).

Make the right assertion
@TomAugspurger

Copy link
Copy Markdown
Member Author

OK, by linear algebra is a little rusty, but I think a0a5bc5 has the fix for the compressed SVD test. Does that look right @marianotepper?

@bmerry

bmerry commented Sep 18, 2017

Copy link
Copy Markdown
Contributor

OK, by linear algebra is a little rusty, but I think a0a5bc5 has the fix for the compressed SVD test

Testing only that two matrices have the same norm seems like a weak test; perhaps the original intent was to check that the norm of the difference was (approximately) zero?

@TomAugspurger

TomAugspurger commented Sep 19, 2017 •

Copy link
Copy Markdown
Member Author

Checking that the norms are the same follows the pattern of the other tests (and there are additional checks below).

I think this is ready to go. If we want to fix up test_svd_compressed further, we can do that in a separate issue.

tol = 0.2
assert_eq(np.linalg.norm(mat - usvt),
assert_eq(np.linalg.norm(usvt),
np.linalg.norm(mat),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah what was happening here makes no sense to me. This seems like the best fix without taking this PR in a new direction. Would be nice to have an issue to revisit test_svd_compressed for someone to look at later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrocklin, do you know more about what this test was trying to do?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was probably trying to compare equality between the original and the recomputed result. This might be better written as assert_eq(mat, usvt) if there is some way to add in a tolerance to assert_eq (which there may be, I'm just a little rusty here)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a related note, should we document assert_eq and it to the public API?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...if there is some way to add in a tolerance to assert_eq (which there may be, I'm just a little rusty here)

Seems like there is as assert_eq calls allclose.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have an issue to revisit test_svd_compressed for someone to look at later.

Added as issue ( #2695 ).

@mrocklin

mrocklin commented Sep 21, 2017 via email

Copy link
Copy Markdown
Member

@jakirkham

jakirkham commented Sep 21, 2017 •

Copy link
Copy Markdown
Member

On a related note, should we document assert_eq and it to the public API?

No objection from me :)

Ok, added as issue ( #2694 ).

@jakirkham

Copy link
Copy Markdown
Member

Any objections to merging? :)

@mrocklin

Copy link
Copy Markdown
Member

None from me. In the future I recommend an optimistic approach like saying "I plan to merge this in a bit if no one objects", waiting for a few hours, and then merging.

@jakirkham

Copy link
Copy Markdown
Member

Ok noted.

@TomAugspurger

TomAugspurger commented Sep 21, 2017 via email

Copy link
Copy Markdown
Member Author

@mrocklin
mrocklin merged commit 3486d17 into dask:master Sep 21, 2017
fujiisoup pushed a commit to fujiisoup/dask that referenced this pull request Feb 3, 2018
* TST: Have array assert_eq check scalars

Closes dask#2680

* TST: Fixed broadcasting test

* TST: Fix svd_compressed

Make the right assertion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants