Skip to content

cornucopia.intensity

This module contains transforms that operate on image intensities.

OpConstTransform

OpConstTransform(value, op=None, value_name='value', **kwargs)

Bases: FinalTransform

Base class for arithmetic operations with a constant value

Parameters:

Name Type Description Default
value number or tensor

right-hand side of the operation

required
op (add, mul)

Arithmetic operation

torch.add
value_name str

Name used when returning the rhs value

'value'

AddValueTransform

AddValueTransform(value, op=None, value_name='value', **kwargs)

Bases: OpConstTransform

Add a constant value

MulValueTransform

MulValueTransform(value, op=None, value_name='value', **kwargs)

Bases: OpConstTransform

Multiply with a constant value

FillValueTransform

FillValueTransform(mask, value, mask_name='mask', value_name='value', **kwargs)

Bases: FinalTransform

Fills the tensor with a value inside a mask

Parameters:

Name Type Description Default
mask tensor

Mask of voxels in which to set the value

required
value number or tensor

right-hand side of the operation

required
mask_name str

Name used when returning the mask

'mask'
value_name str

Name used when returning the rhs value

'value'

ReturnValueTransform

ReturnValueTransform(value, value_name='output', dtype=None, **kwargs)

Bases: FinalTransform

Fills the tensor with a value inside a mask

Parameters:

Name Type Description Default
value number or tensor

right-hand side of the operation

required
value_name str

Name used when returning the rhs value

'output'

AddMulTransform

AddMulTransform(slope=1, offset=0, **kwargs)

Bases: FinalTransform

Constant intensity affine transform: y = x * slope + offset

Parameters:

Name Type Description Default
slope number or tensor

Affine slope

1
offset number or tensor

Affine offset

0

ClipTransform

ClipTransform(vmin=None, vmax=None, **kwargs)

Bases: FinalTransform

Clip extremum values

Parameters:

Name Type Description Default
vmin number or tensor

Min value

None
vmax number or tensor

Max value

None

RandomMulTransform

RandomMulTransform(value=(0.5, 2), *, shared=False, **kwargs)

Bases: RandomizedTransform

Random multiplicative transform.

Parameters:

Name Type Description Default
value Sampler | [pair of] float

Bound for multiplicative value

(0.5, 2)
shared (channels, tensors, channels + tensors, '')

Apply same transform to all images/channels

'channels'

Final class-attribute instance-attribute

Final = MulValueTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = MulValueTransform

The transform type returned by make_final.

RandomAddTransform

RandomAddTransform(value=1, *, shared=False, **kwargs)

Bases: RandomizedTransform

Random additive transform.

Parameters:

Name Type Description Default
value Sampler | [pair of] float

Bound for additive value

1
shared (channels, tensors, channels + tensors, '')

Apply same transform to all images/channels

'channels'

Final class-attribute instance-attribute

Final = AddValueTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddValueTransform

The transform type returned by make_final.

RandomAddMulTransform

RandomAddMulTransform(slope=1, offset=0.5, *, shared=False, **kwargs)

Bases: RandomizedTransform

Random intensity affine transform.

Parameters:

Name Type Description Default
slope Sampler | [pair of] float

Bound for slope

1
offset Sampler | [pair of] float

Bound for offset

0.5
shared (channels, tensors, channels + tensors, '')

Apply same transform to all images/channels

'channels'

Final class-attribute instance-attribute

Final = AddMulTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddMulTransform

The transform type returned by make_final.

SplineUpsampleTransform

SplineUpsampleTransform(order=3, prefilter=False, **kwargs)

Bases: FinalTransform

Upsample a field using spline interpolation

Parameters:

Name Type Description Default
order int

Spline interpolation order

3
prefilter bool

Spline prefiltering (True for interpolation, False for spline evaluation)

False

BaseFieldTransform

BaseFieldTransform(shape=5, vmin=0, vmax=1, order=3, slice=None, thickness=None, *, shared=False, **kwargs)

Bases: NonFinalTransform

Base class for transforms that sample a smooth field

Parameters:

Name Type Description Default
shape [list of] int

Number of spline control points

5
vmin float

Minimum value

0
vmax float

Maximum value

1
order int

Spline order

3
slice int

Slice direction, if slicewise.

None
thickness int

Slice thickness, if slicewise. Note that shape will be scaled along the slice direction so that the number of nodes is approximately preserved.

None
returns [list or dict of] {'input', 'output', 'field'}

Which tensor(s) to return

required
shared (channels, tensors, channels + tensors, '')

Apply the same field to all channels

'channels'

