-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
First step to simplify NDCubeSequence slicing implementation #251
Conversation
Hi @hayesla. I think this PR should also change the type of I don't think should cause any tests to fail, but if it does I think they should be simple enough fixes. Also change the docstring accordingly, i.e. this line to
|
It also looks like there is only one test that's failing: ndcube/tests/test_sequence_plotting.py:482 This PR also needs a |
@hayesla, could you move the code in |
so do you mean that getitem just returns the |
Yep, that's right, e.g. def __getitem__(self, item):
return utils.sequence.slice_sequence(self, item) |
Added a To Do checklist in the PR description to show what's left to do. As you can see there is not much to do. I am considering doing a bugfix release soon, perhaps at the start of next week? Since this is needed for How does this sound? |
This commit doesn't do things the way I meant. I think this is my fault for a confusing explanation. I suggest you wind back this commit and proceed without it. I'll remove the |
18f88fb
to
231c641
Compare
@DanRyanIrish - I think this is more along the lines you were thinking? It still don't fix the |
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.
This looks good!
ndcube/ndcube_sequence.py
Outdated
@@ -84,14 +84,21 @@ def cube_like_world_axis_physical_types(self): | |||
return self.data[0].world_axis_physical_types | |||
|
|||
def __getitem__(self, item): | |||
|
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.
changelog/251.trivial.rst
Outdated
@@ -0,0 +1 @@ | |||
use the `utils.sequence.slice_sequence` for __getitem__ |
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.
use the `utils.sequence.slice_sequence` for __getitem__ | |
Simplify and speed up implementation of NDCubeSequence slicing. |
Yes, let's keep that for a separate PR. |
ndcube/ndcube_sequence.py
Outdated
result = copy.deepcopy(self) | ||
result.data = self.data[item] | ||
if isinstance(item, slice): | ||
result.data = self.data[item] |
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.
result.data = self.data[item] | |
result.data = self.data[item] |
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.
Over-indented line.
Hmmm. I don't understand those test failures... Perhaps @nabobalis can interpret them for us? |
To get the tests to behave correctly, we have to pin pytest to <5.4. To do this change this line in Be sure to make this the only change in the commit. It makes it easy to |
done! |
Great! Hopefully that'll make the test fails more understandable. |
Seems to be: ===================================================================================== ERRORS ======================================================================================
_________________________________________________ ERROR collecting .tox/py38/Lib/site-packages/ndcube/tests/test_ndcollection.py __________________________________________________
..\..\.tox\py38\Lib\site-packages\ndcube\tests\test_ndcollection.py:77: in <module>
NDCollection([("seq0", sequence02[:, 1, 1:3]), ("seq1", sequence20[:, 1, 1:3])],
..\..\.tox\py38\Lib\site-packages\ndcube\ndcube_sequence.py:97: in __getitem__
elif isinstance(item[0], slice) and item[0].stop - item[0].start == 1:
E TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' The mac os build failed due to threading, so we will need to add |
I think this may be an actually bug in the code no? |
I would say yes? |
ndcube/ndcube_sequence.py
Outdated
else: | ||
if isinstance(item[0], numbers.Integral): | ||
result = result.data[item[0]][item[1:]] | ||
elif isinstance(item[0], slice) and item[0].stop - item[0].start == 1: |
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.
Just looked at this again. I don't think this line and the one below are needed at all? The line after the else
will do the same job in all remaining cases, I'm pretty sure? If so, that'll also get rid of the test failure.
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.
yeah its needed, as if you just do the list comprehension on the NDCube
rather on the list of NDCubes it then loops through the arrays in the NDCube if that makes sense
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.
i.e. as in for this result.data = [cube[item[1:]] for cube in result.data[item[0]]]
to work you need result.data[item[0]] to be a list of NDCubes rather than an NDCube. but if item[0] is a 0, or slice(0, 1) this doesn't work ... the way it is currently implemented also follows similar logic to the way the utils.sequence.slice_sequence kind of did it without the need of a SequenceItem
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.
Ah! You're dead right!
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.
elif isinstance(item[0], slice) and item[0].stop - item[0].start == 1: | |
elif isinstance(item[0], slice): | |
start = 0 if item[0].start is None else item[0].start | |
stop = len(self.data) if item[0].stop is None else item[0].stop | |
if stop - start == 1: | |
result.data = [result.data[item[0].start][item[1:]]] |
So this code passes tests now with the exception of one doctest in Comparison of test output:
Output when executing code locally: >>> regions_of_interest_in_sequence = my_sequence[1:3, 0, 1:3, 1:4]
>>> regions_of_interest_in_sequence.dimensions
(<Quantity 2. pix>, <Quantity 2. pix>, <Quantity 3. pix>) # This is the same as what the doctest expects. |
I am not sure. There has to be some difference but what I can't say for now. |
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.
Just realised why this was failing. The case where item[0]
is a slice but stop - start != 1
is not handled!
The case where the slice item was a tuple, item[0] is a slice of length > 1 so simply dropped. This fixes that oversight.
Hi @hayesla. I got this PR working. Hooray!!! Before merging, could I ask you to squash all the commits? If you aren't sure how to do that we can walk through it together. Making this a 1 commit PR will make cherry-picking it for the release of 1.3.1 much easier. |
Thanks for this successful PR @hayesla!! |
whoop whoop first NDCube PR 👯 |
First step to simplify NDCubeSequence slicing implementation
First pass at #248
To Do