Skip to content

Network plots and animations: per-node colors, per-frame sizes, views, legend, parallel frames (#188) - #189

Merged
jeremymanning merged 47 commits into
mainfrom
epic/network-plots-188
Nov 22, 2026
Merged

jeremymanning merged 47 commits into
mainfrom
epic/network-plots-188

Conversation

@jeremymanning

@jeremymanning jeremymanning commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #188. Version 0.2.0 (not published to PyPI).

What changes

htfa.brainnet (plot_network, animate_network, write_animation) and the matching HTFAResults docstrings:

#188 item What the code does now
Per-node colors node_color takes one color or K colors
Per-frame sizes and colors animate_network takes (T, K) sizes and per-frame node_color; connectivity may be None or a static (K, K) matrix when sizes or colors carry the time axis
Several views per frame views= on both functions, with frame_size= for an exact output size
Legend legend= rows of dots and lines under the views
Custom layout compose= callable on animate_network receives the per-view images and the frame index
Parallel frames n_jobs on animate_network (private worker pool; blank renders and dead workers restart it, at most 5 times)
Encoding options crf and faststart on write_animation and animate_network
Crop helper content_box is public (htfa.content_box, htfa.brainnet.content_box)

Also in this branch:

  • On macOS, off-screen render windows are released through VTK's Cocoa autorelease pool (VTK >= 9.5; skipped on older VTK). Before, resident memory grew by about 9 MB per render and macOS eventually printed "Context leak detected"; this was the case on main as well.
  • tests/test_stats.py::test_fitted_count_and_scoring asserts the ConvergenceWarning its one-iteration fit raises, so the suite prints no warnings.
  • Tutorials 10 and 11 show the new arguments and write temporary files to a TemporaryDirectory.
  • Design and plan: docs/superpowers/specs/2026-11-21-network-plots-188-design.md, docs/superpowers/plans/2026-11-21-network-plots-188.md.

Behavior of existing calls

Calls that use none of the new arguments give the same pixels as on main for still images without edges and for annotate_pair / montage (11 reference outputs compared array-equal). Renders with translucent edges over the translucent mesh vary by a few dozen pixels from run to run inside VTK, on main and on this branch alike, so they cannot be compared exactly.

Measurements

  • 300 frames, two views, 1920x1080: 61 / 50 / 35 / 31 s with n_jobs = 1 / 2 / 3 / 4 (Apple Silicon). Workers take about 4 s to start, so a 30-frame movie gains nothing.
  • 60 renders in one process on macOS: 635 MB resident at the end, 991 MB before the autorelease pool.

Checks

At 42846c2, all four workflows this PR triggers are green (34 checks): tests on Ubuntu, macOS and Windows with Python 3.10 to 3.14 plus minimum versions; docs; the BrainIAK cross-check; and the release-candidate workflow (wheel install and every tutorial executed on the same matrix).

Locally (macOS, Python 3.12) at the last commit that changes code: make lint clean, make test 1216 passed, 19 skipped, 0 warnings, make docs 0 warnings.

Found through CI

  • ubuntu-latest is now Ubuntu 26.04 with Mesa 26, where an actor colored by cell data is not drawn; main fails the same way in an Ubuntu 26.04 container. Edge and node colors are now per-point colors (93e465f). macOS images are pixel-identical before and after.
  • Tutorial 10's new sections first took the Windows tutorial job past its 120-minute limit (25.5 minutes for that notebook, 7 before this branch). They now render about 64 frames instead of 400 (fd55f59); the workflow file is unchanged.
  • tests/test_stats.py::test_fitted_count_and_scoring allows numpy 2.2.6's RuntimeWarning in matmul on macOS with Python 3.10 (inputs and result are finite; the result matches einsum).

Known limits

  • The first frame of an animation with edges is not reproducible run to run (VTK depth peeling); not changed here.
  • The VTK < 9.5 path (no autorelease pool) was not run: the local VTK is 9.7.
  • The import-order fix for the optional plotting dependencies on macOS has no test in the suite.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G

jeremymanning and others added 30 commits November 21, 2026 23:47
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…pty input

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
… in _Scene

node_color accepts K colors (names or a (K, 3)/(K, 4) array) through the new
_node_colors helper; component lobes still take one color. Sphere creation
moves into _Scene._add_nodes, used by _build and the new set_nodes, which
replaces the spheres with new radii and colors. New style key supersample
(default _SUPERSAMPLE). Existing calls render the same pixels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…st crf with faststart

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ene; one color keeps the 0.1.0 add_mesh call

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ty may be None

(T, K) sizes are mapped to radii jointly by one _radii call and interpolated
like the edges; (T, K) colors use the nearest window. Time axes of
connectivity, sizes, colors and captions must agree. Calls without per-frame
sizes or colors keep the 0.1.0 frame loop (pixel-identical frames).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…network; annotate_pair shares _layout

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
… resampled-color and spin tests

T = 0 sizes, per-frame colors or connectivity raise ValueError naming the
input and its shape instead of failing in a NumPy reduction. Tests compare
scale_by_mass against sizes scaled by hand, check nearest-timepoint colors
at fractional times, and check that a spin with growing nodes keeps every
sphere inside the frame.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…e still crop; views scale to fit both ways; 12 px legend font floor

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
… frame_size still raises

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…s; crf/faststart forwarded from animate_network

n_jobs != 1 draws frames in a private loky pool (not joblib's shared
reusable executor): contiguous chunks per worker, each worker builds its own
_Views and writes NNNNN.png via a temporary name and os.replace into a
temporary folder owned by the call. A blank view (light background, over
5 percent near-black pixels) or a dead worker stops the pool with its
workers and redraws only the missing frames, up to 5 restarts; other errors
are raised at once. The parent feeds the PNGs through the same per-frame
tail as the sequential path. n_jobs=1 is unchanged (pixel-identical).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ame the received values

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…nup failures surface; crf/faststart documented

_is_blank now also requires a near-black corner pixel, so dark nodes or a
dark mesh on a light background are not taken for failed renders (spec
amended). A temporary frame folder that cannot be removed raises OSError
after a written movie, or is added as a note to an error already raised
(RuntimeWarning on Python 3.10). Tests: corner and interior cases, large
black interior nodes render with n_jobs=2, the restart path with spheres
that fill the view, real permission-denied cleanup, _jobs_pair checks n.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…g, API docs, README example

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…oins its exception chain on every Python version

The rendering exception always propagates unchanged (no warning, no
version check); the cleanup OSError becomes the __context__ at the end of
the chain its traceback shows. The test turns warnings into errors.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…pin views are uncropped and limits the 0.1.0 statement

plot_network's sizes parameter and Raises section now say that K values in
any shape, such as (1, K) or (K, 1), are accepted and that (T, K) sizes with
T > 1 raise. The 0.2.0 changelog says views are not cropped with spin and
that the 0.1.0 output holds for calls using none of the new options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…nd sizes, views, legends, frame_size, content_box, compose, n_jobs, crf and faststart

Tutorial 11 gains section 16 (node colors, several views, legends,
frame_size, content_box). Tutorial 10 gains sections 10 (per-window node
sizes and colors, two views, legend; new asset 10_isfc_nodes.gif, 1.07 MB)
and 11 (compose, n_jobs=1 vs 4, crf and faststart); the summary becomes
section 12. Both notebooks re-executed with htfa 0.2.0 from this checkout.
10_isfc_animation.gif was regenerated: 3 pixels of one frame differ from
the previous file (render nondeterminism of an unchanged call).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ows passed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ocoa autorelease pool; tutorial 11 output has no context-leak line

VTK's Cocoa render windows hand their OpenGL contexts to an autorelease
pool that a Python process without a Cocoa event loop never drains, so
every closed window stayed in memory (5-9 MB per render with the ch2
mesh) and macOS printed "Context leak detected" after about 40 renders.
_Views now holds a vtkCocoaAutoreleasePool and releases it after closing
its scenes. A subprocess test bounds the peak-memory growth over renders
61-100 and requires no context-leak line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…oraryDirectory removed after its last use

tempfile.mkdtemp() left a folder of frame sets and movies (tutorial 10)
or images, configs and animations (tutorial 11, since 0.1.0) behind on
every execution. Both notebooks re-executed; 10_isfc_animation.gif
differs from the committed file in 3 pixels of frame 1 (first-frame
render variation) and matches the GIF embedded in the notebook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
The strip's axis label and the printed middle-frame line used British
spelling; re-executed, output regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…animate_network still names (K,) or (T, K)

The shared size check printed "((K,) or (T, K))" for both functions.
_network_geometry, _display_nodes and _nodes take time_sizes (default
True); plot_network passes False, so its message drops the (T, K) hint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…he PNG round trip and when workers help

Measured on a 12-core Apple M-series Mac: a 300-frame two-view
1920 x 1080 movie took 61 s with n_jobs=1 and 50, 35 and 31 s with 2, 3
and 4 workers; a 30-frame movie took the same time with 1 and 3. No code
change: each worker builds its scenes once per chunk, one chunk per
worker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ings its one-iteration fit raises

The fit stops after one global iteration on purpose, so HTFA warns once;
with 2 Mexican-hat components an unpenalized stage-0 subject fit also
reaches TFA's max_iter=100 and warns. The test records the warnings and
requires exactly these, so the suite's output has no warnings and a
missing or new warning fails the test. The checked counts and scores are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…lds the scenes, loaded after pyvista, and skipped on VTK < 9.5

_autorelease (a context manager) is entered and left in plot_network,
_movie_views and so in each worker task, around building, drawing and
closing the scenes; _Views owns no pool. The pool class is looked up after
lazy_import("pyvista"), and is None off macOS or when VTK lacks
vtkCocoaAutoreleasePool (VTK < 9.5), which renders as 0.1.0 did. Tests: a
subprocess closes _Views in creation order and across threads and nests
plot_network in compose; the no-pool path renders the same image. The
memory test runs on macOS only and prints each 10-render window.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
jeremymanning and others added 17 commits November 22, 2026 04:35
…und need neither pyvista nor matplotlib

_rgb255 returns white as (255, 255, 255) without pyvista; content_box,
_layout and _legend_rows use it. A subprocess test calls the three image
helpers on PIL images and checks that pyvista, vtkmodules and matplotlib
were not imported.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
… color, so nodes hidden in frame 1 are not cropped away

_movie_crop renders every view in scenes of its own: no edges, each node at
its largest radius, opaque, black on a light background and white on a dark
one. Both paths compute the crop before drawing. Test: the reviewer's case
(a node black on black in frame 1, blue in frame 2) keeps the whole blue
node, with n_jobs 1 and 2.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…cts odd sizes

plot_network and GIF output accept odd W or H; animate_network raises
ValueError for an odd frame_size with an mp4, saying H.264 needs even
sizes. test_plot_network_frame_size now renders (961, 540), (960, 541) and
(641, 401) at exactly that size; test_animate_odd_frame_size covers the mp4
error and an odd-size GIF. The spec's Views section says so in one sentence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…orker tests run in their own TMPDIR and count pools by worker pids

_draw_parallel catches TerminatedWorkerError instead of every
BrokenProcessPool, so a task that cannot be unpickled fails at once.

Tests: the cases that inspect htfa-frames-* folders run as scenarios in a
subprocess whose TMPDIR (inherited by its workers) is a tmp_path folder.
Workers record their pids; the success, error, gives-up and blank-render
cases assert every one has exited, and the two give-up cases assert exactly
6 pools (one first, 5 restarts). The restart case logs each composed frame,
checks frames finished before the death are not redrawn and compares the
movie with a sequential one. compose is a closure capturing data while a
joblib workload runs in joblib's shared pool across a successful and a
failing call. The 300 s elapsed-time assertion is replaced by the pid check;
no case takes 30 s, so none is marked slow.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…r-only movies, crf in both paths, faded per-node colors

- test_animate_views_spin: each panel changes on its own and equals the
  single-view spin movie of its view.
- test_legend_marks_dots_and_lines: dot and line bounds, fill and spacing
  at fonts 30 and 15.
- test_animate_colors_without_connectivity: (T, K) colors alone, no edges.
- test_animate_network_forwards_encoding: crf with n_jobs 1 and 2; the
  x264 settings in each file name the crf.
- test_set_nodes_bad_input_leaves_scene_unchanged: exact actors, radii and
  colors; the image within 20 changed pixels (documented).
- test_fade_by_mass_lightens_per_node_colors: faded vs unfaded renders of
  nodes with known mass fractions, ratio 1 - (1 - a)^2 (front and back
  surfaces).
- test_fitted_count_and_scoring: the HTFA warning is required, TFA
  ConvergenceWarnings allowed (one per subject at most), others fail.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…e width or the views' summed width

No behavior change: without frame_size the fonts are set by the summed
width of the cropped views, also when a legend row then widens the frame.
The plot_network docstring, _layout's docstring and the spec's Legend
section say so (test_natural_frame_widens_for_legend covers it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…w pixels per frame

The layout movie (views, legend) now takes its crop from separate scenes,
so its own scenes start with frame 1 and the translucent renders that
follow differ by up to 27 pixels per frame from before; the crop and frame
size (532 x 324) are unchanged. Text outputs differ only in timings; no
stderr output.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…g-compose case records and checks every worker's pid

test_autorelease_without_pool_class imports vtkCocoaAutoreleasePool itself
and expects _autorelease_pool_class to return it only on macOS when VTK
provides it, else None. In _scenario_temp_folder_removed every compose call
of the failing movie writes its worker's pid first; none of them may exist
after the call.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
Mesa 26.0.8's llvmpipe (GitHub's ubuntu-26.04 runner image) draws nothing
for an actor colored by cell scalars, with GLX/Xvfb and EGL alike and with
VTK 9.7.1 and 9.7.2, so every edge (and per-node colored spheres) vanished
on Linux CI; main failed the same way. Every tube and sphere has points of
its own, so per-point colors give the same color per primitive; macOS
renders are array-equal to before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ectly on Windows

_alive used os.kill(pid, 0), which raises WinError 87 on Windows; it now
opens the process (OpenProcess) and checks whether it exited
(WaitForSingleObject), keeping os.kill(pid, 0) on POSIX. Workers appended
log lines to one shared file, and Windows append (seek to end, then
write) let them overwrite each other (2 of 3 records on 3.11/3.12, a
garbled line on 3.13); each record is now a file of its own.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…us matmul warning

On macOS arm64 with Python 3.10 (numpy 2.2.6, Accelerate) the float32
G @ G.T in the engine warns "invalid value encountered in matmul" on
finite inputs, with a finite result equal to einsum's; repeating the
product does not warn. The warning is allowed, not required; the test now
also asserts a finite rss.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…first CI round

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…ated worker leaves no empty record

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
…r view and section 11 one n_jobs=2 movie, cutting their renders from 400 to about 64

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyt2ULJKvi7Z1bbZU4G
@jeremymanning
jeremymanning merged commit daf8c0f into main Nov 22, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Network plots and animations: per-node colors, per-frame sizes, several views, legend, parallel rendering

1 participant