[libcamera-devel,2/3] utils: libtuning: Replace eprint with Logger
diff mbox series

Message ID 20221129053326.2858347-3-paul.elder@ideasonboard.com
State New
Headers show
Series
  • utils: tuning: Add logging infrastructure
Related show

Commit Message

Paul Elder Nov. 29, 2022, 5:33 a.m. UTC
Replace all instances (including the definition) of eprint with Logger.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 .../libtuning/generators/yaml_output.py       |  4 ++--
 utils/tuning/libtuning/image.py               |  6 +++---
 utils/tuning/libtuning/libtuning.py           | 20 +++++++++----------
 utils/tuning/libtuning/macbeth.py             | 12 ++++++-----
 .../libtuning/modules/lsc/raspberrypi.py      | 12 +++++------
 utils/tuning/libtuning/utils.py               | 17 ++++++++--------
 6 files changed, 37 insertions(+), 34 deletions(-)

Patch
diff mbox series

diff --git a/utils/tuning/libtuning/generators/yaml_output.py b/utils/tuning/libtuning/generators/yaml_output.py
index effb4fb3..9966cb4c 100644
--- a/utils/tuning/libtuning/generators/yaml_output.py
+++ b/utils/tuning/libtuning/generators/yaml_output.py
@@ -9,7 +9,7 @@  from .generator import Generator
 from numbers import Number
 from pathlib import Path
 
-import libtuning.utils as utils
+from libtuning.logger import Logger
 
 
 class YamlOutput(Generator):
@@ -112,7 +112,7 @@  class YamlOutput(Generator):
                 continue
 
             if not isinstance(output_dict[module], dict):
-                utils.eprint(f'Error: Output of {module.type} is not a dictionary')
+                Logger.log_error(self, f'Output of {module.type} is not a dictionary')
                 continue
 
             lines = self._stringify_dict(output_dict[module])
diff --git a/utils/tuning/libtuning/image.py b/utils/tuning/libtuning/image.py
index aa9d20b5..0a35f384 100644
--- a/utils/tuning/libtuning/image.py
+++ b/utils/tuning/libtuning/image.py
@@ -12,7 +12,7 @@  import rawpy as raw
 import re
 
 import libtuning as lt
-import libtuning.utils as utils
+from libtuning.logger import Logger
 
 
 class Image:
@@ -25,13 +25,13 @@  class Image:
         try:
             self._load_metadata_exif()
         except Exception as e:
-            utils.eprint(f'Failed to load metadata from {self.path}: {e}')
+            Logger.log_error(self, f'Failed to load metadata from {self.path}: {e}')
             raise e
 
         try:
             self._read_image_dng()
         except Exception as e:
-            utils.eprint(f'Failed to load image data from {self.path}: {e}')
+            Logger.log_error(self, f'Failed to load image data from {self.path}: {e}')
             raise e
 
     @property
diff --git a/utils/tuning/libtuning/libtuning.py b/utils/tuning/libtuning/libtuning.py
index a1238bb1..d34d98ab 100644
--- a/utils/tuning/libtuning/libtuning.py
+++ b/utils/tuning/libtuning/libtuning.py
@@ -8,8 +8,8 @@  import argparse
 from pathlib import Path
 
 import libtuning as lt
+from libtuning.logger import Logger
 import libtuning.utils as utils
-from libtuning.utils import eprint
 
 from enum import Enum, IntEnum
 
@@ -109,10 +109,10 @@  class Tuner(object):
         for module_type in output_order:
             modules = [module for module in self.modules if module.type == module_type.type]
             if len(modules) > 1:
-                eprint(f'Multiple modules found for module type "{module_type.type}"')
+                Logger.log_error(self, f'Multiple modules found for module type "{module_type.type}"')
                 return False
             if len(modules) < 1:
-                eprint(f'No module found for module type "{module_type.type}"')
+                Logger.log_error(self, f'No module found for module type "{module_type.type}"')
                 return False
             self.output_order.append(modules[0])
 
@@ -121,19 +121,19 @@  class Tuner(object):
     # \todo Validate parser and generator at Tuner construction time?
     def _validate_settings(self):
         if self.parser is None:
-            eprint('Missing parser')
+            Logger.log_error(self, 'Missing parser')
             return False
 
         if self.generator is None:
