Skip to content

Commit

Permalink
improve: log format
Browse files Browse the repository at this point in the history
fix: 3 log file issue
cleanup
  • Loading branch information
MarcelRosier committed Apr 16, 2024
1 parent 543a49f commit 71bc3ff
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ereg/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def register(
log_file = self._get_log_file(output_image, log_file)
self._set_log_file(log_file)

logger.info(f"{'register: Starting registration':=^100}")

logger.info(f"Target image: {target_image}, Moving image: {moving_image}")
target_image = read_image_and_cast_to_32bit_float(target_image)
moving_image = read_image_and_cast_to_32bit_float(moving_image)
Expand All @@ -158,8 +160,11 @@ def register(
try:
self.transform = sitk.ReadTransform(transform_file)
compute_transform = False
logger.info("Specified transform file already exists.")
except:
logger.info("Could not read transform file. Computing transform.")
logger.warning(
"Could not read transform file. Computing transform."
)
pass
if compute_transform:
logger.info(f"Starting registration with parameters:: {self.parameters}")
Expand Down Expand Up @@ -195,15 +200,13 @@ def register(

self.transform = current_transform

# no need for logging since resample_image will log by itself
logging.shutdown()

# resample the moving image to the target image
self.resample_image(
target_image=target_image,
moving_image=moving_image,
output_image=output_image,
transform_file=transform_file,
log_file=log_file,
)

def resample_image(
Expand All @@ -219,15 +222,17 @@ def resample_image(
Resample the moving image to the target image.
Args:
logger (logging.Logger): The logger to use.
target_image (Union[str, sitk.Image]): The target image.
moving_image (Union[str, sitk.Image]): The moving image.
output_image (str): The output image.
transform_file (str, optional): The transform file. Defaults to None.
"""

log_file = self._get_log_file(output_image, log_file)
self._set_log_file(log_file)

logger.info(f"{'resample_image: Starting transformation':=^100}")

# check if output image exists
if not os.path.exists(output_image):
if transform_file is not None:
Expand All @@ -240,7 +245,6 @@ def resample_image(
), "Transform could not be read."
except Exception as e:
logger.error(f"Could not read transform file: {e}")
logging.shutdown()
return None

logger.info(
Expand All @@ -265,7 +269,6 @@ def resample_image(
logger.info(
f"SSIM score of moving against target image: {self.ssim_score}"
)
logging.shutdown()

return self.ssim_score

Expand Down Expand Up @@ -797,7 +800,7 @@ def _register_image_and_get_transform(

def _setup_logger(self):
logging.basicConfig(
format="%(asctime)s,%(name)s,%(levelname)s,%(message)s",
format="[%(levelname)-8s | %(module)-15s | L%(lineno)-5d] | %(asctime)s: %(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
Expand All @@ -819,7 +822,7 @@ def _set_log_file(self, log_file: str | Path) -> None:
self.log_file_handler = logging.FileHandler(log_file)
self.log_file_handler.setFormatter(
logging.Formatter(
"%(asctime)s,%(name)s,%(levelname)s,%(message)s",
"[%(levelname)-8s | %(module)-15s | L%(lineno)-5d] | %(asctime)s: %(message)s",
"%Y-%m-%dT%H:%M:%S%z",
)
)
Expand Down

0 comments on commit 71bc3ff

Please sign in to comment.