-
Notifications
You must be signed in to change notification settings - Fork 12
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
Update spatial axis
arg supported type and keys
#226
Conversation
Codecov Report
@@ Coverage Diff @@
## main #226 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 9 8 -1
Lines 742 691 -51
=========================================
- Hits 742 691 -51
Continue to review full report at Codecov.
|
xcdat/spatial.py
Outdated
raise KeyError( | ||
f"The data variable '{data_var.name}' is missing the '{axis}' " | ||
"dimension, which is required for spatial averaging." | ||
f"An '{generic_key}' axis dimension was not found in the data " | ||
f"variable. Make sure the data variable has '{generic_key}' axis " | ||
f"coordinates and its `axis` attribute is set to '{generic_key}'." | ||
) |
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.
Related to #166 (comment)
xcdat/spatial.py
Outdated
self, data_var: xr.DataArray, axis: Union[List[SpatialAxis], SpatialAxis] | ||
) -> List[SpatialAxis]: | ||
"""Validates if ``axis`` arg is supported and exists in the data var. | ||
"""Validates the ``axis`` dimension exists in the data variable. |
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.
Should this be "Validation that the axis
dimension exists..."?
xcdat/spatial.py
Outdated
|
||
Parameters | ||
---------- | ||
data_var : xr.DataArray | ||
The data variable. | ||
axis : Union[List[SpatialAxis], SpatialAxis] | ||
List of axis dimensions or single axis dimension to average over. | ||
List of axis dimensions or single axis dimensions. |
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 like how you wrote this below: "List of axis dimensions or a single axis dimension"
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 meant to use the bottom description, thanks for the catch
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.
In CDAT, you specify the axes as a single string, e.g., 'xy'. I think it would be worth including that as a method for specifying the axes. I think this could be done simply by tweaking the _validate_axis_arg
. Something like this (which could be combined with this line):
if isinstance(axis, str):
# convert string axis argument to list of strings
# representing the axes
axis = [adim.upper() for adim in axis]
What do you think? Other than that potential change this looks great!
Thanks for the review!
It is more Pythonic (and probably standard coding practice) for a function argument to accept a list of strings ( In this case, each string should be treated as an independent instance, rather than coupled as a single instance that has to be parsed. There is also less logic involved with accepting just a list of strings because type checking isn't necessary, the behavior of the API is more predictable, and it is more explicit and readable (in my opinion). Posts I found on this subject: https://stackoverflow.com/a/51737249
This is what we're currently doing for a single axis (
I do agree with this person that it is ugly to have to perform this check for every function that has an argument that accepts a string or a list of strings.
I suggest that we accept just a list of strings unless there is a strong reason otherwise. Examples of valid
Examples of invalid
|
2e51b18
to
556942b
Compare
Thanks for the detailed reply. |
a765971
to
afd6954
Compare
- Update "lat" and "lon" references to "Y" and "X" - Valid arg types are now just List[SpatialAxis] - Remove `_get_generic_axis_keys()` since it is no longer needed
afd6954
to
7fdab59
Compare
- Update exception messages and docstrings - Update conditional in TemporalAccessor.__init__
axis
keys to generic formataxis
arg supported type and keys
Description
axis
arg keys generic format #225Summary of changes
.upper()
to convert them)._get_generic_axis_keys()
since it is no longer neededSpatialAccessor.average()
docstringTemporalAccessor.__init__
check for 'T' axisTemporalAccessor.average()
docstring examplesChecklist
If applicable: