Skip to content

fix(samples): errors_at_sigma(as_instance=True) on a prior-valued model - #1578

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/samples-errors-at-sigma-instance
Sep 8, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/samples-errors-at-sigma-instance

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Samples.errors_at_sigma(1.0) and values_at_sigma(1.0) (default as_instance=True) raised TypeError: broadcast_arrays requires ndarray or scalar arguments, got <class 'tuple'> on any global model containing a Model(af.GaussianPrior) component — i.e. every FactorGraphModel.global_prior_model with a HierarchicalFactor. Model.instance_for_arguments handed the (lower, upper) bounds pairs to the Prior's constructor, which builds a NormalMessage from them.

Fix (autofit/mapper/prior_model/prior_model.py): when the model class is a Prior subclass and any constructor argument is a tuple, return a ModelInstance holding the arguments (.mean == (lo, hi), .sigma == (lo, hi)) instead of constructing the prior. Scalar vectors (median_pdf, max_log_likelihood) are unchanged and still build the real prior. An explicit typed branch, not an exception guard.

The alternative of returning a pair of instances was rejected: it would break the public tuple-attribute instance contract used by autofit_workspace_test/scripts/graphical/ep_parity.py, simultaneous.py and the workspace result tutorials.

Finding D6 of the analytic Gaussian benchmark (autofit_workspace_test#91); umbrella PyAutoFit#1405; closes #1577. Workspace follow-up: remove the as_instance=False workaround from autofit_workspace_test/scripts/graphical/analytic_autofit.py (ships after this PR merges).

API Changes

Bug fix only — no signatures changed. Samples.errors_at_sigma / values_at_sigma with as_instance=True now return an instance on models with Prior-class components where they previously raised; the Prior-class component becomes a ModelInstance of bounds pairs.
See full details below.

Test Plan

  • test_autofit/graphical/global/test_errors_at_sigma_instance.py: errors_at_sigma and values_at_sigma instances match the as_instance=False tuples through prior_tuples_ordered_by_id (both fail without the fix); median_pdf still yields a real af.GaussianPrior
  • pytest test_autofit -q: 2472 passed, 2 skipped (baseline 2469 + 3 new); graphical 278, messages 95
  • CI: Tests [pull_request] 3.12 / 3.13 / nojax, Docs
  • Workspace follow-up PR on autofit_workspace_test: analytic_gaussian.py info lines byte-identical through the instance path
Full API Changes (for automation & release notes)

Changed Behaviour

  • Model.instance_for_arguments — for a Prior-subclass model whose constructor arguments contain a tuple (a bounds pair from Samples.errors_at_sigma / values_at_sigma), returns ModelInstance(constructor_arguments) instead of constructing the prior (which raised). Scalar arguments unchanged.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01E2HKd5ZocDmTyZ544eVEh4

Model.instance_for_arguments constructed a Prior from (lower, upper)
bounds pairs whenever a global model contains Model(af.GaussianPrior) (a
HierarchicalFactor's distribution model), so errors_at_sigma and
values_at_sigma with as_instance=True raised TypeError from
NormalMessage's broadcast_arrays. A Prior's constructor only takes
scalars, so a tuple argument can only be such a bounds pair: store it on
a ModelInstance, as every other model class stores it, instead of
building a message from it. Scalar vectors (median_pdf,
max_log_likelihood) still construct the real prior.

Three tests in test_autofit/graphical/global/test_errors_at_sigma_instance.py;
the two tuple-valued ones fail without the fix.

Closes #1577 (graphical-ep phase 1, finding D6 of autofit_workspace_test#91).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2HKd5ZocDmTyZ544eVEh4
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autofit_workspace_test#99

Library-first: that workspace PR (docstring only, no code change) merges only after this one.

@Jammy2211
Jammy2211 merged commit 6331b80 into main Sep 8, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/samples-errors-at-sigma-instance branch September 8, 2026 00:05
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 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.

fix(samples): errors_at_sigma(as_instance=True) on a prior-valued model

1 participant