synthbold.base¶
Abstract base classes and mixins for the synthBOLD synthesis pipeline.
- class synthbold.base.BaseGeometry(shape: tuple[int, ...], *, device: str | device = 'cpu', seed: int | None = None)¶
Bases:
ABC,RandomGeneratorMixinAbstract base for creating basic geometries.
Subclasses implement
forward()to produce a single sample. This class provides the batch loop (__call__), optional save-to-disk (ZARR or NIfTI), cached coordinate grids, and serialisation of metadata viaattrs.- Parameters:
shape – Spatial dimensions
(X, Y, Z)of each generated volume.device – PyTorch device for tensor allocation and RNG.
seed – Integer seed for reproducible output;
Nonefor random.
- property attrs: dict[str, Any]¶
Metadata for ZARR/NIfTI storage.
- dtype = torch.float32¶
- abstractmethod forward() Tensor¶
Generate random geometry labels.
- abstractmethod classmethod from_config(config: Config) Self¶
Constructs a geometry instance from a configuration object.
- property normalized_grid: Tensor¶
Grid normalized to [-1, 1] of shape
(X, Y, Z, 3).
- property voxel_grid: Tensor¶
Creates target mesh grid of shape
(X, Y, Z, 3).
- class synthbold.base.Model(*, device: str = 'cpu', seed: int | None = None)¶
Bases:
ABC,RandomGeneratorMixinAbstract base class for making single models callable.
- Parameters:
device – PyTorch device for tensor allocation and RNG.
seed – Integer seed for reproducible output;
Nonefor random.
- property attrs: dict[str, Any]¶
Common metadata for ZARR/NIfTI storage.
- abstractmethod forward(data: Tensor) Tensor¶
Generate data.
Note
Call the instance (
model(data)) instead ofmodel.forward(data). In this implementation,__call__transfers inputs toself.devicebefore invokingforward.
- class synthbold.base.ObjectGeometry(shape: tuple[int, int, int] = (128, 128, 128), fov: tuple[float, float, float] = (1.0, 1.0, 1.0), num_objects: int | None = None, vf_range: tuple[float, float] | None = (0.05, 0.1), diameter_range: tuple[float, float] = (0.05, 2.0), allow_overlap: bool = True, device: str = 'cpu', seed: int | None = None)¶
Bases:
BaseGeometry,ABCAbstract base for creating basic objects.
- Parameters:
shape – Spatial dimensions
(X, Y, Z)of each generated volume.fov – Field of view in mm, used to compute voxel size.
num_objects – Fixed number of objects to generate. Mutually exclusive with
vf_range.
vf_range – Range of target volume fractions. Objects are added until the volume fraction is reached. Mutually exclusive with num_objects.
diameter_range – Range of object diameters in mm.
allow_overlap – If False, objects are only added if they contribute new voxels.
device – PyTorch device for tensor allocation and RNG.
seed – Integer seed for reproducible output;
Nonefor random.
- Raises:
ValueError – If neither or both of num_objects and vf_range are given.
Notes
num_objects and vf_range are mutually exclusive. If num_objects is set, objects are added iteratively until the specified number is reached. If vf_range is set, a target volume fraction is randomly sampled from the specified range and objects are added iteratively until the target fraction of occupied voxels is reached.
- property attrs: dict[str, Any]¶
Metadata for ZARR/NIfTI storage.
- forward() Tensor¶
Generate a labeled 3D volume with random objects.
- class synthbold.base.RandomGeneratorMixin(seed: int | None, device: str | device = 'cpu', **kwargs: Any)¶
Bases:
objectMixin that provides a paired PyTorch and NumPy RNG, both seedable for reproducibility, for device-aware random sampling.
- Parameters:
seed – Integer seed for deterministic output. Pass
Nonefor non-deterministic behaviour.device – PyTorch device on which
generatoris created.
- class synthbold.base.Transform(device: str | device, seed: int | None = None)¶
Bases:
ABC,RandomGeneratorMixinBase class for single transformations on PyTorch tensors.
This abstract class provides the foundation for all data transformations. Subclasses are required to implement sample, apply, and from_config. The base implementation automatically handles moving input data to the target device and sequencing the sampling and application steps.
- Parameters:
device – PyTorch device for tensor allocation and RNG.
seed – Integer seed for reproducible output;
Nonefor random.
- abstractmethod static apply(x: Tensor, transform: Tensor) Tensor¶
Apply pre-sampled transform parameters to the input tensor.
- forward(data: Tensor) Tensor¶
Generates transformation and applies to input tensor.
Note
Call the instance (
transform(data)) instead oftransform.forward(data). In this implementation,__call__transfers inputs toself.devicebefore invokingforward.
- abstractmethod classmethod from_config(config: Config) Self¶
Constructs a Transform instance from a configuration object.
- abstractmethod sample(shape: tuple[int, ...]) Tensor¶
Sample transform parameters for a tensor of the given shape.