Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mesh-to-Connectomics Pipeline: Mathematical Design

Status: Original design proposal, not a specification of current behavior. Pseudocode, dependency versions, performance estimates, and open questions below require revalidation against the implementation. See the remaining validation gates and GPU deployment guide for follow-up work. Scope: General-purpose heuristic pipeline for extracting connectome graphs from 3D segmentation volumes and/or mesh data available from Neuroglancer, BossDB, and other volumetric neuroscience data repositories. Motivation: Datasets like the songbird Area X connectome (Rother et al. 2025) have publicly accessible segmentation volumes and synapse meshes via Neuroglancer, but no public connectivity table. This pipeline derives the connectome directly from the 3D data.


1. Problem Statement

1.1 Formal Definition

Given a 3D segmentation volume $V: \mathbb{Z}^3 \to \mathbb{N}$ where each voxel’s value is a neuron segment ID, optionally accompanied by per-neuron surface meshes ${M_i}_{i=1}^{N}$ and/or synapse mesh positions ${\mathbf{s}_k}$, derive:

  1. A directed weighted graph $G = (N, E, w)$ where $w: E \to \mathbb{R}^+$ represents connection strength
  2. Per-synapse records with pre/post neuron assignment, centroid position, and morphological features
  3. Per-neuron morphological features (volume, surface area, skeleton, cell type classification)

1.2 Two Operating Modes

ModeInputUse Case
Synapse-awareSegmentation + synapse mesh positionsSongbird (Neuroglancer has synapse meshes)
Contact-detectionSegmentation only (no synapse meshes)Future datasets where only segmentation exists

1.3 Output Schema

The pipeline populates these existing SynDB tables (schema v3):

  • neurons: neuron_id, cell_type, mesh_volume, mesh_surface_area, mesh_sphericity, centroid_x/y/z, cable_length, n_branches, surface_area, volume, max_axis_length
  • synapses: synapse_id, pre_neuron_id, post_neuron_id, synapse_type, neurotransmitter, strength, synapse_count, centroid_x/y/z
  • pre_synaptic_terminals: terminal_id, vesicle_count, forms_synapse_with, mesh_volume, mesh_surface_area, centroid_x/y/z
  • neuron_relations: aggregated pre->post connection with strength and relation_count

2. Pipeline Architecture

                     +-----------------------------+
                     |   Stage 0: Data Acquisition |
                     |  (Neuroglancer / BossDB / S3)|
                     +----------+-------+----------+
                                |       |
                    +-----------v--+ +--v-------------+
                    | Segmentation | | Synapse Mesh   |
                    |   Volume V   | | Positions S    |
                    +-------+------+ +------+---------+
                            |               |
              +-------------v---------+     |
              | Stage 1: Neuron       |     |
              | Morphology Extraction |     |
              | (mesh metrics, skel)  |     |
              +-------------+---------+     |
                            |               |
              +-------------v---------------v-----+
              |   Stage 2: Contact Detection       |
              |   (synapse-aware OR contact-based) |
              +-------------+-----------------------+
                            |
              +-------------v--------------+
              | Stage 3: Surface Geometry |
              | (normals, curvature, area)|
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 4: Synapse Scoring  |
              | (probability per contact) |
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 5: Pre/Post         |
              | Assignment                |
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 6: Process          |
              | Classification (axon/dend)|
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 7: Cell Type        |
              | Classification (GMM)      |
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 8: Graph            |
              | Construction + Validation |
              +-------------+--------------+
                            |
              +-------------v--------------+
              | Stage 9: Confidence        |
              | Scoring + Parquet Output  |
              +---------------------------+

3. Mathematical Foundations

3.1 Notation

SymbolDefinition
$V(\mathbf{x})$Segmentation volume; returns neuron ID at voxel $\mathbf{x} = (x, y, z)$
$V^{-1}(i)$Set of voxels belonging to neuron $i$: ${\mathbf{x} : V(\mathbf{x}) = i}$
$M_i$Triangle mesh surface of neuron $i$
$\partial V^{-1}(i)$Boundary voxels of neuron $i$ (voxels in $V^{-1}(i)$ with at least one non-$i$ face-neighbor)
$N_6(\mathbf{x})$6-connected voxel neighborhood: ${\mathbf{x} \pm \hat{e}k}{k=1}^{3}$
$N_{26}(\mathbf{x})$26-connected voxel neighborhood
$\Delta = (\Delta_x, \Delta_y, \Delta_z)$Voxel resolution in nm
$\phi_i(\mathbf{x})$Signed distance field: positive inside $V^{-1}(i)$, negative outside
$\hat{\mathbf{n}}_i(\mathbf{x})$Outward surface normal of neuron $i$ at boundary point $\mathbf{x}$
$H(\mathbf{x})$Mean curvature at surface point $\mathbf{x}$
$\kappa_1, \kappa_2$Principal curvatures

3.2 Voxel Resolution Conventions

All physical quantities are computed in nanometers and stored as Int32 in ClickHouse. Voxel-to-physical conversion:

$$\text{physical_position} = \text{voxel_coord} \times \Delta_k \quad (k \in {x, y, z})$$

$$\text{voxel_volume}{nm^3} = N{voxels} \times \Delta_x \times \Delta_y \times \Delta_z$$

$$\text{contact_area}{nm^2} = N{face_pairs} \times \Delta_j \times \Delta_k \quad (j \neq k \text{ depending on face orientation})$$

3.3 Distance Fields

The signed distance field $\phi_i$ for neuron $i$ is computed via a chamfer distance transform (3D variant of the Borgefors algorithm):

Forward pass (z ascending):
    phi(x,y,z) = min over N6 of [phi(neighbor) + w_directional]

Backward pass (z descending):
    phi(x,y,z) = min over N6 of [phi(neighbor) + w_directional]

Weights (Borgefors optimal):
    w_face   = 3     (+/-x, +/-y, +/-z steps)
    w_edge   = 4     (face-diagonal steps, for 26-conn)
    w_corner = 5     (body-diagonal steps, for 26-conn)

The gradient $\nabla \phi_i$ gives the surface normal direction:

$$\hat{\mathbf{n}}_i(\mathbf{x}) = \frac{\nabla \phi_i(\mathbf{x})}{|\nabla \phi_i(\mathbf{x})|}$$

Computed via central differences:

$$\nabla \phi_i(\mathbf{x}) = \left(\frac{\phi_i(\mathbf{x}+\hat{e}_x) - \phi_i(\mathbf{x}-\hat{e}_x)}{2\Delta_x}, \quad \frac{\phi_i(\mathbf{x}+\hat{e}_y) - \phi_i(\mathbf{x}-\hat{e}_y)}{2\Delta_y}, \quad \frac{\phi_i(\mathbf{x}+\hat{e}_z) - \phi_i(\mathbf{x}-\hat{e}_z)}{2\Delta_z}\right)$$


4. Stage 0: Data Acquisition

4.1 Neuroglancer Precomputed Format

Neuroglancer datasets expose:

  • Segmentation volume: sharded precomputed chunks at /{layer}/{scale_key}/{shard}/ (gzip-compressed uint32 or uint64 arrays)
  • Mesh layer: Draco-encoded meshes per segment ID at /{layer}/meshes/{segment_id}:{lod}
  • Synapse mesh layer (if present): Draco-encoded synapse meshes at /{layer}/synapses/
  • Segment properties: JSON with cell type labels, IDs

4.2 Existing Infrastructure

The SynDB codebase already has:

  • bossdb.rs — Neuroglancer info file parsing, BossDbLayerInfo struct with scales, chunk sizes, resolutions
  • bossdb_zhang2025.rs — Full pipeline for downloading precomputed chunks from S3, decompressing, extracting per-segment statistics (centroid, volume, bounding box), and mapping vesicle centroids to parent neurons via coordinate lookup
  • swc.rs — SWC skeleton parsing with 3D Euclidean geometry (cable length, centroids, bounding boxes)

The mesh-to-connectomics pipeline extends this infrastructure with:

  1. Chunk-level contact detection during segmentation volume iteration (not a separate pass)
  2. Mesh fetching via the existing S3/HTTP download infrastructure
  3. Parry3D BVH for mesh-level proximity queries

4.3 Data Acquisition Pseudocode

#![allow(unused)]
fn main() {
// Reuse existing bossdb.rs infrastructure
fn fetch_segmentation_volume(layer_info: &BossDbLayerInfo) -> Result<SegmentationVolume> {
    let scale = select_optimal_scale(layer_info);  // balance resolution vs. size
    let chunks = list_chunk_keys(layer_info, &scale)?;
    let mut volume = SegmentationVolume::new(scale.size, scale.resolution);

    for chunk_key in chunks {
        let compressed = http_get(&chunk_key)?;
        let raw = decompress(&compressed, scale.encoding)?;
        volume.insert_chunk(&raw, &chunk_key.coordinates)?;
    }

    Ok(volume)
}

fn fetch_neuron_meshes(mesh_layer: &str, segment_ids: &[u64]) -> Result<Vec<NeuronMesh>> {
    // Download Draco-encoded meshes, decode to (vertices, triangles)
    segment_ids.par_iter().map(|&sid| {
        let draco = http_get(&format!("{mesh_layer}/{sid}:0"))?;
        let (vertices, triangles) = decode_draco(&draco)?;
        Ok(NeuronMesh { segment_id: sid, vertices, triangles })
    }).collect()
}

fn fetch_synapse_positions(synapse_layer: &str) -> Result<Vec<Point3D>> {
    // If synapse meshes exist, download and extract centroids
    let syn_info = fetch_info(synapse_layer)?;
    let mut positions = Vec::new();

    for chunk in list_chunk_keys(synapse_layer, &syn_info.scales[0])? {
        let draco = http_get(&chunk)?;
        let (verts, _) = decode_draco(&draco)?;
        let centroid = compute_centroid(&verts);
        positions.push(centroid);
    }

    Ok(positions)
}
}

5. Stage 1: Neuron Morphology Extraction

5.1 Per-Neuron Voxel Statistics

For each neuron $i$, compute from $V^{-1}(i)$:

$$\mathbf{c}i = \frac{1}{|V^{-1}(i)|} \sum{\mathbf{x} \in V^{-1}(i)} \mathbf{x} \quad \text{(centroid)}$$

$$\text{Vol}_i = |V^{-1}(i)| \cdot \Delta_x \Delta_y \Delta_z \quad \text{(volume in nm}^3\text{)}$$

$$\text{SA}_i = |\partial V^{-1}(i)| \cdot \overline{\Delta_j \Delta_k} \quad \text{(surface area)}$$

$$\Psi_i = \frac{\pi^{1/3} (6 \cdot \text{Vol}_i)^{2/3}}{\text{SA}_i} \quad \text{(sphericity)}$$

$$\text{AVR}_i = \frac{\text{SA}_i}{\text{Vol}_i^{2/3}} \quad \text{(area-volume ratio)}$$

Voxel radius (equivalent sphere):

$$r_i = \left(\frac{3 \cdot \text{Vol}_i}{4\pi}\right)^{1/3}$$

Bounding box:

$$\mathbf{b}{min}^{(i)} = \min{\mathbf{x} \in V^{-1}(i)} \mathbf{x}, \quad \mathbf{b}{max}^{(i)} = \max{\mathbf{x} \in V^{-1}(i)} \mathbf{x}$$

$$L_{max}^{(i)} = |\mathbf{b}{max}^{(i)} - \mathbf{b}{min}^{(i)}|_\infty \quad \text{(max axis length)}$$

5.2 Surface Area Computation

The surface area is the count of boundary face pairs – faces where one side is neuron $i$ and the other is not:

$$\text{SA}i = \sum{f \in \text{Faces}(i)} A_f$$

where each face $f$ has area $\Delta_j \times \Delta_k$ depending on its normal direction (xy-face: $\Delta_x \Delta_y$, xz-face: $\Delta_x \Delta_z$, yz-face: $\Delta_y \Delta_z$).

5.3 Skeleton Extraction (TEASAR)

For process classification (Stage 6), we need a 1D skeleton of each neuron. The TEASAR (Tree-structure Extraction Algorithm for Skeletons and Accurate Registration) algorithm:

  1. Compute distance transform $D_i(\mathbf{x})$ = distance from $\mathbf{x}$ to $\partial V^{-1}(i)$
  2. Find the root: $\mathbf{r} = \arg\max_{\mathbf{x}} D_i(\mathbf{x})$ (deepest interior point)
  3. Iteratively extract longest paths (Dijkstra on the distance field):
    • Find farthest point from current skeleton: $\mathbf{p} = \arg\max_{\mathbf{x}} d_{skeleton}(\mathbf{x})$
    • Trace shortest path from $\mathbf{p}$ to existing skeleton
    • Add path to skeleton
  4. Repeat until all points are within threshold $\tau_{TEASAR}$ of the skeleton

The skeleton is stored as an SWC-compatible tree, reusing the existing swc.rs infrastructure.

From the skeleton, extract:

  • Cable length: $L_{cable} = \sum_{(a,b) \in \text{edges}} |\mathbf{p}_a - \mathbf{p}_b|$ (already in swc.rs:184)
  • Branch count: nodes with $>1$ child (already in swc.rs:238)
  • Max axis length (already in swc.rs:242)
  • Process diameter profile: at each skeleton node, $d(\mathbf{p}) = 2 \cdot D_i(\mathbf{p})$ (distance to boundary $\times$ 2)

5.4 Mesh-Based Morphology (when meshes are available)

Using tri-mesh for per-mesh structural analysis:

#![allow(unused)]
fn main() {
fn compute_mesh_morphology(mesh: &TriMesh) -> MeshMorphology {
    MeshMorphology {
        volume: mesh.volume(),                    // exact watertight volume
        surface_area: mesh.surface_area(),         // sum of triangle areas
        sphericity: compute_sphericity(mesh),
        centroid: mesh.center_of_mass(),
        bounding_box: mesh.bounding_box(),
        is_watertight: mesh.is_watertight(),
    }
}
}

6. Stage 2: Contact Detection

6.1 Mode A: Synapse-Aware Detection

When synapse mesh positions ${\mathbf{s}_k}$ are available, each synapse position is a point in 3D space. To find pre/post partners:

  1. Look up the segmentation volume at the synapse position: $V(\mathbf{s}_k)$ gives one neuron ID
  2. Search the local neighborhood for the partner:

$$\text{partner}(\mathbf{s}k) = \arg\max{j \neq V(\mathbf{s}k)} |N{26}(\mathbf{s}_k) \cap V^{-1}(j)|$$

The neuron with the most voxels in the 26-neighborhood of $\mathbf{s}_k$ (excluding the neuron that contains $\mathbf{s}_k$) is the partner.

Apposition midpoint: If both neurons are found in the neighborhood, the synapse is assigned to the pair. If only one neuron is found, it’s an orphan (edge of volume or segmentation error).

#![allow(unused)]
fn main() {
fn synapse_aware_detection(
    synapse_positions: &[Point3D],
    volume: &SegmentationVolume,
) -> Vec<RawContact> {
    synapse_positions.par_iter().filter_map(|s| {
        let center_id = volume.lookup(s);
        if center_id == 0 { return None; }

        // Count neighbor labels in 26-neighborhood
        let mut label_counts: HashMap<u64, usize> = HashMap::new();
        for offset in NEIGHBORHOOD_26 {
            let neighbor = s + offset;
            let lid = volume.lookup(&neighbor);
            if lid != 0 && lid != center_id {
                *label_counts.entry(lid).or_default() += 1;
            }
        }

        // Partner = most common non-center label
        let partner_id = label_counts.iter()
            .max_by_key(|(_, &c)| c)
            .map(|(&id, _)| id)?;

        Some(RawContact {
            neuron_a: center_id,
            neuron_b: partner_id,
            position: *s,
            neighbor_count: *label_counts.get(&partner_id).unwrap(),
            detection_mode: SynapseMesh,
        })
    }).collect()
}
}

