tomosipo.types module

tomosipo.types.to_float_tuple(val, n, var_name='value')[source]

Convert value to tuple of n floats

>>> to_float_tuple(1, 2)
(1.0, 1.0)
>>> to_float_tuple(1, n=2)
(1.0, 1.0)
>>> to_float_tuple((1.0, 2.0), n=2)
(1.0, 2.0)
>>> to_tuple((1, 2, 3), n=2)
Traceback (most recent call last):
...
TypeError: Expected tuple with 2 elements. Got (1, 2, 3).
>>> to_float_tuple(('a', 0), n=2)
Traceback (most recent call last):
...
TypeError: value must contain only floats. Got ('a', 0).
tomosipo.types.to_homogeneous_pos(vec)[source]

Create an array of vectors

Parameters:

vec (Tuple[float, float, float] | Iterable[Tuple[float, float, float]] | ndarray) – The array that is to be converted to a homogeneous position. Must be of shape (3,), (4,), (N, 3), or (N, 4).

Returns:

An array with shape (N, 4)

Return type:

ndarray

Examples

>>> to_vec((1, 1, 1))
array([[1., 1., 1.]])
>>> to_vec((1, 1, 1)).shape
(1, 3)
>>> to_vec([(1, 1, 1), (2, 2, 2)]).shape
(2, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 3))).shape
(1, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 2))).shape
Traceback (most recent call last):
...
TypeError: Value cannot be converted to vector. Expected shape: (3,) or (N, 3). Got shape: (1, 2).
>>> to_vec("string")
Traceback (most recent call last):
...
TypeError: Could not convert vector to np.array. Got: 'string'.
tomosipo.types.to_homogeneous_vec(vec)[source]

Create an array of vectors

Return type:

ndarray

Examples

>>> to_vec((1, 1, 1))
array([[1., 1., 1.]])
>>> to_vec((1, 1, 1)).shape
(1, 3)
>>> to_vec([(1, 1, 1), (2, 2, 2)]).shape
(2, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 3))).shape
(1, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 2))).shape
Traceback (most recent call last):
...
TypeError: Value cannot be converted to vector. Expected shape: (3,) or (N, 3). Got shape: (1, 2).
>>> to_vec("string")
Traceback (most recent call last):
...
TypeError: Could not convert vector to np.array. Got: 'string'.
tomosipo.types.to_pos(pos)[source]

Create a 3D position tuple

Return type:

Tuple[float, float, float]

>>> to_pos(0)
(0.0, 0.0, 0.0)
>>> to_pos(0.0)
(0.0, 0.0, 0.0)
>>> to_pos((3, 2, 1))
(3.0, 2.0, 1.0)
>>> to_pos((2, 1))
Traceback (most recent call last):
...
TypeError: Expected tuple with 3 elements. Got (2, 1).
tomosipo.types.to_scalars(s, var_name='scalars', accept_empty=False)[source]

Create an array of scalars

Parameters:
  • s (float | Collection[float] | ndarray) – A single float or collection of floats.

  • var_name – The name to use in error messages.

Returns:

A 1-dimensional array contains floats.

Return type:

ndarray

Examples

>>> to_scalars((1, 1, 1))
array([1., 1., 1.])
>>> to_scalars(1).shape
(1,)
>>> import numpy as np
>>> to_scalars(np.ones((1, 3))).shape
Traceback (most recent call last):
...
TypeError: Value cannot be converted to scalars. Expected shape: (N,). Got shape: (1, 3).
>>> import numpy as np
>>> to_scalars([])
Traceback (most recent call last):
...
TypeError: Value cannot be converted to scalars. Expected shape: (N,). Got shape: (0,).
>>> to_scalars("string")
Traceback (most recent call last):
...
TypeError: Could not convert scalars to np.array. Got: 'string'.
>>> to_scalars(None)
Traceback (most recent call last):
...
TypeError: Could not convert to array of scalars: array contains NaN.
tomosipo.types.to_shape2d(shape)[source]

Create a 2D shape tuple

Return type:

Tuple[int, int]

>>> to_shape2d(1)
(1, 1)
>>> to_shape2d((5, 3))
(5, 3)
>>> to_shape2d((5.0, 3))
Traceback (most recent call last):
...
TypeError: Shape must contain only integers. Got (5.0, 3) with type <class 'float'>.
tomosipo.types.to_shape3d(shape)[source]

Create a 3D shape tuple

Return type:

Tuple[int, int, int]

>>> to_shape3d(1)
(1, 1, 1)
>>> to_shape3d((5, 3, 2))
(5, 3, 2)
>>> to_shape3d((5.0, 3))
Traceback (most recent call last):
...
TypeError: Expected tuple with 3 elements. Got (5.0, 3).
tomosipo.types.to_size2d(size)[source]

Create a 2D size tuple

Return type:

Tuple[float, float]

>>> to_size2d(1)
(1.0, 1.0)
>>> to_size2d((5, 3))
(5.0, 3.0)
>>> to_size2d((3, 2, 1))
Traceback (most recent call last):
...
TypeError: Expected tuple with 2 elements. Got (3, 2, 1).
tomosipo.types.to_size3d(size)[source]

Create a 3D size tuple

Return type:

Tuple[float, float, float]

>>> to_size3d(1)
(1.0, 1.0, 1.0)
>>> to_size3d((3, 2, 1))
(3.0, 2.0, 1.0)
>>> to_size3d((2, 1))
Traceback (most recent call last):
...
TypeError: Expected tuple with 3 elements. Got (2, 1).
tomosipo.types.to_tuple(val, n)[source]

Convert value to tuple of length n

Return type:

Tuple[TypeVar(T)]

>>> to_tuple(1, 2)
(1, 1)
>>> to_tuple(1, n=2)
(1, 1)
>>> to_tuple((1, 2), n=2)
(1, 2)
>>> to_tuple((1, 2, 3), n=2)
Traceback (most recent call last):
...
TypeError: Expected tuple with 2 elements. Got (1, 2, 3).
tomosipo.types.to_vec(vec, var_name='vector')[source]

Create an array of vectors

Return type:

ndarray

Examples

>>> to_vec((1, 1, 1))
array([[1., 1., 1.]])
>>> to_vec((1, 1, 1)).shape
(1, 3)
>>> to_vec([(1, 1, 1), (2, 2, 2)]).shape
(2, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 3))).shape
(1, 3)
>>> import numpy as np
>>> to_vec(np.ones((1, 2))).shape
Traceback (most recent call last):
...
TypeError: Value cannot be converted to vector. Expected shape: (3,) or (N, 3). Got shape: (1, 2).
>>> to_vec("string")
Traceback (most recent call last):
...
TypeError: Could not convert vector to np.array. Got: 'string'.