Skip to content

Extending CaptureGraph#

Three of CaptureGraph's Python packages carry the extension points: the core package defines them, and the other two use them exactly as a package of your own would. Everything a study adds — a type, a viewer, a log sink, a recipe — plugs into one of those points.

Package Import Holds
capturegraph-lib import capturegraph as cg The standard: the type system and the node IR (capturegraph.types, capturegraph.procedures), with the recording surface (@cg.procedure, cg.do) aggregated at the cg root; the server wire shapes (capturegraph.api_models); cg.logging; and cg.scratch, the content-addressed cache every recipe runs against.
capturegraph-ui import capturegraph_ui as cgui Page and PageBuilder, the components, viewers and charts, and Notebook, the HTML log sink.
capturegraph-recipes import capturegraph_recipes as cgr sfm, depth, splats and scheduling, the analysis-only types they produce (cgr.Splat, cgr.Reconstruction, …), and SplatViewer.

The layers#

A package imports the packages beneath it and never one above. Each arrow below is a dependencies entry in the importing package's pyproject.toml; the arrow points at the package that does the importing:

              capturegraph-lib (cg)
                 │             │
                 ▼             ▼
    capturegraph-ui (cgui)   capturegraph-sync
          │          │
          ▼          ▼
capturegraph-recipes (cgr)   capturegraph-server

So capturegraph-ui imports the core; capturegraph-recipes and capturegraph-server import the core and the ui package; capturegraph-sync imports the core alone. Nothing in the core names capturegraph_ui or capturegraph_recipes, nothing in the ui package names the recipes, and the server never imports the recipes — a target module the server loads may (the shipped examples do), but that is study code, not the server. Inside the core the same rule holds: cg.scratch is built on capturegraph.types and cg.logging, and neither of those imports the scratch.

The server and the sync tool define no extension point of their own and a package of yours never depends on them; they are documented as programs under Running a Server and Syncing Data. Across platforms the order is lib → iOS → server → sync: the iOS app is the other consumer of the standard, executing the procedure JSON the core serializes and writing the captured-data layout the core reads back. The sixth workspace member, capturegraph-icon, renders the app icon from the app's own color tables; it imports no CaptureGraph package and sits outside the layering.

The extension points#

What the core needs from above, it asks for through a hook it defines, and each hook has a worked example in the package that uses it. The one hook that is not the core's is the ui package's Render, since a component is a ui concept the core never sees.

To add Implement The hook Worked example
a type a subclass of cg.FileScalar, cg.DirScalar or cg.JSONScalar; of the file bases cg.NumpyScalar, cg.EXRScalar, cg.PLYScalar or cg.StillImageFile; or of cg.CGType itself the wire-name registry, which declaring name= adds a row to, and the workspace, which answers T.new() cgr.Splat — Adding a type
a component a subclass of cgui.Component, and a cgui.Library for vendored JavaScript cgui.Render, the context html writes files and links through — the ui package's hook, not the core's cgr.SplatViewer — Adding a component
a log sink a subclass of cg.logging.Sink cg.logging.to(sink), the block that routes every entry made inside it to the sink cgui.Notebook — Adding a log sink
a recipe a @cg.pure_function, or a cg.ImpureState for state that persists and changes between calls the scratch: a call's result memoized by the content hash of its inputs, a state committed under its key cgr.sfm (pure) and cgr.scheduling (state) — Recipes
a package a uv workspace member none in any package; the workspace root lists it capturegraph-recipes — Packaging an extension

