pytomography.priors.relative_difference - PyTomography 0.21 documentation

Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# `pytomography.priors.relative_difference`

## Module Contents

### Classes

`DiffAndSumSmoothnessPrior` | Implementation of priors where gradients depend on difference and the sum of neighbouring voxels: \(\frac{\partial V}{\partial f_r}=\beta\sum_{r,s}w_{r,s}\phi(f_r-f_s, f_r+f_s)\) where \(V\) is from the log-posterior probability \(\ln L (\tilde{f}, f) - \beta V(f)\).
`RelativeDifferencePrior` | Subclass of SmoothnessPrior where \(\phi(f_r-f_s,f_r+f_s) = \frac{4(f_r-f_s)(f_r+f_s)}{((f_r+f_s)+\gamma|f_r-f_s|)^2}\) corresponds to the Relative Difference Prior \(V(f) = \sum_{r,s} w_{r,s} \frac{(f_r-f_s)^2}{(f_r+f_s)+\gamma|f_r-f_s|}\)

class pytomography.priors.relative_difference.DiffAndSumSmoothnessPrior(beta, phi, device=None, **kwargs)

  Bases: `pytomography.priors.prior.Prior`

  Implementation of priors where gradients depend on difference and the sum of neighbouring voxels: \(\frac{\partial V}{\partial f_r}=\beta\sum_{r,s}w_{r,s}\phi(f_r-f_s, f_r+f_s)\) where \(V\) is from the log-posterior probability \(\ln L (\tilde{f}, f) - \beta V(f)\).

  Parameters:

    - beta (float) – Used to scale the weight of the prior

    - phi (function) – Function $phi$ used in formula above

    - device (str, optional) – Pytorch device used for computation. Defaults to ‘cpu’.

  get_kernel(sign=1)

    Obtains the kernel used to get \(\frac{\partial V}{\partial f_r}\) (this is an array with the same dimensions as the object space image)

    Parameters:

      sign (float) – Kernel computes image \(f_r + \text{sign} \cdot f_k\) for all 26 nearest neighbours \(k\) (i.e. a 3D image is returned with 26 channels). Defaults to 1.

    Returns:

      Kernel used for convolution (number of output channels equal to number of \(s\)), and array of weights \(w_s\) used in expression for gradient.

    Return type:

      (torch.nn.Conv3d, torch.tensor)

  set_kernel(object_meta)

    Sets the kernel using get_kernel and the corresponding object metadata.

    Parameters:

      object_meta (ObjectMeta) – Metadata for object space.

    Return type:

      None

  forward()

    Computes the prior on `self.object`

    Returns:

      Tensor of shape [batch_size, Lx, Ly, Lz] representing \(\frac{\partial V}{\partial f_r}\)

    Return type:

      torch.tensor

class pytomography.priors.relative_difference.RelativeDifferencePrior(beta=1, gamma=1, device=None)

  Bases: `DiffAndSumSmoothnessPrior`

  Subclass of SmoothnessPrior where \(\phi(f_r-f_s,f_r+f_s) = \frac{4(f_r-f_s)(f_r+f_s)}{((f_r+f_s)+\gamma|f_r-f_s|)^2}\) corresponds to the Relative Difference Prior \(V(f) = \sum_{r,s} w_{r,s} \frac{(f_r-f_s)^2}{(f_r+f_s)+\gamma|f_r-f_s|}\)

  Parameters:

    - beta (float) – Used to scale the weight of the prior

    - phi (function) – Function $phi$ used in formula above

    - gamma (float, optional) – Parameter $gamma$ in equation above. Defaults to 1.

    - device (str, optional) – Pytorch device used for computation. Defaults to ‘cpu’.

  gradient(sum, diff, gamma, eps=1e-11)

    Gradient function.

    Parameters:

      - sum (torch.Tensor) – tensor of size [batch_size,Lx,Ly,Lz] representing \(f_r+f_s\)

      - diff (torch.Tensor) – tensor of size [batch_size,Lx,Ly,Lz] representing \(f_r-f_s\)

      - gamma (torch.Tensor) – hyperparameter \(\gamma\)

      - eps (float, optional) – Used to prevent division by 0. Defaults to 1e-11.

    Returns:

      Returns \(\frac{(f_r-f_s)^2}{(f_r+f_s)+\gamma|f_r-f_s|}\) for a given \(r\) and \(s\).

    Return type:

      torch.Tensor