The SpecFit Module

This module introduces the SpecFit class and its functionality. The SpecFit class is designed to facilitate complex model fits to astronomical spectra.

It is initialized with the supplied astronomical spectrum and can hold multiple SpecModel objects, which themselves hold the fit models and their parameters.

class sculptor.specfit.SpecFit(spectrum=None, redshift=0)

Base class for fitting of astronomical spectroscopic data.

The SpecFit class takes a SpecOneD object of an astronomical spectrum and allows complex models to be fit to it using the LMFIT module.

SpecModel objects will be added to the SpecFit class to hold information on the different models and parameters. Each SpecModel object will be consecutively fit to the astronomical spectrum.

spec

Astronomical spectrum as a SpecOneD object

Type

SpecOneD

xlim

Wavelength limits for plotting

Type

list of float

ylim

Flux density limits for plotting

Type

list of float

redshift

Cosmological redshift of the astronomical object

Type

float

fitting_method

Fitting method (default: ‘Levenberg-Marquardt’)

Type

str

colors

Float values to set colors for plotting

Type

numpy.ndarray of floats

super_params

Parameter list of “Super Parameters”, which are global for the specfit class and are added as “Global Parameters” to all SpecModels

Type

lmfit.parameters

specmodels

List of SpecModel objects added to the SpecFit class.

Type

list of SpecModel

add_specmodel()

Add a SpecModel to the SpecFit class

Returns

None

add_super_param(param_name, value=None, vary=True, min=-inf, max=inf, expr=None)

Adding a “Super Parameter” to the SpecFit object.

Parameters
  • param_name (str) – Name of the super parameter

  • value (float,optional) – Initial value of the super parameter

  • vary (bool,optional) – Boolean to indicate whether the super parameter should be varied during the fit

  • min (float,optional) – Minimum value for the super parameter

  • max (float,optional) – Maximum value for the super parameter

  • expr (str,optional) – Optional expression for the super parameter

Returns

None

copy()

Copy the SpecFit object

Returns

SpecFit

delete_specmodel(index=None)

Delete the latest SpecModel or the one indicated by the index keyword argument from the SpecFit class.

Parameters

index (int) – Index of the SpecModel to delete in specmodels

Returns

None

fit(save_results=False, foldername='.')

Fit all SpecModels consecutively

Parameters
  • save_results (bool) – Boolean to indicate whether fit results will be saved.

  • foldername (str,optional) – If “save_results==True” the fit results will be saved to the folder specified in foldername. This variable defaults to the current folder. If set to “None” fit results will not be saved.

Returns

None

get_result_dict()

Get the best-fit parameter values and return them as a dictionary

Returns

(dict) result_dict Dictionary with best-fit parameter values.

import_spectrum(filename, filetype='IRAF')

Import an astronomical spectrum into SpecFit class

Currently the allowed ‘filetype’ options are: ‘IRAF’, ‘PypeIt’, ‘SpecOneD’, ‘SDSS’

Note that the SpecFit class can be initialized a SpecOneD spectrum object, that can be constructed manually from the spectral format of choice.

Parameters
  • filename (str) – Full file name of the astronomical spectrum

  • filetype (str) – String specifying the type of the spectrum to select the appropriate read method.

Returns

None

load(foldername)

Load a full spectral fit (SpecFit) from a folder

This function overwrites all SpecModels, SpecFit parameters, and the astronomical spectrum.

Parameters

foldername (str) – Folder from which the SpecFit class will be loaded.

Returns

None

normalize_spectrum_by_error()

Normalize the flux density, flux density error and object model arrays of the spectrum by the median value of the flux density error array.

The flux density unit will be scaled accordingly. Hence, this normalization does not affect the physical values of the flux density and only serves to normalize the values in the flux density array.

This enables more efficient calculations on the flux density array by avoiding small numerical values.

Returns

normalize_spectrum_by_factor(factor)

Normalize the flux density, flux density error and object model arrays of the spectrum by the specified numerical factor.

The flux density unit will be scaled accordingly. Hence, this normalization does not affect the physical values of the flux density and only serves to normalize the values in the flux density array.

This enables more efficient calculations on the flux density array by avoiding small numerical values.

Parameters

factor

Returns

normalize_spectrum_to_factor(factor)

Normalize the flux density, flux density error and object model arrays of the spectrum to the specified unit factor.

The flux density unit will be scaled accordingly. Hence, this normalization does not affect the physical values of the flux density and only serves to normalize the values in the flux density array.

This enables more efficient calculations on the flux density array by avoiding small numerical values.

Parameters

factor

Returns

plot()

Plot the astronomical spectrum with all SpecModels

Returns

None

remove_super_param(param_name)

Remove “Super Parameter” from SpecFit object.

Parameters

param_name (str) – Parameter name of the super parameter to remove.

Returns

None

resample(n_samples=100, save_result_plots=True, foldername='.', seed=1234)

Resample and fit the spectrum.

Resample the spectral flux on a pixel by pixel basis by assuming a Gaussian distribution of flux values around the measured flux value with a sigma equal to the flux uncertainty.

Fit all SpecModels to the resampled spectrum and record the best-fit values of all fit parameters. The fits are initialized with the current parameter values from all SpecModels.

All n_samples results for each parameter are saved in a hdf5 file with the filename ‘resampled_fitting_results_[n_samples]_raw.hdf5’. Median, lower (15.9 percentile) and upper (84.1 percentile) values are calculated from each parameter distribution and saved in a csv/hdf5 file with the name ‘resampled_fitting_results_[n_samples].hdf5/.csv’.

If fit result plots are enabled (‘save_result_plots=True’) then the best-fie value distributions for each parameters, including their median, lower and upper values are saved to ‘[foldername]/[parameter name]_results.pdf’.

Parameters
  • n_samples (int) – Number of samples to draw

  • save_result_plots (bool) – Boolean indicating whether histograms for all parameters should be saved in the specified folder.

  • foldername (str) – Path to the folder where the result plots will be saved. This defaults to ‘.’.

  • seed (int) – Random seed for initializing the numpy random number generator

Returns

None

save(foldername)

Save the spectral fit (SpecFit) to a folder.

Parameters

foldername (str) – Folder to which the SpecFit class will be saved.

Returns

None

update_specmodel_spectra()

Update all SpecModel spectra

This function updates the SpecModel spectra consecutively. Model fits from each SpecModel will be automatically subtracted/divided.

Note: Not only the dispersion and the fluxden, but also the mask will be updated.

Returns

None

update_specmodels()

Update SpecFit parameters in all SpecModels