Repository navigation
Make the KDE-vs-LAN plot configurable and return its figure - #175
Conversation
kde_vs_lan_likelihoods hard-coded its titles and font sizes, saved under a fixed name at the default dpi, closed the figure and returned None, so a caller who wanted the figure in a paper had to rename the file, wrap the call to set the dpi and size the figure around the fonts. - Return the Matplotlib figure. It is closed only when show displayed it; with show=False it stays open for the caller. - PlotConfig gains title (None for no title), panel_titles (a list of labels, or "params" for each row's parameter values), the five font sizes, layout (a Matplotlib layout engine, for figures smaller than the fixed margins allow), filename and dpi. The file extension now picks the saved format. - Style the figure in seaborn contexts instead of sns.set(), which restyled every later figure in the session and remapped Matplotlib's single-letter colour codes. The font and tick-label size are pinned on the figure, since Matplotlib reads both at draw time and the returned figure would otherwise render differently outside the context. Defaults are unchanged: the default figure is pixel-identical to the one drawn before this change, for two-choice and multi-choice models. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Notebooks changed:
Review visual diffs and comment on any notebook cell. All comments sync back to this PR. Powered by ReviewNB |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
There was a problem hiding this comment.
🟡 Changes recommended
The unscoped sns.despine call can alter previously returned or unrelated open figures; passing fig=fig is needed to confine styling to the new plot.
1 open issue
What changed in this PR
Makes the KDE-vs-LAN network check configurable for publication figures and returns its Matplotlib figure for further customization.
Changes:
- Adds configurable titles, font sizes, layout, output filename, and resolution.
- Returns figures, keeps them open with
show=False, and replaces global seaborn settings with scoped contexts. - Adds plotting and API tests, styling documentation, and notebook guidance.
| File | Description |
|---|---|
tests/test_network_inspectors_plotting.py |
Tests customization, saving, figure lifecycle, and style preservation. |
tests/test_network_inspectors_api.py |
Tests early title validation and parameter-based panel titles. |
src/lanfactory/network_inspectors/plotting.py |
Applies plot options, scopes styling, and returns the figure. |
src/lanfactory/network_inspectors/config.py |
Defines and documents new plotting options. |
src/lanfactory/network_inspectors/api.py |
Validates title counts, passes parameter values, and returns the figure. |
notebooks/network_inspectors_tutorial.ipynb |
Assigns the returned figure to avoid duplicate notebook output. |
docs/api/network_inspectors.md |
Documents styling options and figure ownership. |
Review effort: Balanced
ruff 0.17.6, which CI installs, reports the noqa on the OnsetGrid() default as unused, while the pre-commit pin (0.17.0) raises B008 without it. A module-level default passes on both. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The test drew a 6 in wide figure, which the 30 pt title only fits in Arial. On the Linux runners Matplotlib falls back to the wider DejaVu Sans, so the title left the figure and the test failed. Use a 10 x 4 in figure, and say in the PlotConfig docstring that no layout engine fits a title wider than the figure. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
sns.despine() without arguments already acts on the current figure only, which here is the one just created, so nothing changes in the output. Naming the figure makes that explicit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Purpose
Make the native KDE-vs-LAN network check (
network_inspectors.kde_vs_lan_likelihoods) usable as a paper figure without post-processing. Until now the titles, font sizes, file name and dpi were hard-coded, the function closed the figure and returnedNone, and it changed the global seaborn style.Defaults are backward compatible: the default figure is pixel-identical to
mainfor a two-choice and a three-choice model.Changes
New
PlotConfigfields, appended after the existing ones:title"auto""auto"keeps "Likelihoods KDE vs. LAN: <MODEL>";Nonedraws no title; any other string is used as givenpanel_titles"index""params"titles each panel with its row's values (v=1, a=1.5, ...); a sequence gives one title per parameter vectortitle_fontsize,panel_title_fontsize,xlabel_fontsize,ylabel_fontsize,legend_fontsizelayoutNone"constrained"/"tight");Nonekeeps the fixed marginsfilename,dpi"kde_vs_mlp_plot.png",Nonekde_vs_lan_likelihoodsandplot_kde_vs_lanreturn the Matplotlib figure. It is closed only whenshowdisplayed it; withshow=Falseit stays open and closing it is the caller's job.sns.axes_style/sns.plotting_contextinstead ofsns.set(), sorcParamsand the single-letter colour codes are unchanged after a call. The font and tick-label size are pinned on the figure, so saving the returned figure later gives the same pixels as the file the function saves.panel_titlesis rejected before any simulation runs; an unknownpanel_titlesmode is rejected when thePlotConfigis built.docs/api/network_inspectors.mdgets a "Styling the KDE-vs-LAN figure" section; thePlotConfigdocstring documents every field.Behaviour changes to be aware of
show=True, then again as the cell's value). Assign the result or passshow=False. The tutorial notebook cell is updated accordingly.show=Falsethe figure is no longer closed.filenamepicks the format (the default name is still a PNG).Verification
uv run pytest tests/test_network_inspectors_plotting.py tests/test_network_inspectors_api.py: 21 passed (15 new).uv run ruff checkon the changed files anduv run ruff format --check .: clean.uv run mkdocs build --strict: passes.main; returned figure compared against the saved file, also under a different caller style and withlayout="constrained".Lint fix carried along (second commit)
maincurrently fails the lint check: ruff 0.17.6, which CI installs, reports the# noqa: B008on theOnsetGrid()default ofderive.choice_massas unused, while the pre-commit pin (0.17.0) raises B008 without it. The default is now a module-level constant, which passesruff check src/lanfactoryon both versions. Behaviour is unchanged;tests/derivepasses (65 tests).Not in this PR
🤖 Generated with Claude Code
Summary by CodeRabbit