Skip to content

Saving and loading

Call What it does
pf.to_json(graph, *, indent=2) The interchange format.
pf.from_json(text) Read one back.
pf.FORMAT, pf.VERSION The format tag and the schema version.
import probability_flow as pf

graph = pf.CausalGraph("example")
rain = graph.add_variable("It rained last night")
rain.set_prior(0.3)

text = pf.to_json(graph)
back = pf.from_json(text)
assert pf.to_json(back) == text          # a textual no-op, not merely equivalent

Reading a document and writing it back is a textual no-op. That is a stronger property than round-tripping to an equivalent graph, and it is what makes a diff between two documents mean something.

Versions

The schema version is the package version, and compatibility is the first two numbers. A reader refuses a document outside its own series rather than guessing at it, so a document written by 1.5 is refused by 1.4 instead of being read with a field quietly dropped.

The rule for which number moves: minor when an older reader stops working on a newer document, patch when nothing happens to any reader.

What a document looks like

{
  "format": "probability-flow/causal-graph",
  "version": "1.4.5",
  "name": "speeding-camera",
  "hypothesis": "v0",
  "variables": [
    {"id": "v5", "name": "The camera recorded a violation",
     "description": "What the device output.",
     "metadata": {"implicit_prior": 0.5}}
  ],
  "edges": [
    {"source": "v3", "target": "v0", "kind": "causal"},
    {"source": "v1", "target": "v4", "kind": "constitutive"}
  ],
  "cpds": [
    {"variable": "v5", "cpd": {
      "type": "independent_evidence",
      "parents": ["v2", "v3"],
      "baseline": 0.02,
      "weights": {"v2": 0.5998, "v3": 0.0},
      "gates": [{"source": "v3", "modulates": "v2",
                 "weight_when_gated": 8.7846}]
    }}
  ]
}

The other CPD types:

{"type": "noisy_or", "leak": 0.02,
 "inputs": [{"node": "v0", "activation": 0.8}]}

{"type": "logical", "operator": "and",
 "constituents": [{"node": "v1"}, {"node": "v2"}]}

{"type": "table", "parents": ["v0"], "rows": [[0.8, 0.2], [0.1, 0.9]]}

Three things to know when reading one.

A gated edge appears in the edge list as causal. What makes it gating is the gates entry in the child's CPD, so the test for a gating edge is whether the source appears in gates. Do not test the weight: zero is what the library writes for a gate source carrying no evidence of its own, but an ordinary cause can honestly carry zero too.

metadata is not validated. It is where implicit priors live, and where decoration can carry several wordings of the same edge with one promoted into description. That is what makes it useful and what makes it the wrong place to put something a reader must not see.

name and description do not affect the numbers. A variable's name is a full proposition, and its description says what the node is. Both are for a human or a decorator to read.

Two worked documents

They ship with the source distribution and the site serves them: speeding-camera.json is the short one, a definition and a gate in eleven variables, and twelve-angry-men.json is a 51-variable version with latent common causes for facts that travel together and gates running in both directions.