6.2 Mode B: Contact Detection via Voxel Boundary Intersection

When no synapse meshes are available, detect contacts by scanning the segmentation volume for boundary voxels.

Boundary voxel: A voxel $\mathbf{x}$ with $V(\mathbf{x}) = i$ is a boundary voxel for neuron $i$ if any 6-neighbor has a different non-zero ID.

Contact pair: A face-adjacent pair $(\mathbf{x}, \mathbf{y})$ where $V(\mathbf{x}) = i$, $V(\mathbf{y}) = j$, $i \neq j$, $i, j \neq 0$.

The set of all contact pairs:

$$\mathcal{C} = { (i, j, \mathbf{m}) : \exists \mathbf{x}, \mathbf{y} \in N_6, V(\mathbf{x})=i, V(\mathbf{y})=j, \mathbf{m} = \frac{\mathbf{x}+\mathbf{y}}{2} }$$

where $\mathbf{m}$ is the midpoint (contact centroid).

Contact area for pair $(i,j)$:

$$A_{ij} = |{ (\mathbf{x}, \mathbf{y}) \in \mathcal{C}{ij} }| \times \overline{\Delta^2{jk}}$$

where $\overline{\Delta^2_{jk}}$ is the face area (depends on which axis the pair is adjacent along: xy-face = $\Delta_x \Delta_y$, xz-face = $\Delta_x \Delta_z$, yz-face = $\Delta_y \Delta_z$).

Efficient implementation: Single pass through the volume, checking only 3 of 6 face directions (avoid double-counting):

#![allow(unused)]
fn main() {
fn detect_contacts(volume: &SegmentationVolume) -> Vec<RawContact> {
    let (sx, sy, sz) = volume.shape();
    let (dx, dy, dz) = volume.resolution;
    let mut contacts: HashMap<(u64, u64), ContactAccumulator> = HashMap::new();

    // Check +x, +y, +z neighbors only (avoid double counting)
    for z in 0..sz {
        for y in 0..sy {
            for x in 0..sx {
                let id_a = volume.get(x, y, z);
                if id_a == 0 { continue; }

                // +x neighbor
                if x + 1 < sx {
                    let id_b = volume.get(x + 1, y, z);
                    if id_b != 0 && id_b != id_a {
                        accumulate_contact(&mut contacts, id_a, id_b,
                            x, y, z, dy * dz);  // yz-face area
                    }
                }
                // +y neighbor
                if y + 1 < sy {
                    let id_b = volume.get(x, y + 1, z);
                    if id_b != 0 && id_b != id_a {
                        accumulate_contact(&mut contacts, id_a, id_b,
                            x, y, z, dx * dz);  // xz-face area
                    }
                }
                // +z neighbor
                if z + 1 < sz {
                    let id_b = volume.get(x, y, z + 1);
                    if id_b != 0 && id_b != id_a {
                        accumulate_contact(&mut contacts, id_a, id_b,
                            x, y, z, dx * dy);  // xy-face area
                    }
                }
            }
        }
    }

    contacts.into_values().collect()
}
}

6.3 Mode C: Mesh-Based Contact Detection (parry3d)

When neuron meshes are available, use parry3d-f64 BVH for precise proximity queries.

Algorithm:

  1. Load all neuron meshes as parry3d::shape::TriMesh with identity transforms
  2. Build a global BVH (Bounding Volume Hierarchy) over all meshes
  3. Query all pairs within distance $d_{max}$:
#![allow(unused)]
fn main() {
use parry3d_f64::shape::TriMesh;
use parry3d_f64::query;
use parry3d_f64::math::Isometry;

fn detect_mesh_contacts(
    meshes: &[NeuronMesh],
    d_max: f64,
) -> Vec<MeshContact> {
    let identity = Isometry::identity();

    // Build BVH for each mesh
    let trimeshes: Vec<TriMesh> = meshes.iter().map(|m| {
        TriMesh::new(m.vertices.clone(), m.indices.clone())
    }).collect();

    let mut contacts = Vec::new();

    // O(N * log(N)) per mesh using BVH
    for (i, mesh_a) in trimeshes.iter().enumerate() {
        for (j, mesh_b) in trimeshes.iter().enumerate().skip(i + 1) {
            // Distance query via parry3d
            if let Ok(dist) = query::distance(&identity, mesh_a, &identity, mesh_b) {
                if dist < d_max {
                    // Find closest point pairs for contact region
                    let contact = extract_contact_region(mesh_a, mesh_b, d_max);
                    contacts.push(MeshContact {
                        neuron_a: meshes[i].segment_id,
                        neuron_b: meshes[j].segment_id,
                        distance: dist,
                        contact_points: contact.points,
                        contact_area: contact.area,
                    });
                }
            }
        }
    }

    contacts
}
}

Complexity: $O(N^2 \cdot \log T)$ where $T$ is average triangle count per mesh. For large $N$ ($>10^4$), use a coarse voxel pre-filter: first check if bounding boxes overlap (expanded by $d_{max}$), then do precise parry3d query only for close pairs.

The optimal pipeline combines all three modes:

  1. If synapse meshes exist -> Mode A (fastest, most accurate)
  2. Else if neuron meshes exist but no synapse meshes -> Mode C with coarse voxel pre-filter
  3. Else -> Mode B (voxel contact detection, single volume pass)

Mode B can also serve as a pre-filter for Mode C: first detect approximate contacts from the segmentation volume, then refine with mesh-level queries.


7. Stage 3: Surface Geometry Extraction

7.1 Surface Normal Estimation

Voxel-based normals

At each boundary voxel $\mathbf{x}$ of neuron $i$, compute the normal from the distance field gradient:

$$\hat{\mathbf{n}}_i(\mathbf{x}) = -\frac{\nabla \phi_i(\mathbf{x})}{|\nabla \phi_i(\mathbf{x})|}$$

(negative because $\phi_i$ is positive inside, so the gradient points inward; we want outward normals)

The distance field $\phi_i$ is computed via chamfer distance transform (Section 3.3). Central differences:

$$\nabla_x \phi_i(\mathbf{x}) = \frac{\phi_i(\mathbf{x} + \hat{e}_x) - \phi_i(\mathbf{x} - \hat{e}_x)}{2 \Delta_x}$$

Mesh-based normals

Using tri-mesh, vertex normals are area-weighted averages of adjacent face normals:

$$\hat{\mathbf{n}}i(\mathbf{v}) = \frac{\sum{f \in \mathcal{F}(\mathbf{v})} A_f \hat{\mathbf{n}}f}{\left|\sum{f \in \mathcal{F}(\mathbf{v})} A_f \hat{\mathbf{n}}_f\right|}$$

where $\mathcal{F}(\mathbf{v})$ is the set of faces adjacent to vertex $\mathbf{v}$, $A_f$ is the area of face $f$, and $\hat{\mathbf{n}}_f$ is the face normal.

7.2 Normal Alignment Score

For a contact between neurons $i$ and $j$ at midpoint $\mathbf{m}$:

$$\alpha(\mathbf{m}) = \hat{\mathbf{n}}_i(\mathbf{m}) \cdot \left(-\hat{\mathbf{n}}_j(\mathbf{m})\right)$$

This is the cosine of the angle between the outward normal of neuron $i$ and the inward normal of neuron $j$. For true synaptic contacts, the membranes face each other, so $\alpha \approx 1$.

Interpretation:

  • $\alpha > 0.7$ (within ~45 degrees): aligned membranes, strong candidate
  • $0 < \alpha < 0.7$: oblique contact, weak candidate
  • $\alpha < 0$: membranes face away from each other, not a synapse

For a contact region with multiple boundary voxels:

$$\bar{\alpha}{ij} = \frac{1}{|C{ij}|} \sum_{\mathbf{m} \in C_{ij}} \alpha(\mathbf{m})$$

