itacart.serialization.data_tree_blob.encode_data_tree

itacart.serialization.data_tree_blob.encode_data_tree(data, variables, *, observed=None, resolution=None, prune=False)[source]

Encode values on cells as a DataTreeBlob.

The input is pairs, never an index: an index folds a daughter into its parent, and the daughter’s own value would go with it.

Parameters:
  • data (Union[Mapping[Any, Mapping[str, Any]], Iterable[tuple[Any, Mapping[str, Any]]]]) – The pyramid values, as a mapping of cells to {name: value} or as pairs of them. A cell is a spelling or an integer of the integer index. Every cell given is a leaf of the tree: the values of its ancestors are aggregated, never given. None is the variable’s absence.

  • variables (Iterable[DataVariable]) – The columns, as DataVariable.

  • observed (Union[Mapping[Any, Mapping[str, Any]], Iterable[tuple[Any, Mapping[str, Any]]], None], default: None) – The level values, in the same form, at any node.

  • resolution (int | None, default: None) – The observation resolution: the unit of every count and the floor of every read. Defaults to the finest cell in data. A leaf above it stands for the block of cells beneath it.

  • prune (bool, default: False) – Fold every complete family of leaves that agree on every variable into its parent, as a block, bottom-up. Off by default.

Return type:

bytes

Returns:

The blob. The same input gives the same bytes, in whatever order the pairs arrive.

Raises:
  • InvalidIndexError – If a cell is malformed.

  • NonExistentCellError – If a cell names no cell.

  • NonAtomicIndexError – If an entry holds more than one cell.

  • ResolutionError – If resolution is outside 0 to 13, or a cell is finer than it.

  • SerializationError – If the schema or a value is invalid: a name unknown or given twice, a value outside its type, a pyramid value at a node that has daughters, or a block that absorbs the border or whose sum does not divide evenly over its cells.

Example

>>> n = DataVariable("n", "u32", op="sum", leaf_max=1)
>>> blob = encode_data_tree(
...     {"NE(0625/0451(1))": {"n": 1}, "NE(0625/0451(2))": {"n": 1}}, [n]
... )
>>> read_data_cell(blob, "NE(0625/0451)")
{'n': 2}