// The brain card: the 3D view (scene.ts) with the Streamlit controls — Follow live, Learned pathways, the central owner // (whole brain), a minute slider per fly — plus playback through the last 24 hours and a pinned neuron's history. // Variant "memecoin" is the Overview's (the Solana fly's 41,756 neurons, topic brain.memecoin); "flies" is the Kalshi // overview's (the whole brain with both flies, topic brain.flies). import { useEffect, useMemo, useRef, useState, type Dispatch, type SetStateAction } from 'react' import { Callout, Caption, Card, Help, Icon, Segmented, Toggle } from '../components/ui' import { hm } from '../lib/format' import { useStored, useTopic } from '../lib/hooks' import { cachedFrame, fetchFrame, inFlight, inverseIndex, loadGeometry, loadIndex, minuteLabel, neuronHistory, normalise, payloadFrame, shortLabel, stampLabel, stampTime, type BrainPayload, type FlyData, type FlyName, type Frame, type Geometry, type Mode, type NeuronPoint, } from './data' import { BrainScene, popColor, rgbCss, strategyTints, type Layer, type SceneLine } from './scene' import './brain.css' export type BrainVariant = 'memecoin' | 'flies' const MODES = ['bootstrap', '24h'] as const const MODE_LABELS: Record = { bootstrap: 'since bootstrap', '24h': 'last 24 h' } const OWNERS = ['kalshi', 'memecoin'] as const const SPEEDS = ['1', '4', '16'] as const type Speed = (typeof SPEEDS)[number] const SPEED_LABELS: Record = { '1': '1×', '4': '4×', '16': '16×' } const MAX_INFLIGHT = 6 const HEAD: Record = { memecoin: { title: "The fly's brain", height: 560, sub: "the 41,756 neurons it runs on, lit by each minute's scoring (warm = positive, cool = negative); green and red lines are the KC→MBON synapses it has changed most", live: 'Show the newest minute as it arrives; moving the slider turns this off.', }, flies: { title: 'One brain, two flies', height: 620, sub: 'the optic lobes belong to the Kalshi fly (its inputs enter the photoreceptors), the antennal lobe and the other senses to the memecoin fly; the central brain is shared and shows the fly chosen below', live: "Show each fly's newest minute as it arrives; moving the slider turns this off.", }, } const HINT = 'drag to orbit · wheel to zoom · shift-drag to pan · double-click to recentre · click a neuron to pin it' const FOREIGN = "this minute's activity was recorded on another connectome" const errText = (e: unknown) => (e instanceof Error ? e.message : String(e)) /** The greatest stamp ≤ ``at`` in a sorted list, or null. */ function stampAt(minutes: string[], at: string): string | null { let lo = 0, hi = minutes.length - 1, out: string | null = null while (lo <= hi) { const mid = (lo + hi) >> 1 if (minutes[mid] <= at) { out = minutes[mid]; lo = mid + 1 } else hi = mid - 1 } return out } /** The index of the first stamp > ``at`` in a sorted list (its length when none is). */ function after(minutes: string[], at: string): number { let lo = 0, hi = minutes.length while (lo < hi) { const mid = (lo + hi) >> 1; if (minutes[mid] <= at) lo = mid + 1; else hi = mid } return lo } type Chosen = Partial> // a fly's picked minute; null: it had none yet (playback) export function BrainPanel({ variant }: { variant: BrainVariant }) { const flies = variant === 'flies' const [storedMode, setMode] = useStored(flies ? 'kbrain_pmode' : 'brain_pmode', 'bootstrap') const mode: Mode = MODES.includes(storedMode) ? storedMode : 'bootstrap' const t = useTopic(flies ? 'brain.flies' : 'brain.memecoin', { mode }) const [last, setLast] = useState(undefined) // the previous payload stays up while a new mode loads useEffect(() => { if (t.data) setLast(t.data) }, [t.data]) const data = t.data ?? last const h = HEAD[variant] if (data && !data.unavailable && data.geometry) return return ( {!data ? (t.error ? {t.error} :
) : {data.unavailable || 'No brain to draw: the payload has no geometry.'}} ) } function BrainView({ data, variant, mode, setMode, error }: { data: BrainPayload; variant: BrainVariant; mode: Mode; setMode: (m: Mode) => void; error?: string }) { const whole = variant === 'flies' const flies = useMemo(() => normalise(data), [data]) const [storedOwner, setOwner] = useStored('kbrain_owner', 'kalshi') const owner: FlyName = whole ? (OWNERS.includes(storedOwner) ? storedOwner : 'kalshi') : 'memecoin' const [storedSpeed, setSpeed] = useStored('brain_speed', '1') const speed: Speed = SPEEDS.includes(storedSpeed) ? storedSpeed : '1' const [legendOpen, setLegendOpen] = useStored('brain_legend', true) const [live, setLive] = useState(true) const [chosen, setChosen] = useState({}) const [playing, setPlaying] = useState(false) const [buffering, setBuffering] = useState(false) const [shown, setShown] = useState>>({}) const [pin, setPin] = useState(null) const [hiddenPops, setHiddenPops] = useState>(() => new Set()) const [hiddenGroups, setHiddenGroups] = useState>(() => new Set()) const host = useRef(null) const [scene, setScene] = useState(null) const [indices, setIndices] = useState>>({}) const [loadMsg, setLoadMsg] = useState('loading the brain…') const geomRef = data.geometry! const emphasis = data.emphasis ?? [] // ---- the scene: built once per connectome (geometry and index maps are cached for the tab), disposed on unmount const indexKey = JSON.stringify(flies.map((f) => f.index?.url ?? null)) useEffect(() => { const el = host.current if (!el) return let cancelled = false, sc: BrainScene | null = null setLoadMsg('loading the brain…'); setScene(null) void (async () => { let geom: Geometry try { geom = await loadGeometry(geomRef) } catch (e) { if (!cancelled) setLoadMsg(`the neuron geometry could not be fetched (${errText(e)}) — is static serving on?`) return } const ix: Partial> = {}, errs: string[] = [] if (whole) { for (const f of flies) { if (!f.index) continue try { ix[f.name] = await loadIndex(f.index) } catch (e) { errs.push(`${f.name}: index map could not be fetched (${errText(e)})`) } } } if (cancelled) return try { sc = new BrainScene(el, geom, emphasis) } catch (e) { setLoadMsg(`WebGL is not available in this browser (${errText(e)})`) return } sc.onPick = (i) => setPin(i) setIndices(ix); setScene(sc); setLoadMsg(errs.length ? errs.join('; ') : null) })() return () => { cancelled = true; sc?.dispose() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [geomRef.sha, geomRef.url, indexKey, whole]) const meta = scene?.geom.meta // ---- which minute each fly shows: its newest (live), or the picked one; frames come from the topic or the activity endpoint const target = useMemo(() => { const out: Partial> = {} for (const f of flies) { const newest = f.stamp ?? f.minutes[f.minutes.length - 1] ?? null, c = chosen[f.name] out[f.name] = live || c === undefined ? newest : c === null ? null : f.minutes.includes(c) ? c : newest } return out }, [flies, chosen, live]) useEffect(() => { let cancelled = false for (const f of flies) { const fromPayload = payloadFrame(f), s = target[f.name] ?? null if (!s) { setShown((p) => (p[f.name] === null ? p : { ...p, [f.name]: null })); continue } const hit = s === f.stamp && fromPayload ? fromPayload : cachedFrame(f.name, s) if (hit) { setShown((p) => (p[f.name] === hit ? p : { ...p, [f.name]: hit })); continue } void fetchFrame(f.name, s).then((fr) => { if (!cancelled && fr) setShown((p) => ({ ...p, [f.name]: fr })) }) } return () => { cancelled = true } }, [flies, target]) const check = (f: FlyData, fr: Frame | null | undefined): { act: Int8Array | null; note: string | null } => { if (!fr) return { act: null, note: null } if (fr.activity.length !== f.n || (fr.connectome && meta && fr.connectome !== meta.connectome_file)) return { act: null, note: FOREIGN } return { act: fr.activity, note: null } } const valid = flies.map((f) => ({ f, fr: shown[f.name] ?? null, ...check(f, shown[f.name]) })) const layersKey = valid.map((v) => `${v.f.name}:${v.fr?.stamp ?? ''}:${v.act ? 1 : 0}`).join('|') + `|${owner}|${Object.keys(indices).join(',')}` useEffect(() => { if (!scene) return const layers: Layer[] = valid.map((v) => ({ name: v.f.name, activity: v.act, index: whole ? indices[v.f.name] ?? null : null })) scene.recolor(layers, owner) // eslint-disable-next-line react-hooks/exhaustive-deps }, [scene, layersKey]) // ---- learned pathways: one line group per strategy (prefixed with the fly in the whole brain), rebuilt when a key changes const pkey = JSON.stringify(flies.map((f) => f.pathways?.key ?? null)) const lines = useMemo(() => { if (!whole) return (flies[0]?.pathways?.items ?? []).map((it) => ({ kc: it.kc, mbon: it.mbon, delta: it.delta, ratio: it.ratio, group: it.strategy })) const out: SceneLine[] = [] for (const f of flies) { const ix = indices[f.name] if (!ix) continue for (const it of f.pathways?.items ?? []) out.push({ kc: ix[it.kc] ?? -1, mbon: ix[it.mbon] ?? -1, delta: it.delta, ratio: it.ratio, group: `${f.name}:${it.strategy}` }) } return out // eslint-disable-next-line react-hooks/exhaustive-deps }, [pkey, indices, whole]) const groups = useMemo(() => { const base = whole ? flies.flatMap((f) => (f.strategies.length ? [...f.strategies.map((s) => `${f.name}:${s}`), `${f.name}:shared`] : [])) : flies[0]?.strategies.length ? [...flies[0].strategies, 'shared'] : [] for (const l of lines) if (!base.includes(l.group)) base.push(l.group) return base }, [flies, lines, whole]) const groupsKey = groups.join('|') const tints = useMemo(() => strategyTints(document.documentElement, groups), [groupsKey]) // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { scene?.setLines(lines, tints) }, [scene, lines, tints]) useEffect(() => { scene?.setHidden(hiddenPops, hiddenGroups) }, [scene, hiddenPops, hiddenGroups]) // ---- playback: step through every minute of the last 24 hours (1× = 2 minutes a second), prefetching ahead const timeline = useMemo(() => { const newest = flies.map((f) => f.minutes[f.minutes.length - 1]).filter(Boolean).sort().pop() if (!newest) return [] as string[] const from = stampTime(newest) - 86400e3, all = new Set() for (const f of flies) for (const s of f.minutes) if (stampTime(s) >= from) all.add(s) return [...all].sort() }, [flies]) const stampsAt = (at: string): Chosen => Object.fromEntries(flies.filter((f) => f.minutes.length).map((f) => [f.name, stampAt(f.minutes, at)])) const cursor = useRef('') // the minute playback shows (a stamp: the timeline shifts as minutes arrive) const live$ = useRef({ timeline, flies, stampsAt }) live$.current = { timeline, flies, stampsAt } useEffect(() => { if (!playing) return const period = 500 / Number(speed), ahead = Math.max(8, Math.ceil(Number(speed) * 2 * 3)) let raf = 0, lastT = performance.now(), acc = period, wasBuffering = false const ready = (c: Chosen) => Object.entries(c).every(([fly, s]) => !s || !!cachedFrame(fly as FlyName, s)) const prefetch = (from: number) => { const { timeline: tl, flies: fl } = live$.current for (let k = from; k < Math.min(tl.length, from + ahead) && inFlight() < MAX_INFLIGHT; k++) { for (const f of fl) { const s = f.minutes.length ? stampAt(f.minutes, tl[k]) : null if (s && !cachedFrame(f.name, s)) void fetchFrame(f.name, s) } } } const tick = () => { const now = performance.now(); acc = Math.min(acc + now - lastT, period * 8); lastT = now // a background tab does not jump ahead const { timeline: tl, stampsAt: at } = live$.current let moved: Chosen | null = null, stalled = false while (acc >= period) { // several minutes a frame at 16× when the frames are there const next = after(tl, cursor.current) if (next >= tl.length) { setPlaying(false); setBuffering(false); setLive(true); return } const want = at(tl[next]) if (!ready(want)) { for (const [fly, s] of Object.entries(want)) if (s) void fetchFrame(fly as FlyName, s) stalled = true; acc = period break } cursor.current = tl[next]; acc -= period; moved = want } if (moved) setChosen(moved) if (stalled && !wasBuffering) { wasBuffering = true; setBuffering(true) } else if (moved && wasBuffering) { wasBuffering = false; setBuffering(false) } prefetch(after(tl, cursor.current)) raf = requestAnimationFrame(tick) } raf = requestAnimationFrame(tick) return () => { cancelAnimationFrame(raf); setBuffering(false) } }, [playing, speed]) const play = () => { if (playing) { setPlaying(false); return } if (timeline.length < 2) return const shownMax = flies.map((f) => target[f.name]).filter((s): s is string => !!s).sort().pop() let k = 0 if (!live && shownMax && shownMax < timeline[timeline.length - 1]) { for (let i = 0; i < timeline.length; i++) if (timeline[i] <= shownMax) k = i } cursor.current = timeline[k] setChosen(stampsAt(timeline[k])); setLive(false); setPlaying(true) } const setLiveTo = (v: boolean) => { setPlaying(false) if (v) setLive(true) else { setChosen(Object.fromEntries(flies.map((f) => [f.name, target[f.name] ?? null]))); setLive(false) } } const pick = (f: FlyData, i: number) => { // the other fly stays on the minute it shows setPlaying(false); setLive(false) setChosen({ ...Object.fromEntries(flies.map((x) => [x.name, target[x.name] ?? null])), [f.name]: f.minutes[i] }) } // ---- the pinned neuron: its fly (where both flies have it, the central owner — unless only the other fly has a minute // to show, as the scene then shows that one) and that fly's sub-graph index useEffect(() => { scene?.setPinned(pin) }, [scene, pin]) const lit = valid.filter((v) => v.act).map((v) => v.f.name).join(',') const pinned = useMemo(() => { if (pin === null || !meta) return null if (!whole) return { fly: 'memecoin' as FlyName, sub: pin } const other: FlyName = owner === 'kalshi' ? 'memecoin' : 'kalshi', on = lit.split(',') for (const name of (on.includes(owner) || !on.includes(other) ? [owner, other] : [other, owner])) { const ix = indices[name] if (!ix) continue const k = inverseIndex(ix, meta.n)[pin] if (k >= 0) return { fly: name, sub: k } } return null }, [pin, meta, whole, owner, indices, lit]) // ---- what the card says const notes = valid.filter((v) => v.note).map((v) => (whole ? `${v.f.name}: ${v.note}` : v.note!)) const anyLit = valid.some((v) => v.act) const message = loadMsg ?? (live ? data.message ?? null : notes.length ? notes.join('; ') : anyLit ? null : data.message ?? null) let caption: string if (whole) { caption = valid.map((v) => `${v.f.name}: ` + (v.act && v.fr ? `${hm(v.fr.minute ?? '')} UTC, ${v.fr.candidates} candidates of ${v.fr.rows} rows` : 'no minute')).join(' · ') } else { const v = valid[0], fr = v?.act ? v.fr : null const when = fr ? (fr.minute ? minuteLabel(fr.minute) : `${stampLabel(fr.stamp)} UTC`) : null const head = fr && when ? (fr.candidates ? `${when}: mean activity over ${fr.candidates} candidate rows of ${fr.rows} eligible` : `${when}: no candidates, mean over all ${fr.rows} eligible rows`) : null const rest = data.caption_rest ?? (data.caption ?? '').replace(/^\d{2} \w{3} \d{2}:\d{2} UTC: [^·]*(· )?/, '') // an older server: drop its minute caption = [head, rest].filter(Boolean).join(' · ') } const h = HEAD[variant] const actions = ( <> ) return ( {error && {error} (showing the last good data)}
{whole && (
Central brain shows
)}
Learned pathways
{buffering && loading minutes…}
{flies.filter((f) => f.minutes.length).map((f) => ( pick(f, i)} /> ))}
{meta && ( )} {message &&
{message}
} {pin !== null && scene && ( setPin(null)} /> )} {scene &&
{HINT}
}
{caption} ) } function MinuteSlider({ label, minutes, value, onPick }: { label: string; minutes: string[]; value: string | null; onPick: (i: number) => void }) { const i = value ? minutes.indexOf(value) : -1 return (
{label}{value ? stampLabel(value) : '—'}
onPick(Number(e.target.value))} />
{stampLabel(minutes[0])}{stampLabel(minutes[minutes.length - 1])}
) } function Legend({ meta, groups, tints, hiddenPops, setHiddenPops, hiddenGroups, setHiddenGroups, open, setOpen }: { meta: Geometry['meta']; groups: string[]; tints: Record hiddenPops: Set; setHiddenPops: Dispatch>>; hiddenGroups: Set; setHiddenGroups: Dispatch>> open: boolean; setOpen: (v: boolean) => void }) { const flip = (set: Dispatch>>, name: string, on: boolean) => set((s) => { const n = new Set(s); if (on) n.delete(name); else n.add(name); return n }) return (
Populations
{open && ( <> {meta.pop_order.map((name) => { const [lo, hi] = meta.pop_ranges[name], on = !hiddenPops.has(name) return ( ) })} {groups.length > 0 &&
Learned pathways (green +, red −)
} {groups.map((g) => { const on = !hiddenGroups.has(g) return ( ) })} )}
) } type Valid = { f: FlyData; fr: Frame | null; act: Int8Array | null; note: string | null } function PinPanel({ scene, index, pinned, whole, flies, valid, onClose }: { scene: BrainScene; index: number; pinned: { fly: FlyName; sub: number } | null; whole: boolean; flies: FlyData[]; valid: Valid[]; onClose: () => void }) { const v = pinned ? valid.find((x) => x.f.name === pinned.fly) : undefined const raw = v?.act && pinned ? v.act[pinned.sub] : undefined const value = raw === undefined ? null : raw / 127 const newest = pinned ? flies.find((f) => f.name === pinned.fly)?.minutes.slice(-1)[0] ?? '' : '' const [hist, setHist] = useState<{ key: string; series: NeuronPoint[] | null; error: string | null }>({ key: '', series: null, error: null }) const nkey = pinned ? `${pinned.fly}/${pinned.sub}` : '' useEffect(() => { if (!pinned) return const ctl = new AbortController() neuronHistory(pinned.fly, pinned.sub, newest, ctl.signal).then( (series) => { if (!ctl.signal.aborted) setHist({ key: nkey, series, error: null }) }, (e) => { if (!ctl.signal.aborted) setHist((h) => ({ key: nkey, series: h.key === nkey ? h.series : null, error: errText(e) })) }) return () => ctl.abort() // eslint-disable-next-line react-hooks/exhaustive-deps }, [nkey, newest]) const series = hist.key === nkey ? hist.series : null const at = v?.fr?.minute ? Date.parse(v.fr.minute) : null return (
{scene.cellType(index)}
{scene.population(index)}{pinned ? ` · neuron ${pinned.sub.toLocaleString('en-US')}` : ''}{whole && pinned ? ` · the ${pinned.fly} fly` : ''}
0 ? 'pos' : value < 0 ? 'neg' : ''}`}> {value === null ? '—' : `${value >= 0 ? '+' : ''}${value.toFixed(2)}`} {value === null ? (pinned ? 'not lit this minute' : 'no fly runs this neuron') : `activity at ${hm(v?.fr?.minute ?? '')} UTC`}
{!pinned ? null : series ? : hist.key === nkey && hist.error ?
history unavailable: {hist.error}
:
loading its day…
}
) } function NeuronChart({ series, at }: { series: NeuronPoint[]; at: number | null }) { const W = 290, H = 64 const pts = useMemo(() => series.map((p) => [Date.parse(p.t), p.v] as const), [series]) if (pts.length < 2) return
Not enough minutes on disk yet for a history.
const t0 = pts[0][0], t1 = pts[pts.length - 1][0], span = t1 - t0 || 1 const m = Math.max(1, ...pts.map((p) => Math.abs(p[1]))) const x = (t: number) => ((t - t0) / span) * W, y = (v: number) => H / 2 - (v / m) * (H / 2 - 3) const line = pts.map(([t, v]) => `${x(t).toFixed(1)},${y(v).toFixed(1)}`).join(' ') return ( <> {at !== null && at >= t0 && at <= t1 && }
{shortLabel(pts[0][0])}±{(m / 127).toFixed(2)}{shortLabel(pts[pts.length - 1][0])}
) } export default BrainPanel