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

103 we should be using the logging module #224

Merged
merged 19 commits into from
Feb 15, 2024

Conversation

bschroeter
Copy link
Collaborator

Added changes from yesterday's discussion. Also updated the CLI interface to engage the log level from the verbosity flag.

@bschroeter bschroeter linked an issue Dec 20, 2023 that may be closed by this pull request
Copy link

codecov bot commented Dec 20, 2023

Codecov Report

Attention: 72 lines in your changes are missing coverage. Please review.

Comparison is base (7c8fa5a) 59.61% compared to head (b65ddbb) 60.76%.
Report is 2 commits behind head on main.

Files Patch % Lines
benchcab/benchcab.py 22.85% 54 Missing ⚠️
benchcab/fluxsite.py 74.07% 7 Missing ⚠️
benchcab/utils/repo.py 40.00% 6 Missing ⚠️
benchcab/main.py 25.00% 3 Missing ⚠️
benchcab/comparison.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #224      +/-   ##
==========================================
+ Coverage   59.61%   60.76%   +1.15%     
==========================================
  Files          30       30              
  Lines        2199     2238      +39     
==========================================
+ Hits         1311     1360      +49     
+ Misses        888      878      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ccarouge ccarouge left a comment

Choose a reason for hiding this comment

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

Just a small change on how the log_level is set. I would do it directly within the argument parser instead of during parse_and_dispatch.

benchcab/main.py Outdated
Comment on lines 21 to 22
# Intercept the verbosity flag to engage the logger
log_level = 'debug' if args.get('verbose', False) == True else 'info'
Copy link
Member

Choose a reason for hiding this comment

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

This could be handled in the parser definition directly:

    args_subcommand.add_argument(
        "-v",
        "--verbose",
        help="Enable more detailed output in the command line.",
        action="store_const",
        default="info",
        const="debug",
        dest="log_level",
    )

or something like that because I haven't tried it out... I believe action isn't necessary but it doesn't hurt either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See, this was my first thought, but then I saw that the verbose argument is passed around 150+ times throughout the code so there is a bit of a refactoring exercise involved to ensure things still work. This suggestion is probably related to, but a bit outside the scope of this ticket.

Should we raise this as a different Issue and PR to address this?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we can put it in a different issue.

benchcab/main.py Outdated
Comment on lines 24 to 25
# We just need to instantiate this with the desired level
logger = get_logger(level=log_level)
Copy link
Member

Choose a reason for hiding this comment

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

Considering the suggestion above this would then need to change to:

Suggested change
# We just need to instantiate this with the desired level
logger = get_logger(level=log_level)
# We just need to instantiate this with the desired level
logger = get_logger(level=args.log_level)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See comment above :)

Copy link
Collaborator

@SeanBryan51 SeanBryan51 left a comment

Choose a reason for hiding this comment

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

Looks good. Just a few suggestions:

  • Can you run black on your changes?
  • Can you run ruff on your changes and fix any small errors like unused imports etc?
  • Now that we have a logger, can you remove all occurrences of the verbose argument?

@SeanBryan51
Copy link
Collaborator

Also, I realise the integration test will fail due to #223. I will fix this today so that we have working integration tests.

Comment on lines 1 to 3
# Copyright 2022 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details.
# SPDX-License-Identifier: Apache-2.0

Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't remove the copyright notice from this file. Can we reinstate please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure thing, I think it was when I was trying to get ruff to work, for some reason it didn't like the double "#" comments. Typically a triple quote is used at the top of Python files but I will see how it goes.

Copy link
Member

@ccarouge ccarouge left a comment

Choose a reason for hiding this comment

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

Actually, I think this is ready. We will have to open an issue about the way log_level is set.
I've added a comment yesterday about a deleted copyright statement in a file that needs to be added. Please make sure to add that statement back first and then this can be merged.

@bschroeter
Copy link
Collaborator Author

@SeanBryan51, OK. Massive update with merges and reversion to the native logger class. We had to do this because there is some python magic behind the scenes that will enable the native logger to pickle, whereas subclassing the native logger loses this ability. Many hours chasing that down.

This means that we lose the ability for multi-line logging in a single call (which also means we retain the code location information in the log), and we have to call an explicit log call for each line, which is technically the proper practice and will enable us to later use a log-scanning framework to extract events from log lines.

I've updated from main, merged, applied a bunch of changes suggested from ruff and black and ran the integration and pytest suites through to success. I think we're good to go on this.

We should look at adding black and ruff to the GitHub actions in another PR, if we haven't already.

Let me know if you have any questions.

@ccarouge
Copy link
Member

ccarouge commented Feb 8, 2024

@bschroeter There is a minor conflict now with main since the merge of #232. Since Sean is away, I'll try and review.

Copy link
Member

@ccarouge ccarouge left a comment

Choose a reason for hiding this comment

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

I've spotted a few errors while resolving merge conflicts. But otherwise only minor typos.

If you don't mind resubmitting for review once this is solved, I want to make sure I'm not missing anything else.

Comment on lines 151 to 173
pbs_config=config["fluxsite"]["pbs"],
verbose=verbose,
Copy link
Member

Choose a reason for hiding this comment

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

We want to keep the pbs_config argument here! That comes from #238