Final class-attribute instance-attribute

Final = AddValueTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddValueTransform

The transform type returned by make_final.

make_field

make_field(batch, smallshape, fullshape=None, **backend)

Generate the random coefficients.

Parameters:

Name Type Description Default
batch int

Number of fields to generate

required
smallshape list of int

Number of spline control points

required
fullshape list of int

If given, the coefficients will be upsampled to this shape.

None

Other Parameters:

Name Type Description
dtype dtype

Data type of the generated field.

device device | str

Device on which to generate the field.

Returns:

Name Type Description
field (batch, *smallshape) tensor | (batch, *fullshape) tensor

If fullshape is given, returns the upsampled field of values. Otherise, returns the spline coefficients.

upsample_field

upsample_field(coeff, shape)

Compute the full-sized field from its spline coefficients.

Parameters:

Name Type Description Default
coeff (batch, *smallshape) tensor

Spline coefficients

required
shape list of int

Target shape for the upsampled field

required

Returns:

Name Type Description
field (batch, *shape) tensor

Upsampled field of values

MulFieldTransform

MulFieldTransform(shape=5, vmin=0, vmax=1, order=3, slice=None, thickness=None, *, shared=False, **kwargs)

Bases: BaseFieldTransform

Smooth multiplicative (bias) field

Final class-attribute instance-attribute

Final = MulValueTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = MulValueTransform

The transform type returned by make_final.

RandomMulFieldTransform

RandomMulFieldTransform(shape=8, vmax=1, order=3, symmetric=False, *, shared=False, shared_field=None, **kwargs)

Bases: NonFinalTransform

Random multiplicative bias field transform

Parameters:

Name Type Description Default
shape Sampler | int

Sampler or Upper bound for number of control points

8
vmax Sampler | float

Sampler or Upper bound for maximum value

1
order int

Spline order

3
symmetric bool | float

If a float, the bias field will take values in (symmetric-vmax, symmetric+vmax). If False, it will take values in (0, vmax). If True, it will take values in (1-vmax, 1+vmax).

False

Other Parameters:

Name Type Description
returns [list or dict of] {'input', 'output', 'field'}

Which tensor(s) to return

shared (channels, tensors, channels + tensors, '')

Whether to share random parameters across tensors and/or channels

shared_field (channels, tensors, channels + tensors, '')

Whether to share random field across tensors and/or channels. By default: same as shared

Next class-attribute instance-attribute

Next = MulFieldTransform

The transform type returned by make_final(..., max_depth=1).

Final class-attribute instance-attribute

Final = MulValueTransform

The transform type returned by make_final(..., max_depth=inf).

RandomSlicewiseMulFieldTransform

RandomSlicewiseMulFieldTransform(shape=8, vmax=1, order=3, slice=None, thickness=32, shape_through=None, *, shared=False, shared_field=None, **kwargs)

Bases: NonFinalTransform

Random multiplicative bias field transform, per slice or slab

Parameters:

Name Type Description Default
shape Sampler | int

Sampler or Upper bound for number of control points

8
vmax Sampler | float

Sampler or Upper bound for maximum value

1
order int

Spline order

3
slice int

Slice axis. If None, sample one randomly

None
thickness Union[Sampler, int]

Sampler or Upper bound for slice thickness

32
shape_through Sampler | int

Sampler or Upper bound for number of control points along the slice direction. If None, same as shape.

None

Other Parameters:

Name Type Description
shared (channels, tensors, channels + tensors, '')

Whether to share random parameters across tensors and/or channels

shared_field (channels, tensors, channels + tensors, '')

Whether to share random field across tensors and/or channels. By default: same as shared

Next class-attribute instance-attribute

Next = MulFieldTransform

The transform type returned by make_final(..., max_depth=1).

Final class-attribute instance-attribute

Final = MulValueTransform

The transform type returned by make_final(..., max_depth=inf).

AddFieldTransform

AddFieldTransform(shape=5, vmin=0, vmax=1, order=3, slice=None, thickness=None, *, shared=False, **kwargs)

Bases: BaseFieldTransform

Smooth additive (bias) field

Final class-attribute instance-attribute

Final = AddValueTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddValueTransform

The transform type returned by make_final.

RandomAddFieldTransform

RandomAddFieldTransform(shape=8, vmin=-1, vmax=1, order=3, *, shared=False, shared_field=None, **kwargs)

Bases: NonFinalTransform

Random additive bias field transform

Parameters:

Name Type Description Default
shape Sampler | int

Sampler or Upper bound for number of control points

8
vmin Sampler | float

