|
| 1 | +"""Generate the pipeline diagrams used in the explanation pages. |
| 2 | +
|
| 3 | +Three committed figures come from one four-module example pipeline, defined in |
| 4 | +``scripts/pipeline_example/``: |
| 5 | +
|
| 6 | +- ``pipeline-modules.svg`` — the whole pipeline at the table level, dashed |
| 7 | + clusters grouping each module (``src/explanation/data-pipelines.md``). |
| 8 | +- ``pipeline-modules-collapsed.svg`` — the same pipeline at the module level, |
| 9 | + one node per schema, via ``Diagram.collapse()`` (same page). |
| 10 | +- ``imaging-schema.svg`` — the ``imaging`` module on its own |
| 11 | + (``src/explanation/relational-workflow-model.md``, ``src/index.md``). |
| 12 | +
|
| 13 | +Keeping the pipeline in the repo makes the figures reproducible. The prose |
| 14 | +describes specific edges, tiers, and table counts; those claims are only |
| 15 | +checkable if the pipeline that produced them can be rebuilt. |
| 16 | +
|
| 17 | +Usage |
| 18 | +----- |
| 19 | +Needs a database and a graphviz ``dot`` on PATH:: |
| 20 | +
|
| 21 | + docker compose up -d postgres |
| 22 | + DJ_HOST=localhost DJ_PORT=5432 DJ_USER=postgres DJ_PASS=tutorial \ |
| 23 | + DJ_BACKEND=postgresql DJ_USE_TLS=false \ |
| 24 | + python scripts/gen_pipeline_diagrams.py |
| 25 | +
|
| 26 | +``--check`` renders without writing and exits non-zero if any committed figure |
| 27 | +differs — suitable for CI. The example schemas are dropped afterwards unless |
| 28 | +``--keep-schemas`` is given. |
| 29 | +
|
| 30 | +This reproduces the committed figures' nodes, tiers, edges, tooltips, clusters |
| 31 | +and labels exactly, with the caveats below. |
| 32 | +
|
| 33 | +Reproducibility caveats |
| 34 | +----------------------- |
| 35 | +- **Needs an empty database.** The schema names are unprefixed (``reference``, |
| 36 | + ``lab``, ``session``, ``imaging``) because ``dj.Diagram`` takes each cluster |
| 37 | + label from the Python module name and these must agree. On a server that |
| 38 | + already holds a schema by one of those names, the diagram silently picks up the |
| 39 | + foreign tables instead. Point ``DJ_HOST``/``DJ_PORT`` at a throwaway instance. |
| 40 | +- **Padding entities depend on pydot.** Tooltip padding is emitted as `` `` |
| 41 | + by the pydot that produced the committed figures and as literal spaces by |
| 42 | + 4.0.1, which shows up as a whole-file diff with no visual change. Compare |
| 43 | + rendered content, not bytes, when the pydot version moves. Nothing pins pydot. |
| 44 | +- **One collapsed edge is traversal-order dependent.** A collapsed edge inherits |
| 45 | + the attributes of whichever foreign key in its bundle is visited first |
| 46 | + (``diagram.py``, ``_collapse_graph``: ``if not new_graph.has_edge(...)``), with |
| 47 | + no aggregation over the bundle. Where a bundle mixes a primary and a secondary |
| 48 | + foreign key — ``lab -> session`` here, which bundles ``Subject -> Session`` |
| 49 | + (primary) and ``User -> Session`` (secondary) — the edge renders solid or |
| 50 | + dashed depending on order alone. The committed figure has it solid; this script |
| 51 | + produces dashed. Both are outputs of the same renderer. |
| 52 | +
|
| 53 | +A non-empty diff after a DataJoint upgrade is the signal to review the notation |
| 54 | +and the surrounding prose together — see issue #246. |
| 55 | +""" |
| 56 | + |
| 57 | +import argparse |
| 58 | +import os |
| 59 | +import sys |
| 60 | +import tempfile |
| 61 | +from pathlib import Path |
| 62 | + |
| 63 | +import datajoint as dj |
| 64 | + |
| 65 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 66 | + |
| 67 | +from pipeline_example import imaging, lab, reference, session # noqa: E402 |
| 68 | + |
| 69 | +IMAGES = Path(__file__).resolve().parent.parent / "src" / "images" |
| 70 | + |
| 71 | +MODULES = (reference, lab, session, imaging) |
| 72 | + |
| 73 | +# dj.Diagram labels each node by resolving its table against this context. Passing |
| 74 | +# the classes under their bare names keeps node labels unqualified ("Session", not |
| 75 | +# "session.Session") while the cluster labels still come from the module names. |
| 76 | +CONTEXT = { |
| 77 | + name: obj |
| 78 | + for module in MODULES |
| 79 | + for name, obj in vars(module).items() |
| 80 | + if isinstance(obj, type) and issubclass(obj, dj.Table) |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +def diagram(schema) -> dj.Diagram: |
| 85 | + return dj.Diagram(schema, context=CONTEXT) |
| 86 | + |
| 87 | + |
| 88 | +def whole_pipeline() -> dj.Diagram: |
| 89 | + """The four modules unioned into one diagram.""" |
| 90 | + result = diagram(reference.schema) |
| 91 | + for module in MODULES[1:]: |
| 92 | + result += diagram(module.schema) |
| 93 | + return result |
| 94 | + |
| 95 | + |
| 96 | +FIGURES = { |
| 97 | + # Whole pipeline, table level: every module expanded. |
| 98 | + "pipeline-modules.svg": whole_pipeline, |
| 99 | + # Same pipeline, module level: one node per schema. |
| 100 | + "pipeline-modules-collapsed.svg": lambda: whole_pipeline().collapse(), |
| 101 | + # The imaging module on its own. |
| 102 | + "imaging-schema.svg": lambda: diagram(imaging.schema), |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +def main() -> int: |
| 107 | + parser = argparse.ArgumentParser( |
| 108 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "--check", |
| 112 | + action="store_true", |
| 113 | + help="report figures that differ from the committed SVGs without writing them; " |
| 114 | + "exits 1 if any differ", |
| 115 | + ) |
| 116 | + parser.add_argument( |
| 117 | + "--keep-schemas", |
| 118 | + action="store_true", |
| 119 | + help="leave the example schemas in the database (default: drop them)", |
| 120 | + ) |
| 121 | + args = parser.parse_args() |
| 122 | + |
| 123 | + # Left-to-right layout, matching scripts/execute-notebooks.sh. |
| 124 | + with tempfile.TemporaryDirectory() as tmp: |
| 125 | + with dj.config.override(display__diagram_direction="LR"): |
| 126 | + rendered = {} |
| 127 | + for name, build in FIGURES.items(): |
| 128 | + staged = Path(tmp) / name |
| 129 | + build().save(str(staged)) |
| 130 | + rendered[name] = staged.read_text() |
| 131 | + |
| 132 | + if not args.keep_schemas: |
| 133 | + for module in reversed(MODULES): |
| 134 | + module.schema.drop(prompt=False) |
| 135 | + |
| 136 | + differs = [] |
| 137 | + for name, svg in rendered.items(): |
| 138 | + target = IMAGES / name |
| 139 | + old = target.read_text() if target.exists() else None |
| 140 | + if old == svg: |
| 141 | + print(f" unchanged {name}") |
| 142 | + elif args.check: |
| 143 | + differs.append(name) |
| 144 | + print(f" DIFFERS {name}") |
| 145 | + else: |
| 146 | + differs.append(name) |
| 147 | + target.write_text(svg) |
| 148 | + print(f" written {name}") |
| 149 | + |
| 150 | + if args.check and differs: |
| 151 | + print( |
| 152 | + f"\n{len(differs)} figure(s) differ from the committed SVGs. Re-run " |
| 153 | + "without --check to update them, then review the notation and the " |
| 154 | + "prose in src/explanation/ together (see #246). If only tooltip " |
| 155 | + "padding moved, check the pydot version first — see the module " |
| 156 | + "docstring.", |
| 157 | + file=sys.stderr, |
| 158 | + ) |
| 159 | + return 1 |
| 160 | + return 0 |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + os.environ.setdefault("DJ_USE_TLS", "false") |
| 165 | + raise SystemExit(main()) |
0 commit comments