Skip to content

Add implementation of Prospector alpha & beta showing complex hierarchical priors - #37

Open
tHarvey303 wants to merge 7 commits into
mainfrom
complex_model_priors
Open

tHarvey303 wants to merge 7 commits into
mainfrom
complex_model_priors

Conversation

@tHarvey303

@tHarvey303 tHarvey303 commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Prospector-alpha and Prospector-beta (Wang+23) are models which incorporate physically motivated priors. The SFH prior is redshift dependent and matched to the CSFRD of Behroozi+19, a redshift dependent GSMF prior is used (Leja+20 and Tacchella+18) and the SDSS mass-metallicity of Gallazzi+05 is incorporated.

These are added primarily as an example of how to implement hierarchical prior based sampling for a galaxy population, where the draws for different SPS parameters are dependent on other parameters. There are example scripts added showing how to use these models.

TODO:

  1. Implement Prospector-alpha like AGN model as well.

Summary by CodeRabbit

  • New Features

    • Added native PyTorch Simformer training, posterior and interval-constrained sampling, likelihood evaluation, log probabilities, and model persistence.
    • Added flexible conditioning masks, uniform-box priors, and Prospector Alpha/Beta library-generation examples.
    • Added prior utilities and packaged astrophysical reference data.
    • Added a complete two-moons Simformer demonstration.
    • Preserved library precision when loading generated data.
  • Documentation

    • Updated Simformer guidance, installation instructions, examples, and repository usage documentation.
  • Chores

    • Removed the optional JAX runtime dependency and related configuration.

tHarvey303 and others added 6 commits July 5, 2026 00:27
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…iants

Adds src/synference/simformer/, a self-contained PyTorch port of the original
JAX Simformer (Gloeckler et al. 2024, scoresbibm/probjax), and rewires
Simformer_Fitter to use it. Replaces (a) the scoresbibm/JAX wrapper
(src/synference/simformer.py, deleted) and (b) SBI_Fitter.run_single_simformer,
which depended on the unmerged sbi PR #1621 (deleted). jax is removed from the
dependencies.

The new subpackage faithfully replicates the paper architecture and math:
per-variable tokens with frozen node-id embeddings and a mask-gated condition
token, pre-LN transformer with time context injected in every MLP block,
VE-SDE (default) and VP-SDE, masked denoising score matching with
structured-random condition-mask sampling, adaptive gradient clipping, and
early stopping. Inference supports arbitrary condition masks (posterior,
likelihood, joint, missing bands), base attention masks, interval-constrained
sampling via Tweedie guidance, and log probabilities via the probability-flow
ODE. Models serialize to a plain torch payload and reload in a fresh process
(SimformerModel.save/load).

API notes (clean break):
- sample_posterior(condition_mask=...) replaces the misnamed attention_mask
  argument (it was always a condition mask); full sentinel unchanged.
- log_prob(X_test, theta, ...) takes theta as the second positional argument,
  fixing evaluate_model silently passing y_test as a mask.
- Config overrides are validated against known keys instead of being silently
  swallowed.
- Old JAX-era model pickles cannot be loaded by the new implementation.
- fit_catalogue's Simformer branch now transposes samples like the regular
  branch, fixing wrong quantiles for multi-row catalogues.

Validation: analytic 2D-Gaussian conditional gate, TwoMoons (bimodal posterior,
TARP calibration, likelihood/joint conditionals, interval guidance, log-prob),
and the galaxy test library end-to-end (training, artifacts, reload in a fresh
process, fit_catalogue) in tests/test_simformer.py; full suite passes. A demo
lives in examples/simformer/scripts/two_moons_demo.py and the docs notebook
was rewritten for the new API.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…loat32 grid precision for improved performance and reduced memory impact.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR replaces the JAX-based Simformer implementation with native PyTorch components, adds Prospector-alpha and Prospector-beta prior utilities, updates library-generation examples, and expands documentation and tests.

Changes

Native Simformer implementation

Layer / File(s) Summary
Simformer architecture and conditioning
src/synference/simformer/*
Adds condition-mask utilities, transformer score networks, VE/VP SDEs, and public package exports.
Sampling, guidance, and persistence
src/synference/simformer/model.py, src/synference/simformer/sampling.py
Adds conditional sampling, interval guidance, probability-flow log probabilities, and versioned model serialization.
Training and fitter integration
src/synference/simformer/train.py, src/synference/sbi_runner.py, src/synference/__init__.py
Replaces task-based JAX training and persistence with native PyTorch training, diagnostics, catalogue fitting, condition masks, and UniformBoxPrior.
Prospector priors and library examples
src/synference/priors_alpha.py, src/synference/priors_beta.py, src/synference/prior_data/*, examples/library_generation/*
Adds packaged prior data, alpha and beta prior samplers, and Prospector-style library-generation scripts.
Examples and validation
examples/simformer/scripts/two_moons_demo.py, tests/test_simformer.py
Adds a two-moons workflow and tests for architecture, SDEs, masks, sampling, guidance, log probabilities, persistence, training, calibration, fitter integration, and priors.
Documentation and dependency alignment
CLAUDE.md, README.md, docs/source/*, pyproject.toml
Documents native Simformer usage and removes JAX and the obsolete Simformer dependency group from project guidance.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SimformerFitter
  participant SimformerModel
  participant Sampling
  participant Diagnostics
  SimformerFitter->>SimformerModel: provide observations and condition_mask
  SimformerModel->>Sampling: run reverse SDE or probability-flow solver
  Sampling->>SimformerModel: return posterior samples or log probabilities
  SimformerModel->>Diagnostics: provide inference outputs
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary Prospector alpha and beta hierarchical-prior implementation described in the objectives and changes.
Docstring Coverage ✅ Passed Docstring coverage is 89.08% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch complex_model_priors

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 13

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/synference/sbi_runner.py (1)

7909-7924: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Preserve the load_saved_model contract in Simformer_Fitter.

Simformer_Fitter changes the inherited positional order from (model_file, library_path, model_name, load_arrays) to (model_name, library_path, model_file). Positional callers therefore bind values incorrectly. The override also omits load_arrays and forwards it through **kwargs instead of handling it explicitly. Match the base signature or use a distinct method.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/sbi_runner.py` around lines 7909 - 7924, Update
Simformer_Fitter.load_saved_model to preserve the inherited positional contract:
accept arguments in the base order (model_file, library_path, model_name,
load_arrays), handle load_arrays explicitly, and forward it appropriately during
model initialization. Keep the existing model loading behavior while ensuring
positional callers bind each value correctly.
🧹 Nitpick comments (10)
examples/library_generation/scripts/prospector_alpha_library_generation.py (1)

295-307: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Z_dists is built for every model, but sfh_models respects mask.

Lines 295-298 construct one ZDist.DeltaConstant for all Nmodels entries. Lines 303-307 construct SFH objects only where mask[i] is True. Apply the same mask to the ZDist loop to avoid allocating unused objects on each rank.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/library_generation/scripts/prospector_alpha_library_generation.py`
around lines 295 - 307, Update the Z_dists construction to honor mask in the
same way as sfh_models: iterate over the model indices, create a
ZDist.DeltaConstant only when mask[i] is true, and store None otherwise.
Preserve the existing log_zmet mapping and tqdm behavior while ensuring every
rank avoids allocating unused ZDist objects.
src/synference/priors_alpha.py (2)

76-80: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

The qmc.LatinHypercube seed/rng kwarg detection is copied at three sites. Each site runs inspect.signature(qmc.LatinHypercube) to choose between the rng and seed keyword, which SciPy renamed in 1.15. Extract one private helper, for example _lhc(d, rng), into a shared module and call it from all three sites. That removes the duplication and keeps the SciPy version handling in one place.

  • src/synference/priors_alpha.py#L76-L80: replace the inline block in sample_dirichlet_sfr_fractions with the helper call.
  • src/synference/priors_alpha.py#L111-L115: replace the inline block in sample_dust_ratio with the helper call.
  • src/synference/priors_beta.py#L431-L436: replace the inline block in sample_prospector_beta_prior with the helper call.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/priors_alpha.py` around lines 76 - 80, The LatinHypercube
rng/seed compatibility logic is duplicated across three sampling sites; add one
private shared helper, such as _lhc(d, rng), that performs the signature
detection and constructs qmc.LatinHypercube, then replace the inline blocks in
sample_dirichlet_sfr_fractions and sample_dust_ratio in
src/synference/priors_alpha.py#L76-L80 and `#L111-L115`, and
sample_prospector_beta_prior in src/synference/priors_beta.py#L431-L436 with
calls to that helper.

43-54: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Correct the docstring summary. dirichlet_zfraction_beta_params returns Beta shape parameters, not SFR fractions. The current Beta(ncomp - i, 1) shapes are correct because SFH.Dirichlet and prospect.models.transforms.zfrac_to_sfrac use sfr_fraction[i] = prod(z_fraction[:i]) * (1 - z_fraction[i]).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/priors_alpha.py` around lines 43 - 54, The docstring summary
of dirichlet_zfraction_beta_params incorrectly says it draws Dirichlet SFR
fractions; update it to state that the function returns per-latent-variable Beta
shape parameters for the stick-breaking z_fraction variables. Preserve the
existing parameter description and implementation.
src/synference/priors_beta.py (2)

466-502: 🚀 Performance & Scalability | 🔵 Trivial | 🏗️ Heavy lift

The per-galaxy loop calls astropy cosmology functions for every draw.

For each index, expected_logsfr_ratios calls cosmo.age(zred), and _agebins_for_expectation calls cosmo.lookback_time(zstart) plus cosmo.lookback_time(15). beta_agebins calls cosmo.age(zred) again. Astropy cosmology evaluations are numerically integrated and cost on the order of milliseconds per scalar call. The example script sets Nmodels = 500_000, so this loop performs several million such calls.

Two changes remove most of the cost without changing the results measurably:

  • Hoist cosmo.lookback_time(15) out of _agebins_for_expectation. It is constant for a fixed cosmo.
  • Build one age/lookback-time interpolator over a redshift grid, in the same way as _make_age_to_redshift, and pass it into expected_logsfr_ratios and beta_agebins.

cdf_mass_func_at_z is also recomputed on a 1000-point grid per galaxy. Consider precomputing the CDF on the existing z_grid and interpolating in z.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/priors_beta.py` around lines 466 - 502, Optimize the
per-galaxy loop by hoisting the constant cosmo.lookback_time(15) out of
_agebins_for_expectation and creating one age/lookback-time interpolator over
the existing redshift grid, following _make_age_to_redshift. Pass that
interpolator into expected_logsfr_ratios and beta_agebins so they avoid scalar
cosmology evaluations while preserving results. Also precompute
cdf_mass_func_at_z on z_grid and interpolate by redshift instead of rebuilding
the 1000-point CDF for each galaxy.

171-192: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

setup_mass_normalization ignores its cosmo argument.

The function signature takes cosmo as the first positional parameter, but the body never uses it. This is misleading for a public, exported function. Remove the parameter, or document why it is reserved. If you remove it, update the call site at Line 440.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/priors_beta.py` around lines 171 - 192, Update
setup_mass_normalization to remove the unused cosmo parameter from its
signature, then adjust its call site near the existing invocation around line
440 to match the new positional arguments. Keep the normalization calculations
and return values unchanged.
src/synference/simformer/model.py (2)

340-354: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Extract the sampler dispatch shared with sample().

Lines 340-354 repeat the sde/ode dispatch and the error message from lines 277-286. A new method or a new sampler added later must be added in both places.

Extract a private helper and call it from both methods.

♻️ Proposed helper to remove the duplicated dispatch
def _integrate(self, method, score_fn, x_T, condition_mask, num_steps, generator):
    """Run the requested integrator and return the final state."""
    if method == "sde":
        return euler_maruyama_reverse(
            score_fn, self.sde, x_T, condition_mask, num_steps=num_steps, generator=generator
        )
    if method == "ode":
        return heun_probability_flow(
            score_fn, self.sde, x_T, condition_mask, num_steps=num_steps
        )
    raise ValueError(f"Unknown sampling method '{method}'. Use 'sde' or 'ode'.")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/model.py` around lines 340 - 354, Extract the
duplicated sde/ode sampler dispatch into a private helper near the model
sampling methods, preserving the existing euler_maruyama_reverse and
heun_probability_flow arguments and unknown-method ValueError. Update both
sample() and the shown sampling flow to call this helper, passing generator
through for SDE sampling.

199-199: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Cache node_ids as a tensor instead of rebuilding it on every score call.

score() runs once per integration step. With the default num_steps=500, this line performs 500 numpy→tensor conversions per sample() call. On CUDA each conversion is a separate host-to-device copy.

Build the tensor once and reuse it.

♻️ Proposed refactor to cache the node-id tensor

In __init__ (after line 136), store a tensor alongside the numpy array:

self.node_ids = np.asarray(node_ids, dtype=np.int64)
self._node_ids_t = torch.as_tensor(self.node_ids)

In to() (after line 148), move it with the model:

self._node_ids_t = self._node_ids_t.to(self.device)

Then use it in score():

-        node_ids = torch.as_tensor(self.node_ids, device=x.device)
+        node_ids = self._node_ids_t.to(x.device)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/model.py` at line 199, Cache the node-ID tensor
during model initialization alongside the NumPy array, move the cached tensor in
the model’s to() method, and update score() to reuse this tensor instead of
calling torch.as_tensor on every invocation. Preserve the existing device
placement behavior by ensuring the cached tensor follows self.device.
tests/test_simformer.py (2)

405-427: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Mark the training fixtures as slow.

two_moons_model trains for 3000 steps on 10,000 samples, and trained_fitter trains for 600 steps and then runs full diagnostics. Both run on every default pytest invocation. Add a marker so contributors can deselect them during fast iteration.

`@pytest.fixture`(scope="module")
`@pytest.mark.slow`  # register `slow` in pyproject.toml markers
def two_moons_model():

A marker on a fixture does not deselect it; apply pytestmark = pytest.mark.slow at the TestTwoMoons and TestSimformerFitterLibrary class level instead so the dependent tests are deselected together.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_simformer.py` around lines 405 - 427, Mark the test classes
TestTwoMoons and TestSimformerFitterLibrary with pytest.mark.slow so tests
depending on the two_moons_model and trained_fitter fixtures are deselected
together; register the slow marker in pyproject.toml.

458-500: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

These statistical tolerances may be fragile across platforms.

test_posterior_calibration_tarp asserts abs(ecp[mid] - 0.5) < 0.15 using only 100 test simulations and 250 posterior draws. test_arbitrary_conditionals asserts atol=0.1 on the mean and standard deviation of a model trained for 3000 steps. Both are seeded, but seeded PyTorch results are not bit-identical across BLAS backends, CPU architectures, and PyTorch versions, so the sampled statistics can shift.

Consider widening the tolerances or increasing the number of test simulations to reduce flake risk in CI.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_simformer.py` around lines 458 - 500, Harden the statistical
assertions in test_posterior_calibration_tarp and test_arbitrary_conditionals
against platform-dependent sampling and training variation. Prefer increasing
the TARP test simulations and/or posterior draws, and widen the mean/std
tolerances for the conditional and joint checks while preserving the intended
distributional validation.
src/synference/__init__.py (1)

31-47: 🚀 Performance & Scalability | 🔵 Trivial | ⚖️ Poor tradeoff

These eager imports add module-level work to import synference.

priors_beta builds module-level state at import time: it loads the packaged gallazzi_05_massmet.txt and behroozi_19_sfrd.txt files and constructs the MASSMET array and the SPL_TL_SFRD spline. It also pulls in astropy.cosmology and scipy.interpolate. Importing it from __init__.py moves that file I/O and those heavy dependencies into every import synference.

Consider a lazy __getattr__ for these prior helpers, matching the guarded import style already used for sbi_runner.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/__init__.py` around lines 31 - 47, Replace the eager
priors_beta and priors_alpha imports in the package initializer with a
module-level __getattr__-based lazy import, following the existing guarded
sbi_runner pattern. Preserve the same public helper names and load each priors
module only when one of its exported symbols is accessed, avoiding
priors-related file I/O and heavy dependencies during import synference.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/library_generation/scripts/prospector_alpha_library_generation.py`:
- Around line 338-341: Replace all lambdas stored in alt_parametrizations and
param_transforms_to_save with module-level named functions (or functools.partial
over module-level functions) so every transform passed to GalaxyBasis or
create_mock_library is picklable. Apply this in
examples/library_generation/scripts/prospector_alpha_library_generation.py at
lines 338-341 and 363-367, and
examples/library_generation/scripts/prospector_beta_library_generation.py at
lines 343-346 and 369-373; share the function definitions between scripts if
practical while preserving each transform’s current behavior.
- Around line 176-234: In
examples/library_generation/scripts/prospector_alpha_library_generation.py lines
176-234, parse sys.argv[2] once as a guarded integer run_mode alongside n_proc,
then replace the direct integer comparison with run_mode == 1 and update the
later string comparisons at lines 360-361 to run_mode == 0 and run_mode == 1.
Apply the identical change in
examples/library_generation/scripts/prospector_beta_library_generation.py lines
170-228, including its later comparisons at lines 366-367; ensure both scripts
reuse the named integer and handle missing arguments without IndexError.

In `@src/synference/library.py`:
- Line 5319: Update the library persistence and loading flow around Grid and
from_library so the configured use_precision is stored with new libraries and
restored when loading them. For legacy libraries without precision metadata,
apply an explicit backward-compatible fallback, while preserving the original
precision rather than forcing np.float32. Add compatibility coverage that
verifies numerical results within appropriate tolerances for both
metadata-bearing and legacy libraries.

In `@src/synference/priors_beta.py`:
- Around line 130-141: The mass_func_at_z path ignores const_phi and leaves the
documented high-redshift behavior unsupported. Implement the Tacchella+18 branch
and blending using _Z_T18, _PHI_T18, _LOGM_T18, and _ALPHA_T18 when
const_phi=False, while preserving the clamped Leja+20 result for const_phi=True;
otherwise remove const_phi from mass_func_at_z and all propagating callers,
delete the unused T18 arrays, and update the module documentation to describe
only the clamped Leja+20 fit.

In `@src/synference/sbi_runner.py`:
- Around line 8331-8344: Normalize 1-D y_test arrays to a parameter-axis shape
immediately after each np.asarray(y_test) call in both diagnostic methods:
src/synference/sbi_runner.py lines 8331-8344 before the per-parameter indexing
in the coverage plotting method, and lines 8422-8433 before y_test[:, None, :]
in the SBC rank computation. Apply the same y_test = y_test[:, None] promotion
used by SBI_Fitter.plot_coverage; no direct change is needed elsewhere.
- Around line 8092-8100: Stop diagnostic methods from applying unanchored “name”
replacement to caller-supplied paths. In src/synference/sbi_runner.py lines
8271-8272, 8364, and 8406, update the metric_path and plots_dir handling in the
relevant methods so substitution occurs only when the value is the default
template; leave the already-resolved plots_dir and metric_path passed by
run_single_sbi at lines 8092-8100 unchanged. Preserve template expansion while
writing explicit paths exactly as supplied.
- Around line 8149-8153: Update the load logic near self.has_features and the
restored feature array so the flag reflects whether feature_array was actually
restored, including missing metadata and has_grid=False cases. Replace the
unconditional True assignment with a value derived from the loaded array,
preserving downstream create_priors and test_in_distribution_pyod behavior.
- Around line 8369-8378: Update Simformer_Fitter.plot_coverage to match the
public SBI_Fitter.plot_coverage API, including compatibility with keywords such
as X, y, and sample_method while preserving existing Simformer-specific
behavior. Ensure base-API callers no longer receive TypeError, or explicitly
document and implement it as a separate API if signature parity is not intended.

In `@src/synference/simformer/masks.py`:
- Around line 66-68: Update random_condition_mask() so the Beta-distributed
probability is sampled through the provided generator rather than Beta.sample(),
which cannot accept one. Preserve the existing alpha/beta distribution semantics
and use generator for all random draws, including the subsequent mask
generation, so seeded calls are reproducible.

In `@src/synference/simformer/model.py`:
- Around line 403-416: Update constraint_mask conversion in sample_intervals to
create the boolean tensor directly on self.device, matching condition_mask and
the tensors used by _expand_bounds. Then remove the redundant
constraint_mask_dev conversion and ensure downstream uses reference the
device-correct constraint_mask.
- Around line 554-557: Update checkpoint loading around torch.load in the
Simformer load method to avoid unrestricted deserialization: use
weights_only=True with the minimal allowlist covering NumPy values and plain
meta and sampling_defaults when the saved payload supports it, or explicitly
document that path must be trusted if unrestricted loading remains necessary.
Before calling payload.get, validate that the loaded payload is a dictionary and
raise the documented ValueError for non-dict payloads; preserve the existing
SAVE_FORMAT_VERSION validation for valid dictionaries.

In `@src/synference/simformer/train.py`:
- Around line 278-283: Update the validation flow around data_val and its
compute_loss call to process data_val in chunks no larger than
training_batch_size, rather than one full forward pass. Compute each chunk’s
loss and average the chunk losses for validation, preserving the existing
behavior when data_val is absent and ensuring all chunks use the same device and
loss computation path.

In `@tests/test_simformer.py`:
- Around line 567-576: Update test_artifacts_written to avoid requiring exactly
600 completed steps when early stopping is enabled: assert that
stats["steps_run"] is within the configured training bounds, or adjust the
trained_fitter configuration to disable early stopping. Preserve the existing
artifact and plot assertions.

---

Outside diff comments:
In `@src/synference/sbi_runner.py`:
- Around line 7909-7924: Update Simformer_Fitter.load_saved_model to preserve
the inherited positional contract: accept arguments in the base order
(model_file, library_path, model_name, load_arrays), handle load_arrays
explicitly, and forward it appropriately during model initialization. Keep the
existing model loading behavior while ensuring positional callers bind each
value correctly.

---

Nitpick comments:
In `@examples/library_generation/scripts/prospector_alpha_library_generation.py`:
- Around line 295-307: Update the Z_dists construction to honor mask in the same
way as sfh_models: iterate over the model indices, create a ZDist.DeltaConstant
only when mask[i] is true, and store None otherwise. Preserve the existing
log_zmet mapping and tqdm behavior while ensuring every rank avoids allocating
unused ZDist objects.

In `@src/synference/__init__.py`:
- Around line 31-47: Replace the eager priors_beta and priors_alpha imports in
the package initializer with a module-level __getattr__-based lazy import,
following the existing guarded sbi_runner pattern. Preserve the same public
helper names and load each priors module only when one of its exported symbols
is accessed, avoiding priors-related file I/O and heavy dependencies during
import synference.

In `@src/synference/priors_alpha.py`:
- Around line 76-80: The LatinHypercube rng/seed compatibility logic is
duplicated across three sampling sites; add one private shared helper, such as
_lhc(d, rng), that performs the signature detection and constructs
qmc.LatinHypercube, then replace the inline blocks in
sample_dirichlet_sfr_fractions and sample_dust_ratio in
src/synference/priors_alpha.py#L76-L80 and `#L111-L115`, and
sample_prospector_beta_prior in src/synference/priors_beta.py#L431-L436 with
calls to that helper.
- Around line 43-54: The docstring summary of dirichlet_zfraction_beta_params
incorrectly says it draws Dirichlet SFR fractions; update it to state that the
function returns per-latent-variable Beta shape parameters for the
stick-breaking z_fraction variables. Preserve the existing parameter description
and implementation.

In `@src/synference/priors_beta.py`:
- Around line 466-502: Optimize the per-galaxy loop by hoisting the constant
cosmo.lookback_time(15) out of _agebins_for_expectation and creating one
age/lookback-time interpolator over the existing redshift grid, following
_make_age_to_redshift. Pass that interpolator into expected_logsfr_ratios and
beta_agebins so they avoid scalar cosmology evaluations while preserving
results. Also precompute cdf_mass_func_at_z on z_grid and interpolate by
redshift instead of rebuilding the 1000-point CDF for each galaxy.
- Around line 171-192: Update setup_mass_normalization to remove the unused
cosmo parameter from its signature, then adjust its call site near the existing
invocation around line 440 to match the new positional arguments. Keep the
normalization calculations and return values unchanged.

In `@src/synference/simformer/model.py`:
- Around line 340-354: Extract the duplicated sde/ode sampler dispatch into a
private helper near the model sampling methods, preserving the existing
euler_maruyama_reverse and heun_probability_flow arguments and unknown-method
ValueError. Update both sample() and the shown sampling flow to call this
helper, passing generator through for SDE sampling.
- Line 199: Cache the node-ID tensor during model initialization alongside the
NumPy array, move the cached tensor in the model’s to() method, and update
score() to reuse this tensor instead of calling torch.as_tensor on every
invocation. Preserve the existing device placement behavior by ensuring the
cached tensor follows self.device.

In `@tests/test_simformer.py`:
- Around line 405-427: Mark the test classes TestTwoMoons and
TestSimformerFitterLibrary with pytest.mark.slow so tests depending on the
two_moons_model and trained_fitter fixtures are deselected together; register
the slow marker in pyproject.toml.
- Around line 458-500: Harden the statistical assertions in
test_posterior_calibration_tarp and test_arbitrary_conditionals against
platform-dependent sampling and training variation. Prefer increasing the TARP
test simulations and/or posterior draws, and widen the mean/std tolerances for
the conditional and joint checks while preserving the intended distributional
validation.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 89070702-1f2e-4734-b145-e26dff1fa9c4

📥 Commits

Reviewing files that changed from the base of the PR and between 0e24fd6 and 9043774.

📒 Files selected for processing (36)
  • CLAUDE.md
  • README.md
  • docs/source/advanced_topics/simformer.ipynb
  • docs/source/getting_started/installation.rst
  • docs/source/library_gen/basic_library_generation.ipynb
  • docs/source/library_gen/complex_library_generation.ipynb
  • examples/library_generation/scripts/batch_library_generation.py
  • examples/library_generation/scripts/final_library_generation.py
  • examples/library_generation/scripts/final_library_generation_multinode.py
  • examples/library_generation/scripts/fixed_params.py
  • examples/library_generation/scripts/prospector_alpha_library_generation.py
  • examples/library_generation/scripts/prospector_beta_library_generation.py
  • examples/online/scripts/online_learning.py
  • examples/sbi/scripts/train_model.py
  • examples/sbi/scripts/train_sphinx_model.py
  • examples/simformer/scripts/fit_simformer_model.py
  • examples/simformer/scripts/two_moons_demo.py
  • pyproject.toml
  • src/synference/__init__.py
  • src/synference/library.py
  • src/synference/prior_data/__init__.py
  • src/synference/prior_data/behroozi_19_sfrd.txt
  • src/synference/prior_data/gallazzi_05_massmet.txt
  • src/synference/priors_alpha.py
  • src/synference/priors_beta.py
  • src/synference/sbi_runner.py
  • src/synference/simformer.py
  • src/synference/simformer/__init__.py
  • src/synference/simformer/masks.py
  • src/synference/simformer/model.py
  • src/synference/simformer/nn.py
  • src/synference/simformer/sampling.py
  • src/synference/simformer/sde.py
  • src/synference/simformer/train.py
  • src/synference/utils.py
  • tests/test_simformer.py
💤 Files with no reviewable changes (5)
  • pyproject.toml
  • README.md
  • docs/source/getting_started/installation.rst
  • examples/sbi/scripts/train_model.py
  • src/synference/simformer.py

Comment thread src/synference/library.py Resolved
Comment thread src/synference/priors_beta.py Resolved
Comment on lines 8092 to 8100
self.plot_diagnostics(
task=task,
X_test=test_x,
y_test=theta_val,
posteriors=trained_score_model,
num_samples=1000,
num_evaluations=25,
X_test=x_test,
y_test=theta_test,
posteriors=trained_model,
num_samples=num_posterior_draws_per_sample,
rng_seed=random_seed,
plots_dir=f"{code_path}/models/{self.name}/plots/{name_append}/",
metric_path=f"{code_path}/models/{self.name}/{self.name}_{name_append}_metrics.json",
plots_dir=os.path.join(out_dir, "plots", name_append.lstrip("_")),
metric_path=os.path.join(out_dir, f"{self.name}{name_append}_metrics.json"),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Unanchored .replace("name", self.name) corrupts resolved output paths. The diagnostic methods treat the literal substring "name" as a placeholder in their default template paths. run_single_sbi now builds absolute paths from out_dir and passes them in, so the substitution rewrites any directory component that happens to contain "name" (for example /home/username/...). Plots and metrics are then written to the wrong location or the write fails. The single fix is to stop rewriting caller-supplied paths: substitute only when the value still equals the default template, or switch the templates to a distinct token such as {name} and use str.format.

  • src/synference/sbi_runner.py#L8092-L8100: pass the already-resolved plots_dir and metric_path as final paths and rely on the callees no longer rewriting them.
  • src/synference/sbi_runner.py#L8271-L8272: replace metric_path.replace("name", self.name) with a substitution that only applies to the default template value.
  • src/synference/sbi_runner.py#L8364-L8364: apply the same change to plots_dir.replace("name", self.name) in plot_sample_accuracy.
  • src/synference/sbi_runner.py#L8406-L8406: apply the same change to plots_dir.replace("name", self.name) in plot_coverage.
📍 Affects 1 file
  • src/synference/sbi_runner.py#L8092-L8100 (this comment)
  • src/synference/sbi_runner.py#L8271-L8272
  • src/synference/sbi_runner.py#L8364-L8364
  • src/synference/sbi_runner.py#L8406-L8406
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/sbi_runner.py` around lines 8092 - 8100, Stop diagnostic
methods from applying unanchored “name” replacement to caller-supplied paths. In
src/synference/sbi_runner.py lines 8271-8272, 8364, and 8406, update the
metric_path and plots_dir handling in the relevant methods so substitution
occurs only when the value is the default template; leave the already-resolved
plots_dir and metric_path passed by run_single_sbi at lines 8092-8100 unchanged.
Preserve template expansion while writing explicit paths exactly as supplied.

Comment thread src/synference/simformer/masks.py Resolved
Comment thread src/synference/simformer/model.py Resolved
Comment on lines +554 to +557
payload = torch.load(path, map_location="cpu", weights_only=False)
version = payload.get("format_version")
if version != SAVE_FORMAT_VERSION:
raise ValueError(f"Unsupported Simformer save format version: {version}.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check torch version, weights_only default, and add_safe_globals availability.
fd -t f 'pyproject.toml' -x rg -n 'torch' {}

python - <<'PY'
import inspect, torch
print("torch", torch.__version__)
print(inspect.signature(torch.load))
print("add_safe_globals:", hasattr(torch.serialization, "add_safe_globals"))
PY

Repository: synthesizer-project/synference

Length of output: 302


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- model.py structure ---'
ast-grep outline src/synference/simformer/model.py
printf '%s\n' '--- load implementation and docstring ---'
sed -n '480,575p' src/synference/simformer/model.py
printf '%s\n' '--- save payload construction ---'
rg -n -C 8 'format_version|sampling_defaults|torch\.save|meta' src/synference/simformer/model.py
printf '%s\n' '--- load callers and tests ---'
rg -n -C 3 '\.load\(|Simformer\.load|load\(' src tests 2>/dev/null | head -240
printf '%s\n' '--- torch dependency declarations ---'
rg -n -C 3 'torch|python' pyproject.toml setup.cfg setup.py requirements* 2>/dev/null || true

Repository: synthesizer-project/synference

Length of output: 28649


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- load completion ---'
sed -n '540,582p' src/synference/simformer/model.py
printf '%s\n' '--- SimformerModel construction and metadata sources ---'
rg -n -C 6 'SimformerModel\(|meta=|sampling_defaults=|self\.meta|self\.sampling_defaults|config\s*=' src tests | head -320
printf '%s\n' '--- configuration definitions ---'
rg -n -C 8 'class ScoreTransformer|def __init__|self\.config|class .*SDE|self\.config' src/synference/simformer

Repository: synthesizer-project/synference

Length of output: 40048


🌐 Web query:

PyTorch torch.load weights_only=False arbitrary code execution add_safe_globals official documentation

💡 Result:

In PyTorch, setting weights_only=False in torch.load is inherently insecure when processing data from untrusted sources because it relies on the standard Python pickle module [1][2][3]. This allows an attacker to craft malicious files that can execute arbitrary code upon deserialization [1][2]. Official guidance and security best practices: 1. Use weights_only=True: The recommended default is to set weights_only=True [2][4]. This mode restricts the unpickler to loading only safe types, such as tensors, primitive types, and dictionaries [1][3]. 2. Allowlisting with add_safe_globals: If you must load custom types or functions that are not supported by the default weights_only=True restricted unpickler, you can explicitly mark them as safe using torch.serialization.add_safe_globals [5][6][3]. This function accepts a list of callables (functions or classes) or tuples of (callable, string_path) to allowlist them for secure loading [5][6]. 3. Context Manager: Alternatively, you can use the torch.serialization.safe_globals context manager to temporarily allow specific globals during a load operation [5][6]. 4. Avoid Implicit Defaults: PyTorch has issued warnings for relying on default settings that effectively use weights_only=False [2]. Always explicitly set the weights_only argument to avoid ambiguity and potential security risks [2]. If you must use weights_only=False, ensure that the data source is entirely trusted and verified [1][3]. Loading files from unknown or untrusted sources when this flag is set is considered a critical security vulnerability [1][7][2].

Citations:


Restrict or document checkpoint loading.

torch.load(..., weights_only=False) can execute arbitrary code from an untrusted checkpoint. Use weights_only=True with a narrow allowlist when all saved values are safe, including NumPy values and plain meta and sampling_defaults; otherwise document that path must be trusted. Reject non-dict payloads with the documented ValueError before calling .get.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/model.py` around lines 554 - 557, Update checkpoint
loading around torch.load in the Simformer load method to avoid unrestricted
deserialization: use weights_only=True with the minimal allowlist covering NumPy
values and plain meta and sampling_defaults when the saved payload supports it,
or explicitly document that path must be trusted if unrestricted loading remains
necessary. Before calling payload.get, validate that the loaded payload is a
dictionary and raise the documented ValueError for non-dict payloads; preserve
the existing SAVE_FORMAT_VERSION validation for valid dictionaries.

Source: Linters/SAST tools

Comment thread src/synference/simformer/train.py Resolved
Comment thread tests/test_simformer.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/synference/priors_beta.py (1)

144-151: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Integrate over the supplied mass grid.

np.cumsum(pdf) ignores log_mass_grid spacing. A non-uniform sorted grid produces an incorrect CDF and biased sampled masses. Use cumulative_trapezoid(pdf, x=log_mass_grid, initial=0.0) before normalization.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/priors_beta.py` around lines 144 - 151, Update
cdf_mass_func_at_z to compute the cumulative integral with cumulative_trapezoid
using log_mass_grid as the x-coordinate and an initial zero before normalizing.
Preserve the existing clipping and endpoint assignments, while ensuring
normalization uses the integrated CDF for non-uniform sorted grids.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/library_generation/scripts/prospector_beta_library_generation.py`:
- Around line 184-199: Update the module-level transforms _tau_v_ism_to_av,
_av_to_tau_v_ism, and _av_to_tau_v_birth to be self-contained when persisted and
reloaded by removing their dependency on the undefined av_to_tau_v symbol. Embed
the 1.086 conversion constant in each affected calculation while preserving the
existing transform behavior.

In `@src/synference/simformer/masks.py`:
- Around line 67-71: Validate alpha and beta immediately before the
_beta_dist.ppf call, rejecting any non-finite or non-positive shape parameter
with an appropriate error. Preserve the generator-seeded uniform sampling and
only call ppf after both parameters pass validation.

In `@src/synference/simformer/model.py`:
- Around line 563-565: Update the payload validation in the Simformer loading
method around torch.load so dictionary payloads are checked for all required
fields, including model_config, immediately after the format-version check.
Raise ValueError for any missing required key before later indexing, preserving
the documented malformed-payload error contract.

---

Outside diff comments:
In `@src/synference/priors_beta.py`:
- Around line 144-151: Update cdf_mass_func_at_z to compute the cumulative
integral with cumulative_trapezoid using log_mass_grid as the x-coordinate and
an initial zero before normalizing. Preserve the existing clipping and endpoint
assignments, while ensuring normalization uses the integrated CDF for
non-uniform sorted grids.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6c9e9b5c-badb-4594-8206-ffeb46cc9caa

📥 Commits

Reviewing files that changed from the base of the PR and between 9043774 and 4b77484.

📒 Files selected for processing (9)
  • examples/library_generation/scripts/prospector_alpha_library_generation.py
  • examples/library_generation/scripts/prospector_beta_library_generation.py
  • src/synference/library.py
  • src/synference/priors_beta.py
  • src/synference/sbi_runner.py
  • src/synference/simformer/masks.py
  • src/synference/simformer/model.py
  • src/synference/simformer/train.py
  • tests/test_simformer.py
🚧 Files skipped from review as they are similar to previous changes (4)
  • examples/library_generation/scripts/prospector_alpha_library_generation.py
  • tests/test_simformer.py
  • src/synference/simformer/train.py
  • src/synference/sbi_runner.py

Comment on lines +184 to +199
# Module-level (picklable) parameter transforms used by GalaxyBasis/create_mock_library,
# which run with n_proc worker processes and an optional multi_node path.
def _tau_v_ism_to_av(x):
return x["tau_v_ism"] * av_to_tau_v


def _tau_v_birth_to_dust_ratio(x):
return x["tau_v_birth"] / x["tau_v_ism"]


def _av_to_tau_v_ism(x):
return x["Av"] / av_to_tau_v


def _av_to_tau_v_birth(x):
return (x["Av"] / av_to_tau_v) * x["dust_ratio"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- conversion constant declarations ---'
rg -n -C 2 '^\s*av_to_tau_v\s*=' \
  examples/library_generation/scripts/prospector_beta_library_generation.py \
  src/synference/library.py || true

printf '%s\n' '--- transform serialization and restoration ---'
rg -n -C 4 'getsource\(value\[1\]\)|exec\(code' src/synference/library.py

Repository: synthesizer-project/synference

Length of output: 2281


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- source module context ---'
sed -n '165,205p' examples/library_generation/scripts/prospector_beta_library_generation.py
printf '%s\n' '--- library module imports and matching identifiers ---'
sed -n '1,120p' src/synference/library.py
rg -n -C 3 '\bav_to_tau_v\b|def from_library|transforms_group|exec\(code' src/synference/library.py examples/library_generation/scripts/prospector_beta_library_generation.py
printf '%s\n' '--- restoration context ---'
sed -n '5500,5570p' src/synference/library.py
printf '%s\n' '--- exact persisted-transform call sites ---'
sed -n '2110,2150p' src/synference/library.py

Repository: synthesizer-project/synference

Length of output: 15131


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
import ast
from pathlib import Path

example = Path("examples/library_generation/scripts/prospector_beta_library_generation.py").read_text()
library = Path("src/synference/library.py").read_text()

example_tree = ast.parse(example)
functions = {
    node.name: node
    for node in example_tree.body
    if isinstance(node, ast.FunctionDef)
    and node.name in {
        "_tau_v_ism_to_av",
        "_tau_v_birth_to_dust_ratio",
        "_av_to_tau_v_ism",
        "_av_to_tau_v_birth",
    }
}

library_tree = ast.parse(library)
library_globals = {
    node.targets[0].id
    for node in library_tree.body
    if isinstance(node, ast.Assign)
    and len(node.targets) == 1
    and isinstance(node.targets[0], ast.Name)
}
print("library defines av_to_tau_v:", "av_to_tau_v" in library_globals)

for name, node in functions.items():
    source = ast.unparse(node)
    names = {
        child.id
        for child in ast.walk(node)
        if isinstance(child, ast.Name) and isinstance(child.ctx, ast.Load)
    }
    print(f"{name}: references av_to_tau_v =", "av_to_tau_v" in names)

    namespace = {}
    exec(source, library_globals, namespace)
    func = namespace[name]
    try:
        samples = {
            "_tau_v_ism_to_av": {"tau_v_ism": 2.0},
            "_tau_v_birth_to_dust_ratio": {
                "tau_v_birth": 2.0,
                "tau_v_ism": 1.0,
            },
            "_av_to_tau_v_ism": {"Av": 2.0},
            "_av_to_tau_v_birth": {"Av": 2.0, "dust_ratio": 0.5},
        }
        print(f"  call result: {func(samples[name])}")
    except Exception as exc:
        print(f"  call error: {type(exc).__name__}: {exc}")
PY

Repository: synthesizer-project/synference

Length of output: 375


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
import ast
from pathlib import Path

example_tree = ast.parse(
    Path("examples/library_generation/scripts/prospector_beta_library_generation.py").read_text()
)
library_tree = ast.parse(Path("src/synference/library.py").read_text())

wanted = {
    "_tau_v_ism_to_av",
    "_tau_v_birth_to_dust_ratio",
    "_av_to_tau_v_ism",
    "_av_to_tau_v_birth",
}
functions = {
    node.name: node
    for node in example_tree.body
    if isinstance(node, ast.FunctionDef) and node.name in wanted
}

has_constant = any(
    isinstance(node, (ast.Assign, ast.AnnAssign))
    and "av_to_tau_v" in ast.unparse(node)
    for node in library_tree.body
)
print("library defines av_to_tau_v:", has_constant)

samples = {
    "_tau_v_ism_to_av": {"tau_v_ism": 2.0},
    "_tau_v_birth_to_dust_ratio": {"tau_v_birth": 2.0, "tau_v_ism": 1.0},
    "_av_to_tau_v_ism": {"Av": 2.0},
    "_av_to_tau_v_birth": {"Av": 2.0, "dust_ratio": 0.5},
}

for name, node in functions.items():
    source = ast.unparse(node)
    loaded_names = {
        child.id
        for child in ast.walk(node)
        if isinstance(child, ast.Name) and isinstance(child.ctx, ast.Load)
    }
    print(f"{name}: references av_to_tau_v =", "av_to_tau_v" in loaded_names)

    # Match exec(code, library_globals, locals()) without executing repository code.
    library_globals = {"__builtins__": __builtins__}
    local_namespace = {}
    exec(source, library_globals, local_namespace)
    func = local_namespace[name]
    try:
        print(f"  call result: {func(samples[name])}")
    except Exception as exc:
        print(f"  call error: {type(exc).__name__}: {exc}")
PY

Repository: synthesizer-project/synference

Length of output: 604


Make persisted parameter transforms self-contained

If a generated library is reloaded, three transforms raise NameError because src/synference/library.py does not define av_to_tau_v. Embed 1.086 in the transforms or persist the conversion constant with them.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/library_generation/scripts/prospector_beta_library_generation.py`
around lines 184 - 199, Update the module-level transforms _tau_v_ism_to_av,
_av_to_tau_v_ism, and _av_to_tau_v_birth to be self-contained when persisted and
reloaded by removing their dependency on the undefined av_to_tau_v symbol. Embed
the 1.086 conversion constant in each affected calculation while preserving the
existing transform behavior.

Comment on lines +67 to +71
# torch.distributions.Beta.sample() cannot accept a generator, so the
# Beta draw is done via inverse-CDF from a generator-seeded uniform
# instead — otherwise a seeded `generator` wouldn't make this reproducible.
u = torch.rand((), generator=generator).item()
prob = float(_beta_dist.ppf(u, alpha, beta))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- masks.py outline ---'
ast-grep outline src/synference/simformer/masks.py --match random_condition_mask --view expanded || true

printf '%s\n' '--- masks.py relevant lines ---'
cat -n src/synference/simformer/masks.py | sed -n '1,110p'

printf '%s\n' '--- call sites ---'
rg -n -C 3 'random_condition_mask|alpha\s*=|beta\s*=' src tests 2>/dev/null | head -240 || true

printf '%s\n' '--- dependency declarations ---'
rg -n -C 2 'scipy|torch' pyproject.toml setup.cfg setup.py requirements*.txt 2>/dev/null || true

printf '%s\n' '--- behavioral probe ---'
python3 - <<'PY'
import math
try:
    import torch
    from scipy.stats import beta as beta_dist
except Exception as exc:
    print(f"IMPORT_ERROR: {type(exc).__name__}: {exc}")
    raise SystemExit(0)

cases = [
    (1.0, 1.0),
    (0.0, 1.0),
    (-1.0, 1.0),
    (float("nan"), 1.0),
    (float("inf"), 1.0),
    (1.0, 0.0),
    (1.0, -1.0),
    (1.0, float("nan")),
    (1.0, float("inf")),
]
for alpha, b in cases:
    try:
        values = [beta_dist.ppf(u, alpha, b) for u in (0.0, 0.5, 1.0)]
        print(repr((alpha, b)), repr(values), "finite:", [math.isfinite(float(v)) for v in values])
    except Exception as exc:
        print(repr((alpha, b)), "ERROR", type(exc).__name__, str(exc))

for alpha, b in cases:
    try:
        u = 0.5
        prob = float(beta_dist.ppf(u, alpha, b))
        mask_value = 0.25 < prob
        print("comparison", repr((alpha, b)), repr(prob), repr(mask_value))
    except Exception as exc:
        print("comparison", repr((alpha, b)), "ERROR", type(exc).__name__, str(exc))
PY

Repository: synthesizer-project/synference

Length of output: 20292


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- SciPy Beta behavior without torch ---'
python3 - <<'PY'
import math
from scipy.stats import beta as beta_dist

cases = [
    (1.0, 1.0),
    (0.0, 1.0),
    (-1.0, 1.0),
    (float("nan"), 1.0),
    (float("inf"), 1.0),
    (1.0, 0.0),
    (1.0, -1.0),
    (1.0, float("nan")),
    (1.0, float("inf")),
]
for alpha, beta in cases:
    try:
        values = [float(beta_dist.ppf(u, alpha, beta)) for u in (0.0, 0.5, 1.0)]
        print(
            repr((alpha, beta)),
            "ppf=", repr(values),
            "finite=", [math.isfinite(v) for v in values],
            "mid_comparison=", repr(0.25 < values[1]),
        )
    except Exception as exc:
        print(repr((alpha, beta)), "ERROR", type(exc).__name__, str(exc))
PY

printf '%s\n' '--- direct random_condition_mask references ---'
rg -n -C 5 'random_condition_mask' . --glob '*.py' --glob '*.pyi' --glob '*.md' 2>/dev/null || true

printf '%s\n' '--- tests and docs for mask parameters ---'
rg -n -C 4 'rnd1_prob|rnd2_prob|Beta\(alpha|condition_mask|random mask|random_condition' tests src README.md docs 2>/dev/null | head -320 || true

Repository: synthesizer-project/synference

Length of output: 328


🌐 Web query:

site:docs.scipy.org scipy.stats.beta shape parameters positive ppf invalid parameters nan

💡 Result:

In scipy.stats.beta, the shape parameters 'a' and 'b' must be strictly positive (a > 0, b > 0) [1][2][3]. When 'a' or 'b' are non-positive, or if other input parameters (like the probability 'q' in the percent point function, ppf) are invalid, SciPy typically returns 'nan' (Not a Number) or raises an error, depending on the specific implementation and version [4]. Historically, there have been documented bugs regarding the accuracy and behavior of beta distribution methods, including the ppf [4][5]. For example, reports of non-monotonicity or unexpected results in ppf and related functions (such as isf) have been addressed in various SciPy releases through updates to the underlying Boost Math C++ library and internal implementation improvements [4][5]. If you are encountering unexpected 'nan' values, it is recommended to ensure that: 1. Shape parameters 'a' and 'b' are positive values [1]. 2. The probability input 'q' for the ppf method is within the valid range [6] [6]. 3. You are using an up-to-date version of SciPy, as recent releases (such as 1.12.0 and later) have included significant improvements to parameter handling, nan support, and the accuracy of statistical methods [4]. If parameters are valid and you still receive unexpected results, consider checking the specific SciPy version and referencing the release notes for known bugs or behavioral changes [7][4][5].

Citations:


Validate the Beta shape parameters before calling ppf.

Reject non-finite or non-positive alpha and beta. Invalid parameters can produce a non-finite prob, causing the comparison to silently create an all-False mask.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/masks.py` around lines 67 - 71, Validate alpha and
beta immediately before the _beta_dist.ppf call, rejecting any non-finite or
non-positive shape parameter with an appropriate error. Preserve the
generator-seeded uniform sampling and only call ppf after both parameters pass
validation.

Source: MCP tools

Comment on lines +563 to +565
payload = torch.load(path, map_location="cpu", weights_only=False)
if not isinstance(payload, dict):
raise ValueError(f"Unsupported Simformer save format version: {payload!r}.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Validate required payload fields.

Line 564 rejects only non-dictionary payloads. A dictionary with the current format version but without model_config or another required field reaches later indexing and raises KeyError. This is a malformed save payload but violates the documented ValueError contract. Validate required keys after the version check.

Proposed fix
         version = payload.get("format_version")
         if version != SAVE_FORMAT_VERSION:
             raise ValueError(f"Unsupported Simformer save format version: {version}.")
+        required_keys = {
+            "state_dict",
+            "model_config",
+            "sde_config",
+            "sde_x0_mean",
+            "sde_x0_var",
+            "z_score_mean",
+            "z_score_std",
+            "theta_dim",
+            "x_dim",
+            "node_ids",
+            "base_mask",
+            "meta",
+            "sampling_defaults",
+        }
+        missing_keys = required_keys.difference(payload)
+        if missing_keys:
+            raise ValueError(
+                f"Malformed Simformer payload; missing keys: {sorted(missing_keys)}."
+            )
         net = ScoreTransformer(**payload["model_config"])
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
payload = torch.load(path, map_location="cpu", weights_only=False)
if not isinstance(payload, dict):
raise ValueError(f"Unsupported Simformer save format version: {payload!r}.")
version = payload.get("format_version")
if version != SAVE_FORMAT_VERSION:
raise ValueError(f"Unsupported Simformer save format version: {version}.")
required_keys = {
"state_dict",
"model_config",
"sde_config",
"sde_x0_mean",
"sde_x0_var",
"z_score_mean",
"z_score_std",
"theta_dim",
"x_dim",
"node_ids",
"base_mask",
"meta",
"sampling_defaults",
}
missing_keys = required_keys.difference(payload)
if missing_keys:
raise ValueError(
f"Malformed Simformer payload; missing keys: {sorted(missing_keys)}."
)
net = ScoreTransformer(**payload["model_config"])
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/model.py` around lines 563 - 565, Update the payload
validation in the Simformer loading method around torch.load so dictionary
payloads are checked for all required fields, including model_config,
immediately after the format-version check. Raise ValueError for any missing
required key before later indexing, preserving the documented malformed-payload
error contract.

This branch has not been deployed

No deployments
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.

1 participant