Skip to content

Commit

Permalink
Merge pull request #490 from OpenCOMPES/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
rettigl authored Sep 19, 2024
2 parents 83fe946 + 8716dc0 commit c90e839
Show file tree
Hide file tree
Showing 16 changed files with 642 additions and 374 deletions.
83 changes: 56 additions & 27 deletions sed/calibrator/delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
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
logger = setup_logging("delay")


class DelayCalibrator:
Expand All @@ -22,22 +27,30 @@ class DelayCalibrator:
Args:
config (dict, optional): Config dictionary. Defaults to None.
verbose (bool, optional): Option to print out diagnostic information.
Defaults to True.
"""

def __init__(
self,
config: dict = None,
verbose: bool = True,
) -> None:
"""Initialization of the DelayCalibrator class passes the config.
Args:
config (dict, optional): Config dictionary. Defaults to None.
verbose (bool, optional): Option to print out diagnostic information.
Defaults to True.
"""
if config is not None:
self._config = config
else:
self._config = {}

self._verbose = verbose
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"]
self.corrected_delay_column = self._config["dataframe"].get(
Expand All @@ -47,6 +60,25 @@ def __init__(
self.calibration: dict[str, Any] = self._config["delay"].get("calibration", {})
self.offsets: dict[str, Any] = self._config["delay"].get("offsets", {})

@property
def verbose(self) -> bool:
"""Accessor to the verbosity flag.
Returns:
bool: Verbosity flag.
"""
return self._verbose

@verbose.setter
def verbose(self, verbose: bool):
"""Setter for the verbosity.
Args:
verbose (bool): Option to turn on verbose output. Sets loglevel to INFO.
"""
self._verbose = verbose
set_verbosity(logger, self._verbose)

def append_delay_axis(
self,
df: pd.DataFrame | dask.dataframe.DataFrame,
Expand All @@ -61,7 +93,7 @@ def append_delay_axis(
p1_key: str = None,
p2_key: str = None,
t0_key: str = None,
verbose: bool = True,
suppress_output: bool = False,
) -> tuple[pd.DataFrame | dask.dataframe.DataFrame, dict]:
"""Calculate and append the delay axis to the events dataframe, by converting
values from an analog-digital-converter (ADC).
Expand Down Expand Up @@ -92,8 +124,7 @@ def append_delay_axis(
Defaults to config["delay"]["p2_key"]
t0_key (str, optional): hdf5 key for t0 value (mm).
Defaults to config["delay"]["t0_key"]
verbose (bool, optional): Option to print out diagnostic information.
Defaults to True.
suppress_output (bool, optional): Option to suppress log output. Defaults to False.
Raises:
ValueError: Raised if delay parameters are not found in the file.
Expand Down Expand Up @@ -126,11 +157,11 @@ def append_delay_axis(
calibration["delay_range_mm"] = delay_range_mm
else:
# report usage of loaded parameters
if "creation_date" in calibration and verbose:
if "creation_date" in calibration and not suppress_output:
datestring = datetime.fromtimestamp(calibration["creation_date"]).strftime(
"%m/%d/%Y, %H:%M:%S",
)
print(f"Using delay calibration parameters generated on {datestring}")
logger.info(f"Using delay calibration parameters generated on {datestring}")

if adc_column is None:
adc_column = self.adc_column
Expand Down Expand Up @@ -166,8 +197,8 @@ def append_delay_axis(
calibration["datafile"] = datafile
calibration["delay_range_mm"] = (ret[0], ret[1])
calibration["time0"] = ret[2]
if verbose:
print(f"Extract delay range from file '{datafile}'.")
if not suppress_output:
logger.info(f"Extract delay range from file '{datafile}'.")
else:
raise NotImplementedError(
"Not enough parameters for delay calibration.",
Expand All @@ -179,8 +210,8 @@ def append_delay_axis(
calibration["time0"],
),
)
if verbose:
print(f"Converted delay_range (ps) = {calibration['delay_range']}")
if not suppress_output:
logger.info(f"Converted delay_range (ps) = {calibration['delay_range']}")
calibration["creation_date"] = datetime.now().timestamp()

if "delay_range" in calibration.keys():
Expand All @@ -190,8 +221,8 @@ def append_delay_axis(
calibration["adc_range"][1] - calibration["adc_range"][0]
)
self.calibration = deepcopy(calibration)
if verbose:
print(
if not suppress_output:
logger.info(
"Append delay axis using delay_range = "
f"[{calibration['delay_range'][0]}, {calibration['delay_range'][1]}]"
" and adc_range = "
Expand All @@ -214,7 +245,7 @@ def add_offsets(
preserve_mean: bool | Sequence[bool] = False,
reductions: str | Sequence[str] = None,
delay_column: str = None,
verbose: bool = True,
suppress_output: bool = False,
) -> tuple[dask.dataframe.DataFrame, dict]:
"""Apply an offset to the delay column based on a constant or other columns.
Expand All @@ -234,8 +265,7 @@ def add_offsets(
If None, the shift is applied per-dataframe-row. Defaults to None. Currently only
"mean" is supported.
delay_column (str, optional): Name of the column containing the delay values.
verbose (bool, optional): Option to print out diagnostic information.
Defaults to True.
suppress_output (bool, optional): Option to suppress log output. Defaults to False.
Returns:
tuple[dask.dataframe.DataFrame, dict]: Dataframe with the shifted delay axis and
Expand Down Expand Up @@ -299,27 +329,25 @@ def add_offsets(
if flip_delay_axis:
offsets["flip_delay_axis"] = flip_delay_axis

elif "creation_date" in offsets and verbose:
elif "creation_date" in offsets and not suppress_output:
datestring = datetime.fromtimestamp(offsets["creation_date"]).strftime(
"%m/%d/%Y, %H:%M:%S",
)
print(f"Using delay offset parameters generated on {datestring}")
logger.info(f"Using delay offset parameters generated on {datestring}")

if len(offsets) > 0:
# unpack dictionary
columns = []
weights = []
preserve_mean = []
reductions = []
if verbose:
print("Delay offset parameters:")
log_str = "Delay offset parameters:"
for k, v in offsets.items():
if k == "creation_date":
continue
if k == "constant":
constant = v
if verbose:
print(f" Constant: {constant} ")
log_str += f"\n Constant: {constant}"
elif k == "flip_delay_axis":
fda = str(v)
if fda.lower() in ["true", "1"]:
Expand All @@ -330,8 +358,7 @@ def add_offsets(
raise ValueError(
f"Invalid value for flip_delay_axis in config: {flip_delay_axis}.",
)
if verbose:
print(f" Flip delay axis: {flip_delay_axis} ")
log_str += f"\n Flip delay axis: {flip_delay_axis}"
else:
columns.append(k)
try:
Expand All @@ -343,11 +370,13 @@ def add_offsets(
preserve_mean.append(pm)
red = v.get("reduction", None)
reductions.append(red)
if verbose:
print(
f" Column[{k}]: Weight={weight}, Preserve Mean: {pm}, ",
f"Reductions: {red}.",
)
log_str += (
f"\n Column[{k}]: Weight={weight}, Preserve Mean: {pm}, "
f"Reductions: {red}."
)

if not suppress_output:
logger.info(log_str)

if len(columns) > 0:
df = dfops.offset_by_other_columns(
Expand Down
Loading

0 comments on commit c90e839

Please sign in to comment.