Skip to content

Commit

Permalink
add function to set verbosity, and use in each class
Browse files Browse the repository at this point in the history
  • Loading branch information
rettigl committed Sep 19, 2024
1 parent 2a69836 commit 79a181d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 43 deletions.
8 changes: 2 additions & 6 deletions sed/calibrator/delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from collections.abc import Sequence
from copy import deepcopy
from datetime import datetime
from logging import INFO
from logging import WARNING
from typing import Any

import dask.dataframe
Expand All @@ -15,6 +13,7 @@
import pandas as pd

from sed.core import dfops
from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging

# Configure logging
Expand Down Expand Up @@ -50,10 +49,7 @@ def __init__(
self._config = {}

self.verbose = verbose
if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.adc_column: str = self._config["dataframe"].get("adc_column", None)
self.delay_column: str = self._config["dataframe"]["delay_column"]
Expand Down
8 changes: 2 additions & 6 deletions sed/calibrator/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from copy import deepcopy
from datetime import datetime
from functools import partial
from logging import INFO
from logging import WARNING
from typing import Any
from typing import cast
from typing import Literal
Expand Down Expand Up @@ -37,6 +35,7 @@

from sed.binning import bin_dataframe
from sed.core import dfops
from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging
from sed.loader.base.loader import BaseLoader

Expand Down Expand Up @@ -93,10 +92,7 @@ def __init__(
self._config = config

self.verbose = verbose
if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.loader = loader
self.biases: np.ndarray = None
Expand Down
8 changes: 2 additions & 6 deletions sed/calibrator/momentum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import itertools as it
from copy import deepcopy
from datetime import datetime
from logging import INFO
from logging import WARNING
from typing import Any

import bokeh.palettes as bp
Expand All @@ -34,6 +32,7 @@
from symmetrize import sym
from symmetrize import tps

from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging

# Configure logging
Expand Down Expand Up @@ -81,10 +80,7 @@ def __init__(
self._config = config

self.verbose = verbose
if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.image: np.ndarray = None
self.img_ndim: int = None
Expand Down
17 changes: 16 additions & 1 deletion sed/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def setup_logging(
Args:
name (str): The name of the logger.
base_name (str, optional): The name of the base logger. Defaults to "sed".
set_base_handler (bool, optional): Option to re-initialize the base handler logging to the
logfile. Defaults to False.
user_log_path (str, optional): Path to the user-specific log directory.
Defaults to DEFAULT_LOG_DIR.
Expand Down Expand Up @@ -88,6 +89,20 @@ def setup_logging(
return logger


def set_verbosity(logger: logging.Logger, verbose: bool) -> None:
"""Sets log level for the given logger's default handler.
Args:
logger (logging.Logger): The logger on which to set the log level.
verbose (bool): Sets loglevel to INFO if True, to WARNING otherwise.
"""
handler = logger.handlers[0]
if verbose:
handler.setLevel(logging.INFO)
else:
handler.setLevel(logging.WARNING)


def call_logger(logger: logging.Logger):
def log_call(func: Callable):
@wraps(func)
Expand Down
8 changes: 2 additions & 6 deletions sed/core/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import pathlib
from collections.abc import Sequence
from datetime import datetime
from logging import INFO
from logging import WARNING
from typing import Any
from typing import cast

Expand All @@ -30,6 +28,7 @@
from sed.core.dfops import apply_filter
from sed.core.dfops import apply_jitter
from sed.core.logging import call_logger
from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging
from sed.core.metadata import MetaHandler
from sed.diagnostics import grid_histogram
Expand Down Expand Up @@ -121,10 +120,7 @@ def __init__(
self.verbose = self._config["core"].get("verbose", True)
else:
self.verbose = verbose
if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self._dataframe: pd.DataFrame | ddf.DataFrame = None
self._timed_dataframe: pd.DataFrame | ddf.DataFrame = None
Expand Down
8 changes: 2 additions & 6 deletions sed/loader/flash/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import re
import time
from collections.abc import Sequence
from logging import INFO
from logging import WARNING
from pathlib import Path

import dask.dataframe as dd
from natsort import natsorted

from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging
from sed.loader.base.loader import BaseLoader
from sed.loader.flash.buffer_handler import BufferHandler
Expand Down Expand Up @@ -55,10 +54,7 @@ def __init__(self, config: dict, verbose: bool = True) -> None:
"""
super().__init__(config=config, verbose=verbose)

if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.instrument: str = self._config["core"].get("instrument", "hextof") # default is hextof
self.raw_dir: str = None
Expand Down
8 changes: 2 additions & 6 deletions sed/loader/mpes/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import json
import os
from collections.abc import Sequence
from logging import INFO
from logging import WARNING
from typing import Any
from urllib.error import HTTPError
from urllib.error import URLError
Expand All @@ -25,6 +23,7 @@
import scipy.interpolate as sint
from natsort import natsorted

from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging
from sed.loader.base.loader import BaseLoader

Expand Down Expand Up @@ -571,10 +570,7 @@ def __init__(
):
super().__init__(config=config, verbose=verbose)

if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.read_timestamps = self._config.get("dataframe", {}).get(
"read_timestamps",
Expand Down
8 changes: 2 additions & 6 deletions sed/loader/sxp/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import time
from collections.abc import Sequence
from functools import reduce
from logging import INFO
from logging import WARNING
from pathlib import Path

import dask.dataframe as dd
Expand All @@ -30,6 +28,7 @@
from pandas import Series

from sed.core import dfops
from sed.core.logging import set_verbosity
from sed.core.logging import setup_logging
from sed.loader.base.loader import BaseLoader
from sed.loader.utils import parse_h5_keys
Expand Down Expand Up @@ -57,10 +56,7 @@ class SXPLoader(BaseLoader):
def __init__(self, config: dict, verbose: bool = True) -> None:
super().__init__(config=config, verbose=verbose)

if self.verbose:
logger.handlers[0].setLevel(INFO)
else:
logger.handlers[0].setLevel(WARNING)
set_verbosity(logger, self.verbose)

self.multi_index = ["trainId", "pulseId", "electronId"]
self.index_per_electron: MultiIndex = None
Expand Down

0 comments on commit 79a181d

Please sign in to comment.