Skip to content

Enable user-selectable cosmology and update default to Planck18#391

Draft
elijahmathews wants to merge 3 commits into
bd-j:mainfrom
elijahmathews:variable_cosmo
Draft

Enable user-selectable cosmology and update default to Planck18#391
elijahmathews wants to merge 3 commits into
bd-j:mainfrom
elijahmathews:variable_cosmo

Conversation

@elijahmathews

@elijahmathews elijahmathews commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #171

This PR refactors the codebase to treat cosmology as a configurable model parameter (model.params['cosmology']) rather than a hard-coded global constant.

Previously, Prospector was hard-coded to use WMAP9 (Hinshaw et al. 2013) via astropy. This PR updates the default cosmology to Planck18 (Planck Collaboration 2018) to match modern simulations and survey parameters, while allowing users to inject custom astropy.cosmology objects (e.g., FlatLambdaCDM, Planck15) via the parameter file.

Changes Implemented

1. Cosmology Parameterization

  • Model Integration: Added cosmology to the default parameter templates in prospect/models/templates.py. It is now stored in model.params alongside physics parameters.
  • SpecModel Logic: Added a @property to SpecModel in prospect/models/sedmodel.py that retrieves the cosmology object. This property handles the necessary "unwrapping" of the object (as ProspectorParams wraps all inputs in NumPy arrays).
  • Transform Functions: Updated helper functions in prospect/models/transforms.py (e.g., zred_to_agebins, tage_from_tuniv) to accept an optional cosmology argument.

2. Physics & Defaults

  • New Default: Updated the global default in prospect/sources/constants.py from WMAP9 to Planck18.
  • Prospector-β Constraint: Explicitly pinned the beta and beta_phisfh templates in prospect/models/templates.py to use WMAP9. This is mathematically required because the Prospector-β prior data (specifically the redshift-age relationship tables in prospect/models/prior_data/wmap9_z_age.txt) was generated using WMAP9.

3. Maintenance & Compatibility

  • Backward Compatibility: Retained a cosmo alias in prospect/sources/constants.py and implemented fallback logic in transform functions. Existing scripts that do not specify a cosmology parameter will default gracefully to Planck18 without raising errors.
  • Gitignore: Updated .gitignore to use the standard Python GitHub template while preserving project-specific rules (e.g., *.h5, manual*, demo/*/).

Verification

I have added a new test module tests/test_cosmology.py that verifies:

  • Default Behavior: Ensures models initialized without explicit cosmology parameters default correctly to Planck18.
  • Customization: Verifies that passing a WMAP9 object in model_params alters derived properties (e.g., luminosity distance, age of universe) compared to the default.
  • NumPy Unwrapping: Explicitly tests the robust handling of wrapped object arrays within SpecModel and transforms, preventing AttributeError when accessing cosmology methods.

Checklist

  • Refactor SpecModel and transforms to accept cosmology objects.
  • Update TemplateLibrary defaults.
  • Default Prospector-β to WMAP9 to ensure consistency with prior data.
  • Add backward compatibility fallbacks in constants.py.
  • Add unit tests for cosmology selection and object unwrapping.
  • Allow Prospector-β to use cosmologies other than WMAP9 (maybe?).
  • Update .gitignore.
  • Update documentation.

… rules

This commit replaces the ad-hoc .gitignore with the standard GitHub
Python template to better handle common artifacts like __pycache__,
build directories, and coverage reports.

It retains project-specific exclusions, including:
- Scientific data formats (*.fits, *.h5)
- Manuscript and LaTeX files
- Custom demo and misc directories
- The generated _version.py file
This commit updates the default cosmology used in Prospector to
the `astropy.cosmology.Planck18` cosmology (Planck Collaboration
2020, Table 2), in place of the previous default of `WMAP9`
(Hinshaw et al. 2013).
Refactors the codebase to treat cosmology as a model parameter
(`model.params['cosmology']`) rather than a hard-coded global
constant. This allows users to specify custom `astropy` cosmology
objects in their parameter files.

Key Changes:
- **Model Parameters:** Added `cosmology` to the default parameter
  templates in `prospect.models.templates`.
- **Prospector-Beta:** Explicitly pinned the `beta` template to
  `WMAP9` to ensure consistency with
  `prospect/models/prior_data/wmap9_z_age.txt`.
- **SpecModel:** Added a `cosmology` property to `SpecModel` that
  handles retrieval and unwrapping of the cosmology object from
  the parameters dictionary.
- **Transforms:** Updated helper functions in
  `prospect.models.transforms` (e.g., `zred_to_agebins`,
  `tage_from_tuniv`) to accept an optional `cosmology` argument,
  resolving the correct object via a new `_get_cosmology` helper.
- **Backward Compatibility:** Retained `cosmo` alias in `constants.py`
  and implemented fallbacks in transform functions to prevent breaking
  changes for scripts relying on the global default.

Resolves bd-j#171
@elijahmathews

Copy link
Copy Markdown
Contributor Author

@bd-j Do you have a preference about allowing the Prospector-β priors to be used with cosmologies other than WMAP9? Since we're currently relying on the tabulated wmap9_z_age.txt file (used by prospect.models.priors_beta.expe_logsfr_ratios), the Prospector-β priors really only stay self-consistent when using WMAP9. I'm guessing this was done this way for speed reasons, but perhaps this table could be auto-generated ahead of time using the user-provided cosmology (within BetaPrior.__init__ maybe?) rather than reading it from disk (allowing wmap9_z_age.txt to be removed from the repo).

@bd-j

bd-j commented Feb 3, 2026

Copy link
Copy Markdown
Owner

Hi @elijahmathews I do not have a strong preference about the cosmology for prospector-beta, as long as it's noted in the documentation. That said, I'd be in favor of removing the data file and instead computing during initialization.

@bd-j

bd-j commented Feb 3, 2026

Copy link
Copy Markdown
Owner

Regarding having a cosmology object instance as a parameter, I wonder if this will cause any issues with writing outputs.

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.

Make cosmology user changeable

2 participants