torchvtk.utils

make_nd

torchvtk.utils.make_2d(t)
torchvtk.utils.make_3d(t)
torchvtk.utils.make_4d(t)

Prepends singleton dimensions to t until 4D

torchvtk.utils.make_5d(t)

Prepends singleton dimensions to t until 5D

torchvtk.utils.make_nd(t, n)

Prepends singleton dimensions to t until n-dimensional

normalize_hounsfield

torchvtk.utils.normalize_hounsfield(vol, dtype=None)

Normalizes vol by 4095 and clamps to [0,1]. dtype=None defaults to 32-bit float

normalize_voxel_scale

torchvtk.utils.normalize_voxel_scale(vol, vox_scl)

TransferFunctionApplication

class torchvtk.utils.TransferFunctionApplication(as_pts=False)

Bases: torch.nn.Module

__init__(as_pts=False)

A torch.nn.Module that applies a transfer function differentiably

Parameters

as_pts (bool) – Wether the given TF will be in point form (True) or 1D texture (False).

forward(x, tf)

Applies the transfer function tf to the volume x

Parameters
  • x (torch.Tensor) – Tensor depicting a volume with intensity values (N, 1, D, H, W)

  • tf (torch.Tensor, list of such) – If as_pts a list of Tensors (NumPoints, Channels) is expected with len(list) == batch_size. If False a torch.Tensor (N, Channels, TF_resolution).

Returns

Tensor with the TF applied. New shape (N, Channels, D, H, W)

Return type

(torch.Tensor)

tex_from_pts

torchvtk.utils.tex_from_pts(tf_pts, resolution=4096)

Interpolates tf_pts to generate a TF texture of shape (N, C, resolution) with C determined by the TF

apply_tf_torch

torchvtk.utils.apply_tf_torch(x, tf_pts)

Applies the TF described by points tf_pts (N x [0,1]^C+1 with x pos and C channels) to x. The operation always computes on torch.float32. The output is cast to x.dtype

Parameters
  • x (torch.Tensor) – The intensity values to apply the TF on. Assumed shape is ([N,] 1, …) (optionally with batch size N, must match length of tf_pts list)

  • tf_pts (torch.Tensor, List of such) – Tensor of shape (N, (1+C)) containing N points consisting of x coordinate and mapped features (e.g. RGBO)

Returns

Tensor with TF applied of shape (N, C, …) with batch size N (same as x) and number of channels C (same as tf_pts)

Return type

torch.Tensor

apply_tf_tex_torch

torchvtk.utils.apply_tf_tex_torch(vol, tf_tex)

Applies a (batch of) transfer function textures tf_tex to a (batch of) volume vol

Parameters
  • vol (torch.Tensor) – ([N,] 1, D, H, W) volume with intensity values

  • tf_tex (torch.Tensor) – ([N,] C, R) transfer function texture with C channels and resolution R

Returns

The preclassified volume of shape ([N,] C, D, H, W)

random_tf_from_vol

torchvtk.utils.random_tf_from_vol(vol, colors='random', max_num_peaks=5, height_range=(0.1, 0.7), width_range=(0.02, 0.3), peak_center_noise_std=0.05, bins=1024, valid_fn=None, use_hist=True, fixed_shape=False, override_peaks=None)

pool_map

torchvtk.utils.pool_map(fn, data, num_workers=0, dlen=None, title=None)

Multithreaded map function that displays a progress bar

Parameters
  • fn (function) – Function to be applied to the elements in data

  • data (iterable) – Iterable on which the function fn is applied.

  • num_workers (int) – Number of threads to do the computation

  • dlen (int) – A way to supply the length of data separately (to display in progress bar)

  • title (str) – Title to be displayed next to the progress bar

Returns

A list of results [fn(data[0]), …. fn(data[-1])]

pool_map_uo

torchvtk.utils.pool_map_uo(fn, data, num_workers=0, dlen=None)

Multithreaded unordered map function that displays a progress bar

Parameters
  • fn (function) – Function to be applied to the elements in data

  • data (iterable) – Iterable on which the function fn is applied.

  • num_workers (int) – Number of threads to do the computation

  • dlen (int) – A way to supply the length of data separately (to display in progress bar)

Returns

A list of results [fn(data[0]), …. fn(data[-1])]