# 0005 — Heatmap rules Date: 2026-11-29. Status: accepted. Revisit when the 3D brain figure gets its colour scale, when node ordering by the data (clustering) is wanted, or when a measure gains a value range. ## Question `connectivity_grid` drew every result the same way whatever it was: `Blues` from 0 to the data maximum, or `RdBu_r` around 0 as soon as one drawn value was negative; a title of the method name and the slice; the diagonal and undefined cells both in the grey axis background; no numbers, no significance, no node groups. The tutorials worked around it with their own titles and suptitles, colour limits shared by hand, a significance mask drawn as a 0 to 1 matrix, and one heatmap drawn with `imshow` "as Connectivity.plot". Two figures of the same measure had different scales, because the range came from the data, and a title repeated what the colour bar should say. Prototypes at 30 and 120 nodes showed that full tick labels and cell numbers do not fit there. What should the heatmap show by default, and from what? ## Decision The rules live in `dirconn.viz.matrices` (`connectivity_grid`, `connectivity_grids`, `truth_comparison`) with the helpers `_scale.py` (colour), `_cells.py` (numbers and marks) and `_groups.py` (groups). **Colour map by value type.** A measure whose theoretical range (`MethodInfo.value_range`) has a negative lower bound is drawn in `RdBu_r`, centred on 0 with symmetric limits; any other measure in `Reds` from 0. A result without a known range (a truth, a hand-made result, a derived result, see below) is signed when a drawn value is below zero by more than 1e-9 times the largest magnitude. `cmap=` replaces the map. **Range from the theory when it is finite.** DTF, PDC, gPDC, ADTF, TPDC and WDTF are drawn 0 to 1, imcoh and PCMCI -1 to 1. An unbounded measure (GC, MTGC, rPDC, SSGC, TE, PSI, TRGC) takes 0, or minus the largest magnitude, to the data maximum. `vmin` and `vmax` override; `vmax="p95"` takes the 95th percentile of the drawn values (of their magnitudes on a diverging scale). The colour bar has an arrow on each side where drawn values fall outside the limits. On a diverging scale `vmax` is the half range and `vmin`, if given, must equal minus it. **Provenance allow-list.** The theoretical range is used only when every step of `provenance.history` after the fit keeps it: `reduce_freq` and `reduce_time` with `how` in mean, median, max or min, `abs`, `prepare`, `exclude_freqs` and `shared_order_selection`. `abs` maps a signed range to `(0, max(|lo|, |hi|))`, so `|imcoh|` is 0 to 1 in `Reds`. Any other step (a net direction, a peak frequency or time, a baseline normalisation, a time-reversal asymmetry) removes the range; sign and limits then come from the data, and the colour bar names the step: `"DTF (net direction), 8-12 Hz"`. **No title by default.** The colour bar names the measure (`gPDC`, `rPDC`), the derivation and the slice (`"PDC, 13-20 Hz"`). A result already reduced over a band, drawn at its one frequency, is named by that band, and one reduced over a time span, drawn at its one time point, by that span (`"ADTF, 8-30 Hz, 0-32.5 s"`). An `abs` of values that could be negative is named `magnitude`: a signed theoretical range (`"imcoh (magnitude), alpha band"`) or a range from the data (`"DTF (net direction, magnitude), 8-12 Hz"`, a measure without a known range); an `abs` of a known non-negative range adds nothing and is not named. `network_graph` uses the same notes and display names. `title=` and `label=` are there when asked for. In `connectivity_grids` panels that would get the same default title also name the slice note that sets them apart (`"DTF, 21.5 Hz"`, `"DTF, beta band"`). **Diagonal and undefined cells.** On a result whose sources and targets are the same nodes the diagonal is light grey and outside the colour scale, also where the estimator defines it (the DTF diagonal of tutorial 17 is 0.34 to 1.00 against an off-diagonal maximum of 0.66). Other undefined cells are hatched, with a legend entry. **Numbers, marks and tick labels by node count only.** | nodes | numbers | significance mark (`"auto"`) | tick labels | |---|---|---|---| | up to 6 | yes, 2 significant digits, `<0.001`, minus sign U+2212 | `*` at the number's upper right | all; x rotated 90 degrees when a label is longer than 4 characters | | 7 to 20 | no | `*` in the cell centre | all, x rotated 90 degrees | | 21 to 60 | no | small dot | all, `max(4, 9 - n/10)` pt | | above 60 | no | none (`"dot"` and `"fade"` on request) | the group names with `groups=`, else none | Marks are white on dark cells and near-black on light ones. `significance=` takes a mask or a test result; a test result gives its FDR-BH mask at its own `alpha`, the family is the whole tested result, the slice is taken from that mask and never corrected again, and the legend names the correction. The legend stays when no cell is significant: it says a test was applied. **Groups come from the user.** `groups=` (node to group) reorders rows and columns so each group is contiguous, with boundary lines and colour strips of a fixed width in points; Yeo-7 colours when every group is a Yeo-7 network, else Okabe-Ito. **One shared colour bar.** `connectivity_grids` draws several results on one bar only when their theoretical ranges are identical and finite (DTF and PDC), or when they are the same measure with the same derivation and the same range. An unbounded range is no fixed scale, so GC and rPDC (both 0 to inf) do not share; any other combination raises a `ValueError`. **Truth.** No truth layer in the heatmap. `truth_comparison` draws the truth in two colours (white: no connection, dark grey: a true connection) with a two-entry legend and no colour bar, and the estimate under the rules above; neither panel has a title. Several tutorials say in the text which true connections are among the largest values. **Legends.** Marks and hatched cells: under the matrix on the caller's axes (aligned right in a figure with a layout engine), at the bottom of a figure the function makes. The test detail goes on a second line, so a constrained layout does not shrink the matrix to make room. Groups: right of the colour bar. ## Consequences - Every heatmap looks different from 0.1.0: `Reds` instead of `Blues`, and a weak DTF is pale because its scale ends at 1, not at its own maximum. Code that read the default title gets none. The tutorials were changed in the same branch. - GC and TE cannot share a bar, even though both are in nats: GC is 0 to inf, TE -inf to inf, and neither range is finite. They are drawn separately (tutorial 12). - TE, PSI and TRGC are drawn on `RdBu_r` even when every value is positive; only the red half is used, and a value near 0 stays near white. - A derived result (net direction, peak frequency, ...) has no fixed scale; two such figures can differ in range. - Numbers and marks follow the node count, not the rendered size, so the caller sizes the figure. With long region names the default `connectivity_grids` figure is cramped; `figsize=` gives it more room (tutorials 05 and 14). With `groups=` the figures the functions make are wider, for the strips and the group legend: 8 x 4.8 in for `connectivity_grid`, 0.8 in per column plus 1.0 in more for `connectivity_grids`; an explicit `figsize=` wins. - The 60-node boundary is inferred: only 30 and 120 nodes were drawn. ## Alternatives considered - **Ranges from the data for every measure.** The previous behaviour: the strongest cell is always the darkest, whether it is 0.3 or 0.9, and the same value changes colour between figures. Kept only where no finite range exists, with `vmax="p95"` for outliers. - **`**` and `***` levels.** Rejected: the smallest surrogate p value is 1/(n_surrogates + 1), so with 99 surrogates no p value is below 0.01, and the levels would show the number of surrogates more than the strength of the evidence. One `*` only. - **Numbers and fonts by rendered cell size.** Rejected: the same result would look different in a different figure, and the rule could not be tested without drawing. - **Hierarchical clustering of the nodes.** Not now: an order from the data changes between results and looks like a finding. Ordering comes from the user's `groups=`. ## Not done - `time_frequency` keeps its own scale (`Blues` from 0, or diverging on a negative value, data range). - The 3D brain figure (a prototype on another branch) does not yet use the same normalisation. - No `heatmap` alias of `connectivity_grid`, no interactive inspection.