CarsFit¶
Least-square fit of experimental CARS spectra.
-
class
carspy.cars_fit.
CarsFit
(spec_cars, nu_spec, ref_fac=100.0, bg_cars=None, spec_stokes=None, bg_stokes=None, fit_mode=None, **kwargs)[source]¶ Fitting experimental CARS spectrum.
Note
It can also be used to fit the laser linewidth or slit function.
Input measured CARS spectrum and its background.
- Parameters
- spec_cars1d array of floats
Measured CARS spectrum.
- nu_spec1d array of floats
Spectral range (axis) of the supplied CARS spectrum in [\(\mathrm{cm}^{-1}\)]. This can either be absolute values or Raman shifted ones.
- ref_facfloat
Refining factor based on the supplied spectral domain, by default 100. This factor is used in the fitting procedure to synthesize spectra with much higher spectral resolution to be compared (after downsampling) with the supplied (expt.) CARS spectrum. Higher value will result in more accurate spectra at the cost of CPU time.
- bg_cars1d array of floats
Background noise for the measured CARS spectrum.
- spec_stokes1d array of floats
Measured broadband Stokes profile (usually with Ar), by default None. If not supplied, it is assumed that spec_cars is already corrected by the Stokes profile.
- bg_stokes1d array of floats
Background noise for the measured Stokes profile.
- fit_modedict, optional
A dictionary containing the control parameters used to perform the CARS fit. By default:
- power_factor0
0 (fit I) or 1 (fit sqrt(I)).
- downsample‘local_mean’
Choose between ‘local_mean’ (highly efficient custom algorithm) and ‘interp’ (interpolation with
numpy.interp
).- slit‘sVoigt’
Choose between (asymmetric) ‘sVoigt’ and ‘sGaussian’ as the slit impulse response function, see the documentations for
carspy.convol_fcn.asym_Voigt
andcarspy.convol_fcn.asym_Gaussian
. ‘sGaussian’ will be deprecated in future updates.- pump_ls‘Gaussian’
‘Gaussian’ or ‘Lorentzian’ laser lineshape.
- chi_rs‘G-matrix’
Choose between ‘isolated’ and ‘G-matrix’ for the consideration of pressure effects.
- convol‘Kataoka’
‘Kataoka’/’K’ (double convolution) or ‘Yuratich’/’Y’ (single convolution).
- doppler_effectTrue
Whether or not to consider Doppler effect on the Raman lineshape.
- chem_eqFalse
Whether or not to assume chemical equilibrium during the fit.
- fit‘custom’
Type of build-in fitting setups: ‘T_x’ or ‘custom’.
‘T_x’: fitting variables related to the experimental setup (e.g., spectrometer) are inheritted from an existing fit and fixed. Only temperature and species concentrations are by default allowed to vary.
‘custom’: all fitting variables need to be provided before a fit can process.
See
carspy.cars_fit.CarsFit.ls_fit
for details.
- Other Parameters
- **kwargs:
This method also allows the keyword arguments found for initializing
carspy.cars_synth.CarsSpectrum
.
-
cars_expt_synth
(nu_expt, x_mol, temperature, del_Tv, nu_shift, nu_stretch, pump_lw, param1, param2, param3, param4, param5, param6)[source]¶ Synthesize a CARS spectrum based on the experimental spectral domain.
- Parameters
- nu_expt1d array of floats
The spectral axis determined in the experiment. This is used as the independent variable during the fit.
- x_molfloat
Mole fraction of probed molecule.
- temperaturefloat
Temperature in the probe volume in [K].
- nu_shiftfloat
Shift applied to correctly center the spectrum in [\(\mathrm{cm}^{-1}\)].
- nu_strechfloat
Strech applied to nu to compensate for incorrect dispersion calibration.
- pump_lwfloat
Pump laser linewdith in [\(\mathrm{cm}^{-1}\)].
- del_Tvfloat
The amount vibrational temperature exceeds the rotational temperature.
- param1, param2, param3, param4, param5, param6float
Fitting parameters for the slit function
-
ls_fit
(**kwargs)[source]¶ Fitting the experimental CARS spectrum.
Attention
The least-quare fit module
lmfit
is necessary for this method. Please be aware that certain Python versions may not be supported bylmfit
. For displaying the fit resultsmatplotlib
will be needed as well.- Parameters
- add_paramsnested tuple, optional
List of parameters controlling the fitting process. This option can be used to modify these initial parameters:
(('temperature', 2000, True, 250, 3000), ('del_Tv', 0, False), ('x_mol', 0.6, x_mol_var, 0.2, 1.5), ('nu_shift', fit_params['nu_shift'], False), ('nu_stretch', fit_params['nu_stretch'], False), ('pump_lw', fit_params['pump_lw'], False), ('param1', fit_params['param1'], False), ('param2', fit_params['param2'], False), ('param3', fit_params['param3'], False), ('param4', fit_params['param4'], False), ('param5', fit_params['param5'], False), ('param6', fit_params['param6'], False))
Each element of the nested tuple has the following element in order:
- variable_namestr
All the arguments of
carspy.cars_fit.CarsFit.cars_expt_synth
are admissible variables except for the independent variable nu_expt.- initial_guessfloat
Initial guess or fixed value set for this variable.
- variablebool
Determine if the variable is fixed (False) or not (True) during the fit.
- lower_boundfloat
Lower boundary for the fitting variable. If not provided, negative infinity will be assumed.
- upper_boundfloat
Upper boundary for the fitting variable. If not provide, positive infinity will be assumed.
For more details refer to the documentation of
lmfit.Model
.- path_fitstr
Path to the .pkl file of fitting result created by
carspy.cars_fit.CarsFit.save_fit
. This allows importing the fitting result of an existing spectrum, such that the inferred values of certain parameters (such as those related to the spectrometer) could be re-used in the next fit. A standard use case for this would be the fitting result of a room-temperature spectrum. This is needed if the fit in fit_mode ofcarspy.cars_fit.CarsFit
is set to T_x.- show_fitbool, optional
If True, the fitting results will be reported and plotted. This is done via built-in functions in
lmfit
.
- Other Parameters
- **kwargs:
Keyword arguments of the Model.fit() method from the module lmfit.
-
preprocess
(w_Stokes=0, nu_Stokes=0, crop=None, bg_subtract=False, bg_offset=0, bg_loc=None)[source]¶ Prepare the raw data for the fitting.
- Parameters
- w_Stokesfloat, optional
Center wavelength of the Stokes (e.g., dye laser) beam in [nm], by default 0.
- nu_shiftfloat, optional
Center wavenumber of the Stokes beam in [\(\mathrm{cm}^{-1}\)], by default 0. In essence, w_Stokes and nu_Stokes are equivalent.
- croplist, optional
Two indices to crop the spectrum with, by default None. Needs to be adjusted based on the experimental setup.
- bg_subtractbool, optional
If True, an extra offset specified by bg_offset or determined within bg_loc is subtracted from the spectrum. This is not recommended as there shouldn’t be any physical background left if backgrounds are subtracted properly from the experimental spectrum beforehand. This might help if S/N is bad. By default it is set to False.
- bg_offsetfloat, optional
Value used as background to subtract, be default 0. This is ignored if bg_log is provided.
- bg_loclist, optional
Two indices to select the part of spectrum as background, only used if bg_subtract is True.
-
carspy.cars_fit.
bg_removal
(spec, bg=None)[source]¶ Subtract background from spectrum and normalize it by its peak.
- Parameters
- spec1d array
Spectrum.
- bgNone, 1d array, optional
Background noise.
-
carspy.cars_fit.
slit_fit
(*args, **kwargs)[source]¶ fitting the experimental slit function with a chosen lineshape
Attention
It is recommended to always look for initial parameters by using the eval_only option to roughly match the shape of the experimental slit function. The built-in initial parameters may not work for all the cases.
- Parameters
- nu1d array of floats
Spectral positions in [\(\mathrm{cm}^{-1}\)].
- spec1d array of floats
Experimentally obtained slit function, usually an isolated atomic line from a calibration lamp.
- init_paramsdict, optional
Initial fitting parameters for the lineshape. Refer to
carspy.convol_fcn.asym_Voigt
andcarspy.convol_fcn.asym_Gaussian
for the required arguments.- lineshapestr, optional
Type of the lineshape, by default ‘sGaussian’. Choose between ‘sGaussian’ and ‘sVoigt’.
- eval_onlybool, optional
If true, returns the evaluation with given initial parameters.
- save_fitbool, optional
If true, fit results will be saved in a pickle file.
- dir_savestr, optional
If save_fit is true, a string to the desired directory for saving.
- file_namestr, optional
If save_fit is true, specify the file name without file extension.
- Returns
- 1d array
If eval_only is true, the evaluation result is returned.
- Other Parameters
- **kwargs:
Keyword arguments of the Model.fit() method from the module lmfit.