pangolin.interface.graph_upstream

Contents

pangolin.interface.graph_upstream#

pangolin.interface.graph_upstream(vars, namespace=None, *, number_parents='auto')#

Print all upstream RVs and shapes as a Unicode DAG.

Circles are RVs. Edges always travel from top to bottom and from left to right; they never travel upward or leftward.

┼ is always a pure crossing: the horizontal and vertical lines do not connect there.

A node’s distinct parents are read from left to right by default. When that visual order differs from the operation’s parent order, automatic subscripts (₀, ₁, ₂, …) record the position of each parent. When the same parent appears in more than one position, ⁎ is used.

This gives a compact overview of chains and simple branches; for dense graphs or an explicit statement of every parent relationship, use print_upstream. Detailed rendering conventions are documented by pangolin.util.render_dag.

Parameters:
  • vars (PyTree[RV]) – An RV or a PyTree of RVs whose upstream graph is printed. Each distinct RV appears once.

  • namespace (Mapping[str, object] | None) – Optional mapping supplying names, such as locals(). Lists, tuples, and dictionary values are searched recursively; the first name found for each upstream RV is appended to its label. The namespace supplies labels only and does not add roots to the graph. Unnamed RVs have no name annotation. See pangolin.util.node_names for path naming rules.

  • number_parents (Literal['off', 'on', 'auto']) – Whether to number no edges, all edges, or only edges whose parent order or multiplicity would otherwise be ambiguous. A repeated parent is marked with ⁎ when annotations are enabled.

Raises:
  • TypeError – If an input leaf is not an RV.

  • ValueError – If number_parents is not "off", "on", or "auto"; or if parent annotations would be printed for a node with more than ten parent positions. Also raised if namespace traversal encounters a cyclic container or a name shared by distinct upstream RVs.

Return type:

None

Examples

A simple example:

>>> from pangolin import interface as pi
>>> x = pi.constant(3.3)
>>> y = pi.exp(x)
>>> z = pi.normal(x, y)
>>> graph_upstream(z, {"z": z})
○   () 3.3
│
├─○ () exp
│ │
└─○ () normal ← z

A more complex example:

>>> x = pi.constant(2)
>>> y = pi.exp(x)
>>> z = pi.log(x)
>>> u = pi.beta_binomial(x, y, z)
>>> v = y + u

These RVs form a small DAG that can be drawn spaciously as follows:

x
│
├─────────┬────┐
│         │    │
└───▶y    │    │
     │    ↓    │
     │    z    │
     │    │    │
     │    ↓    │
     ├───▶u ◀──┘
     │    │
     │    ↓
     └───▶v

graph_upstream draws the same DAG compactly, assigning each node its own row:

>>> graph_upstream(v, locals())
○     () 2 ← x
│
├─○   () exp ← y
│ │
├─┼─○ () log ← z
│ ├┐│
└─┼┴○ () beta_binomial ← u
  │ │
  └─○ () add ← v
>>> x = pi.constant([-3.0, 0.0, 2.5])
>>> y = pi.constant(3.1)
>>> z = pi.vfor(lambda i: pi.normal(pi.exp(x[i]), y), i=3)
>>> graph_upstream(z, {"z": z})
○   (3,) [-3.  0.  2.5]
│
○   (3,) vmap(exp, [0], 3)
│
│ ○ ()   3.1
│ │
└─○ (3,) vmap(normal, [0, None], 3) ← z

See also

print_upstream

Print upstream RVs as a table.

pangolin.util.render_dag

Generic renderer with a complete glyph reference.