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
Allow comparison between Cell and numpy array or list of len = 1. #4083
Conversation
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.
Hi @gcaria thanks for this and sorry nobody looked at it sooner. Your solution looks great for length 1 lists and arrays, but I'm afraid it doesn't quite address the case from #2914, as that involved a zero dimensioned array:
b = np.array(1.5)
len(b)
gives
TypeError: len() of unsized object
An alternative solution for numpy arrays might be to check other.size
and use other.item()
to get the value out. Or you might come up with a better idea - I don't want to be prescriptive!
It would also be good to add some simple tests to Test___common_cmp__ as a very basic check that you get the answer you expect for cases you have addressed (e.g. the example in #2914 should produce False
).
Finally, it looks like the tests didn't actually run when you opened this PR. There has been a lot of effort since then to fix our testing infrastructure. So hopefully if you rebase it will get everything working sensibly again.
Again, thanks for the contribution!
d60e042
to
27bf094
Compare
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.
Thanks for your feedback @rcomer!
I have edited the code to take care of arrays such as np.array(0)
, although it is arguably a bit of a misuse to not feed a sequence to np.array
.
More than happy to add tests too, just wanted to check first if the casting to float
sounds like a good idea (since the operations are <, >, <=, >=
it seems fine to me).
Thanks for the update @gcaria!
I don't know how the original issue came up and unfortunately the developers involved have both since moved on. However I've found a couple of ways the 0-dimensional array could be created: cube = iris.cube.Cube(1)
print(repr(cube.data))
array = np.squeeze([[[4]]])
print(repr(array)) gives
so possibly there was a use-case where the 0-dimensional array came out of some other calculation. Using |
Thanks for the clarification @rcomer, I was mostly surprised to see I have added a minimal set of lines for the testing. |
Thanks @gcaria, these tests look good to me. Could you add similar tests for the size 1 array/list case you originally added? Just to make sure the various paths through the Really appreciate your work on this - it's great to get some of these old issues tidied up! |
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.
Great; thanks @gcaria!
Fixes #2914.
A simple fix to allow numpy arrays (or lists) with only one element to be compared to a Cell.