Skip to content

fix: LOSSampler mass function from colossus, drop hmf and silent los_halos fallback #867

Closed

Description

@Jammy2211

Overview

LOSSampler with no precomputed coefficients raises ModuleNotFoundError in the declared environment: mass_function_ab_from imports hmf (undeclared) and hmf.fitting_functions (moved in hmf 3.7). The workspace los_halos simulators catch the ImportError and silently fall back to placeholder coefficients that under-count halos by up to 20% at z = 0.9. This fix computes the Sheth-Tormen mass function from colossus (already used for the concentration relation), declares colossus as an optional dependency, and removes the silent fallback from the workspace.

Plan

  • Rewrite mass_function_ab_from to fit colossus massFunction(model='sheth99'), dropping hmf entirely.
  • Give both coefficient functions one clear ImportError naming colossus and how to install it.
  • Keep the old cosmology_astropy argument accepted but ignored, with a DeprecationWarning, so existing calls do not break.
  • Declare a los = ["colossus"] extra in PyAutoLens and add it to optional (which CI installs).
  • Add unit tests: the Witness (slope and expected count) and the clear-message path when colossus is missing.
  • Workspace: drop the try/except ImportError placeholder block from simulator.py and simulator_jax.py; let LOSSampler compute the coefficients, so a missing colossus stops with the library's message.
  • Tier line: effective level: supervised; merge mode: auto-merge on green if the Witness passes after a merge slot
Detailed implementation plan

Affected Repositories

  • PyAutoLens (primary)
  • autolens_workspace

Branch Survey

Repository Current Branch Dirty?
lens/PyAutoLens main (== origin/main a79a6b2) clean
lens/autolens_workspace main (origin/main 4674ae89) workspace_index.json only

Suggested branch: feature/los-sampler-colossus-mass-function
Worktree root: ~/Code/PyAutoLabs-wt/lossampler_hmf_import_fails_and_los_halos/
Work classification: Both (library first)

