mirror of
https://github.com/pim-n/pg-rad
synced 2026-02-02 14:33:09 +01:00
improving logging setup
This commit is contained in:
File diff suppressed because one or more lines are too long
@ -1,12 +1,13 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from pg_rad.logger import setup_logger
|
|
||||||
from pg_rad.exceptions import DataLoadError, InvalidCSVError
|
from pg_rad.exceptions import DataLoadError, InvalidCSVError
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def load_data(filename: str) -> pd.DataFrame:
|
def load_data(filename: str) -> pd.DataFrame:
|
||||||
logger.debug(f"Attempting to load data from {filename}")
|
logger.debug(f"Attempting to load file: {filename}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
df = pd.read_csv(filename, delimiter=',')
|
df = pd.read_csv(filename, delimiter=',')
|
||||||
@ -23,4 +24,5 @@ def load_data(filename: str) -> pd.DataFrame:
|
|||||||
logger.exception(f"Unexpected error while loading {filename}")
|
logger.exception(f"Unexpected error while loading {filename}")
|
||||||
raise DataLoadError("Unexpected error while loading data") from e
|
raise DataLoadError("Unexpected error while loading data") from e
|
||||||
|
|
||||||
|
logger.debug(f"File loaded: {filename}")
|
||||||
return df
|
return df
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
from matplotlib.patches import Circle
|
from matplotlib.patches import Circle
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -5,6 +7,8 @@ import numpy as np
|
|||||||
from pg_rad.path import Path
|
from pg_rad.path import Path
|
||||||
from pg_rad.sources import PointSource
|
from pg_rad.sources import PointSource
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Landscape:
|
class Landscape:
|
||||||
"""A generic Landscape that can contain a Path and sources.
|
"""A generic Landscape that can contain a Path and sources.
|
||||||
|
|
||||||
@ -31,6 +35,7 @@ class Landscape:
|
|||||||
|
|
||||||
self.path: Path = None
|
self.path: Path = None
|
||||||
self.sources: list[PointSource] = []
|
self.sources: list[PointSource] = []
|
||||||
|
logger.debug("Landscape initialized.")
|
||||||
|
|
||||||
def plot(self, z = 0):
|
def plot(self, z = 0):
|
||||||
"""Plot a slice of the world at a height `z`.
|
"""Plot a slice of the world at a height `z`.
|
||||||
|
|||||||
@ -4,8 +4,11 @@ import pathlib
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
def setup_logger(name):
|
def setup_logger(log_level: str = "WARNING"):
|
||||||
logger = logging.getLogger(name)
|
levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
||||||
|
|
||||||
|
if not log_level in levels:
|
||||||
|
raise ValueError(f"Log level must be one of {levels}.")
|
||||||
|
|
||||||
base_dir = pathlib.Path(__file__).resolve().parent
|
base_dir = pathlib.Path(__file__).resolve().parent
|
||||||
config_file = base_dir / "configs" / "logging.yml"
|
config_file = base_dir / "configs" / "logging.yml"
|
||||||
@ -13,5 +16,6 @@ def setup_logger(name):
|
|||||||
with open(config_file) as f:
|
with open(config_file) as f:
|
||||||
config = yaml.safe_load(f)
|
config = yaml.safe_load(f)
|
||||||
|
|
||||||
|
config["loggers"]["root"]["level"] = log_level
|
||||||
|
|
||||||
logging.config.dictConfig(config)
|
logging.config.dictConfig(config)
|
||||||
return logger
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
import logging
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
@ -7,9 +8,8 @@ import pandas as pd
|
|||||||
import piecewise_regression
|
import piecewise_regression
|
||||||
|
|
||||||
from pg_rad.exceptions import ConvergenceError
|
from pg_rad.exceptions import ConvergenceError
|
||||||
from pg_rad.logger import setup_logger
|
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class PathSegment:
|
class PathSegment:
|
||||||
def __init__(self, a: tuple[float, float], b: tuple[float, float]):
|
def __init__(self, a: tuple[float, float], b: tuple[float, float]):
|
||||||
@ -73,6 +73,8 @@ class Path:
|
|||||||
|
|
||||||
self.z = z
|
self.z = z
|
||||||
|
|
||||||
|
logger.debug("Path created.")
|
||||||
|
|
||||||
def get_length(self) -> float:
|
def get_length(self) -> float:
|
||||||
return sum([s.length for s in self.segments])
|
return sum([s.length for s in self.segments])
|
||||||
|
|
||||||
@ -136,7 +138,7 @@ def simplify_path(
|
|||||||
pw_res = pw_fit.get_results()
|
pw_res = pw_fit.get_results()
|
||||||
|
|
||||||
if pw_res == None:
|
if pw_res == None:
|
||||||
logger.error("Piecewise regression failed to converge.")
|
logger.warning("Piecewise regression failed to converge.")
|
||||||
raise ConvergenceError("Piecewise regression failed to converge.")
|
raise ConvergenceError("Piecewise regression failed to converge.")
|
||||||
|
|
||||||
est = pw_res['estimates']
|
est = pw_res['estimates']
|
||||||
@ -184,4 +186,5 @@ def path_from_RT90(
|
|||||||
coord_pairs = list(zip(east_arr, north_arr))
|
coord_pairs = list(zip(east_arr, north_arr))
|
||||||
|
|
||||||
path = Path(coord_pairs, **kwargs)
|
path = Path(coord_pairs, **kwargs)
|
||||||
|
logger.debug("Loaded path from provided RT90 coordinates.")
|
||||||
return path
|
return path
|
||||||
@ -1,6 +1,10 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from pg_rad.objects import Object
|
from pg_rad.objects import Object
|
||||||
from pg_rad.isotope import Isotope
|
from pg_rad.isotope import Isotope
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class PointSource(Object):
|
class PointSource(Object):
|
||||||
_id_counter = 1
|
_id_counter = 1
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -39,5 +43,7 @@ class PointSource(Object):
|
|||||||
self.isotope = isotope
|
self.isotope = isotope
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
||||||
|
logger.debug(f"Source created: {self.name}")
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"PointSource(name={self.name}, pos={(self.x, self.y, self.z)}, isotope={self.isotope.name}, A={self.activity} MBq)"
|
return f"PointSource(name={self.name}, pos={(self.x, self.y, self.z)}, isotope={self.isotope.name}, A={self.activity} MBq)"
|
||||||
Reference in New Issue
Block a user