tuskitoo.SpectralExtraction.Expectra2D module

df_get(df, key, default=None)[source]
class Expectra2D(object, center_cut=None, size_cut=None, distances=None, verbose=False, header=None, **kwargs)[source]

Bases: object

Main class to handle 2D spectra and extract the spectra

get_header_keys(distances=None)[source]

Retrieve and store a subset of header keys relevant for further processing.

Parameters:

distancesoptional

If provided as a dictionary, it may be used for additional processing related to distances.

Notes:

If no header is available, a warning is issued.

arc_to_pix(value)[source]
run_parallel_fit(n_picks=2, pixel_limit=[], bound_sigma=[2], distribution='gaussian', param_value=None, param_limit=None, param_fix=None, no_use_real_error=False, initial_separation=[], initial_center=None, **kwargs)[source]

Run the parallel fitting process on the instance’s image data.

This function prepares the fitting parameters based on the instance attributes, defines masks based on the instrument band, and calls parallel_fit to perform the actual parallel fitting. It also stores the local parameters used for fitting in the attribute keywords_fit and the final results in fit_result.

Parameters:

n_picksint, optional, default=2

Number of sources (or picks) to consider in the fitting. This should match the number of distinct peaks expected in the data.

pixel_limitlist or tuple, optional, default=[]

Pixel (column) limits to process. Example: [start_column, end_column]. If empty, all columns are processed.

bound_sigmalist, optional, default=[2]

List of component indices for which the sigma value should be bounded to that of the first component.

distributionstr, optional, default=”gaussian”

Type of distribution to use for fitting. Options include “gaussian” and “moffat”.

param_valuedict or None, optional, default=None
Dictionary of initial parameter values. For example:
{

“height_1”: 10.0, “sigma_1”: 2.0, “center_1”: 150.0

}

These values provide starting points for the fitting algorithm.

param_limitdict or None, optional, default=None
Dictionary of limits (min, max) for parameters. For example:
{

“sigma_1”: (0.1, 5.0), “center_1”: (100, 200)

}

This restricts the range over which parameters can be optimized.

param_fixlist or None, optional, default=None
List of parameter names to be fixed (kept constant) during fitting. For example:

[“height_1”, “center_1”]

Parameters listed here will not be varied during the optimization process.

no_use_real_errorbool, optional, default=False

If True, the function uses a constant error (i.e., ones) instead of the real error provided.

initial_separationlist, optional, default=[]

Initial guess for the separation between the sources/components. Must have a length corresponding to n_picks - 1.

initial_centerfloat or None, optional, default=None

Initial guess for the center position. If None, the function estimates it from the data.

Examples:

>>> # Example: Fix the height of the first source and set an initial value for sigma.
>>> param_fix_example = ["height_1"]
>>> param_value_example = {"sigma_1": 2.0, "center_1": 150.0}
>>> self.run_paralel_fit(n_picks=2, pixel_limit=[0, 1024], bound_sigma=[2],
...                      distribution="gaussian", param_fix=param_fix_example,
...                      param_value=param_value_example, init_separation=[20], init_center=150)

Returns:

None

The results are stored in the instance attributes keywords_fit and fit_result.

array_to_pandas(max_iter=5, sigma=2, region_size=20, over_write=False, images=[])[source]

Convert the fitting results into a pandas DataFrame.

Processes the output of the parallel fit, applies sigma clipping, and organizes the results into a DataFrame for further analysis or plotting.

Parameters:

max_iterint, optional, default=5

Maximum number of iterations for sigma clipping.

sigmafloat, optional, default=2

Sigma threshold for sigma clipping.

region_sizeint, optional, default=20

Size of the region to be considered in the sigma clipping routine.

over_writebool, optional, default=False

If True, overwrites any existing results in the instance attribute.

imageslist, optional, default=[]

Optional list of image names corresponding to the different sources.

Returns:

DataFrame or dictionary

Returns the DataFrame if not overwriting; otherwise, the results are stored in the instance.

save_fit_keywords_as_pickle(filename)[source]

Save the dictionary of fitting keywords (parameters used in the fit) to a pickle file.

Parameters:

filenamestr

The base filename for saving (without extension).

save_spectra_as_pickle(save=None, band=None)[source]

_summary_

Parameters:
  • save (_type_, optional) – _description_. Defaults to None.

  • band (_type_, optional) – _description_. Defaults to None.

Returns:

_description_

Return type:

_type_

save_to_fits(filename, person='F. Avila-Vera')[source]

Save the extracted spectra (results) to a FITS file.

Parameters:

filenamestr

The base filename for the FITS file.

personstr, optional, default=”F. Avila-Vera”

Name of the person responsible for the extraction; stored in the FITS header.

Raises:

AttributeError:

If the results have not been computed (i.e., ‘array_to_pandas’ has not been run).

plot_column()[source]
plot_data_model(n)[source]

Plot the data, individual model components, and the residual for the nth column.

Parameters:

nint

Index of the column (pixel) to be plotted.

Raises:

AttributeError:

If the results have not been computed (i.e., ‘array_to_pandas’ has not been run).

plot_spectra(add_error=False, add_raw=False, save='', force_pix=False, z_s=None, add_lines=False, rest_frame=False, flux_columns=None, **kwargs)[source]

Plot the extracted spectra with optional error bars, raw spectra, and emission/absorption lines. Parameters: ———– add_error : bool, optional, default=False

If True, adds error bars to the plot.

add_rawbool, optional, default=False

If True, plots the raw flux values.

savestr, optional, default=’’

If provided, the plot is saved to the specified filename.

force_pixbool, optional, default=False

If True, the x-axis will be in pixel units instead of wavelength.

z_sfloat or None, optional, default=None

Redshift value for converting wavelengths to the rest frame.

add_linesbool, optional, default=False

If True, vertical lines for known spectral features will be added.

rest_framebool, optional, default=False

If True and z_s is provided, the wavelengths are converted to the rest frame.

kwargsdict

Additional keyword arguments for customizing the plot (e.g., xlim, ylim, text_fontsize).

Raises:

AttributeError:

If the results have not been computed (i.e., ‘array_to_pandas’ has not been run).

plot_cut_out()[source]

Plot the 2D cut-out image and the stacked median profile.

run_cut_2d(center, size, verbose=False)[source]
static cut_2d_image(image, center=None, size=None, verbose=False)[source]

Cut a 2D image to the specified region.

Parameters:

imagearray-like

The input 2D image to be cut.

centerint or None, optional, default=None

The center position for cutting the 2D image. If None, the center will be estimated.

sizeint or None, optional, default=None

The size of the cut-out region. If None, a default size of 70 will be used.

verbosebool, optional, default=False

If True, print additional information during processing.

Returns:

array-like

The cut-out 2D image.