7.3 Mean Curvature

Discrete curvature (Meyer et al. 2002)

For a mesh vertex $\mathbf{v}$ with 1-ring neighbors $\mathbf{w}_1, \ldots, \mathbf{w}_n$:

$$H(\mathbf{v}) = \frac{1}{2A_{Vor}(\mathbf{v})} \sum_{j=1}^{n} (\cot \alpha_j + \cot \beta_j) |\mathbf{v} - \mathbf{w}_j|$$

where $\alpha_j, \beta_j$ are the angles opposite to edge $(\mathbf{v}, \mathbf{w}j)$ in the two adjacent triangles, and $A{Vor}(\mathbf{v})$ is the Voronoi area of the 1-ring.

Voxel-based curvature approximation

For voxel surfaces, approximate curvature from the second derivatives of the distance field:

$$H \approx \frac{1}{2}\left(\frac{\partial^2 \phi}{\partial x^2} + \frac{\partial^2 \phi}{\partial y^2} + \frac{\partial^2 \phi}{\partial z^2}\right) \cdot \frac{1}{|\nabla \phi|}$$

(Laplace-Beltrami approximation on the implicit surface)

7.4 Curvature Score

Synapses form on relatively flat membrane regions. The curvature score:

$$\kappa(\mathbf{m}) = \exp\left(-\frac{H(\mathbf{m})^2}{2\sigma_H^2}\right)$$

where $\sigma_H$ is the expected curvature standard deviation for synaptic sites (~$0.01\ \text{nm}^{-1}$ for typical EM resolution).

Interpretation:

  • $\kappa \approx 1$: flat membrane, good synapse candidate
  • $\kappa \approx 0$: highly curved, unlikely synapse site

8. Stage 4: Synapse Candidate Scoring

8.1 Combined Synapse Probability

For each contact region $C_{ij}$, compute a composite synapse probability score:

$$S(C_{ij}) = \sigma\left(\beta_0 + \beta_1 \bar{\alpha}{ij} + \beta_2 f_A(A{ij}) + \beta_3 \bar{\kappa}{ij} + \beta_4 f{sz}(N_{voxels})\right)$$

where:

  • $\sigma(x) = (1 + e^{-x})^{-1}$ is the logistic function
  • $\bar{\alpha}_{ij}$ is the mean normal alignment (Section 7.2)
  • $f_A(A)$ is the area match function (Section 8.2)
  • $\bar{\kappa}_{ij}$ is the mean curvature score (Section 7.4)
  • $f_{sz}$ is a size penalty for very small/large contacts
  • $\beta_0, \ldots, \beta_4$ are coefficients fit from training data (or set to neurobiologically motivated defaults)

8.2 Area Match Function

Synaptic contacts have a characteristic size range. The PSD (postsynaptic density) area ranges from ~$0.01\ \mu m^2$ (small inhibitory) to ~$5\ \mu m^2$ (large excitatory). The area match function:

$$f_A(A) = \begin{cases} 0 & A < A_{min} \ \frac{A - A_{min}}{A_{typical} - A_{min}} & A_{min} \leq A < A_{typical} \ 1 & A_{typical} \leq A \leq A_{max} \ \frac{A_{max_large} - A}{A_{max_large} - A_{max}} & A > A_{max} \end{cases}$$

with defaults:

  • $A_{min} = 0.005\ \mu m^2$ (below this, likely noise)
  • $A_{typical} = 0.1\ \mu m^2$ (typical synapse)
  • $A_{max} = 2.0\ \mu m^2$ (large synapse)
  • $A_{max_large} = 10.0\ \mu m^2$ (above this, likely membrane apposition not synapse)

8.3 Default Coefficients

Coefficients initialized from neurobiological priors, refinable via logistic regression on validation data:

CoefficientDefaultInterpretation
$\beta_0$$-2.0$Bias (most contacts are not synapses)
$\beta_1$$+3.0$Normal alignment is the strongest signal
$\beta_2$$+1.5$Area match contributes moderately
$\beta_3$$+2.0$Flatness is a strong signal
$\beta_4$$-0.5$Size penalty (slight)

8.4 Threshold

Contacts with $S(C_{ij}) > \tau_{synapse}$ (default $\tau = 0.5$) are classified as synapse candidates. Below threshold -> discarded or stored as “putative contact” with low confidence.

8.5 Multi-Synapse Detection

A single contact region may represent multiple adjacent synapses. If $A_{ij} > 2 \cdot A_{typical}$, split the contact region into sub-regions using watershed segmentation on the curvature field (local curvature minima = synapse centers).

#![allow(unused)]
fn main() {
fn score_contacts(contacts: &mut Vec<Contact>) {
    for contact in contacts.iter_mut() {
        let alpha = mean_normal_alignment(&contact.boundary_voxels);
        let area = contact.total_area_nm2;
        let kappa = mean_curvature_score(&contact.boundary_voxels);

        let logit = BETA_0
            + BETA_1 * alpha
            + BETA_2 * area_match(area)
            + BETA_3 * kappa
            + BETA_4 * size_penalty(contact.voxel_count);

        contact.synapse_probability = 1.0 / (1.0 + (-logit).exp());

        // Multi-synapse splitting for large contacts
        if area > 2.0 * A_TYPICAL {
            contact.sub_synapses = watershed_split(&contact.boundary_voxels);
        }
    }
}
}

9. Stage 5: Pre/Post Synaptic Assignment

9.1 The Directionality Problem

Contact detection identifies which neurons touch, but not direction of signal flow. The presynaptic neuron sends neurotransmitters; the postsynaptic neuron receives them. We need morphological features that distinguish pre from post.

9.2 Feature Vector

At each contact site, for each of the two neurons $i$ and $j$, extract local morphological features within a sphere of radius $r_{local}$ (default: $2\ \mu m$) centered on the contact:

FeatureSymbolPresynaptic (bouton)Postsynaptic (spine/shaft)
Mean curvature$\bar{H}$Positive (convex swelling)Near-zero or slightly negative
Process diameter$\bar{d}$Small ($0.2$–$1\ \mu m$, axon)Large ($>1\ \mu m$, dendrite) or spine ($<1\ \mu m$)
Local volume ratio$\rho_V$$>1$ (swelling relative to process)$\approx 1$ (uniform) or spine protrusion
Curvature variance$\sigma_H$High (bouton creates bump)Low (smooth dendrite)
Distance field gradient$|\nabla \phi|$Sharp (small process)Gradual (large process)

9.3 Decision Function

The presynaptic score for neuron $i$ at a contact:

$$\Phi_i = w_H \cdot \text{sgn}(\bar{H}i) \cdot |\bar{H}i| + w_d \cdot \frac{1}{\bar{d}i} + w\rho \cdot \rho{V,i} + w\sigma \cdot \sigma_{H,i}$$

The postsynaptic score is $\Phi_j$ for the partner. Assignment:

$$\text{pre} = \begin{cases} i & \text{if } \Phi_i > \Phi_j + \delta \ j & \text{if } \Phi_j > \Phi_i + \delta \ \text{ambiguous} & \text{otherwise} \end{cases}$$

where $\delta$ is a margin threshold (default: $0.1$) to avoid unreliable assignments on close calls.

Default weights:

WeightDefaultRationale
$w_H$$1.0$Curvature is the strongest bouton signal
$w_d$$0.5$Diameter differentiates axon from dendrite
$w_\rho$$0.3$Volume ratio detects bouton swellings
$w_\sigma$$0.2$Curvature variance is a secondary signal

9.4 Process-Type-Based Assignment (Stronger Signal)

If Stage 6 has classified processes (axon vs. dendrite), use the biological rule: axons are presynaptic, dendrites are postsynaptic.

$$\text{pre} = \begin{cases} i & \text{if } \text{type}(i) = \text{axon and } \text{type}(j) = \text{dendrite} \ j & \text{if } \text{type}(j) = \text{axon and } \text{type}(i) = \text{dendrite} \ \text{use } \Phi \text{ heuristic} & \text{if both same type} \ \text{ambiguous} & \text{if both unknown} \end{cases}$$

