ncalab.utils

Attributes

DEFAULT_RANDOM_SEED

Functions

get_compute_device(→ torch.device)

Obtain a pytorch compute device handle based on input string.

pad_input(→ torch.Tensor)

Pads the BCWH input tensor along its channel dimension to match the expected number of

print_NCALab_banner()

Show NCALab banner on terminal.

print_mascot(message)

Show help text in a speech bubble.

fix_random_seed([seed])

Fixes the random seed for all pseudo-random number generators,

unwrap(x)

Panics if x is None, otherwise returns x.

intepret_range_parameter(→ int)

Interpret a range parameter that is passed for NCA timesteps.

Module Contents

ncalab.utils.get_compute_device(device: str = 'cuda:0') torch.device

Obtain a pytorch compute device handle based on input string. If user tries to get a CUDA device, but none is available, defaults to CPU.

Parameters:

device (str) – Device string, defaults to “cuda:0”.

Returns:

Pytorch compute device.

Return type:

torch.device

ncalab.utils.pad_input(x: torch.Tensor, nca: ncalab.models.AbstractNCAModel, noise: bool = True, mean: float = 0.5, std: float = 0.225) torch.Tensor

Pads the BCWH input tensor along its channel dimension to match the expected number of channels required by the NCA model. Pads with either Gaussian noise (parameterized by mean and std) or zeros, depending on the “noise” parameter.

Parameters:
  • x (torch.Tensor) – Input image tensor, BCWH.

  • nca (ncalab.BasicNCAModel) – NCA model definition.

  • noise (bool, optional) – Whether to pad with noise. Otherwise zeros, defaults to True.

  • mean (float, optional) – Mean (mu) of Gaussian noise distribution, defaults to 0.5.

  • std (float, optional) – Standard deviation (sigma) of Gaussian noise distribution, defaults to 0.225.

Returns:

Input tensor, BCWH, padded along the channel dimension.

Return type:

torch.Tensor

ncalab.utils.print_NCALab_banner()

Show NCALab banner on terminal.

ncalab.utils.print_mascot(message: str)

Show help text in a speech bubble.

Parameters:

message (str) – Message to display.

ncalab.utils.DEFAULT_RANDOM_SEED = 1337
ncalab.utils.fix_random_seed(seed: int = DEFAULT_RANDOM_SEED)

Fixes the random seed for all pseudo-random number generators, including Python-native, Numpy and Pytorch.

Parameters:

seed (int, optional) – Random seed, defaults to DEFAULT_RANDOM_SEED.

ncalab.utils.unwrap(x: Any)

Panics if x is None, otherwise returns x.

This is a useful shorthand for cases such as x = unwrap(some_object).do_something() in which we are 99% certain that some_object is not None and want to avoid a mypy complaint.

Parameters:

x (Any) – Any kind of object.

Raises:

RuntimeError – If x is None.

Returns:

Just passes through the input x if it is not None.

ncalab.utils.intepret_range_parameter(x: int | Tuple[int, int]) int

Interpret a range parameter that is passed for NCA timesteps.

If the parameter is a single int, just return it as is. If the parameter is a two-valued tuple, interpret it as a [min,max) and randomly sample from that range.

Parameters:

x (int | Tuple[int, int]) – _description_

Raises:

TypeError – If something else than an int or a tuple was passed.

Returns:

_description_

Return type:

int