The nouns in that table, each defined once on the page that owns it:

  • A CGType is the root of the type system: the class is the type, its instances are values, and it owns a wire form (schema) and an on-disk form (load and store under a base path). Hashing, caching and staging all work on the directory tree a value stores as. The scalar bases fix the on-disk form for you — a FileScalar value is one file, a DirScalar a directory, a JSONScalar a frozen dataclass in one JSON file; among the file bases, a NumpyScalar is named arrays in one npz, an EXRScalar named float planes in one OpenEXR, a PLYScalar named vertex columns in one PLY, and a StillImageFile a picture with the decode helpers Image, Panorama and Thumbnail share — so subclass one of them whenever it fits, and subclass CGType directly, implementing load and store yourself, only for a shape none of them cover (Adding a type).
  • The wire-name registry is a table from wire name to class. Declaring a scalar subclass with name="edu.example.splat" adds its row, and cg.from_schema uses the table to rebuild a schema document into your class — once your package has been imported, which is the only registration there is (Registration is import).
  • The workspace answers T.new() — a value of T at a path that does not exist yet — and DirScalar.exec, which runs a tool with its output logged beside the owner's files. The type system installs no workspace; importing capturegraph.scratch provides one, which is why new works for a type defined in any package (Where new() gets its path). The two other verbs the type pages mention are DirScalar's: stage copies a source file or tree into the directory as its own copy, and put(relative, data) writes bytes or text at a relative path, leaving a file that already holds those bytes untouched.
  • cg.loaded_from and cg.held are what a value says about its place on disk: cg.loaded_from(value) is the tree it was read from — which makes it read-only, .clone() being the copy that can change — None for a value built in memory, and cg.held(value) is true when its files exist only while the value does — a recipe's result, pinned in the scratch. A page links a value only when it has a path and no hold (Links); hashing never looks at either (Loaded values are read-only).
  • The scratch is a garbage-collected, content-addressed directory the core manages: every value tree it holds lives under objects/<hash>/, named by its own merkle hash, and a name resolves to one such object (The scratch). A @cg.pure_function memoizes a function over CGType values: its arguments hash by content, its result is stored under the schema the return annotation declares, and a second call with equal inputs is served from the scratch without running (pure_function). A cg.ImpureState is for state that persists and changes between calls — a reservation ledger a fleet of server workers bumps: subclass it like a cg.Struct, whose annotated fields are the persistent state, open it by key, and every method call is one locked read-modify-write committed as a new sealed version (ImpureState).
  • Render is the context a component's html receives: stage(file) copies a file-valued prop under the page's files/, lib(library, file) copies a Library under lib/, and href resolves a link to another value's page, each returning the relative href the component writes (Writing HTML).
  • A sink is where a cg.logging tree shows. cg.logging.to(sink) opens a block: the sink's start runs as it opens, emit once per entry and once per change to one, and finish as it ends (Logging and Progress).

Why the recipes are a separate package#

It keeps the core light. The reconstruction and scheduling code pulls in SciPy, pandas, shapely, Hugging Face Hub, meteostat and the weather clients and, through a subprocess, torch and nerfstudio. A study that only defines procedures installs none of it; the core carries only what its own types and wire shapes need: image, video and RAW codecs, a hash, and pydantic.

The two change at different rates. The core is the standard — the JSON form the iOS app executes and the wire shapes every platform agrees on — and a change to it touches Swift as well as Python. A recipe changes whenever a better matcher or model appears, and touches nothing else.

It proves the extension points work. capturegraph-recipes adds the types in cgr, a viewer with its own vendored JavaScript, and the whole scheduling library without editing a line of the core; every hook it uses is public, so a third party's tools integrate exactly as deeply as ours.

That matters for the research. Distribution-aware capture is the thesis: naive collection — "every five minutes" — oversamples convenient conditions, burns out participants, and hides dataset bias until the study ends, so cgr.scheduling computes the marginal utility of a potential capture and interrupts a participant only when the data is valuable (Scheduling Captures). A server interceptor calls it like any other recipe, so retargeting a crowd's behavior is a server-side edit, never an app update. A study with a different notion of coverage replaces the scheduler in a package of its own, and the server and the app are unchanged.

Section contents#

Declare an on-disk form, get hashing, caching, new and stage for free

Props, html, the Render context, and a browser library of your own

start, emit, finish, and redrawing a whole tree

A workspace member that ships types, recipes, viewers and package data