Sampler or Lower bound for minimum value

-1
vmax Sampler | float

Sampler or Upper bound for maximum value

1
order Sampler | int

Spline order

3

Other Parameters:

Name Type Description
shared (channels, tensors, channels + tensors, '')

Whether to share random parameters across tensors and/or channels

shared_field (channels, tensors, channels + tensors, '')

Whether to share random field across tensors and/or channels. By default: same as shared

GammaFinalTransform

GammaFinalTransform(gamma=1, vmin=0, vmax=1, **kwargs)

Bases: FinalTransform

Gamma correction with fixed parameters.

The transform is defined as:

y = (x-vmin) / (vmax-vmin) ** gamma * (vmax-vmin) + vmin

In this transform, vmin and vmax are pre-calculated and fixed, whereas in GammaTransform, they are computed from the image intensities.

Parameters:

Name Type Description Default
gamma number | (C,) list[number] | (C,) tensor

Exponent of the Gamma transform

1
vmin number | (C,) list[number] | (C,) tensor

Minimum value for the transform

0
vmax number | (C,) list[number] | (C,) tensor

Maximum value for the transform

1

GammaTransform

GammaTransform(gamma=1, vmin=None, vmax=None, *, shared=False, **kwargs)

Bases: NonFinalTransform

Gamma correction

References
  1. https://en.wikipedia.org/wiki/Gamma_correction

Parameters:

Name Type Description Default
gamma float

Exponent of the Gamma transform

1
vmin float | None

Value to use as the minimum (default: x.min())

None
vmax float | None

Value to use as the maximum (default: x.max())

None
returns [list or dict] {'input', 'output', 'vmin', 'vmax', 'gamma'}

Which tensors to return

required

Other Parameters:

Name Type Description
shared (channels, tensors, channels + tensors, '')

Use the same vmin/vmax for all channels

Final class-attribute instance-attribute

Final = GammaFinalTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = GammaFinalTransform

The transform type returned by make_final.

RandomGammaTransform

RandomGammaTransform(gamma=(0.5, 2), *, shared=False, shared_minmax=None, **kwargs)

Bases: NonFinalTransform

Random Gamma transform.

Parameters:

Name Type Description Default
gamma Sampler or [pair of] float

Sampler or range for the exponent value

(0.5, 2)

Other Parameters:

Name Type Description
shared (channels, tensors, channels + tensors, '')

Apply same gamma for all images/channels

shared_minmax (channels, tensors, channels + tensors, '', None)

Use the same vmin/vmax for all channels. Default: same as shared.

Next class-attribute instance-attribute

Next = GammaTransform

The transform type returned by make_final(..., max_depth=1).

Final class-attribute instance-attribute

Final = GammaFinalTransform

The transform type returned by make_final(..., max_depth=inf).

ZTransform

ZTransform(mu=0, sigma=1, *, shared=False, **kwargs)

Bases: NonFinalTransform

Z-transform the data -> zero mean, unit standard deviation

Parameters:

Name Type Description Default
mu float

Target mean. If None, keep the input mean.

0
sigma float

Target standard deviation. If None, keep the input sd.

1

Other Parameters:

Name Type Description
shared (channels, tensors, channels + tensors, '')

Use the same mean/sigma for all images/channels

Final class-attribute instance-attribute

Final = AddMulTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddMulTransform

The transform type returned by make_final.

QuantileTransform

QuantileTransform(pmin=0.01, pmax=0.99, vmin=0, vmax=1, clip=False, max_samples=10000, **kwargs)

Bases: NonFinalTransform

Match lower and upper quantiles to (0, 1)

Parameters:

Name Type Description Default
pmin (0..1)

Lower quantile

0.01
pmax (0..1)

Upper quantile

0.99
vmin float

Lower target value

0
vmax float

Upper target value

1
clip bool

Clip values outside (vmin, vmax)

False
max_samples int

Maximum number of pixels to use for quantile estimation (for speed)

10000

Final class-attribute instance-attribute

Final = AddMulTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddMulTransform

The transform type returned by make_final.

MinMaxTransform

MinMaxTransform(vmin=0, vmax=1, clip=False, **kwargs)

Bases: NonFinalTransform

Match min and max values to (0, 1)

Parameters:

Name Type Description Default
vmin float

Lower target value

0
vmax float

Upper target value

1
clip bool

Clip values outside (vmin, vmax)

False

Final class-attribute instance-attribute

Final = AddMulTransform

The transform type returned by make_final.

Next class-attribute instance-attribute

Next = AddMulTransform

The transform type returned by make_final.