-
Notifications
You must be signed in to change notification settings - Fork 296
Add NameConstraint with relaxed name loading #3463
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
pp-mo
merged 14 commits into
SciTools:master
from
bjlittle:relax-name-loading-and-add-name-contraint
Nov 20, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad34259
Add NameConstraint with relaxed name loading
bjlittle 58f12f6
appease the god of stickler-ci
bjlittle 9d24096
add whatsnew newfeature entries
bjlittle a25e8e4
review actions
bjlittle 3a4f9c2
review actions
bjlittle d73621a
review actions
bjlittle 6d1b42b
make NameConstraint convenient
bjlittle d6b7db2
Fix NameConstraint doc-string
bjlittle b52f7c3
Add new license header to new files
bjlittle 71e7929
Names property returns namedtuple
bjlittle 7073811
fix manual typo from rebase and blackify
bjlittle 1e10bda
review actions
bjlittle c3ce008
add _coordless_match comment
bjlittle b535c77
further review actions
bjlittle 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
1 change: 1 addition & 0 deletions
1
docs/iris/src/whatsnew/contributions_3.0.0/newfeature_2019-Oct-15_nameconstraint.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
* The :class:`~iris.NameConstraint` provides richer name constraint matching when loading or extracting against cubes, by supporting a constraint against any combination of ``standard_name``, ``long_name``, NetCDF ``var_name`` and ``STASH`` from the attributes dictionary of a :class:`~iris.cube.Cube`. |
1 change: 1 addition & 0 deletions
1
docs/iris/src/whatsnew/contributions_3.0.0/newfeature_2019-Oct-15_names_property.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
* Cubes and coordinates now have a new ``names`` property that contains a tuple of the ``standard_name``, ``long_name``, NetCDF ``var_name``, and ``STASH`` attributes metadata. |
1 change: 1 addition & 0 deletions
1
docs/iris/src/whatsnew/contributions_3.0.0/newfeature_2019-Oct-15_relaxed_name_loading.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
* Name constraint matching against cubes during loading or extracting has been relaxed from strictly matching against the :meth:`~iris.cube.Cube.name`, to matching against either the ``standard_name``, ``long_name``, NetCDF ``var_name``, or ``STASH`` attributes metadata of a cube. |
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 |
---|---|---|
|
@@ -35,7 +35,8 @@ def __init__(self, name=None, cube_func=None, coord_values=None, **kwargs): | |
Args: | ||
|
||
* name: string or None | ||
If a string, it is used as the name to match against Cube.name(). | ||
If a string, it is used as the name to match against the | ||
`~iris.cube.Cube.names` property. | ||
* cube_func: callable or None | ||
If a callable, it must accept a Cube as its first and only argument | ||
and return either True or False. | ||
|
@@ -128,7 +129,9 @@ def _coordless_match(self, cube): | |
""" | ||
match = True | ||
if self._name: | ||
match = self._name == cube.name() | ||
# Require to also check against cube.name() for the fallback | ||
# "unknown" default case, when there is no name metadata available. | ||
match = self._name in cube.names or self._name == cube.name() | ||
if match and self._cube_func: | ||
match = self._cube_func(cube) | ||
return match | ||
|
@@ -466,7 +469,7 @@ def __init__(self, **attributes): | |
|
||
""" | ||
self._attributes = attributes | ||
Constraint.__init__(self, cube_func=self._cube_func) | ||
super().__init__(cube_func=self._cube_func) | ||
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. This one comes for free, whilst I'm here... |
||
|
||
def _cube_func(self, cube): | ||
match = True | ||
|
@@ -490,3 +493,103 @@ def _cube_func(self, cube): | |
|
||
def __repr__(self): | ||
return "AttributeConstraint(%r)" % self._attributes | ||
|
||
|
||
class NameConstraint(Constraint): | ||
"""Provides a simple Cube name based :class:`Constraint`.""" | ||
|
||
def __init__( | ||
self, | ||
standard_name="none", | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
long_name="none", | ||
var_name="none", | ||
STASH="none", | ||
): | ||
""" | ||
Provides a simple Cube name based :class:`Constraint`, which matches | ||
against each of the names provided, which may be either standard name, | ||
long name, NetCDF variable name and/or the STASH from the attributes | ||
dictionary. | ||
|
||
The name constraint will only succeed if *all* of the provided names | ||
match. | ||
|
||
Kwargs: | ||
* standard_name: | ||
A string or callable representing the standard name to match | ||
against. | ||
* long_name: | ||
A string or callable representing the long name to match against. | ||
* var_name: | ||
A string or callable representing the NetCDF variable name to match | ||
against. | ||
* STASH: | ||
A string or callable representing the UM STASH code to match | ||
against. | ||
|
||
.. note:: | ||
The default value of each of the keyword arguments is the string | ||
"none", rather than the singleton None, as None may be a legitimate | ||
value to be matched against e.g., to constrain against all cubes | ||
where the standard_name is not set, then use standard_name=None. | ||
|
||
Returns: | ||
* Boolean | ||
|
||
Example usage:: | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
iris.NameConstraint(long_name='air temp', var_name=None) | ||
|
||
iris.NameConstraint(long_name=lambda name: 'temp' in name) | ||
|
||
iris.NameConstraint(standard_name='air_temperature', | ||
STASH=lambda stash: stash.item == 203) | ||
|
||
""" | ||
self.standard_name = standard_name | ||
self.long_name = long_name | ||
self.var_name = var_name | ||
self.STASH = STASH | ||
self._names = ("standard_name", "long_name", "var_name", "STASH") | ||
super().__init__(cube_func=self._cube_func) | ||
|
||
def _cube_func(self, cube): | ||
def matcher(target, value): | ||
if callable(value): | ||
result = False | ||
if target is not None: | ||
# | ||
# Don't pass None through into the callable. Users should | ||
# use the "name=None" pattern instead. Otherwise, users | ||
# will need to explicitly handle the None case, which is | ||
# unnecessary and pretty darn ugly e.g., | ||
# | ||
# lambda name: name is not None and name.startswith('ick') | ||
# | ||
result = value(target) | ||
else: | ||
result = value == target | ||
return result | ||
|
||
match = True | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for name in self._names: | ||
expected = getattr(self, name) | ||
if expected != "none": | ||
if name == "STASH": | ||
actual = cube.attributes.get(name) | ||
else: | ||
actual = getattr(cube, name) | ||
match = matcher(actual, expected) | ||
# Make this is a short-circuit match. | ||
if match is False: | ||
break | ||
|
||
return match | ||
|
||
def __repr__(self): | ||
names = [] | ||
for name in self._names: | ||
value = getattr(self, name) | ||
if value != "none": | ||
names.append("{}={!r}".format(name, value)) | ||
return "{}({})".format(self.__class__.__name__, ", ".join(names)) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.