Scene composition (connectomekg.scene)¶
connectomekg.scene
¶
connectomekg/scene.py
Builds the real-geometry 3-D views of a connectome: view A, every neuron's
marked point as a dim context cloud, and view B, the skeletons of a query's
neurons drawn at full brightness inside it, and view C, neuropils linked by
the signal flow their neurons carry between them. Both views can also draw
the neuropil surface meshes connkg meshes caches
(:mod:connectomekg.neuropil_meshes). Unlike the fleet's other viz3d
consumers (gutenberg_kg, pycode_kg, genealogy_kg), this graph
already has space -- every neuron carries real x/y/z coordinates
and the download holds a traced skeleton for most of them -- so there is no
tree to grow here, no kg_utils.viz3d.organic call anywhere in this file.
What is reused from that engine is just the camera rule (frame_tree, in
cli/cmd_viz3d.py) and seed_from_key for a stable per-type color.
Split the way pycode_kg.scene3d and genealogy_kg.scene split their own
layout from composition, so most of this is testable without PyVista:
- :class:
WorldFrame, :func:world_frame, :func:context_points, :func:circuit_neurons, :func:neuropil_flowand :func:flow_arcare pure NumPy plus SQL -- no PyVista import. - :func:
build_brain_scenecomposes those into a caller-suppliedpv.Plotter. This half needs theviz3dextra.
Colors come from :mod:connectomekg.colors, never from :mod:connectomekg.viz
-- viz.py imports plotly at module scope, and this module must stay
importable with only the viz3d extra installed, no viz. The same
reason genealogy_kg keeps its palette in theme.py rather than
viz.py.
NeuronGroup(label, neuron_ids, color)
dataclass
¶
Neurons drawn together in one color, under one label.
The circuit view otherwise groups by cell type and colors by name, which is right when the question is "show me LC4" and wrong when it is "show me the answer": a path's hops are an order, and the neurons in one hop rarely share a type. A group lets the caller say what belongs together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Names the group in the scene's title and its actor names. |
required |
neuron_ids
|
Sequence[str]
|
Neuron node ids in this group. |
required |
color
|
str
|
|
required |
NeuropilFlow(names, bases, centroids_nm, n_synapses, pairs)
dataclass
¶
Neuropil positions and the signal flow between them (view C).
Flow from neuropil A to B is carried by neurons: each neuron's output synapses in B, apportioned by the share of its input synapses that lie in A, summed over neurons, with A = B excluded. See docs/rendering.md, "What flow measures".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
list[str]
|
Neuropil abbreviations, e.g. |
required |
bases
|
list[str]
|
Each neuropil's side-free base, e.g. |
required |
centroids_nm
|
ndarray
|
|
required |
n_synapses
|
ndarray
|
|
required |
pairs
|
list[tuple[str, str, float]]
|
|
required |
SceneInfo(title, points, n_context, n_circuit, n_skeletons, missing_skeletons, soma_fallbacks, view='circuit', n_flow_pairs=0, n_flow_total=0, n_neuropil_meshes=0, n_context_somas=0, picks=PickTargets.empty())
dataclass
¶
What a composed brain scene contains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Human-readable summary for a window title or CLI echo. |
required |
points
|
ndarray
|
Every drawn point, world units, for camera framing via
|
required |
n_context
|
int
|
Neurons drawn in the context cloud. |
required |
n_circuit
|
int
|
Neurons resolved for the circuit view. |
required |
n_skeletons
|
int
|
Circuit neurons drawn from a loaded skeleton file. |
required |
missing_skeletons
|
list[int]
|
Circuit neurons' root ids with no skeleton file
loaded (no |
required |
soma_fallbacks
|
int
|
Circuit neurons whose skeleton had no |
required |
view
|
str
|
The view composed, one of :data: |
'circuit'
|
n_flow_pairs
|
int
|
Flow arcs drawn (view C). |
0
|
n_flow_total
|
int
|
Directed neuropil pairs with nonzero flow (view C). |
0
|
n_neuropil_meshes
|
int
|
Neuropil surface meshes drawn; 0 when none are cached. |
0
|
n_context_somas
|
int
|
Context-cloud neurons drawn at a real soma rather
than at FlyWire's marked point; 0 until |
0
|
picks
|
PickTargets
|
Every circuit point with the neuron that owns it, so a click in the viewer resolves to a neuron. Empty for the flow view. |
empty()
|
WorldFrame(center, scale=NM_PER_WORLD_UNIT, marker_scale=1.0)
dataclass
¶
The nm-to-world-units mapping a scene is framed in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
ndarray
|
|
required |
scale
|
float
|
Nanometres per world unit. |
NM_PER_WORLD_UNIT
|
marker_scale
|
float
|
Multiplier on marker sizes (neuropil spheres, flow tubes, soma spheres), so a dataset that frames larger than FAFB draws them the same size on screen; 1 for FAFB. The context cloud is left at FAFB's size: scaled up, its dots speckle the spheres inside it. |
1.0
|
to_world(points_nm)
¶
Map nm points to world units, dorsal up, section axis away from camera.
(x, y, z) nm becomes world
((x - cx) / S, (z - cz) / S, -(y - cy) / S) -- FAFB's y grows
ventrally, so negating it after centring puts dorsal at larger world
z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points_nm
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/connectomekg/scene.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
add_floor(plotter, background=BACKGROUND)
¶
Put a shadow-receiving floor under the composed scene, lit from above.
Adds a floor plane (actor floor) a step off the background just below
plotter.bounds, replaces the lights with a shadow-casting key
spotlight above the scene plus a weak headlight, and enables shadow
mapping. The floor is only visible from a camera that looks down on it,
so pair it with a non-zero elevation in :func:aim_camera, and call
it after framing: the floor is far larger than the scene and would
otherwise decide the framing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plotter
|
Plotter
|
Plotter with the scene composed and the camera framed. |
required |
background
|
str
|
The scene's background, which the floor is shaded
from (:func: |
BACKGROUND
|
Source code in src/connectomekg/scene.py
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 | |
add_studio_lighting(plotter)
¶
Light a floorless scene so its geometry reads as solid.
Replaces PyVista's five on-axis camera lights with three offset ones; see
:data:_STUDIO_LIGHTS for why the offset is the whole point and why they
stay camera-relative. Skeletons drawn as lines are unaffected either way
-- a line has no surface to shade, so --tubes is what makes lighting
visible at all on a circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plotter
|
Plotter
|
Plotter with the scene composed. |
required |
Source code in src/connectomekg/scene.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
aim_camera(plotter, points, *, fov=14.0, elevation=0.0, spec=None)
¶
Point the camera at a composed scene and frame it tightly at that view.
kg_utils.viz3d.frame_tree sets the view direction (from the front,
dorsal up), the camera tilts up by elevation degrees so it looks down on
the scene, and quiltwright.frame_and_focus then fits the scene at that
final direction and puts the focal plane at the harmonic mean of its near
and far depths. reset_camera() would fit the un-tilted bounds instead,
which leaves a tilted scene small in frame.
frame_and_focus measures plotter.bounds, so call this before
:func:add_floor. It also fits to the window's aspect: pass the quilt or
still spec you will render, and the window is set to the aspect
render_quilt captures each view at (16:9 for 16-landscape, whose
tiles are 4:3). Afterwards, pass fov=None to render_quilt and
depth_report: the camera is locked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plotter
|
Plotter
|
Plotter with the scene composed. |
required |
points
|
ndarray
|
World points the view direction is computed from,
usually |
required |
fov
|
float
|
Vertical field of view to lock the camera to, in degrees. |
14.0
|
elevation
|
float
|
Degrees to tilt the camera up from the front view. |
0.0
|
spec
|
QuiltSpec | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
|
Source code in src/connectomekg/scene.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | |
auto_skeleton_step(n_neurons, *, budget=SCENE_POINT_BUDGET)
¶
The stride to draw n_neurons at so the scene stays near budget points.
A stride of 4 is the finest the skeleton cache holds, so that is the floor
and a small scene simply gets it. Past roughly a thousand neurons the
stride grows to keep the point count flat, which is what lets
:data:connectomekg.validation.MAX_SCENE_NEURONS be five thousand rather
than five hundred: the cost of a scene stops tracking the neuron count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neurons
|
int
|
Neurons the scene will draw. |
required |
budget
|
int
|
Points to aim for across all of them. |
SCENE_POINT_BUDGET
|
Returns:
| Type | Description |
|---|---|
int
|
A stride between :data: |
Source code in src/connectomekg/scene.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
build_brain_scene(plotter, kg, *, specs=(), view='circuit', data_dir=None, color_by='super_class', skeleton_step=None, tubes=False, top=100, neuropils=True, cloud=None, groups=None, progress=None, background=BACKGROUND)
¶
Compose the whole-brain context cloud plus a circuit or neuropil flow into plotter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plotter
|
Plotter
|
PyVista plotter to compose into; actors are cleared first. |
required |
kg
|
ConnectomeKG
|
An open |
required |
specs
|
Sequence[str]
|
For |
()
|
view
|
str
|
|
'circuit'
|
data_dir
|
str | Path | None
|
Skeleton download root ( |
None
|
color_by
|
str
|
Context cloud coloring, |
'super_class'
|
skeleton_step
|
int | None
|
Skeleton simplification stride; |
None
|
tubes
|
bool
|
Draw circuit skeletons as tubes instead of lines. |
False
|
top
|
int
|
Flow arcs drawn, strongest first, bounded to
|
100
|
neuropils
|
bool
|
Draw the neuropil surface meshes when the dataset has
them cached ( |
True
|
cloud
|
bool | None
|
Draw the whole-brain context cloud of marked points;
|
None
|
progress
|
Callable[[str], None] | None
|
Called with a short message at each stage; |
None
|
background
|
str
|
Scene background, a :data: |
BACKGROUND
|
Returns:
| Type | Description |
|---|---|
SceneInfo
|
The composed :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
On an out-of-range argument, an unknown |
Source code in src/connectomekg/scene.py
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 | |
circuit_neurons(kg, specs)
¶
The union of every spec's neurons, for the circuit view (view B).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kg
|
ConnectomeKG
|
An open |
required |
specs
|
Sequence[str]
|
Specs, as accepted by :meth: |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted, deduplicated neuron node ids. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the union exceeds :data: |
Source code in src/connectomekg/scene.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
context_points(store, *, color_by='super_class')
¶
Every neuron's soma, or its marked point, for the whole-brain context cloud (view A).
A neuron carries FlyWire's marked point as x/y/z always, and
its real soma as soma_x/soma_y/soma_z once connkg skeletons
has back-filled one (:func:connectomekg.skeleton_cache.write_somas). The
soma wins where there is one, so a graph with the back-fill draws a soma
cloud and one without still draws the marked-point cloud it always did.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
GraphStore
|
The graph store. |
required |
color_by
|
str
|
|
'super_class'
|
Returns:
| Type | Description |
|---|---|
tuple[list[str], ndarray, list[str], int]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/connectomekg/scene.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
floor_color(background)
¶
The floor under background: :data:FLOOR_COLOR under the default,
else a step darker, or a step lighter under a dark background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
background
|
str
|
The scene background, |
required |
Returns:
| Type | Description |
|---|---|
str
|
The floor color, |
Source code in src/connectomekg/scene.py
641 642 643 644 645 646 647 648 649 650 651 | |
flow_arc(start, end, *, bow=_FLOW_BOW, n_points=_FLOW_ARC_POINTS)
¶
A quadratic arc from start to end, bowed to one side of its direction.
The bow points along direction x world-up (world +x when the
direction is vertical), so the arc for A -> B and the arc for B -> A bow to
opposite sides instead of overdrawing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
ndarray
|
|
required |
end
|
ndarray
|
|
required |
bow
|
float
|
Control point offset as a fraction of the chord length. |
_FLOW_BOW
|
n_points
|
int
|
Points along the arc, including both ends. |
_FLOW_ARC_POINTS
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/connectomekg/scene.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
neuropil_flow(store, neuron_ids=None)
¶
Aggregate IN_NEUROPIL evidence into neuropil centroids and flow.
Centroids always use every neuron, so neuropils sit in the same place in every flow render; only the flow sum is restricted by neuron_ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
GraphStore
|
The graph store. |
required |
neuron_ids
|
Iterable[str] | None
|
Neuron node ids whose flow is summed; |
None
|
Returns:
| Type | Description |
|---|---|
NeuropilFlow
|
The :class: |
Source code in src/connectomekg/scene.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
region_color(neuropil)
¶
The color for a neuropil: its brain region's color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuropil
|
str
|
FlyWire abbreviation, with or without side suffix. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in src/connectomekg/scene.py
243 244 245 246 247 248 249 250 | |
resolve_background(value)
¶
A background color from a :data:BACKGROUNDS name or a #RRGGBB string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
A :data: |
required |
Returns:
| Type | Description |
|---|---|
str
|
The color as upper-case |
Raises:
| Type | Description |
|---|---|
ValueError
|
If value is neither. |
Source code in src/connectomekg/scene.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
type_color(name)
¶
A deterministic color for a cell type name.
Indexes a fixed qualitative palette via seed_from_key, so the same
type name always draws the same color -- across renders, sessions and a
printed figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Cell type name, e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in src/connectomekg/scene.py
253 254 255 256 257 258 259 260 261 262 263 | |
world_frame(store)
¶
The scene's :class:WorldFrame, centered on the median neuron position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
GraphStore
|
The graph store. |
required |
Returns:
| Type | Description |
|---|---|
WorldFrame
|
A frame centered on the median of every neuron's marked point
(skipping neurons with no coordinates), at :data: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no neuron in the store has coordinates. |
Source code in src/connectomekg/scene.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |