Commit 3e231125 authored by Stephen Thompson's avatar Stephen Thompson

Merge branch '3-add_logger'

parents 8d536255 91408786
Pipeline #4459 failed with stages
in 2 minutes and 28 seconds
......@@ -16,6 +16,11 @@
"map" : "data/full",
"model to world" : "configuration/model_to_world.txt",
"logo" : false,
"search radius" : 1000
"search radius" : 1000,
"logger" : {
"log file name" : "sks_eval_liver_phantom.log",
"overwrite existing" : false
}
}
......@@ -40,10 +40,11 @@ class Locators():
self.models[index].actor.GetProperty().SetColor(1.0, 1.0, 1.0)
def is_hit(self, tracking):
def is_hit(self, tracking, logger):
"""
Checks whether a target has been hit
:param: the tracking data (3D point)
:param: a logger to write notification to
"""
index, distance = point_in_locator(tracking[0:3, 3],
self._locators,
......@@ -57,7 +58,7 @@ class Locators():
str(self._targets[self._target_index])])
if index == self._targets[self._target_index]:
print("hit")
logger.log(message="Hit target:{0}".format(index))
self._set_target_inactive(self._targets[self._target_index])
self._target_index = self._target_index + 1
if self._target_index < len(self._locators):
......@@ -66,6 +67,7 @@ class Locators():
self._text.set_text([str(index), str(distance),
str(tracking),
str("Finished")])
logger.log(message="All targets hit")
else:
self._text.set_text([str(index), str(distance),
str(tracking),
......
""" Class to handle logging """
from logging import getLogger, FileHandler, Formatter, INFO
class Logger():
"""Implements logging functionality for sksurgery-evaluation.
Configuration is done by passing a dictionary on construction.
Subsequent calls to log("message") will write to log file.
"""
def __init__(self, config):
"""
Initialises the logger based on the passed configuration:
:params: config - a dictionary containing configuration
parameters. If dictionary contains no "logger" entry then
an empty logger is created and subsequent calls to log() will
have no effect. Otherwise a logger is created according to the
entries in the logger config dictionary. ("log file name",
"overwrite existing"
:raises: IOError if the user can't write to the named log file?
"""
self._no_logging = True
log_config = config.get("logger")
if log_config is not None:
self._logger = getLogger("sksevaluation")
log_file_name = log_config.get("log file name",
"sks_evaluation.log")
overwrite = log_config.get("overwrite existing", False)
mode = 'a'
if overwrite:
mode = 'w'
file_handler = FileHandler(log_file_name, mode)
formatter = Formatter('%(asctime)s - %(name)s -' +
' %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
self._logger.addHandler(file_handler)
self._logger.setLevel(INFO)
self._no_logging = False
def log(self, message):
"""If logging, passes message to logger"""
if self._no_logging:
return
self._logger.info(message)
def __del__(self):
"""Releases the log file"""
if not self._no_logging:
self._logger.handlers[0].flush()
self._logger.handlers[0].close()
......@@ -9,6 +9,7 @@ from sksurgeryeval.algorithms.background_image import \
OverlayBackground
from sksurgeryeval.shapes.cone import VTKConeModel
from sksurgeryeval.algorithms.locators import Locators
from sksurgeryeval.logging.surgery_logger import Logger
class OverlayApp(OverlayBaseApp):
......@@ -26,6 +27,7 @@ class OverlayApp(OverlayBaseApp):
self.timer = None
self.save_frame = None
self._logger = Logger(config)
if "logo" in config:
self.bg_image = OverlayBackground(config)
else:
......@@ -61,6 +63,8 @@ class OverlayApp(OverlayBaseApp):
self.vtk_overlay_window.add_vtk_actor(self._locator._text.text_actor)
self._logger.log(message="OverlayApp init OK")
def update(self):
"""Update the background renderer with a new frame,
......@@ -88,4 +92,8 @@ class OverlayApp(OverlayBaseApp):
if not isnan(quality[ph_index]):
self._pointer.actor.SetUserMatrix(np2vtk(tracking[ph_index]))
self._locator.is_hit(tracking[ph_index])
self._locator.is_hit(tracking[ph_index], self._logger)
def __del__(self):
self._logger.log(message="Closing overlay app")
del self._logger
# coding=utf-8
"""scikit-surgery-evaluation tests"""
from os import path
from sksurgeryeval.logging.surgery_logger import Logger
def test_empty_config():
"""
Test that the app runs
"""
config = {
"tracker type" : "aruco",
"video source" : "data/aruco_tag.avi"
}
logger = Logger(config)
logger.log(message="testing")
del logger
def test_non_empty_config():
"""
Test that the app runs
"""
config = {
"logger" : {}
}
logger = Logger(config)
logger.log(message="testing")
assert path.exists("sks_evaluation.log")
del logger
def test_overwrite():
"""
Test that overwrite works
"""
config = {
"logger" : {
"log file name" : "testing_log_file.log",
"overwrite existing" : True
}
}
logger = Logger(config)
logger.log(message="testing")
assert path.exists("testing_log_file.log")
del logger
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment