Ptree: Interactive Tree of Life Explorer Blog & articles

Blog · Data · · 8 min read

A depth-first columnar index answers filtered summaries in milliseconds

Per-clade box plots and category shares bound a filtered mean only loosely: they never say whether the species assessed for conservation status are the species weighed for body mass. A bound that assumes they are excludes the exact answer on 8 of 26 benchmark queries, by up to ×3.69 for the mass of group-living mammals. Dropping the assumption gives the worst-case interval of Manski's partial identification, which held at every one of 17,126 frontiers recorded and is still ×3.48 wide at the median after sixty clades are opened.

The server has the whole taxonomy and every leaf's traits on local disk, so Ptree answers filtered questions from the leaves. A depth-first columnar index and an H3 inverted index (filter_index.sqlite) give exact filtered summaries of all 42 indexed properties in 1 to 150 ms, and the browser shows its pooled estimate only until the exact one arrives.

Numbering the tree once

Every backbone's terminal leaves are numbered 0 .. S − 1 in depth-first order. Every node in the tree, from kingdom to leaf, becomes one contiguous half-open interval [start, end), the containment encoding Zhang and colleagues used for trees in relational stores. On GBIF (2.94 million nodes, 2.67 million leaves), Mammalia is a 22,514-leaf slice, Aves 15,516, and Animalia 1.86 million, counting fossils and subspecies. A single primary-key lookup in spans(backbone, id, start, end) turns any clade into two integers, and a 334 KB bitset marks fossil leaves (fossils=hide is one bit test per leaf).

The same pass turns the rules the rollup applies while folding the tree into plain facts on each leaf, before any column is written:

  • A fossil node marks every leaf in its span as fossil, so descendants with no fossil record of their own are hidden exactly as the rollup hides them.
  • Species-with-subspecies overrides (ownPolicy: "species") push a species' own categorical vector across its subspecies span and pin its own numeric observation to the first living leaf in that span so it is counted once rather than per subspecies and never hidden by a fossil sibling.
  • Range overrides (isSpeciesOrBelow) follow map-range.mjs: a species with its own range marks every leaf in its span and ignores subspecies ranges; one without merges its subspecies' ranges.

An unfiltered query over a clade's whole interval reproduces the clade's stored rollup: categorical counts match exactly across Mammalia, Aves, and Amphibia (conservation, setting, nutrition), and numeric geometric means within 0.4%.

Fractional leaves without virtual rows

Leaf traits in Ptree are not booleans. Multi-bucket sets and multi-facet properties split unit mass across buckets: GBIF has 1.57 million fractional leaves in construction, 647,000 in developmentmode, 517,000 in reproduction, and 111,000 in nutrition (for example, 0.7 herbivorous and 0.3 predatory). A plain bitmask drops those weights, and splitting each leaf into one binary row per bucket multiplies every scan by the number of buckets a leaf touches.

Even across 2.67 million leaves, each categorical property has only tens to a few hundred distinct weight vectors once separated from free-text claims. Each categorical column stores a small dictionary of { weights, sources } entries and one Uint8 or Uint16 code per leaf (0 = unrecorded). Column stores call this dictionary encoding and query it without decompressing. The index stores each column dense or sparse, whichever is smaller: sparse costs 4 + c bytes per recorded leaf against c bytes per leaf dense, so dense wins above 20% coverage for Uint8 codes and 33% for Uint16.

At query time, a categorical filter evaluates categoricalShare once per dictionary entry (< 0.01 ms), turning every leaf's match weight into a single array lookup. Multiple filters multiply leaf weights, and aggregating a categorical target accumulates weights into a tiny Float64Array(dict.length) before expanding buckets and combining the sources' bitmasks once at the end.

Point numeric properties (mass, longevity, offspring, etc.) use a sorted Float64 value dictionary with Uint16/Uint32 value codes and Uint8 source codes. Float64 keeps the min and max the API sends identical to the recorded values, a numeric window from..to becomes a contiguous code interval [loCode, hiCode], and weighted quantiles are exact at every sample size, from sorted matched pairs when few and a prefix scan of Float64Array(vals.length) when many. Interval-valued distribution, channel, envelope, extreme, and paired properties keep their multi-coordinate Float64 streams and follow the same weighted midpoint rules as the rollup.

Recorded-only semantics

Unfiltered bars and histograms in Ptree show only recorded data, washing saturation by coverage to signal what is missing, and a filter keeps that rule: a species with no record of the filter's property does not match. Filtering by conservation:CR|EN|VU summarizes species assessed as threatened, and the browser's pooled estimate and the index answer the same question. Letting an unrecorded species inherit its relatives' share answers a different one: for the mass of group-living mammals, the inheriting reading is several times lighter than the recorded one, because the bats that lack a social record would inherit their relatives' mostly social one. On threatened marine mammals, the exact geometric mean is 361 kg, over 29.2 matched leaves of which 26.4 are weighed (a leaf counts by its match weight, so the totals are fractional).

H3 postings at resolutions 1, 2, and 3

Range maps in Ptree live on H3 resolution 3 (41,162 cells globally; 123,910 iNaturalist models and 5,741 MDD expert ranges, ~61 million taxon-cell pairs on GBIF). Visiting leaves in DFS order produces ascending leaf indices for every cell, so a counting pass (count per cell, then scatter) writes one flat Uint32Array with no comparison sort, delta-varint encodes each cell's posting list as an inverted file does, and zstd-compresses it into h3_postings(backbone, cell, ids).

