Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
region: `~specutils.utils.SpectralRegion`
Region within the spectrum to calculate the centroid.
Returns
-------
centroid : float or list (based on region input)
Centroid of the spectrum or within the regions
Notes
-----
This is a helper function for the above `centroid()` method.
"""
if region is not None:
calc_spectrum = extract_region(spectrum, region)
else:
calc_spectrum = spectrum
if hasattr(spectrum, 'mask') and spectrum.mask is not None:
flux = calc_spectrum.flux[~calc_spectrum.mask]
dispersion = calc_spectrum.spectral_axis[~calc_spectrum.mask].quantity
else:
flux = calc_spectrum.flux
dispersion = calc_spectrum.spectral_axis.quantity
if len(flux.shape) > 1:
dispersion = np.tile(dispersion, [flux.shape[0], 1])
# the axis=-1 will enable this to run on single-dispersion, single-flux
# and single-dispersion, multiple-flux
return np.sum(flux * dispersion, axis=-1) / np.sum(flux, axis=-1)
region: `~specutils.utils.SpectralRegion`
Region within the spectrum to calculate the SNR.
Returns
-------
snr : `~astropy.units.Quantity` or list (based on region input)
Signal to noise ratio of the spectrum or within the regions
Notes
-----
This is a helper function for the above `snr_derived()` method.
"""
if region is not None:
calc_spectrum = extract_region(spectrum, region)
else:
calc_spectrum = spectrum
flux = calc_spectrum.flux
# Values that are exactly zero (padded) are skipped
n = len(flux)
# For spectra shorter than this, no value can be returned
if n > 4:
signal = np.median(flux)
noise = 0.6052697 * np.median(np.abs(2.0 * flux[2:n-2] - flux[0:n-4] - flux[4:n]))
return signal / noise
else:
return 0.0
def _compute_fwhm(spectrum, regions=None):
"""
This is a helper function for the above `fwhm()` method.
"""
if regions is not None:
calc_spectrum = extract_region(spectrum, regions)
else:
calc_spectrum = spectrum
flux = calc_spectrum.flux
spectral_axis = calc_spectrum.spectral_axis
if flux.ndim > 1:
return [_compute_single_fwhm(x, spectral_axis) for x in flux]
else:
return _compute_single_fwhm(flux, spectral_axis)
region: `~specutils.utils.SpectralRegion`
Region within the spectrum to calculate the SNR.
Returns
-------
snr : `~astropy.units.Quantity` or list (based on region input)
Signal to noise ratio of the spectrum or within the regions
Notes
-----
This is a helper function for the above `snr()` method.
"""
if region is not None:
calc_spectrum = extract_region(spectrum, region)
else:
calc_spectrum = spectrum
flux = calc_spectrum.flux
uncertainty = calc_spectrum.uncertainty.array * spectrum.uncertainty.unit
# the axis=-1 will enable this to run on single-dispersion, single-flux
# and single-dispersion, multiple-flux
return np.mean(flux / uncertainty, axis=-1)
def _compute_gaussian_sigma_width(spectrum, regions=None):
"""
This is a helper function for the above `gaussian_sigma_width()` method.
"""
if regions is not None:
calc_spectrum = extract_region(spectrum, regions)
else:
calc_spectrum = spectrum
if hasattr(spectrum, 'mask') and spectrum.mask is not None:
flux = calc_spectrum.flux[~spectrum.mask]
spectral_axis = calc_spectrum.spectral_axis[~spectrum.mask]
else:
flux = calc_spectrum.flux
spectral_axis = calc_spectrum.spectral_axis
centroid_result = centroid(spectrum, regions)
if flux.ndim > 1:
spectral_axis = np.broadcast_to(spectral_axis, flux.shape, subok=True)
centroid_result = centroid_result[:, np.newaxis]
if spectral_region is not None:
if not check_unit_compatibility(spec, spectral_region):
self.set_status("Region units are not compatible with "
"selected data's spectral axis units.")
return self.clear_statistics()
spectral_region = clip_region(spec, spectral_region)
if spectral_region is None:
self.set_status("Region out of bound.")
return self.clear_statistics()
try:
idx1, idx2 = spectral_region.bounds
if idx1 == idx2:
self.set_status("Region over single value.")
return self.clear_statistics()
spec = extract_region(spec, spectral_region)
if not len(spec.flux) > 0:
self.set_status("Regione range is too small.")
return self.clear_statistics()
except ValueError as e:
self.set_status("Region could not be extracted "
"from target data.")
return self.clear_statistics()
elif self._workspace_has_region():
self.set_status("Region has no units")
return self.clear_statistics()
# Compute stats and update widget:
self.stats = compute_stats(spec)
self._update_stat_widgets(self.stats)
self.set_status(self._get_target_name())