Skip to content

feat(galaxy): named-galaxy CSV reader/writer for full model round-trips#428

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/cluster-csv-api
May 19, 2026
Merged

feat(galaxy): named-galaxy CSV reader/writer for full model round-trips#428
Jammy2211 merged 2 commits into
mainfrom
feature/cluster-csv-api

Conversation

@Jammy2211
Copy link
Copy Markdown
Collaborator

Summary

Adds a CSV layer over the full galaxy model — names, profile classes, and constructor parameters — so cluster modelling scripts can store and reload an entire af.Collection of Galaxy objects as a small set of human-editable CSVs. Sibling of the existing galaxy_table.py (which keeps its narrow y, x, luminosity scaling-tier schema unchanged).

Workspace integration follows in a separate PR on autolens_workspace (issue PyAutoLabs/autolens_workspace#187) — this PR is library helpers + tests only.

API Changes

Two new public dataclasses (GalaxyModelRow, GalaxyModelTable) and four public functions (galaxy_models_to_csv, galaxy_models_from_csv, galaxies_from_csv_tables, galaxy_af_models_from_csv_tables). One CSV per profile family (mass / light / point); profile classes dispatch via getattr against autogalaxy.profiles.{mass,light.standard,point_sources}. Sparse columns supported — disparate profile classes can share one CSV.

No breaking changes. galaxy_table.py is untouched.

See full details below.

Test Plan

  • pytest test_autogalaxy/galaxy/test_galaxy_model_csv.py — 8 new tests cover single-family round-trip, sparse-column round-trip (dPIE + NFW), light family (Sersic + SersicCore), point family, cross-family join, af.Model round-trip, redshift-consistency check, class-not-found error.
  • pytest test_autogalaxy/galaxy/ — 100 passing, no regressions.
Full API Changes (for automation & release notes)

Added

  • autogalaxy.GalaxyModelRow — dataclass: galaxy: str, attr_name: str, profile_class: type, params: Dict[str, Any], redshift: Optional[float].
  • autogalaxy.GalaxyModelTable — dataclass: rows: List[GalaxyModelRow], family: str.
  • autogalaxy.galaxy_models_to_csv(profiles_by_galaxy, file_path, family, redshifts=None) — writes a family CSV with one row per (galaxy, attr_name) pair. Introspects each profile instance's constructor via inspect.signature to discover the column set; sparse columns supported (different profile classes can share one CSV).
  • autogalaxy.galaxy_models_from_csv(file_path, family) — reads a family CSV and dispatches profile_class against autogalaxy.profiles.{mass,light.standard,point_sources} for family ∈ {"mass", "light", "point"} respectively. Returns a typed GalaxyModelTable.
  • autogalaxy.galaxies_from_csv_tables(*tables) — joins one or more GalaxyModelTables on the galaxy column and returns Dict[str, Galaxy]. Per-galaxy redshift consistency is enforced.
  • autogalaxy.galaxy_af_models_from_csv_tables(*tables) — same as above but returns Dict[str, af.Model[Galaxy]] with concrete CSV values as fixed af.Model defaults (ready to mutate selected params into priors).

CSV schema

  • Fixed columns: galaxy, attr_name, profile_class.
  • centre tuple param → y, x columns (precedent from galaxy_table.py).
  • Other tuple params (e.g. ell_comps) → <name>_0, <name>_1 columns.
  • Optional redshift column for per-galaxy redshift (consistent across rows for the same galaxy or all blank).

Module

  • New file: autogalaxy/galaxy/galaxy_model_csv.py (~280 lines).
  • Existing autogalaxy/galaxy/galaxy_table.py unchanged.

🤖 Generated with Claude Code

Adds galaxy_model_csv with helpers that read/write named-galaxy CSVs keyed by
a `galaxy` column and `profile_class` dispatch, alongside the existing
galaxy_table.py (which keeps its narrow scaling-tier schema). Three family-
level CSV schemas — mass, light, point — each with one row per
(galaxy, attr_name, profile_class) tuple and sparse parameter columns. The
join across families produces concrete Galaxy instances or af.Model[Galaxy]
ready for non-linear search composition.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Add a writer-side guard: each profile passed to galaxy_models_to_csv must
have its class exposed in the family namespace declared by the call.
Surfaces misuse like passing a SersicSph under family='mass' with a clear
error rather than silently writing a wrong-family row that fails on read.

Found while writing the workspace csv_api.py guide — passing a Galaxy
dict containing both bulge + mass profiles to family='light' silently
wrote dPIE rows into light.csv, which then failed on read as
'profile_class dPIEMassSph not found in light namespace'.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211
Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autolens_workspace#189

@Jammy2211 Jammy2211 merged commit 1778f51 into main May 19, 2026
6 checks passed
@Jammy2211 Jammy2211 deleted the feature/cluster-csv-api branch May 19, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-release PR queued for the next release build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant