This repository contains code for running various connectome-related services related currently deployed
on flyem.mrc-lmb.cam.ac.uk (internally flyem1):
- a supervoxel look-up service for the Zheng et al. CA3 volume
- a supervoxel look-up service for the Aedes aegypti EM volume
- a service for dynamically generating neuroglancer segmentation properties from tables on flytable
- a sparse voxel service returning a segment's voxels as run-length encoded runs, instead of shipping raw chunks to the client
- a verdict log collecting Coda readers' verdicts on SEAM's merge suggestions, for SEAM's calibration
The project uses uv to manage dependencies and the virtual environment.
# Run to setup dependencies
uv syncIf you are running this server in production, you probably want a virtual environment:
# Create a virtual environment
uv envIn order to use the segment property service, you will also need to setup environment variables:
SEATABLE_SERVER: URL for FlyTableSEATABLE_TOKEN: API token for FlyTable
Note to self: on flyem1 I added these to the gunicorn_start script.
Returns the voxels of a segment as run-length encoded runs, the way DVID's
sparsevol does. The point is to stop clients downloading raw segmentation:
to get a neuron's voxels today you have to pull every chunk it touches and mask
them yourself, which is gigabytes over the wire to keep a few megabytes of
neuron. The service does that masking next to the data and sends you the
answer instead.
# Every voxel of a neuron, as runs
curl .../sparsevol/dataset/wclee_aedes_brain/s/0/root/720575940626838909 -o neuron.bin
# Or from supervoxel IDs you already hold
curl -X POST .../sparsevol/dataset/wclee_aedes_brain/s/0/supervoxels \
-H 'Content-type: application/json' \
-d '{"supervoxels": ["81624458144052736", "81624458144052737"]}'
# What is available, and how each dataset is served
curl .../sparsevol/datasetsThe default response is binary: an (M, 4) little-endian int32 array of runs,
each x, y, z, length, meaning length voxels from (x, y, z) along +X.
Coordinates are voxels at the requested scale. Add ?fmt= for npy, json,
or coords (an (N, 3) array with every voxel spelled out — far larger, so
prefer the default). Responses are gzipped when the client accepts it.
X-Sparsevol-Columns gives the row width of whatever came back, so a client
reshapes by that rather than by which endpoint and format it asked for.
fmt=json is size-limited (SparseVolMaxJsonRuns). It is the one format that
cannot be serialized concurrently — the JSON encoder holds Python's global
lock for its whole run, so a large JSON response stalls every other client the
worker is serving. Past the limit the request is refused with a 400 rather
than served at everyone else's expense; rle and npy carry the same numbers
in a fraction of the bytes and have no such limit.
Every sparsevol answer is served with a long Cache-Control: immutable. Root
and supervoxel IDs are immutable — proofreading mints new ones rather than
changing the voxels under an existing ID — so an answer is valid forever
against the request that produced it. Only /root is a GET, where a shared
cache will act on it; on the POST endpoints the header is advisory, but the
claim holds and is worth keying your own cache on.
import numpy as np, requests
r = requests.get(".../sparsevol/dataset/wclee_aedes_brain/s/0/root/720575940626838909")
runs = np.frombuffer(r.content, dtype="<i4").reshape(-1, 4)
print(r.headers["X-Sparsevol-Voxels"], "voxels in", len(runs), "runs")
print("read", r.headers["X-Sparsevol-Voxels-Read"], "voxels to get there")Every response carries X-Sparsevol-* headers accounting for what the request
actually did — voxels returned, runs, chunks read, and how many dense voxels
were read and discarded to produce them.
The endpoints above ask where a segment is, and the answer is bounded by what
that segment occupies. /box asks the opposite question — what is inside this
box — for up to a few hundred objects at once, which is what you want when
scoring every candidate around a point rather than fetching one neuron.
Reading the box once costs the same whether you ask about one object or a
hundred, so the per-object cost collapses.
curl -X POST .../sparsevol/dataset/wclee_aedes_brain/s/1/box \
-H 'Content-type: application/json' \
-d '{"bbox": [[12000, 8000, 3000], [12512, 8512, 3256]],
"roots": ["720575940626838909", "720575940637352552"],
"down": [2, 2, 1]}'Runs come back as x, y, z, length, label, where label is the 1-based index
into the objects as you gave them. Pass objects instead of roots — a list
of {"supervoxels": [...]} — if you already hold the leaves.
down is an optional per-axis OR-downsampling factor: a coarse voxel
belongs to every object with at least one fine voxel in it, so runs of
different labels may overlap. That is deliberate. Taking a majority instead
would erase a thin neurite running beside a thick one, which is usually the
thing you are looking for.
Coordinates are absolute — floor(voxel / down) of the real voxel, not an
offset into the box — so the same object described from two different boxes
comes back with the same coordinates, and an answer is valid forever against
(objects, scale, box, down).
Asking about many objects at once is close to free: the box is read and labelled in one pass however many you name, so 250 objects cost about 7% more than one.
A label is a position in the request, never a real ID, and that will not change. The service needs no credentials of its own but holds credentials for the chunkedgraph, and some datasets behind it are not open to everyone. Today you have to know an ID before you can ask about it. If responses carried real root or supervoxel IDs, anyone who could reach the service could sweep boxes and read back the grouping instead — so returning them would mean putting the whole service behind a login. An object with no voxels in the box simply contributes no runs; that is an answer, not an error. Boxes are bounded by
SparseVolBoxMaxVoxels(how much gets read) andSparseVolBoxMaxCoarseVoxels(how much comes back) — a sparse and a dense box of the same size cost the same to read and differ by orders of magnitude in what they return, so the two limits are separate.SparseVolBoxMaxSupervoxelsbounds a third thing again: how large a request may be, since anobjectsrequest spells out every supervoxel.
Deployment note. An
objectsrequest at that ceiling is ~10 MB of JSON, well over nginx's 1 MB default body size. The reverse proxy needsclient_max_body_sizeraised for this path — see Run in production — otherwise an oversized request is rejected by the proxy with an opaque 413 instead of reaching the service, which would answer with a 400 naming the limit.
These segmentations are flat: the stored labels are watershed supervoxels, and nothing about a raw label says where it lives. What makes the lookup tractable is the chunkedgraph sitting on top of them. Its labels are graphene labels with the chunk position packed into the high bits, so:
get_leaves(root, stop_layer=2)gives the chunks a neuron occupies without reading a single voxel, and- a supervoxel query needs no graph call at all — the chunk falls straight out of arithmetic on the ID.
So the read is confined to the chunks the segment actually occupies. The graph is consulted only for the manifest and the geometry; image data is always read from the local copy of the segmentation, never through graphene.
Each dataset therefore needs two things in DATASOURCES: sparsevol in its
services, and a graphene source pointing at its chunkedgraph.
The dense read happens per request the first time anyone asks — there is no precomputed index. That is affordable only because the segmentation is on the same machine: it trades the client's bandwidth for the server's local I/O. Repeat requests are served from the layer-2 cache below.
It is not free, though. Graph chunks are large — aedes is 512×512×128 at mip 0,
so one chunk is 33.5M voxels and ~268 MB once decompressed to uint64, and
CA3 at mip 1 is about half that. Two ceilings in app/config.py
keep a single request from running away, both overridable per dataset:
| setting | default | meaning |
|---|---|---|
SparseVolMaxChunks |
256 | chunks one request may read |
SparseVolMaxVoxels |
4e9 | voxels one request may read (~32 GB, ~120 aedes chunks) |
SparseVolMaxWorkers |
4 | concurrent chunk reads within a request; each holds a whole chunk, so ~1 GB peak |
SparseVolMaxConcurrent |
2 | reads in flight at once, process-wide (~1.1 GB each) |
SparseVolQueueSeconds |
20 | how long a request waits for a slot before being shed |
Going over the size limits returns a 400 naming the limit rather than occupying a worker until something times out. If real neurons turn out to span more than this, raise the limits per dataset — but raise them knowing what they cost.
SparseVolMaxConcurrent is the one that protects the machine rather than the
request. Size limits bound a single read; without a concurrency cap nothing
stops several large reads arriving together, each claiming its own chunks,
which is an out-of-memory kill rather than an error. Requests queue for a slot
and are shed with a 503 and Retry-After if none frees up in time. The wait is
reported in X-Sparsevol-Queued-Seconds, which is what to watch when tuning.
The cap is per worker process. Under gunicorn the real ceiling is
(gunicorn workers) × SparseVolMaxConcurrent × ~1.5 GB, so size it against the
machine's memory and how many workers you run, not against this number alone.
Two deployment settings matter alongside these: a read near the size limit can
take a while, so gunicorn's worker timeout and nginx's proxy_read_timeout
both need to exceed it, or a slow success arrives as a 502 with a killed worker.
Requesting a coarser scale is the cheap way to reduce work, where a dataset has one downloaded locally.
Most of that dense read is repeated work: ask about the same neuron twice, or
about two neurons sharing a branch, and the same chunks get read and masked
again. So the runs are cached per layer-2 node — see
app/l2cache.py. A fully cached neuron reads no image data
and makes exactly one chunkedgraph call.
Layer 2 is the useful key because proofreading mostly leaves it alone. A merge spanning two chunks adds an edge above layer 2 and mints no new L2 node at all; only within-chunk merges and splits recompute one, and only for the chunks the cut touches. So an edited neuron pays for the part that changed and reuses the rest, and entries that do go stale are simply never asked for again.
X-Sparsevol-L2-Cached and -L2-Computed report the split per request, and
GET /sparsevol/cache reports how full the store is.
curl .../sparsevol/cache| setting | default | meaning |
|---|---|---|
L2CacheEnabled |
True |
off falls back to computing every request live |
L2_CACHE_PATH |
./l2_cache.sqlite |
env-overridable; put it on a roomy disk |
L2CacheMaxBytes |
50 GB | ~25,000 aedes neurons at mip 1 |
L2CacheMaxKeys |
20M | the other ceiling |
L2CacheManifestWorkers |
8 | concurrent chunkedgraph calls on a cold read |
It fails loudly when full. On reaching either ceiling the cache stops
accepting writes and every sparse volume request starts returning 503 —
including ones it could have served, and the supervoxel endpoint that never
touches it. This is deliberate. An LRU would quietly thrash at the ceiling and
look, from outside, exactly like a cache that is working; the thing worth
noticing is the ceiling being reached at all, and that is a capacity decision
for a person. The 503 names the three ways out: raise the limits, clear the
cache, or set L2CacheEnabled = False.
Sizing, measured on aedes at mip 1: ~1,650 runs per L2 node, ~3.8 kB packed, ~500 L2 nodes for a large neuron — so roughly 2 MB per neuron.
One cost worth knowing about: attributing a voxel to an L2 node needs that node's supervoxel manifest, and there is no batch endpoint, so a cold neuron costs one chunkedgraph GET per L2 node. It is paid once per node ever. With caching disabled the service uses the old path instead — a single manifest call for the whole root — which is why both paths still exist.
Populating happens on demand, so this is optional; it only moves the cost of the first request to a time of your choosing.
uv run python -m app.l2cache_warm --stats
uv run python -m app.l2cache_warm wclee_aedes_brain --scale 1 --roots roots.txt
uv run python -m app.l2cache_warm --clear --dataset wclee_aedes_brainIt is not a loop over the endpoint. Neurons share chunks, so it gathers the L2
nodes of every root first, groups them by chunk — which needs no extra graph
traffic, since a node's chunk falls out of arithmetic on its ID — and reads
each chunk once for all the nodes in it. On aedes that is the difference
between ~120 hours and ~18 for ten thousand neurons. --chunks bounds how much
is held per pass; --dry-run reports the work without reading a voxel.
Run it against a quiet server: it holds the same chunks in memory as a request does, and its concurrency limit is its own — the service's cap lives in the service's process and knows nothing about this one.
The same endpoints can also be served from a precomputed supervoxel → RLE
index instead of reading chunks live, configured via SPARSEVOL_DATASOURCES.
Mode is a per-dataset deployment detail: the request and the response are
identical either way, and /sparsevol/datasets reports which is in use.
That path exists for a dataset too large to sparsify on demand — FlyWire, say,
where a mip-0 neuron is ~20 billion voxels and the volume is not local. It
moves the dense read to a one-time offline build keyed on supervoxel IDs, which
are immutable, so the index never needs invalidating. No such index is built
today, so a root query against flywire_public will return empty until one
exists. app/sparsevol_build.py builds one for the
chunks a single neuron occupies:
uv run python -m app.sparsevol_build flywire_public 720575940626838909 --mip 2That builder is a bootstrap, not a production job: it reads serially, one box
per graph chunk, with no ceiling — see the notes at the top of the module for
what a real in-region batch build should do differently. Background and
measurements are in pcg_sparse,
whose RLE codec and chunk conventions this shares.
The
grapheneURL on theflywire_publicentry has not been checked against a live datastack — confirm it withCAVEclient("flywire_fafb_public").info.segmentation_source()before relying on it. The aedes and CA3 sources have been verified to open and authenticate.
Readers of Coda's Review Suggestions card accept, reject or mark unsure SEAM's
merge suggestions. Those who opt in have each verdict posted here, and SEAM
reads them back to refit how often a suggestion at a given score is right. The
plan and the decisions behind it are in Coda's docs/seam-verdict-log-plan.md.
Coda never sends a reader's CAVE token. To post, a reader first answers a challenge: the service names a supervoxel and a moment, and the reader answers with that supervoxel's root at that moment, looked up through their own CAVE access. The service checks the answer with its own credentials. A right answer buys a session for that datastack (12 hours).
# Which datastacks are collected
curl .../verdicts/datastacks
# 1. A challenge: {"challenge", "supervoxel", "at", "datastack", "expires"}
curl ".../verdicts/challenge?datastack=wclee_aedes_brain"
# 2. Answer it with the supervoxel's root at "at": {"session", "expires", ...}
curl -X POST .../verdicts/session -H 'Content-type: application/json' \
-d '{"challenge": "...", "root": "720575940626838909"}'
# 3. Post records under the session (up to 500 a call)
curl -X POST .../verdicts -H 'Authorization: Bearer <session>' \
-H 'Content-type: application/json' -d '{"records": [...]}'
# Read everything back, as JSON lines (admin key)
curl ".../verdicts/export?datastack=wclee_aedes_brain&since=2026-11-18T00:00:00Z" \
-H 'Authorization: Bearer <VERDICT_ADMIN_KEY>'What the check proves, and what it does not: that whoever posted can look up
that dataset's roots, or holds a recent copy of its root table (which answers
for every supervoxel not edited since). Not who they are. Anyone with access to the
dataset can post, and a record's name and email are what the reader typed.
session keeps one reader's records together either way, and a record's
datastack is its session's, never the client's to say.
A dataset collects verdicts when "verdicts" is among its services in
DATASOURCES; it then names its CAVE datastack and the verdict_scale its
challenge supervoxels are drawn from (one random chunk of the local copy at a
time), and needs a graphene source. Offer it only where the chunkedgraph
refuses an anonymous root lookup: where anyone can read it, the challenge
proves nothing. Nothing checks that for you.
Checking an answer is a call to the chunkedgraph under the service's own
credentials, so each worker checks at most VerdictChecksPerMinute (30); past
that, an answer gets a 429 and its challenge is not spent.
A verdict is accept, reject (a wrong continuation), ends (rejected because
the neuron ends at this tip), unsure, or ignore (the right continuation, left
unmerged because the neuron holds a false merge it would extend). Records are append-only: a changed mind
is a newer record (cleared when a reader takes a verdict back). A verdict is
about a piece for a neuron, not about one tip: the card shows it on every tip
the piece is listed at, so the latest record for a neuron and candidate
stands, whichever end_uid it was given at. Each carries an id minted by Coda, so a retried
post is not counted twice. A batch with one bad record is refused whole, and
the error names the record's index and field. The fields are listed on
Record in app/verdicts.py.
Three environment variables, set in gunicorn_start:
VERDICT_SECRETsigns challenges and sessions. Unset, nothing is collected and every endpoint but/verdicts/datastackssays so. Changing it only ends current sessions.VERDICT_ADMIN_KEYguards the export. Unset, there is no export.VERDICTS_DB_PATHis where the SQLite file goes (default:verdicts.sqlitebeside the checkout).
A record holds an email when the reader gave one, which is personal data. Remove
a reader's records on request with DELETE FROM verdicts WHERE email = ? (or
session).
uv run uvicorn --reload app.main:app
uv run pytest
We deployed this service on flyem1 using:
gunicornas the webserversupervisoras the process control system for the gunicorn server- a reverse proxy set up in
nginxforwarding requests to the gunicorn webserver
Please see this tutorial for general step-by-step instructions.
This Slack message contains details on how
the service is currently deployed on flyem1.
The sparse voxel service's /box endpoint accepts requests that list every
supervoxel of every object, which can run to ~10 MB at
SparseVolBoxMaxSupervoxels. nginx defaults to a 1 MB body, so the service's
location block needs:
location /transform-service/ {
client_max_body_size 16m; # /box requests list supervoxels explicitly
client_body_buffer_size 1m; # optional: keeps typical bodies out of /tmp
...
}Keep this above whatever SparseVolBoxMaxSupervoxels implies, so an oversized
request is refused by the service with a message naming the limit rather than
by the proxy with a bare 413. Apply with:
sudo nginx -t && sudo systemctl reload nginxThe segmentations the service reads live on /fafbz and are linked into
segmentation/. They are kept as gzip-compressed shards, not as the
one-file-per-chunk layout the buckets use. Downloading with gs cp unzips
every chunk on the way down (the bucket stores them gzipped), which roughly
triples the size on disk — and tensorstore cannot read gzipped chunk files in
that layout. Shards are the one place the format allows gzip.
Two tools in tools/ produce them. Neither unpacks any voxels, and
the chunk size stays the same, so nothing in the service needs to change.
# Repack a scale that is already on disk unzipped (the source is only read)
uv run python tools/to_sharded.py SRC_DIR DST_DIR 32_32_45 --workers 8
# Or fill a scale straight from the bucket, keeping its gzip bytes as they are.
# DST_DIR/info must already list the scale -- copy the bucket's info first.
uv run python tools/fetch_sharded.py \
https://storage.googleapis.com/fanc-fly_segmentation/wclee_aedes_brain \
/fafbz/aedes/wclee_aedes_brain_sharded 16_16_45 --processes 8 --threads 32Prefer fetch_sharded for anything not yet downloaded: it writes only the
compressed result, where downloading first needs the unzipped size as scratch
space. Both can resume after an interruption and write info last. Run them in
tmux with nice/ionice, since they share the machine with the service.
| dataset | scale | on disk |
|---|---|---|
wclee_aedes_brain |
mip 0 (16_16_45) |
363 GiB |
wclee_aedes_brain |
mip 1 (32_32_45) |
168 GiB (526 GiB unzipped) |
zheng_ca3_v2 |
mip 1 (36_36_45) |
179 GiB (639 GiB unzipped) |
To restart the service (e.g. after changing the code), you can run:
sudo supervisorctl restart transform-service