radis.tools packageΒΆ
SubmodulesΒΆ
- radis.tools.code_profiler module
- radis.tools.database module
- radis.tools.fitting module
- radis.tools.gascomp module
- radis.tools.line_survey module
- radis.tools.new_fitting module
- radis.tools.plot_tools module
- radis.tools.slit module
- radis.tools.track_ref module
Module contentsΒΆ
Tools : database of spectra, line survey, interface with Cantera.
- class SpecDatabase(path='.', filt='.spec', add_info=None, add_date='%Y%m%d', verbose=True, binary=True, nJobs=-2, batch_size='auto', lazy_loading=True, update_register_only=False)[source]ΒΆ
Bases:
SpecList
A Spectrum Database class to manage them all.
It basically manages a list of Spectrum JSON files, adding a Pandas dataframe structure on top to serve as an efficient index to visualize the spectra input conditions, and slice through the Dataframe with easy queries
Similar to
SpecList
, but associated and synchronized with a folder- Parameters:
path (str) β a folder to initialize the database
filt (str) β only consider files ending with
filt
. Default.spec
binary (boolean) β if
True
, open Spectrum files as binary files. IfFalse
and it fails, try as binary file anyway. DefaultFalse
.lazy_loading (bool``) β If
True
, load only the data from the summary csv file and the spectra will be loaded when accessed by the get functions. IfFalse
, load all the spectrum files. IfTrue
and the summary .csv file does not exist, load all spectra
- Other Parameters:
*input for :class:`~joblib.parallel.Parallel` loading of database*
nJobs (int) β Number of processors to use to load a database (useful for big databases). BE CAREFULL, no check is done on processor use prior to the execution ! Default
-2
: use all but 1 processors. Use1
for single processor.batch_size (int or
'auto'
) β The number of atomic tasks to dispatch at once to each worker. When individual evaluations are very fast, dispatching calls to workers can be slower than sequential computation because of the overhead. Batching fast computations together can mitigate this. Default:'auto'
More information in :class:`joblib.parallel.Parallel`
Examples
>>> db = SpecDatabase(r"path/to/database") # create or loads database >>> db.update() # in case something changed >>> db.see(['Tvib', 'Trot']) # nice print in console >>> s = db.get('Tvib==3000')[0] # get a Spectrum back >>> db.add(s) # update database (and raise error because duplicate!)
Note that
SpectrumFactory
can be configured to automatically look-up and update a database when spectra are calculated.The function to auto retrieve a Spectrum from database on calculation time is a method of DatabankLoader class
You can see more examples on the Spectrum Database section of the website.
See also
load_spec()
,store()
,Methods
,get()
,get_closest()
,get_unique()
,Methods
,see()
,update()
,add()
,compress_to()
,find_duplicates()
,Compare
,fit_spectrum()
Spectrum Database- add(spectrum: Spectrum, store_name=None, if_exists_then='increment', **kwargs)[source]ΒΆ
Add Spectrum to database, whether itβs a
Spectrum
object or a file that stores one. Check itβs not in database already.- Parameters:
spectrum (
Spectrum
object, or path to a .spec file (str
)) β if aSpectrum
object: stores it in the database (using thestore()
method), then adds the file to the database folder. if a path to a file (str
): first copy the file to the database folder, then loads the copied file to the database.- Other Parameters:
store_name (
str
, orNone
) β name of the file where the spectrum will be stored. IfNone
, name is generated automatically from the Spectrum conditions (seeadd_info=
andif_exists_then=
)if_exists_then (
'increment'
,'replace'
,'error'
,'ignore'
) β what to do if file already exists. If'increment'
an incremental digit is added. If'replace'
file is replaced (!). If'ignore'
the Spectrum is not added to the database and no file is created. If'error'
(or anything else) an error is raised. Default'increment'
.**kwargs (**dict) β extra parameters used in the case where spectrum is a file and a .spec object has to be created (useless if
spectrum
is a file already). kwargs are forwarded to Spectrum.store() method. See thestore()
method for more information.Note
Other
store()
parameters can be given as kwargs arguments. See below :compress (0, 1, 2) β if
True
or 1, save the spectrum in a compressed formif 2, removes all quantities that can be regenerated with
update()
, e.g, transmittance if abscoeff and path length are given, radiance if emisscoeff and abscoeff are given in non-optically thin case, etc. If not given, use the value ofSpecDatabase.binary
The performances are usually better if compress = 2. See https://github.com/radis/radis/issues/84.add_info (list) β append these parameters and their values if they are in conditions example:
nameafter = ['Tvib', 'Trot']
discard (list of str) β parameters to exclude. To save some memory for instance Default
['lines', 'populations']
: retrieved Spectrum will loose theline_survey()
andplot_populations()
methods (but it saves a ton of memory!).
Examples
from radis.tools import SpecDatabase db = SpecDatabase(r"path/to/database") # create or loads database db.add(s, discard=['populations'])
You can see more examples on the Spectrum Database section of the website.
Spectrum DatabaseSee also
- compress_to(new_folder, compress=True, if_exists_then='error')[source]ΒΆ
Saves the Database in a new folder with all Spectrum objects under compressed (binary) format. Read/write is much faster. After the operation, a new database should be initialized in the new_folder to access the new Spectrum.
- Parameters:
new_folder (str) β folder where to store the compressed SpecDatabase. If doesnβt exist, it is created.
compress (boolean, or 2) β if
True
, saves under binary format. Faster and takes less space. If2
, additionally remove all redundant quantities.if_exists_then (
'increment'
,'replace'
,'error'
,'ignore'
) β what to do if file already exists. If'increment'
an incremental digit is added. If'replace'
file is replaced (!). If'ignore'
the Spectrum is not added to the database and no file is created. If'error'
(or anything else) an error is raised. Default'error'
.
See also
- find_duplicates(columns=None)[source]ΒΆ
Find spectra with same conditions. The first duplicated spectrum will be
'False'
, the following will be'True'
(see .duplicated()).- Parameters:
columns (list, or
None
) β columns to find duplicates on. IfNone
, use all conditions.
Examples
db.find_duplicates(columns={'x_e', 'x_N_II'}) Out[34]: file 20180710_101.spec True 20180710_103.spec True dtype: bool
You can see more examples in the Spectrum Database section
- fit_spectrum(s_exp, residual=None, normalize=False, normalize_how='max', conditions='', **kwconditions)[source]ΒΆ
Returns the Spectrum in the database that has the lowest residual with
s_exp
.- Parameters:
s_exp (Spectrum) β
Spectrum
to fit (typically: experimental spectrum)- Other Parameters:
residual (func, or
None
) β which residual function to use. IfNone
, useget_residual()
with optionignore_nan=True
and optionsnormalize
andnormalize_how
as defined by the user.get_residual
should have the form:lambda s_exp, s, normalize: func(s_exp, s, normalize=normalize)
where the output is a float. Default
None
conditions, **kwconditions (str, **dict) β restrain fitting to only Spectrum that match the given conditions in the database. See
get()
for more information.normalize (bool, or Tuple) β see
get_residual()
normalize_how (βmaxβ, βareaβ) β see
get_residual()
- Returns:
s_best β closest Spectrum to
s_exp
- Return type:
Examples
Using a customized residual function (below: to get the transmittance):
from radis import get_residual db = SpecDatabase('...') db.fit_spectrum(s_exp, get_residual=lambda s_exp, s: get_residual(s_exp, s, var='transmittance'))
You can see more examples on the Spectrum Database section More advanced tools for interactive fitting of multi-dimensional, multi-slabs spectra can be found in
fitroom
.See also
- interpolate(**kwconditions)[source]ΒΆ
Interpolate existing spectra from the database to generate a new spectrum with conditions kwargs
Examples
db.interpolate(Tgas=300, mole_fraction=0.3)
- to_dict()[source]ΒΆ
Returns all Spectra in database under a dictionary, indexed by file.
- Returns:
out β {path : Spectrum object} dictionary
- Return type:
dict
Note
SpecList.items().values()
is equivalent toSpecList.get()
- update(force_reload=False, filt='.spec', update_register_only=False)[source]ΒΆ
Reloads database, updates internal index structure and export it in
<database>.csv
.- Parameters:
force_reload (boolean) β if
True
, reloads files already in database. DefaultFalse
filt (str) β only consider files ending with
filt
. Default.spec
- Other Parameters:
update_register_only (bool) β if
True
, load files and update csv but do not keep the Spectrum in memory. DefaultFalse
Notes
Can be loaded in parallel using joblib by setting the
nJobs
andbatch_size
attributes ofSpecDatabase
. Seejoblib.parallel.Parallel
for information on the arguments
- get_FWHM(w, I, return_index=False)[source]ΒΆ
Calculate full width half maximum (FWHM) by comparing amplitudes
- Parameters:
w, I (arrays)
return_index (boolean) β if True, returns indexes for half width boundaries. Default
False
- Returns:
FWHM (float) β full width at half maximum
[xmin, xmax] (int)
- get_effective_FWHM(w, I)[source]ΒΆ
Calculate full-width-at-half-maximum (FWHM) of a triangular slit of same area and height 1
- Parameters:
w, I (arrays)
- Returns:
fwhm β effective FWHM
- Return type:
float
- get_eq_mole_fraction(initial_mixture, T_K, p_Pa)[source]ΒΆ
Calculates chemical equilibrium mole fraction at temperature T, using the CANTERA
equilibrate()
function.The calculation uses the default GRI3.0 mechanism, which was designed to model natural gas combustion, including NO formation and reburn chemistry. See GRI 3.0.
When using, cite the [CANTERA] package.
- Parameters:
initial_mixture (str) β
Gas composition. Example:
'N2:0.79, O2:0.21, CO2:363e-6'
Or:
'CO2:1'
T_K (float (K)) β temperature (Kelvin) to calculate equilibrium
P_Pa (float (Pa)) β temperature (Pascal) to calculate equilibrium
Examples
Calculate equilibrium mixture of CO2 at 2000 K, 1 atm:
get_eq_mole_fraction('CO2:1', 2000, 101325) >>> {'C': 1.7833953335281855e-19, 'CO': 0.01495998583472384, 'CO2': 0.9775311634424326, 'O': 5.7715610124613225e-05, 'O2': 0.007451135112719029}
References
- load_spec(file, binary=True) Spectrum [source]ΒΆ
Loads a .spec file into a
Spectrum
object. Addsfile
in the Spectrumfile
attribute.- Parameters:
file (str) β .spec file to load
binary (boolean) β set to
True
if the file is encoded as binary. DefaultTrue
. Will autodetect if it fails, but that may take longer.
- Returns:
Spectrum
- Return type:
a
Spectrum
object
Examples
Load an experimental spectrumRemove a baselineExample #1: Temperature fitExample #3: non-equilibrium spectrum (Tvib, Trot, x_CO)
Example #3: non-equilibrium spectrum (Tvib, Trot, x_CO)Legacy #1: Temperature fit of CO2 spectrum
Legacy #1: Temperature fit of CO2 spectrumLegacy vs recommended fitting examples
Legacy vs recommended fitting examplesSee also
- plot_slit(w, I=None, wunit='', plot_unit='same', Iunit=None, warnings=True, ls='-', title=None, waveunit=None)[source]ΒΆ
Plot slit, calculate and display FWHM, and calculate effective FWHM. FWHM is calculated from the limits of the range above the half width, while FWHM is the equivalent width of a triangular slit with the same area
- Parameters:
w, I (arrays or (str, None)) β if str, open file directly
waveunit (
'nm'
,'cm-1'
or''
) β unit of input wplot_unit (
'nm'
,'cm-1'
or'same'
) β change plot unit (and FWHM units)Iunit (str, or None) β give Iunit
warnings (boolean) β if True, test if slit is correctly centered and output a warning if it is not. Default
True
- Returns:
fix, ax β figure and ax
- Return type:
matplotlib objects
Examples
Slit Function
- plot_spec(file, what='radiance', title=True, **kwargs)[source]ΒΆ
Plot a .spec file. Uses the
plot()
method internally.- Parameters:
file (str, or Spectrum object) β .spec file to load, or Spectrum object directly
- Other Parameters:
kwargs (dict) β arguments forwarded to
plot()
- Returns:
fig β where the Spectrum has been plotted
- Return type:
matplotlib figure
See also
- save(s: Spectrum, path, discard=[], compress=True, add_info=None, add_date=None, if_exists_then='increment', verbose=True, warnings=True)[source]ΒΆ
Save a
Spectrum
object in JSON format. Object can be recovered withload_spec()
. If manySpectrum
are saved in a same folder you can view their properties with theSpecDatabase
structure.- Parameters:
s (Spectrum) β to save
path (str) β filename to save. No extension needed. If filename already exists then a digit is added. If filename is a directory then a new file is created within this directory.
discard (list of str) β parameters to discard. To save some memory.
compress (boolean) β if
False
, save under text format, readable with any editor. ifTrue
, saves under binary format. Faster and takes less space. If2
, removes all quantities that can be regenerated with s.update(), e.g, transmittance if abscoeff and path length are given, radiance if emisscoeff and abscoeff are given in non-optically thin case, etc. DefaultFalse
add_info (list, or None/False) β append these parameters and their values if they are in conditions. e.g:
add_info = ['Tvib', 'Trot']
add_date (str, or
None
/False
) β adds date in strftime format to the beginning of the filename. e.g:add_date = '%Y%m%d'
if_exists_then (
'increment'
,'replace'
,'error'
,'ignore'
) β what to do if file already exists. If'increment'
an incremental digit is added. If'replace'
file is replaced (!). If'ignore'
the Spectrum is not added to the database and no file is created. If'error'
(or anything else) an error is raised. Default'increment'
.
- Returns:
fout β filename used (may be different from given path as new info or incremental identifiers are added)
- Return type:
str
See also