-
Notifications
You must be signed in to change notification settings - Fork 70
Allow negative indices in pick_nth_selected
#110
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
Merged
danieldjohnson
merged 4 commits into
google-deepmind:main
from
JEM-Mosig:select_negative
Apr 26, 2025
Merged
Changes from 2 commits
Commits
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
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 |
|---|---|---|
|
|
@@ -20,12 +20,31 @@ | |
|
|
||
| import collections | ||
| import dataclasses | ||
| from typing import Any | ||
| from typing import Any, Iterable | ||
|
|
||
| from absl.testing import absltest | ||
| import jax | ||
| from penzai import pz | ||
| import penzai.core.selectors | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_indices, shift, expected_output", | ||
| [ | ||
| ((), 1, ()), | ||
| ([0, 3, -2], len(range(6)), (0, 3, 4)), | ||
| ] | ||
| ) | ||
| def test_shift_negative_indices( | ||
| input_indices: Iterable[int], | ||
| shift: int, | ||
| expected_output: tuple[int, ...], | ||
| ): | ||
| assert ( | ||
| penzai.core.selectors._shift_negative_indices(input_indices, shift=shift) | ||
| == expected_output | ||
| ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding this as an absltest-style test (as a method inside SelectorsTest) instead of using pytest here? The rest of the tests do not use pytest yet and I'd prefer to be consistent here for now. |
||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
|
|
@@ -565,6 +584,26 @@ def test_pick_nth_selected(self): | |
| ), | ||
| [0, 1, 2, 3, SELECTED_PART(value=4), 5, 6, 7, 8, 9], | ||
| ) | ||
| # Test negative indices for `pick_nth_selected` | ||
| self.assertEqual( | ||
| ( | ||
| pz.select(list(range(10))) | ||
| .at_instances_of(int) | ||
| .pick_nth_selected(-2) | ||
| .apply(SELECTED_PART) | ||
| ), | ||
| [0, 1, 2, 3, 4, 5, 6, 7, SELECTED_PART(value=8), 9], | ||
| ) | ||
| # Don't select anything if index is out of range | ||
| self.assertEqual( | ||
| ( | ||
| pz.select([0, 1, 2]) | ||
| .at_instances_of(int) | ||
| .pick_nth_selected(5) | ||
| .apply(SELECTED_PART) | ||
| ), | ||
| [0, 1, 2], | ||
| ) | ||
|
|
||
| def test_invert__example_1(self): | ||
| predicate = lambda node: isinstance(node, CustomLeaf) and node.tag <= 10 | ||
|
|
||
Oops, something went wrong.
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.
Not sure if I should just use this instead of defining
_shift_negative_indices. I'm weary of doing so because we're in the module that defines selectors here. Also, we don't need that kind of generality at this point and_shift_negative_indicesavoids some overheadThere 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.
Defining a new function here like you've done makes sense to me, the logic is probably too simple to benefit much from using selectors. (You could also just inline the logic into the call site since it's only used once.)
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'd prefer to keep it separate so code blocks stay short and hence easy to interpret