Skip to content

plot: fix limits handling, unknown kwargs and a old docstring reference #1978

Closed

Description

@janfb

Follow-up to #1425, which PR #1631 largely delivered. These are the concrete items that survive, each small and independent:

  1. prepare_for_plot compares limits == [], which is wrong for a np.ndarray. Use a length check, and align the pairplot / marginal_plot type hints. Needs a regression test.
  2. Warn or raise on unrecognised keys in diag_kwargs, upper_kwargs and lower_kwargs. A typo is currently silent.
  3. sbi/analysis/plot.py:1336 points readers at _get_default_fig_kwargs(), and the deprecation message at plot.py:2862 tells them to use it. That function does not exist. Only _get_default_opts is defined, at plot.py:2860.

Activity

  1. added
    bug
    diagnostics
    on Aug 11, 2026
  2. BHARATH0153 commented on Aug 12, 2026

    @BHARATH0153
    Contributor

    all three items are real and reproducible on main. I worked through each one and have fixes ready (with a regression test for each)

    1. prepare_for_plot compares limits == []

    plot.py:884 currently does if limits is None or limits == []:. For a np.ndarray this is elementwise comparison, so passing a valid numpy array raises a cryptic error before any plotting happens:

    ValueError: operands could not be broadcast together with shapes (2,2) (0,)
    (a torch.Tensor is broken too — tensor == [] returns a tensor and the truth test fails). Fix is a length check, which works for list, np.ndarray and torch.Tensor alike:

    if limits is None or len(limits) == 0:
    limits = infer_limits(samples, dim, points)
    I also aligned the public type hints so this is actually advertised: pairplot and marginal_plot now declare limits: Optional[Union[List, torch.Tensor, np.ndarray]] (matching what prepare_for_plot already accepts). The existing [] → infer behavior is preserved.

    1. Silent unknown keys in diag_kwargs / upper_kwargs / lower_kwargs

    A typo such as diag_kwargs={"bw_metod": "scott"} was silently absorbed by the deep-merge update() in _prepare_kwargs and then ignored. I added _validate_plot_kwargs, called from _prepare_kwargs, which raises a ValueError naming the offending keys and listing the valid ones, e.g.:

    Unrecognised keyword(s) ['bw_metod'] in diag_kwargs for plot type 'hist'.
    Valid options are ['bin_heuristic', 'mpl_kwargs'].
    The valid set is derived from the *Options dataclasses (dataclasses.fields), i.e. it is the union of documented keys per plot family rather than the current plot type. That deliberately keeps cases like lower_kwargs=HistOffDiagOptions() with lower=None working (which the existing test_plotting_kwargs_validation treats as valid), while still catching typos — which are the actual silent-failure case in the issue.

    1. Stale _get_default_fig_kwargs references

    Both places that point readers at the non-existent _get_default_fig_kwargs() now reference FigOptions, which is the real source of truth for figure defaults (and what fig_kwargs accepts via _prepare_fig_kwargs):

    the _arrange_grid docstring for fig_kwargs (plot.py:1336),
    the deprecation message in _get_default_opts (plot.py:2862).

    New regression tests in tests/plot_test.py: prepare_for_plot with list / np.ndarray / torch.Tensor limits; empty-list inference; pairplot + marginal_plot with numpy limits (asserting the xlim is applied); and ValueError on unknown keys in all three kwargs arguments.

  3. janfb commented on Sep 2, 2026

    @janfb
    ContributorAuthor

    item 1 is now fixed via #2000. thanks @BHARATH0153 for proposing it here and @dhruvb2028 implementing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdiagnostics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions