calc_chisq#
- mufasa.UltraCube.calc_chisq(ucube, compID, reduced=False, usemask=False, mask=None, expand=20)[source]#
Calculate the chi-squared (χ²) or reduced chi-squared value for a spectral cube model fit.
This function computes the goodness-of-fit by comparing the data in the spectral cube to the model values. Optionally, the calculation can include masking and spectral region expansion.
- Parameters:
ucube (UltraCube) – An instance of the UltraCube class containing the spectral data and fitted models.
compID (int or str) – The component ID or number of components in the model to evaluate. If compID is 0, the model is assumed to be a flat baseline (y = 0).
reduced (bool, optional) – Whether to compute the reduced chi-squared value by normalizing with the degrees of freedom. If False, computes the standard chi-squared value. Default is False.
usemask (bool, optional) – Whether to apply a mask to exclude invalid or unwanted regions from the calculation. Default is False.
mask (numpy.ndarray, optional) – A 3D boolean array specifying which voxels to include in the chi-squared calculation. If None and usemask is True, a default mask derived from the model is used.
expand (int, optional) – Number of spectral channels to expand the mask region. Default is 20.
- Returns:
chisq – A 2D array representing the chi-squared (or reduced chi-squared) values for each spatial pixel in the cube.
- Return type:
Notes
The chi-squared statistic is calculated as:
χ² = Σ [(data - model)² / rms²]
where the sum is taken over spectral channels for each spatial pixel.
If reduced=True, the reduced chi-squared is computed by dividing χ² by the degrees of freedom, which is the number of samples minus the number of model parameters.
- Raises:
ValueError – If the dimensions of the data cube and model cube do not match.