Core¶
tg.Categories(names, *, colors=None) holds category names in code order. encode(labels) returns
int64 codes with -1 for missing labels; decode(codes) returns the names, None for -1.
Categories.from_labels(labels) sorts the unique labels, numerically when every label is an integer.
tg.Grid(origin, size, count) is an axis-aligned grid whose origin is the corner of the first cell.
Cells run x fastest, then y, then z, so values.reshape(grid.shape) indexes [k, j, i].
grid.coords gives the centroids and grid.snap(coords, codes, *, mask=None) assigns hard data to
cells: the point nearest to a cell's centroid wins.
Every error derives from tg.TransiogramsError. tg.InvalidInput is also a ValueError and
tg.ConvergenceError a RuntimeError.
Categories
¶
Named categories: a code is the index of its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
sequence of str
|
Category names in code order, unique and non-empty. |
required |
colors
|
sequence of str
|
One matplotlib color per name. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidInput
|
When a name is empty or repeated, or |
Examples:
>>> cats = tg.Categories(["sand", "silt", "clay"])
>>> len(cats), cats.names
(3, ['sand', 'silt', 'clay'])
>>> codes = cats.encode(["clay", None, "sand"])
>>> codes
array([ 2, -1, 0])
>>> cats.decode(codes)
array(['clay', None, 'sand'], dtype=object)
>>> tg.Categories.from_labels(["b", "a", "b"]).names
['a', 'b']
>>> tg.Categories.from_json(cats.to_json()) == cats
True
Grid
¶
Axis-aligned grid, cells x fastest, then y, then z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
sequence of 3 floats
|
Corner of the first cell (minimum x, y, z). |
required |
size
|
sequence of 3 floats
|
Cell size along x, y and z. |
None
|
count
|
sequence of 3 ints
|
Number of cells along x, y and z; 1 along z for a 2D grid. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidInput
|
When a size is not positive and finite or a count is 0. |
Examples:
>>> grid = tg.Grid((0.0, 0.0, 0.0), (10.0, 10.0, 1.0), (3, 2, 4))
>>> grid.n_cells, grid.shape
(24, (4, 2, 3))
>>> grid.coords[:2]
array([[ 5. , 5. , 0.5],
[15. , 5. , 0.5]])
>>> cells, codes = grid.snap([[4.0, 6.0, 0.2], [99.0, 0.0, 0.0]], [1, 2])
>>> cells, codes
(array([0], dtype=uint64), array([1]))
>>> tg.Grid.from_json(grid.to_json()) == grid
True
shape
property
¶
shape: tuple[int, int, int]
(nz, ny, nx), so values.reshape(grid.shape) indexes [k, j, i].
coords
property
¶
coords: NDArray[float64]
Cell centroids, shape (n_cells, 3), in cell order. Raises InvalidInput before
allocating more than the available memory.
snap
¶
snap(
coords: ArrayLike | object,
codes: ArrayLike | str | None = None,
*,
mask: ArrayLike | None = None,
) -> tuple[NDArray[np.uint64], NDArray[np.int64]]
Snap point data to cells.
Points outside the grid or mask and missing codes are skipped; several points in a cell keep the one nearest to the centroid, ties to the first.
Parameters¶
coords : array_like
(n, 3) point coordinates.
codes : array_like
(n,) category codes, -1 or NaN for missing.
mask : sequence of bool, optional
(n_cells,) active cells.
Returns¶
cells : numpy.ndarray uint64 cell indices, ascending. codes : numpy.ndarray int64 code of each snapped cell.
Raises¶
InvalidInput
When coords is not (n, 3), the lengths differ or mask has another length.
TransiogramsError
¶
Bases: Exception
Base class of every error raised by transiograms.
InvalidInput
¶
Bases: TransiogramsError, ValueError
An argument is invalid; the message names the offending item.
ConvergenceError
¶
Bases: TransiogramsError, RuntimeError
An iterative method did not converge or a decomposition failed.