Skip to content
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

Provide ParameterCollection.where for efficient conditional iteration of parameters #1899

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

drewj-tp
Copy link
Contributor

What is the change?

Provide ParameterCollection.where for better iteration of parameters

Allows selective iteration of parameters that meet, by their definition
not their value, meet a certain condition. This would be useful to
iterate over all parameters on a Block that are relevant for
neutronics calculations with

for p in block.p.where(lambda pd: pd.hasCategory("neutronics")):
    ...

The argument is a function that should return true for a given parameter
and can be complicated

block.p.where(
    lambda pd: (
        pd.atLocation(ParamLocation.EDGES) or
        pd.atLocation(ParamLocation.CORNERS)
    )
)

Why is the change being made?

Inspired by HexBlock rotation where we need to do stuff for parameters that meet certain criteria.

Closes #1898


Checklist

  • The release notes have been updated if necessary.
  • The documentation is still up-to-date in the doc folder.
  • The dependencies are still up-to-date in pyproject.toml.

Allows selective iteration of parameters that meet, by their definition
not their value, meet a certain condition. This would be useful to
iterate over all parameters on a ``Block`` that are relevant for
neutronics calculations with
```python
for p in block.p.where(lambda pd: "neutronics" in pd.categories):
    ...
```
The argument is a function that should return true for a given parameter
and can be complicated
```python
block.p.where(
    lambda pd: (
        pd.atLocation(ParamLocation.EDGES) or
        pd.atLocation(ParamLocation.CORNERS)
    )
)
```

Closes #1898
(cherry picked from commit 4162cd7)
@drewj-tp drewj-tp self-assigned this Sep 20, 2024
@drewj-tp drewj-tp marked this pull request as ready for review September 21, 2024 00:01
>>> pdef = block.p.paramDefs
>>> for param in pdef.where(lambda pd: pd.atLocation(ParamLocation.EDGES)):
... print(param.name, block.p[param.name])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNIMPORTANT

I see you put these empty lines at the end of a about half your docstrings. I just checked, the RST renders fine without this.

Comment on lines +553 to +554
pc: typing.ClassVar[parameters.ParameterCollection]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pc: typing.ClassVar[parameters.ParameterCollection]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, but I believe this line is unneeded. The setupClass() method handles this for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow a way to efficiently iterate over some parameters
2 participants