A single-point query (51.5,-0.1) reads one posting list and binary-searches [start, end). A country like the United States spans 905 res-3 cells, and decoding 905 blobs on every query takes 61 ms on Mammalia. Storing union postings for res-2 (5,882 cells) and res-1 (842 cells) alongside res-3 adds 6,724 rows per backbone and lets the server run compactCells on any multi-cell place before reading postings:

Place Res-3 cells Compacted (res 1–3) Posting lookup, Mammalia, cold Posting lookup, warm
51.5,-0.1 (London) 1 1 2.1 ms 0.8 ms
US-TN (Tennessee) 18 12 3.1 ms 0.7 ms
GB (United Kingdom) 48 36 3.9 ms 0.7 ms
US (United States) 905 305 20.7 ms 3.5 ms

Multi-cell places union their slices into a bitset over [start, end), counting every widespread species once instead of approximating occupancy from the single densest cell. Compaction happens on the server inside refineNode, so sidebar clicks, hover previews, and /ask tours naming a state or country all get the compacted lookup.

Query latency

The full 11-backbone build (node api/extract.mjs filter_index) produces 13.1 million spans, every indexed property's column, and all 47,886 res-1/2/3 H3 postings in 169 seconds (1.4 GB on disk), with per-property and per-backbone incremental signatures so a single changed property rebuilds in under a second. Columns decode lazily on first use and stay in memory; decoded H3 posting lists sit behind a 2,048-entry LRU cache.

The time below is the whole query as the server reports it, posting lookup included, the median of five requests after one that loads the columns, and matched is the summed weight of the leaves the filters keep:

Query (gbif, fossils hidden) Leaves in the clade Warm Matched
Mammalia · conservation:CR|EN|VU, setting:ocean 22,514 4.0 ms 29.2
Mammalia · mass:10000.., diel:nocturnal 22,514 4.1 ms 82.4
Mammalia · 51.5,-0.1 22,514 1.3 ms 101
Mammalia · GB 22,514 1.3 ms 127
Mammalia · US 22,514 2.7 ms 560
Aves · diel:nocturnal 15,516 2.1 ms 368.5
Aves · conservation:CR|EN|VU, US 15,516 2.0 ms 105
Animalia · conservation:CR|EN|VU 1,862,533 23.1 ms 17,350.5
Animalia · 51.5,-0.1 1,862,533 10.6 ms 6,674
Animalia · nutrition:predatory 1,862,533 143 ms 289,184.1
Tracheophyta · growthform:tree 467,998 111 ms 65,958.5

Exact answers at these speeds run inline on the HTTP worker, so /refined takes only a filter, a place, and the fossil setting. The browser pools loaded children at once on a filter click or map scrub, morphs to the exact response when it lands, waits for thumb release on numeric drags, and debounces sidebar map hover by one second so resting on any spot snaps to the exact species-level readout.

Filtered range maps from the same store

Because refineNode already produces the exact weighted leaf set for any filter combination, adding a forward table leaf_ranges (dfsIndex → modeled cells, precise cells) alongside the inverted h3_postings lets the server compute live filtered range maps too. Each mapped species is pinned to its first living leaf (matching map-range.mjs, which counts a species' own range once per cell in modeled and precise), delta-encoded in Uint16 H3 dictionary IDs (0..41,161), and packed in 256-leaf zstd chunks during the same H3 pass (+11.6 s build time, +0.1 GB across all 11 backbones).

Checking unfiltered full-span accumulation against the precomputed maps.sqlite rollup across Mammalia, Aves, Amphibia, Plantae, and Animalia gives modeledIoU = 1.0000, preciseIoU = 1.0000, maxDiff = 0 across all 41,162 cells, and identical generated geographic descriptions (sameWords = true) on all five clades.

Contour shaping over up to 41,000 H3 cells takes 38–392 ms, so /refined/map runs on heavypool rather than inline, keeping /refined sidebar numbers unblocked at 1–150 ms. As every property ignores its own filter, the map ignores the place (filtering by diel:nocturnal and GB still shows the global range of nocturnal mammals with GB outlined on top) and stays separate from taxon.range so client-side location hit-testing is never disturbed.

Filtered map (gbif, fossils hidden) Thumb Detail Range coverage Generated description
Amphibia · parentalcare:brooding 41 ms 38 ms 32 / 109 (29%) modeled: eastern Africa; the Amazon basin; northwestern Europe; 26 smaller patches
Aves · diel:nocturnal 133 ms 148 ms 217 / 920 (24%) modeled: nearly the whole Earth
Mammalia · diel:nocturnal, motility:flying 146 ms 147 ms 1,048 / 1,934 (54%) expert-assessed and modeled: across the world's land from 40°S to 70°N; 7 smaller patches
Plantae · growthform:tree, persistence:evergreen 229 ms 218 ms 1,920 / 8,778 (22%) modeled: around the world from 40°S to 70°N; 22 smaller patches
Mammalia · conservation:CR|EN|VU, setting:ocean 289 ms 265 ms 28 / 46 (61%) expert-assessed: around the world's oceans from 75°S to 80°N; 5 smaller patches
Mammalia · mass:80000.. 294 ms 302 ms 212 / 282 (75%) expert-assessed and modeled: nearly the whole Earth
Animalia · conservation:CR|EN|VU 392 ms 369 ms 2,464 / 18,912 (13%) expert-assessed and modeled: nearly the whole Earth