raccoon package#
Submodules#
raccoon.util module#
- class raccoon.util.Util[source]#
Bases:
object- classmethod find_extrema(curve, init_peak_detection_proximity_threshold=50, is_peak=True)[source]#
Find peaks or troughs of a curve.
- Parameters:
curve (np.ndarray) β 1D numpy array
init_peak_detection_proximity_threshold (int) β Minimum distance between two extrema in pixels
is_peak (bool) β If True, find peaks, otherwise find troughs
- Returns:
Indices of the extrema
- Return type:
np.ndarray
- classmethod find_init_peaks_troughs_mids(curve, init_peak_detection_proximity_threshold=50)[source]#
Find initial peaks, troughs, and midpoints of a curve.
- Parameters:
curve (np.ndarray) β 1D numpy array
init_peak_detection_proximity_threshold (int) β Minimum distance between two extrema in pixels
- Returns:
Peaks, troughs, and midpoints
- Return type:
Tuple[np.ndarray, np.ndarray, np.ndarray]
- classmethod fit_sine_function_to_extrema_polynomial(extrema_positions, extrema_vals, is_peak, n_amplitude, n_offset, n_frequency, phi_0=None)[source]#
Fit a wiggly function to the peaks and troughs of a curve.
- Parameters:
extrema_positions (np.ndarray) β Positions of the extrema
extrema_vals (np.ndarray) β Values of the extrema
is_peak (np.ndarray) β If True, the extrema are peaks, otherwise they are troughs
n_amplitude (int) β Degree of the polynomial for the amplitude
n_offset (int) β Degree of the polynomial for the offset
n_frequency (int) β Degree of the polynomial for the frequency
phi_0 (float) β Phase offset
- Returns:
amplitude coefficients, offset coefficients, frequency coefficients, phase offset
- classmethod fit_sine_function_to_extrema_spline(extrema_positions, extrema_vals, is_peak, n_amplitude, n_frequency, phi_0=None)[source]#
Fit a wiggly function to the peaks and troughs of a curve using splines for amplitude and frequency.
- Parameters:
extrema_positions (np.ndarray) β Positions of the extrema
extrema_vals (np.ndarray) β Values of the extrema
is_peak (np.ndarray) β If True, the extrema are peaks, otherwise they are troughs
n_amplitude (int) β Degree of the polynomial for the amplitude
n_offset (int) β Degree of the polynomial for the offset
n_frequency (int) β Degree of the polynomial for the frequency
phi_0 (float) β Phase offset
- Returns:
amplitude spline, frequency spline, phase offset
- Return type:
tuple
- classmethod fitted_sine_function_polynomial(xs, amplitude_coeffs, offset_coeffs, frequency_coeffs, phi_0)[source]#
Evaluate the fitted sine function at x.
- Parameters:
xs (np.ndarray) β x values
amplitude_coeffs (np.ndarray) β Amplitude coefficients
offset_coeffs (np.ndarray) β Offset coefficients
frequency_coeffs (np.ndarray) β Frequency coefficients
phi_0 (float) β Phase offset
- Returns:
Fitted sine function values
- Return type:
np.ndarray
- classmethod fitted_sine_function_spline(xs, amp_spline, frequency_spline, phi_0)[source]#
Evaluate the fitted sine function at x.
- Parameters:
xs (np.ndarray) β x values
amplitude_coeffs (np.ndarray) β Amplitude coefficients
offset_coeffs (np.ndarray) β Offset coefficients
frequency_coeffs (np.ndarray) β Frequency coefficients
phi_0 (float) β Phase offset
- Returns:
Fitted sine function values
- Return type:
np.ndarray
- classmethod get_init_params_polynomial(curve, scaled_wavelengths, n_amplitude=2, n_offset=7, n_frequency=1, init_peak_detection_proximity_threshold=50, plot=False)[source]#
Get initial parameters for the curve fitting.
- Parameters:
curve (np.ndarray) β 1D numpy array
n_amplitude (int) β Degree of the polynomial for the amplitude
n_frequency (int) β Degree of the polynomial for the frequency
n_offset (int) β Degree of the polynomial for the offset
plot (bool) β If True, plot the initial parameters
- Returns:
Initial amplitude, frequency, offset, and phase
- Return type:
Tuple[np.ndarray, np.ndarray, np.ndarray, float]
- classmethod get_init_params_spline(curve, scaled_wavelengths, n_amplitude=2, n_frequency=1, init_peak_detection_proximity_threshold=50, plot=False)[source]#
Get initial parameters for curve fitting using spline-based amplitude and frequency.
- Parameters:
curve β 1D numpy array of spectral values
scaled_wavelengths β Corresponding wavelength values
n_amplitude β Number of knots for amplitude spline
n_frequency β Number of knots for frequency spline
init_peak_detection_proximity_threshold β Minimum distance between extrema (in indices)
plot β Whether to show diagnostic plots
- Returns:
Tuple containing (amplitude_spline, frequency_spline, phase_offset)
- classmethod get_linear_freq_coeffs_from_extrema(extrema, scaled_wavelengths)[source]#
Get the linear frequency coefficients from the extrema.
- Parameters:
extrema (np.ndarray) β Extrema
scaled_wavelengths (np.ndarray) β Scaled wavelengths
- Returns:
Linear frequency coefficients
- Return type:
np.ndarray
raccoon.wiggle_cleaner module#
- class raccoon.wiggle_cleaner.WiggleCleaner(wavelengths, datacube, noise_cube, gaps=None, symmetric_sharpening=False, asymmetric_sharpening=False, continuum_diff_polynomial_order=2)[source]#
Bases:
objectWiggleCleaner class for cleaning modulation/wiggles in spectral data.
- BLUE = '#377eb8'#
- GREEN = '#4daf4a'#
- GREY = '#999999'#
- ORANGE = '#ff7f00'#
- PURPLE = '#984ea3'#
- RED = '#e41a1c'#
- property asymmetric_sharpening#
Get the asymmetric sharpening flag.
- Returns:
Asymmetric sharpening flag
- Return type:
bool
- clean_cube(wiggle_detection_sigma_threshold=3, wiggle_detection_variance_ratio_threshold=0.8, n_amplitude=10, n_frequency=1, min_n_amplitude=None, min_n_frequency=None, n_amplitude_for_detection=None, n_frequency_for_detection=None, specified_noise_level=0, cleaning_mask=None, init_peak_detection_proximity_threshold=200, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, conserve_flux=True, outlier_rejection_method='fdr', fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=5, extract_uncertainty=True, symmetric_sharpening=False, asymmetric_sharpening=False, selection_criteria='bic', min_selection_difference=None, num_samples_uncertainty_region=1000, verbose=True, plot=True, fit_full_model=True)[source]#
Clean the datacube.
- Parameters:
sigma_threshold (float) β Sigma threshold
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
min_n_amplitude (int) β Minimum number of amplitude parameters
min_n_frequency (int) β Minimum number of frequency parameters
specified_noise_level (float) β Artificial noise level in fraction of the spectrum
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
aperture_size (int) β Aperture size. Spaxels at the edges with width less than this value will not be cleaned.
min_x (int) β Minimum spaxel x to set lower limit for the cleaning process
max_x (int) β Maximum spaxel x to set upper limit for the cleaning process
min_y (int) β Minimum spaxel y to set lower limit for the cleaning process
max_y (int) β Maximum spaxel y to set upper limit for the cleaning process
conserve_flux (bool) β If True, conserve flux in each spaxel
cleaning_mask (np.ndarray) β Mask for cleaning, if None, clean all spaxels
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
use_huber_loss (bool) β If True, use Huber loss function
huber_delta (float) β Delta for Huber loss function
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
sigma_clip_sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Number of sigma clip iterations
extract_uncertainty (bool) β If True, extract the uncertainties
symmetric_sharpening (bool) β If True, use symmetric sharpening
asymmetric_sharpening (bool) β If True, use asymmetric sharpening
selection_criteria (str) β Selection criteria for model selection, βbicβ or βchi2β
min_selection_difference (float) β Minimum difference between the best and the next best model selection criteria
verbose (bool) β If True, print the results
plot (bool) β If True, plot the results
- Returns:
Cleaned datacube
- Return type:
np.ndarray
- Returns:
Cleaned datacube
- Return type:
np.ndarray
- configure_noise(wiggle_signal, wiggle_noise, specified_noise_level)[source]#
Configure the noise.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
specified_noise_level (float) β User-defined noise level to be used instead of the actual noise. Set to 0 to disable.
- Returns:
modified wiggle noise
- Return type:
np.ndarray
- configure_polynomial_ns(n_amplitude=None, n_frequency=None)[source]#
Configure the number of parameters.
- Parameters:
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
Number of amplitude and frequency parameters
- Return type:
Tuple
- cost_function(params, wiggle_signal, wiggle_noise)[source]#
Compute the cost function (sum of squared residuals).
- Parameters:
params (np.ndarray) β Parameters
wiggle_signal (np.ndarray) β wiggle data
wiggle_noise (np.ndarray) β wiggle_noise
- Returns:
cost
- Return type:
float
- fit_wiggle(x, y, aperture_radius=4, annulus_outer_radius=5, annulus_inner_radius=3, n_amplitude=None, n_frequency=None, specified_noise_level=0, init_peak_detection_proximity_threshold=200, do_interim_fit_phase_only=False, extract_covariance=True, outlier_rejection_method=None, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=5, symmetric_sharpening=False, asymmetric_sharpening=False, plot=True, verbose=True, fit_full_model=True, save_figure_dir=None)[source]#
Fit the wiggle data.
- Parameters:
wiggle_signal (np.ndarray) β Wiggle data
wiggle_noise (np.ndarray) β wiggle noise
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
specified_noise_level (float) β User-defined noise level to be used instead of the actual noise. Set to 0 to disable.
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
plot (bool) β If True, plot the results
verbose (bool) β If True, print the results
do_interim_fit_phase_only (bool) β If True, do an interim fit with phase only
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
use_huber_loss (bool) β If True, use Huber loss function for outlier rejection with FDR
huber_delta (float) β Delta for Huber loss function
sigma_clip_sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Number of sigma clip iterations
extract_uncertainty (bool) β If True, extract the uncertainties
- Returns:
Fitted parameters
- Return type:
np.ndarray
- fit_wiggle_with_model_selection(x, y, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, n_amplitude=10, n_frequency=1, min_n_amplitude=None, min_n_frequency=None, specified_noise_level=0, init_peak_detection_proximity_threshold=200, plot=False, selection_criteria='bic', min_selection_difference=None, extract_covariance=True, outlier_rejection_method=None, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=3, symmetric_sharpening=False, asymmetric_sharpening=False, fit_full_model=False)[source]#
Fit the wiggle signal with selecting amplitude polynomial order based on BIC.
- Parameters:
x (int) β x coordinate
y (int) β y coordinate
aperture_radius (int) β Aperture radius
annulus_outer_radius (int) β Annulus outer radius
annulus_inner_radius (int) β Annulus inner radius
n_amplitude (int) β Maximum number of amplitude parameters
n_frequency (int) β Number of frequency parameters
min_n_amplitude (int) β Minimum number of amplitude parameters
min_n_frequency (int) β Minimum number of frequency parameters
specified_noise_level (float) β Artificial noise level
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
plot (bool) β If True, plot the results
selection_criteria (str) β Selection criteria, βbicβ or βchi2β
sigma_clip_sigma (float) β Sigma clip threshold
sigma_clip_max_iterations (int) β Number of sigma clip iterations
combine_bic_weighted (bool) β If True, combine the BIC weighted by the number of parameters
extract_uncertainty (bool) β If True, extract the uncertainties
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
huber_delta (float) β Delta for Huber loss function
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
sigma_clip_sigma β Sigma threshold for sigma clipping
sigma_clip_max_iterations β Number of sigma clip iterations
extract_uncertainty β If True, extract the uncertainties
symmetric_sharpening (bool) β If True, use symmetric sharpening
asymmetric_sharpening (bool) β If True, use asymmetric sharpening
selection_criteria β Selection criteria for model selection, βbicβ or βchi2β
min_selection_difference (float) β Minimum difference between the best and the next best model selection criteria
verbose (bool) β If True, print the results
plot β If True, plot the results
- Returns:
Fitted parameters
- Return type:
np.ndarray
- get_a1_a2(params)[source]#
Get the a_1 and a_2 parameters for asymmetric and symmetric sharpening from the params array.
- Parameters:
params (np.ndarray) β Parameters
- Returns:
a_1 and a_2 parameters
- Return type:
Tuple[float, float]
- get_design_matrix(aperture_spectra, annulus_spectra, annulus_outer_radius, a, wavelengths)[source]#
Get the design matrix for the full model fit.
- Parameters:
a (float) β Non-linear parameter, the exponent for the wavelength term in power- law component of the continuum
annulus_outer_radius (float) β Outer radius of the annulus
aperture_spectra (np.ndarray) β Aperture spectra
annulus_spectra (np.ndarray) β Annulus spectra
wavelengths (np.ndarray) β Wavelengths
- Returns:
Design matrix
- Return type:
np.ndarray
- get_exponent_param(params)[source]#
Get the exponent parameter for the wavelength term in the power-law component of the continuum.
- Parameters:
params (np.ndarray) β Parameters
- Returns:
Exponent parameter
- Return type:
float
- get_model_selection_metric(wiggle_signal, wiggle_noise, result_params, selection_criteria='bic')[source]#
Get the fit metric: BIC or chi^2.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
result_params (np.ndarray) β Fitted parameters
selection_criteria (str) β Selection criteria, βbicβ or βchi2β
- Returns:
Fit metric
- Return type:
float
- get_model_uncertainty(result_params, cov_matrix, num_samples_uncertainty_region=1000)[source]#
Get the model uncertainty.
- Parameters:
result_params (np.ndarray) β Fitted parameters
cov_matrix (np.ndarray) β Covariance matrix
num_samples_uncertainty_region (int) β Number of samples for uncertainty region
- Returns:
Model uncertainty at each wavelength
- Return type:
np.ndarray
- get_residual_func(wiggle_signal, wiggle_noise)[source]#
Get the residual function.
- Parameters:
wiggle_signal (np.ndarray) β wiggle data
wiggle_noise (np.ndarray) β wiggle noise
- Returns:
Residual function
- Return type:
Callable
- get_residual_func_phase_only(init_params, wiggle_signal, wiggle_noise)[source]#
Get the residual function with phase only.
- Parameters:
params (np.ndarray) β Parameters
init_params (np.ndarray) β Initial parameters
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
- Returns:
Residual function
- Return type:
Callable
- get_spectra_set(x, y, aperture_radius, annulus_outer_radius, annulus_inner_radius)[source]#
Get the spectra set.
- Parameters:
x (int) β x coordinate
y (int) β y coordinate
aperture_radius (int) β Aperture radius
annulus_outer_radius (int) β annulus outer radius
annulus_inner_radius (int) β annulus inner radius
- Returns:
spectra, noise, aperture_spectra, aperture_noise, annulus_spectra, annulus_noise
- Return type:
Tuple of np.ndarray
- get_wiggle_signal(x, y, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, plot=False)[source]#
Get the wiggle signal for preliminary assessments.
- Parameters:
spaxel_x (int) β Spaxel x
spaxel_y (int) β Spaxel y
aperture (int) β aperture size to sum the spectra to average out the wiggles
- Returns:
Modulation wiggle_signal and noise
- Return type:
Tuple of np.ndarray
- is_wiggle_detected(wiggle_signal, wiggle_noise, result_params, sigma_threshold=5, variance_ratio_threshold=0.8, verbose=True)[source]#
Check if wiggle is detected.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle_noise
sigma_threshold (float) β Sigma threshold
variance_ratio_threshold (float) β Variance ratio threshold, set 0 to disable
verbose (bool) β If True, print the results
- Returns:
True if wiggle is detected
- Return type:
bool
- model_full_fit(params, x, y, aperture_radius, annulus_outer_radius=0, annulus_inner_radius=0)[source]#
Compute the full model fit.
- Parameters:
params (np.ndarray) β Parameters
x (np.ndarray) β x coordinates
y (np.ndarray) β y coordinates
aperture_radius (float) β Aperture radius
annulus_outer_radius (float) β Outer radius of the annulus
annulus_inner_radius (float) β Inner radius of the annulus
- Returns:
Full model, spectra, and total noise
- Return type:
Tuple[np.ndarray, np.ndarray, np.ndarray]
- plot_full_fit_model(bestfit_model, model_uncertainty, spectra, noise, wiggle_model=None, save_figure_info={'dir': None, 'x': None, 'y': None})[source]#
Plot the full fit model.
- Parameters:
bestfit_model (np.ndarray) β Best-fit model
model_uncertainty (np.ndarray) β Model uncertainty
spectra (np.ndarray) β Spectra
noise (np.ndarray) β Noise
- Returns:
None
- Return type:
None
- plot_wiggle_model(wiggle_signal, wiggle_noise, result_params, cov_matrix=None, num_samples_uncertainty_region=1000, save_figure_info={'dir': None, 'x': None, 'y': None})[source]#
Plot the model.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
result_params (np.ndarray) β Fitted parameters
cov_matrix (np.ndarray) β Covariance matrix
num_samples_uncertainty_region (int) β Number of samples for uncertainty region
- Returns:
None
- Return type:
None
- reject_outliers(residual, num_params=0, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, sigma_clip_sigma=5, sigma_clip_max_iterations=5)[source]#
Reject outliers using the selected method.
- Parameters:
residual (np.ndarray) β Residuals
num_params (int) β Number of parameters
fdr_q (float) β FDR correction threshold
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject
huber_delta (float) β Delta for Huber loss function
sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Maximum number of iterations for sigma clipping
- Returns:
Indices of outliers
- Return type:
np.ndarray
- residual_vector(params, wiggle_signal, wiggle_noise)[source]#
Get the residual vector.
- Parameters:
params (np.ndarray) β Parameters
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle_noise
- Returns:
Residual vector
- Return type:
np.ndarray
- residual_vector_full_fit(params, x, y, aperture_radius, annulus_outer_radius=0, annulus_inner_radius=0)[source]#
Compute the residual vector for the full model fit.
- Parameters:
params (np.ndarray) β Parameters
x (np.ndarray) β x coordinates
y (np.ndarray) β t coordinates
aperture_radius (float) β Aperture radius
annulus_outer_radius (float) β Outer radius of the annulus
annulus_inner_radius (float) β Inner radius of the annulus
- Returns:
Residual vector
- Return type:
np.ndarray
- scale_wavelengths_negative1_to_1(w)[source]#
Scale the wavelengths to -1 to 1.
- Parameters:
w (np.ndarray) β Wavelengths
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- scale_wavelengths_to_0_1(w)[source]#
Scale the wavelengths to 0 to 1.
- Parameters:
w (np.ndarray) β Wavelengths
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- property scaled_w#
Scaled wavelengths.
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- set_gaps(gaps)[source]#
Set the gaps to be ignored during the fitting process.
- Parameters:
gaps (list of tuples) β List of wavelength ranges to be ignored
- Returns:
None
- Return type:
None
- set_params(amplitude_params, frequency_params, phi_0, n_amplitude=None, n_frequency=None)[source]#
Set the parameters. Opposite function of the split_params function.
- Parameters:
amplitude_params (np.ndarray) β Amplitude parameters
frequency_params (np.ndarray) β Frequency parameters
phi_0 (float) β Phase
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
Parameters
- Return type:
np.ndarray
- static solve_ridge_regression(A, spectra)[source]#
Solve the ridge regression problem.
- Parameters:
A (np.ndarray) β Design matrix
spectra (np.ndarray) β Target spectra
- Returns:
Coefficients
- Return type:
np.ndarray
- split_params(params, n_amplitude=None, n_frequency=None)[source]#
Split the parameters. Opposite of the set_params function.
- Parameters:
params (np.ndarray) β Parameters
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
amplitude parameters, frequency parameters, and phi_0
- Return type:
Tuple
- property symmetric_sharpening#
Get the symmetric sharpening flag.
- Returns:
Symmetric sharpening flag
- Return type:
bool
- wiggle_func(xs, amplitude_params, frequency_params, phi, a_1=0, a_2=0)[source]#
Get the wiggle function.
- Parameters:
xs (np.ndarray) β Scaled wavelengths
frequency_params (np.ndarray) β Frequency parameters
amplitude_params (np.ndarray) β Amplitude parameters
phi (float) β Phase
- Returns:
Wiggle function
- Return type:
np.ndarray
Module contents#
Top-level package for raccoon.
- class raccoon.WiggleCleaner(wavelengths, datacube, noise_cube, gaps=None, symmetric_sharpening=False, asymmetric_sharpening=False, continuum_diff_polynomial_order=2)[source]#
Bases:
objectWiggleCleaner class for cleaning modulation/wiggles in spectral data.
- BLUE = '#377eb8'#
- GREEN = '#4daf4a'#
- GREY = '#999999'#
- ORANGE = '#ff7f00'#
- PURPLE = '#984ea3'#
- RED = '#e41a1c'#
- property asymmetric_sharpening#
Get the asymmetric sharpening flag.
- Returns:
Asymmetric sharpening flag
- Return type:
bool
- clean_cube(wiggle_detection_sigma_threshold=3, wiggle_detection_variance_ratio_threshold=0.8, n_amplitude=10, n_frequency=1, min_n_amplitude=None, min_n_frequency=None, n_amplitude_for_detection=None, n_frequency_for_detection=None, specified_noise_level=0, cleaning_mask=None, init_peak_detection_proximity_threshold=200, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, conserve_flux=True, outlier_rejection_method='fdr', fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=5, extract_uncertainty=True, symmetric_sharpening=False, asymmetric_sharpening=False, selection_criteria='bic', min_selection_difference=None, num_samples_uncertainty_region=1000, verbose=True, plot=True, fit_full_model=True)[source]#
Clean the datacube.
- Parameters:
sigma_threshold (float) β Sigma threshold
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
min_n_amplitude (int) β Minimum number of amplitude parameters
min_n_frequency (int) β Minimum number of frequency parameters
specified_noise_level (float) β Artificial noise level in fraction of the spectrum
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
aperture_size (int) β Aperture size. Spaxels at the edges with width less than this value will not be cleaned.
min_x (int) β Minimum spaxel x to set lower limit for the cleaning process
max_x (int) β Maximum spaxel x to set upper limit for the cleaning process
min_y (int) β Minimum spaxel y to set lower limit for the cleaning process
max_y (int) β Maximum spaxel y to set upper limit for the cleaning process
conserve_flux (bool) β If True, conserve flux in each spaxel
cleaning_mask (np.ndarray) β Mask for cleaning, if None, clean all spaxels
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
use_huber_loss (bool) β If True, use Huber loss function
huber_delta (float) β Delta for Huber loss function
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
sigma_clip_sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Number of sigma clip iterations
extract_uncertainty (bool) β If True, extract the uncertainties
symmetric_sharpening (bool) β If True, use symmetric sharpening
asymmetric_sharpening (bool) β If True, use asymmetric sharpening
selection_criteria (str) β Selection criteria for model selection, βbicβ or βchi2β
min_selection_difference (float) β Minimum difference between the best and the next best model selection criteria
verbose (bool) β If True, print the results
plot (bool) β If True, plot the results
- Returns:
Cleaned datacube
- Return type:
np.ndarray
- Returns:
Cleaned datacube
- Return type:
np.ndarray
- configure_noise(wiggle_signal, wiggle_noise, specified_noise_level)[source]#
Configure the noise.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
specified_noise_level (float) β User-defined noise level to be used instead of the actual noise. Set to 0 to disable.
- Returns:
modified wiggle noise
- Return type:
np.ndarray
- configure_polynomial_ns(n_amplitude=None, n_frequency=None)[source]#
Configure the number of parameters.
- Parameters:
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
Number of amplitude and frequency parameters
- Return type:
Tuple
- cost_function(params, wiggle_signal, wiggle_noise)[source]#
Compute the cost function (sum of squared residuals).
- Parameters:
params (np.ndarray) β Parameters
wiggle_signal (np.ndarray) β wiggle data
wiggle_noise (np.ndarray) β wiggle_noise
- Returns:
cost
- Return type:
float
- fit_wiggle(x, y, aperture_radius=4, annulus_outer_radius=5, annulus_inner_radius=3, n_amplitude=None, n_frequency=None, specified_noise_level=0, init_peak_detection_proximity_threshold=200, do_interim_fit_phase_only=False, extract_covariance=True, outlier_rejection_method=None, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=5, symmetric_sharpening=False, asymmetric_sharpening=False, plot=True, verbose=True, fit_full_model=True, save_figure_dir=None)[source]#
Fit the wiggle data.
- Parameters:
wiggle_signal (np.ndarray) β Wiggle data
wiggle_noise (np.ndarray) β wiggle noise
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
specified_noise_level (float) β User-defined noise level to be used instead of the actual noise. Set to 0 to disable.
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
plot (bool) β If True, plot the results
verbose (bool) β If True, print the results
do_interim_fit_phase_only (bool) β If True, do an interim fit with phase only
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
use_huber_loss (bool) β If True, use Huber loss function for outlier rejection with FDR
huber_delta (float) β Delta for Huber loss function
sigma_clip_sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Number of sigma clip iterations
extract_uncertainty (bool) β If True, extract the uncertainties
- Returns:
Fitted parameters
- Return type:
np.ndarray
- fit_wiggle_with_model_selection(x, y, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, n_amplitude=10, n_frequency=1, min_n_amplitude=None, min_n_frequency=None, specified_noise_level=0, init_peak_detection_proximity_threshold=200, plot=False, selection_criteria='bic', min_selection_difference=None, extract_covariance=True, outlier_rejection_method=None, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, use_huber_loss=False, huber_delta=1.35, sigma_clip_sigma=5, sigma_clip_max_iterations=3, symmetric_sharpening=False, asymmetric_sharpening=False, fit_full_model=False)[source]#
Fit the wiggle signal with selecting amplitude polynomial order based on BIC.
- Parameters:
x (int) β x coordinate
y (int) β y coordinate
aperture_radius (int) β Aperture radius
annulus_outer_radius (int) β Annulus outer radius
annulus_inner_radius (int) β Annulus inner radius
n_amplitude (int) β Maximum number of amplitude parameters
n_frequency (int) β Number of frequency parameters
min_n_amplitude (int) β Minimum number of amplitude parameters
min_n_frequency (int) β Minimum number of frequency parameters
specified_noise_level (float) β Artificial noise level
init_peak_detection_proximity_threshold (float) β Proximity lower limit in Angstrom for initial identifaction of peaks and troughs
plot (bool) β If True, plot the results
selection_criteria (str) β Selection criteria, βbicβ or βchi2β
sigma_clip_sigma (float) β Sigma clip threshold
sigma_clip_max_iterations (int) β Number of sigma clip iterations
combine_bic_weighted (bool) β If True, combine the BIC weighted by the number of parameters
extract_uncertainty (bool) β If True, extract the uncertainties
outlier_rejection_method (str) β Outlier rejection method, βfdrβ or βsigma_clipβ, set None to disable
huber_delta (float) β Delta for Huber loss function
fdr_alpha (float) β False discovery rate (FDR) correction threshold, smaller value will reject less outliers
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject using FDR
sigma_clip_sigma β Sigma threshold for sigma clipping
sigma_clip_max_iterations β Number of sigma clip iterations
extract_uncertainty β If True, extract the uncertainties
symmetric_sharpening (bool) β If True, use symmetric sharpening
asymmetric_sharpening (bool) β If True, use asymmetric sharpening
selection_criteria β Selection criteria for model selection, βbicβ or βchi2β
min_selection_difference (float) β Minimum difference between the best and the next best model selection criteria
verbose (bool) β If True, print the results
plot β If True, plot the results
- Returns:
Fitted parameters
- Return type:
np.ndarray
- get_a1_a2(params)[source]#
Get the a_1 and a_2 parameters for asymmetric and symmetric sharpening from the params array.
- Parameters:
params (np.ndarray) β Parameters
- Returns:
a_1 and a_2 parameters
- Return type:
Tuple[float, float]
- get_design_matrix(aperture_spectra, annulus_spectra, annulus_outer_radius, a, wavelengths)[source]#
Get the design matrix for the full model fit.
- Parameters:
a (float) β Non-linear parameter, the exponent for the wavelength term in power- law component of the continuum
annulus_outer_radius (float) β Outer radius of the annulus
aperture_spectra (np.ndarray) β Aperture spectra
annulus_spectra (np.ndarray) β Annulus spectra
wavelengths (np.ndarray) β Wavelengths
- Returns:
Design matrix
- Return type:
np.ndarray
- get_exponent_param(params)[source]#
Get the exponent parameter for the wavelength term in the power-law component of the continuum.
- Parameters:
params (np.ndarray) β Parameters
- Returns:
Exponent parameter
- Return type:
float
- get_model_selection_metric(wiggle_signal, wiggle_noise, result_params, selection_criteria='bic')[source]#
Get the fit metric: BIC or chi^2.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
result_params (np.ndarray) β Fitted parameters
selection_criteria (str) β Selection criteria, βbicβ or βchi2β
- Returns:
Fit metric
- Return type:
float
- get_model_uncertainty(result_params, cov_matrix, num_samples_uncertainty_region=1000)[source]#
Get the model uncertainty.
- Parameters:
result_params (np.ndarray) β Fitted parameters
cov_matrix (np.ndarray) β Covariance matrix
num_samples_uncertainty_region (int) β Number of samples for uncertainty region
- Returns:
Model uncertainty at each wavelength
- Return type:
np.ndarray
- get_residual_func(wiggle_signal, wiggle_noise)[source]#
Get the residual function.
- Parameters:
wiggle_signal (np.ndarray) β wiggle data
wiggle_noise (np.ndarray) β wiggle noise
- Returns:
Residual function
- Return type:
Callable
- get_residual_func_phase_only(init_params, wiggle_signal, wiggle_noise)[source]#
Get the residual function with phase only.
- Parameters:
params (np.ndarray) β Parameters
init_params (np.ndarray) β Initial parameters
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
- Returns:
Residual function
- Return type:
Callable
- get_spectra_set(x, y, aperture_radius, annulus_outer_radius, annulus_inner_radius)[source]#
Get the spectra set.
- Parameters:
x (int) β x coordinate
y (int) β y coordinate
aperture_radius (int) β Aperture radius
annulus_outer_radius (int) β annulus outer radius
annulus_inner_radius (int) β annulus inner radius
- Returns:
spectra, noise, aperture_spectra, aperture_noise, annulus_spectra, annulus_noise
- Return type:
Tuple of np.ndarray
- get_wiggle_signal(x, y, aperture_radius=4, annulus_outer_radius=0, annulus_inner_radius=0, plot=False)[source]#
Get the wiggle signal for preliminary assessments.
- Parameters:
spaxel_x (int) β Spaxel x
spaxel_y (int) β Spaxel y
aperture (int) β aperture size to sum the spectra to average out the wiggles
- Returns:
Modulation wiggle_signal and noise
- Return type:
Tuple of np.ndarray
- is_wiggle_detected(wiggle_signal, wiggle_noise, result_params, sigma_threshold=5, variance_ratio_threshold=0.8, verbose=True)[source]#
Check if wiggle is detected.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle_noise
sigma_threshold (float) β Sigma threshold
variance_ratio_threshold (float) β Variance ratio threshold, set 0 to disable
verbose (bool) β If True, print the results
- Returns:
True if wiggle is detected
- Return type:
bool
- model_full_fit(params, x, y, aperture_radius, annulus_outer_radius=0, annulus_inner_radius=0)[source]#
Compute the full model fit.
- Parameters:
params (np.ndarray) β Parameters
x (np.ndarray) β x coordinates
y (np.ndarray) β y coordinates
aperture_radius (float) β Aperture radius
annulus_outer_radius (float) β Outer radius of the annulus
annulus_inner_radius (float) β Inner radius of the annulus
- Returns:
Full model, spectra, and total noise
- Return type:
Tuple[np.ndarray, np.ndarray, np.ndarray]
- plot_full_fit_model(bestfit_model, model_uncertainty, spectra, noise, wiggle_model=None, save_figure_info={'dir': None, 'x': None, 'y': None})[source]#
Plot the full fit model.
- Parameters:
bestfit_model (np.ndarray) β Best-fit model
model_uncertainty (np.ndarray) β Model uncertainty
spectra (np.ndarray) β Spectra
noise (np.ndarray) β Noise
- Returns:
None
- Return type:
None
- plot_wiggle_model(wiggle_signal, wiggle_noise, result_params, cov_matrix=None, num_samples_uncertainty_region=1000, save_figure_info={'dir': None, 'x': None, 'y': None})[source]#
Plot the model.
- Parameters:
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle noise
result_params (np.ndarray) β Fitted parameters
cov_matrix (np.ndarray) β Covariance matrix
num_samples_uncertainty_region (int) β Number of samples for uncertainty region
- Returns:
None
- Return type:
None
- reject_outliers(residual, num_params=0, fdr_alpha=0.01, fdr_outlier_max_fraction=0.1, sigma_clip_sigma=5, sigma_clip_max_iterations=5)[source]#
Reject outliers using the selected method.
- Parameters:
residual (np.ndarray) β Residuals
num_params (int) β Number of parameters
fdr_q (float) β FDR correction threshold
fdr_outlier_max_fraction (float) β Maximum fraction of outliers to reject
huber_delta (float) β Delta for Huber loss function
sigma (float) β Sigma threshold for sigma clipping
sigma_clip_max_iterations (int) β Maximum number of iterations for sigma clipping
- Returns:
Indices of outliers
- Return type:
np.ndarray
- residual_vector(params, wiggle_signal, wiggle_noise)[source]#
Get the residual vector.
- Parameters:
params (np.ndarray) β Parameters
wiggle_signal (np.ndarray) β wiggle_signal
wiggle_noise (np.ndarray) β wiggle_noise
- Returns:
Residual vector
- Return type:
np.ndarray
- residual_vector_full_fit(params, x, y, aperture_radius, annulus_outer_radius=0, annulus_inner_radius=0)[source]#
Compute the residual vector for the full model fit.
- Parameters:
params (np.ndarray) β Parameters
x (np.ndarray) β x coordinates
y (np.ndarray) β t coordinates
aperture_radius (float) β Aperture radius
annulus_outer_radius (float) β Outer radius of the annulus
annulus_inner_radius (float) β Inner radius of the annulus
- Returns:
Residual vector
- Return type:
np.ndarray
- scale_wavelengths_negative1_to_1(w)[source]#
Scale the wavelengths to -1 to 1.
- Parameters:
w (np.ndarray) β Wavelengths
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- scale_wavelengths_to_0_1(w)[source]#
Scale the wavelengths to 0 to 1.
- Parameters:
w (np.ndarray) β Wavelengths
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- property scaled_w#
Scaled wavelengths.
- Returns:
Scaled wavelengths
- Return type:
np.ndarray
- set_gaps(gaps)[source]#
Set the gaps to be ignored during the fitting process.
- Parameters:
gaps (list of tuples) β List of wavelength ranges to be ignored
- Returns:
None
- Return type:
None
- set_params(amplitude_params, frequency_params, phi_0, n_amplitude=None, n_frequency=None)[source]#
Set the parameters. Opposite function of the split_params function.
- Parameters:
amplitude_params (np.ndarray) β Amplitude parameters
frequency_params (np.ndarray) β Frequency parameters
phi_0 (float) β Phase
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
Parameters
- Return type:
np.ndarray
- static solve_ridge_regression(A, spectra)[source]#
Solve the ridge regression problem.
- Parameters:
A (np.ndarray) β Design matrix
spectra (np.ndarray) β Target spectra
- Returns:
Coefficients
- Return type:
np.ndarray
- split_params(params, n_amplitude=None, n_frequency=None)[source]#
Split the parameters. Opposite of the set_params function.
- Parameters:
params (np.ndarray) β Parameters
n_amplitude (int) β Number of amplitude parameters
n_frequency (int) β Number of frequency parameters
- Returns:
amplitude parameters, frequency parameters, and phi_0
- Return type:
Tuple
- property symmetric_sharpening#
Get the symmetric sharpening flag.
- Returns:
Symmetric sharpening flag
- Return type:
bool
- wiggle_func(xs, amplitude_params, frequency_params, phi, a_1=0, a_2=0)[source]#
Get the wiggle function.
- Parameters:
xs (np.ndarray) β Scaled wavelengths
frequency_params (np.ndarray) β Frequency parameters
amplitude_params (np.ndarray) β Amplitude parameters
phi (float) β Phase
- Returns:
Wiggle function
- Return type:
np.ndarray