Skip to content

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 colors has another length.

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

names property

names: list[str]

Category names in code order.

colors property

colors: list[str] | None

One matplotlib color per category, or None.

from_labels staticmethod

from_labels(labels: ArrayLike) -> Categories

Categories from the sorted unique non-missing labels; numeric order when every label is an integer.

Parameters

labels : array_like Labels of any type; None and NaN are missing.

Returns

Categories

Raises

InvalidInput When there is no non-missing label.

encode

encode(labels: ArrayLike) -> NDArray[np.int64]

Codes of labels.

Parameters

labels : array_like Category names; None and NaN are missing.

Returns

numpy.ndarray int64 codes, -1 for missing.

Raises

InvalidInput When a label is not one of the names.

decode

decode(codes: ArrayLike) -> NDArray[np.object_]

Names of codes.

Parameters

codes : array_like Codes 0..k-1, -1 or NaN for missing.

Returns

numpy.ndarray Object array of names, None for missing.

Raises

InvalidInput When a code is not -1 or in 0..k-1.

to_json

to_json() -> str

JSON text of this object; Categories.from_json reads it back.

Returns

str

from_json staticmethod

from_json(text: str) -> Categories

Categories from the text written by to_json.

Parameters

text : str

Returns

Categories

Raises

InvalidInput When text is not Categories JSON or describes an invalid Categories.

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

origin property

origin: tuple[float, float, float]

Corner of the first cell (x, y, z).

size property

size: tuple[float, float, float]

Cell size along x, y and z.

count property

count: tuple[int, int, int]

Number of cells along x, y and z.

n_cells property

n_cells: int

Total number of cells.

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.

to_json

to_json() -> str

JSON text of this object; Grid.from_json reads it back.

Returns

str

from_json staticmethod

from_json(text: str) -> Grid

Grid from the text written by to_json.

Parameters

text : str

Returns

Grid

Raises

InvalidInput When text is not Grid JSON or describes an invalid Grid.

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.