This cascades: Stage 5 can use preliminary process classification, then refine with Stage 6 results, then re-assign if needed.

9.5 Vesicle-Based Assignment (When Available)

If vesicle segmentation is available (e.g., Zhang 2025 data), the neuron with vesicles near the contact site is presynaptic:

$$\text{pre} = \arg\max_{k \in {i,j}} |B_{vesicle}(k, r_{local})|$$

where $B_{vesicle}(k, r)$ is the count of vesicle centroids within radius $r$ of the contact, belonging to neuron $k$.

9.6 Symmetry Breaking for Axo-axonic Contacts

When both partners are axons (e.g., axo-axonic inhibition), use the curvature asymmetry:

$$\Delta\Phi = \Phi_i - \Phi_j$$

The neuron with the larger bouton swelling ($\Delta\Phi > 0$) is presynaptic.

#![allow(unused)]
fn main() {
fn assign_pre_post(
    contact: &Contact,
    morph_a: &LocalMorphology,
    morph_b: &LocalMorphology,
    process_types: Option<(ProcessType, ProcessType)>,
) -> Assignment {
    // Strong signal: process type
    if let Some((ta, tb)) = process_types {
        match (ta, tb) {
            (Axon, Dendrite) => return Assignment::PreA,
            (Dendrite, Axon) => return Assignment::PreB,
            _ => {}  // fall through to morphology
        }
    }

    // Morphological asymmetry
    let phi_a = presynaptic_score(morph_a);
    let phi_b = presynaptic_score(morph_b);

    if (phi_a - phi_b).abs() < DELTA_MARGIN {
        Assignment::Ambiguous
    } else if phi_a > phi_b {
        Assignment::PreA
    } else {
        Assignment::PreB
    }
}
}

10. Stage 6: Process Classification (Axon vs. Dendrite)

10.1 Skeleton-Based Features

From the TEASAR skeleton (Stage 1.3), classify each skeleton branch as axon or dendrite using:

10.1.1 Mean Process Diameter

At each skeleton node $\mathbf{p}$, the local process diameter is $d(\mathbf{p}) = 2 \cdot D_i(\mathbf{p})$ (twice the distance to the nearest boundary).

For a skeleton branch $B$ with nodes ${\mathbf{p}_1, \ldots, \mathbf{p}_n}$:

$$\bar{d}B = \frac{1}{n} \sum{k=1}^{n} d(\mathbf{p}k), \quad \sigma_d(B) = \sqrt{\frac{1}{n} \sum{k=1}^{n} (d(\mathbf{p}_k) - \bar{d}_B)^2}$$

$$CV_d(B) = \frac{\sigma_d(B)}{\bar{d}_B} \quad \text{(coefficient of variation)}$$

Classification:

  • $\bar{d}_B < 0.5\ \mu m$ -> axon (thin process)
  • $\bar{d}_B > 1.0\ \mu m$ -> dendrite (thick process)
  • $0.5 \leq \bar{d}_B \leq 1.0\ \mu m$ -> use $CV_d$: high ($>0.3$) suggests axon with boutons

10.1.2 Branching Angle

At a branch point with child direction vectors $\mathbf{t}_1, \mathbf{t}_2$:

$$\theta_{branch} = \arccos\left(\frac{\mathbf{t}_1 \cdot \mathbf{t}_2}{|\mathbf{t}_1| |\mathbf{t}_2|}\right)$$

Classification:

  • $\theta_{branch} < 60$ degrees -> axon (acute branching)
  • $\theta_{branch} > 90$ degrees -> dendrite (obtuse branching)

10.1.3 Tortuosity

$$\tau(B) = \frac{L_{path}(B)}{L_{euclidean}(B)} = \frac{\sum_{k=1}^{n-1} |\mathbf{p}_{k+1} - \mathbf{p}_k|}{|\mathbf{p}_n - \mathbf{p}_1|}$$

Classification:

  • $\tau > 1.5$ -> axon (tortuous path)
  • $\tau < 1.3$ -> dendrite (straighter)

10.2 Per-Neuron Classification

Aggregate branch-level classifications per neuron:

$$P(\text{axon} \mid \text{neuron}) = \frac{|\text{axon branches}|}{|\text{total branches}|}$$

A neuron is classified as:

  • Projection neuron (axon-dominant): if the longest branch is an axon and $>50%$ of branches are axons
  • Interneuron (if short axon, locally branching): if axon branches are all short ($<200\ \mu m$)
  • Dendrite-only (fragment): if no axon branches detected

10.3 Logistic Regression Model

Combine features into a probability:

$$P(\text{axon} \mid \mathbf{f}) = \sigma\left(w_0 + w_1 \bar{d} + w_2 CV_d + w_3 \theta_{branch} + w_4 \tau\right)$$

Default weights (from neurobiological literature):

WeightDefaultFeatureRange
$w_0$$+2.0$Bias–
$w_1$$-4.0$Mean diameter ($\mu m$)0.1–3.0
$w_2$$+2.0$CV of diameter0.05–1.0
$w_3$$-0.02$Branch angle (degrees)20–150
$w_4$$+1.5$Tortuosity1.0–3.0
#![allow(unused)]
fn main() {
fn classify_process(branch: &SkeletonBranch) -> ProcessType {
    let mean_d = branch.mean_diameter_um();
    let cv_d = branch.diameter_cv();
    let branch_angle = branch.mean_branch_angle_deg();
    let tortuosity = branch.tortuosity();

    let logit = W0 + W1 * mean_d + W2 * cv_d + W3 * branch_angle + W4 * tortuosity;
    let p_axon = 1.0 / (1.0 + (-logit).exp());

    if p_axon > 0.7 {
        ProcessType::Axon
    } else if p_axon < 0.3 {
        ProcessType::Dendrite
    } else {
        ProcessType::Unknown
    }
}
}

11. Stage 7: Cell Type Classification

11.1 Morphometric Feature Vector

For each neuron $i$, extract:

$$\mathbf{f}i = \begin{bmatrix} \log(V{soma,i}) \ \log(SA_{soma,i}) \ \log(L_{axon,i}) \ \log(L_{dendrite,i}) \ N_{branches,i} \ \bar{d}{axon,i} \ \bar{d}{dendrite,i} \ \tau_{axon,i} \ N_{boutons,i} \ \Psi_i \quad \text{(sphericity)} \ \text{Sholl}_1, \ldots, \text{Sholl}_K \end{bmatrix}$$

where Sholl analysis counts skeleton intersections at $K$ concentric radii from the soma:

$$\text{Sholl}_k = |{\text{skeleton points at } r_k \text{ from soma center}}|, \quad r_k = k \cdot \Delta r$$

with $\Delta r = 20\ \mu m$ and $K = 10$.

11.2 Gaussian Mixture Model

$$P(\text{type} = k \mid \mathbf{f}) = \frac{\pi_k \mathcal{N}(\mathbf{f} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}k)}{\sum{j=1}^{K} \pi_j \mathcal{N}(\mathbf{f} \mid \boldsymbol{\mu}_j, \boldsymbol{\Sigma}_j)}$$

where $K$ is the number of cell types (dataset-dependent), $\pi_k$ are mixing coefficients, $\boldsymbol{\mu}_k$ are mean feature vectors, and $\boldsymbol{\Sigma}_k$ are covariance matrices.

11.3 Initialization

If cell type labels are available from the source dataset (e.g., segment properties in Neuroglancer), use them as supervised labels instead of GMM. If partial labels exist, use semi-supervised GMM: initialize $\boldsymbol{\mu}_k$ from labeled examples, then refine on all data.

11.4 Known Cell Type Taxonomies

For datasets with known cell type taxonomies, map morphometric clusters to known types:

