Skip to content

Add check_type to array assert_eq - #7491

Merged
jrbourbeau merged 7 commits into
dask:mainfrom
jsignell:check-type
Apr 2, 2021
Merged

jrbourbeau merged 7 commits into
dask:mainfrom
jsignell:check-type

Conversation

@jsignell

@jsignell jsignell commented Mar 31, 2021 •

Copy link
Copy Markdown
Member

As mentioned in #7486 (comment) this check was missing.

@jrbourbeau jrbourbeau left a comment

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.

Thanks @jsignell! It looks like we only want to use *_computed when the corresponding input is a Dask collection, otherwise we'll be comparing things like numpy.ndarray to None. In this case it probably makes sense to fallback to using a when a_computed is None

@jsignell

Copy link
Copy Markdown
Member Author

lol yeah I am looking at this locally. I think we just want to use a and b actually. They get computed if needed at the start.

@jsignell

Copy link
Copy Markdown
Member Author

Ok so there are a bunch of failures, my plan on this PR is to add in check_type=False to all the ones that fail and make a ticket to fix them.

z = da.map_overlap(lambda x, y: x + y, x, y)
# func should be called twice and get (5,) and (1, 5) arrays of ones each time
assert_eq(z.shape, (1, 10))
assert_eq(z.sum(), 20)

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.

This is a real fix! Since 20 doesn't have a dtype we weren't catching that the output of z.sum() is a float not an int.

@jsignell

Copy link
Copy Markdown
Member Author

not sure why test_mmep is segfaulting...

assert z.shape == (2, 2)
# func call will receive (8,) and (10, 8) arrays for each of 4 blocks
assert_eq(z.sum(), 4 * (10 * 8 + 8))
assert_eq(z.sum(), 4.0 * (10 * 8 + 8))

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.

same here :)

@jsignell

Copy link
Copy Markdown
Member Author

Ok I expect this to pass tests now.

@jsignell jsignell changed the title Add check_type to array assert_eq Add check_type to array assert_eq Mar 31, 2021

@gjoseph92 gjoseph92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@jsignell I think having the option to turn off check_type in assert_eq is useful in general. For @jakirkham's failing test though, what do you think about the approach in 757927a of having the final concatenate preserve ndarray subtypes? That basically solves the recarray and memap tests (memap with this small hack in df24a87; debatable if that's a good idea).

For non-ndarray subclasses, we should already get similar type-preserving behavior from __array_function__, if the type implements it. (You could argue that np.recarray and np.memap are just poorly-behaved ndarray subclasses, since they don't implement __array_function__. But alas, we still have to deal with it.)

I think in some cases, you might actually care that ddf.to_records().compute() doesn't return a np.recarray as you were expecting—for example, attribute access to fields would fail. For that one in particular, maybe we want to change the behavior, not the testing?


# Check if it's an equivalent array
assert_eq(x, y, check_meta=False)
assert_eq(x, y, check_meta=False, check_type=False)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@jsignell I'm a little confused why this needs check_type=False; is the result here not an EncapsulateNDArray? Since the EncapsulateNDArray implements __array_function__, I'd think the np.concatenate in finalize would get dispatched to it.

I feel like it's important to check that this is indeed an EncapsulateNDArray; for example, if you called compute on a CuPy-backed array, you'd want to get a CuPy array out, not an ndarray where the data has been copied to host memory.

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 agree that it should not need a check_type=False

@jsignell

jsignell commented Apr 1, 2021

Copy link
Copy Markdown
Member Author

Maybe the intent of this PR was maybe unclear. I think check_type is probably something that people assumed assert_eq was already doing. So in this PR I am really just trying to highlight cases where check_type is failing. Then in another PR (or this one) we can fix them and turn check_type back on.

@jsignell

jsignell commented Apr 1, 2021

Copy link
Copy Markdown
Member Author

what do you think about the approach in 757927a of having the final concatenate preserve ndarray subtypes

Yeah, I think that concatenate should preserve subtypes.

@gjoseph92

Copy link
Copy Markdown
Collaborator

Maybe the intent of this PR was maybe unclear. I think check_type is probably something that people assumed assert_eq was already doing.

Thanks for the clarification—I had in fact made exactly that misunderstanding! Totally makes sense now. I think the "add it now, fix failures as we go" approach sounds good. Sorry for not understanding what you were going for here.

Would you like me to pull 757927a and df24a87 out into separate PRs once this is merged, so we can get them in independently of the DataFrame -> Array meta discussion?

@jrbourbeau jrbourbeau left a comment

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.

Thanks @jsignell. I've got one small question, otherwise this is good to go

Comment thread dask/array/utils.py
a.shape == b.shape
), f"a and b have different shapes (a: {a.shape}, b: {b.shape})"
if check_type:
_a = a if a.shape else a.item()

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.

What objects do we need to call .item() for?

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.

Arrays with shape 0 are effectively the same as just the .item() of themselves.

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.

For example:

da.assert_eq(np.array(12.33), 12.33)

This passes assert_eq on main, and I wanted it to pass with check_type as well.

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.

FWIW doing [()] should also work. This has the added benefit of being a no-op for arrays with some shape

In [1]: import numpy as np

In [2]: np.array(12)[()]
Out[2]: 12

In [3]: np.array([12])[()]
Out[3]: array([12])

In [4]: np.array([11, 12])[()]
Out[4]: array([11, 12])

In [5]: np.array([[12]])[()]
Out[5]: array([[12]])

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 nice! I am happy to do whichever people think is cleaner. I am checking shape, so the functionality is the same either way.

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.

No strong feelings. Either approach seems fine 🙂

@jsignell

jsignell commented Apr 1, 2021

Copy link
Copy Markdown
Member Author

Would you like me to pull 757927a and df24a87 out into separate PRs once this is merged, so we can get them in independently of the DataFrame -> Array meta discussion?

Yeah that sounds good.

@jrbourbeau jrbourbeau left a comment

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.

Thanks @jsignell!

@jrbourbeau
jrbourbeau merged commit 5061f9b into dask:main Apr 2, 2021
@gjoseph92 gjoseph92 mentioned this pull request Apr 2, 2021
2 tasks done
@jsignell
jsignell deleted the check-type branch April 2, 2021 21:32
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