-
Notifications
You must be signed in to change notification settings - Fork 87
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
drewj-tp
wants to merge
9
commits into
main
Choose a base branch
from
drewj/iter-params/1898
base: main
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4483527
Provide ParameterCollection.where for better iteration of parameters
drewj-tp 0c5f542
Type hint ParameterCollection.__eq__
drewj-tp 6fc7204
Type hint and doc ParameterCollection.__iter__
drewj-tp 3bb13f7
Provide Parameter.hasCategory
drewj-tp 83e126f
Use Parameter.hasCategory in ParameterCollection.where tests
drewj-tp 18e5219
Test Parameters from ParameterCollection.where pass the filter function
drewj-tp e335f07
Add docs for ParameterCollectionWhere tests
drewj-tp 6efe6e1
Add release notes for PR 1899
drewj-tp 9493ef6
Fix ParameterCollection.where docstring
drewj-tp 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 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 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 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 | ||
---|---|---|---|---|
|
@@ -13,6 +13,7 @@ | |||
# limitations under the License. | ||||
"""Tests of the Parameters class.""" | ||||
import copy | ||||
import typing | ||||
import unittest | ||||
|
||||
from armi.reactor import parameters | ||||
|
@@ -456,10 +457,18 @@ class MockPC(parameters.ParameterCollection): | |||
self.assertEqual(p2.categories, set(["awesome", "stuff", "bacon"])) | ||||
self.assertEqual(p3.categories, set(["bacon"])) | ||||
|
||||
for p in [p1, p2, p3]: | ||||
self._testCategoryConsistency(p) | ||||
|
||||
self.assertEqual(set(pc.paramDefs.inCategory("awesome")), set([p1, p2])) | ||||
self.assertEqual(set(pc.paramDefs.inCategory("stuff")), set([p1, p2])) | ||||
self.assertEqual(set(pc.paramDefs.inCategory("bacon")), set([p2, p3])) | ||||
|
||||
def _testCategoryConsistency(self, p: parameters.Parameter): | ||||
for category in p.categories: | ||||
self.assertTrue(p.hasCategory(category)) | ||||
self.assertFalse(p.hasCategory("this_shouldnot_exist")) | ||||
|
||||
def test_parameterCollectionsHave__slots__(self): | ||||
"""Tests we prevent accidental creation of attributes.""" | ||||
self.assertEqual( | ||||
|
@@ -502,3 +511,86 @@ class MockPCChild(MockPC): | |||
pcc = MockPCChild() | ||||
with self.assertRaises(AssertionError): | ||||
pcc.whatever = 33 | ||||
|
||||
|
||||
class ParamCollectionWhere(unittest.TestCase): | ||||
"""Tests for ParameterCollection.where.""" | ||||
|
||||
class ScopeParamCollection(parameters.ParameterCollection): | ||||
pDefs = parameters.ParameterDefinitionCollection() | ||||
with pDefs.createBuilder() as pb: | ||||
pb.defParam( | ||||
name="empty", | ||||
description="Bare", | ||||
location=None, | ||||
categories=None, | ||||
units="", | ||||
) | ||||
pb.defParam( | ||||
name="keff", | ||||
description="keff", | ||||
location=parameters.ParamLocation.VOLUME_INTEGRATED, | ||||
categories=[parameters.Category.neutronics], | ||||
units="", | ||||
) | ||||
pb.defParam( | ||||
name="cornerFlux", | ||||
description="corner flux", | ||||
location=parameters.ParamLocation.CORNERS, | ||||
categories=[ | ||||
parameters.Category.neutronics, | ||||
], | ||||
units="", | ||||
) | ||||
pb.defParam( | ||||
name="edgeTemperature", | ||||
description="edge temperature", | ||||
location=parameters.ParamLocation.EDGES, | ||||
categories=[parameters.Category.thermalHydraulics], | ||||
units="", | ||||
) | ||||
|
||||
pc: typing.ClassVar[parameters.ParameterCollection] | ||||
|
||||
Comment on lines
+553
to
+554
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.
Suggested change
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. Up to you, but I believe this line is unneeded. The |
||||
@classmethod | ||||
def setUpClass(cls) -> None: | ||||
"""Define a couple useful parameters with categories, locations, etc.""" | ||||
cls.pc = cls.ScopeParamCollection() | ||||
|
||||
def test_onCategory(self): | ||||
"""Test the use of Parameter.hasCategory on filtering.""" | ||||
names = {"keff", "cornerFlux"} | ||||
for p in self.pc.where( | ||||
lambda pd: pd.hasCategory(parameters.Category.neutronics) | ||||
): | ||||
self.assertTrue(p.hasCategory(parameters.Category.neutronics), msg=p) | ||||
names.remove(p.name) | ||||
self.assertFalse(names, msg=f"{names=} should be empty!") | ||||
|
||||
def test_onLocation(self): | ||||
"""Test the use of Parameter.atLocation in filtering.""" | ||||
names = { | ||||
"edgeTemperature", | ||||
} | ||||
for p in self.pc.where( | ||||
lambda pd: pd.atLocation(parameters.ParamLocation.EDGES) | ||||
): | ||||
self.assertTrue(p.atLocation(parameters.ParamLocation.EDGES), msg=p) | ||||
names.remove(p.name) | ||||
self.assertFalse(names, msg=f"{names=} should be empty!") | ||||
|
||||
def test_complicated(self): | ||||
"""Test a multi-condition filter.""" | ||||
names = { | ||||
"cornerFlux", | ||||
} | ||||
|
||||
def check(p: parameters.Parameter) -> bool: | ||||
return p.atLocation(parameters.ParamLocation.CORNERS) and p.hasCategory( | ||||
parameters.Category.neutronics | ||||
) | ||||
|
||||
for p in self.pc.where(check): | ||||
self.assertTrue(check(p), msg=p) | ||||
names.remove(p.name) | ||||
self.assertFalse(names, msg=f"{names=} should be empty") |
This file contains 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
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.
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.