astra.contrib.thecannon.fitting¶
Fitting functions for use in The Cannon.
Module Contents¶
Functions¶
fit_spectrum(flux, ivar, initial_labels, vectorizer, theta, s2, fiducials, scales, dispersion=None, use_derivatives=True, op_kwds=None) |
Fit a single spectrum by least-squared fitting. |
fit_theta_by_linalg(flux, ivar, s2, design_matrix) |
Fit theta coefficients to a set of normalized fluxes for a single pixel. |
chi_sq(theta, design_matrix, flux, ivar, axis=None, gradient=True) |
Calculate the chi-squared difference between the spectral model and flux. |
L1Norm_variation(theta) |
Return the L1 norm of theta (except the first entry) and its derivative. |
fit_pixel_fixed_scatter(flux, ivar, initial_thetas, design_matrix, regularization, censoring_mask, **kwargs) |
Fit theta coefficients and noise residual for a single pixel, using |
-
astra.contrib.thecannon.fitting.fit_spectrum(flux, ivar, initial_labels, vectorizer, theta, s2, fiducials, scales, dispersion=None, use_derivatives=True, op_kwds=None)¶ Fit a single spectrum by least-squared fitting.
Parameters: - flux – The normalized flux values.
- ivar – The inverse variance array for the normalized fluxes.
- initial_labels – The point(s) to initialize optimization from.
- vectorizer – The vectorizer to use when fitting the data.
- theta – The theta coefficients (spectral derivatives) of the trained model.
- s2 – The pixel scatter (s^2) array for each pixel.
- dispersion – [optional] The dispersion (e.g., wavelength) points for the normalized fluxes.
- use_derivatives – [optional]
Boolean
Trueindicating to use analytic derivatives provided by the vectorizer,Noneto calculate on the fly, or a callable function to calculate your own derivatives. - op_kwds – [optional]
Optimization keywords that get passed to
scipy.optimize.leastsq.
Returns: A three-length tuple containing: the optimized labels, the covariance matrix, and metadata associated with the optimization.
-
astra.contrib.thecannon.fitting.fit_theta_by_linalg(flux, ivar, s2, design_matrix)¶ Fit theta coefficients to a set of normalized fluxes for a single pixel.
Parameters: - flux – The normalized fluxes for a single pixel (across many stars).
- ivar – The inverse variance of the normalized flux values for a single pixel across many stars.
- s2 – The noise residual (squared scatter term) to adopt in the pixel.
- design_matrix – The model design matrix.
Returns: The label vector coefficients for the pixel, and the inverse variance matrix.
-
astra.contrib.thecannon.fitting.chi_sq(theta, design_matrix, flux, ivar, axis=None, gradient=True)¶ Calculate the chi-squared difference between the spectral model and flux.
Parameters: - theta – The theta coefficients.
- design_matrix – The model design matrix.
- flux – The normalized flux values.
- ivar – The inverse variances of the normalized flux values.
- axis – [optional] The axis to sum the chi-squared values across.
- gradient – [optional] Return the chi-squared value and its derivatives (Jacobian).
Returns: The chi-squared difference between the spectral model and flux, and optionally, the Jacobian.
-
astra.contrib.thecannon.fitting.L1Norm_variation(theta)¶ Return the L1 norm of theta (except the first entry) and its derivative.
Parameters: theta – An array of finite values. Returns: A two-length tuple containing: the L1 norm of theta (except the first entry), and the derivative of the L1 norm of theta.
-
astra.contrib.thecannon.fitting.fit_pixel_fixed_scatter(flux, ivar, initial_thetas, design_matrix, regularization, censoring_mask, **kwargs)¶ Fit theta coefficients and noise residual for a single pixel, using an initially fixed scatter value.
Parameters: - flux – The normalized flux values.
- ivar – The inverse variance array for the normalized fluxes.
- initial_thetas – A list of initial theta values to start from, and their source. For example: `[(theta_0, “guess”), (theta_1, “old_theta”)]
- design_matrix – The model design matrix.
- regularization – The regularization strength to apply during optimization (Lambda).
- censoring_mask – A per-label censoring mask for each pixel.
Keyword Arguments: - op_method – The optimization method to use. Valid options are:
l_bfgs_b,powell. - op_kwds – A dictionary of arguments that will be provided to the optimizer.
Returns: The optimized theta coefficients, the noise residual
s2, and metadata related to the optimization process.