Skip to content

Utility#

octarine.register_converter(t, converter, insert='first') #

Register a converter for a given data type.

PARAMETER DESCRIPTION
t
    Data type the converter is meant to convert. If a function
    it is expected to take a single argument `x` and return
    True if `x` can be converted using `converter` and False
    if not.

TYPE: type | hashable | callable

converter
    Function that converts `x` to pygfx visuals. Must accept
    at least a single argument and return either a single
    visual or a list thereof.

TYPE: callable

insert
    Whether to insert the converter at the beginning or end
    of the list of converters. This is important because when
    looking for a converter for a given type we will use the
    first one that matches.

TYPE: "first" | "last" DEFAULT: 'first'

Source code in octarine/conversion.py
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
def register_converter(t, converter, insert='first'):
    """Register a converter for a given data type.

    Parameters
    ----------
    t :         type | hashable | callable
                Data type the converter is meant to convert. If a function
                it is expected to take a single argument `x` and return
                True if `x` can be converted using `converter` and False
                if not.
    converter : callable
                Function that converts `x` to pygfx visuals. Must accept
                at least a single argument and return either a single
                visual or a list thereof.
    insert :    "first" | "last"
                Whether to insert the converter at the beginning or end
                of the list of converters. This is important because when
                looking for a converter for a given type we will use the
                first one that matches.

    """
    global CONVERTERS
    assert insert in ('first', 'last')

    if not callable(converter):
        raise ValueError("Converter must be callable.")

    if not callable(t) and not is_hashable(t):
        raise ValueError("Type must be hashable or callable.")


    if insert == 'first':
        items = list(CONVERTERS.items())
        items.insert(0, (t, converter))
        CONVERTERS = dict(items)
    else:
        CONVERTERS[t] = converter

octarine.get_converter(t, raise_missing=True) #

Get the converter for a given data type.

Source code in octarine/conversion.py
22
23
24
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
def get_converter(t, raise_missing=True):
    """Get the converter for a given data type."""
    # Go through converters in order
    for k, v in CONVERTERS.items():
        # First check if we have a direct match
        if is_hashable(t) and t == k:
            return v

        # If not, check if k is a type
        if type(t) == k:
            return v

        # Check if t is a subclass of k
        if isinstance(k, type) and isinstance(t, k):
            return v

        # Check if k is a callable
        if callable(k):
            try:
                if k(t) is True:
                    return v
            except Exception:
                pass

    if not raise_missing:
        return None

    raise NotImplementedError(f"No converter found for {t} ({type(t)}).")

octarine.VoxelCloud #

Container for sparse volumetric data.

Wrapping voxel coordinates in this class tells Viewer.add to render them as a (sparse) volume instead of as points.

PARAMETER DESCRIPTION
coords
    Voxel coordinates (xyz). Floats are floored to integers.

TYPE: (N, 3) array

values
    Per-voxel scalar values.

TYPE: (N,) array DEFAULT: None

octarine.VoxelRuns #

Container for run-length-encoded sparse volumetric data.

Wrapping voxel runs in this class tells Viewer.add to render them as a (sparse) volume. Runs are rendered as binary occupancy from a bitmask, which uses roughly 23x less GPU memory than the same data given as VoxelCloud coordinates - see Sparse Volumes.

PARAMETER DESCRIPTION
runs
    Runs as (x, y, z, x_run_length), i.e. the layout returned by
    `dvid.get_sparsevol(..., voxels=False)`. Runs extend along x
    and the length is an inclusive voxel count.

TYPE: (N, 4) array

n_voxels property #

Number of occupied voxels.