mixle.represent.segment module

Segmenters – cut a raw object of any modality into units, WITHOUT committing to a vocabulary.

A Segmenter turns one raw object (a string, an image, a waveform, a set of node features) into an array of units: (n_units,) integer ids for a discrete alphabet, or (n_units, feat...) float features for a continuous modality. That is the whole of “the tokenizer” that is not objective-dependent – it is a decomposition, not a vocabulary. Discreteness (mapping units to a codebook of ids) is a separate, optional, learned step (mixle.represent.quantize), so a segmenter never has to guess the right tokens.

Fixed segmenters (bytes, characters, patches, windows, whole-object, element-set) commit to nothing beyond where to cut and keep all information; the model learns the rest. Learned segmenters (a segmental HMM over boundaries) are the objective-coupled upgrade and plug into the same contract.

class Segmenter[source]

Bases: object

Base: segment(raw) -> np.ndarray. discrete says whether units are ids (vs. float features).

segment(raw)[source]

Segment raw input into model units.

Parameters:

raw (Any)

Return type:

ndarray

class ByteSegmenter[source]

Bases: Segmenter

A string/bytes -> (n,) byte ids in [0, 256). The vocabulary-free text decomposition.

segment(raw)[source]

Return UTF-8 byte ids for a string or bytes-like object.

Parameters:

raw (Any)

Return type:

ndarray

class ElementSegmenter(alphabet)[source]

Bases: Segmenter

A sequence of hashable symbols (chars, amino acids, k-mers, categories) -> (n,) ids via a fixed alphabet.

Given alphabet (the ordered symbols), each element maps to its index. Out-of-vocabulary symbols map to unknown_id, a state reserved past the declared alphabet (len(alphabet)), so num_categories is len(alphabet) + 1. Unknown symbols used to collapse onto id 0, which is already the first declared real category – an unseen residue/character/class then became positive evidence for a genuinely observed symbol. The alphabet must also be unique: a repeated entry used to overwrite its own index while still inflating num_categories, leaving an id no symbol could ever produce.

The natural decomposition for proteins/genomes/any categorical sequence, and for characters (alphabet=list(...)).

Parameters:

alphabet (list[Any])

segment(raw)[source]

Map sequence elements through the fixed alphabet index, unknowns to the reserved unknown_id.

Parameters:

raw (Any)

Return type:

ndarray

class PatchSegmenter(patch=8)[source]

Bases: Segmenter

An image (H, W) or (C, H, W) -> (n_patches, patch_features) float units (ViT-style, no vocab).

Parameters:

patch (int)

segment(raw)[source]

Split an image tensor into flattened non-overlapping patches.

Parameters:

raw (Any)

Return type:

ndarray

unit_features(channels=1)[source]

Return feature width of one flattened patch.

Parameters:

channels (int)

Return type:

int

class WindowSegmenter(window=64, hop=None)[source]

Bases: Segmenter

A 1-D signal (T,) -> (n_frames, window) float units by a sliding window (seismic/audio/time-series).

Parameters:
  • window (int)

  • hop (int | None)

segment(raw)[source]

Split a one-dimensional signal into fixed windows.

A signal shorter than window still yields exactly one window (WindowSegmenter’s contract is to always return at least one unit, unlike PatchSegmenter which has no such contract and rejects a too-small image outright): the real samples fill [:len(x)] and only the remaining slots are zero-padded, so the padding never overwrites real data.

Parameters:

raw (Any)

Return type:

ndarray

class WholeSegmenter[source]

Bases: Segmenter

A single feature vector -> (1, feat): the object is one unit (a pooled structure descriptor, a record).

segment(raw)[source]

Treat the whole input as one feature-vector segment.

Parameters:

raw (Any)

Return type:

ndarray

class SetSegmenter[source]

Bases: Segmenter

A set/list of feature vectors -> (n_elements, feat): nodes of a graph, atoms of a molecule, taxa of a section.

The general structured-object decomposition – a scientific structure becomes its set of element features (which a downstream model can further couple with a structure/message-passing embedding).

segment(raw)[source]

Return set elements as rows of a feature matrix.

Parameters:

raw (Any)

Return type:

ndarray