bounds
| MODULE | DESCRIPTION |
|---|---|
indexing |
Functions that wrap out-of-bound indices back in-bounds, according to some boundary condition. |
padding |
Reimplements |
realtransforms |
Implements the discrete sine and cosine transforms, variants I, II, III. |
types |
Defines names and aliases for different boundary conditions, as well as tools to convert between different naming conventions. |
| FUNCTION | DESCRIPTION |
|---|---|
pad |
Pad a tensor |
roll |
Roll a tensor |
ensure_shape |
Pad/crop a tensor so that it has a given shape |
to_enum |
Convert any boundary type to |
to_int |
Convert any boundary type to |
to_fourier |
Convert any boundary type to discrete transforms |
to_scipy |
Convert any boundary type to |
to_torch |
Convert any boundary type to |
dct |
One-dimensional Discrete Cosine Transform (DCT) |
dst |
One-dimensional Discrete Sine Transform (DST) |
idct |
One-dimensional Inverse Discrete Cosine Transform (DCT) |
idst |
One-dimensional Inverse Discrete Sine Transform (DST) |
dctn |
N-dimensional Discrete Cosine Transform (IDCT) |
dstn |
N-dimensional Discrete Sine Transform (IDST) |
idctn |
N-dimensional Inverse Discrete Cosine Transform (IDCT) |
idstn |
N-dimensional Inverse Discrete Sine Transform (IDST) |
| CLASS | DESCRIPTION |
|---|---|
BoundType |
Enum type for bounds |
| ATTRIBUTE | DESCRIPTION |
|---|---|
BoundLike |
A type hint for any boundary type
|
SequenceOrScalar |
A type hint for values or sequences of values
|
SequenceOrScalar
module-attribute
Either an element or type T, or a sequence of elements of type T.
BoundLike
module-attribute
A boundary mode.
Most conventions are handled (numpy, scipy, torch, see below). Can be one of:
zero,zeros,constantorgridconstant;replicate,nearest,borderoredge;dct1ormirror;dct2,reflect,reflection,gridmirrororneumann;dst1orantimirror;dst2,antireflectordirichlet;dft,fft,wrap,gridwrap,circularorcirculant.
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:
- Discrete Fourier Transform (DFT)
- Discrete Cosine Transform I & II (DCT-I, DCT-II)
- Discrete Sine Transform I & II (DST-I, DST-II)
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 |
Numpy |
PyTorch |
PyTorch |
Other |
Description |
|---|---|---|---|---|---|---|
|
nearest |
edge |
border |
replicate |
repeat |
|
|
|
constant, |
constant |
constant |
zeros |
zero |
|
|
|
dct1 |
mirror |
reflect |
reflect |
reflection |
|
|
|
dct2 |
reflect, |
symmetric |
reflection |
neumann |
|
|
|
dst1 |
antimirror |
|
||||
|
dst2 |
antireflect, |
|
||||
|
dft |
grid-wrap |
wrap |
circular |
circulant |
|
|
|
wrap |
|
|||||
|
linear_ramp |
||||||
|
minimum, |
Some of these conventions are inconsistant 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.
BoundType
Bases: Enum
An Enum type that maps boundry 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
pad
Pad a tensor.
This function is a bit more generic than torch's native pad
(torch.nn.functional.pad), but probably a bit slower:
- works with any input type
- works with arbitrarily large padding size
- crops the tensor for negative padding values
- implements additional padding modes
When used with defaults parameters (side=None), it behaves
exactly like torch.nn.functional.pad
Boundary modes
Like in PyTorch's pad, boundary modes include:
'circular'(or'dft')'mirror'(or'dct1')'reflect'(or'dct2')'replicate'(or'nearest')'constant'(or'zero')
as well as the following new modes:
'antimirror'(or'dst1')'antireflect'(or'dst2')
Side modes
Side modes are 'pre' (or 'left'), 'post' (or 'right'),
'both' or None.
- If side is not
None,inp.dim()values (or less) should be provided. - If side is
None, twice as many values should be provided, indicating different padding sizes for the'pre'and'post'sides. - If the number of padding values is less than the dimension of the input tensor, zeros are prepended.
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input tensor
TYPE:
|
padsize |
Amount of padding in each dimension.
TYPE:
|
mode |
Padding mode
TYPE:
|
value |
Value to pad with in mode
TYPE:
|
side |
Use padsize to pad on left side (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Padded tensor.
TYPE:
|
Source code in bounds/padding.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
ensure_shape
Pad/crop a tensor so that it has a given shape
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input tensor
TYPE:
|
shape |
Output shape
TYPE:
|
mode |
Boundary mode
TYPE:
|
value |
Value for mode
TYPE:
|
side |
Side to crop/pad
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Padded tensor with shape
TYPE:
|
Source code in bounds/padding.py
roll
Like torch.roll, but with any boundary condition
Warning
When dims is None, we do not flatten but shift all dimensions.
This differs from the behavior of torch.roll .
| PARAMETER | DESCRIPTION |
|---|---|
inp |
Input
TYPE:
|
shifts |
Amount by which to roll. Positive shifts to the right, negative to the left.
TYPE:
|
dims |
Dimensions to roll. By default, shifts apply to all dimensions if a scalar, or to the last N if a sequence.
TYPE:
|
bound |
Boundary condition
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
out
|
Rolled tensor
TYPE:
|
Source code in bounds/padding.py
to_enum
Convert boundary type to enum type.
See also
| PARAMETER | DESCRIPTION |
|---|---|
bound |
Boundary condition in any convention
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bound
|
Boundary condition
TYPE:
|
Source code in bounds/types.py
to_int
Convert boundary type to enum integer.
See also
| PARAMETER | DESCRIPTION |
|---|---|
bound |
Boundary condition in any convention
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bound
|
Boundary condition
TYPE:
|
Source code in bounds/types.py
to_fourier
Convert boundary type to discrete transforms.
| PARAMETER | DESCRIPTION |
|---|---|
bound |
Boundary condition in any convention
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bound
|
Boundary condition in terms of discrete transforms
TYPE:
|
Source code in bounds/types.py
to_scipy
Convert boundary type to SciPy's convention.
See also
| PARAMETER | DESCRIPTION |
|---|---|
bound |
Boundary condition in any convention
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bound
|
Boundary condition in SciPy's convention
TYPE:
|
Source code in bounds/types.py
to_torch
Convert boundary type to PyTorch's convention.
See also
| PARAMETER | DESCRIPTION |
|---|---|
bound |
Boundary condition in any convention
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bound
|
The first element is the boundary condition in PyTorchs's
convention, and the second element is the value of
TYPE:
|
Source code in bounds/types.py
dct
Return the Discrete Cosine Transform
Type IV not implemented
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. Default is the last one.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
idct
Return the Inverse Discrete Cosine Transform
Warning
Type IV not implemented
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. Default is the last one.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
dst
Return the Discrete Sine Transform
Type IV not implemented
Warning
dst(..., norm="ortho") yields a different result than scipy
and cupy for types 2 and 3. This is because their DST is not
properly orthogonalized. Use norm="ortho_scipy" to get results
matching their implementation.
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. Default is the last one.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
idst
Return the Inverse Discrete Sine Transform
Type IV not implemented
Warning
idst(..., norm="ortho") yields a different result than scipy
and cupy for types 2 and 3. This is because their DST is not
properly orthogonalized. Use norm="ortho_scipy" to get results
matching their implementation.
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. Default is the last one.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
dctn
Return multidimensional Discrete Cosine Transform along the specified axes.
Type IV not implemented
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. If not given, all dimensions are used.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
idctn
Return multidimensional Inverse Discrete Cosine Transform along the specified axes.
Type IV not implemented
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. If not given, all dimensions are used.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
dstn
Return multidimensional Discrete Sine Transform along the specified axes.
Type IV not implemented
Warning
dstn(..., norm="ortho") yields a different result than scipy
and cupy for types 2 and 3. This is because their DST is not
properly orthogonalized. Use norm="ortho_scipy" to get results
matching their implementation.
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. If not given, all dimensions are used.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|
Source code in bounds/realtransforms.py
idstn
Return multidimensional Inverse Discrete Sine Transform along the specified axes.
Type IV not implemented
Warning
idstn(..., norm="ortho") yields a different result than scipy
and cupy for types 2 and 3. This is because their DST is not
properly orthogonalized. Use norm="ortho_scipy" to get results
matching their implementation.
| PARAMETER | DESCRIPTION |
|---|---|
x |
The input tensor
TYPE:
|
dim |
Dimensions over which the DCT is computed. If not given, all dimensions are used.
TYPE:
|
norm |
Normalization mode. Default is "backward".
TYPE:
|
type |
Type of the DCT. Default type is 2.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
y
|
The transformed tensor.
TYPE:
|