Skip to content

fiery.bounds.types

This module defines names and aliases for different boundary conditions, as well as tools to convert between different naming conventions.

Classes:

Name Description
BoundType

Enum type for bounds

Attributes:

Name Type Description
BoundLike

A type hint for any boundary type

SequenceOrScalar

A type hint for values or sequences of values

Functions:

Name Description
to_enum

Convert any boundary type to BoundType

to_int

Convert any boundary type to BoundType-based integer values

to_fourier

Convert any boundary type to discrete transforms

to_scipy

Convert any boundary type to scipy.ndimage convention

to_torch

Convert any boundary type to torch.grid_sample convention

Attributes

SequenceOrScalar module-attribute

SequenceOrScalar = T | Sequence[T]

Either an element or type T, or a sequence of elements of type T.

BoundLike module-attribute

BoundLike = BoundType | str | int

A boundary mode.

Most conventions are handled (numpy, scipy, torch, see below). Can be one of:

  1. zero, zeros, constant or gridconstant;
  2. replicate, nearest, border or edge;
  3. dct1 or mirror;
  4. dct2, reflect, reflection, gridmirror or neumann;
  5. dst1 or antimirror;
  6. dst2, antireflect or dirichlet;
  7. dft, fft, wrap, gridwrap, circular or circulant.

Each of these modes can be a BoundType value (e.g., BoundType.mirror), or its string representation (e.g., "mirror").

The aliases dft, dct1, dct2, dst1 and dst2 exist because these boundary modes correspond to the implicit boundary conditions of each of these frequency transform:

The reason why so many aliases are supported is that there is no common convention across python packages to name boundary conditions. This table contains an extensive list of aliases:

Fourier

SciPy ndimage

Numpy pad

PyTorch pad

PyTorch grid_sample

Other

Description

nearest

edge

border

replicate

repeat

a a | a b c d | d d

constant,
grid-constant

constant

constant

zeros

zero

0 0 | a b c d | 0 0

dct1

mirror

reflect

reflect

reflection
(False)

c b | a b c d | c b

dct2

reflect,
grid-mirror

symmetric

reflection
(True)

neumann

b a | a b c d | d c

dst1

antimirror

-a 0 | a b c d | 0 -d

dst2

antireflect,
dirichlet

-b -a | a b c d | -d -c

dft

grid-wrap

wrap

circular

circulant

c d | a b c d | a b

wrap

c d | a b c d | b c

linear_ramp

minimum,
maximum,
mean,
median

Some of these conventions are inconsistent with each other. For example "wrap" in scipy.ndimage is different from "wrap" in numpy.pad, which corresponds to "grid-wrap" in scipy.ndimage. Also, "reflect" in numpy.pad and torch.pad is different from "reflect" in scipy.ndimage, which correspond to "symmetric" in numpy.pad.

Classes

BoundType

Bases: Enum

An Enum type that maps boundary modes of any convention to a unique set of values.

class BoundType(Enum):
    zero = zeros = constant = gridconstant = 0
    replicate = repeat = nearest = border = edge = 1
    dct1 = mirror = 2
    dct2 = reflect = reflection = gridmirror = neumann = 3
    dst1 = antimirror = 4
    dst2 = antireflect = dirichlet = 5
    dft = fft = wrap = gridwrap = circular = circulant = 6
    nocheck = -1

Functions:

to_enum

Convert boundary type to enum type.

Parameters:

Name Type Description Default
bound SequenceOrScalar[BoundLike]

Boundary condition in any convention

required

Returns:

Name Type Description
bound SequenceOrScalar[BoundType]

Boundary condition

to_int

Convert boundary type to enum integer.

Parameters:

Name Type Description Default
bound SequenceOrScalar[BoundLike]

Boundary condition in any convention

required

Returns:

Name Type Description
bound SequenceOrScalar[{0..6}]

Boundary condition

to_fourier

Convert boundary type to discrete transforms.

Parameters:

Name Type Description Default
bound SequenceOrScalar[BoundLike]

Boundary condition in any convention

required

Returns:

Name Type Description
bound SequenceOrScalar[{replicate, zero, dct2, dct1, dst2, dst1, dft}]

Boundary condition in terms of discrete transforms

to_scipy

Convert boundary type to SciPy's convention.

Parameters:

Name Type Description Default
bound SequenceOrScalar[BoundLike]

Boundary condition in any convention

required

Returns:

Name Type Description
bound SequenceOrScalar[{border, constant, reflect, mirror, wrap}]

Boundary condition in SciPy's convention

to_torch

Convert boundary type to PyTorch's convention.

Parameters:

Name Type Description Default
bound SequenceOrScalar[BoundLike]

Boundary condition in any convention

required

Returns:

Name Type Description
bound SequenceOrScalar[{nearest, zero, reflection}, bool]

The first element is the boundary condition in PyTorchs's convention, and the second element is the value of align_corners.