Serialization#

Chain serialization (decisions D31/D32).

A chain exports to JSON as a list of blocks {"id", "type", "params", "inputs"}. The intent is serialized – block types and constructor parameters – never derived state. Free arrays (an explicit alphabet, a carrier-type mask) go to a companion .npz file; the JSON stays readable and editable.

Known frontier (documented, not worked around): callables – callbacks, the rule of scattered_allocation – are not serializable.

Examples

>>> from comnumpy import Sequential, SymbolGenerator
>>> from comnumpy.core.channels import AWGN
>>> chain = Sequential([SymbolGenerator(4, seed=1), AWGN(sigma2=0.1, seed=2)])
>>> text = to_json(chain)
>>> chain2 = from_json(text)
>>> chain2.block_ids()
['generator', 'awgn']
to_json(chain: Sequential, path: str | Path | None = None, npz_path: str | Path | None = None, indent: int = 2) → str[source]#

Serialize a Sequential chain to JSON (+ optional array sidecar).

Parameters:
  • chain (Sequential) – The chain to serialize.

  • path (str or Path, optional) – Where to write the JSON. When omitted, the text is only returned.

  • npz_path (str or Path, optional) – Companion file for array-valued parameters. Required if the chain contains any (an explicit alphabet, a mask…).

  • indent (int, optional) – JSON indentation. Default 2.

Returns:

str – The JSON document.

from_json(source: str | Path, npz_path: str | Path | None = None) → Sequential[source]#

Rebuild a Sequential chain from to_json() output.

Parameters:
  • source (str or Path) – JSON text, or path to a JSON file.

  • npz_path (str or Path, optional) – Companion array file, required when the document references one.

register_block(cls: type) → type[source]#

Class decorator adding a user block to the deserialization registry.

Works for Processor subclasses and for the plain dataclasses a block may take as a parameter.