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

Profiling: subparameter parser support #353

Merged
merged 1 commit into from
May 30, 2023

Conversation

marshallward
Copy link
Member

The very crude MOM_input parser in the automatic profiler did not support subparameters (e.g. MLE% ... %MLE), which caused an error when trying to read the FMS clock output.

This patch adds the support, or at least enough support to avoid errors.

@codecov
Copy link

codecov bot commented Apr 21, 2023

Codecov Report

Merging #353 (506f596) into dev/gfdl (501fcff) will decrease coverage by 0.01%.
The diff coverage is n/a.

❗ Current head 506f596 differs from pull request most recent head 7f8c6bd. Consider uploading reports for the commit 7f8c6bd to get more accurate results

@@             Coverage Diff              @@
##           dev/gfdl     #353      +/-   ##
============================================
- Coverage     38.13%   38.13%   -0.01%     
============================================
  Files           269      269              
  Lines         75743    75743              
  Branches      13926    13926              
============================================
- Hits          28884    28882       -2     
- Misses        41660    41662       +2     
  Partials       5199     5199              

see 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Hallberg-NOAA
Copy link
Member

This is a relatively simple PR, but it should be reviewed by someone who is proficient in Python. @raphaeldussin or @adcroft, would one of you be willing to take a quick look at it, please, to see if it makes sense to you. This PR has to be handled before we can deal with PR #352.

@raphaeldussin raphaeldussin self-assigned this May 26, 2023
Copy link

@raphaeldussin raphaeldussin left a comment

Choose a reason for hiding this comment

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

I think the function would benefit from a docstring and some comments explaining the logic

@@ -60,18 +60,34 @@ def main():
print(json.dumps(config))


def parse_mom6_param(param_file):
def parse_mom6_param(param_file, header=None):
params = {}
for line in param_file:
param_stmt = line.split('!')[0].strip()

Choose a reason for hiding this comment

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

param_stmt is a string used as a bool in the logic, how about if line.find("!") == 0

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for looking over this, @raphaeldussin. My Python may be a bit old fashioned. Is the implicit bool(string) operation frowned upon these days? (I tend to use if var too often, though I had thought this one was safe.)

Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW, the PEP8 style guide suggests using if not str: to check if a string is empty:

https://peps.python.org/pep-0008/#programming-recommendations

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

# Correct:
if not seq:
if seq:

# Wrong:
if len(seq):
if not len(seq):


# Skip blank lines
if not param_stmt:
continue

Choose a reason for hiding this comment

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

continue -> pass

Copy link
Member Author

@marshallward marshallward May 29, 2023

Choose a reason for hiding this comment

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

pass is a null operation, whereas continue terminates the loop iteration and begins the next one. Using pass would leave value undefined (or worse, defined from the previous loop) and have unexpected effects.


# Subparameters
if param_stmt[-1] == '%':
key = param_stmt.split('%')[-2]

Choose a reason for hiding this comment

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

now param_stmt is back to being used as a string

subheader = key
if header:
subheader = header + '%' + subheader
value = parse_mom6_param(param_file, header=subheader)

Choose a reason for hiding this comment

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

recursive call

Copy link
Member Author

Choose a reason for hiding this comment

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

The function is intentionally recursive, in order to parse the nested p1%p2%p3% ... %p3%p2%p1 blocks. Is there a better solution here?

@marshallward marshallward force-pushed the prof_subparam_parser branch from 297994d to 516c318 Compare May 29, 2023 20:54
@marshallward
Copy link
Member Author

I think the function would benefit from a docstring and some comments explaining the logic

@raphaeldussin Thanks for the suggestions, I've added docstrings and additional comments to explain the recursive behavior of the function. I've also responded to the inline comments.

The very crude MOM_input parser in the automatic profiler did not
support subparameters (e.g. MLE% ... %MLE), which caused an error when
trying to read the FMS clock output.

This patch adds the support, or at least enough support to avoid errors.
@Hallberg-NOAA Hallberg-NOAA force-pushed the prof_subparam_parser branch from 516c318 to 7f8c6bd Compare May 29, 2023 21:26
Copy link
Member

@Hallberg-NOAA Hallberg-NOAA left a comment

Choose a reason for hiding this comment

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

Thank you for the productive conversation surrounding this PR. It has passed pipeline testing at https://gitlab.gfdl.noaa.gov/ogrp/MOM6/-/pipelines/19324.

@Hallberg-NOAA Hallberg-NOAA merged commit b32b2ed into NOAA-GFDL:dev/gfdl May 30, 2023
@marshallward marshallward deleted the prof_subparam_parser branch July 21, 2023 14:40
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.

3 participants