pangolin.interface.signature#
- pangolin.interface.signature(fun, *, in_ops=None, out_ops=None, ops=False)#
Return a function that describes the inputs and output of each call as a string.
Calling
signature(fun)(*args, **kwargs)callsfunonce with the supplied arguments unchanged and returns the string(inputs) -> output. No coercion, abstract substitution, sampling, or numerical evaluation is added by this combinator. Any such behavior infunitself still occurs.RVs are displayed as
RV()for scalars,RV(3,)for vectors, andRV(2, 3)for matrices. If enabled, the Op’s str is appended after the dimensions, as inRV(3, vmap(normal, [0, None], 3)). Lists, tuples, and dictionaries are described recursively; dictionary keys and other values use their repr. Positional parameter names are omitted, while supplied keyword names are retained.- Parameters:
fun (Callable[[P], object]) – Function whose calls should be summarized.
in_ops (bool | None) – Whether to show the Op of each input RV. None uses
ops.out_ops (bool | None) – Whether to show the Op of each output RV. None uses
ops.ops (bool) – Show both input and output Ops. Default: False. Setting this to True conflicts with an explicit
in_ops=Falseorout_ops=Falseand raises ValueError.
- Returns:
describe_fun – Function with the same accepted arguments as
fun. It returns a summary string, rather thanfun’s result, without printing it.- Return type:
Callable[[P], str]
Notes
This describes one observed call, not a general static type signature. Ordinary Python values can change the output structure or shape, so their values are shown rather than just their types. Omitted defaults are not listed. Input descriptions are recorded before the call, and Python side effects and exceptions from
funare preserved.An RV’s displayed Op is only its own Op, not its parents’ graphs. Use
ir.print_upstreamto inspect the full graph. Interface properties are neither changed nor displayed.Examples
Inspect how
pi.normalcoerces Python values into RVs and broadcasts scalar and array inputs. Inputs are shown as supplied, before coercion:>>> from pangolin import interface as pi >>> print(signature(pi.normal)(pi.constant(0.0), pi.constant(1.0))) (RV(), RV()) -> RV() >>> print(signature(pi.normal)(pi.constant([0.0, 1.0]), pi.constant(1.0))) (RV(2,), RV()) -> RV(2,) >>> print(signature(pi.normal)(0.0, 1.0)) (0.0, 1.0) -> RV() >>> print(signature(pi.normal)([0.0, 1.0], 1.0)) ([0.0, 1.0], 1.0) -> RV(2,)
Optionally include Ops alongside shapes, for outputs alone or for both inputs and outputs:
>>> print(signature(pi.normal, out_ops=True)(pi.constant([0.0, 1.0]), pi.constant(1.0))) (RV(2,), RV()) -> RV(2, vmap(normal, [0, None], 2)) >>> x = pi.normal(0., 1.) >>> print(signature(pi.exp, in_ops=True)(x)) (RV(normal)) -> RV() >>> print(signature(pi.exp, ops=True)(x)) (RV(normal)) -> RV(exp)
Inspect a pairwise squared-distance matrix built with two named
vforindices:>>> def distances(x): ... return pi.vfor( ... lambda i, j: pi.sum((x[i, :] - x[j, :]) ** 2, axis=0), ... i=x.shape[0], ... j=x.shape[0], ... ) >>> print(signature(distances)(pi.constant([[0., 0.], [1., 2.], [3., 1.]]))) (RV(3, 2)) -> RV(3, 3)
Input and output PyTree structure remains visible:
>>> def model(parameters): ... x = pi.normal(parameters['mean'], parameters['scales'][0]) ... return {'draw': x, 'derived': (pi.exp(x), x)} >>> print(signature(model)({'mean': pi.constant(0.0), 'scales': [pi.constant(1.0)]})) ({'mean': RV(), 'scales': [RV()]}) -> {'draw': RV(), 'derived': (RV(), RV())}
Captured RVs are not call arguments and do not appear in the signature:
>>> scale = pi.exponential(1.0) >>> def model(mean): ... return pi.normal(mean, scale) >>> print(signature(model)(pi.constant(0.0))) (RV()) -> RV()
PyTree structure and keyword names remain visible, even when the output structure depends on a Python argument:
>>> def model(parameters, *, squared=False): ... x = pi.normal(parameters['mean'], parameters['scale']) ... if squared: ... return {'x': x, 'squared': x * x} ... else: ... return x >>> print(signature(model)({'mean': 0.0, 'scale': 1.0}, squared=True)) ({'mean': 0.0, 'scale': 1.0}, squared=True) -> {'x': RV(), 'squared': RV()} >>> print(signature(model)({'mean': 0.0, 'scale': 1.0}, squared=False)) ({'mean': 0.0, 'scale': 1.0}, squared=False) -> RV()