Repository navigation
Description
Overview
ExternalShear, MassSheet and ExternalPotential describe the tidal field of everything outside the modelled system, yet the only redshift-bearing container PyAutoGalaxy has is Galaxy, so they ride on one galaxy and model.info misrepresents them as a property of that galaxy (the shear_galaxy workaround in multi_galaxy/). This task adds ag.MassField(redshift, **mass_profiles): a standalone, MassProfile-only container (not a Galaxy subclass) with a zero-light interface so a plane (Galaxies) can hold it beside galaxies. It is phase 1 of the mass-field epic (PyAutoMind/draft/feature/autogalaxy/mass_field_epic.md); the PyAutoLens Tracer(fields=) integration is phase 2. Hard invariant: Galaxy's public behaviour, dict() output and the prior configs are unchanged — proven by an identifier pin.
Plan
- Extract the mass-sum implementation (
cls_list_from,has,deflections_yx_2d_from,convergence_2d_from,potential_2d_from) fromGalaxyinto a behaviour-preserving mixin that bothGalaxyandMassFielduse;galaxy.pychanges only by that extraction. - Add
MassField(af.ModelObject, <mixin>): validates the redshift likeGalaxy, rejects lists with the same message, and rejects any non-MassProfilecomponent with aGalaxyExceptionnaming the offending key. - Give it the zero-light interface
Galaxies/OperateImageGalaxiesneed (hasFalse for non-mass classes, empty image list, zero image, noOperateImageListinheritance), plus__repr__,__eq__/__hash__and adict()/from_dictround trip. - Export
ag.MassField; confirm the JAX pytree walker registers it when it sits in aGalaxieslist, and measure with the three-way probe. - Tests in
test_autogalaxy/galaxy/test_mass_field.py, including the identifier pin captured onmainas a regression against a hard-coded value. - Docs:
docs/api/galaxy.rstgainsMassField; a short "External fields" paragraph in the galaxy overview.
Detailed implementation plan
Work Classification
Library (PyAutoGalaxy only). Phase 2 (PyAutoLens Tracer(fields=)) is a separate prompt, PyAutoMind/draft/feature/autolens/mass_field_integration.md, blocked on this one merging.
Affected Repositories
- PyAutoGalaxy (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
PyAutoGalaxy (main @ 67f6ca6, PR #619 merged) |
main | clean |
Remote branches: main, dev_Q, claude/slowest-ci-tests-s81dza, archive/condemned/pyautogalaxy-docs-cite-prodigy — none touch galaxy/. No active.md task claims PyAutoGalaxy (worktree_check_conflict exit 0).
Suggested branch: feature/mass-field-class
Worktree root (local-dev): ~/Code/PyAutoLabs-wt/mass-field-class/ (created by /start_library; a web session works on its clone instead)
Implementation Steps
-
Capture the identifier pin on
mainfirst (before any edit). Run, onmain:import autofit as af, autogalaxy as ag model = af.Collection(galaxies=af.Collection(lens=af.Model(ag.Galaxy, redshift=0.5, mass=af.Model(ag.mp.Isothermal), shear=af.Model(ag.mp.ExternalShear)))) print(model.identifier)
Record the string; it becomes the hard-coded expected value in the regression test (step 8) with a comment naming the model. The same value must hold on the branch at every later step.
-
Mixin extraction —
autogalaxy/galaxy/mass_aggregate.py(new),class MassProfileAggregate. Move verbatim fromautogalaxy/galaxy/galaxy.py:cls_list_from(cls, cls_filtered=None)(aa.util.misc.cls_list_fromoverself.__dict__.values()),deflections_yx_2d_from(routed throughdeflections_memo.deflections_yx_2d_from(p, grid, xp); zeros(n, 2)when no mass profile),convergence_2d_fromandpotential_2d_from(both@aa.decorators.to_array, zeros(n,)when no mass profile),has(cls):OperateImageList.hasraisesNotImplementedError, so find whereGalaxyactually resolveshas(checkaf.ModelObject/ the MRO at implementation time) and give the mixin an explicithasthat returnsany(isinstance(v, cls) for v in self.__dict__.values())only ifGalaxydoes not already inherit a concrete one — otherwise leaveGalaxy's resolution untouched and giveMassFieldits own.
Galaxybecomesclass Galaxy(af.ModelObject, OperateImageList, MassProfileAggregate)(or the mixin ahead ofOperateImageListifhasmust come from it — decide by the MRO check, and keepGalaxy's existing resolution order for every other name). The module docstring ofgalaxy.pyis updated to point at the mixin. Nothing else ingalaxy.pychanges: constructor,dict(),profile_dict,__eq__,__hash__,__repr__,traced_grid_2d_from,mass_angular_within_circle_fromall stay in place (the latter two are not moved — they areGalaxy-only today and the prompt does not ask for them on a field).
-
autogalaxy/galaxy/mass_field.py(new) —class MassField(af.ModelObject, MassProfileAggregate):__init__(self, redshift, **kwargs):super().__init__();validate.validate_redshift(redshift=redshift); the same list-rejectionexc.GalaxyExceptionmessage asGalaxy; then for every(name, val): if notisinstance(val, MassProfile)raiseexc.GalaxyException(f"MassField received '{name}' = {type(val).__name__}, which is not a MassProfile. A MassField holds only the mass components describing the tidal field of everything outside the modelled system (ExternalShear, MassSheet, ExternalPotential, or any other MassProfile); put light profiles, pixelizations and regularizations on a Galaxy.").MassProfileisautogalaxy.profiles.mass.abstract.abstract.MassProfile. Docstring names the three intended profiles and says anyMassProfileis accepted.- Zero-light interface (what
Galaxies,OperateImageListlist-methods andOperateImageGalaxiescall on a member — list each in the PR body):has(cls)→ False unless some component is an instance ofcls(so True forMassProfileand its subclasses, False forLightProfile,LightProfileLinear,LightProfileOperated,aa.Pixelization,Basis);image_2d_list_from(grid, xp=np, operated_only=None)→[];image_2d_from(grid, xp=np, operated_only=None)→xp.zeros((grid.shape[0],))wrapped with@aa.decorators.to_arraysoGalaxies.image_2d_fromsums anArray2Dlike it would for a light-lessGalaxy;image_2d_unbinned_from(grid, xp=np, operated_only=None)→xp.zeros((grid.over_sampled.shape[0],));image_2d_list_unbinned_from→[]. Do not inheritOperateImageList—blurred_image_2d_frometc. flow throughGalaxies, which callsimage_2d_fromon members; verify with theGalaxies([galaxy, field])tests below rather than adding methods speculatively.extract_attribute(cls, attr_name)— copyGalaxy's (it is used by plotters overGalaxies) or move it into the mixin if it is purecls_list_fromlogic; decide at implementation, keepGalaxybehaviour identical. __repr__→MassField(redshift=0.5, shear, mass_sheet)(component names in insertion order);__eq__viaself.dict() == other.dict()and__hash__→int(self.id), mirroringGalaxy.profile_dict(components that areGeometryProfile) anddict()exactly asGalaxydoes them (instance_as_dict+to_dictper profile) soautonerves.dictable.from_dictrebuilds aMassField— test it.
-
Export —
autogalaxy/__init__.py:from .galaxy.mass_field import MassFieldbesideGalaxy/Galaxies. -
JAX pytree —
autogalaxy/jax/registration.py's_register_object_classesalready registers any non-builtin class reachable from aGalaxiesmember viaregister_instance_pytree(cls)(the prompt'sno_flatten=("redshift",)claim is not what the code does today —Galaxyis registered by the walker with nono_flatten). SoMassFieldneeds no code change there; add a docstring line naming it and measure with the three-way probe fromPyAutoMind/complete/2026/07/public-register-galaxies-classes.mdusingag.Galaxies([galaxy, ag.MassField(...)])inside a user@jax.jit(deflections and convergence). If the probe fails becauseredshiftis a Python float that must ride as aux, registerMassFieldexplicitly withno_flatten=("redshift",)in the walker and note the asymmetry withGalaxyin the PR. -
chaining_util.py— it pattern-matches onGalaxyin several places (lines ~96–129); read them and confirm aMassFieldin a model is simply ignored (it should not be ingalaxiescollections in phase 1); no change expected, say so in the PR. -
Docs —
docs/api/galaxy.rst: addMassFieldto the autosummary; a short "External fields" paragraph in the galaxy overview page (docs/overview/overview_2_new_user_guide.mdor whereverGalaxyis introduced) explaining when to useMassFieldand that the galaxy-attached form remains fully supported. No PyAutoMemory citations in public docs. -
Tests —
test_autogalaxy/galaxy/test_mass_field.py:- construction with
shear,mass_sheet,potentialindividually and together; attribute access by name; Sersic/Pixelization/Regularization/ a list →GalaxyException, message names the key;isinstance(field, ag.Galaxy)is False;has(LightProfile)False,has(MassProfile)True,has(ExternalShear)True;deflections_yx_2d_from,convergence_2d_from,potential_2d_fromon agrid_2d_7x7arenp.array_equalto the same profiles on anag.Galaxy; zero when empty;image_2d_fromzeros of the grid shape,image_2d_list_from == [];ag.Galaxies([galaxy, field]):deflections_yx_2d_fromequals galaxy + field sums,image_2d_fromequals the galaxy alone,has(LightProfile)unchanged,cls_list_from(MassProfile)includes the field's profiles,galaxies_with_cls_list_from(MassProfile)includes the field,perform_inversionFalse;to_dict/from_dictround trip returns aMassFieldequal to the original;__repr__string;- identifier pin:
model.identifier == "<value from step 1>"with a comment saying which model produced it and that it was captured onmain; git diff --name-only maincontains noautogalaxy/config/priors/path (a test in the PR description checklist, not pytest).
Runpytest test_autogalaxy -q -n auto— green.
- construction with
Key Files
autogalaxy/galaxy/galaxy.py—Galaxy; the mass-sum methods move out into the mixin (only edit).autogalaxy/galaxy/mass_aggregate.py— new mixinMassProfileAggregate.autogalaxy/galaxy/mass_field.py— newMassField.autogalaxy/galaxy/galaxies.py— read-only: defines what a plane calls on a member (image_2d_from,image_2d_unbinned_from,has,cls_list_from,deflections_yx_2d_from,convergence_2d_from,potential_2d_from,extract_attribute).autogalaxy/operate/image.py— read-only:OperateImageList/OperateImageGalaxiesmember contract.autogalaxy/jax/registration.py— walker that registers member classes; docstring note.autogalaxy/profiles/mass/sheets/{external_shear,mass_sheet,external_potential}.py— the three intended components.autogalaxy/__init__.py— export.docs/api/galaxy.rst,docs/overview/— API + overview docs.test_autogalaxy/galaxy/test_mass_field.py— new tests.
Hard constraints (from the prompt)
Galaxy's import path, constructor,dict()output and hash/eq unchanged; the identifier pin proves nothing PyAutoFit hashes moved.- No deprecation warning for sheets on a
Galaxy. - No change under
autogalaxy/config/priors/.
Out of scope
Tracer(fields=), the analysis fields slot, COOLEST, the LOS sampler and model_util.mass_field_from are phase 2 (PyAutoLens); workspaces are phases 3–5.
Original Prompt
Click to expand starting prompt
MassField: a standalone, MassProfile-only container for external shear, mass sheets and external potentials
Type: feature
Target: PyAutoGalaxy
Repos:
- PyAutoGalaxy
Themes: - cluster
Difficulty: small
Autonomy: supervised
Priority: normal
Consequence: glance
Witness:ag.MassField(redshift=0.5, shear=ag.mp.ExternalShear(0.05, 0.05)).deflections_yx_2d_from(grid)isnp.array_equalto the same profile on anag.Galaxy;isinstance(field, ag.Galaxy)is False;ag.MassField(redshift=0.5, bulge=ag.lp.Sersic())raises; identifier pin:af.Collection(galaxies=af.Collection(lens=af.Model(ag.Galaxy, redshift=0.5, mass=af.Model(ag.mp.Isothermal), shear=af.Model(ag.mp.ExternalShear)))).identifierequal onmainand on the branch;pytest test_autogalaxy -q -n autogreen;git difftouches noautogalaxy/config/priors/file.
Review-minutes: 5
Unattended: ready
Epic: mass-field
Phase: 1
Filed: 2026-09-17
Add MassField to PyAutoGalaxy: the redshift-bearing container for the mass
components that describe the tidal field of everything outside the modelled
system — ExternalShear, MassSheet, ExternalPotential — so a model can say
"this is a property of the system" instead of pinning the field to one galaxy.
It is its own thing, not a Galaxy subclass. The design, prior art and the
backwards-compatibility invariant are in the epic ledger
draft/feature/autogalaxy/mass_field_epic.md; read it first.
What to build
- Share the mass sums, do not copy them.
@PyAutoGalaxy/autogalaxy/galaxy/galaxy.py
implementshas,cls_list_from,deflections_yx_2d_from,
convergence_2d_from,potential_2d_fromand thedeflections_memo
routing. Extract them, behaviour-preservingly, into a mixin
(e.g.autogalaxy/galaxy/mass_aggregate.py: MassProfileAggregate) that
Galaxykeeps using andMassFieldalso uses.Galaxy's import path,
constructor,dict()/to_dictoutput and__eq__/__hash__are unchanged
— the identifier pin in the Witness is the proof, run it before and after. @PyAutoGalaxy/autogalaxy/galaxy/mass_field.py—class MassField(af.ModelObject, MassProfileAggregate).__init__(self, redshift, **kwargs): validate the redshift the way
Galaxydoes (validate.validate_redshift), reject lists with the same
message, then require every component to be aMassProfile
(autogalaxy.profiles.mass.abstract.abstract.MassProfile); a
LightProfile,Pixelization,Regularizationor anything else raises
exc.GalaxyExceptionnaming the offending key and what aMassFieldis
for. AnyMassProfileis accepted (a user may want an NFW "environment"
halo as a field); the docstring names the three intended ones.- Zero-light interface, so a tracer plane can hold it beside galaxies:
has(cls)is False for any non-mass class;image_2d_list_fromreturns
[];image_2d_fromreturns zeros of the grid's shape; whatever else
Galaxies/OperateImageGalaxiescall on a member (read
autogalaxy/galaxy/galaxies.pyandautogalaxy/operate/image.pyand list
them in the PR) gets the no-op that makes a field contribute nothing to
light and everything to mass. Do not inheritOperateImageList. __repr__:MassField(redshift=..., <component names>);__eq__/__hash__
followingGalaxy's pattern (it is used as a dict key nowhere today, but
keep it hashable).dict()/to_dictround trip viaautonerves.dictablerebuilds a
MassField(test it).
- Export
ag.MassFieldfrom@PyAutoGalaxy/autogalaxy/__init__.pybeside
Galaxy/Galaxies. - JAX pytree registration —
@PyAutoGalaxy/autogalaxy/jax/registration.py
registersGalaxywithno_flatten=("redshift",); registerMassFieldthe
same way in the same walker. Measure with the three-way probe from
complete/2026/07/public-register-galaxies-classes.mdusing aMassField
in theGalaxieslist. - Tests —
@PyAutoGalaxy/test_autogalaxy/galaxy/test_mass_field.py:
construction with each of the three sheets and all three; rejection of
light profiles / pixelizations with the named key in the message;
isinstance(field, Galaxy)False; field equality of deflections /
convergence / potential against the same profiles on aGalaxy;
image_2d_fromzeros;Galaxies([galaxy, field])aggregate sums (a plane
can hold it);to_dict/from_dictround trip type;__repr__; the
identifier pin as a regression test against a hard-coded value captured on
mainand a comment saying which model produced it. - Docs —
@PyAutoGalaxy/docs/api/galaxy.rstgainsMassField; a short
"External fields" paragraph in the galaxy overview page explaining when to
use it and that the galaxy-attached form remains supported.
Hard constraints
Galaxy's public behaviour is unchanged (import path, constructor, dict
output, hash/eq); the mixin extraction is the only edit togalaxy.pyand
the identifier pin proves it moved nothing that PyAutoFit hashes.- No deprecation warning anywhere for sheets on a
Galaxy. - No prior config changes (
autogalaxy/config/priors/).
Out of scope (later phases)
The tracer's fields= argument, the analysis slot, COOLEST, the LOS sampler
and the model_util helper are phase 2; workspaces are phases 3–5.
Activity
Jammy2211 commentedon Sep 17, 2026
Library PR Created
feature/mass-field-class, labelledpending-release)API changes: added
ag.MassFieldand theMassProfileAggregatemixin;Galaxyunchanged in behaviour (identifier pin7b251058cdae562470abab6203bacf72holds). No removals or renames, so no workspace migration is needed from this PR; the workspace adoption ofMassFieldis the epic's phases 3–5, filed as their own prompts and gated on phase 2 (PyAutoLensTracer(fields=)).Gate:
pytest test_autogalaxy -n auto1236 passed / 0 failed; JAX three-way probe green with aMassFieldin aGalaxieslist; noautogalaxy/config/change; no deprecation warning.Next:
/prmonce CI is green on #621 (merge is human). Then phase 2,PyAutoMind/draft/feature/autolens/mass_field_integration.md, can be issued.Generated by Claude Code
Jammy2211 commentedon Sep 17, 2026
Shipped
33714b80; CI green on unittest 3.12, 3.13, nojax and docs-build).What changed:
ag.MassField(redshift, **mass_profiles)— a standalone, MassProfile-only container for external shear, mass sheets and external potentials, with a zero-light interface so aGalaxiesplane holds it beside galaxies.Galaxy's mass sums moved verbatim into theMassProfileAggregatemixin both classes share;Galaxybehaviour,dict()output and prior configs are unchanged (identifier pin7b251058cdae562470abab6203bacf72asserted). API docs and the start-here overview gainedMassField.Release state:
pending-release— on PyAutoGalaxymain, not yet on PyPI.Next: phase 2 of the mass-field epic,
PyAutoMind/draft/feature/autolens/mass_field_integration.md(PyAutoLensTracer(fields=)), is now unblocked.Generated by Claude Code