OrganismCell TypesClassification Method
Zebra finch (songbird)DA, LMAN, HVC, MSN, STN, TAN, GPe, GPi, LTS, INT1-3Supervised (segment properties) or GMM with $K=10$
DrosophilaKenyon cells, PN, MBON, DAN, etc.Supervised (hemibrain provides labels)
Mouse (MICrONS)Excitatory (Pyramidal, Spiny), Inhibitory (BC, MC, BP)Binary from neurotransmitter -> GMM for subtypes
C. elegans302 named neuronsSupervised (WormAtlas)

12. Stage 8: Graph Construction and Validation

12.1 Graph Construction

From the validated synapse candidates, build the directed weighted graph:

$$G = (N, E, w, \mathbf{c})$$

where:

  • $N$ = set of neuron IDs
  • $E = {(u, v) : \exists\ \text{synapse from } u \text{ to } v}$
  • $w(u, v) = \sum_{s \in \text{synapses}(u \to v)} S(s)$ (total synapse score)
  • $\mathbf{c}(u, v) = \frac{1}{|synapses(u \to v)|} \sum_{s} \mathbf{p}_s$ (mean synapse centroid)

12.2 Statistical Validation

Real connectomes have known statistical signatures. Validate the extracted graph:

12.2.1 Degree Distribution

The in-degree and out-degree should follow a heavy-tailed distribution:

$$P(k) \propto k^{-\gamma}, \quad \gamma \in [2, 3]$$

Test via Kolmogorov-Smirnov:

$$D_{KS} = \sup_k |F_{emp}(k) - F_{powerlaw}(k)|$$

Accept if $D_{KS} < 0.1$ (or use the Clauset et al. 2009 likelihood ratio test for power law vs. lognormal).

12.2.2 Synapse Count Distribution

The number of synapses per connection should follow approximately a log-normal:

$$\log w \sim \mathcal{N}(\mu, \sigma^2)$$

Typical parameters: $\mu \approx 0$ (1 synapse median), $\sigma \approx 0.7$ (most connections have 1-5 synapses, some have 10+).

12.2.3 Reciprocity

$$R = \frac{|{(u,v) : (u,v) \in E \wedge (v,u) \in E}|}{|E|}$$

Expected range: $R \in [0.1, 0.5]$ for most circuits. Too low ($<0.05$) suggests missed contacts; too high ($>0.6$) suggests false positives from over-detection.

12.2.4 Clustering Coefficient

$$C = \frac{3 \times \text{triangles}}{\text{connected triples}}$$

Expected range: $C \in [0.05, 0.5]$.

12.2.5 Small-World Index

$$\sigma_{SW} = \frac{C / C_{random}}{L / L_{random}}$$

where $C_{random}$ and $L_{random}$ are from an Erdos-Renyi graph with the same $N$ and $p$. Real connectomes: $\sigma_{SW} \gg 1$.

12.3 Graph Cleaning

After validation, apply graph-level corrections:

  1. Remove isolated neurons (degree 0) – likely segmentation fragments
  2. Merge split neurons – if two IDs have $>90%$ of their contacts in common (same neighbors), they may be over-split segmentation
  3. Filter weak edges – remove edges with $w < w_{min}$ (default: $S_{min} \times 0.1$)
  4. Symmetrize errors – if a contact has $\alpha < 0$ (normals face away), it’s likely a segmentation error, not a synapse

13. Stage 9: Confidence Scoring and Output

13.1 Per-Synapse Confidence

Each detected synapse gets a confidence score:

$$\text{conf}(s) = \underbrace{S(s)}{\text{synapse probability}} \times \underbrace{c{prepost}(s)}{\text{direction confidence}} \times \underbrace{c{type}(s)}_{\text{type consistency}}$$

where:

  • $S(s)$ is the synapse score from Stage 4
  • $c_{prepost}(s) = \frac{|\Phi_{pre} - \Phi_{post}|}{|\Phi_{pre} + \Phi_{post}|}$ (direction margin)
  • $c_{type}(s) = \begin{cases} 1.0 & \text{if process types are consistent (axon->dendrite)} \ 0.5 & \text{if ambiguous} \ 0.1 & \text{if inconsistent (dendrite->axon)} \end{cases}$

13.2 Per-Connection Confidence

$$\text{conf}(u \to v) = \frac{1}{|syn(u,v)|} \sum_{s \in syn(u,v)} \text{conf}(s)$$

13.3 Tiered Output

TierConfidenceTableTreatment
Confirmed$> 0.7$synapsesFull confidence, included in graph
Putative$0.3$–$0.7$synapses with metadata.confidence = "putative"Included with flag
Rejected$< 0.3$Not storedDiscarded

13.4 Parquet Output

The pipeline outputs Parquet files compatible with the existing ETL import framework:

prepared/
  neurons.parquet          # Stage 1: morphology + cell type
  synapses.parquet         # Stages 2-5: detected synapses with pre/post
  neuron_relations.parquet # Stage 8: aggregated connections
  pipeline_report.json     # Stage 9: statistics + validation

The pipeline_report.json contains:

{
  "dataset": "songbird-j0251",
  "mode": "synapse_aware",
  "neurons_detected": 8547,
  "contacts_detected": 128432,
  "synapses_validated": 19847,
  "connections": 12453,
  "mean_confidence": 0.78,
  "degree_distribution": { "gamma": 2.3, "ks_statistic": 0.06 },
  "reciprocity": 0.23,
  "clustering_coefficient": 0.18,
  "small_world_index": 3.2
}

14. Rust Crate Architecture

14.1 New Crate: mesh-connectomics

crates/tools/mesh-connectomics/
  Cargo.toml
  src/
    lib.rs                  # Public API
    volume.rs               # SegmentationVolume struct
    contact.rs              # Stage 2: contact detection
    geometry.rs             # Stage 3: normals, curvature
    scoring.rs              # Stage 4: synapse scoring
    direction.rs            # Stage 5: pre/post assignment
    process.rs              # Stage 6: axon/dendrite classification
    celltype.rs             # Stage 7: cell type GMM
    graph.rs                # Stage 8: graph construction + validation
    confidence.rs           # Stage 9: confidence + Parquet output
    skeleton/
      mod.rs                # TEASAR skeletonization
      teasar.rs             # Distance transform + Dijkstra
    mesh/
      mod.rs                # Mesh loading and validation
      parry_bridge.rs       # parry3d-f64 integration
      draco.rs              # Draco mesh decoding

14.2 Dependencies

[dependencies]
# Geometry -- the "same world" spatial analysis stack
parry3d-f64 = "0.13"       # BVH, distance queries, intersection tests
tri-mesh = "0.6"           # Half-edge mesh: volume, surface area, validation
mesh_repair = "0.3"        # Non-manifold detection, integrity checks

# Linear algebra
nalgebra = "0.33"          # Vectors, matrices, Solvers (used by parry3d)

# Spatial indexing
rstar = "0.12"             # R-tree for nearest-neighbor queries

# Data processing
polars = { workspace = true }
arrow = { workspace = true }
parquet = { workspace = true }

# Concurrency
rayon = "1.13"             # Data-parallel iteration (promote from transitive)

# Graph
petgraph = { workspace = true }

# Existing SynDB crates
syndb-clickhouse = { workspace = true }
etl = { workspace = true }  # Reuse bossdb.rs, swc.rs, download infrastructure

14.3 Integration with tri-mesh and parry3d

The architecture follows the recommended pattern: load/validate with tri-mesh, analyze world-space with parry3d.

#![allow(unused)]
fn main() {
use tri_mesh::Mesh;
use parry3d_f64::shape::TriMesh as ParryTriMesh;
use parry3d_f64::math::Isometry;
use parry3d_f64::query;

/// Convert a tri-mesh Mesh to a parry3d TriMesh for world-space queries.
fn to_parry_trimesh(mesh: &Mesh) -> ParryTriMesh {
    let vertices: Vec<_> = mesh.iter_vertices()
        .map(|v| parry3d_f64::math::Point3::new(v.pos.x, v.pos.y, v.pos.z))
        .collect();
    let indices: Vec<_> = mesh.iter_faces()
        .map(|f| {
            let (a, b, c) = mesh.face_vertices(f);
            [a as u32, b as u32, c as u32]
        })
        .collect();
    ParryTriMesh::new(vertices, indices)
}

/// Detect contacts between two neuron meshes in world space.
fn mesh_contact_distance(mesh_a: &Mesh, mesh_b: &Mesh) -> Option<f64> {
    let tri_a = to_parry_trimesh(mesh_a);
    let tri_b = to_parry_trimesh(mesh_b);
    let identity = Isometry::identity();

    query::distance(&identity, &tri_a, &identity, &tri_b).ok()
}
}

