Skip to content

gds_sim.compat

cadCAD signature detection and adaptation helpers.

Auto-detect and wrap cadCAD-style function signatures.

cadCAD policies have 4 positional args

(params, substep, state_history, previous_state) -> Signal

cadCAD state update functions have 5 positional args

(params, substep, state_history, previous_state, policy_input) -> (key, val)

gds-sim native signatures

policy: (state, params, kw) -> Signal suf: (state, params, signal=, kw) -> (key, val)

Detection runs once at Model construction time — zero cost in the hot loop.

adapt_policy(fn)

Wrap a cadCAD 4-arg policy to the gds-sim signature, or pass through.

Source code in packages/gds-sim/gds_sim/compat.py
def adapt_policy(fn: PolicyFn) -> PolicyFn:
    """Wrap a cadCAD 4-arg policy to the gds-sim signature, or pass through."""
    n = _positional_count(fn)
    if n == 4:

        def _wrapped(
            state: dict[str, Any], params: dict[str, Any], **kw: Any
        ) -> Signal:
            return fn(params, kw.get("substep", 0), [], state)

        return _wrapped
    return fn

adapt_suf(fn)

Wrap a cadCAD 5-arg SUF to the gds-sim signature, or pass through.

Source code in packages/gds-sim/gds_sim/compat.py
def adapt_suf(fn: SUFn) -> SUFn:
    """Wrap a cadCAD 5-arg SUF to the gds-sim signature, or pass through."""
    n = _positional_count(fn)
    if n == 5:

        def _wrapped(
            state: dict[str, Any],
            params: dict[str, Any],
            *,
            signal: dict[str, Any] | None = None,
            **kw: Any,
        ) -> tuple[str, Any]:
            return fn(params, kw.get("substep", 0), [], state, signal or {})

        return _wrapped
    return fn