Implementation Steps

  1. autolens/lens/los.py
    • New private _import_colossus() that raises ImportError("... requires the optional dependency colossus: pip install colossus (or autolens[los]) ...") from the original error.
    • mass_function_ab_from(redshift, cosmology_astropy=None, m_min_log10=5.9, m_max_log10=10.1, dlog10m=0.1, cosmology_name="planck15"): evaluate massFunction(m*h, z, mdef="fof", model="sheth99", q_out="dndlnM"), convert to dn/dm in Mpc^-3 Msun^-1 (dndlnM * h^3 / m), least-squares fit log10(dn/dm) = A log10(m) + B on the same mass grid as before. cosmology_astropy not None → DeprecationWarning, ignored.
    • mass_concentration_ab_from uses _import_colossus().
    • LOSSampler: pass cosmology_name_colossus to the mass-function fit; cosmology_astropy deprecated (warn if given); docstrings updated.
  2. pyproject.toml: los = ["colossus"]; optional gains "autolens[los]".
  3. test_autolens/lens/test_los.py: importorskip("colossus") Witness test (per-plane slope within 0.02 of -1.906; summed number_of_halos_from within 5% of 1282 for z_lens 0.5, z_source 1.0, 1e7–1e10, 5"), plus a missing-colossus test (sys.modules["colossus"] = None via monkeypatch) asserting the message.
  4. Workspace scripts/imaging/features/advanced/los_halos/simulator.py and simulator_jax.py: remove the try/except block and the hmf/astropy text; do not pass coefficients; text explains colossus is required (Sheth-Tormen + Ludlow16). Notebooks are regenerated at release, not edited.

Measured prototype (colossus sheth99, Planck15): A = -1.9054 (z=0.1) to -1.9097 (z=0.9); expected total 1293.7 halos vs 1281.7 with the hmf SMT fit (+0.9%). Fit takes ~0.01 s for 9 planes.

Key Files

  • autolens/lens/los.py, pyproject.toml, test_autolens/lens/test_los.py
  • autolens_workspace/scripts/imaging/features/advanced/los_halos/simulator.py, simulator_jax.py

Original Prompt

Click to expand starting prompt

LOSSampler hmf import fails and los_halos falls back silently

Type: bug
Target: PyAutoLens
Repos:

  • PyAutoLens
  • autolens_workspace
    Difficulty: medium
    Autonomy: supervised
    Priority: medium
    Status: formalised
    Consequence: glance
    Witness: In an environment with the declared dependencies only, LOSSampler(z_lens=0.5, z_source=1.0) with no precomputed coefficients samples a population whose per-plane mass-function slope is within 0.02 of -1.906 and whose expected halo count for 1e7 to 1e10 solar masses in a 5 arcsecond cone is within 5% of 1282; and the workspace los_halos simulators either use those real coefficients or stop with a clear message, never a silent placeholder.
    Review-minutes: 3
    Unattended: ready
    Lane: any

Finding

Three linked problems, measured on PyAutoLens main a79a6b2, 2026-11-19.

  1. @PyAutoLens/autolens/lens/los.py mass_function_ab_from does import hmf.fitting_functions.
    With hmf 3.7.2 this raises ModuleNotFoundError; the module now lives at
    hmf.mass_function.fitting_functions.
  2. hmf is not a declared dependency of any library, and is not installed in the science
    environment. LOSSampler with no precomputed coefficients therefore raises.
  3. @autolens_workspace/scripts/imaging/features/advanced/los_halos/simulator.py and
    simulator_jax.py wrap the coefficient calculation in except ImportError and fall back to
    [-1.9, 8.0] for the mass function and [-3.0, 40.0] for the concentration relation. Because
    problem 1 is an ImportError, this fallback runs even when hmf is installed.

The placeholders are not close. Real fitted coefficients run from A = -1.905, B = 8.053 at z = 0.1 to
A = -1.910, B = 8.171 at z = 0.9. The fallback ignores redshift evolution and under-counts halos by
up to 20 to 23% at z = 0.9. The concentration fallback is off by up to 17%, 32% and 56% at
z = 0.1, 0.5 and 0.9.

Why it matters

Every run of the workspace line-of-sight examples in the current environment simulates a halo
population that is not the Sheth-Tormen plus Ludlow16 population the text describes, with no
message saying so.

Ask

Preferred: drop hmf and compute the mass function from colossus
(colossus.lss.mass_function.massFunction, model='sheth99'), which the sampler already uses for
the concentration relation. Measured agreement of the existing power-law fit with colossus
sheth99: integrated number 0.98 to 1.00, integrated mass 0.992. Then declare colossus as an
optional dependency of PyAutoLens for the line-of-sight feature.

Minimal alternative: fix the import path and declare hmf as an optional dependency.

In both cases, change the workspace simulators so a missing dependency stops with a clear message
or prints a loud warning; do not keep a silent placeholder. Library fix first, workspace second.

Provenance

[from: research dm-sightlines-cdm] Checks 3 and 4 of the verification run. Scripts and logs:
Science/scratch/dm_sightlines_research_2026-11-19/evidence/verify/check3_massfunction.py,
check3_import.log, check4_concentration.py.

Activity

  1. Jammy2211 commented on Nov 19, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #868 and PyAutoLabs/autolens_workspace#681 (2026-11-19, /prm, merged unchanged). LOSSampler now fits the Sheth-Tormen mass function from colossus (new autolens[los] extra); a missing colossus raises a named ImportError and the los_halos simulators no longer fall back silently. Witness PASS with no hmf (slopes within 0.0037 of -1.906, expected count +0.9% vs the old hmf fit). Independent adversary (Fable 5.2): CLEAN. Human ruling: fine for los_halos to stop in Colab. Follow-ups already filed: Heart colab gate rationale, non-Planck cosmology ignored.
    Not yet released (pending-release). Record: PyAutoMind complete/2026/11/lossampler-hmf-import-fails-and-los-halos.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions