|
| 1 | +# Portable Binary Format (`.pcb`) |
| 2 | + |
| 3 | +PyChebyshev v0.14 introduced a portable binary serialization format alongside |
| 4 | +the default pickle format. The goal: let consumers in **C, Rust, Julia, or |
| 5 | +any other language** read PyChebyshev interpolants without a Python runtime. |
| 6 | + |
| 7 | +The format is intentionally minimal — a fixed header, length-prefixed |
| 8 | +sections, raw little-endian `f64` blobs. A C reference reader at |
| 9 | +`examples/binary_reader/` weighs ~240 lines. |
| 10 | + |
| 11 | +## When to use which format |
| 12 | + |
| 13 | +| Format | Use when | |
| 14 | +|---|---| |
| 15 | +| **Pickle** (default) | Python-only round-trips; need full fidelity (build metadata, error caches) | |
| 16 | +| **Binary** (`.pcb`) | Cross-language consumers; sharing models with C/Rust/Julia code; long-term archival without Python pickle compatibility risk | |
| 17 | + |
| 18 | +Pickle stays the default because every existing user keeps working with no |
| 19 | +change. Opt into binary explicitly: |
| 20 | + |
| 21 | +```python |
| 22 | +cheb.save("model.pcb", format='binary') # portable |
| 23 | +cheb.save("model.pkl") # pickle (default) |
| 24 | + |
| 25 | +ChebyshevApproximation.load("model.pcb") # auto-detects |
| 26 | +``` |
| 27 | + |
| 28 | +`load()` sniffs the first 4 bytes — `b"PCB\x00"` routes to the binary |
| 29 | +reader, anything else to the pickle reader. |
| 30 | + |
| 31 | +## Coverage in v0.14 |
| 32 | + |
| 33 | +- **`ChebyshevApproximation`** — full support. |
| 34 | +- **`ChebyshevSpline`** — full support, with one restriction: the spline |
| 35 | + must use **flat** `n_nodes` (a single `int` per dim, shared across pieces). |
| 36 | + Splines built with nested per-piece `n_nodes` (the `[[n00, n01], …]` form |
| 37 | + introduced for special points) cannot be saved as `.pcb` because the |
| 38 | + underlying `ChebyshevSpline.from_values()` factory does not yet support |
| 39 | + that shape; use pickle for those. |
| 40 | +- **`ChebyshevSlider`**, **`ChebyshevTT`** — pickle only in v0.14. |
| 41 | + |
| 42 | +## Format specification (v1) |
| 43 | + |
| 44 | +All multi-byte fields are **little-endian**. Numeric arrays are raw `f64` |
| 45 | +blobs in C-order. |
| 46 | + |
| 47 | +### Header (12 bytes) |
| 48 | + |
| 49 | +``` |
| 50 | +offset size field |
| 51 | +0 4 magic = b"PCB\x00" |
| 52 | +4 1 major_version = 1 |
| 53 | +5 1 minor_version = 0 |
| 54 | +6 2 class_tag 1 = ChebyshevApproximation, 2 = ChebyshevSpline |
| 55 | +8 4 reserved = 0x00000000 |
| 56 | +``` |
| 57 | + |
| 58 | +### `ChebyshevApproximation` body (`class_tag = 1`) |
| 59 | + |
| 60 | +``` |
| 61 | +uint32 num_dimensions d |
| 62 | +f64[d] domain_lo [a_0, ..., a_{d-1}] |
| 63 | +f64[d] domain_hi [b_0, ..., b_{d-1}] |
| 64 | +uint32[d] n_nodes [n_0, ..., n_{d-1}] |
| 65 | +f64[prod(n_nodes)] tensor_values C-order |
| 66 | +``` |
| 67 | + |
| 68 | +`barycentric_weights` and `diff_matrices` are **not** stored; they are |
| 69 | +recomputed from `(domain, n_nodes)` on load (they are pure functions of |
| 70 | +those primitives). |
| 71 | + |
| 72 | +### `ChebyshevSpline` body (`class_tag = 2`) |
| 73 | + |
| 74 | +``` |
| 75 | +uint32 num_dimensions d |
| 76 | +f64[d] domain_lo |
| 77 | +f64[d] domain_hi |
| 78 | +uint32[d] n_nodes shared across pieces |
| 79 | +uint32[d] num_knots_per_dim [k_0, ..., k_{d-1}] |
| 80 | +f64[k_0 + ... + k_{d-1}] knots_concatenated flat, dim-by-dim |
| 81 | +uint32 num_pieces P = prod(k_i + 1) |
| 82 | +
|
| 83 | +# P piece blocks, in C-order over the piece grid: |
| 84 | +for p in 0..P-1: |
| 85 | + f64[prod(n_nodes)] tensor_values_p C-order |
| 86 | +``` |
| 87 | + |
| 88 | +### Versioning policy |
| 89 | + |
| 90 | +- New required fields → bump **major**. v1 readers reject `major != 1`. |
| 91 | +- New optional trailing fields → bump **minor**. v1 readers ignore unknown |
| 92 | + trailing data. |
| 93 | +- Reserved header bytes MUST be zero in v1. |
| 94 | + |
| 95 | +## Worked example: `f(x,y) = x + y` |
| 96 | + |
| 97 | +Python side: |
| 98 | + |
| 99 | +```python |
| 100 | +from pychebyshev import ChebyshevApproximation |
| 101 | + |
| 102 | +cheb = ChebyshevApproximation( |
| 103 | + function=lambda pt, _: pt[0] + pt[1], |
| 104 | + num_dimensions=2, |
| 105 | + domain=[(-1.0, 1.0), (-1.0, 1.0)], |
| 106 | + n_nodes=[3, 3], |
| 107 | +) |
| 108 | +cheb.build() |
| 109 | +cheb.save("xy.pcb", format='binary') |
| 110 | +``` |
| 111 | + |
| 112 | +The resulting file is exactly **128 bytes**: |
| 113 | + |
| 114 | +``` |
| 115 | +12 header |
| 116 | + 4 num_dimensions = 2 |
| 117 | +16 domain_lo = [-1.0, -1.0] |
| 118 | +16 domain_hi = [ 1.0, 1.0] |
| 119 | + 8 n_nodes = [3, 3] |
| 120 | +72 tensor_values (3 × 3 f64) |
| 121 | +``` |
| 122 | + |
| 123 | +C reader: |
| 124 | + |
| 125 | +```bash |
| 126 | +cd examples/binary_reader |
| 127 | +make |
| 128 | +./reader ../../xy.pcb 0.3 0.4 |
| 129 | +# 0.69999999999999996 |
| 130 | +``` |
| 131 | + |
| 132 | +The same IEEE-754 double Python returns (`repr` truncates trailing digits): |
| 133 | + |
| 134 | +```python |
| 135 | +cheb.eval([0.3, 0.4], [0, 0]) # 0.7 |
| 136 | +``` |
| 137 | + |
| 138 | +The two strings render the same `float64` value `0x3fe6666666666666`. The |
| 139 | +C reader prints with `%.17g`, Python with `repr` — they agree bit-for-bit. |
| 140 | + |
| 141 | +### Spline worked example: `|x|` on `[-1, 1]` |
| 142 | + |
| 143 | +```python |
| 144 | +from pychebyshev import ChebyshevSpline |
| 145 | + |
| 146 | +s = ChebyshevSpline( |
| 147 | + function=lambda pt, _: abs(pt[0]), |
| 148 | + num_dimensions=1, |
| 149 | + domain=[(-1.0, 1.0)], |
| 150 | + n_nodes=[3], |
| 151 | + knots=[[0.0]], |
| 152 | +) |
| 153 | +s.build() |
| 154 | +s.save("abs.pcb", format='binary') |
| 155 | +``` |
| 156 | + |
| 157 | +The resulting file is exactly **100 bytes**: |
| 158 | + |
| 159 | +``` |
| 160 | +12 header |
| 161 | + 4 num_dimensions = 1 |
| 162 | + 8 domain_lo = [-1.0] |
| 163 | + 8 domain_hi = [ 1.0] |
| 164 | + 4 n_nodes = [3] |
| 165 | + 4 num_knots = [1] |
| 166 | + 8 knots = [0.0] |
| 167 | + 4 num_pieces = 2 |
| 168 | +48 piece tensor values (2 pieces × 3 × f64) |
| 169 | +``` |
| 170 | + |
| 171 | +Two pieces because one knot at `0.0` splits the domain `[-1, 1]` into `[-1, 0]` |
| 172 | +and `[0, 1]`. Each piece carries its own 3-node Chebyshev grid. |
| 173 | + |
| 174 | +## Writing a reader in another language |
| 175 | + |
| 176 | +The format is small enough to implement in an afternoon: |
| 177 | + |
| 178 | +1. Read 4 bytes; verify equal to `b"PCB\x00"`. |
| 179 | +2. Read major/minor version; reject unknown major. |
| 180 | +3. Read class tag; dispatch. |
| 181 | +4. For class 1: read `uint32 d`, then `d × f64` for `lo`, `d × f64` for `hi`, |
| 182 | + `d × uint32` for `n_nodes`, then `prod(n_nodes) × f64` for tensor values. |
| 183 | +5. To evaluate, generate Chebyshev first-kind nodes per dim, compute |
| 184 | + barycentric weights from node positions, evaluate by dim-by-dim collapse. |
| 185 | + |
| 186 | +`examples/binary_reader/reader.c` is the reference. It is intentionally |
| 187 | +minimal: ~240 lines, stdlib + `libm` only. |
| 188 | + |
| 189 | +## What the format does not store |
| 190 | + |
| 191 | +These fields are dropped on `format='binary'`: |
| 192 | + |
| 193 | +| Field | Replacement | |
| 194 | +|---|---| |
| 195 | +| `function` | always dropped (also dropped by pickle) | |
| 196 | +| `barycentric_weights`, `diff_matrices` | recomputed on load | |
| 197 | +| `_cached_error_estimate` | recomputed lazily | |
| 198 | +| `build_time`, `n_evaluations`, `method` | not preserved (use pickle for full fidelity) | |
| 199 | +| `max_derivative_order` | resets to default `2` on load — re-set manually after `load()` if you need higher orders | |
| 200 | + |
| 201 | +If you need any of those preserved, use pickle. |
| 202 | + |
| 203 | +## Security |
| 204 | + |
| 205 | +The binary reader does no `pickle.loads`-style code execution. It can be |
| 206 | +used to load files from untrusted sources — it will reject malformed |
| 207 | +files with a `ValueError`. |
| 208 | + |
| 209 | +Pickle remains the default and **does** execute arbitrary code from |
| 210 | +loaded files. Treat pickle files like executable code. |
0 commit comments