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

Modify _log_input_summary function #7811

Open
wants to merge 20 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bf9db2c
Modify _log_input_summary function
hyacinth97223 May 27, 2024
7f2633e
Modify "_log_input_summary" function
hyacinth97223 May 27, 2024
aa3d6ac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2024
bacd791
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Jun 6, 2024
0c7857a
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Sep 12, 2024
60b9947
modify _log_input_summary function.
hyacinth97223 Sep 12, 2024
4fe7d52
Merge branch 'fix-issue-7513' of https://github.com/hyacinth97223/MON…
hyacinth97223 Sep 12, 2024
1fda340
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 12, 2024
93ad7a5
fix: auto-fix imports in scripts.py
hyacinth97223 Sep 12, 2024
930ce2c
fix: auto-fix imports in scripts.py
hyacinth97223 Sep 12, 2024
c55e9c7
fix: auto-fix imports in scripts.py
hyacinth97223 Sep 12, 2024
d914f4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 12, 2024
dec4b0c
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Sep 18, 2024
e7fcd41
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Sep 18, 2024
9440c80
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Sep 19, 2024
43ca23d
Fix log_input_summary function
hyacinth97223 Sep 19, 2024
7eb7b79
Merge branch 'fix-issue-7513' of https://github.com/hyacinth97223/MON…
hyacinth97223 Sep 19, 2024
a9507ad
Merge branch 'Project-MONAI:dev' into fix-issue-7513
hyacinth97223 Sep 23, 2024
69001d7
Fix log_input_summary
hyacinth97223 Sep 23, 2024
a4ae330
Merge branch 'fix-issue-7513' of https://github.com/hyacinth97223/MON…
hyacinth97223 Sep 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
63 changes: 57 additions & 6 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@
from __future__ import annotations

import ast
import configparser
import json
import logging.config
import os
import re
import threading
import warnings
import zipfile
from collections.abc import Mapping, Sequence
from datetime import datetime
from functools import partial
from logging import LoggerAdapter
from pathlib import Path
from pydoc import locate
from shutil import copyfile
from textwrap import dedent
from typing import Any, Callable

import torch
import torch.distributed as dist
from torch.cuda import is_available

from monai._version import get_versions
Expand All @@ -51,7 +57,6 @@
get_equivalent_dtype,
min_version,
optional_import,
pprint_edges,
)

validate, _ = optional_import("jsonschema", name="validate")
Expand Down Expand Up @@ -120,11 +125,57 @@ def _pop_args(src: dict, *args: Any, **kwargs: Any) -> tuple:
return tuple([src.pop(i) for i in args] + [src.pop(k, v) for k, v in kwargs.items()])


def _log_input_summary(tag: str, args: dict) -> None:
logger.info(f"--- input summary of monai.bundle.scripts.{tag} ---")
for name, val in args.items():
logger.info(f"> {name}: {pprint_edges(val, PPRINT_CONFIG_N)}")
logger.info("---\n\n")
def _log_input_summary(tag: str, args: dict, log_all_ranks: bool = False) -> None:
is_distributed = dist.is_available() and dist.is_initialized()
rank = dist.get_rank() if is_distributed else 0

if not log_all_ranks and rank != 0:
return

log_lock = threading.Lock()

with log_lock:
config_file = "logging.conf"
config = configparser.ConfigParser()
config.read(config_file)

if config.has_option("handler_fileHandler", "args"):
base_log_dir = config.get("handler_fileHandler", "args").strip("()").split(",")[0].strip().strip("'")
else:
base_log_dir = "logs/"

if not os.path.exists(base_log_dir):
os.makedirs(base_log_dir)

log_file_path = os.path.join(base_log_dir, f"rank_{rank}_logfile.log")
logger = logging.getLogger(f"rank_{rank}_logger")
logger.setLevel(logging.INFO)

if not logger.hasHandlers():
file_handler = logging.FileHandler(log_file_path, mode="a")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)

logger: LoggerAdapter = logging.LoggerAdapter(logger, {"rank": rank})

formatted_args = {
name: ", ".join(map(str, val)) if isinstance(val, (list, tuple, dict)) else val
for name, val in args.items()
}

logger.info(f"--- Input summary of monai.bundle.scripts.{tag} ---")

for name, formatted_val in formatted_args.items():
logger.info(f"> {name}: {formatted_val}")

logger.info("---\n\n")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
logger.info(f"Log written at {timestamp}")


def _get_var_names(expr: str) -> list[str]:
Expand Down
Loading