Comment on lines 204 to 210
# TODO(Sean) we should archive revision numbers for CABLE-AUX
cable_aux_repo = SVNRepo(
svn_root=internal.CABLE_SVN_ROOT,
branch_path=internal.CABLE_AUX_RELATIVE_SVN_PATH,
path=internal.SRC_DIR / "CABLE-AUX",
)
cable_aux_repo.checkout(verbose=verbose)
Copy link
Member

Choose a reason for hiding this comment

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

We removed this in #230

Comment on lines 258 to 270
print("Running fluxsite tasks...")
if config["fluxsite"]["multiprocess"]:
ncpus = config["fluxsite"]["pbs"]["ncpus"]
try:
multiprocess = config["fluxsite"]["multiprocess"]
except KeyError:
multiprocess = internal.FLUXSITE_DEFAULT_MULTIPROCESS
if multiprocess:
ncpus = config.get("pbs", {}).get(
"ncpus", internal.FLUXSITE_DEFAULT_PBS["ncpus"]
)
run_tasks_in_parallel(tasks, n_processes=ncpus, verbose=verbose)
else:
run_tasks(tasks, verbose=verbose)
print("Successfully ran fluxsite tasks")
print("")
run_tasks(tasks)
self.logger.info("Successfully ran fluxsite tasks")
Copy link
Member

Choose a reason for hiding this comment

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

We want the version in main here but I believe the print at the top should call the logger.

Comment on lines 285 to 294
print("Running comparison tasks...")
if config["fluxsite"]["multiprocess"]:
ncpus = config["fluxsite"]["pbs"]["ncpus"]
try:
multiprocess = config["fluxsite"]["multiprocess"]
except KeyError:
multiprocess = internal.FLUXSITE_DEFAULT_MULTIPROCESS
if multiprocess:
try:
ncpus = config["fluxsite"]["pbs"]["ncpus"]
except KeyError:
ncpus = internal.FLUXSITE_DEFAULT_PBS["ncpus"]
Copy link
Member

Choose a reason for hiding this comment

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

We want the version in main. And we want to call self.logger.info instead of print on the first line.

model : Model
Model.
met_forcing_file : str
Met forcinf file.
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
Met forcinf file.
Met forcing file.

Parameters
----------
repo : Repo
Respository.
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
Respository.
Repository.

Copy link
Member

Choose a reason for hiding this comment

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

Not used, remove?

from importlib import resources
from pathlib import Path

import yaml

# from benchcab.utils.singleton_logger import SingletonLogger
Copy link
Member

Choose a reason for hiding this comment

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

Remove instead of commented?

@bschroeter
Copy link
Collaborator Author

I've spotted a few errors while resolving merge conflicts. But otherwise only minor typos.

If you don't mind resubmitting for review once this is solved, I want to make sure I'm not missing anything else.

Thanks Claire - looks like the merge from main didn't pick up some of the recent changes. I'm working through these and getting an update PR ready for you.

@bschroeter bschroeter requested a review from ccarouge February 13, 2024 03:09
@bschroeter
Copy link
Collaborator Author

OK @ccarouge, I've gone back through everything and manually applied all of the changes that got clobbered over the last week. I think it should all be ok now...

I've run all tests and integration and it seems to pass.

Coverage drops, but otherwise I think this is ready?

Copy link
Member

@ccarouge ccarouge left a comment

Choose a reason for hiding this comment

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

@bschroeter There is one more clobbered conflict resolution. I think I have put the right code in the suggestion. Can you try it out before I give approval?

Comment on lines 283 to 292
self.logger.debug("Running comparison tasks...")
try:
multiprocess = config["fluxsite"]["multiprocess"]
except KeyError:
multiprocess = internal.FLUXSITE_DEFAULT_MULTIPROCESS
if multiprocess:
try:
ncpus = config["fluxsite"]["pbs"]["ncpus"]
except KeyError:
ncpus = internal.FLUXSITE_DEFAULT_PBS["ncpus"]
Copy link
Member

Choose a reason for hiding this comment

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

Old version. We want:

Suggested change
self.logger.debug("Running comparison tasks...")
try:
multiprocess = config["fluxsite"]["multiprocess"]
except KeyError:
multiprocess = internal.FLUXSITE_DEFAULT_MULTIPROCESS
if multiprocess:
try:
ncpus = config["fluxsite"]["pbs"]["ncpus"]
except KeyError:
ncpus = internal.FLUXSITE_DEFAULT_PBS["ncpus"]
self.logger.debug("Running comparison tasks...")
if config["fluxsite"]["multiprocess"]:
ncpus = config["fluxsite"]["pbs"]["ncpus"]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @ccarouge, I've committed the fix after running all tests again. Passing.

One thing to note, many of these remaining changes did not trigger failures in the tests. We may need to take a look at how robust our testing is to catch these in future.

@bschroeter bschroeter requested a review from ccarouge February 15, 2024 00:06
Copy link
Member

@ccarouge ccarouge left a comment

Choose a reason for hiding this comment

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

Finally! Last small changes done:

  • clobbered merge conflicts
  • removed unused singleton.py

@bschroeter bschroeter removed the request for review from SeanBryan51 February 15, 2024 05:02
@ccarouge ccarouge merged commit 3b40cd4 into main Feb 15, 2024
5 checks passed
@ccarouge ccarouge deleted the 103-we-should-be-using-the-logging-module branch February 15, 2024 05:03
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.

We should be using the logging module
4 participants