Skip to content

Make the KDE-vs-LAN plot configurable and return its figure - #175

Merged
AlexanderFengler merged 4 commits into
mainfrom
feat/kde-vs-lan-plot-options
Nov 24, 2026
Merged

AlexanderFengler merged 4 commits into
mainfrom
feat/kde-vs-lan-plot-options

Conversation

@AlexanderFengler

@AlexanderFengler AlexanderFengler commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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 returned None, and it changed the global seaborn style.

Defaults are backward compatible: the default figure is pixel-identical to main for a two-choice and a three-choice model.

Changes

New PlotConfig fields, appended after the existing ones:

Field Default Effect
title "auto" "auto" keeps "Likelihoods KDE vs. LAN: <MODEL>"; None draws no title; any other string is used as given
panel_titles "index" "params" titles each panel with its row's values (v=1, a=1.5, ...); a sequence gives one title per parameter vector
title_fontsize, panel_title_fontsize, xlabel_fontsize, ylabel_fontsize, legend_fontsize 30, 20, 24, 20, 12 the previously hard-coded sizes
layout None Matplotlib layout engine ("constrained" / "tight"); None keeps the fixed margins
filename, dpi "kde_vs_mlp_plot.png", None name and resolution of the saved file
  • kde_vs_lan_likelihoods and plot_kde_vs_lan return the Matplotlib figure. It is closed only when show displayed it; with show=False it stays open and closing it is the caller's job.
  • The plot is drawn inside sns.axes_style / sns.plotting_context instead of sns.set(), so rcParams and 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.
  • A wrong number of panel_titles is rejected before any simulation runs; an unknown panel_titles mode is rejected when the PlotConfig is built.
  • Docs: docs/api/network_inspectors.md gets a "Styling the KDE-vs-LAN figure" section; the PlotConfig docstring documents every field.

Behaviour changes to be aware of

  • In a notebook, a cell that ends with the bare call now shows the figure twice (displayed by show=True, then again as the cell's value). Assign the result or pass show=False. The tutorial notebook cell is updated accordingly.
  • With show=False the figure is no longer closed.
  • The file extension of filename picks 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 check on the changed files and uv run ruff format --check .: clean.
  • mypy with the pre-commit arguments: clean.
  • uv run mkdocs build --strict: passes.
  • Default figure compared pixel by pixel against main; returned figure compared against the saved file, also under a different caller style and with layout="constrained".

Lint fix carried along (second commit)

main currently fails the lint check: ruff 0.17.6, which CI installs, reports the # noqa: B008 on the OnsetGrid() default of derive.choice_mass as unused, while the pre-commit pin (0.17.0) raises B008 without it. The default is now a module-level constant, which passes ruff check src/lanfactory on both versions. Behaviour is unchanged; tests/derive passes (65 tests).

Not in this PR

  • Legend labels ("KDE", "MLP") and line colours stay hard-coded.
  • The manifold plot is unchanged.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Customize plot titles, panel titles, font sizes, layout, output filename, and resolution.
    • Receive the plot as a figure for further use. Choose whether it is displayed and closed or left open.
    • Panel titles can show parameter values; mismatched title counts are rejected.
    • Plot styling no longer changes Matplotlib’s global style.
  • Documentation
    • Added guidance and examples for configuring plots and displaying figures in notebooks.

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>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 13:12
@review-notebook-app

Copy link
Copy Markdown

Review Notebooks

Notebooks changed:

Review visual diffs and comment on any notebook cell. All comments sync back to this PR.


Powered by ReviewNB

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

kde_vs_lan_likelihoods now returns a configurable Matplotlib figure. PlotConfig controls titles, fonts, layout, and save options. The API validates panel titles and includes parameter vectors in plotting results. choice_mass now uses a module-level default grid.

Changes: Network Inspector Figures

Layer / File(s) Summary
Plot configuration and API contract
src/lanfactory/network_inspectors/config.py, src/lanfactory/network_inspectors/api.py, src/lanfactory/network_inspectors/plotting.py, tests/test_network_inspectors_api.py
PlotConfig adds title, panel-title, font-size, layout, filename, and DPI options. The API checks panel-title counts, includes parameter vectors in results, and returns a Figure. API tests cover validation and parameter-derived panel titles.
Figure rendering and lifecycle
src/lanfactory/network_inspectors/plotting.py, tests/test_network_inspectors_plotting.py, docs/api/network_inspectors.md, notebooks/network_inspectors_tutorial.ipynb
The plotter applies configured titles, fonts, layout, and save settings. It uses a scoped Seaborn style context. When show=True, it displays and closes the figure; otherwise, it returns the open figure. Tests, documentation, and the tutorial cover the figure behavior and configuration.

Choice Mass Default Grid

Layer / File(s) Summary
Default grid argument
src/lanfactory/derive/integrate.py
choice_mass uses a module-level OnsetGrid() instance as its default grid argument. Its documentation states that default.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant kde_vs_lan_likelihoods
  participant plot_kde_vs_lan
  participant Matplotlib
  Caller->>kde_vs_lan_likelihoods: request likelihood plot
  kde_vs_lan_likelihoods->>plot_kde_vs_lan: pass grid and likelihood results
  plot_kde_vs_lan->>Matplotlib: create, configure, and save figure
  alt show is true
    plot_kde_vs_lan->>Matplotlib: display and close figure
  else show is false
    plot_kde_vs_lan-->>Caller: return open figure
  end
Loading

Possibly related PRs

  • lnccbrown/LANfactory#157: Added the network inspectors package, including the API, configuration, and plotting modules extended by this change.

Suggested reviewers: cpaniaguam

Merge Risk: Minimal · up to 2a43d

This change makes the KDE-vs-LAN plot configurable and returns its figure. No concrete merge-blocking risk was identified, and the new tests cover the main behaviors.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the KDE-vs-LAN plot becomes configurable through PlotConfig, and its figure is returned to the caller. The changeset adds PlotConfig options, returns Figure from kde_v…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the KDE-vs-LAN plot becomes configurable through PlotConfig, and its figure is returned to the caller. The changeset adds PlotConfig options, returns Figure from kde_vs_lan_likelihoods and plot_kde_vs_lan, and updates the related tests and docs, so the title matches the primary change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b38cf49a-1908-452b-bc6e-2b2dc706e6c2
Commits

Reviewing files that changed from the base of the PR and between 0a02b98 and 2a43da0.

Files selected for processing: 4

  • src/lanfactory/derive/integrate.py
  • src/lanfactory/network_inspectors/config.py
  • src/lanfactory/network_inspectors/plotting.py
  • tests/test_network_inspectors_plotting.py
Files skipped from review as they are similar to previous changes: 1

  • src/lanfactory/network_inspectors/config.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 13:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The unscoped sns.despine call modifies every open Matplotlib figure, so subsequent plotting calls can silently alter figures callers have already customized.

1 open issue

Review effort: Balanced

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>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 13:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the plotting configurability and figure-lifecycle updates.

1 open issue

Review effort: Balanced

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.59036% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/lanfactory/network_inspectors/plotting.py 96.87% 1 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
src/lanfactory/derive/integrate.py 98.94% <100.00%> (+<0.01%) ⬆️
src/lanfactory/network_inspectors/api.py 63.63% <100.00%> (+1.13%) ⬆️
src/lanfactory/network_inspectors/config.py 100.00% <100.00%> (ø)
src/lanfactory/network_inspectors/plotting.py 94.84% <96.87%> (+64.46%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 13:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@AlexanderFengler
AlexanderFengler merged commit 7e19c2e into main Nov 24, 2026
12 checks passed
@AlexanderFengler AlexanderFengler mentioned this pull request Nov 27, 2026
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.

2 participants