Skip to content

fix: coerce figure_of_metric return to Python float for Drawer + JAX - #1283

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/drawer-jax-fom-coerce
May 20, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/drawer-jax-fom-coerce

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Fix a TypeError: Object of type ArrayImpl is not JSON serializable raised when af.Drawer runs against a JAX-backed Fitness (reported running autogalaxy_workspace/scripts/ellipse.py-style code with use_jax=True).

Root cause is in AbstractInitializer.figure_of_metric (autofit/non_linear/initializer.py): it returned the raw value from fitness(parameters=...), which under JAX is a 0-d jax.Array. Those scalars propagated into figures_of_merit_list → Drawer._fit's search_internal["log_posterior_list"] → Samples.samples_info → DirectoryPaths.save_json → json.dump → crash.

Drawer is the only search that stuffs the full search_internal dict (with raw parameter_lists and log_posterior_list) into samples_info; DynestyStatic / Emcee / etc. build a scalar-metadata-only samples_info, so they don't hit this. The existing float(figure_of_merit) coercion in Fitness.call_wrap (fitness.py:243) covers the complementary path but only fires when use_jax_jit=True, and Drawer._fit constructs Fitness with the default use_jax_jit=False — so the existing safeguard never fires on this codepath.

Coerce at the producer (figure_of_metric). The function already returned Optional[float]; this just makes the runtime value match the annotation.

API Changes

None — internal bugfix only.
See full details below.

Test Plan

  • New regression test test__figure_of_metric__coerces_non_float_scalar_to_python_float in test_autofit/non_linear/test_initializer.py. Uses a numpy 0-d array fitness (library unit tests stay numpy-only) — the same JSON-unserializable shape as a JAX ArrayImpl. Asserts every entry of figures_of_merit_list is exactly float and that the list survives a bare json.dumps.
  • Full PyAutoFit unit suite — 1259 passed, 1 skipped.
Full API Changes (for automation & release notes)

Removed

  • (none)

Added

  • (none)

Renamed

  • (none)

Changed Signature

  • (none)

Changed Behaviour

  • autofit.non_linear.initializer.AbstractInitializer.figure_of_metric — return value is now coerced via float(...). Previously returned the raw fitness(parameters=...) output, which under a JAX-backed Fitness is a 0-d jax.Array. The function's annotation has always been Optional[float]; this makes the runtime value match.

Migration

None required — pure bugfix.

🤖 Generated with Claude Code

AbstractInitializer.figure_of_metric returned the raw fitness output,
which under a JAX-backed Fitness is a 0-d jax.Array. Drawer is the only
search that stuffs the full search_internal dict (with log_posterior_list)
into samples_info, so those scalars reached json.dump in
DirectoryPaths.save_json and crashed with
"Object of type ArrayImpl is not JSON serializable".

The existing float() coercion in Fitness.call_wrap only fires when
use_jax_jit=True, and Drawer constructs Fitness with the default
use_jax_jit=False — so it never covered this codepath. Coerce at the
producer; the function's annotation was already Optional[float].

Adds a regression test using a numpy 0-d fitness (library unit tests
stay numpy-only) that fails pre-fix on both the type assertion and a
bare json.dumps round-trip.
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 20, 2026
@Jammy2211
Jammy2211 merged commit a89fa59 into main May 20, 2026
7 checks passed
@Jammy2211
Jammy2211 deleted the feature/drawer-jax-fom-coerce branch May 20, 2026 10:05
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 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.

1 participant