ExtractionEngine
- class jwst.extract_1d.soss_extract.atoca.ExtractionEngine(wave_map, trace_profile, throughput, kernels, wave_grid, mask_trace_profile, global_mask=None, orders=None, threshold=0.001)[source]
Bases:
objectRun the ATOCA algorithm.
This class is basically a fitter. On instantiation, it generates a model of the detector, including a mapping between the detector pixels and the wavelength for each spectral order, the throughput and convolution kernel, and known detector bad pixels. This does not require any real data. When called, it ingests data and associated errors, then generates an output 1-D spectrum that explains the pixel brightnesses in the data within the constraints of the model.
The engine can also run in reverse: The
rebuild()method generates a synthetic 2-D detector ‘observation’ from a known or fitted spectrum, and thecompute_likelihood()method compares the synthetic data to the real data to generate a likelihood. This allows for a likelihood-based optimization of the spectrum.This version models the pixels of the detector using an oversampled trapezoidal integration.
- Parameters:
- wave_maplist or ndarray
A list or array of 2-D arrays of the central wavelength position for each order on the detector. Has shape
(N_ord, N, M). It has to have the same(N, M)as data.- trace_profilelist or ndarray
A list or array of 2-D arrays of the spatial profile for each order Has shape
(N_ord, N, M). on the detector. It has to have the same(N, M)as data.- throughputlist of array or callable
A list of functions or array of the throughput at each order. If callable, the functions depend on the wavelength. If array, projected on
wave_grid. Has shape(N_ord [, N_k]).- kernelscallable, sparse matrix, or None
Convolution kernel to be applied on spectrum (
f_k) for each orders. Can be a callable with the formf(x, x0)wherex0is the position of the center of the kernel. In this case, it must return a 1D array (len(x)), so a kernel value for each pairs of(x, x0). If callable, it will be passed toget_c_matrix()and thec_kwargscan be passed to this function. If sparse, the shape has to be(N_k_c, N_k)and it will be used directly.N_keris the length of the effective kernel andN_k_cis the length of the spectrum (f_k) convolved. If None, the kernel is set to 1, i.e., do not do any convolution.- wave_gridarray-like, required
The grid on which
f(lambda)will be projected, shape(N_k).- mask_trace_profilelist or ndarray
A list or array of 2-D boolean arrays of the pixel that need to be used for extraction, for each order on the detector. It has to have the same shape
(N_ord, N, M)astrace_profile.- global_maskndarray, optional
Boolean mask of the detector pixels to mask for every extraction, e.g., bad pixels. Should not be related to a specific order (if so, use
mask_trace_profileinstead). Has shape(N, M).- orderslist, optional
List of orders considered. Default is
[1, 2].- thresholdfloat, optional
The contribution of any order on a pixel is considered significant if its estimated spatial profile is greater than this threshold value. If it is not properly modeled (not covered by the wavelength grid), it will be masked. Default is 1e-3.
References
Darveau-Bernier 2022, PASP, DOI:10.1088/1538-3873/ac8a77
Attributes Summary
Return the Tikhonov matrix, computing it if needed.
Methods Summary
__call__(data, error[, tikhonov, factor])Extract underlying flux on the detector.
best_tikho_factor(tests, fit_mode)Compute the best scale factor for Tikhonov regularization.
build_sys(data, error)Build linear system arising from the logL maximisation.
compute_likelihood(spectrum, data, error)Return the log likelihood associated with a particular spectrum.
Compute integration weights.
estimate_tikho_factors(flux_estimate)Estimate an initial guess of the Tikhonov factor.
get_attributes(*args[, i_order])Return list of attributes.
get_detector_model(data, error)Get the linear model of the detector pixel, B.dot(flux) = pixels.
get_mask_wave(i_order)Generate mask bounded by limits of wavelength grid.
get_pixel_mapping(i_order[, error, quick])Calculate the pixel mapping.
get_tikho_test_structure(data, error)Structure to test different factors for Tikhonov regularization.
get_tikho_tests(tikho, factors)Test different factors for Tikhonov regularization.
get_w(i_order)Compute integration weights 'k' for each grid point and pixel 'i'.
grid_from_map([i_order])Return the wavelength grid and the columns for a given order index.
precompute_detector_model(data, error, tikfac)Return the matrices and mask needed to solve for the spectrum.
rebuild(spectrum[, fill_value])Build current model image of the detector.
update_throughput(throughput)Update internal throughput values.
wave_grid_c(i_order)Return wave_grid for a given order constrained according to the i_bounds of that order.
Attributes Documentation
- dtype = 'float64'
- tikho_mat
Return the Tikhonov matrix, computing it if needed.
Methods Documentation
- __call__(data, error, tikhonov=False, factor=None)[source]
Extract underlying flux on the detector.
Performs an overlapping extraction of the form:
(B_T * B) * f = (data/sig)_T * B
where
Bis a matrix andfis an array. The matrix multiplicationB * fis the 2D model of the detector. We want to solve for the arrayf. The elements offare labelled by ‘k’. The pixels are labeled by ‘i’. Every pixel ‘i’ is covered by a set of ‘k’ for each order of diffraction.Note
To be quicker, only specify the psf (
p_list) inkwargs. There will be only one matrix multiplication:(P/sig).(w.T.lambda.c_n)
- Parameters:
- data(N, M) array-like
A 2-D array of real values representing the detector image.
- error(N, M) array-like
Estimate of the error on each pixel. Same shape as data.
- tikhonovbool, optional
Whether to use Tikhonov extraction Default is
False.- factorfloat, optional
The Tikhonov factor to use if
tikhonovisTrue.
- Returns:
- spectrumarray[float]
Solution of the linear system (
f_k).
- best_tikho_factor(tests, fit_mode)[source]
Compute the best scale factor for Tikhonov regularization.
The scale factor is determined by taking the factor giving the lowest reduced chi2 on the detector, the highest curvature of the l-curve or when the improvement on the chi2 (so the derivative of the chi2, ‘d_chi2’) reaches a certain threshold.
- Parameters:
- testsdict
Results of Tikhonov extraction tests for different factors. Must have the keys “factors” and “-logl”.
- fit_modestr
Which mode is used to find the best Tikhonov factor. Options are ‘all’, ‘curvature’, ‘chi2’, or ‘d_chi2’. If ‘all’ is chosen, the best of the three other options will be selected.
- Returns:
- best_facfloat
The best Tikhonov factor.
- build_sys(data, error)[source]
Build linear system arising from the logL maximisation.
- Parameters:
- data(N, M) array-like
A 2-D array of real values representing the detector image.
- error(N, M) array-like
Estimate of the error on each pixel.
- Returns:
scipy.sparse.csr_array, array[float]A, bfromAx = bbeing the system to solve.
- compute_likelihood(spectrum, data, error)[source]
Return the log likelihood associated with a particular spectrum.
- Parameters:
- spectrumarray[float] or callable
Flux as a function of wavelength if callable or array of flux values corresponding to
self.wave_grid.- data(N, M) array-like
A 2-D array of real values representing the detector image.
- error(N, M) array-like
Estimate of the error on each pixel. Same shape as data.
- Returns:
- array[float]
The log-likelihood of the spectrum.
- compute_weights()[source]
Compute integration weights.
The weights depend on the integration method used to solve the integral of the flux over a pixel and are encoded in the method
get_w().- Returns:
- weights, weights_k_idxlist
Lists of weights and corresponding grid indices
- estimate_tikho_factors(flux_estimate)[source]
Estimate an initial guess of the Tikhonov factor.
The output factor will be used to find the best Tikhonov factor. The
flux_estimateis used to generate afactor_guess. The user should construct a grid with this output in log space, e.g.,np.logspace(np.log10(flux_estimate)-4, np.log10(flux_estimate)+4, 9).- Parameters:
- flux_estimatecallable
Estimate of the underlying flux (the solution
f_k). Must be function of wavelengths and it will be projected onself.wave_grid.
- Returns:
- float
Estimated Tikhonov factor.
- get_attributes(*args, i_order=None)[source]
Return list of attributes.
- Parameters:
- *argsstr or list of str
All attributes to return.
- i_orderNone or int, optional
Index of order to extract. If specified, it will be applied to all attributes in
args, so it cannot be mixed with non-order dependent attributes.
- Returns:
- list
Result of
[getattr(arg) for arg in args], withi_orderindexing if provided.
- get_detector_model(data, error)[source]
Get the linear model of the detector pixel, B.dot(flux) = pixels.
- Parameters:
- dataarray-like
A 2-D array of real values representing the detector image, shape
(N, M).- errorarray-like
Estimate of the error on each pixel, shape
(N, M).
- Returns:
- B, pix_arrayarray[float]
From the linear equation:
B.dot(flux) = pix_array
- get_mask_wave(i_order)[source]
Generate mask bounded by limits of wavelength grid.
- Parameters:
- i_orderint
Order to select the
wave_mapon which a mask will be generated
- Returns:
- array[bool]
A mask with
Truewherewave_mapis outside the bounds ofwave_grid
- get_pixel_mapping(i_order, error=None, quick=False)[source]
Calculate the pixel mapping.
Compute the matrix
b_n = (P/sig).w.T.lambda.c_n, wherePis the spatial profile matrix (diag),wis the integrations weights matrix,Tis the throughput matrix (diag),lambdais the convolved wavelength grid matrix (diag),c_nis the convolution kernel. The model of the detector at order n (model_n) is given by the system:model_n = b_n.c_n.f, wherefis the incoming flux projected on the wavelength grid. This method updates theb_n_listattribute.- Parameters:
- i_orderint
Label of the order (depending on the initiation of the object).
- errorarray-like or None, optional
Estimate of the error on each pixel. Same shape
(N, M)as data. If None, the error is set to 1, which means the method will returnb_ninstead ofb_n/sigma. Default is None.- quickbool, optional
If
True, only perform one matrix multiplication instead of the whole system:(P/sig).(w.T.lambda.c_n)
- Returns:
- array[float]
Sparse matrix of
b_ncoefficients.
- get_tikho_test_structure(data, error)[source]
Structure to test different factors for Tikhonov regularization.
- Parameters:
- data(N, M) array-like
A 2-D array of real values representing the detector image.
- error(N, M) array-like
Estimate of the error on each pixel. Same shape as data.
- Returns:
- tikho
Tikhonov Instance of class with matrices pre-computed. Suitable for calling
get_tikho_tests().
- tikho
- get_tikho_tests(tikho, factors)[source]
Test different factors for Tikhonov regularization.
- Parameters:
- tikho
Tikhonov Instance of class with matrices pre-computed.
- factors1D list or array-like
Factors to be tested.
- tikho
- Returns:
- testsdict
Dictionary of the test results
- get_w(i_order)[source]
Compute integration weights ‘k’ for each grid point and pixel ‘i’.
These depend on the type of interpolation used, i.e., the order
n.- Parameters:
- i_orderint
Order to set the value of
nin output arrays.
- Returns:
- w_narray
2D array of weights at this specific order
n. The shape is given by: (number of pixels, max number of wavelengths covered by a pixel).- k_narray
2D array of the wavelength grid indices corresponding to the weights. Same shape as
w_n.
- grid_from_map(i_order=0)[source]
Return the wavelength grid and the columns for a given order index.
- Parameters:
- i_orderint, optional
Order index to get the wavelength grid for. Default is 0.
- Returns:
- wave_gridarray[float]
Wavelength grid for the given order index.
- icolarray[float]
Column indices for the wavelength grid.
- precompute_detector_model(data, error, tikfac)[source]
Return the matrices and mask needed to solve for the spectrum.
- Parameters:
- data(N, M) array-like
A 2-D array of real values representing the detector image.
- error(N, M) array-like
Estimate of the error on each pixel. Same shape as data.
- tikfacfloat
The Tikhonov factor to use.
- Returns:
- design_matrix_inv(N, N) array
The inverse of the design matrix
MinM*f_k=b*(y/err).- b_matrix(N, M) array
The matrix
binM*f_k=b*(y/err).
- rebuild(spectrum, fill_value=0.0)[source]
Build current model image of the detector.
- Parameters:
- spectrumcallable or array-like
Flux as a function of wavelength if callable or array of flux values corresponding to
self.wave_grid.- fill_valuefloat, optional
Pixel value where the detector is masked. Default is 0.0.
- Returns:
- array[float]
The modeled detector image.