Fitter
¶
- class src.binary_sed_fitting.Fitter¶
Collections of functions which deal with SED fitting.
Fitting blackbody spectrum
Functions to calculate chi2 values for a parameter grid.
Functions to find best fit values and their errors
At present, only chi2 minimization is implimented.
- static blackbody(star, p0=[5000.0, -20])¶
Fit blackbody parameters to observational data using scipy.optimize.curve_fit.
- Parameters:
star (Star object) – Star data with observed flux.
p0 (list, optional) – Initial guess for the fit parameters [Temperature, Log Scaling Factor].
Notes
Updates:
Star.Te_blackbody
,Star.log_sf_blackbody
- static calculate_chi2(star, model, refit=True, plot=False, _trim=None, _minimising_param='chi2')¶
Calculate chi2 values for different model parameters.
- Parameters:
star (Star object) – Star data.
model (Model object) – Model information.
refit (bool, optional) – Whether to refit the data (default is True).
plot (bool, optional) – Whether to plot the scatter matrix (default is False).
_trim (int, optional) – Number of top results to trim the DataFrame (default is None).
_minimising_param (str, optional) – Parameter over which to determine the best fit (‘chi2’, ‘vgf2’, ‘vgfb2’). Currently, only ‘chi2’ is implemented.
Notes
Updates:
Star.df_chi2
,Star.df_chi2_trimmed
Calculates chi2 values based on model parameters and observed data.
Saves the results into DataFrame star.df_chi2.
Optionally plots the scatter matrix using plot_chi2_matrix if plot=True.
Trims star.df_chi2 to top _trim results if _trim is provided.
Saves star.df_chi2 into a CSV file.
Uses calc_radius and calc_luminosity functions to calculate radius (R) and luminosity (L) based on model parameters.
- Raises:
NotImplementedError – If _minimising_param is not ‘chi2’. (‘vgf2’ and ‘vgfb2’ are to be implemented).
- static calculate_noisy_chi2(star, model, total_iterations=100, refit=True, plot=False, _percentile_threshold=10)¶
Calculate chi2 values for noise added data.
- Parameters:
star (Star object) – Star data.
model (Model object) – Model information.
total_iterations (int, optional) – Number of iterations to perform noisy chi2 calculations (default is 100).
refit (bool, optional) – Whether to refit the data if a previous result exists (default is True).
plot (bool, optional) – Whether to plot the scatter matrix of noisy chi2 results (default is False).
_percentile_threshold (int, optional) – Percentile threshold for filtering chi2 values from the original chi2 results (default is 10).
Notes
Updates:
Star.df_chi2_noisy
Uses star.df_chi2 to filter out top performing parameters based on _percentile_threshold.
Generates total_iterations versions of noisy observed data (da_obs_noisy) by adding Gaussian noise to the original flux.
Computes chi2 values for each noisy iteration and stores the best fitting parameters in star.df_chi2_noisy.
Saves star.df_chi2_noisy into a CSV file named based on star and model information.
- Raises:
AttributeError – If star.df_chi2 does not exist or _percentile_threshold is not within the valid range [0, 100].
- static get_blackbody_spectrum_loglog(log_wavelength, Te, log_scaling_factor)¶
Compute the log-log blackbody spectrum flux.
Adapted from the IUE RDAF 1989.
- Parameters:
log_wavelength (float or list) – Logarithm of the wavelength in Angstroms.
Te (float) – Temperature of the source in Kelvin.
log_scaling_factor (float) – Logarithmic scaling factor to normalize flux (for a star: log10(radius/distance^2)).
- Returns:
log_bbflux – Logarithm of the blackbody flux in ergs/cm2/s/A for the given temperature and wavelength range.
- Return type:
float or list
- static get_parameters_from_chi2_minimization(star, model)¶
Estimate best fit parameters from the least chi2 fit.
- Parameters:
star (Star object) – Star data containing chi2 results and observed data.
model (Model object) – Model information used for fitting.
Notes
Updates:
Star.data_all
,Star.data
,Star.data_not_fitted
,Star.N_Np
,Star.chi2_r
,Star.vgf2
,Star.vgfb2
,Star.
This method retrieves and updates parameters from the best chi2 fit found in star.df_chi2.
It calculates additional statistical metrics for the fitted data.
It raises a warning if any data points have chi2 values significantly higher than the average (3-sigma threshold).
- static get_parameters_from_noisy_chi2_minimization(star, model)¶
Estimate best fit parameters from noisy least chi2 fit.
- Parameters:
star (Star object) – Star data containing noisy chi2 results.
model (Model object) – Model information used for fitting.
Notes
Updates:
Star.data_all
,Star.data
,Star.data_not_fitted
,Star.chi2_median
,Star.chi2_r_median
,Star.vgf2_median
,Star.vgfb2_median
,Star.<dim>_median
,Star.<dim>_error_lower
,Star.<dim>_error_upper
This method estimates the best fit parameters by calculating the median and errors from the noisy chi2 fits:
It uses Fitter.get_realistic_errors_from_iterations() to estimate realistic errors based on noisy fits and grid steps.
Updates various attributes in star related to best fit parameters, errors, and fit quality metrics.
Logs warnings if the best fit parameters differ from their median value.
- static get_realistic_errors_from_iterations(parameter_values, grid_steps, component, para_type='parameter')¶
Estimates errors using spread in the noisy fits and edge cases.
- If the best fit parameter is near a boundary:
Exaggerates errors and issues a warning.
- If spread in iterations is less than step size:
Keeps errors similar to the step size.
- Otherwise:
Calculates the 16nd, 50th, and 84th percentiles of the parameter_values.
Adjusts percentiles to grid_steps boundaries if needed.
Computes lower and upper bound errors in the parameter.
- Parameters:
parameter_values (list) – List of parameter values obtained from noisy fits.
grid_steps (list) – List of grid steps available for the parameter.
component (str) – Component information related to the parameter.
para_type (str, optional) – Type of the parameter (default is ‘parameter’).
- Returns:
para_50 (float) – Median of the parameter_values.
error_lower (float) – Lower bound error in the parameter.
error_upper (float) – Upper bound error in the parameter.
Notes
This method estimates realistic errors based on noisy fits and grid steps:
It calculates percentiles and adjusts them to grid_steps boundaries if necessary.
Issues warnings if the best fit parameter is close to model boundaries.
Handles cases where spread in iterations is minimal or zero.
- static plot_chi2_matrix(df, model, alpha=0.3)¶
Plot scatter matrix for chi2 DataFrame.
- Parameters:
df (DataFrame) – DataFrame containing chi2 values.
model (Model object) – Model information.
alpha (float, optional) – Transparency of points in the scatter matrix plot (default is 0.3).