Skip to content

Taxa & Namespace

Global taxon namespace shared across all alignments and trees.

TaxonTable

Global taxon namespace: maps taxon label (str) to a stable integer index.

Indices are assigned in order of first registration and never change. A dict holds the label-to-index map, so a lookup costs the same at any table size, and the order of the table follows the order of registration.

Attributes:

Name Type Description
n_taxa int

Number of registered taxa.

labels list[str]

Taxon labels in registration order (index 0, 1, ...).

Source code in src/hifuku/taxa.py
class TaxonTable:
    """
    Global taxon namespace: maps taxon label (str) to a stable integer index.

    Indices are assigned in order of first registration and never change.
    A dict holds the label-to-index map, so a lookup costs the same at any
    table size, and the order of the table follows the order of registration.

    Attributes
    ----------
    n_taxa : int
        Number of registered taxa.
    labels : list[str]
        Taxon labels in registration order (index 0, 1, ...).
    """

    def __init__(self) -> None:
        self._index: dict[str, int] = {}
        self._labels: list[str] = []

    # ------------------------------------------------------------------
    # Registration & lookup

    def require(self, label: str) -> int:
        """Register *label* if absent; return its global index."""
        idx = self._index.get(label)
        if idx is None:
            idx = len(self._labels)
            self._index[label] = idx
            self._labels.append(label)
        return idx

    def index_of(self, label: str) -> int:
        """Return the global index of *label*; raise KeyError if absent."""
        return self._index[label]

    def label_of(self, idx: int) -> str:
        """Return the label registered at *idx*."""
        return self._labels[idx]

    # ------------------------------------------------------------------
    # Properties

    @property
    def n_taxa(self) -> int:
        return len(self._labels)

    @property
    def labels(self) -> list[str]:
        return list(self._labels)

    # ------------------------------------------------------------------

    def __len__(self) -> int:
        return len(self._labels)

    def __contains__(self, label: str) -> bool:
        return label in self._index

    def __repr__(self) -> str:
        return f"TaxonTable({self.n_taxa} taxa)"

require

require(label: str) -> int

Register label if absent; return its global index.

Source code in src/hifuku/taxa.py
def require(self, label: str) -> int:
    """Register *label* if absent; return its global index."""
    idx = self._index.get(label)
    if idx is None:
        idx = len(self._labels)
        self._index[label] = idx
        self._labels.append(label)
    return idx

index_of

index_of(label: str) -> int

Return the global index of label; raise KeyError if absent.

Source code in src/hifuku/taxa.py
def index_of(self, label: str) -> int:
    """Return the global index of *label*; raise KeyError if absent."""
    return self._index[label]

label_of

label_of(idx: int) -> str

Return the label registered at idx.

Source code in src/hifuku/taxa.py
def label_of(self, idx: int) -> str:
    """Return the label registered at *idx*."""
    return self._labels[idx]

AlignmentIndex

Per-alignment local ↔ global index map and presence mask.

Parameters:

Name Type Description Default
taxon_table TaxonTable

The shared global TaxonTable. Taxa in labels are registered (added if absent) during construction.

required
labels list[str]

Ordered list of taxon labels for this alignment. The order defines the local index (0 … n_local-1).

required
Notes

The presence mask depends on the total number of global taxa, which may grow after construction. Use :meth:build_mask and pass the current taxon_table.n_taxa to get a correctly-sized mask.

Source code in src/hifuku/taxa.py
class AlignmentIndex:
    """
    Per-alignment local ↔ global index map and presence mask.

    Parameters
    ----------
    taxon_table:
        The shared global TaxonTable. Taxa in *labels* are registered
        (added if absent) during construction.
    labels:
        Ordered list of taxon labels for this alignment. The order
        defines the local index (0 … n_local-1).

    Notes
    -----
    The presence mask depends on the total number of global taxa, which
    may grow after construction. Use :meth:`build_mask` and pass the
    current ``taxon_table.n_taxa`` to get a correctly-sized mask.
    """

    def __init__(self, taxon_table: TaxonTable, labels: list[str]) -> None:
        if len(labels) != len(set(labels)):
            raise ValueError("Duplicate taxon labels in alignment")

        # Register all taxa; collect global indices in local order
        global_indices = [taxon_table.require(lbl) for lbl in labels]

        self._labels: list[str] = list(labels)
        self._local_to_global: np.ndarray = np.array(global_indices, dtype=np.int32)
        # Reverse mapping: global_idx → local_idx (only for present taxa)
        self._global_to_local: dict[int, int] = {
            g: loc for loc, g in enumerate(global_indices)
        }

    # ------------------------------------------------------------------
    # Mappings

    @property
    def local_to_global(self) -> np.ndarray:
        """Shape (n_local,) int32: maps local index to global index."""
        return self._local_to_global

    def global_to_local(self, global_idx: int) -> int:
        """Return local index for *global_idx*; raise KeyError if absent."""
        return self._global_to_local[global_idx]

    @property
    def n_local(self) -> int:
        return len(self._local_to_global)

    @property
    def labels(self) -> list[str]:
        """Taxon labels in local (alignment) order."""
        return list(self._labels)

    # ------------------------------------------------------------------
    # Mask

    def build_mask(self, n_global: int) -> np.ndarray:
        """
        Return a boolean mask of shape (n_global,) where True = taxon present.

        Pass ``taxon_table.n_taxa`` after all alignments have been registered
        so that the mask has the correct final length.
        """
        mask = np.zeros(n_global, dtype=bool)
        for g in self._local_to_global:
            mask[g] = True
        return mask

    # ------------------------------------------------------------------

    def __len__(self) -> int:
        return self.n_local

    def __repr__(self) -> str:
        return f"AlignmentIndex(n_local={self.n_local}, labels={self._labels})"

local_to_global property

local_to_global: ndarray

Shape (n_local,) int32: maps local index to global index.

labels property

labels: list[str]

Taxon labels in local (alignment) order.

global_to_local

global_to_local(global_idx: int) -> int

Return local index for global_idx; raise KeyError if absent.

Source code in src/hifuku/taxa.py
def global_to_local(self, global_idx: int) -> int:
    """Return local index for *global_idx*; raise KeyError if absent."""
    return self._global_to_local[global_idx]

build_mask

build_mask(n_global: int) -> np.ndarray

Return a boolean mask of shape (n_global,) where True = taxon present.

Pass taxon_table.n_taxa after all alignments have been registered so that the mask has the correct final length.

Source code in src/hifuku/taxa.py
def build_mask(self, n_global: int) -> np.ndarray:
    """
    Return a boolean mask of shape (n_global,) where True = taxon present.

    Pass ``taxon_table.n_taxa`` after all alignments have been registered
    so that the mask has the correct final length.
    """
    mask = np.zeros(n_global, dtype=bool)
    for g in self._local_to_global:
        mask[g] = True
    return mask