calc_rss#
- mufasa.UltraCube.calc_rss(ucube, compID, usemask=True, mask=None, return_size=True, update_cube=False, planemask=None, expand=20)[source]#
Calculate the residual sum of squares (RSS) for a spectral cube model fit.
The RSS is computed as the sum of squared differences between the observed data and the model values for each voxel in the spectral cube. The calculation can be restricted using masks and expanded spectral regions.
- 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).
usemask (bool, optional) – Whether to apply a mask during the RSS calculation. If True, regions where the model is zero or invalid are excluded. Default is True.
mask (numpy.ndarray, optional) – A 3D boolean array specifying which voxels to include in the calculation. If None and usemask is True, a default mask derived from the model is used.
return_size (bool, optional) – Whether to return the effective sample size (number of valid voxels) for each spatial pixel. Default is True.
update_cube (bool, optional) – Whether to update the UltraCube instance with the calculated RSS values. Default is False.
planemask (numpy.ndarray, optional) – A 2D spatial boolean array specifying which spatial pixels to calculate RSS for. If None, all spatial pixels are considered. Default is None.
expand (int, optional) – Number of spectral channels to expand the mask region for RSS calculation. Default is 20.
- Returns:
rss (numpy.ndarray) – A 2D array containing the RSS values for each spatial pixel.
nsamp (numpy.ndarray, optional) – A 2D array containing the effective sample size (number of valid spectral samples) for each spatial pixel. Returned only if return_size is True.
final_mask (numpy.ndarray, optional) – The 3D boolean mask used in the RSS calculation. Returned only if explicitly required by the caller.
Notes
The RSS is calculated as:
RSS = Σ (data - model)²
where the sum is taken over spectral channels for each spatial pixel.
If expand > 0, the mask region is extended by the specified number of spectral channels.
- Raises:
ValueError – If the dimensions of the data cube and model cube do not match, or if an invalid mask is provided.