Skip to content

refactor(import): defer scipy.special out of import autofit - #1566

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/defer-import-scipy-special
Sep 7, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/defer-import-scipy-special

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Refs #1565 (census option O2 of the ci-timing-fast-tests epic; the PyAutoLens half, O3, is PyAutoLens feature/defer-import-pyplot).

Summary

TransformedMessage.__init__ computed the physical-space support eagerly, and for phi_transform the inverse is scipy.special.ndtri — so the five module-level literals in autofit/messages/normal.py (UniformNormalMessage and friends) pulled scipy.special into every import autofit: 25 nodes, ~0.18 s cumulative, for a value almost no process reads. The multiplier is 494 script processes per board round plus every unit-test and should_simulate subprocess.

_support is now a functools.cached_property of the same name: computed on first access and cached in __dict__ under the key the eager code wrote, so default pickling is unchanged and old pickles carrying a _support state entry restore into the cache slot.

Measured

before after
-X importtime scipy.special under import autofit 177–180 ms cumulative absent
import autofit wall (best of 3, fresh processes) 0.436 s 0.227 s
af.UniformPrior(0.0, 1.0).identifier 6e85def0116bfefb6478e9f707324a7f identical
af.LogUniformPrior(1e-3, 1.0).identifier 84cb3563e638e34128f31008e556880d identical
af.Model(autofit.example.Gaussian) / a Collection identical identical

Supports of all five literals unchanged; pickle round-trips equal for un-accessed and accessed instances and for the two priors.

API Changes

None. Every public name in autofit.messages.normal and autofit.messages imports exactly as before; _support is still read as an attribute.

Validation

  • python3 -m pytest -q -x: 2454 passed, 2 skipped.
  • Ten new tests in test_autofit/messages/test_lazy_transformed_support.py: the clean import in a fresh process (scipy.special and scipy.stats absent), the unchanged supports, the pickle round trip, the identifiers against the literals above.
  • Reader audit: composed_transform.py itself (the while isinstance(base_message, TransformedMessage) unwrapping guarantees the base is a non-transformed message with a class-level _support), beta.py:360, two test sites; _support_kwargs / _parameter_support / check_support untouched; the identifier hasher skips underscore keys.

Guard

Import deferral only — no likelihood, sampler or numerical change; kaplinghat.py untouched (belongs with O4).

pending-release: this lands in the next PyAutoFit release; the import_s column of PyAutoHeart timings/unit/PyAutoFit.jsonl reads it back after merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg


Generated by Claude Code

…azy TransformedMessage support

`TransformedMessage.__init__` computed the physical-space support eagerly,
and for `phi_transform` the inverse is `scipy.special.ndtri` — so the five
module-level literals in `autofit/messages/normal.py` (`UniformNormalMessage`
and friends) pulled `scipy.special` into every `import autofit`: 25 nodes,
~0.18 s cumulative, for a value almost no process reads (census O2 of the
ci-timing-fast-tests epic, PyAutoHeart timings/unit_import_census_2026-09.md).

`_support` is now a `functools.cached_property` of the same name: computed on
first access, cached in `__dict__` under the key the eager code wrote, so
default pickling and old pickles carrying a `_support` state entry are
unchanged. Every reader audited (composed_transform itself, beta.py, two
tests); `_support_kwargs` / `_parameter_support` / `check_support` untouched;
the identifier hasher never saw the key.

Measured: `-X importtime` scipy.special 177-180 ms → absent; `import autofit`
0.436 → 0.227 s (best of 3, fresh processes). Identifiers byte-identical
before/after for UniformPrior, LogUniformPrior, Model(Gaussian) and a
Collection. Suite 2454 passed, 2 skipped; ten new tests pin the clean import,
the unchanged supports, the pickle round trip and the identifiers.

Refs #1565

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 7, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit 088013c into main Sep 7, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/defer-import-scipy-special branch September 7, 2026 02:22
@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.

1 participant