[ENH]: Consistency of logging #239

Merged
synchon merged 20 commits from update/logging-consistency into main 2026-06-15 09:02:42 +00:00
80 changed files with 284 additions and 100 deletions

View file

@ -0,0 +1 @@
Improve logging consistency of components by `Synchon Mandal`_

View file

@ -23,6 +23,7 @@ DataGrabbers, Preprocessors, Markers, etc., following the *junifer* way.
:caption: Contents: :caption: Contents:
extension extension
logging
datagrabber datagrabber
marker marker
preprocessor preprocessor

View file

@ -0,0 +1,38 @@
.. include:: ../links.inc
.. _logging_in_extension:
Logging in a ``junifer`` extension
==================================
It is important to have proper logging in your code and ``junifer`` makes it trivial to do so for your extensions.
For DataGrabber:
.. code-block:: python
from junifer.datagrabber import logger
For Preprocessor:
.. code-block:: python
from junifer.preprocess import logger
For Marker:
.. code-block:: python
from junifer.marker import logger
For Storage:
.. code-block:: python
from junifer.storage import logger
Importing the correct logger will give you a properly set up logger which will fit right in with the rest of ``junifer``\'s log output.

View file

@ -10,6 +10,8 @@ import os
import shutil import shutil
from pathlib import Path from pathlib import Path
import structlog
from ..api.queue_context import GnuParallelLocalAdapter, HTCondorAdapter from ..api.queue_context import GnuParallelLocalAdapter, HTCondorAdapter
from ..datagrabber import BaseDataGrabber from ..datagrabber import BaseDataGrabber
from ..markers import BaseMarker from ..markers import BaseMarker
@ -27,11 +29,14 @@ from ..typing import (
PreprocessorLike, PreprocessorLike,
StorageLike, StorageLike,
) )
from ..utils import logger, raise_error, warn_with_log, yaml from ..utils import raise_error, warn_with_log, yaml
__all__ = ["collect", "list_elements", "queue", "reset", "run"] __all__ = ["collect", "list_elements", "queue", "reset", "run"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="api")
def _get_datagrabber(datagrabber_config: dict) -> DataGrabberLike: def _get_datagrabber(datagrabber_config: dict) -> DataGrabberLike:
"""Get DataGrabber. """Get DataGrabber.

View file

@ -9,12 +9,13 @@ from pathlib import Path
from typing import Any from typing import Any
from ...typing import Elements from ...typing import Elements
from ...utils import logger, make_executable, raise_error, run_ext_cmd from ...utils import make_executable, raise_error, run_ext_cmd
from .queue_context_adapter import ( from .queue_context_adapter import (
EnvKind, EnvKind,
EnvShell, EnvShell,
QueueContextAdapter, QueueContextAdapter,
QueueContextEnv, QueueContextEnv,
logger,
) )

View file

@ -10,12 +10,13 @@ from pathlib import Path
from typing import Any from typing import Any
from ...typing import Elements from ...typing import Elements
from ...utils import logger, make_executable, raise_error, run_ext_cmd from ...utils import make_executable, raise_error, run_ext_cmd
from .queue_context_adapter import ( from .queue_context_adapter import (
EnvKind, EnvKind,
EnvShell, EnvShell,
QueueContextAdapter, QueueContextAdapter,
QueueContextEnv, QueueContextEnv,
logger,
) )

View file

@ -19,6 +19,7 @@ else:
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from enum import Enum from enum import Enum
import structlog
from pydantic import BaseModel, ConfigDict from pydantic import BaseModel, ConfigDict
from ...utils import raise_error from ...utils import raise_error
@ -26,6 +27,9 @@ from ...utils import raise_error
__all__ = ["EnvKind", "EnvShell", "QueueContextAdapter", "QueueContextEnv"] __all__ = ["EnvKind", "EnvShell", "QueueContextAdapter", "QueueContextEnv"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="api")
class EnvKind(str, Enum): class EnvKind(str, Enum):
"""Accepted Python environment kind.""" """Accepted Python environment kind."""

View file

@ -10,13 +10,17 @@ import sys
from pathlib import Path from pathlib import Path
import pandas as pd import pandas as pd
import structlog
from ..typing import Elements from ..typing import Elements
from ..utils import logger, raise_error, warn_with_log, yaml from ..utils import raise_error, warn_with_log, yaml
__all__ = ["parse_elements", "parse_yaml"] __all__ = ["parse_elements", "parse_yaml"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="cli")
def parse_yaml(filepath: str | Path) -> dict: # noqa: C901 def parse_yaml(filepath: str | Path) -> dict: # noqa: C901
"""Parse YAML. """Parse YAML.

View file

@ -9,7 +9,7 @@ import numpy as np
from numpy.typing import ArrayLike from numpy.typing import ArrayLike
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, run_ext_cmd from ...utils import run_ext_cmd
__all__ = ["ANTsCoordinatesWarper"] __all__ = ["ANTsCoordinatesWarper"]
@ -46,6 +46,9 @@ class ANTsCoordinatesWarper:
The transformed coordinates. The transformed coordinates.
""" """
# Imported here to avoid circular import
from ._coordinates import logger
logger.debug("Using ANTs for coordinates transformation") logger.debug("Using ANTs for coordinates transformation")
# Create element-specific tempdir for storing post-warping assets # Create element-specific tempdir for storing post-warping assets

View file

@ -9,10 +9,11 @@ from typing import Any
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import structlog
from junifer_data import get from junifer_data import get
from numpy.typing import ArrayLike from numpy.typing import ArrayLike
from ...utils import logger, raise_error from ...utils import raise_error
from ..pipeline_data_registry_base import BasePipelineDataRegistry from ..pipeline_data_registry_base import BasePipelineDataRegistry
from ..utils import JUNIFER_DATA_PARAMS, get_dataset_path, get_native_warper from ..utils import JUNIFER_DATA_PARAMS, get_dataset_path, get_native_warper
from ._ants_coordinates_warper import ANTsCoordinatesWarper from ._ants_coordinates_warper import ANTsCoordinatesWarper
@ -21,6 +22,9 @@ from ._fsl_coordinates_warper import FSLCoordinatesWarper
__all__ = ["CoordinatesRegistry"] __all__ = ["CoordinatesRegistry"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="data")
class CoordinatesRegistry(BasePipelineDataRegistry): class CoordinatesRegistry(BasePipelineDataRegistry):
"""Class for coordinates data registry. """Class for coordinates data registry.

View file

@ -9,7 +9,7 @@ import numpy as np
from numpy.typing import ArrayLike from numpy.typing import ArrayLike
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, run_ext_cmd from ...utils import run_ext_cmd
__all__ = ["FSLCoordinatesWarper"] __all__ = ["FSLCoordinatesWarper"]
@ -46,6 +46,9 @@ class FSLCoordinatesWarper:
The transformed coordinates. The transformed coordinates.
""" """
# Imported here to avoid circular import
from ._coordinates import logger
logger.debug("Using FSL for coordinates transformation") logger.debug("Using FSL for coordinates transformation")
# Create element-specific tempdir for storing post-warping assets # Create element-specific tempdir for storing post-warping assets

View file

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
import nibabel as nib import nibabel as nib
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, raise_error, run_ext_cmd from ...utils import raise_error, run_ext_cmd
from ..template_spaces import get_template, get_xfm from ..template_spaces import get_template, get_xfm
@ -71,6 +71,9 @@ class ANTsMapsWarper:
If ``warp_data`` is None when ``dst="T1w"``. If ``warp_data`` is None when ``dst="T1w"``.
""" """
# Imported here to avoid circular import
from ._maps import logger
# Create element-scoped tempdir so that warped maps is # Create element-scoped tempdir so that warped maps is
# available later as nibabel stores file path reference for # available later as nibabel stores file path reference for
# loading on computation # loading on computation

View file

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
import nibabel as nib import nibabel as nib
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, run_ext_cmd from ...utils import run_ext_cmd
if TYPE_CHECKING: if TYPE_CHECKING:
@ -53,6 +53,9 @@ class FSLMapsWarper:
The transformed maps image. The transformed maps image.
""" """
# Imported here to avoid circular import
from ._maps import logger
logger.debug("Using FSL for maps transformation") logger.debug("Using FSL for maps transformation")
# Create element-scoped tempdir so that warped maps is # Create element-scoped tempdir so that warped maps is

View file

