Skip to content

lanfactory.network_inspectors

The public inspection namespace loads a trained Torch LAN, compares its likelihood with simulation-based KDE estimates, and plots likelihood manifolds. The configuration dataclasses keep model metadata, evaluation grids, and plot defaults explicit.

lanfactory.network_inspectors.get_torch_mlp

get_torch_mlp(model_file_path: str | PathLike[str], network_config: str | PathLike[str] | dict[str, Any], input_dim: int) -> Callable[[NDArray[np.float32]], Any]

Return a predict_on_batch callable for the TORCH_MLP likelihood.

The returned function expects a 2d float32 array whose rows are a parameter vector trailed by a reaction time and a choice, and returns the per-row LAN log-likelihood.

lanfactory.network_inspectors.kde_vs_lan_likelihoods

kde_vs_lan_likelihoods(parameter_df: DataFrame, model: str, torch_mlp_predict: Callable[[NDArray[float32]], Any], n_samples: int = 10, n_reps: int = 10, grid: GridSpec | None = None, plot: PlotConfig | None = None) -> Figure

Compare kernel density estimates from simulation data with LAN output.

parameter_df: one model-compatible parameter vector per row. model: model name. torch_mlp_predict: predict_on_batch from get_torch_mlp. n_samples/n_reps: samples per KDE / KDEs per subplot. grid: optional GridSpec. plot: optional PlotConfig (title, panel titles, font sizes, file name and dpi of the saved figure).

Returns the Matplotlib figure. With plot.show (the default) it is displayed and closed; with show=False it stays open so the caller can add to it, and closing it (plt.close(fig)) is then the caller's job.

lanfactory.network_inspectors.lan_manifold

lan_manifold(parameter_df: DataFrame | ndarray | None = None, vary_dict: dict[str, Any] | None = None, model: str = 'ddm', torch_mlp_predict: Callable[[NDArray[float32]], Any] | None = None, grid: GridSpec | None = None, plot: PlotConfig | None = None) -> go.Figure

Plot LAN likelihoods as a 3D manifold while sweeping one parameter.

parameter_df: parameter vector (first row used). vary_dict: {param: values}. model: model name. torch_mlp_predict: predict_on_batch from get_torch_mlp. grid: optional GridSpec. plot: optional PlotConfig. Returns a Plotly Figure.

lanfactory.network_inspectors.ModelSpec dataclass

ModelSpec(name: str, params: list[str], choices: list[int], predictor: Callable[[NDArray[float32]], Any] | None = None)

Model metadata (name, params, choices) plus a supplied LAN predictor.

Methods:

  • from_model –

    Build a ModelSpec from an ssms model name and optional predictor.

Attributes:

lanfactory.network_inspectors.ModelSpec.n_choices property

n_choices: int

Number of possible choices.

lanfactory.network_inspectors.ModelSpec.n_params property

n_params: int

Number of model parameters.

lanfactory.network_inspectors.ModelSpec.from_model classmethod

from_model(model: str, predictor: Callable[[NDArray[float32]], Any] | None = None) -> ModelSpec

Build a ModelSpec from an ssms model name and optional predictor.

lanfactory.network_inspectors.PlotConfig dataclass

PlotConfig(font_scale: float = 1.5, figsize: tuple[float, float] = (10, 10), cols: int = 3, alpha: float = 0.1, save: bool = False, show: bool = True, save_dir: str = 'figures/', fig_scale: float = 1.0, title: str | None = 'auto', panel_titles: str | Sequence[str] = 'index', title_fontsize: float = 30, panel_title_fontsize: float = 20, xlabel_fontsize: float = 24, ylabel_fontsize: float = 20, legend_fontsize: float = 12, layout: str | None = None, filename: str = 'kde_vs_mlp_plot.png', dpi: float | None = None)

Plotting defaults shared by the entry points.

save, show and save_dir apply to both plots and fig_scale only to the manifold. Every other field styles the KDE-vs-LAN figure.

Parameters:

  • font_scale (float, default: 1.5 ) –

    Seaborn font scale; sets the tick-label size.

  • figsize (tuple[float, float], default: (10, 10) ) –

    Figure size in inches.

  • cols (int, default: 3 ) –

    Number of panel columns.

  • alpha (float, default: 0.1 ) –

    Opacity of the KDE lines.

  • save (bool, default: False ) –

    Write the figure to save_dir.

  • show (bool, default: True ) –

    Display the figure.

  • save_dir (str, default: 'figures/' ) –

    Directory for saved figures; created if missing.

  • fig_scale (float, default: 1.0 ) –

    Size multiplier for the manifold figure.

  • title (str | None, default: 'auto' ) –

    Figure title. "auto" gives "Likelihoods KDE vs. LAN: ", None draws no title, any other string is used as given.

  • panel_titles (str | Sequence[str], default: 'index' ) –

    "index" titles each panel with the row number of its parameter vector, "params" with that vector's values (v=1, a=1.5, ...). A sequence of strings gives one title per parameter vector.

  • title_fontsize (float, default: 30 ) –

    Font size of the figure title, in points.

  • panel_title_fontsize (float, default: 20 ) –

    Font size of the panel titles.

  • xlabel_fontsize (float, default: 24 ) –

    Font size of the x-axis label.

  • ylabel_fontsize (float, default: 20 ) –

    Font size of the y-axis label.

  • legend_fontsize (float, default: 12 ) –

    Font size of the legend.

  • layout (str | None, default: None ) –

    Matplotlib layout engine, "constrained" or "tight", which sizes the margins to the titles and labels. None keeps the fixed margins, which suit the default figure size and clip smaller figures. No engine can fit a title that is wider than the figure; lower its font size.

  • filename (str, default: 'kde_vs_mlp_plot.png' ) –

    File name inside save_dir; its extension picks the format.

  • dpi (float | None, default: None ) –

    Resolution of the saved file; None uses Matplotlib's savefig.dpi.

lanfactory.network_inspectors.GridSpec dataclass

GridSpec(n_rt_steps: int = 200, max_rt: float = 5.0, n_points_2c: int = 2000, rt_step_2c: float = 0.0025, n_points_mc: int = 1000, rt_step_mc: float = 0.01)

Reaction-time grid resolution for the inspection plots.

Styling the KDE-vs-LAN figure

kde_vs_lan_likelihoods returns the Matplotlib figure, and PlotConfig sets its titles, font sizes, layout and output file. The defaults draw the same figure as before. For a compact figure that goes into a paper:

from lanfactory.network_inspectors import PlotConfig, kde_vs_lan_likelihoods

fig = kde_vs_lan_likelihoods(
    parameter_df=parameter_df,  # two rows that differ in v
    model="angle",
    torch_mlp_predict=predictor,
    plot=PlotConfig(
        cols=2,
        figsize=(10, 4),
        layout="constrained",  # fit titles and labels into this size
        title=None,  # the caption names the figure
        panel_titles=["v = 0", "v = 1.5"],  # or "params" for each row's values
        panel_title_fontsize=14,
        xlabel_fontsize=14,
        ylabel_fontsize=14,
        font_scale=1.0,  # tick labels
        show=False,  # return the figure open
        save=True,
        save_dir="figures",
        filename="network_check.png",  # a .pdf name saves a PDF
        dpi=300,
    ),
)

With show=False the figure stays open, so Matplotlib's own tools still apply to it (fig.axes, fig.savefig); close it with plt.close(fig) when you are done. With show=True, the default, it is displayed and closed, and still returned. In a notebook, assign the result (fig = ...) or the figure appears a second time as the cell's value.

The function styles its own figure only: Matplotlib's global style is the same after the call as before it.