-            eprint('Missing generator')
+            Logger.log_error(self, 'Missing generator')
             return False
 
         if len(self.modules) == 0:
-            eprint('No modules added')
+            Logger.log_error(self, 'No modules added')
             return False
 
         if len(self.output_order) != len(self.modules):
-            eprint('Number of outputs does not match number of modules')
+            Logger.log_error(self, 'Number of outputs does not match number of modules')
             return False
 
         return True
@@ -208,7 +208,7 @@  class Tuner(object):
         # Validate modules and set debug
         for module in self.modules:
             if not module.validate_config(self.config):
-                eprint(f'Config is invalid for module {module.type}')
+                Logger.log_error(self, f'Config is invalid for module {module.type}')
                 return -1
 
             if args.debug_dir is not None and \
@@ -224,14 +224,14 @@  class Tuner(object):
 
         images = utils.load_images(args.input, self.config, not has_only_lsc, has_lsc)
         if images is None or len(images) == 0:
-            eprint(f'No images were found, or able to load')
+            Logger.log_error(self, f'No images were found, or able to load')
             return -1
 
         # Do the tuning
         for module in self.modules:
             out = module.process(self.config, images, self.output)
             if out is None:
-                eprint(f'Module {module.name} failed to process, aborting')
+                Logger.log_error(self, f'Module {module.name} failed to process, aborting')
                 break
             self.output[module] = out
 
diff --git a/utils/tuning/libtuning/macbeth.py b/utils/tuning/libtuning/macbeth.py
index 5faddf66..df16e868 100644
--- a/utils/tuning/libtuning/macbeth.py
+++ b/utils/tuning/libtuning/macbeth.py
@@ -13,7 +13,9 @@  from pathlib import Path
 import numpy as np
 
 from libtuning.image import Image
+from libtuning.logger import Logger
 
+macbeth_log_str = 'Macbeth'
 
 # Reshape image to fixed width without distorting returns image and scale
 # factor
@@ -369,7 +371,7 @@  def get_macbeth_chart(img, ref_data):
 
     # Catch macbeth errors and continue with code
     except MacbethError as error:
-        eprint(error)
+        Logger.log_error(macbeth_log_str, error)
         return (0, None, None, False)
 
 
@@ -486,7 +488,7 @@  def find_macbeth(img, mac_config):
 
     coords_fit = coords
     if cor < 0.75:
-        eprint(f'Warning: Low confidence {cor:.3f} for macbeth chart in {img.path.name}')
+        Logger.log_warn(macbeth_log_str, f'Low confidence {cor:.3f} for macbeth chart in {img.path.name}')
 
     if show:
         draw_macbeth_results(img, coords_fit)
@@ -499,18 +501,18 @@  def locate_macbeth(image: Image, config: dict):
     av_chan = (np.mean(np.array(image.channels), axis=0) / (2**16))
     av_val = np.mean(av_chan)
     if av_val < image.blacklevel_16 / (2**16) + 1 / 64:
-        eprint(f'Image {image.path.name} too dark')
+        Logger.log_error(macbeth_log_str, f'Image {image.path.name} too dark')
         return None
 
     macbeth = find_macbeth(av_chan, config['general']['macbeth'])
 
     if macbeth is None:
-        eprint(f'No macbeth chart found in {image.path.name}')
+        Logger.log_error(macbeth_log_str, f'No macbeth chart found in {image.path.name}')
         return None
 
     mac_cen_coords = macbeth[1]
     if not image.get_patches(mac_cen_coords):
-        eprint(f'Macbeth patches have saturated in {image.path.name}')
+        Logger.log_error(macbeth_log_str, f'Macbeth patches have saturated in {image.path.name}')
         return None
 
     return macbeth
diff --git a/utils/tuning/libtuning/modules/lsc/raspberrypi.py b/utils/tuning/libtuning/modules/lsc/raspberrypi.py
index a0298348..91aa56e3 100644
--- a/utils/tuning/libtuning/modules/lsc/raspberrypi.py
+++ b/utils/tuning/libtuning/modules/lsc/raspberrypi.py
@@ -8,7 +8,7 @@ 
 from .lsc import LSC
 
 import libtuning as lt
-import libtuning.utils as utils
+from libtuning.logger import Logger
 
 from numbers import Number
 import numpy as np
