synference.sbi_runner¶
SBI Inference classes for SED fitting.
Functions
- synference.sbi_runner.sample_with_timeout(sampler, nsteps, x, timeout_seconds)[source]¶
Sample with timeout using threading.
Classes
- class synference.sbi_runner.MissingPhotometryHandler(training_photometry, posterior_estimator, run_params=None, photometry_units='AB', uncertainty_models=None, band_names=None, feature_names=None, device='cpu')[source]¶
Handles missing photometry in SEDs using an sbi++ approach.
This class separates the process into two main steps: 1. Imputation: Generating a distribution of plausible, complete photometry vectors. 2. Sampling: Drawing posterior samples from the SBI model for a given set of vectors.
- generate_imputations(obs, true_flux_units=None, out_units=None)[source]¶
Generates a set of plausible, complete observation vectors for an SED with missing data.
- Parameters:
obs (
Dict[str,ndarray]) – Dictionary with observed data (‘mags_sbi’, ‘mags_unc_sbi’, ‘missing_mask’).true_flux_units (
Optional[str]) – Units of the input flux for the noise model.out_units (
Optional[str]) – Desired output units for the noise model.
- Returns:
An array of imputed observation vectors, shape (nmc, n_features).
A dictionary with metadata (‘success’, ‘timeout’, ‘count’).
- Return type:
A tuple containing
- classmethod init_from_synference(synference, **run_params)[source]¶
Initialize from a fitted SBI model.
Parameters:¶
- synferenceobject
Fitted SBI model object
- process_observation(obs, true_flux_units=None, out_units=None)[source]¶
Processes a single observation, handling missing bands and returning posterior samples.
This is a convenience wrapper that calls generate_imputations followed by sample_posterior.
- Return type:
Dict[str,Any]
- sample_posterior(observation_vectors)[source]¶
Draws posterior samples from the SBI model for one or more observation vectors.
- Parameters:
observation_vectors (
Union[ndarray,Tensor]) – A single vector (1D array) or a batch of vectors (2D array) representing one or more complete observations.- Return type:
ndarray- Returns:
An array of posterior samples, shape (n_vectors * nposterior, n_params).
- class synference.sbi_runner.ModelComparison(model1, model2, data)[source]¶
Class for model comparison using e.g. Evidence Networks or Harmonic Evidence.
- class synference.sbi_runner.SBI_Fitter(name, parameter_names, raw_observation_names, raw_observation_grid=None, parameter_array=None, parameter_units=None, raw_observation_units=nJy, simulator=None, feature_array=None, feature_names=None, feature_units=None, library_path=None, supplementary_parameters=None, supplementary_parameter_names=None, supplementary_parameter_units=None, device='cpu', observation_type='photometry')[source]¶
Class to fit a model to the data using the ltu-ili package.
Datasets are loaded from HDF5 files. The data is then split into training and testing sets. Flexible models including ensembles are supported.
- name: str
The name of the model.
- parameter_names: list
The names of the parameters to fit.
- raw_observation_names: list
The names of the photometry filters in the grid.
- raw_observation_grid: np.ndarray
The raw photometry or spectral grid to use for fitting.
- parameter_array: np.ndarray
The parameter array to use for fitting.
- raw_observation_units: list
The units of the raw observation grid.
- simulator: callable
The simulator function to use for generating synthetic data.
- feature_array: np.ndarray
The feature array to use for fitting.
- feature_names: list
The names of the features to use for fitting.
- feature_units: list
The units of the features to use for fitting.
- library_path: str
The path to the library file.
- supplementary_parameters: np.ndarray
Any supplementary parameters to include in the fitting.
- supplementary_parameter_names: list
The names of the supplementary parameters.
- supplementary_parameter_units: list
The units of the supplementary parameters.
- device: str
The device to use for fitting. Default is ‘cuda’ if available, otherwise ‘cpu’.
- observation_type: str
The type of model to use for fitting. Can be ‘photometry’ or ‘spectra’. Default is ‘photometry’.
- bin_noisy_testing_data(X_test=None, y_test=None, snr_bins=[1, 5, 8, 10, inf], snr_feature_names=None, return_indices=False)[source]¶
Bin the testing data based on SNR.
This method calculates the SNR for each feature in the testing data and bins the data based on the provided SNR bins. It returns the binned testing data and the corresponding labels.
- Parameters:
X_test (np.ndarray, optional) – The testing data features. If None, uses the stored _X_test.
y_test (np.ndarray, optional) – The testing data labels. If None, uses the stored _y_test.
snr_bins (list, optional) – The SNR bins to use for binning the data. Default is [5, 8, 10].
snr_feature_names (list, optional) – The feature names corresponding to the SNR features. If None, it will use the all the photomety feature names from the feature array.
return_indices (bool, optional) – If True, return the indices of the binned data instead of the data itself.
- calculate_PIT(X, y, num_samples=1000, posteriors=None, samples=None)[source]¶
Calculate the probability integral transform (PIT) for the samples.
- Parameters:
X (2-dimensional array of shape (num_samples, n_features)) – Feature array.
y (1-dimensional array of shape (num_samples,)) – Target variable.
Returns
-------
pit (1-dimensional array) – The PIT values.
- Return type:
ndarray
- calculate_TARP(X, y, num_samples=1000, posteriors=None, num_bootstrap=200, samples=None)[source]¶
Calculate Tests of Accuracy with Random Points (TARP) for the samples.
- Parameters:
X (2-dimensional array of shape (num_samples, n_features)) – Feature array.
y (1-dimensional array of shape (num_samples,)) – Target variable.
Returns
-------
tarp (1-dimensional array) – The TARP values.
- Return type:
ndarray
- create_dataframe(data='all')[source]¶
Create a DataFrame from the training data.
Parameters:¶
- datastr
‘all’ to include all data, ‘photometry’ for only photometry, ‘parameters’ for only parameters
- create_feature_array(flux_units='AB', extra_features=None, **kwargs)[source]¶
Create a feature array from the raw observation library.
A simpler wrapper for create_feature_array_from_raw_photometry with default values. This function will create a feature array from the raw observation library with no noise, and all photometry in mock catalogue used.
- create_feature_array_from_raw_photometry(normalize_method=None, extra_features=None, normed_flux_units='AB', normalization_unit='AB', verbose=True, scatter_fluxes=False, empirical_noise_models=None, depths=None, include_errors_in_feature_array=False, min_flux_pc_error=0.0, simulate_missing_fluxes=False, missing_flux_value=99.0, missing_flux_fraction=0.0, missing_flux_options=None, include_flags_in_feature_array=False, override_phot_grid=None, override_phot_grid_units=None, norm_mag_limit=50.0, remove_nan_inf=True, parameters_to_remove=None, photometry_to_remove=None, parameters_to_add=None, drop_dropouts=False, drop_dropout_fraction=1.0, asinh_softening_parameters=None, max_rows=-1, parameter_transformations=None)[source]¶
Create a feature array from the raw observation library.
- Parameters:
normalize_method (str, optional) – Method to normalize photometry (e.g., filter names, parameter names). Used to normalize fluxes. Defaults to None.
extra_features (list, optional) – Extra features to add. Can be functions of filter codes (e.g., [‘F090W - F115W’]) or parameters from the library (e.g., redshift). Defaults to None.
normed_flux_units (str, optional) – Target units for normalized flux (e.g., “AB”, “asinh”, “nJy”). Fluxes will be relative to the normalization filter in these units. Defaults to “AB”.
normalization_unit (str, optional) – Units for the normalization factor (e.g., ‘log10 nJy’, ‘nJy’, ‘AB’). Can differ from normed_flux_units. Defaults to “AB”.
verbose (bool, optional) – If True, prints progress and summary information. Defaults to True.
scatter_fluxes (Union[int, bool], optional) – Whether to scatter fluxes with uncertainty. If False, no scatter. If an integer, applies that many scatters using noise models or depths. Defaults to False.
empirical_noise_models (Dict[str, EmpiricalUncertaintyModel], optional) – Dictionary mapping filter names to EmpiricalUncertaintyModel objects for flux scattering. Defaults to None.
depths (unyt_array, optional) – An array of depths, one per photometric filter. Defaults to None.
include_errors_in_feature_array (bool, optional) – If True, includes the RMS uncertainty as a separate feature in the input. Defaults to False.
min_flux_pc_error (float, optional) – Minimum percentage error to apply to fluxes when scattering. Defaults to 0.0.
simulate_missing_fluxes (bool, optional) – If True, simulates missing photometry for a fraction of data, marked with missing_flux_value. Defaults to False.
missing_flux_value (float, optional) – Value to use for missing fluxes. Defaults to 99.0.
missing_flux_fraction (float, optional) – Fraction of missing fluxes to simulate. Defaults to 0.0.
missing_flux_options (list, optional) – A list of predefined masks for missing fluxes, used if simulate_missing_fluxes is True. Defaults to None.
include_flags_in_feature_array (bool, optional) – If True, includes flags in the feature array. Defaults to False.
override_phot_grid (np.ndarray, optional) – A photometry grid to use instead of the raw observation grid. Defaults to None.
override_phot_grid_units (str, optional) – Units for the override_phot_grid. Defaults to None.
norm_mag_limit (float, optional) – Maximum magnitude limit for normalized fluxes. Defaults to 50.0.
remove_nan_inf (bool, optional) – If True, removes rows with NaN or Inf values from the feature array. Defaults to True.
parameters_to_remove (list, optional) – List of parameters to remove from the parameter array. Defaults to None.
photometry_to_remove (list, optional) – List of filters to remove. This is applied early, so other arguments (like depths) should not include these filters. Defaults to None.
parameters_to_add (list, optional) – List of supplementary parameters to add to the feature array. Defaults to None.
drop_dropouts (bool, optional) – If True, drops dropouts from the feature array. Defaults to False.
drop_dropout_fraction (float, optional) – The fraction of filters a galaxy can drop out in before being dropped. Defaults to 1.0.
asinh_softening_parameters (Union[unyt_array, List, Dict, str], optional) – Softening parameter for ‘asinh’ normalization. Can be a single quantity, list, dict, or string (e.g., ‘SNR_10’). Defaults to None.
max_rows (int, optional) – Maximum number of rows to return. If -1, all rows are returned. If > 0, rows are randomly sampled. Defaults to -1.
parameter_transformations (Dict[str, Callable], optional) – Dictionary of functions to apply to parameters (e.g., {‘age’: np.log10} to infer log_age). Defaults to None.
- Returns:
feature_array (np.ndarray): The processed feature array.
feature_names (List[str]): The names of the features (columns) in the feature array.
- Return type:
tuple
- create_feature_array_from_raw_spectra(extra_features=['redshift'], crop_wavelength_range=None, normed_flux_units='AB', flux_norm_method=None, flux_norm_parameters=None, parameters_to_remove=None, parameters_to_add=None, parameter_transformations=None, resample_wavelengths=None, inst_resolution_wavelengths=None, inst_resolution_r=None, theory_r=inf, min_flux_value=-inf, max_flux_value=inf)[source]¶
Create a feature array from the raw spectral library.
Currently only support basic scaling and cropping
- Parameters:
extra_features (list, optional) – Any extra features to be added from the parameter array. e.g. redshift
crop_wavelength_range (unyt_array (in wavelength units) of length 2: (min, max)) – A tuple or list of two values specifying the wavelength range to crop the spectra to. Should be given in microns (um). This will crop in the observed frame if ‘redshift’ is a feature.
crop_wavelength_range – wavelength range to crop the spectra to.
normed_flux_units (str, optional) – The units of the flux to normalize to. E.g. ‘AB’, ‘nJy’, etc. If it starts with ‘log10 ‘, it will be treated as a logarithmic normalization, e.g. ‘log10 nJy’. This is not supported for AB magnitudes.
flux_norm_method (str or callable, optional) –
Method to use for flux normalization. Options are: - ‘tophat’: Normalize by the flux in a tophat filter. - ‘bandpass’: Normalize by the flux in a bandpass filter. - callable: A function that takes in the wavelength and flux arrays
and returns the normalized flux array.
flux_norm_parameters (dict, optional) – Parameters to pass to the flux normalization method. E.g. for ‘tophat’, this should include the Filter width and center. For ‘bandpass’, this should be a Synthesizer filter object. For callable, this should include any parameters the function requires. Specify ‘rest_frame’: True/False to indicate whether the normalization should be done in the rest frame or observed frame.
parameters_to_remove (list, optional) – Parameters to remove from the parameter array.
parameters_to_add (list, optional) – Parameters to add to the parameter array.
parameter_transformations (dict, optional) – A dictionary of parameter transformations to apply to the parameters in the parameter array. The keys should be the parameter names, and the values should be functions that take a numpy array and return a transformed array.
resample_wavelengths (unyt_array, optional) – If provided, the spectra will be resampled to these wavelengths after transforming to the observed frame. Should be in microns (um). Required if ‘redshift’ is a feature.
inst_resolution_wavelengths (unyt_array, optional) – The wavelength array for the instrument resolution curve. Should be in microns (um). Required if ‘redshift’ is a feature.
inst_resolution_r (unyt_array, optional) – The resolution (R = lambda / delta_lambda) of the instrument as a function of wavelength. Should be the same length as instrument_resolution_wave. Required if ‘redshift’ is a feature.
theory_r (float, optional) – The resolution of the theoretical spectra. Used for convolution when transforming to the observed frame. Default is np.inf (no convolution).
min_flux_value (float, optional) – Minimum flux value to clip the feature array to in units of normed_flux_units.
max_flux_value (float, optional) – Maximum flux value to clip the feature array to in units of normed_flux_units.
- create_features_from_observations(observations, columns_to_feature_names=None, flux_units=None, missing_data_flag=-99, override_transformations={}, ignore_missing=False)[source]¶
Create a feature array from observational data.
Note
Transformations applied to an existing feature array are saved in self.feature_array_flags, but can be overridden with override_transformations. Available transformations are arguments to create_feature_array_from_raw_photometry, although some (like scattering fluxes or error modelling) are not applicable here and will have no effect.
- Parameters:
observations (Union[np.ndarray, Table, pd.DataFrame]) – The observational data to create the feature array from.
flux_units (Union[str, unyt_quantity, None], optional) – Required if the observations do not match ‘normed_flux_units’ in self.feature_array_flags. If None, units are taken from column metadata or assumed to be the same as the training data. Defaults to None.
columns_to_feature_names (Dict[str, str], optional) – A dictionary mapping column names in observations to feature names. If None, a direct mapping to self.feature_array_flags[‘raw_observation_names’] is assumed. Defaults to None.
missing_data_flag (Any, optional) – Value in columns delineating missing data. Depending on setup, this may be flagged to the model or cause the entry to be ignored. Defaults to None.
override_transformations (Dict[str, Any], optional) – A dictionary of transformations to override defaults in self.feature_array_flags (e.g., normalization method). Defaults to None.
ignore_missing (bool, optional) – If True, will ignore missing data in the observations, keeping missing values unchanged. Primarily useful for SBI++ techniques. Defaults to False.
- Returns:
The processed feature array derived from the observations.
- Return type:
np.ndarray
- create_priors(override_prior_ranges={}, prior=<class 'synference.custom_runner.CustomIndependentUniform'>, verbose=True, debug_sample_acceptance=False, extend_prior_range_pc=0.0, set_self=False)[source]¶
Create parameter priors.
Create the priors for the parameters. By default we will use the range of the parameters in the grid. If override_prior_ranges is provided for a given parameter, then that will be used instead.
- Parameters:
override_prior_ranges (
dict) – A dictionary containing the prior ranges for the parameters.prior – The prior distribution to be used.
verbose (
bool) – Whether to print the prior ranges.debug_sample_acceptance (
bool) – Whether to print debug information about sample acceptance in the prior. Only used if prior is CustomIndependentUniform.extend_prior_range_pc (
float) – Percentage to extend the prior range for parameters. However will not be applied to extend parameters to negative values, or parameters close to unity.
- Returns:
A prior object.
- create_restricted_priors(prior=None, set_self=True)[source]¶
Create restricted priors for the parameters.
- detect_misspecification(x_obs, X_train=None, retrain=False)[source]¶
Tests misspecification of the model using the MarginalTrainer from sbi.
This function uses the MarginalTrainer from sbi to train a density estimator on the training data, and then calculates the misspecification log probability of the test data.
- Parameters:
x_obs (np.ndarray) – The observed data to test for misspecification.
X_test (np.ndarray) – The test data to check for misspecification.
X_train (Optional[np.ndarray], optional) – The training data to use for the misspecification check. If None, it will use the training data set in the class. Defaults to None.
retrain (bool, optional) – Whether to retrain the density estimator. If False, it will use the previously trained estimator if available. Defaults to False.
- Returns:
An array of misspecification log probabilities for the test data.
- Return type:
np.ndarray
Note
This method is based on the following paper:
@inproceedings{schmitt2023detecting, title={Detecting model misspecification in amortized Bayesian inference with neural networks}, author={Schmitt, Marvin and Burkner, Paul-Christian and Kothe, Ullrich and Radev, Stefan T}, booktitle={DAGM German Conference on Pattern Recognition}, pages={541--557}, year={2023}, organization={Springer} }
- evaluate_model(posteriors=None, X_test=None, y_test=None, num_samples=1000, verbose=True, samples=None, independent_metrics=True, return_samples=False)[source]¶
Evaluate the trained model on test data.
Note
The following metrics are calculated:
mse: Mean Squared Error (Lower is better). Average of squared differences between predicted and actual values.
rmse: Root Mean Squared Error (Lower is better). Square root of MSE, in the same units as the target.
mae: Mean Absolute Error (Lower is better). Average of absolute differences, less sensitive to outliers.
median_ae: Median Absolute Error (Lower is better). Median of absolute differences, robust to outliers.
r_squared: Coefficient of Determination (1 is perfect). Proportion of variance in the target explained by the model.
rmse_norm: Normalized RMSE (Lower is better). RMSE divided by the range of the target variable.
mae_norm: Normalized MAE (Lower is better). MAE divided by the range of the target variable.
tarp: Tests of Accuracy with Random Points (Lower is better). Probability of predictions falling within a range of true values.
log_dpit_max: (Lower is better). Log of the max distance between predicted and true values.
mean_log_prob: Mean log probability of the predictions given the true values.
- Parameters:
X_test (np.ndarray) – Test feature array.
y_test (np.ndarray) – Test target array.
X_scaler (StandardScaler, optional) – Scaler for the features (if used). Defaults to None.
y_scaler (StandardScaler, optional) – Scaler for the targets (if used). Defaults to None.
num_samples (int, optional) – Number of samples to draw from the posterior. Defaults to 1000.
verbose (bool, optional) – Whether to print verbose output. Defaults to True.
posteriors (List[Any], optional) – List of posterior distributions. If None, will use the stored posteriors. Defaults to None.
samples (np.ndarray, optional) – Precomputed samples from the posterior. If None, will draw new samples. Defaults to None.
independent_metrics (bool, optional) – If True, calculate metrics independently for each parameter. Defaults to True.
return_samples (bool, optional) – If True, return the samples used for evaluation. Defaults to False.
- Raises:
ValueError – If posteriors or test data are not provided.
- Returns:
A dictionary of evaluation metrics. If return_samples is True, returns a tuple of (metrics_dict, samples_array).
- Return type:
Union[Dict[str, Any], Tuple[Dict[str, Any], np.ndarray]]
- fit_catalogue(observations, columns_to_feature_names=None, flux_units=None, missing_data_flag=-99, quantiles=[0.16, 0.5, 0.84], sample_method='direct', sample_kwargs={}, num_samples=1000, timeout_seconds_per_row=5, override_transformations={}, append_to_input=True, return_feature_array=False, recover_SEDs=False, plot_SEDs=False, check_out_of_distribution=True, simulator=None, outlier_methods=['iforest', 'feature_bagging', 'ecod', 'knn', 'lof', 'gmm', 'mcd', 'kde'], missing_data_mcmc=False, missing_data_mcmc_params={'ini_chi': 5.0, 'max_chi2': 50.0, 'nmc': 100, 'nposterior': 1000, 'tmax_all': 10, 'verbose': True}, return_full_samples=False, log_times=False, use_temp_samples=False, **kwargs)[source]¶
Infer posteriors for observational data.
This method creates a feature array from the observations and then samples the posterior distributions of the fitted parameters based on the feature array.
- Parameters:
observations (Union[Table, pd.DataFrame]) – The observational data to create the feature array from.
columns_to_feature_names (dict) – A dictionary mapping the column names in the observations to feature names.
flux_units (Union[str, unyt_quantity, None]) – The units of the flux in the observations. If None, the units will be taken from the column metadata if available and otherwise assumed to be the same as the training data.
missing_data_flag (Any) – Value in columns delineating missing data. Depending on setup of features, this may be flagged to model or the galaxy will be ignored entirely.
quantiles (list) – List of quantiles to compute from the posterior samples.
sample_method (str) – The method to use for sampling the posterior distributions.
sample_kwargs (dict) – Additional keyword arguments to pass to the sampling method.
num_samples (int) – The number of samples to draw from the posterior distributions.
timeout_seconds_per_row (int) – The timeout in seconds for sampling each row of the feature array.
override_transformations (dict) – A dictionary of transformations to override the defaults in self.feature_array_flags. This can be used to change the normalization method, extra features, etc.
append_to_input (bool) – If True, append the quantiles to the input observations DataFrame/Table.
return_feature_array (bool) – If True, return the feature array and the mask instead of the observations.
recover_seds (bool) – If True, recover the SEDs from the posterior samples. This will run self.recover_SED, which will use a simulator to recreate the Synthesizer model and recover the SEDs from the posterior samples.
plot_SEDs (bool) – If True, plot the recovered SEDs. This will only work if recover_seds is True.
check_out_of_distribution (bool) – If True, check if the feature array is in distribution using the robust Mahalanobis method. This will raise an error if any row is out of distribution. If False, no check is performed.
simulator (Optional[GalaxySimulator]) – simulator: A GalaxySimulator object to use for generating the SED. Optional. Will attempt to create one from the library if not provided, or use the existing one.
outlier_methods (list) – List of outlier detection methods to use from PyOD. See PyOD documentation for available methods.
missing_data_mcmc (bool) – If True, use SBI++ to marginalize over missing data using KDE.
return_full_samples (bool) – If True, return the full posterior samples as well as the quantiles.
log_times (bool) – If True, log the time taken to sample each row of the feature array.
use_temp_samples (bool) – If True, try to use the samples stored in self. This is useful for debugging and testing, or jumping straight to SED recovery.
**kwargs (Optional params - only used internally for simformer model.)
Returns
-------
observations – The original observations with additional columns for the quantiles of the fitted parameters, or a new Table with the quantiles if append_to_input is False.
- fit_observation_using_sampler(observation, override_prior_ranges={}, sampler='dynesty', truths=None, min_flux_error=0.0, min_flux_pc_error=0.0, interpolate_grid=False, time_loglikelihood=False, override_prior_distributions={}, out_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/nested_logs/', sampler_kwargs={'bound': 'multi', 'nlive': 500, 'sample': 'rwalk', 'update_interval': 0.6, 'walks': 25}, remove_params=None, plot_name='')[source]¶
Fit the observation using the Dynesty sampler.
- Parameters:
observation (
ndarray) – The observed data to fit.override_prior_ranges (
dict) – Dictionary of prior ranges to override the default ranges.sampler (
str) – The sampler to use. Currently ‘dynesty’, ‘nautilus ‘ or ‘ultranest’.truths (
Optional[ndarray]) – The true parameter values for the observation, if known.min_flux_error (
float) – Minimum flux error added in quadrature to the observation errors.min_flux_pc_error (
float) – Minimum flux percentage error added in quadrature to the observation errors. Either min_flux_error or min_flux_pc_error can be used, not both.interpolate_grid (
bool) – Whether to recreate the simulator using interpolation.override_prior_distributions (
dict) – Dictionary of prior distributions to override the default prior distributions. Needs to transform the unit cube to the parameter space. If multiple parameters are to be overridden by the same function, provide a list of parameter names as the key.sampler_kwargs (
dict) – Additional keyword arguments to pass to the sampler.out_dir (
str) – directory for outputs.time_loglikelihood (
bool) – whether to print execution times for log likelhood calls.remove_params (
Optional[list]) – List of parameters to remove from the fitting.plot_name – Name to use for the output plots.
- Return type:
None- Returns:
The result of the fitting.
- has_simulator¶
- assert (self.simulator is not None) or (self.raw_observation_grid is not None), (
“Either a simulator or raw photometry grid must be provided.”
)
- classmethod init_from_hdf5(model_name, hdf5_path, return_output=False, **kwargs)[source]¶
Initialize the SBI fitter from an HDF5 file.
- Parameters:
hdf5_path (
str) – Path to the HDF5 file.model_name (
str) – Name of the model to be used.return_output – If True, returns the output dictionary from the HDF5 file.
**kwargs – Additional keyword arguments to pass to the SBI_Fitter constructor.
- Returns:
An instance of the SBI_Fitter class.
- lc2st(x_obs, posterior=None, X_test=None, y_test=None)[source]¶
Perform the L-C2ST test for model coverage.
This function uses the L-C2ST classifier to test the model coverage by comparing the posterior samples of the observed data with the posterior samples of the training data. It generates a plot showing the classifier probabilities on the observed data and the null hypothesis.
- Parameters:
x_obs – The observed data to test.
posterior – The posterior distribution to use for sampling. If None, it will use the posteriors set in the class.
X_test – The test data to use for the L-C2ST test. If None, it will use the training data set in the class.
y_test – The labels for the test data. If None, it will use the training labels set in the class.
- property likelihood_func¶
Get the likelihood function from the posterior if available.
- load_model_from_pkl(model_file, set_self=True, load_arrays=True)[source]¶
Load the model from a pickle file.
- Parameters:
model_file (str) – Path to the model file. Can be a directory or a file.
set_self (bool, optional) – If True, set the attributes of the class to the loaded values. Default is True.
load_arrays (bool, optional) – If True, load the feature and parameter arrays from the model file. Default is True.
Returns
---------
posteriors (list) – List of posteriors loaded from the model file.
stats (dict) – Dictionary of statistics loaded from the model file.
params (dict) – Dictionary of parameters loaded from the model file.
- Return type:
Tuple[list,dict,dict]
- classmethod load_saved_model(model_file, library_path=None, model_name=None, load_arrays=True, **kwargs)[source]¶
Load a prefit SBI model from a file.
- Parameters:
model_file (str) – Path to the saved model file.
library_path (Optional[str], optional) – Optional path to the library file. If not provided, it will be loaded from the model file parameters. Defaults to None.
model_name (Optional[str], optional) – Optional name of the model. If not provided, it will be loaded from the model file parameters. Defaults to None.
load_arrays (bool, optional) – Whether to load the feature and parameter arrays. Defaults to True.
**kwargs (Any) – Additional keyword arguments to pass to the SBI_Fitter constructor.
- Returns:
An instance of SBI_Fitter initialized with the loaded model.
- Return type:
- log_prob(X, y, posteriors=None, verbose=True)[source]¶
Calculate the log-probability of the posterior for the given samples.
- Parameters:
X (2-dimensional array of shape (num_samples, n_features)) – Feature array.
y (2-dimensional array of shape (num_samples, n_parameters)) – Test parameter values.
posteriors (object, optional) – Posteriors to use for the log-probability calculation. If None, will use the posteriors stored in the object.
verbose (bool, optional) – Whether to print progress information. Default is True.
Returns
-------
lp (1-dimensional array) – The log-probability values for each sample.
- Return type:
ndarray
- optimize_sbi(study_name, suggested_hyperparameters={'clip_max_norm': [0.1, 5.0], 'hidden_features': [12, 200], 'learning_rate': [1e-06, 0.001], 'num_components': [2, 16], 'num_transforms': [1, 4], 'stop_after_epochs': [10, 30], 'training_batch_size': [32, 128], 'validation_fraction': [0.1, 0.3]}, fixed_hyperparameters={'model_type': 'mdn', 'n_nets': 1}, n_trials=100, n_jobs=1, random_seed=42, verbose=False, train_test_fraction=0.9, persistent_storage=False, out_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/', score_metrics='log_prob-pit', direction='maximize', timeout_minutes_trial_sampling=120.0, sql_db_path=None)[source]¶
Use Optuna to optimize the SBI model hyperparameters.
Possible hyperparameters to optimize: - n_nets: Number of networks to use in the ensemble. - model_type: Type of model to use. Either ‘mdn’ or ‘maf’. - hidden_features: Number of hidden features in the neural network. - num_components: Number of components in the mixture density network. - num_transforms: Number of transforms in the masked autoregressive flow. - training_batch_size: Batch size for training. - learning_rate: Learning rate for the optimizer. - validation_fraction: Fraction of the training set to use for validation. - stop_after_epochs: Number of epochs without improvement before stopping. - clip_max_norm: Maximum norm for gradient clipping.
- Return type:
None
Parameters:¶
study_name: Name of the Optuna study.
- suggested_hyperparameters: Dictionary of hyperparameters to suggest.
Keys are hyperparameter names and values are lists of possible values.
- fixed_hyperparameters: Dictionary of hyperparameters to fix.
Keys are hyperparameter names and values are fixed values.
n_trials: Number of trials to run in the optimization.
- n_jobs: Number of parallel jobs to run.
Note that Optuna uses the threading backend, not true parallelism, so the Python GIL can still be a bottleneck. See https://optuna.readthedocs.io/en/stable/faq.html#how-can-i-parallelize-optimization for discussion.
random_seed: Random seed for reproducibility.
verbose: Whether to print progress and results.
persistent_storage: Whether to use persistent storage for the study.
out_dir: Directory to save the study results.
- score_metrics: Metrics to use for scoring the trials. Either a string
or a list of metrics.
- direction: Direction of optimization, either ‘minimize’ or ‘maximize’,
or a list of directions if using multi-objective optimization.
- timeout_minutes_trial_sampling: Timeout in minutes for each trial sampling.
e.g. if sampling gets stuck, will prune this trial.
- sql_db_path: Optional path to an existing MySQL database for Optuna.
If you don’t have one set up, leave as None to create a SQLite database.
- plot_coverage(X=None, y=None, posterior_plot_type='total', num_samples=1000, sample_method='direct', sample_kwargs={}, plot_list=['predictions', 'histogram', 'logprob', 'coverage', 'tarp'], plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', n_test_draws=1000, posteriors=None, overwrite=False)[source]¶
Plot the coverage of the SBI model.
- Parameters:
X (2-dimensional array of shape (num_samples, n_features), optional) – Feature array. If None, will use the test set stored in the object.
y (2-d array of true target values, optional) – Target variable. If None, will use the test set stored in the object.
posterior_plot_type (str or int, optional) – Type of posterior plot to use. Can be ‘total’, ‘seperate’, or an index of the posterior.
num_samples (int, optional) – Number of samples to draw from the posterior. Default is 1000.
sample_method (str, optional) – Method to use for sampling from the posterior. Default is ‘direct’.
sample_kwargs (dict, optional) – Additional keyword arguments for the sampling method.
plot_list (list, optional) – List of plots to include in the coverage plot. Default is [‘predictions’, ‘histogram’, ‘logprob’, ‘coverage’, ‘tarp’].
plots_dir (str, optional) – Directory to save the plots. Default is f”{code_path}/models/name/plots/”.
n_test_draws (int, optional) – Number of test draws to generate if X and y are not provided.
posteriors (object, optional) – Posteriors to use for the coverage plot. If None, will use the posteriors stored in the object.
Returns
-------
fig (matplotlib Figure) – The figure containing the coverage plots.
- Return type:
None
- plot_diagnostics(X_train=None, y_train=None, X_test=None, y_test=None, stats=None, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', sample_method='direct', posteriors=None, online=False, num_samples=1000)[source]¶
Plot the diagnostics of the SBI model.
- Parameters:
X_train (np.ndarray) – Training feature array.
y_train (np.ndarray) – Training target array.
X_test (np.ndarray) – Test feature array.
y_test (np.ndarray) – Test target array.
stats (Optional[List[str]], optional) – List of statistics to plot. If None, will use self.stats. Defaults to None.
plots_dir (str, optional) – Directory to save the plots.
sample_method (str, optional) – Method to use for sampling. Options are ‘direct’, ‘emcee’, ‘pyro’, or ‘vi’. Defaults to ‘direct’.
posteriors (Optional[List[Any]], optional) – List of posterior distributions. If None, will use self.posteriors. Defaults to None.
online (bool, optional) – If True, will not plot the posterior and coverage. Defaults to False.
num_samples (int, optional) – Number of samples to draw from the posterior for plotting for each test sample. Samples will be saved in the plots_dir. Defaults to 1000.
- Return type:
None
- plot_histogram_feature_array(bins='knuth', plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', seperate_test_train=False, max_bins_row=6, comparison_array=None, density=False, log=False)[source]¶
Plot histogram of each feature using astropy.visualization.hist.
- plot_histogram_parameter_array(bins='knuth', plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', seperate_test_train=False, max_bins_row=6)[source]¶
Plot histogram of each parameter using astropy.visualization.hist.
- plot_loss(summaries='self', plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', overwrite=False)[source]¶
Plot the loss of the SBI model.
- Return type:
None
- plot_parameter_deviations(parameters='all', X_test=None, y_test=None, posteriors=None, snr_bins=None, snr_feature_names=None, contours=True, error_bars=False)[source]¶
Plot the deviation of parameters from the true values.
E.g. Mrecoverd - Mtrue vs Mtrue. One row in grid per snr_bin if not None.
- plot_posterior(ind='random', X=None, y=None, seed=None, num_samples=1000, sample_method='direct', sample_kwargs={}, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', plot_kwargs={'fill': True}, posteriors=None, **kwargs)[source]¶
Plot the posterior of the SBI model.
- Return type:
None
- recover_SED(X_test, samples=None, num_samples=1000, sample_method='direct', sample_kwargs={}, posteriors=None, simulator=None, prior=None, plot=True, marginalized_parameters=None, extra_parameters=None, phot_unit='AB', true_parameters=[], plot_name=None, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', sample_color='firebrick', param_labels=None, plot_closest_draw_to={}, plot_sfh=True, plot_histograms=True, kde=True, save_plots=True, fig=None, ax=None, ax_sfh=None, verbose=True)[source]¶
Recover the SED for a given observation, if a simulator is provided.
- Parameters:
X_test (
ndarray) – The input observation to recover the SED for.samples (
Optional[ndarray]) – Samples from the posterior distribution. if None, samples will be drawn from the posterior.num_samples – Number of samples to draw from the posterior.
sample_method (
str) – Method to sample from the posterior if samples is None. Either ‘direct’ or ‘emcee’.sample_kwargs (
dict) – Additional keyword arguments for sampling.posteriors – The posterior distribution to use. If None, uses self.posteriors.
simulator (
Optional[callable]) – A callable simulator function to generate the SED.prior (
Optional[object]) – The prior distribution to use. If None, uses self._prior.plot (
bool) – Whether to plot the results.marginalized_parameters (
Optional[Dict[str,callable]]) – Dictionary of parameters to marginalize over.phot_unit – Photometric unit for the SED plot (default is ‘AB’).
true_parameters – List of true parameters to plot on the SED.
plot_name – Name for the plot file.
plots_dir – Directory to save the plots.
sample_color – Color for the sampled SED lines.
param_labels – Labels for the parameters in the plot.
plot_closest_draw_to – Dictionary of parameters and values to plot closest draws. E.g. if you want to plot the closest draw for a specific redshift, or mass, to see degeneracies in the posteriors.
plot_sfh – Whether to plot the SFH in addition to the SED.
plot_histograms – Whether to plot histograms of the parameters.
kde – Whether to use KDE for the histograms.
save_plots – Whether to save the plots.
fig – Matplotlib figure to use for the SED plot.
ax – Matplotlib axis to use for the SED plot.
ax_sfh – Matplotlib axis to use for the SFH plot.
- Returns:
The quantiles of the recovered SED.
- Return type:
fnu_quantiles
- recreate_simulator_from_library(set_self=True, overwrite=False, override_library_path=None, override_grid_path=None, **kwargs)[source]¶
Recreate the simulator from the HDF5 library.
Simple libraries (single SFH, ZDist, one basis) with pre-existing simple EmissionModels can be recreated as a simulator, allowing for SED recovery without manual specification of the simulator function.
Two possible use cases, either to generate samples for fitting (in which case we want the config to closely match the feature array) or to recover the SED, in which case we aren’t concerned about depths, noise models, etc.
- Parameters:
set_self – Whether to set the simulator attribute.
overwrite – Whether to overwrite an existing simulator.
override_library_path – Path to the library to use.
override_grid_path – Path to the synthesizer grid directory to use.
**kwargs – Additional keyword arguments to pass to the GalaxySimulator.
- Returns:
The recreated GalaxySimulator object.
- run_single_sbi(train_test_fraction=0.8, random_seed=None, backend='sbi', engine='NPE', train_indices=None, test_indices=None, n_nets=1, model_type='mdn', hidden_features=50, num_components=4, num_transforms=4, training_batch_size=64, learning_rate=0.0001, validation_fraction=0.2, stop_after_epochs=15, clip_max_norm=5.0, additional_model_args={}, save_model=True, verbose=True, prior_method='ili', out_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/', plot=True, name_append='timestamp', feature_scalar=<class 'sklearn.preprocessing._data.StandardScaler'>, target_scalar=<class 'sklearn.preprocessing._data.StandardScaler'>, set_self=True, learning_type='offline', simulator=None, num_simulations=1000, num_online_rounds=5, initial_training_from_library=False, override_prior_ranges={}, online_training_xobs=None, load_existing_model=True, use_existing_indices=True, evaluate_model=True, save_method='joblib', num_posterior_draws_per_sample=1000, embedding_net=Identity(), custom_config_yaml=None, sql_db_path=None)[source]¶
Run a single SBI training instance.
- Parameters:
train_test_fraction (float, optional) – Fraction of the dataset to be used for training. Defaults to 0.8.
random_seed (int, optional) – Random seed for reproducibility. Defaults to None.
backend (str, optional) – Backend to use for training (‘sbi’, ‘lampe’, or ‘pydelfi’). Pydelfi cannot be installed in the same environment. Defaults to “sbi”.
engine (Union[str, List[str]], optional) – Engine to use (‘NPE’, ‘NLE’, ‘NRE’ or sequential variants). Defaults to “NPE”.
train_indices (np.ndarray, optional) – Indices of the training set. Defaults to None.
test_indices (np.ndarray, optional) – Indices of the test set. If None, no test set is used. Defaults to None.
n_nets (int, optional) – Number of networks to use in the ensemble. Defaults to 1.
model_type (Union[str, List[str]], optional) – Type of model (e.g., ‘mdn’, ‘maf’, ‘nsf’). If a list, an ensemble is used. Defaults to “mdn”.
hidden_features (Union[int, List[int]], optional) – Number of hidden features in the neural network. Defaults to 50.
num_components (Union[int, List[int]], optional) – Number of components in the mixture density network. Defaults to 4.
num_transforms (Union[int, List[int]], optional) – Number of transforms in the masked autoregressive flow. Defaults to 4.
training_batch_size (int, optional) – Batch size for training. Defaults to 64.
learning_rate (float, optional) – Learning rate for the optimizer. Defaults to 1e-4.
validation_fraction (float, optional) – Fraction of training set for validation to prevent over-fitting. Training stops if validation loss doesn’t improve for stop_after_epochs. Defaults to 0.2.
stop_after_epochs (int, optional) – Epochs without improvement before stopping. Defaults to 15.
clip_max_norm (float, optional) – Maximum norm for gradient clipping. Defaults to 5.0.
additional_model_args (dict, optional) – Additional arguments for the model (e.g., num_layers, use_batch_norm). Defaults to {}.
save_model (bool, optional) – Whether to save the trained model. Defaults to True.
verbose (bool, optional) – Whether to print verbose output. Defaults to True.
prior_method (str, optional) – Method to create the prior (‘manual’ or ‘ili’). Defaults to “ili”.
feature_scalar (Callable, optional) – Scaler class for the features. Defaults to StandardScaler.
target_scalar (Callable, optional) – Scaler class for the targets. Defaults to StandardScaler.
out_dir (str, optional) – Directory to save the model. Defaults to f”{code_path}/models/”.
plot (bool, optional) – Whether to plot the diagnostics. Defaults to True.
name_append (str, optional) – String to append to the model name. Defaults to “timestamp”.
set_self (bool, optional) – Whether to set instance attributes with the trained model. Defaults to True.
learning_type (str, optional) – Type of learning (‘offline’ or ‘online’). If ‘online’, a simulator must be provided. Defaults to “offline”.
simulator (Callable, optional) – Function to simulate data for ‘online’ learning. Defaults to None.
num_simulations (int, optional) – Number of simulations to run in each call during ‘online’ learning. Defaults to 1000.
num_online_rounds (int, optional) – Number of rounds for ‘online’ learning. Defaults to 5.
initial_training_from_library (bool, optional) – Whether to use the initial training from the library in ‘online’ learning. WARNING: This is broken. Defaults to False.
override_prior_ranges (dict, optional) – Dictionary of prior ranges to override the defaults. Defaults to {}.
online_training_xobs (np.ndarray, optional) – A single input observation to condition on for ‘online’ training. Defaults to None.
load_existing_model (bool, optional) – Whether to load an existing model if it exists. Defaults to True.
use_existing_indices (bool, optional) – Whether to use existing train/test indices if they exist. Defaults to True.
evaluate_model (bool, optional) – Whether to evaluate the model after training (computes log prob, PIT). Defaults to True.
save_method (str, optional) – Method to save the model (‘torch’, ‘pickle’, ‘joblib’, or ‘h5py’). Defaults to “joblib”.
num_posterior_draws_per_sample (int, optional) – Number of posterior draws for metrics and plots. Defaults to 1000.
embedding_net (Optional[torch.nn.Module], optional) – Optional embedding network for the simulator. Defaults to torch.nn.Identity().
custom_config_yaml (Optional[str], optional) – Path to a custom YAML config file to override settings. Defaults to None.
sql_db_path (Optional[str], optional) – Path to an SQL database for logging or results. Defaults to None.
- Returns:
A tuple containing the trained posterior distribution and a dictionary of training statistics.
- Return type:
tuple
- run_single_simformer(train_test_fraction=0.8, random_seed=None, train_indices=None, test_indices=None, save_model=True, verbose=True, out_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/', plot=True, name_append='timestamp', save_method='dill', set_self=True, override_prior_ranges={}, load_existing_model=True, use_existing_indices=True, evaluate_model=True, max_num_epochs=100000, sde_type='ve', simformer_type='score', model_kwargs={}, learning_rate=0.0005, training_batch_size=200, validation_fraction=0.1, stop_after_epochs=20, clip_max_norm=5.0, training_args={})[source]¶
Trains a single Simformer model using the SBI implementation.
May need to be on this branch: https://github.com/sbi-dev/sbi/pull/1621/
- Parameters:
train_test_fraction (
float) – Fraction of the dataset to be used for training.random_seed (
Optional[int]) – Random seed for reproducibility.train_indices (
Optional[ndarray]) – Indices of the training set.test_indices (
Optional[ndarray]) – Indices of the test set. If None, no test set is used.save_model (
bool) – Whether to save the trained model.verbose (
bool) – Whether to print verbose output.out_dir (
str) – Directory to save the model.plot (
bool) – Whether to plot the results.name_append (
str) – String to append to the model name.set_self (
bool) – Whether to set the self attribute.override_prior_ranges (
dict) – Dictionary to override prior ranges.load_existing_model (
bool) – Whether to load an existing model.use_existing_indices (
bool) – Whether to use existing indices.evaluate_model (
bool) – Whether to evaluate the model.max_num_epochs (
int) – Maximum number of epochs to train the model.sde_type (
str) – Type of SDE to use (‘ve’,’vp’ or ‘subvp’). Not used for flow matching.simformer_type – Type of Simformer to use (‘score’ or ‘flow’).
model_kwargs (
dict) – Additional keyword arguments to pass to the Simformer builder. Available kwargs and defaults are: For both ‘score’ and ‘flow’: - hidden_features: int = 100, - num_heads: int = 4, - num_layers: int = 4, - mlp_ratio: int = 2, - time_embedding_dim: int = 32, - embedding_net: nn.Module = nn.Identity(), - dim_val: int = 64, - dim_id: int = 32, - dim_cond: int = 16, - ada_time: bool = False, - **kwargs: Any,learning_rate (
float) – Learning rate for the optimizer.training_batch_size (
int) – Batch size for training.validation_fraction (
float) – Fraction of the training set to use for validation.stop_after_epochs (
int) – Number of epochs without improvement before stopping.clip_max_norm (
float) – Maximum norm for gradient clipping.training_args (
dict) – Additional arguments to pass to the training function.
- Return type:
tuple
- run_validation_from_file(validation_file, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', metrics=['coverage', 'histogram', 'predictions', 'tarp', 'logprob'])[source]¶
Run the validation from a file.
- Return type:
None
- sample_posterior(X_test=None, sample_method='direct', sample_kwargs={}, posteriors=None, num_samples=1000, timeout_seconds_per_test=30, log_times=False, **kwargs)[source]¶
Sample from the posterior distribution.
- Parameters:
X_test (
Optional[ndarray]) – Test feature array. If None, will use the stored test data.sample_method (
str) – Method to use for sampling. Options are ‘direct’, ‘emcee’, ‘pyro’, or ‘vi’.sample_kwargs (
dict) – Additional keyword arguments for the sampler.posteriors (
Optional[object]) – List of posterior distributions. If None, will use the stored posteriors.num_samples (
int) – Number of samples to draw from the posterior.timeout_seconds_per_test – Timeout in seconds for each test sample.
- Return type:
ndarray- Returns:
A numpy array of samples drawn from the posterior distribution. Shape (num_objects, num_samples, num_parameters).
- save_state(out_dir, name_append='', save_method='joblib', has_grid=True, **extras)[source]¶
Save the state of the SBI fitter to a file.
- Parameters:
out_dir – Directory to save the model parameters.
name_append – String to append to the model name in the filename.
save_method – Method to use for saving. Options are ‘torch’, ‘pickle’, ‘ ‘joblib’, ‘hdf5’, ‘dill’, or ‘hickle’. Default is ‘joblib’.
has_grid – Whether the model has a grid. If True, feature and parameter arrays will be saved. Default is True.
**extras – Additional parameters to save in the parameter dictionary.
- split_dataset(train_fraction=0.8, random_seed=None, verbose=True)[source]¶
Split the dataset into training and testing sets.
- Parameters:
train_fraction (
float) – The fraction of the dataset to be used for training.random_seed (
Optional[int]) – The random seed for reproducibility.
- Return type:
tuple- Returns:
A tuple containing the training and testing indices.
- test_in_distribution(X_test, method='robust_mahalanobis', direction='in', contamination=0.1, n_neighbors=20, threshold=None, confidence=0.95, n_components=None, plot=True, feature_breakdown=False, **kwargs)[source]¶
Test if X_test is in distribution of self.feature_array.
If direction is “in”, then we check if X_test is within the distribution of self.feature_array. If ‘out’, we check if self.feature_array is within the distribution of X_test.
Note
This function is a wrapper for utils.detect_outliers.
- Methods available:
‘robust_mahalanobis’: Uses robust Mahalanobis distance.
‘mahalanobis’: Uses standard Mahalanobis distance.
‘lof’: Uses Local Outlier Factor.
‘isolation_forest’: Uses Isolation Forest.
‘one_class_svm’: Uses One-Class SVM.
‘pca’: Uses PCA.
‘hotelling_t2’: Uses Hotelling’s T-squared test.
‘kde’: Uses Kernel Density Estimation.
- Parameters:
X_test (np.ndarray) – The test data to check for in-distribution or out-of-distribution.
method (str, optional) – The method to use for outlier detection. Defaults to ‘robust_mahalanobis’.
direction (str, optional) – Direction of the test. ‘in’ (is X_test in self.feature_array?) or ‘out’ (is self.feature_array in X_test?). Defaults to ‘in’.
contamination (float, optional) – Expected proportion of outliers (for applicable methods). Defaults to 0.1.
n_neighbors (int, optional) – Number of neighbors for LOF. Defaults to 20.
threshold (float, optional) – Manual threshold for outlier detection. Defaults to None.
confidence (float, optional) – Confidence level for statistical tests. Defaults to 0.95.
n_components (int, optional) – Number of components for PCA (if None, uses all). Defaults to None.
plot (bool, optional) – Whether to plot the results of the outlier detection. Defaults to True.
feature_breakdown (bool, optional) – If True, will analyze feature contributions to outlier scores. Only works for ‘robust_mahalanobis’, ‘mahalanobis’, ‘euclidean’. Defaults to False.
**kwargs (Any) – Additional parameters passed to the underlying detect_outliers method.
- Returns:
A dictionary containing the outlier detection results:
’outlier_mask’ (np.ndarray): Boolean array indicating outliers.
’scores’ (np.ndarray): Outlier scores for each observation.
’threshold_used’ (float): The threshold value used for detection.
’method_info’ (dict): Additional method-specific information.
- Return type:
Dict[str, Any]
- test_in_distribution_pyod(X_test, methods=['lof', 'isolation_forest'], contamination=0.01, direction='in', combination_method='majority')[source]¶
Test if X_test is in distribution of self.feature_array using pyod.
If direction is “in”, then we check if X_test is within the distribution of self.feature_array. If ‘out’ we check if self.feature_array is within the distribution of X_test.
Parameters:¶
- X_testnp.ndarray
The test data to check for in-distribution or out-of-distribution.
- methodslist
List of methods to use for outlier detection. See https://pyod.readthedocs.io/en/latest/pyod.models.html
- contaminationfloat
Expected proportion of outliers (for applicable methods)
- directionstr
Direction of the test, either ‘in’ or ‘out’.
- combination_methodstr
Method to combine the results from different methods. Options are ‘majority’ (default), ‘any’, ‘all’, ‘none’.
- property training_log_probs¶
Training set log-probability of each epoch for each net.
Returns:¶
list of 1-dimensional array
- update_parameter_array(parameters_to_remove=[], delete_rows=[], n_scatters=1, parameters_to_add=[], parameter_transformations=None)[source]¶
Updates parameter array based on feature array creation.
This function removes parameters from the parameter array and names based on the provided feature parameters and parameters to remove. It also handles the case where the parameter array needs to be duplicated for multiple scatters.
- Parameters:
parameters_to_remove (
list) – List of parameters to remove from the parameter array.delete_rows – List of rows to delete from the parameter array.
n_scatters (
int) – Number of scatters to duplicate the parameter array for.parameters_to_add (
list) – List of parameters to add to the parameter array. Only parameters from self.supplementary_parameters are currently supported
- Return type:
None
- class synference.sbi_runner.Simformer_Fitter(name='simformer_fitter', **kwargs)[source]¶
Simformer Fitter for SBI models.
This class implements the Simformer architecture for SBI tasks.
To Do: - Ensure more inherited methods actually work with Simformer. - Implement TARP and other metrics which are native to LtU-ILI. - Ensure methods to act on real observations and recover photometry work.
- create_priors(override_prior_ranges={}, verbose=True, set_self=False)[source]¶
Create priors for the Simformer model.
Parameters:¶
- override_prior_rangesdict, optional
Dictionary to override the default prior ranges. Default is an empty dictionary.
- verbosebool, optional
If True, prints information about the prior creation. Default is True.
- set_selfbool, optional
If True, sets the created prior to the instance attribute.
- fit_catalogue(observations, columns_to_feature_names=None, flux_units=None, missing_data_flag=-99, quantiles=[0.16, 0.5, 0.84], num_samples=1000, override_transformations={}, append_to_input=True, return_feature_array=False, recover_SEDs=False, plot_SEDs=False, check_out_of_distribution=True, simulator=None, rng_seed=42, attention_mask='full', batch_size=100, missing_data_mcmc=False, log_times=False)[source]¶
Wrapper for fit_catalogue in parent.
To Do: Better attention mask.
- classmethod init_from_hdf5(model_name, hdf5_path=None, return_output=False, **kwargs)[source]¶
Initialize the Simformer Fitter.
- load_model_from_pkl(model_dir, model_name='simformer', set_self=True)[source]¶
Load a Simformer model from a pickle file.
Parameters:¶
- model_dirstr
Directory where the model file is located.
- model_namestr, optional
Name of the model file to load. Default is “simformer”.
- set_selfbool, optional
If True, sets the loaded model and task to the instance attributes. Default is True.
- classmethod load_saved_model(model_name, library_path, model_file, **kwargs)[source]¶
Load a saved Simformer model from a file.
- log_prob(X_test, condition_mask='full', posteriors=None, theta=None)[source]¶
Compute the log probability of the data given the model.
- Parameters:
X_test (np.ndarray) – Observed data of shape (n_observations, n_features) or (n_features,)
condition_mask (np.ndarray or str) – Mask indicating which parts of the data are observed. If ‘full’, assumes all features are observed.
posteriors (object, optional) – Posteriors to use for computing the log probability. If None, will use the posteriors stored in the object.
theta (np.ndarray, optional) – Parameter samples of shape (n_samples, n_params). If None, will sample from posterior.
Returns
-------
np.ndarray – Log probabilities of shape (n_observations, n_samples) where each element [i,j] is the log probability of observation i under posterior sample j.
- plot_coverage(num_samples=1000, num_evaluations=25, task=None, posteriors=None, rng_seed=42, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/')[source]¶
Plot the coverage of the posterior distribution.
Parameters:¶
- num_samplesint, optional
Number of samples to draw from the posterior. Default is 1000.
- num_evaluationsint, optional
Number of evaluations to perform for coverage. Default is 25.
- taskobject, optional
Task object containing the model and data. If None, uses the simformer_task attribute of the object.
- posteriorsobject, optional
Posteriors to use for sampling. If None, uses the posteriors stored in the object.
- rng_seedint, optional
Random seed for reproducibility. Default is 42.
- plots_dirstr, optional
Directory to save the plots. Default is f”{code_path}/models/{name}/plots/”.
- plot_diagnostics(X_test=None, y_test=None, num_samples=1000, num_evaluations=25, task=None, posteriors=None, rng_seed=42, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', metric_path='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/name_metrics.json', overwrite=False)[source]¶
Plot diagnostics for the Simformer model.
- Parameters:
X_test (np.ndarray, optional) – Test data to evaluate the model. If None, uses the X_test attribute. Defaults to None.
y_test (np.ndarray, optional) – True values for the test data. If None, uses the y_test attribute. Defaults to None.
num_samples (int, optional) – Number of samples to draw from the posterior. Defaults to 1000.
num_evaluations (int, optional) – Number of evaluations to perform for coverage. Defaults to 25.
task (object, optional) – Task object containing the model and data. If None, uses the simformer_task attribute. Defaults to None.
posteriors (object, optional) – Posteriors to use for sampling. If None, uses the posteriors stored in the object. Defaults to None.
rng_seed (int, optional) – Random seed for reproducibility. Defaults to 42.
plots_dir (str, optional) – Directory to save the plots. Defaults to f”{code_path}/models/{name}/plots/”.
metric_path (str, optional) – Path to save the metrics JSON file. Defaults to f”{code_path}/models/{name}/{name}_metrics.json”.
overwrite (bool, optional) – If True, overwrites existing plots and metrics. Defaults to False.
- Returns:
This function saves plots and metrics to disk and does not return anything.
- Return type:
None
- plot_sample_accuracy(num_samples=1000, X_test=None, y_test=None, task=None, posteriors=None, rng_seed=42, plots_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/plots/', samples=None)[source]¶
Plot the accuracy of the sampled posterior distribution.
Parameters:¶
- num_samplesint, optional
Number of samples to draw from the posterior. Default is 1000.
- X_testnp.ndarray, optional
Test data to sample from the posterior. If None, uses the X_test attribute of the object.
- y_testnp.ndarray, optional
True values for the test data. If None, uses the y_test attribute of the object.
- taskobject, optional
Task object containing the model and data. If None, uses the simformer_task attribute of the object.
- posteriorsobject, optional
Posteriors to use for sampling. If None, uses the posteriors stored in the object.
- rng_seedint, optional
Random seed for reproducibility. Default is 42.
- plots_dirstr, optional
Directory to save the plots. Default is f”{code_path}/models/{name}/plots/”.
- samplesnp.ndarray, optional
Pre-computed samples from the posterior. If None, samples will be drawn from the posterior using the sample_posterior method.
- run_single_sbi(backend='jax', num_training_simulations=10000, train_test_fraction=0.9, random_seed=42, set_self=True, verbose=True, load_existing_model=True, name_append='timestamp', save_method='joblib', task_func=None, model_config_dict_overrides=None, sde_config_dict=None, train_config_dict_overrides=None, sde_config_dict_overrides=None, attention_mask_type='full', evaluate_model=True)[source]¶
Train a Simformer model using the provided configurations.
This method sets up the Simformer task, prepares the data, trains the model using the specified configurations, and saves the trained model to a pickle file.
- Parameters:
backend (str, optional) – Backend to use for training (‘jax’ or ‘torch’). Defaults to “jax”.
num_training_simulations (int, optional) – Number of training simulations to generate. Only used if has_simulator is True, otherwise the feature_array is used. Defaults to 10,000.
train_test_fraction (float, optional) – Fraction of samples to use for training. Defaults to 0.9.
random_seed (int, optional) – Random seed for reproducibility. Defaults to 42.
set_self (bool, optional) – If True, sets the trained model and task to the instance attributes. Defaults to True.
verbose (bool, optional) – If True, prints progress information during training. Defaults to True.
load_existing_model (bool, optional) – If True, loads an existing model from a pickle file if it exists. Defaults to True.
name_append (str, optional) – String to append to the model name in the output file. Defaults to “timestamp”.
save_method (str, optional) – Method to use for saving the model (e.g., ‘torch’, ‘pickle’). Defaults to “joblib”.
task_func (Callable, optional) – Function to create the task. If None, uses the default GalaxyPhotometryTask. Defaults to None.
model_config_dict_overrides (dict, optional) – Dictionary to override the default model configuration. Defaults to None.
sde_config_dict (dict, optional) – Dictionary to override configs for the SDE. Defaults to a pre-defined VPSDE configuration.
train_config_dict_overrides (dict, optional) – Dictionary to override the training configuration. Defaults to a pre-defined configuration.
sde_config_dict_overrides (dict, optional) – Dictionary to override the SDE configuration. Defaults to None.
attention_mask_type (str, optional) – Type of attention mask to use (‘full’, ‘causal’, or ‘none’). Defaults to “full”.
evaluate_model (bool, optional) – If True, evaluates the trained model on the validation set and prints metrics. Defaults to True.
- Add support for constraint functions during sampling to allow intervals
(e.g., from scoresbibm.methods.guidance import generalized_guidance, get_constraint_fn).
- sample_posterior(X_test, num_samples=1000, posteriors=None, rng_seed=42, attention_mask='full', batch_size=100, **kwargs)[source]¶
Sample from the posterior distribution.
- Parameters:
X_test (np.ndarray) – Test data to sample from the posterior.
num_samples (int, optional) – Number of samples to draw from the posterior. Default is 1000.
posteriors (object, optional) – Posteriors to use for sampling. If None, will use the posteriors stored in the object.
attention_mask (Union[str, np.ndarray], optional) – Attention mask to use for sampling. Can be ‘full’ or a numpy array. Default is ‘full’. Full means you have full observations for all bands. If you have missing bands, you can provide a numpy array with the shape
TODO (Make this work for multidimensional X_test.)
- save_model_to_pkl(task=None, posteriors=None, name_append='', out_dir='/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/models/name/', save_method='joblib', **extras)[source]¶
Save the Simformer model to a pickle file.
Parameters:¶
- taskobject, optional
Task object containing the model and data. If None, uses the simformer_task attribute of the object.
- posteriorsobject, optional
Posteriors to save. If None, uses the posteriors attribute of the object.
- name_appendstr, optional
String to append to the model name in the output file. Default is an empty string.
- out_dirstr, optional
Directory to save the model files. Default is f”{code_path}/models/name/”.
- save_methodstr, optional
Method to use for saving the model. Options are ‘torch’, ‘joblib’, ‘pickle’, and ‘hdf5’. Default is ‘torch’.
- extrasdict, optional
Additional parameters to save with the model. These will be added to the saved dictionary.