@ -9,9 +9,10 @@ from typing import TYPE_CHECKING, Any, Optional
import nibabel as nib import nibabel as nib
import nilearn.image as nimg import nilearn.image as nimg
import numpy as np import numpy as np
import structlog
from junifer_data import get from junifer_data import get
from ...utils import logger, raise_error from ...utils import raise_error
from ..pipeline_data_registry_base import BasePipelineDataRegistry from ..pipeline_data_registry_base import BasePipelineDataRegistry
from ..utils import ( from ..utils import (
JUNIFER_DATA_PARAMS, JUNIFER_DATA_PARAMS,
@ -29,6 +30,9 @@ if TYPE_CHECKING:
__all__ = ["MapsRegistry"] __all__ = ["MapsRegistry"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="data")
class MapsRegistry(BasePipelineDataRegistry): class MapsRegistry(BasePipelineDataRegistry):
"""Class for maps data registry. """Class for maps data registry.

View file

@ -10,7 +10,7 @@ import nibabel as nib
import numpy as np import numpy as np
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, raise_error, run_ext_cmd from ...utils import raise_error, run_ext_cmd
from ..template_spaces import get_template, get_xfm from ..template_spaces import get_template, get_xfm
@ -92,6 +92,9 @@ class ANTsMaskWarper:
If ``warp_data`` is None when ``dst="T1w"``. If ``warp_data`` is None when ``dst="T1w"``.
""" """
# Imported here to avoid circular import
from ._masks import logger
# Create element-scoped tempdir so that warped mask is # Create element-scoped tempdir so that warped mask is
# available later as nibabel stores file path reference for # available later as nibabel stores file path reference for
# loading on computation # loading on computation

View file

@ -10,7 +10,7 @@ import nibabel as nib
import numpy as np import numpy as np
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, run_ext_cmd from ...utils import run_ext_cmd
if TYPE_CHECKING: if TYPE_CHECKING:
@ -74,6 +74,9 @@ class FSLMaskWarper:
The transformed mask image. The transformed mask image.
""" """
# Imported here to avoid circular import
from ._masks import logger
logger.debug("Using FSL for mask transformation") logger.debug("Using FSL for mask transformation")
# Create element-scoped tempdir so that warped mask is # Create element-scoped tempdir so that warped mask is

View file

@ -15,6 +15,7 @@ from typing import (
import nibabel as nib import nibabel as nib
import nilearn.image as nimg import nilearn.image as nimg
import numpy as np import numpy as np
import structlog
from junifer_data import get from junifer_data import get
from nilearn.masking import ( from nilearn.masking import (
compute_background_mask, compute_background_mask,
@ -22,7 +23,7 @@ from nilearn.masking import (
intersect_masks, intersect_masks,
) )
from ...utils import logger, raise_error from ...utils import raise_error
from ..pipeline_data_registry_base import BasePipelineDataRegistry from ..pipeline_data_registry_base import BasePipelineDataRegistry
from ..template_spaces import get_template from ..template_spaces import get_template
from ..utils import ( from ..utils import (
@ -41,6 +42,9 @@ if TYPE_CHECKING:
__all__ = ["MaskRegistry", "compute_brain_mask"] __all__ = ["MaskRegistry", "compute_brain_mask"]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="data")
def compute_brain_mask( def compute_brain_mask(
target_data: dict[str, Any], target_data: dict[str, Any],

View file

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
import nibabel as nib import nibabel as nib
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, raise_error, run_ext_cmd from ...utils import raise_error, run_ext_cmd
from ..template_spaces import get_template, get_xfm from ..template_spaces import get_template, get_xfm
@ -71,6 +71,9 @@ class ANTsParcellationWarper:
If ``warp_data`` is None when ``dst="T1w"``. If ``warp_data`` is None when ``dst="T1w"``.
""" """
# Imported here to avoid circular import
from ._parcellations import logger
# Create element-scoped tempdir so that warped parcellation is # Create element-scoped tempdir so that warped parcellation is
# available later as nibabel stores file path reference for # available later as nibabel stores file path reference for
# loading on computation # loading on computation

View file

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any
import nibabel as nib import nibabel as nib
from ...pipeline import WorkDirManager from ...pipeline import WorkDirManager
from ...utils import logger, run_ext_cmd from ...utils import run_ext_cmd
if TYPE_CHECKING: if TYPE_CHECKING:
@ -53,6 +53,9 @@ class FSLParcellationWarper:
The transformed parcellation image. The transformed parcellation image.
""" """
# Imported here to avoid circular import
from ._parcellations import logger
logger.debug("Using FSL for parcellation transformation") logger.debug("Using FSL for parcellation transformation")
# Create element-scoped tempdir so that warped parcellation is # Create element-scoped tempdir so that warped parcellation is

View file

@ -13,9 +13,10 @@ import nibabel as nib
import nilearn.image as nimg import nilearn.image as nimg
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import structlog
from junifer_data import get from junifer_data import get
from ...utils import logger, raise_error, warn_with_log from ...utils import raise_error, warn_with_log
from ..pipeline_data_registry_base import BasePipelineDataRegistry from ..pipeline_data_registry_base import BasePipelineDataRegistry
from ..utils import ( from ..utils import (
JUNIFER_DATA_PARAMS, JUNIFER_DATA_PARAMS,
@ -36,6 +37,9 @@ __all__ = [
"merge_parcellations", "merge_parcellations",
] ]
_log = structlog.get_logger("junifer")
logger = _log.bind(pkg="data")
class ParcellationRegistry(BasePipelineDataRegistry): class ParcellationRegistry(BasePipelineDataRegistry):
"""Class for parcellation data registry. """Class for parcellation data registry.
@ -58,8 +62,12 @@ class ParcellationRegistry(BasePipelineDataRegistry):
# The built-in parcellations are files that are shipped with the # The built-in parcellations are files that are shipped with the
# junifer-data dataset. # junifer-data dataset.
# Make built-in and external dictionaries for validation later # Make built-in and external dictionaries for validation later
self._builtin = {} self._builtin: dict[
self._external = {} str, dict[str, str | int | Path | list[str] | list[int]]
] = {}
self._external: dict[
str, dict[str, str | int | Path | list[str] | list[int]]
] = {}
# Add SUIT # Add SUIT
self._builtin.update( self._builtin.update(

Some files were not shown because too many files have changed in this diff Show more