@@ -35,7 +35,7 @@  class ALSCRaspberryPi(LSC):
 
     def validate_config(self, config: dict) -> bool:
         if self not in config:
-            utils.eprint(f'{self.type} not in config')
+            Logger.log_error(self, f'{self.type} not in config')
             return False
 
         valid = True
@@ -46,14 +46,14 @@  class ALSCRaspberryPi(LSC):
         color_key = self.do_color.name
 
         if lum_key not in conf and self.luminance_strength.required:
-            utils.eprint(f'{lum_key} is not in config')
+            Logger.log_error(self, f'{lum_key} is not in config')
             valid = False
 
         if lum_key in conf and (conf[lum_key] < 0 or conf[lum_key] > 1):
-            utils.eprint(f'Warning: {lum_key} is not in range [0, 1]; defaulting to 0.5')
+            Logger.log_warn(self, f'Warning: {lum_key} is not in range [0, 1]; defaulting to 0.5')
 
         if color_key not in conf and self.do_color.required:
-            utils.eprint(f'{color_key} is not in config')
+            Logger.log_error(self, f'{color_key} is not in config')
             valid = False
 
         return valid
@@ -236,7 +236,7 @@  class ALSCRaspberryPi(LSC):
         if count == 1:
             output['sigma'] = 0.005
             output['sigma_Cb'] = 0.005
-            utils.eprint('Warning: Only one alsc calibration found; standard sigmas used for adaptive algorithm.')
+            Logger.log_warn(self, 'Only one alsc calibration found; standard sigmas used for adaptive algorithm.')
             return output
 
         # Obtain worst-case scenario residual sigmas
diff --git a/utils/tuning/libtuning/utils.py b/utils/tuning/libtuning/utils.py
index b60f2c9b..d33b9294 100644
--- a/utils/tuning/libtuning/utils.py
+++ b/utils/tuning/libtuning/utils.py
@@ -6,6 +6,7 @@ 
 # utils.py - Utilities for libtuning
 
 import decimal
+from inspect import currentframe, getframeinfo
 import math
 import numpy as np
 import os
@@ -15,13 +16,13 @@  import sys
 
 import libtuning as lt
 from libtuning.image import Image
+from libtuning.logger import Logger
 from libtuning.macbeth import locate_macbeth
 
-# Utility functions
 
+utils_log_str = 'Utils'
 
-def eprint(*args, **kwargs):
-    print(*args, file=sys.stderr, **kwargs)
+# Utility functions
 
 
 def get_module_by_type_name(modules, name):
@@ -45,7 +46,7 @@  def _list_image_files(directory):
 def _parse_image_filename(fn: Path):
     result = re.search(r'^(alsc_)?(\d+)[kK]_(\d+)?[lLuU]?.\w{3,4}$', fn.name)
     if result is None:
-        eprint(f'The file name of {fn.name} is incorrectly formatted')
+        Logger.log_error(utils_log_str, f'The file name of {fn.name} is incorrectly formatted')
         return None, None, None
 
     color = int(result.group(2))
@@ -72,7 +73,7 @@  def _validate_images(images):
 def load_images(input_dir: str, config: dict, load_nonlsc: bool, load_lsc: bool) -> list:
     files = _list_image_files(input_dir)
     if len(files) == 0:
-        eprint(f'No images found in {input_dir}')
+        Logger.log_error(utils_log_str, f'No images found in {input_dir}')
         return None
 
     images = []
@@ -83,19 +84,19 @@  def load_images(input_dir: str, config: dict, load_nonlsc: bool, load_lsc: bool)
 
         # Skip lsc image if we don't need it
         if lsc_only and not load_lsc:
-            eprint(f'Skipping {f.name} as this tuner has no LSC module')
+            Logger.log_info(utils_log_str, f'Skipping {f.name} as this tuner has no LSC module')
             continue
 
         # Skip non-lsc image if we don't need it
         if not lsc_only and not load_nonlsc:
-            eprint(f'Skipping {f.name} as this tuner only has an LSC module')
+            Logger.log_info(utils_log_str, f'Skipping {f.name} as this tuner only has an LSC module')
             continue
 
         # Load image
         try:
             image = Image(f)
         except Exception as e:
-            eprint(f'Failed to load image {f.name}: {e}')
+            Logger.log_warn(utils_log_str, f'Failed to load image {f.name}: {e}')
             continue
 
         # Populate simple fields