Repository navigation
refactor(import): defer scipy.special out of import autofit - #1566
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1565 (census option O2 of the
ci-timing-fast-testsepic; the PyAutoLens half, O3, is PyAutoLensfeature/defer-import-pyplot).Summary
TransformedMessage.__init__computed the physical-space support eagerly, and forphi_transformthe inverse isscipy.special.ndtri— so the five module-level literals inautofit/messages/normal.py(UniformNormalMessageand friends) pulledscipy.specialinto everyimport 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 andshould_simulatesubprocess._supportis now afunctools.cached_propertyof 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_supportstate entry restore into the cache slot.Measured
-X importtimescipy.special underimport autofitimport autofitwall (best of 3, fresh processes)af.UniformPrior(0.0, 1.0).identifier6e85def0116bfefb6478e9f707324a7faf.LogUniformPrior(1e-3, 1.0).identifier84cb3563e638e34128f31008e556880daf.Model(autofit.example.Gaussian)/ aCollectionSupports 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.normalandautofit.messagesimports exactly as before;_supportis still read as an attribute.Validation
python3 -m pytest -q -x: 2454 passed, 2 skipped.test_autofit/messages/test_lazy_transformed_support.py: the clean import in a fresh process (scipy.specialandscipy.statsabsent), the unchanged supports, the pickle round trip, the identifiers against the literals above.composed_transform.pyitself (thewhile 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_supportuntouched; the identifier hasher skips underscore keys.Guard
Import deferral only — no likelihood, sampler or numerical change;
kaplinghat.pyuntouched (belongs with O4).pending-release: this lands in the next PyAutoFit release; theimport_scolumn of PyAutoHearttimings/unit/PyAutoFit.jsonlreads it back after merge.🤖 Generated with Claude Code
https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg
Generated by Claude Code