diff --git a/mu_map/vis/alignment.py b/mu_map/vis/alignment.py index 7cdebefe6c34ff99c0692a6433ad1e38501f2871..473b1de5f46a8b6af44990d8ce0b92d011dc8baf 100644 --- a/mu_map/vis/alignment.py +++ b/mu_map/vis/alignment.py @@ -1,3 +1,7 @@ +""" +Visualize different forms of alignment of images with a varying +numbers of slices. +""" import argparse import math import os diff --git a/mu_map/vis/hist2d.py b/mu_map/vis/hist2d.py deleted file mode 100644 index dc65d28a490a48d028e4e5494944c4aa0aaf6e5c..0000000000000000000000000000000000000000 --- a/mu_map/vis/hist2d.py +++ /dev/null @@ -1,40 +0,0 @@ -import cv2 as cv -import matplotlib.pyplot as plt -import numpy as np -import pydicom - -from mu_map.dataset.util import align_images -from mu_map.file.dicom import load_dcm_img -from mu_map.util import to_grayscale - -mu_map = load_dcm_img("./data/second/images/0085-stress-mu_map.dcm") -# mu_map_syn = load_dcm_img("./tmp/0085-stress-mu_map-dl.dcm") -mu_map_syn = load_dcm_img("./data/second/images/0084-stress-mu_map.dcm") - -mu_map, mu_map_syn = align_images(mu_map, mu_map_syn) - -_max = max(mu_map.max(), mu_map_syn.max()) -_mu_map = to_grayscale(mu_map[mu_map.shape[0] // 2], max_val=_max, min_val=0) -_mu_map_syn = to_grayscale(mu_map_syn[mu_map_syn.shape[0] // 2], max_val=_max, min_val=0) - -_mu_map = cv.resize(_mu_map, (512, 512)) -_mu_map_syn = cv.resize(_mu_map_syn, (512, 512)) -s = np.full((512, 10), 239, np.uint8) - -# cv.imshow("Test", np.hstack((_mu_map, s, _mu_map_syn))) -# cv.waitKey(0) - -fig, axs = plt.subplots(1, 2) -for ax in axs: - ax.set_xlabel(r"μ-Map CT (cm$^{-1}$)") - ax.set_ylabel(r"μ-Map DL (cm$^{-1}$)") - -hist, _, _, _ = axs[0].hist2d(mu_map.flatten(), mu_map_syn.flatten(), bins=[100, 100], range=((0.0, 0.2), (0.0, 0.2))) -hist[0, 0] = 0 -axs[1].imshow(hist, origin="lower") - -plt.tight_layout() -plt.show() - - - diff --git a/mu_map/vis/loss_curve.py b/mu_map/vis/loss_curve.py index b0ae6c71e326e05d24ff09f7104941686e9d3cc4..b6e839e12377c1a61b21a372084c29aaf8dbd4e1 100644 --- a/mu_map/vis/loss_curve.py +++ b/mu_map/vis/loss_curve.py @@ -1,16 +1,37 @@ +""" +Visualize a loss curve from logs of a training. +""" import argparse +from typing import List +import matplotlib as mlp import matplotlib.pyplot as plt import numpy as np -from mu_map.logging import parse_file +from mu_map.logging import parse_file, LogLine # https://colorbrewer2.org/#type=diverging&scheme=RdBu&n=3lk COLORS = ["#ef8a62", "#67a9cf"] -def parse_loss(logs, loss_type): +def parse_loss(logs: List[LogLine], loss_type: str) -> np.ndarray: + """ + Parse losses from a list of log lines. + + Parameters + ---------- + logs: list of LogLine + the logs to be parsed + loss_type: str + identifier of the loss (e.g., "train" or "validation") + + Returns + ------- + np.ndarray + array with the shape (2, n) where n is the number of losses, + the first index is either the epoch or the loss + """ _logs = map(lambda logline: logline.message, logs) _logs = filter(lambda log: loss_type in log, _logs) _logs = filter(lambda log: "Loss" in log, _logs) @@ -29,7 +50,35 @@ def parse_loss(logs, loss_type): return np.array(list(epochs)), np.array(list(losses)) -def plot_loss(logfile, loss_types, ax, normalize=False, from_epoch=0, to_epoch=None, start_idx_message=3): +def plot_loss( + logfile: str, + loss_types: List[str], + ax: mpl.axes.Axes, + normalize: bool = False, + from_epoch: int = 0, + to_epoch: int = None, + start_idx_message: int = 3, +): + """ + Plot a loss curve from a log file. + + Parameters + ---------- + logfile: str + the log file + loss_types: list of str + the names of the losses to plot (e.g. "train", "validation") + ax: mlp.axes.Axes + the axes to plot on + normalize: bool, optional + if the losses should be normalized to [0, 1] + from_epoch: int, optional + set the starting epoch of the plot + to_epoch: int, optional + the epoch at which the plot ends + start_idx_message: int, optional + see `mu_map.logging.parse_file` + """ logs = parse_file(logfile, start_idx_message=start_idx_message) logs = list(filter(lambda logline: logline.loglevel == "INFO", logs)) diff --git a/mu_map/vis/slices.py b/mu_map/vis/slices.py index 73d02fc3eec6c3c4bbe621e6588b700ba3bdaab0..75f05027201eb5ab3a13cca1ebf51da639959b26 100644 --- a/mu_map/vis/slices.py +++ b/mu_map/vis/slices.py @@ -1,3 +1,7 @@ +""" +Visualize 3D images slice by slice. +Multiple images can be visualized side by side. +""" from typing import List, Optional import numpy as np