sheap.Profiles.Utils module
Profile Utilities
This module provides utility functions to support the definition, integration, and composition of spectral line and continuum profiles in sheap.
Functions
make_fused_profiles(funcs):Combine multiple profile functions into a single callable that evaluates the sum of all components.
with_param_names(param_names):Decorator to attach parameter names and count metadata to profile functions for consistent handling across the codebase.
make_integrator(profile_fn, method="broadcast"):Factory for JAX-based integrators of profile functions. Supports either broadcasting across batches or nested vmap evaluation.
build_grid_penalty(weights_idx, n_Z, n_age):Construct a Laplacian smoothness penalty on 2D grids of host template weights (Z × age), useful for regularization.
trapz_jax(y, x):Lightweight trapezoidal integration implemented with JAX.
Notes
All utilities are JAX-compatible and designed for use in differentiable fitting pipelines.
with_param_namesensures that each profile function exposes.param_namesand.n_paramsattributes for downstream bookkeeping.make_integratorcan be used to integrate profiles per spectrum, per component, or across batches without manual looping.
- build_grid_penalty(weights_idx, n_Z, n_age)[source]
Construct a Laplacian smoothness penalty over a 2D template grid.
- Parameters:
weights_idx (list[int]) – Indices of weight parameters in the global parameter vector.
n_Z (int) – Number of metallicity bins.
n_age (int) – Number of age bins.
- Returns:
penalty – Function (params) -> float computing the smoothness penalty.
- Return type:
callable
- make_fused_profiles(funcs)[source]
Fuse multiple profile functions into a single callable while preserving parameter metadata, including which parameters are linear.
- make_integrator(profile_fn, method='broadcast')[source]
Create an integrator for profile functions. This works for 1D wavelength and 3D params n_sample,n_lines,n_params.
- Parameters:
profile_fn (callable) – Profile function with signature (x, params) -> y.
method ({"broadcast", "vmap"}, optional) – Integration strategy: - “broadcast”: expand x for broadcasting across batches - “vmap”: use nested vectorization
- Returns:
integrate – Function (x, params) -> integral returning integrated flux.
- Return type:
callable
- trapz_jax(y, x)[source]
JAX-compatible trapezoidal integration.
- Parameters:
y (jnp.ndarray) – Function values.
x (jnp.ndarray) – Grid over which integration is performed.
- Returns:
Approximate integral of y over x.
- Return type:
jnp.ndarray
- with_param_names(param_names, linear_param_names=None, linear_param_mask=None, profile_name=None)[source]
Decorate a profile function with parameter metadata.
- Parameters:
param_names (iterable of str) – Ordered parameter names for the function.
linear_param_names (iterable of str, optional) – Names of parameters that enter linearly.
linear_param_mask (iterable of bool, optional) – Boolean mask of the same length as
param_namesindicating which parameters are linear.
- Returns:
Decorated function with attributes:
param_namesn_paramslinear_param_masklinear_param_indicesnonlinear_param_indiceslinear_param_namesnonlinear_param_names
- Return type:
callable
Notes
Provide either
linear_param_namesorlinear_param_mask. If neither is provided, all parameters are assumed nonlinear.