Skip to main content

Vectorized, numpy-backed tree: parent-id topology and user-defined fields in one growable structured array, with vectorized operations over node ids.

Project description

vcti-nptree

Vectorized, numpy-backed tree: parent-index topology and user-defined fields in one growable structured array, with vectorized operations over node indices.

Overview

vcti-nptree provides NpTree, a single-rooted tree whose structure and per-node data live together in one growable numpy structured array (backed by GrowableArray from vcti-nputils):

  • One record per node, addressed by its integer row index. The record carries parent_index, a flags column (active / locks), and any fixed-width user fields declared at construction.
  • parent_index is the structure. Children and siblings are vectorized scans of that column (parent_index == n), ancestors are a short parent-chain walk, and per-node fields roll up with np.bincount(parent_index, weights=field).
  • Soft deletion clears the ACTIVE bit and leaves the row in place — nothing is moved or renumbered, so node indices stay stable for the life of the tree and every vectorized operation keeps working with deletions present. delete(..., fill=...) optionally writes a per-field "inactive" / "na" / zero / custom value so aggregations stay branchless without even consulting the mask.

It is built for vectorized operations over sets of nodes — aggregating, masking, or rolling up fields across many nodes at once, rather than walking the tree one node at a time. Nodes are addressed by integer indices and index-arrays (not opaque handles), which is what lets those operations be plain numpy.

The design and rationale are documented in docs/design.md.

Installation

pip install vcti-nptree

In requirements.txt

vcti-nptree>=1.0.0

In pyproject.toml dependencies

dependencies = [
    "vcti-nptree>=1.0.0",
]

Quick Start

import numpy as np
from vcti.nptree import FieldSpec, NpTree

# Declare fixed-width user fields: a schema dtype plus the per-field default and
# inactive values (unspecified fields default to zero).
spec = FieldSpec.create(
    np.dtype([("mass", "f8")]), default={"mass": 0.0}, inactive={"mass": 0.0}
)

tree = NpTree(spec)                # the root is node 0
p1 = tree.create_child(tree.root)
p2 = tree.create_child(tree.root)

# Bulk-create a level of elements under the parts (one vectorized append).
tree.create_children(np.array([p1, p1, p2]), mass=np.array([3.0, 4.0, 10.0]))

# Vectorized roll-up: every node's subtree total in one pass.
totals = tree.subtree_sum("mass")
assert totals[tree.root] == 17.0           # whole tree

# Soft-delete a subtree; aggregations still work and node indices stay valid.
tree.delete(p1)
assert tree.subtree_sum("mass")[tree.root] == 10.0

Dependencies

  • vcti-nputils >=1.6.0GrowableArray storage backing.
  • vcti-multi-valued-array >=1.5.0 — the CSR / ragged container for the parent→children index.
  • numpy>=1.26 — structured-array storage and vectorized operations.
  • numba>=0.59 — compiles the per-node kernels (DFS walk orders, ordered ancestor chains).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vcti_nptree-1.0.0.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vcti_nptree-1.0.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file vcti_nptree-1.0.0.tar.gz.

File metadata

  • Download URL: vcti_nptree-1.0.0.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for vcti_nptree-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3e54c78f24f3a10193c400d6706db003fad2c062d42d89a75786ad7c4c51e601
MD5 a55e57bca6388158ed7d79c22a423c11
BLAKE2b-256 3e80964473f3ce37d25e927a7989eb315885a73512f3a9c7d3d95800c29ecf17

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_nptree-1.0.0.tar.gz:

Publisher: release.yml on vcollab/vcti-python-nptree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vcti_nptree-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: vcti_nptree-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for vcti_nptree-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bad32e404a1938d7de14d75ca6392c2137bfbdd8269408646456c912077b836
MD5 9ed42fe0dd8d9b9ad6c69b42e228e997
BLAKE2b-256 690f5501a892fdb0e620183c946a18c7835eabf601e436d40930e30861d78058

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_nptree-1.0.0-py3-none-any.whl:

Publisher: release.yml on vcollab/vcti-python-nptree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page