Scheduling Captures#
cgr.scheduling, the scheduling half of
capturegraph-recipes, decides when and where the next
capture is worth taking, in three stages: forecast the candidate sessions
and the conditions each would be captured under, select the candidates
farthest from what the dataset already holds, and reserve one per
participant. Coverage is an importance sampling problem: instead of "every five
minutes", a participant is interrupted only for a candidate that fills a real
gap.
The three stages#
Each stage is one of the two kinds of recipe a server
interceptor calls: a pure_function,
memoized by the content of its inputs, or an
ImpureState, a
keyed book whose every method call is a locked load-modify-commit.
| Stage | Entry points | What it does | Recipe |
|---|---|---|---|
| Forecast | cgr.scheduling.forecast |
Lay out candidate sessions: time slots with the sun's position (altitude and azimuth) or the weather each would see, or a grid of locations over an area. | pure |
| Select | cgr.scheduling.select_sessions, with a cgr.scheduling.distance metric and a cgr.scheduling.energy function (how distance becomes coverage; Gaussian by default) |
Void & Cluster sampling: repeatedly fill the largest gap — each pick is the candidate least covered by the sessions already captured and by the picks so far, and a crowded early pick can be released and re-placed — under a distance you compose. | pure |
| Reserve | cgr.scheduling.ReservationBook for times; cgr.scheduling.CoverageBook, its spatial sibling, leases places instead |
Hand each participant a distinct candidate — distinct while candidates last; past that, ReservationBook hands the earliest to everyone left over and CoverageBook hands out None — keep it stable while they poll, and release it once it lapses, or once the caller reports a capture that covers it (captured= / fulfilled_by=). |
impure state |
Forecasting and selection depend only on their inputs, so an interceptor wraps
them in a cg.pure_function and a whole crowd's polls share one computation.
Two conditions make that true: the arguments must be content-hashable values
(a cg.Map of sessions, a cg.Location), and time must enter quantized — a
function keyed on a raw datetime.now() misses on every request. The temporal
case ships done:
cgr.scheduling.notification_candidates(sessions, location, cgr.scheduling.hourly_bucket(now))
is that pure function, keyed on the captured sessions, the location, and the
hour — so a crowd's polls within one hour share one selection.
Reservation is honestly stateful, so the books are cg.ImpureState classes:
open(key) names one book, every method call is a locked read-modify-write of
it, and two server workers never hand out the same slot. Both kinds of recipe
live in a scratch, the on-disk cache and state store, which the server and
every script on the machine share.
The interceptor asks nothing more of either — it is a plain function the server
calls per request, and it calls the recipes like any other function.
interceptor(profile, target) ──▶ forecast ──▶ select ──▶ reserve ──▶ the app's next reminder
pure pure impure
Scheduling code runs on the server, inside a target's interceptor; the same functions run unchanged in a notebook to simulate a campaign. From Zero to a Scheduled Target builds both a temporal and a spatial interceptor step by step.
Quick start#
A complete temporal schedule, from candidates to two participants holding
different times. Candidate sessions are rows built by attribute broadcast:
assigning an array to potential.date creates one row per element, and each
row is a plain mapping read by key.
from datetime import UTC, datetime, timedelta
import capturegraph as cg
import capturegraph_recipes as cgr
location = cg.Location(latitude=42.44, longitude=-76.5)
now = datetime.now(UTC)
# 1. Forecast: every hour of the coming day, with the sun's position
# (altitude and azimuth) it would see.
potential = cg.Array()
potential.date = cgr.scheduling.forecast.times(
resolution=timedelta(hours=1),
span=timedelta(hours=24),
start=now,
)
potential.solar_angle = cgr.scheduling.forecast.solar_position(location, potential.date)
# The sessions already captured, in the same shape. On a server these come from
# `target.sessions`, keyed by capture time; here two are made up.
previous = cg.Array()
previous.date = cg.Array([now - timedelta(hours=20), now - timedelta(hours=3)])
previous.solar_angle = cgr.scheduling.forecast.solar_position(location, previous.date)
# 2. Select: the ten candidates whose solar position the dataset lacks most.
selected = cgr.scheduling.select_sessions(
potential,
previous,
cgr.scheduling.distance.combine(solar_angle=cgr.scheduling.distance.solar(sigma_deg=2.0)),
selections=10,
)
times = [session["date"] for session in selected]
# 3. Reserve: one distinct, self-expiring time per participant.
book = cgr.scheduling.ReservationBook.open("my-target")
alice = book.reserve("alice", times, now=now)
bob = book.reserve("bob", times, now=now)
assert alice != bob
forecast.times lays out
the slots, and
forecast.solar_position
is vectorized, so one call over potential.date fills every row. It returns a
SolarPosition
(altitude and azimuth);
distance.solar measures
the angular separation between two of them, in units of sigma_deg.
distance.combine
takes one keyword per session attribute and joins the terms as a Euclidean
norm: solar_angle= reads the solar_angle attribute or key of both sides, so
a cg.Struct row works as well as a mapping, and a term absent on either side
drops out. Distance Functions lists every metric — solar,
location, weather, time of day, and the scalar and angle primitives.
select_sessions
hands back the chosen candidates in potential order, not ranked —
selected[0] is the earliest chosen slot, not the best one — and at most as
many as there are comparable candidates. Its energy_fn, by default
energy.gaussian(sigma=1),
turns each distance into coverage: a candidate near a previous session or an
earlier pick has high energy, and every pick is the lowest-energy candidate
left.
ReservationBook
persists in the scratch, and open(key) names one independent book within it —
any string, one per crowd that must not collide, typically the target's name.
A hold is released once its time is more than grace (30 minutes by default)
in the past; pass captured=, the target's latest capture, and every hold at
or before captured + grace is released too. Alice and Bob hold different
times because ten candidates outnumber two participants; once every candidate
is held, reserve returns the earliest without recording a hold.
The spatial mirror#
A survey covers an area instead of a day, and the same three stages run over
cg.Location rows: a grid over the survey polygon is the candidate pool, the
captured locations are what is already covered, and
CoverageBook
leases each participant a place (30 minutes by default) and retires it once a
capture in fulfilled_by= lands within threshold of it.
bounds = cg.Array[cg.Location](
[
cg.Location(latitude=42.4440, longitude=-76.5020),
cg.Location(latitude=42.4440, longitude=-76.5000),
cg.Location(latitude=42.4425, longitude=-76.5000),
cg.Location(latitude=42.4425, longitude=-76.5020),
]
)
distance = cgr.scheduling.distance.location(sigma_m=10.0)
batch = cgr.scheduling.select_sessions(
cgr.scheduling.forecast.locations_area(bounds, resolution_meters=10.0),
cg.Array[cg.Location](), # nothing captured yet
distance,
selections=8,
)
survey = cgr.scheduling.CoverageBook.open("my-survey")
spot = survey.reserve("alice", distance=distance, refill=list(batch), now=now)
forecast.locations_area
grids the polygon at resolution_meters;
distance.location
is geographic separation in units of sigma_m, so the default threshold=1.0
retires a place once a capture lands within ten metres of it. refill=
installs the batch once the book is empty.
Reservations walks the lease states,
and the walkthrough
puts the batch behind a pure_function keyed on the covered locations.
Section contents#
Compose a metric over solar angle, location, weather, and time of day
The selection algorithm, the forecast utilities that build its candidates, and the energy functions
Hand each participant a distinct time, or a distinct place, that expires on its own
Draw what the dataset still needs as an image the app shows a participant
Put all three stages inside a server interceptor, with a spatial sibling