-
Notifications
You must be signed in to change notification settings - Fork 12
added support of slicing for IDSStructArray #20
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
Draft
prasad-sawantdesai
wants to merge
2
commits into
iterorganization:develop
Choose a base branch
from
prasad-sawantdesai:feature/allow-slices-for-IDSStructArray
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: Test using pytest | ||
|
||
on: [push, pull_request] | ||
on: push | ||
|
||
jobs: | ||
test: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 Prasad, Python slices are weird and have lots of edge cases with negative indices... the current logic doesn't quite capture that.
For example:
Edge cases with negative indices:
[-5:5]
, all items from the fifth from the end to the fifth from the beginning[2:-2]
, all items from the third to the second-to-last[::-1]
all items in reverse orderWhat is the the use case for this feature? With a list comprehension users could capture (most) scenarios as well:
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.
Thank you for reviewing @maarten-ic
Actually, this issue arises when using slicing for wall IDs, as shown below. There is a workaround—you can convert it into a list—but then you lose the IDSStructArray metadata. It would be convenient for users to work it as a list, what do you think @olivhoenen but of course we need to consider edge cases.
units = list(walids.description_2d[0].vessel.unit)
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 Prasad,
I don't understand the drawback: with the slicing you have implemented,
unit[8:]
is also a list, right?Yes, sure, but what's the actual use case for doing this? 🙂 I don't understand why a user would want to print all units except the first 8 (as you do in your code listing).
Note that I found out that slice objects have a method that give you the start/stop/step values for a sequence of given length: https://docs.python.org/3/reference/datamodel.html#slice.indices
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 came up in the context of @prasad-sawantdesai work on IDStools, where different scripts are receiving IMAS URIs that may contain fragments (
#ids:occ/ids_path
) where theids_path
has slicing (as described in https://imas-data-dictionary.readthedocs.io/en/latest/_downloads/9e7ffd162c0237b61062528affd5fe2a/IDS-path-syntax.md).What would be the easiest/recommended way to go from an
ids_path
string that contains slicing indices to an actual list of objects?ps: in the case of the list of
unit
from the wall IDS, the machine description contains many different types oflimiter
andvessel
units, not all of them make sense to overlay when plotting for example an equilibrium psi mapThere 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 Olivier,
This PR doesn't really solve that case either. I could still not do
wall.description_2d[0].vessel.unit[8:].element[:].name
: the slice atunit[8:]
will return a list that has no attributeelement
.This is actually a surprisingly tricky problem! And it mostly has to do with the possibility that IDSs don't have homogeneous array sizes deep down in the tree.
ibex
has an implementation of this and, since it converts everything to JSON (which supports ragged arrays), it can avoid some of the difficulty. Looks like it is mostly implemented in_expand_single_path_element
in this file. Though quickly scanning it, it looks like it doesn't handle the negative indices either (which is probably fine for theibex
use case).I see two options for implementing your use case, with option 1 the easiest to implement and option 2 more generally useful 🙂
IDSPath
, for exampleIDSPath.goto_slices
that returns a list of elements that match the slice syntax. It would be an extended form ofIDSPath.goto
(which returns a single element and raises aNotImplementedError
when a slice is used in the path),IDSSlice
). The IDSSlice can keep track of all objects that matched the slice expression, and allow further slicing of child elements, getting the matching elements, etc. That could look as follows:In either case you'll need to decide how to handle edge cases. For example, what does it mean when you do
unit[:2].element[2]
whenunit[0]
has 4 elements andunit[1]
has only 2 elements. Is this an IndexError? Or does this return a match of[unit[0].element[2]]
and just ignore that there is nounit[1].element[2]
?There's probably more dragons hiding when implementing something like this -- there's a reason that it's not currently implemented in IDSPath 😉
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'm not sure why inhomogeneity is a problem there: if a user ask for a slice that does not exist we have an out-of-bound error, whether it is homogeneous or not (admittedly the risk increase in inhomogeneous case, but that should not be a concern for the implementation). IMO index-errors shall not be swallowed if they happen in nested elements, the error shall then just be propagated.
What do you think @prasad-sawantdesai, would something like 1 or 2 be useful in this case? 2 looks more appealing to me (but maybe that's because of the code snippet 🙄 ).
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 will try to implement second option :)