nirspipe.qc.common.figure_io#

Shared helpers for getting a figure out of memory and onto disk.

Two families, because the package draws with two libraries. Plotly figures become standalone iframe-ready HTML; matplotlib figures arrive already encoded and are decoded to a PNG file. Both end in figures/ beside the report, which is what keeps a report page small: the page carries a URL per figure and the browser fetches the one being looked at.

Module Contents#

Functions#

plotly_script_tags

Plotly.js from the CDN, then the tree’s own copy for a page opened offline.

save_fragment_views_html

Several figures in one file, the one keyed by the URL fragment drawn. Returns the tallest height.

figure_namer

Names every figure of one run, or of one condition of it, with the label bound in.

fig_png_b64

A matplotlib figure as a base64 PNG, closed afterwards so pyplot lets go of it.

stack_png_b64

Base64 PNGs one above the other as one, the narrower ones padded white on the right.

save_png

Decode one base64 PNG into figures_dir/name and return the report’s URL for it.

pair_slug

"" while a group holds one pairing, else _<sub1>x<sub2>.

get_channel_pairs

Return unique channel pair names (without chroma suffix) in MNE channel order.

extract_markers

Return annotations other than BAD_/EDGE_ spans as marker dicts (onset, duration, description).

Data#

API#

nirspipe.qc.common.figure_io.PLOTLY_JS_VERSION = 'get_plotlyjs_version(...)'#
nirspipe.qc.common.figure_io.PLOTLY_CDN_URL = None#
nirspipe.qc.common.figure_io.PLOTLY_LOCAL_NAME = None#
nirspipe.qc.common.figure_io.plotly_script_tags(page: pathlib.Path) → str#

Plotly.js from the CDN, then the tree’s own copy for a page opened offline.

out/sub-01/figures/x.html -> the CDN tag, then ../../plotly-3.5.0.min.js should the CDN not have loaded; that copy is written at out/ on first use.

nirspipe.qc.common.figure_io.CENTER_FIGURE_CSS = '.plotly-graph-div{margin-left:auto;margin-right:auto;}'#
nirspipe.qc.common.figure_io.FILL_FRAME_JS = "<script>(function(){function fit(){var gd=document.querySelector('.plotly-graph-div');if(!gd||!gd.on..."#
nirspipe.qc.common.figure_io.TOPO_PICK_JS = "<script>(function(){function hook(){var gd=document.querySelector('.plotly-graph-div');if(!gd||!gd.o..."#
nirspipe.qc.common.figure_io.save_fragment_views_html(views: dict[str, object], path: pathlib.Path, missing_note: str = '') → int#

Several figures in one file, the one keyed by the URL fragment drawn. Returns the tallest height.

…_desc-trialimage_nirs.html#3.0 draws views["3.0"]; a fragment with no view of its own draws views[""] under missing_note, whose {label} names the fragment. The page changes the fragment rather than the file, so a switch redraws without a reload, and each redraw posts its height as iframe-resize.

nirspipe.qc.common.figure_io.figure_namer(label: str, condition: str | None = None, prefix: str = '')#

Names every figure of one run, or of one condition of it, with the label bound in.

fig_name = figure_namer("sub-01_task-rest")
fig_name("carpet")                  -> "sub-01_task-rest_desc-carpet_nirs.html"
fig_name("detail", channel="S1D1")  -> "sub-01_task-rest_chan-S1D1_desc-detail_nirs.html"
figure_namer("sub-01_task-rest", "game1")("carpet")
                                    -> "sub-01_task-rest_cond-game1_desc-carpet_nirs.html"
figure_namer("sub-01_task-rest", prefix="raw")("carpet")
                                    -> "sub-01_task-rest_desc-rawcarpet_nirs.html"

Binding the label here is what lets one figures/ directory hold every run of a subject, each name saying which run it is; binding the condition does the same for a condition’s panels.

prefix is how the raw viewer keeps its panels apart from the pipeline report’s in that shared folder: the two draw the same panels at different stages of one run, so both would otherwise ask for desc-carpet. Set in one place rather than spelled into each panel’s name, so the two writers call their panels the same thing.

nirspipe.qc.common.figure_io.fig_png_b64(fig, dpi: int = 300, **savefig_kw) → str#

A matplotlib figure as a base64 PNG, closed afterwards so pyplot lets go of it.

nirspipe.qc.common.figure_io.stack_png_b64(b64s: list[str]) → str#

Base64 PNGs one above the other as one, the narrower ones padded white on the right.

nirspipe.qc.common.figure_io.save_png(b64: str, figures_dir: pathlib.Path, name: str) → str | None#

Decode one base64 PNG into figures_dir/name and return the report’s URL for it.

save_png(b64, .../group-01/figures,
         "group-G1_task-rest_chromo-hbo_chan-S1D1_desc-wtcmap_nirs.png")
-> "figures/group-G1_task-rest_chromo-hbo_chan-S1D1_desc-wtcmap_nirs.png"

The two halves of writing a matplotlib figure out, in one call, because every caller does both and saving without taking the href back leaves a figure on disk that never reaches the page. An empty or absent b64 returns None, so a builder that declined to draw leaves the page’s {% if %} gate closed.

nirspipe.qc.common.figure_io.pair_slug(pair: tuple[str, str] | None, n_pairings: int) → str#

"" while a group holds one pairing, else _<sub1>x<sub2>.

Every inter-brain figure is of two members, so a group of three writes three of everything and each needs a name of its own. A dyad has exactly one pairing, where the slug would distinguish nothing and rename every file for no reason, so it is empty there.

pair_slug(("sub-a", "sub-b"), 1)  ->  ""
pair_slug(("sub-a", "sub-b"), 3)  ->  "_subaxsubb"
nirspipe.qc.common.figure_io.get_channel_pairs(raw: mne.io.Raw, exclude='bads') → list[str]#

Return unique channel pair names (without chroma suffix) in MNE channel order.

nirspipe.qc.common.figure_io.extract_markers(raw: mne.io.Raw) → list[dict]#

Return annotations other than BAD_/EDGE_ spans as marker dicts (onset, duration, description).

Unrounded: condition_windows builds window bounds from these, and a rounded bound no longer matches the annotation it came from.