cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.650: full source
This commit is contained in:
1 parent
85edd56ec8
commit
3edd015039
20 files changed
+2021
-35
No files matched your search
@@ -0,0 +1,151 @@
|
||||
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||
"""A LightGBM regression booster as numbers the box can carry and evaluate
|
||||
without LightGBM (docs/methods.md B9). Standard library only; one file for
|
||||
the cloud (building/) and the box (edge/cantabile_edge/lgbtrees.py, a copy:
|
||||
building/test_boxmodel.py checks they are the same).
|
||||
|
||||
pack(dump) takes LightGBM's dump_model() (one tree per round, numerical
|
||||
splits only, objective regression: no output transform) and gives
|
||||
{"enc": "lgbm-trees-v1", "n": trees, "data": base64(zlib(trees))}; per
|
||||
tree, little-endian: uint16 the number of leaves L; L-1 internal nodes of
|
||||
(uint8 feature, uint8 decision: bit 1 default left, bits 2-3 the missing
|
||||
type 0 none / 1 zero / 2 NaN, int16 left, int16 right: >= 0 an internal
|
||||
node, < 0 the leaf ~child, float64 threshold); L float64 leaf values.
|
||||
Nodes by LightGBM's split_index, leaves by its leaf_index. Doubles as they
|
||||
are.
|
||||
|
||||
Trees(enc, n_features).predict(rows) sums the leaves in LightGBM's order
|
||||
with its decision rule (tree.h NumericalDecision), so it gives LightGBM's
|
||||
raw score to the last bit (measured 2026-11-04: max
|
||||
relative difference 0.0 over 192 slots)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import math
|
||||
import struct
|
||||
import zlib
|
||||
|
||||
ENCODING = "lgbm-trees-v1"
|
||||
NODE = struct.Struct("<BBhhd")
|
||||
MISSING = {"None": 0, "Zero": 1, "NaN": 2}
|
||||
ZERO = 1.0000000180025095e-35 # LightGBM's kZeroThreshold (1e-35f)
|
||||
MAX_TREES, MAX_LEAVES, MAX_RAW = 2000, 1024, 4 << 20
|
||||
|
||||
|
||||
def _ref(n: dict) -> int:
|
||||
return int(n["split_index"]) if "split_index" in n else ~int(n.get("leaf_index", 0))
|
||||
|
||||
|
||||
def pack_tree(root: dict) -> bytes:
|
||||
"""One tree (dump_model()'s tree_info[i]["tree_structure"])."""
|
||||
if "split_index" not in root: # no split: one leaf
|
||||
return struct.pack("<Hd", 1, float(root["leaf_value"]))
|
||||
nodes: dict[int, dict] = {}
|
||||
leaves: dict[int, float] = {}
|
||||
stack = [root]
|
||||
while stack:
|
||||
n = stack.pop()
|
||||
if "split_index" in n:
|
||||
if n.get("decision_type", "<=") != "<=":
|
||||
raise ValueError("a categorical split: not supported")
|
||||
nodes[int(n["split_index"])] = n
|
||||
stack += [n["left_child"], n["right_child"]]
|
||||
else:
|
||||
leaves[int(n["leaf_index"])] = float(n["leaf_value"])
|
||||
k = len(leaves)
|
||||
if sorted(nodes) != list(range(k - 1)) or sorted(leaves) != list(range(k)):
|
||||
raise ValueError("a tree's node or leaf indices are not contiguous")
|
||||
out = [struct.pack("<H", k)]
|
||||
for i in range(k - 1):
|
||||
n = nodes[i]
|
||||
dec = (2 if n.get("default_left") else 0) | (MISSING[n.get("missing_type", "None")] << 2)
|
||||
out.append(NODE.pack(int(n["split_feature"]), dec, _ref(n["left_child"]), _ref(n["right_child"]),
|
||||
float(n["threshold"])))
|
||||
out.append(struct.pack(f"<{k}d", *(leaves[i] for i in range(k))))
|
||||
return b"".join(out)
|
||||
|
||||
|
||||
def pack(dump: dict) -> dict:
|
||||
"""A booster's dump_model() as lgbm-trees-v1."""
|
||||
if dump.get("num_class", 1) != 1 or dump.get("num_tree_per_iteration", 1) != 1:
|
||||
raise ValueError("one tree per round only")
|
||||
obj = str(dump.get("objective") or "").split(" ")[0]
|
||||
if obj != "regression":
|
||||
raise ValueError(f"objective {obj!r}: regression only (no output transform)")
|
||||
raw = b"".join(pack_tree(t["tree_structure"]) for t in dump["tree_info"])
|
||||
return {"enc": ENCODING, "n": len(dump["tree_info"]), "data": base64.b64encode(zlib.compress(raw, 9)).decode()}
|
||||
|
||||
|
||||
class Trees:
|
||||
"""lgbm-trees-v1, checked (ValueError): every child a later node or a
|
||||
leaf (no loops), features in range, finite leaves."""
|
||||
|
||||
def __init__(self, enc: dict, n_features: int):
|
||||
if not isinstance(enc, dict) or enc.get("enc") != ENCODING:
|
||||
raise ValueError(f"trees: {ENCODING} only")
|
||||
z = zlib.decompressobj()
|
||||
try:
|
||||
raw = z.decompress(base64.b64decode(str(enc.get("data")), validate=True), MAX_RAW)
|
||||
except zlib.error as e:
|
||||
raise ValueError(f"trees: {e}") from None
|
||||
if z.unconsumed_tail:
|
||||
raise ValueError("trees: too large")
|
||||
if not z.eof:
|
||||
raise ValueError("trees: truncated")
|
||||
self.trees = []
|
||||
off = 0
|
||||
try:
|
||||
while off < len(raw):
|
||||
(k,) = struct.unpack_from("<H", raw, off)
|
||||
off += 2
|
||||
if not 1 <= k <= MAX_LEAVES:
|
||||
raise ValueError("trees: leaves")
|
||||
nodes = []
|
||||
for i in range(k - 1):
|
||||
f, dec, left, right, threshold = NODE.unpack_from(raw, off)
|
||||
off += NODE.size
|
||||
for c in (left, right):
|
||||
if not (i < c < k - 1 or -k <= c < 0):
|
||||
raise ValueError("trees: a child out of range")
|
||||
if f >= n_features or dec & 1 or (dec >> 2) & 3 > 2 or math.isnan(threshold):
|
||||
raise ValueError("trees: a split")
|
||||
nodes.append((f, threshold, bool(dec & 2), (dec >> 2) & 3, left, right))
|
||||
leaves = struct.unpack_from(f"<{k}d", raw, off)
|
||||
off += 8 * k
|
||||
if not all(math.isfinite(v) for v in leaves):
|
||||
raise ValueError("trees: a leaf")
|
||||
self.trees.append((nodes, leaves))
|
||||
if len(self.trees) > MAX_TREES:
|
||||
raise ValueError("trees: too many")
|
||||
except struct.error as e:
|
||||
raise ValueError(f"trees: truncated ({e})") from None
|
||||
if len(self.trees) != int(enc.get("n", -1)):
|
||||
raise ValueError("trees: count")
|
||||
|
||||
def predict_row(self, x) -> float:
|
||||
"""LightGBM's raw score for one row: per tree, a split goes left
|
||||
where the value is <= its threshold; a NaN counts 0 unless the
|
||||
split knows NaN; the split's missing values (zero or NaN, by its
|
||||
type) go its default way; the leaves summed in order."""
|
||||
total = 0.0
|
||||
for nodes, leaves in self.trees:
|
||||
if not nodes:
|
||||
total += leaves[0]
|
||||
continue
|
||||
i = 0
|
||||
while i >= 0:
|
||||
f, threshold, default_left, missing, left, right = nodes[i]
|
||||
v = x[f]
|
||||
if v != v and missing != 2:
|
||||
v = 0.0
|
||||
if (missing == 1 and -ZERO <= v <= ZERO) or (missing == 2 and v != v):
|
||||
i = left if default_left else right
|
||||
else:
|
||||
i = left if v <= threshold else right
|
||||
total += leaves[~i]
|
||||
return total
|
||||
|
||||
def predict(self, rows) -> list[float]:
|
||||
"""Rows: a list of lists or a 2-D numpy array (as Booster.predict)."""
|
||||
rows = rows.tolist() if hasattr(rows, "tolist") else rows
|
||||
return [self.predict_row([float(v) for v in r]) for r in rows]
|
||||
Reference in new issue
Block a user