Skip to content

Commit

Permalink
[FIX] Fix logging in adni to bids (#1181)
Browse files Browse the repository at this point in the history
* refactor slightly the logging configuration logic

* re-configure the clinica logger in child processes (not optimal but better)

* Update clinica/iotools/converters/adni_to_bids/adni_utils.py

Co-authored-by: AliceJoubert <[email protected]>

---------

Co-authored-by: AliceJoubert <[email protected]>
  • Loading branch information
NicolasGensollen and AliceJoubert authored May 17, 2024
1 parent 0871d35 commit 7f7a0b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
42 changes: 26 additions & 16 deletions clinica/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,24 @@ def setup_logging(verbose: bool = False) -> None:
(0 (default): WARNING, 1: INFO, 2: DEBUG)
"""
import logging
import sys
from logging.handlers import RotatingFileHandler as RFHandler

import nipype
from colorlog import ColoredFormatter, StreamHandler

logging_level = "DEBUG" if verbose else "INFO"

# Root logger configuration.
root_logger = logging.getLogger()
root_logger.setLevel(logging_level)

# Clinica logger configuration.
clinica_logger = logging.getLogger("clinica")
clinica_logger.setLevel(logging_level)
console_handler = StreamHandler(stream=sys.stdout)
console_handler.setFormatter(
ColoredFormatter("%(log_color)s%(asctime)s:%(levelname)s:%(message)s")
)
clinica_logger.addHandler(console_handler)

setup_clinica_logging(logging_level)
# Nipype logger configuration.
# Monkey-patch nipype to use Python's RFH logger.
setup_nipype_logging()


def setup_nipype_logging():
"""Monkey-patch nipype to use Python's RFH logger."""
import logging
from logging.handlers import RotatingFileHandler as RFHandler

import nipype

nipype.utils.logger.RFHandler = RFHandler
# Setup debug logging to file.
nipype.config.enable_debug_mode()
Expand All @@ -74,6 +69,21 @@ def setup_logging(verbose: bool = False) -> None:
nipype_logger.addHandler(logging.NullHandler())


def setup_clinica_logging(logging_level: str):
import logging
import sys

from colorlog import ColoredFormatter, StreamHandler

clinica_logger = logging.getLogger("clinica")
clinica_logger.setLevel(logging_level)
console_handler = StreamHandler(stream=sys.stdout)
console_handler.setFormatter(
ColoredFormatter("%(log_color)s%(asctime)s:%(levelname)s:%(message)s")
)
clinica_logger.addHandler(console_handler)


@click.group(context_settings=CONTEXT_SETTINGS)
@click.version_option(version=clinica.__version__)
@click.option("-v", "--verbose", is_flag=True, help="Increase logging verbosity.")
Expand Down
8 changes: 8 additions & 0 deletions clinica/iotools/converters/adni_to_bids/adni_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,12 +1344,20 @@ def create_file(

import numpy as np

from clinica.cmdline import setup_clinica_logging
from clinica.iotools.bids_utils import run_dcm2niix
from clinica.iotools.converter_utils import viscode_to_session
from clinica.iotools.utils.data_handling import center_nifti_origin
from clinica.utils.pet import ReconstructionMethod, Tracer
from clinica.utils.stream import cprint

# This function is executed in a multiprocessing context
# such that we need to re-configure the clinica logger in the child processes.
# Note that logging messages could easily be lost (for example when logging
# to a file from two different processes). A better solution would be to
# implement a logging process consuming logging messages from a multiprocessing.Queue...
setup_clinica_logging("INFO")

modality_specific = {
"t1": {
"output_path": "anat",
Expand Down

0 comments on commit 7f7a0b4

Please sign in to comment.