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

Add logging final version #893

Merged
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fe75f9c
add logging implementation
benmalef Jul 2, 2024
08550d6
update utils.__init__
benmalef Jul 2, 2024
ea42f95
change logging_config
benmalef Jul 2, 2024
f54641c
change logging_config
benmalef Jul 2, 2024
10dee45
change logging_config
benmalef Jul 2, 2024
e0aa707
blacked gandlf_logger
benmalef Jul 2, 2024
d5493f1
add gandlf_setup in the entrypoints
benmalef Jul 2, 2024
0d1ec65
blacked gandlf_logger
benmalef Jul 2, 2024
8ee6308
blacked some files
benmalef Jul 2, 2024
04fee16
blacked forward_pass
benmalef Jul 2, 2024
b5203cf
add logging testing
benmalef Jul 3, 2024
06c9d80
update test_full
benmalef Jul 3, 2024
b29e4ef
Merge pull request #8 from benmalef/add_test_logging
benmalef Jul 3, 2024
abddbd7
black test_full
benmalef Jul 3, 2024
c25396c
add logging test in test_full
benmalef Jul 3, 2024
bd9ba0d
remove unnecessary imports
benmalef Jul 3, 2024
57f02c9
black forward_pass
benmalef Jul 3, 2024
78af3fa
Merge pull request #9 from benmalef/add_logging_test
benmalef Jul 3, 2024
f1a3dfc
change the logging test name
benmalef Jul 3, 2024
7d6f25e
Add logging documentation (#10)
benmalef Jul 4, 2024
a2b834d
change the log format
benmalef Jul 4, 2024
fd434b2
Update docs/extending.md
benmalef Jul 8, 2024
783cbad
Update docs/extending.md
benmalef Jul 8, 2024
8736c11
Update docs/extending.md
benmalef Jul 8, 2024
cf740e8
Merge branch 'new-apis_v0.1.0-dev' into add_logging_final_version
sarthakpati Jul 12, 2024
18921a9
Merge branch 'new-apis_v0.1.0-dev' into add_logging_final_version
sarthakpati Jul 13, 2024
ba795fe
Merge branch 'new-apis_v0.1.0-dev' into add_logging_final_version
sarthakpati Jul 13, 2024
935d4a3
update gandlf_logger
benmalef Jul 15, 2024
af46472
remove unnecessary imports
benmalef Jul 15, 2024
36da4d2
update gandlf_logger_setup
benmalef Jul 18, 2024
d357fff
Update GANDLF/utils/gandlf_logger.py
benmalef Jul 18, 2024
3051b4b
Update GANDLF/utils/gandlf_logger.py
benmalef Jul 18, 2024
ca076df
change the default to create a tmp file
benmalef Jul 19, 2024
50668b9
Merge branch 'new-apis_v0.1.0-dev' into add_logging_final_version
sarthakpati Jul 19, 2024
e129be1
fix the error
benmalef Jul 19, 2024
05fa733
Update GANDLF/utils/gandlf_logger.py
benmalef Jul 19, 2024
e5bfd23
made proposed changes
benmalef Jul 19, 2024
f857e65
Update setup.py
benmalef Jul 19, 2024
ca64876
Merge branch 'new-apis_v0.1.0-dev' into add_logging_final_version
sarthakpati Jul 19, 2024
f425289
change the def name to logger_setup
benmalef Jul 20, 2024
1364c22
made some code changes
benmalef Jul 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ tutorials/classification_medmnist_notebook/output_stats
tutorials/classification_medmnist_notebook/model
tutorials/classification_medmnist_notebook/dataset/*.csv
testing/test_deploy
tmp
4 changes: 2 additions & 2 deletions GANDLF/compute/forward_pass.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pathlib
from typing import Optional, Tuple

import logging
import numpy as np
import pandas as pd
import SimpleITK as sitk
Expand Down Expand Up @@ -116,7 +116,7 @@ def validate_network(
tqdm(valid_dataloader, desc="Looping over " + mode + " data")
):
if params["verbose"]:
print("== Current subject:", subject["subject_id"], flush=True)
logging.debug(f'== Current subject: {subject["subject_id"]}')

# ensure spacing is always present in params and is always subject-specific
params["subject_spacing"] = None
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from GANDLF.anonymize import run_anonymizer
from GANDLF.cli import copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _anonymize_images(
Expand Down Expand Up @@ -77,6 +78,7 @@ def new_way(input_dir, config, modality, output_file):
+ "`gandlf_anonymizer` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_Anonymize",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
5 changes: 3 additions & 2 deletions GANDLF/entrypoints/cli_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click
from .subcommands import cli_subcommands
from GANDLF.entrypoints import append_copyright_to_help

from GANDLF.utils import gandlf_logger_setup
from GANDLF import version


Expand All @@ -24,7 +24,8 @@ def gandlf(ctx, loglevel):
"""GANDLF command-line tool."""
ctx.ensure_object(dict)
ctx.obj["LOGLEVEL"] = loglevel
setup_logging(loglevel)
# setup_logging(loglevel)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove redundant old loglevel stuff here

gandlf_logger_setup()


# registers subcommands: `gandlf anonymizer`, `gandlf run`, etc.
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/collect_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from GANDLF.cli import copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils import gandlf_logger_setup


def plot_all(df_training, df_validation, df_testing, output_plot_dir):
Expand Down Expand Up @@ -205,6 +206,7 @@ def new_way(model_dir: str, output_dir: str):
+ "`gandlf_collectStats` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_CollectStats",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from GANDLF.cli import config_generator, copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _generate_config(config: str, strategy: str, output: str):
Expand Down Expand Up @@ -46,6 +47,7 @@ def new_way(config, strategy, output):
+ "`gandlf_configGenerator` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_ConfigGenerator",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/construct_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from GANDLF.utils import writeTrainingCSV

from GANDLF.cli import copyrightMessage
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _construct_csv(
Expand Down Expand Up @@ -119,6 +120,7 @@ def new_way(
+ "`gandlf_constructCSV` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_ConstructCSV",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/debug_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from GANDLF import __version__
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils import get_git_hash
from GANDLF.utils import gandlf_logger_setup


def _debug_info():
Expand Down Expand Up @@ -38,6 +39,7 @@ def new_way():
)
def old_way():
_debug_info()
gandlf_logger_setup()


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
copyrightMessage,
)
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _deploy(
Expand Down Expand Up @@ -157,6 +158,7 @@ def new_way(
+ "`gandlf_deploy` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_Deploy",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/generate_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from GANDLF.cli import copyrightMessage
from GANDLF.cli.generate_metrics import generate_metrics_dict
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _generate_metrics(
Expand Down Expand Up @@ -82,6 +83,7 @@ def new_way(
+ "`gandlf_generateMetrics` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_Metrics",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/optimize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from GANDLF.cli import copyrightMessage, post_training_model_optimization
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _optimize_model(
Expand Down Expand Up @@ -60,6 +61,7 @@ def new_way(
+ "`gandlf_optimizeModel` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_OptimizeModel",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/patch_miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from GANDLF.cli.patch_extraction import patch_extraction
from GANDLF.cli import copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _mine_patches(input_path: str, output_dir: str, config: Optional[str]):
Expand Down Expand Up @@ -55,6 +56,7 @@ def new_way(input_csv: str, output_dir: str, config: Optional[str]):
+ "`gandlf_patchMiner` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_PatchMiner",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from deprecated import deprecated
from GANDLF.cli import preprocess_and_save, copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _preprocess(
Expand Down Expand Up @@ -110,6 +111,7 @@ def new_way(
+ "`gandlf_preprocess` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_Preprocess",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/recover_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from GANDLF.cli import copyrightMessage, recover_config
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _recover_config(model_dir: Optional[str], mlcube: bool, output_file: str):
Expand Down Expand Up @@ -63,6 +64,7 @@ def new_way(model_dir, mlcube, output_file):
+ "`gandlf_recoverConfig` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_RecoverConfig",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from GANDLF import version
from GANDLF.cli import main_run, copyrightMessage
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _run(
Expand Down Expand Up @@ -184,6 +185,7 @@ def new_way(
+ "`gandlf_run` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/split_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from GANDLF.cli import copyrightMessage, split_data_and_save_csvs
from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _split_csv(input_csv: str, output_dir: str, config_path: Optional[str]):
Expand Down Expand Up @@ -64,6 +65,7 @@ def new_way(input_csv: str, output_dir: str, config: Optional[str]):
+ "`gandlf_splitCSV` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
parser = argparse.ArgumentParser(
prog="GANDLF_SplitCSV",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
2 changes: 2 additions & 0 deletions GANDLF/entrypoints/verify_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from deprecated import deprecated

from GANDLF.entrypoints import append_copyright_to_help
from GANDLF.utils.gandlf_logger import gandlf_logger_setup


def _verify_install():
Expand Down Expand Up @@ -42,6 +43,7 @@ def new_way():
+ "`gandlf_verifyInstall` script would be deprecated soon."
)
def old_way():
gandlf_logger_setup()
argparse.ArgumentParser(
prog="GANDLF_VerifyInstall",
formatter_class=argparse.RawTextHelpFormatter,
Expand Down
47 changes: 47 additions & 0 deletions GANDLF/logging_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: 1
formatters:
detailed:
format: "%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(funcName)s:%(lineno)d - %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
log_colors:
DEBUG: "white"
INFO: "green"
WARNING: "yellow"
ERROR: "red"
CRITICAL: "bold_red"
simple:
(): colorlog.ColoredFormatter
format: "%(log_color)s%(asctime)s - %(levelname)s - %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
filters:
warnings_filter:
(): logging.Filter
name: "py.warnings"
info_only_filter:
(): GANDLF.utils.gandlf_logger.InfoOnlyFilter
handlers:
stdoutHandler: # only display info level
class: logging.StreamHandler
level: INFO
formatter: simple
filters: [info_only_filter]
stream: ext://sys.stdout
stderrHandler: # display warning and above messages
class: logging.StreamHandler
level: WARNING
formatter: detailed
stream: ext://sys.stderr
rotatingFileHandler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: detailed
maxBytes: 10485760
benmalef marked this conversation as resolved.
Show resolved Hide resolved
backupCount: 2
loggers: # you can add your customized logger
debug_logger:
level: DEBUG
handlers: [stdoutHandler, rotatingFileHandler, stderrHandler]
propagate: no
root:
level: DEBUG
handlers: [stdoutHandler, rotatingFileHandler, stderrHandler]
1 change: 1 addition & 0 deletions GANDLF/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@
)

from .data_splitter import split_data
from .gandlf_logger import gandlf_logger_setup, InfoOnlyFilter
75 changes: 75 additions & 0 deletions GANDLF/utils/gandlf_logger.py
sarthakpati marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import logging
import yaml
from pathlib import Path
from importlib import resources
import colorlog


def _flash_to_console():
benmalef marked this conversation as resolved.
Show resolved Hide resolved
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
log_colors={
"DEBUG": "blue",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
},
)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logging.root.setLevel(logging.DEBUG)
logging.root.addHandler(console_handler)


def _create_log_file(log_file):
log_file = Path(log_file)
log_file.write_text("Starting GaNDLF logging session \n")


def _save_logs_in_file(log_file, config_path):
_create_log_file(log_file)
with resources.open_text("GANDLF", config_path) as file:
config_dict = yaml.safe_load(file)
config_dict["handlers"]["rotatingFileHandler"]["filename"] = str(log_file)
logging.config.dictConfig(config_dict)


def gandlf_logger_setup(log_file=None, config_path="logging_config.yaml"):
benmalef marked this conversation as resolved.
Show resolved Hide resolved
"""
It sets up the logger. Reads from logging_config.

Args:
log_file (str): dir path for saving the logs, defaults to `None`, at which time logs are flushed to console.
config_path (str): file path for the configuration

"""

logging.captureWarnings(True)

if log_file is None: # flash logs
_flash_to_console()
benmalef marked this conversation as resolved.
Show resolved Hide resolved

else: # create the log file
try:
_save_logs_in_file(log_file, config_path)
except Exception as e:
logging.error(f"log_file:{e}")
logging.warning("The logs will be flushed to console")


class InfoOnlyFilter(logging.Filter):
"""
Display only INFO messages.
"""

def filter(self, record):
"""
Determines if the specified record is to be logged.
Args:
record (logging.LogRecord): The log record to be evaluated.
Returns:
bool: True if the log record should be processed, False otherwise.
"""
return record.levelno == logging.INFO
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ exclude *.toml
include setup.py
include .dockerignore
include Dockerfile-*
include logging_config.yml
Loading
Loading