14.4 mesh_repair for Validation

Before processing, validate each neuron mesh:

#![allow(unused)]
fn main() {
use mesh_repair::MeshValidator;

fn validate_neuron_mesh(mesh: &Mesh) -> MeshValidationReport {
    let report = MeshValidator::new(mesh).validate();

    MeshValidationReport {
        is_watertight: report.is_watertight(),
        has_non_manifold_edges: report.has_non_manifold_edges(),
        has_self_intersections: report.has_self_intersections(),
        hole_count: report.hole_count(),
        // If broken, attempt repair
        repaired_mesh: if !report.is_watertight() {
            Some(mesh.repair().unwrap_or(mesh.clone()))
        } else {
            None
        },
    }
}
}

14.5 BVH-Based Global Scene

For datasets with meshes, build a global acceleration structure:

#![allow(unused)]
fn main() {
use parry3d_f64::bounding_volume::Aabb;
use parry3d_f64::partitioning::Qbvh;

struct MeshScene {
    bvh: Qbvh<u32>,            // BVH over neuron AABBs
    meshes: Vec<ParryTriMesh>, // All neuron meshes
    segment_ids: Vec<u64>,      // Segment ID per mesh index
}

impl MeshScene {
    fn build(meshes: Vec<(u64, ParryTriMesh)>) -> Self {
        let aabbs: Vec<Aabb> = meshes.iter()
            .map(|(_, m)| m.compute_aabb())
            .collect();

        let mut bvh = Qbvh::new();
        bvh.clear_and_rebuild(
            aabbs.iter().enumerate().map(|(i, aabb)| (i as u32, aabb.clone())),
            0.0,  // no margin
        );

        Self {
            bvh,
            meshes: meshes.into_iter().map(|(_, m)| m).collect(),
            segment_ids: meshes.into_iter().map(|(sid, _)| sid).collect(),
        }
    }

    fn find_close_pairs(&self, d_max: f64) -> Vec<(usize, usize)> {
        let identity = Isometry::identity();
        let mut pairs = Vec::new();

        for (i, mesh_a) in self.meshes.iter().enumerate() {
            // Expand AABB by d_max and query BVH
            let expanded_aabb = mesh_a.compute_aabb().loosened(d_max);

            self.bvh.intersect_aabb(&expanded_aabb, &mut |j| {
                if j > i {  // avoid double counting
                    if let Ok(dist) = query::distance(
                        &identity, mesh_a,
                        &identity, &self.meshes[j as usize],
                    ) {
                        if dist < d_max {
                            pairs.push((i, j as usize));
                        }
                    }
                }
                true  // continue iteration
            });
        }

        pairs
    }
}
}

15. Integration with SynDB ETL Framework

15.1 New ETL Module

The mesh-to-connectomics pipeline registers as a new ETL dataset family: MeshDerivedConnectome.

#![allow(unused)]
fn main() {
// crates/tools/etl/src/mesh_connectome/mod.rs

etl_table!(MeshConnectomeTable, "Mesh-Derived Connectome", {
    Neurons => "neurons" | "neuron": SyndbTable::Neuron,
    Synapses => "synapses" | "synapse": SyndbTable::Synapse,
    Relations => "neuron_relations" | "relation": SyndbTable::NeuronRelation,
});

etl_dataset!(MeshConnectomeTable,
    config_alias: MeshConnectomeImportConfig,
    label: "Mesh-Derived Connectome",
    expected_files: [
        "segmentation/info",
        "segmentation/provenance",
        "meshes/info",
    ],
    optional_files: [
        "synapses/info",           // synapse mesh layer
        "segment_properties/info", // cell type labels
    ],
);
}

15.2 Dataset Registration

Register in the existing dataset specs system:

#![allow(unused)]
fn main() {
// crates/tools/etl/src/registry/specs.rs

pub enum RuntimeFamily {
    SmallStatic,
    ApiHarvest,
    SpreadsheetSupplement,
    BulkPublicPrepared,
    ManualCaveExport,
    BossDbVolume,
    SwcSkeletons,
    MeshDerivedConnectome,  // NEW
}
}

15.3 Helm Configuration

# infrastructure/helm/syndb-etl/values.yaml
meshConnectome:
  prepare:
    image: syndb-etl:latest
    command: ["syndb-etl", "prepare", "--dataset", "mesh-connectome"]
    resources:
      requests:
        cpu: 4
        memory: 32Gi
      limits:
        cpu: 8
        memory: 64Gi
    emptyDirSizeLimit: 100Gi  # segmentation volumes are large
  import:
    image: syndb-etl:latest
    command: ["syndb-etl", "import", "--dataset", "mesh-connectome"]
    resources:
      requests:
        cpu: 2
        memory: 8Gi
      limits:
        cpu: 4
        memory: 16Gi

15.4 CLI Interface

#![allow(unused)]
fn main() {
// crates/tools/cli/src/etl/mesh_connectome.rs

#[derive(Subcommand)]
pub enum MeshConnectomeCmd {
    /// Detect contacts from segmentation volume
    Detect {
        #[arg(long)] data_dir: PathBuf,
        #[arg(long)] mode: ContactMode,  // synapse-aware | voxel | mesh
        #[arg(long, default_value = "0.5")] threshold: f64,
    },
    /// Score and classify detected contacts
    Score {
        #[arg(long)] data_dir: PathBuf,
        #[arg(long)] contacts: PathBuf,  // from Detect
    },
    /// Build graph from scored synapses
    BuildGraph {
        #[arg(long)] data_dir: PathBuf,
        #[arg(long)] synapses: PathBuf,  // from Score
    },
    /// Full pipeline: detect -> score -> build -> validate
    Pipeline {
        #[arg(long)] data_dir: PathBuf,
        #[arg(long)] mode: ContactMode,
        #[arg(long)] output: PathBuf,
    },
}
}

16. Validation Strategy

16.1 MICrONS Ground Truth

MICrONS provides annotated synapses with pre/post neuron IDs (microns_synapses/mod.rs, 19 GB CSV). Use these as ground truth:

  1. Run the mesh-to-connectomics pipeline on the MICrONS segmentation volume
  2. Compare detected synapses to ground truth:
    • Precision: $\frac{|\text{detected} \cap \text{true}|}{|\text{detected}|}$
    • Recall: $\frac{|\text{detected} \cap \text{true}|}{|\text{true}|}$
    • F1 score: $\frac{2 \cdot P \cdot R}{P + R}$
  3. Match criterion: detected synapse within $d_{match}$ (default: $500\ \text{nm}$) of a ground-truth synapse with the same pre/post pair

16.2 Parameter Optimization

Use MICrONS to fit the scoring coefficients $\beta_0, \ldots, \beta_4$:

# Validation script (Python, not production ETL)
from sklearn.linear_model import LogisticRegression

# Features: [alpha, area_match, curvature, size_penalty]
# Labels: 1 if true synapse, 0 if false positive
model = LogisticRegression(penalty='l1', C=1.0)
model.fit(features, labels)
beta = model.coef_[0]  # -> update defaults in scoring.rs

16.3 Cross-Phylum Validation

Run the pipeline on multiple datasets with known connectomes and verify statistical signatures:

DatasetKnown StatsValidate
MICrONS (mouse)~10K neurons, ~100K synapsesPrecision/recall
H01 (human)~3K neuronsDegree distribution
Hemibrain (fly)~25K neurons, ~150K synapsesReciprocity, motif counts
Songbird (zebra finch)~8.5K neurons (target)Cell type proportions

