Skip to content

fix: LogUniform/Uniform NumPy log-prior returns NaN out-of-support (emcee crash) #1328

Closed

Description

@Jammy2211

Overview

Under release-fidelity validation (PyAutoHeart #27, run 28784914443), Emcee legs crash with ValueError: Probability function returned NaN. Root cause: LogUniformPrior's NumPy log-prior path returns -np.log(value) with no bounds-check, so when Emcee's stretch move proposes an out-of-support value (value <= 0) the log-prior is NaN, which propagates into the summed figure-of-merit and crashes Emcee. UniformPrior has the sibling gap (returns 0.0 unconditionally, not -inf, out-of-support). Both NumPy paths already have a correct JAX counterpart that returns -inf out-of-support — this fixes the NumPy paths to match.

Plan

  • Fix the producer. LogUniformPrior.log_prior_from_value (and UniformPrior for parity) NumPy path returns -inf outside [lower_limit, upper_limit], mirroring the already-correct JAX branch. Out-of-support log-prior is legitimately -inf (zero probability → Emcee rejects the move) — a correctness fix, not a masking guard.
  • Reproduce on clean main first, honouring the Pipeline name for meta data should retain own pipeline name if pipelines are added #27 clean-repro discipline; scope only reproducing scripts.
  • Do NOT add a defensive NaN-guard on the FoM in fitness.py — that would mask other producers; a NaN FoM from anything else should still fail loudly.
  • Split/park the non-reproducing autofit_workspace_test/database scripts to a follow-up (per PyAutoFit#1327 precedent).
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: bug/emcee-loguniform-nan

Implementation Steps

  1. Reproduce the NaN crash on clean main: a small Emcee run over a LogUniform-prior model where a walker exits support (or directly LogUniformPrior(...).log_prior_from_value(-1.0) → NaN).
  2. Fix autofit/mapper/prior/log_uniform.py:139-140: replace the unconditional -np.log(value) NumPy branch with a bounds-checked np.where-based form returning -inf outside [lower_limit, upper_limit], matching the JAX branch at lines 141-142; correct the docstring at lines 126-129.
  3. Fix autofit/mapper/prior/uniform.py:167 NumPy path: return -inf out-of-support instead of unconditional 0.0, matching its JAX branch (scalar-safe).
  4. Add tests in test_autofit/mapper/prior/ (+ Emcee integration in test_autofit/non_linear/search/mcmc/test_emcee.py): assert out-of-bounds LogUniformPrior/UniformPrior log-prior is -inf (not NaN/0.0), and a small Emcee run over a LogUniform model with out-of-support proposals completes without ValueError.
  5. Reproduce all 9 bundled scripts from clean output/; keep only reproducing ones in this PR; split non-reproducing database scripts to a parked follow-up.
  6. Validate: python -m pytest test_autofit/ green; rerun reproducing scripts from clean state.

Key Files

  • autofit/mapper/prior/log_uniform.py — NumPy log-prior missing bounds-check (primary defect, causes the NaN crash).
  • autofit/mapper/prior/uniform.py — sibling NumPy path returns 0.0 out-of-support (parity fix).
  • autofit/non_linear/fitness.py:239-248 — FoM = log_likelihood + Σ log_prior; NaN guard only covers likelihood, not the summed prior (context; deliberately not changed).
  • autofit/non_linear/search/mcmc/emcee/search.py:137-138 — fom_is_log_likelihood=False (context).

Context / Discipline

Original Prompt

Click to expand starting prompt

Scoped from PyAutoMind/bug/health_fixes/autofit_sampler_database.md to the reproducing Emcee bounded-prior NaN defect. Full prompt:

# Fix Autofit release sampler and database regressions

## Context

Release run `28784914443` failed two @autofit_workspace cookbooks and seven
@autofit_workspace_test scripts. The cookbook failures reproduce on current `main`:
Emcee proposes invalid LogUniform values and raises `ValueError: Probability function
returned NaN`. Database failures include empty aggregators, stale/mismatched scraped
search metadata, and changed likelihood assertions.

Primary library: @PyAutoFit.

## Scripts

- autofit_workspace/scripts/cookbooks/result.py
- autofit_workspace/scripts/cookbooks/samples.py
- autofit_workspace_test/scripts/database/directory/general.py
- autofit_workspace_test/scripts/database/directory/multi_analysis.py
- autofit_workspace_test/scripts/database/scrape/general.py
- autofit_workspace_test/scripts/database/scrape/grid_search.py
- autofit_workspace_test/scripts/database/scrape/multi_analysis.py
- autofit_workspace_test/scripts/database/scrape/sensitivity.py
- autofit_workspace_test/scripts/features/minimal_output.py

Scope note: this issue tracks only the reproducing Emcee bounded-prior NaN library defect. The database/minimal_output scripts are validated from clean output/; any that do not reproduce are split to a parked follow-up (stale-output false positives, per PyAutoFit#1327).

Reference: PyAutoHeart #27 (run 28784914443), health_fixes cluster.

Activity

  1. Jammy2211 commented on Jul 8, 2026

    @Jammy2211
    CollaboratorAuthor

    🔧 Library PR created — feature/emcee-loguniform-nan

    PR: #1329 (pending-release)

    Root cause (reproduced A/B on clean main): Emcee's stretch move proposes physical values that can go non-positive for a LogUniform-priored parameter. LogUniformPrior.log_prior_from_value computed -np.log(value) on the NumPy path → NaN for value <= 0, which propagated into the summed figure-of-merit (fom = log_likelihood + Σ log_prior, fitness.py) and crashed the search with ValueError: Probability function returned NaN.

    • OLD (clean main): log_prior_from_value(-1.0) → nan; real emcee run → CRASH (ValueError: Probability function returned NaN at log_uniform.py:140).
    • NEW: value <= 0 → -inf; real emcee run (same seed) → completes.

    Fix (minimal scope): NumPy path returns -inf for value <= 0 via the double-where pattern (no log of a non-positive value → no RuntimeWarning). Positive values are unchanged — the NumPy path stays unnormalised/unbounded (-log(value)), preserving its documented, tested contract. UniformPrior and the JAX path are untouched.

    Validation:

    • test_autofit/{mapper,non_linear,messages} → 918 passed, 13 skipped + 250 prior tests.
    • New regression: test__log_prior_from_value__non_positive_returns_neg_inf.
    • Direct workspace callers (autofit_workspace_test prior parity gates priors_xp_dispatch.py, emcee_gaussian_bias_check.py) exercise only positive/in-bounds values → both pass unchanged.

    Still open — separate follow-up: the 7 autofit_workspace_test/database + minimal_output.py scripts from this cluster remain unvalidated; they show the classic stale-output/ signature (empty aggregators, mismatched scraped metadata) and are parked for a clean-output/ reproduction pass, consistent with PyAutoFit#1327. This PR fixes only the reproducing Emcee-NaN library defect; the two autofit_workspace/cookbooks legs that failed on that crash should pass once released.

  2. Jammy2211 commented on Jul 8, 2026

    @Jammy2211
    CollaboratorAuthor

    ✅ Shipped — #1329 merged to main (0f26ff2d8). The LogUniform NumPy log_prior_from_value NaN-on-value<=0 crash is fixed. Remaining #27 autofit_sampler_database database/minimal_output scripts stay parked for a separate clean-output/ reproduction pass (stale-output suspects, per #1327).

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions