Repository navigation
Add check_type to array assert_eq - #7491
Conversation
jrbourbeau
left a comment
There was a problem hiding this comment.
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
|
lol yeah I am looking at this locally. I think we just want to use |
|
Ok so there are a bunch of failures, my plan on this PR is to add in |
| 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) |
There was a problem hiding this comment.
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.
|
not sure why |
| 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)) |
|
Ok I expect this to pass tests now. |
check_type to array assert_eq
gjoseph92
left a comment
There was a problem hiding this comment.
@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) |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
I agree that it should not need a check_type=False
|
Maybe the intent of this PR was maybe unclear. I think |
Yeah, I think that concatenate should preserve subtypes. |
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 |
jrbourbeau
left a comment
There was a problem hiding this comment.
Thanks @jsignell. I've got one small question, otherwise this is good to go
| 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() |
There was a problem hiding this comment.
What objects do we need to call .item() for?
There was a problem hiding this comment.
Arrays with shape 0 are effectively the same as just the .item() of themselves.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]])There was a problem hiding this comment.
Oh nice! I am happy to do whichever people think is cleaner. I am checking shape, so the functionality is the same either way.
There was a problem hiding this comment.
No strong feelings. Either approach seems fine 🙂
As mentioned in #7486 (comment) this check was missing.