Skip to content

ldctbench.utils.test_utils

add_crop(ax, image, coords, cropsize, vmin=0.0, vmax=1.0, edgecolor=(1.0, 0.0, 0.0), pos_crop=(0.0, 0.0), lw_crop=1.0, lw_overlay=1.0, **kwargs)

Function to add crop to an existing matplotlib axis

Parameters:

  • ax (Axes) –

    Axis to add plot to

  • image (array) –

    2-dim array containing the (uncropped) image

  • coords (Tuple[int, int]) –

    Center coordinates of the crop

  • cropsize (int) –

    Cropsize in pixels

  • vmin (float, default: 0.0 ) –

    vmin to use for the cropped image, by default 0.0

  • vmax (float, default: 1.0 ) –

    vmax to use for the cropped image, by default 1.0

  • zoom (float) –

    Zoom of the cropped image, by default 1.0

  • edgecolor (Tuple[float], default: (1.0, 0.0, 0.0) ) –

    Edgecolor around crop and bbox, by default (1.,0.,0.)

  • pos_crop (Tuple[int, int], default: (0.0, 0.0) ) –

    Position (in pixels) to place crop on the axis, by default (0., 0.)

  • lw_crop (float, default: 1.0 ) –

    Linewidth of border around crop, by default 1.

  • lw_overlay (float, default: 1.0 ) –

    Linewidth of border in uncropped image on axis, by default 1.

  • kwargs

    (e.g., cmap, zoom)

Examples:

>>> im = np.random.uniform(low=24.0, high=4096.0, size=(128, 128))
>>> fig, ax = plt.subplots()
>>> ax.imshow(im, vmin=0., vmax=2000., cmap="gray")
>>> add_crop(ax, im, coords=(64,64), cropsize=32, vmin=0., vmax=2000., cmap='gray', pos_crop=(0,0))
>>> plt.show()

apply_center_width(x, center, width, out_range=(0, 1))

Apply some center and width to an image

Parameters:

  • x (ndarray) –

    Image array of arbitrary dimension

  • center (float) –

    Float indicating the center

  • width (float) –

    Float indicating the width

  • out_range (Tuple, default: (0, 1) ) –

    Desired output range, by default (0, 1)

Returns:

  • ndarray

    Copy of input array with center and width applied

compute_metric(targets, predictions, metrics, denormalize_fn=None, exam_type=None)

Compute metrics for given predictions and targets

Parameters:

  • targets (Union[Tensor, ndarray]) –

    Tensor or ndarray of shape (mbs, 1, H, W) holding ground truth

  • predictions (Union[Tensor, ndarray]) –

    Tensor or ndarray of shape (mbs, 1, H, W) holding predictions

  • metrics (List[str]) –

    List of metrics must be "vif" | "psnr" | "rmse" | "ssim"

  • denormalize_fn (Optional[Callable], default: None ) –

    Function to use for denormalizing, by default None

  • exam_type (Optional[str], default: None ) –

    Exam type (for computing SSIM and PSNR on windowed images), by default None

Returns:

  • Dict[str, List]

    Dictionary containing for each metric a list of len(mbs)

Raises:

  • ValueError

    If predictions.shape != targets.shape

  • ValueError

    If element in metric is not "vif" | "psnr" | "rmse" | "ssim"

denormalize(x, method=None, normalization=None)

Denormalize tensor or ndarray based on normalization type of trained model.

Parameters:

  • x (Union[Tensor, ndarray]) –

    Tensor or ndarray to normalize

  • method (Union[Literal[RESNET, CNN10, DUGAN, QAE, REDCNN, TRANSCT, WGANVGG, BILATERAL], str, None], default: None ) –

    Enum item or string, specifying model to dermine which normalization to use. See ldctbench.hub.methods.Methods for more info, by default None

  • normalization (Optional[str], default: None ) –

    Normalization method, must be meanstd | minmax, by default None

Returns:

  • Union[Tensor, ndarray]

    Normalized tensor or ndarray

Raises:

  • ValueError

    If normalization is neither "meanstd" nor "minmax"

normalize(x, method=None, normalization=None)

Normalize tensor or ndarray based on normalization type of trained model.

Parameters:

  • x (Union[Tensor, ndarray]) –

    Tensor or ndarray to normalize

  • method (Union[Literal[RESNET, CNN10, DUGAN, QAE, REDCNN, TRANSCT, WGANVGG, BILATERAL], str, None], default: None ) –

    Enum item or string, specifying model to dermine which normalization to use. See ldctbench.hub.methods.Methods for more info, by default None

  • normalization (Optional[str], default: None ) –

    Normalization method, must be meanstd | minmax, by default None

Returns:

  • Union[Tensor, ndarray]

    Normalized tensor or ndarray

Raises:

  • ValueError

    If normalization is neither "meanstd" nor "minmax"

preprocess(x, **normalization_kwargs)

Preprocess input tensor

Parameters:

  • x (Union[Tensor, ndarray]) –

    Input tensor or ndarray

Returns:

  • Tensor

    Preprocessed tensor

setup_trained_model(run_name, device=torch.device('cuda'), network_name='Model', state_dict=None, return_args=False, return_model=True, eval=True, **model_kwargs)

Setup a trained model with run in ./wandb

Parameters:

  • run_name (str) –

    Name of run (is the same as foldername)

  • device (device, default: device('cuda') ) –

    Device to move model to, by default torch.device("cuda")

  • network_name (str, default: 'Model' ) –

    Class name of network, by default "Model"

  • state_dict (str, default: None ) –

    Name of state_dict. If None, model is initialized with random parameters, by default None

  • return_args (bool, default: False ) –

    Return args of training run, by default False

  • return_model (bool, default: True ) –

    Return model, by default True

  • eval (bool, default: True ) –

    Set model to eval mode, by default True

Returns:

  • Union[Tuple[Module, Namespace], Module, Namespace]

    Either model, args or model and args of training run

vif(x, y, sigma_n_sq=2.0)

Compute visual information fidelity