Skip to content

Commit

Permalink
Update mlflow logger to latest aifs-mono version
Browse files Browse the repository at this point in the history
  • Loading branch information
gmertes committed Jul 2, 2024
1 parent f7fb94a commit f4b781c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/anemoi/training/diagnostics/mlflow/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


import json
import logging
import os
Expand Down
14 changes: 9 additions & 5 deletions src/anemoi/training/diagnostics/mlflow/logger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# (C) Copyright 2024 ECMWF.
#
# (C) Copyright 2024 European Centre for Medium-Range Weather Forecasts.
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

import io
import logging
import os
import re
import sys
Expand All @@ -26,7 +24,9 @@
from pytorch_lightning.loggers.mlflow import _flatten_dict
from pytorch_lightning.utilities.rank_zero import rank_zero_only

LOGGER = logging.getLogger(__name__)
from anemoi.training.utils.logger import get_code_logger

LOGGER = get_code_logger(__name__)


def get_mlflow_run_params(config, tracking_uri):
Expand Down Expand Up @@ -213,14 +213,18 @@ def _handle_csi(line):
# removes the cursor up and down symbols from the line
# skip tqdm status bar updates ending with "curser up" but the last one in buffer to save space
def _remove_csi(line):
# replacing the leftmost non-overlapping occurrences of pattern ansi_csi_re in string line by the replacement ""
return re.sub(ansi_csi_re, b"", line)

for match in ansi_csi_re.finditer(line):
arg, command = match.groups()
arg = int(arg.decode()) if arg else 1
if command == b"A" and (b"0%" not in line and not self._shutdown): # cursor up
if command == b"A" and (
b"0%" not in line and not self._shutdown and b"[INFO]" not in line and b"[DEBUG]" not in line
): # cursor up
# only keep x*10% status updates from tqmd status bars that end with a cursor up
# always keep shutdown commands
# always keep logger info and debug prints
line = b""
return _remove_csi(line)

Expand Down
Empty file.
48 changes: 48 additions & 0 deletions src/anemoi/training/utils/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# (C) Copyright 2024 European Centre for Medium-Range Weather Forecasts.
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

import logging
import sys
import time


def get_code_logger(name: str, debug: bool = True) -> logging.Logger:
"""Returns a logger with a custom level and format.
We use ISO8601 timestamps and UTC times.
Parameters
----------
name : str
Name of logger object
debug : bool, optional
set logging level to logging.DEBUG; else set to logging.INFO, by default True
Returns
-------
logging.Logger
Logger object
"""
# create logger object
logger = logging.getLogger(name=name)
if not logger.hasHandlers():
# logging level
level = logging.DEBUG if debug else logging.INFO
# logging format
datefmt = "%Y-%m-%dT%H:%M:%SZ"
msgfmt = "[%(asctime)s] [%(filename)s:%(lineno)s - %(funcName).30s] [%(levelname)s] %(message)s"
# handler object
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(level)
formatter = logging.Formatter(msgfmt, datefmt=datefmt)
# record UTC time
formatter.converter = time.gmtime
handler.setFormatter(formatter)
logger.setLevel(level)
logger.addHandler(handler)

return logger

0 comments on commit f4b781c

Please sign in to comment.