16.4 Statistical Sanity Checks

For each extracted connectome, verify:

  1. Lognormal synapse counts per connection (Shapiro-Wilk test on $\log w$)
  2. Power-law degree distribution (Clauset et al. 2009 test)
  3. Reciprocity in $[0.05, 0.6]$
  4. Density in expected range ($10^{-5}$ to $10^{-3}$ depending on circuit size)
  5. Largest SCC contains $>80%$ of neurons (real connectomes are strongly connected)

17. Performance Estimates

17.1 Voxel-Based Contact Detection (Mode B)

For a segmentation volume of size $S_x \times S_y \times S_z$ voxels:

  • Memory: $4 \times S_x \times S_y \times S_z$ bytes (uint32 segmentation)
  • Compute: $O(S_x \times S_y \times S_z)$ (single pass, 3 neighbor checks per voxel)
  • Example: MICrONS ($1254 \times 1333 \times 187$ voxels = ~313M voxels)
    • Memory: ~1.2 GB
    • Time: ~30 seconds (single-threaded), ~5 seconds (rayon, 8 cores)

17.2 Mesh-Based Contact Detection (Mode C)

For $N$ neurons with average $T$ triangles each:

  • BVH construction: $O(N \cdot T \cdot \log(N \cdot T))$
  • Pairwise queries: $O(K \cdot \log(N \cdot T))$ where $K$ is the number of close pairs after AABB pre-filter
  • Example: Songbird (~8.5K neurons, ~500 triangles each = 4.2M total triangles)
    • BVH: ~10 seconds
    • Queries: ~60 seconds (assuming ~100K close pairs after pre-filter)

17.3 Skeleton Extraction (TEASAR)

For each neuron with $V_i$ voxels:

  • Distance transform: $O(V_i)$ (chamfer, two passes)
  • Dijkstra: $O(V_i \cdot \log V_i)$ (worst case, typically much faster)
  • Example: Per neuron (~1M voxels): ~5 seconds. Total for 10K neurons: ~14 hours single-threaded, ~2 hours with rayon.

17.4 End-to-End Estimate

StageSongbird (8.5K neurons)MICrONS (10K neurons)
Data acquisition~30 min (S3 download)~45 min
Stage 1: Morphology~2 min (voxel)~2 min
Stage 2: Contact detection~5 sec (Mode A)~30 sec (Mode B)
Stage 3: Surface geometry~10 min~15 min
Stage 4: Scoring~1 min~2 min
Stage 5: Pre/Post~2 min~3 min
Stage 6: Process classification~2 hours (TEASAR)~3 hours
Stage 7: Cell type~5 min (GMM)~5 min
Stage 8: Graph~1 min~2 min
Total~2.5 hours~4 hours

TEASAR skeletonization dominates the runtime. For Mode A (synapse-aware) without full skeletonization, the total drops to ~30 minutes.


18. Summary of Mathematical Heuristics

HeuristicMathematical CoreKey ParametersJustification
Contact detection (voxel)Face-adjacent label pairs$N_6$ neighborhoodSynapses require membrane apposition
Contact detection (mesh)BVH distance query$d_{max} = 2\ \mu m$parry3d proximity
Normal alignment$\hat{n}_i \cdot (-\hat{n}_j)$$\alpha > 0.7$Membranes must face each other
Curvature score$\exp(-H^2 / 2\sigma_H^2)$$\sigma_H = 0.01\ \text{nm}^{-1}$Synapses on flat regions
Area matchPiecewise linear$A_{typ} = 0.1\ \mu m^2$Known PSD sizes
Synapse probabilityLogistic regression$\beta_0 = -2, \beta_1 = 3$Composite of above
Pre/post assignmentMorphological asymmetry $\Phi$$w_H = 1, w_d = 0.5$Bouton vs. dendrite
Process classificationLogistic regression on skeleton$w_1 = -4$ (diameter)Axons thinner
Cell typeGaussian mixture model$K$ = dataset-specificMorphometric clustering
Graph validationPower-law, lognormal, reciprocity$\gamma \in [2,3]$Known connectome signatures

19. Open Questions and Future Work

  1. Draco mesh decoding: Need a pure-Rust Draco decoder, or FFI to the C++ library. Alternatively, use the Neuroglancer mesh fragment format (uncompressed, simpler).

  2. Multi-resolution contact detection: For very large volumes (e.g., whole mouse brain), implement a multi-resolution approach: detect contacts at coarse resolution, then refine at full resolution only for candidate regions.

  3. Active learning: Use the MICrONS validation pipeline to iteratively improve the scoring coefficients. Deploy as a periodic re-scoring job.

  4. 3D point cloud alternative: Instead of processing the full segmentation volume, extract neuron boundary voxels only (sparse representation) and use rstar R-tree for neighbor queries.

  5. GPU acceleration: The chamfer distance transform and voxel contact detection are highly parallelizable. A CUDA or wgpu implementation could provide 10-100x speedup.

  6. Segmentation error correction: Use the graph structure to detect and correct segmentation errors (split/merge neurons) by analyzing connectivity consistency.


Appendix A: Neighborhood Definitions

6-connected neighborhood ($N_6$)

$$N_6(\mathbf{x}) = {\mathbf{x} \pm \hat{e}_x, \mathbf{x} \pm \hat{e}_y, \mathbf{x} \pm \hat{e}_z}$$

The 6 face-adjacent voxels. Used for contact detection (Section 6.2).

26-connected neighborhood ($N_{26}$)

$$N_{26}(\mathbf{x}) = {\mathbf{x} + \mathbf{d} : \mathbf{d} \in {-1, 0, 1}^3 \setminus {\mathbf{0}}}$$

All 26 neighboring voxels (6 face + 12 edge + 8 corner). Used for synapse-aware detection (Section 6.1) to handle synapse meshes that don’t align perfectly with voxel boundaries.

Appendix B: TEASAR Algorithm Detail

Input: binary mask M (voxels of neuron i), threshold tau
Output: skeleton tree S

1. Compute distance transform D(x) = min distance from x to boundary of M
2. Root r = argmax_x D(x)
3. S = {r}
4. While exists x in M with d_shortest(x, S) > tau:
     a. p = argmax_x d_shortest(x, S)  (farthest uncovered point)
     b. path = Dijkstra(p, nearest node in S, weighted by 1/D(x))
     c. S = S union path
5. Prune spurious branches: remove branches shorter than tau_prune
6. Merge nearby endpoints: if two endpoints are within tau_merge, merge

Distance transform weights (Borgefors 3D):

  • Face neighbor: $w_f = 3$
  • Edge neighbor: $w_e = 4$
  • Corner neighbor: $w_c = 5$

Dijkstra edge weight: $w(\mathbf{x}, \mathbf{y}) = \frac{|\mathbf{x} - \mathbf{y}|}{D(\mathbf{x}) + D(\mathbf{y})}$ (prefer paths through the interior of the neuron, where D is large)

Appendix C: Connection to Existing SynDB Code

Existing CodeReused ForLocation
bossdb.rsNeuroglancer info parsing, layer metadatacrates/tools/etl/src/bossdb.rs
bossdb_zhang2025.rsChunk download, decompression, voxel-to-neuron mappingcrates/tools/etl/src/bossdb_zhang2025.rs
swc.rsSkeleton parsing, cable length, branch counting, bounding boxcrates/tools/etl/src/swc.rs
columns.rsSchema: neurons, synapses, pre_synaptic_terminals, neuron_relationscrates/core/syndb-clickhouse/src/schema/columns.rs
etl_table! / etl_dataset! macrosDataset registration patterncrates/tools/etl/src/lib.rs
DownloadFile structS3/HTTP download manifestcrates/tools/etl/src/download.rs
petgraphGraph construction and algorithmscrates/core/connectome-graph/
polars / parquetParquet output for ClickHouse importworkspace dependencies
rayon (transitive)Parallel voxel processingpromote to direct dependency