Repository navigation
Add implementation of Prospector alpha & beta showing complex hierarchical priors - #37
tHarvey303 wants to merge 7 commits into
Conversation
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.
📝 WalkthroughWalkthroughThis 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. ChangesNative Simformer implementation
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
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 winPreserve the
load_saved_modelcontract inSimformer_Fitter.
Simformer_Fitterchanges 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 omitsload_arraysand forwards it through**kwargsinstead 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_distsis built for every model, butsfh_modelsrespectsmask.Lines 295-298 construct one
ZDist.DeltaConstantfor allNmodelsentries. Lines 303-307 construct SFH objects only wheremask[i]is True. Apply the same mask to theZDistloop 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 winThe
qmc.LatinHypercubeseed/rng kwarg detection is copied at three sites. Each site runsinspect.signature(qmc.LatinHypercube)to choose between therngandseedkeyword, 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 insample_dirichlet_sfr_fractionswith the helper call.src/synference/priors_alpha.py#L111-L115: replace the inline block insample_dust_ratiowith the helper call.src/synference/priors_beta.py#L431-L436: replace the inline block insample_prospector_beta_priorwith 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 winCorrect the docstring summary.
dirichlet_zfraction_beta_paramsreturns Beta shape parameters, not SFR fractions. The currentBeta(ncomp - i, 1)shapes are correct becauseSFH.Dirichletandprospect.models.transforms.zfrac_to_sfracusesfr_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 liftThe per-galaxy loop calls astropy cosmology functions for every draw.
For each index,
expected_logsfr_ratioscallscosmo.age(zred), and_agebins_for_expectationcallscosmo.lookback_time(zstart)pluscosmo.lookback_time(15).beta_agebinscallscosmo.age(zred)again. Astropy cosmology evaluations are numerically integrated and cost on the order of milliseconds per scalar call. The example script setsNmodels = 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 fixedcosmo.- Build one age/lookback-time interpolator over a redshift grid, in the same way as
_make_age_to_redshift, and pass it intoexpected_logsfr_ratiosandbeta_agebins.
cdf_mass_func_at_zis also recomputed on a 1000-point grid per galaxy. Consider precomputing the CDF on the existingz_gridand interpolating inz.🤖 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_normalizationignores itscosmoargument.The function signature takes
cosmoas 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 valueExtract the sampler dispatch shared with
sample().Lines 340-354 repeat the
sde/odedispatch 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 winCache
node_idsas a tensor instead of rebuilding it on every score call.
score()runs once per integration step. With the defaultnum_steps=500, this line performs 500 numpy→tensor conversions persample()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 winMark the training fixtures as slow.
two_moons_modeltrains for 3000 steps on 10,000 samples, andtrained_fittertrains for 600 steps and then runs full diagnostics. Both run on every defaultpytestinvocation. 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.slowat theTestTwoMoonsandTestSimformerFitterLibraryclass 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 winThese statistical tolerances may be fragile across platforms.
test_posterior_calibration_tarpassertsabs(ecp[mid] - 0.5) < 0.15using only 100 test simulations and 250 posterior draws.test_arbitrary_conditionalsassertsatol=0.1on 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 tradeoffThese eager imports add module-level work to
import synference.
priors_betabuilds module-level state at import time: it loads the packagedgallazzi_05_massmet.txtandbehroozi_19_sfrd.txtfiles and constructs theMASSMETarray and theSPL_TL_SFRDspline. It also pulls inastropy.cosmologyandscipy.interpolate. Importing it from__init__.pymoves that file I/O and those heavy dependencies into everyimport synference.Consider a lazy
__getattr__for these prior helpers, matching the guarded import style already used forsbi_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
📒 Files selected for processing (36)
CLAUDE.mdREADME.mddocs/source/advanced_topics/simformer.ipynbdocs/source/getting_started/installation.rstdocs/source/library_gen/basic_library_generation.ipynbdocs/source/library_gen/complex_library_generation.ipynbexamples/library_generation/scripts/batch_library_generation.pyexamples/library_generation/scripts/final_library_generation.pyexamples/library_generation/scripts/final_library_generation_multinode.pyexamples/library_generation/scripts/fixed_params.pyexamples/library_generation/scripts/prospector_alpha_library_generation.pyexamples/library_generation/scripts/prospector_beta_library_generation.pyexamples/online/scripts/online_learning.pyexamples/sbi/scripts/train_model.pyexamples/sbi/scripts/train_sphinx_model.pyexamples/simformer/scripts/fit_simformer_model.pyexamples/simformer/scripts/two_moons_demo.pypyproject.tomlsrc/synference/__init__.pysrc/synference/library.pysrc/synference/prior_data/__init__.pysrc/synference/prior_data/behroozi_19_sfrd.txtsrc/synference/prior_data/gallazzi_05_massmet.txtsrc/synference/priors_alpha.pysrc/synference/priors_beta.pysrc/synference/sbi_runner.pysrc/synference/simformer.pysrc/synference/simformer/__init__.pysrc/synference/simformer/masks.pysrc/synference/simformer/model.pysrc/synference/simformer/nn.pysrc/synference/simformer/sampling.pysrc/synference/simformer/sde.pysrc/synference/simformer/train.pysrc/synference/utils.pytests/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
| 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"), | ||
| ) |
There was a problem hiding this comment.
🎯 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-resolvedplots_dirandmetric_pathas final paths and rely on the callees no longer rewriting them.src/synference/sbi_runner.py#L8271-L8272: replacemetric_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 toplots_dir.replace("name", self.name)inplot_sample_accuracy.src/synference/sbi_runner.py#L8406-L8406: apply the same change toplots_dir.replace("name", self.name)inplot_coverage.
📍 Affects 1 file
src/synference/sbi_runner.py#L8092-L8100(this comment)src/synference/sbi_runner.py#L8271-L8272src/synference/sbi_runner.py#L8364-L8364src/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.
| 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}.") |
There was a problem hiding this comment.
🔒 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"))
PYRepository: 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 || trueRepository: 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/simformerRepository: 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:
- 1: https://github.com/pytorch/pytorch/blob/main/torch/serialization.py
- 2: Implicitly setting
weights_only=Falsetoload()gets a warning pytorch/pytorch#138007 - 3: https://docs.pytorch.org/docs/stable/generated/torch.load.md
- 4: https://docs.pytorch.org/tutorials/beginner/saving_loading_models.md
- 5: https://docs.pytorch.org/docs/stable/notes/serialization.md
- 6: https://docs.pytorch.org/docs/2.12/notes/serialization.html
- 7: GHSA-63cw-57p8-fm3p
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
There was a problem hiding this comment.
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 winIntegrate over the supplied mass grid.
np.cumsum(pdf)ignoreslog_mass_gridspacing. A non-uniform sorted grid produces an incorrect CDF and biased sampled masses. Usecumulative_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
📒 Files selected for processing (9)
examples/library_generation/scripts/prospector_alpha_library_generation.pyexamples/library_generation/scripts/prospector_beta_library_generation.pysrc/synference/library.pysrc/synference/priors_beta.pysrc/synference/sbi_runner.pysrc/synference/simformer/masks.pysrc/synference/simformer/model.pysrc/synference/simformer/train.pytests/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
| # 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"] |
There was a problem hiding this comment.
🗄️ 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.pyRepository: 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.pyRepository: 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}")
PYRepository: 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}")
PYRepository: 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.
| # 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)) |
There was a problem hiding this comment.
🎯 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))
PYRepository: 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 || trueRepository: 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:
- 1: https://docs.scipy.org/doc/scipy/tutorial/stats/continuous_beta.html
- 2: https://docs.scipy.org/doc/scipy-1.15.2/tutorial/stats/continuous_beta.html
- 3: https://docs.scipy.org/doc/scipy-1.6.0/reference/generated/scipy.stats.beta.html
- 4: https://docs.scipy.org/doc/scipy/release/1.12.0-notes.html
- 5: https://docs.scipy.org/doc/scipy-1.17.0/release/1.7.0-notes.html
- 6: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html
- 7: https://docs.scipy.org/doc/scipy-1.17.0/release/1.15.0-notes.html
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
| 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}.") |
There was a problem hiding this comment.
🎯 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.
| 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.
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:
Summary by CodeRabbit
New Features
Documentation
Chores