.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_1T_fit.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Run this example online : - Click :ref:`here ` to download the full example code - Then start `Radis-Lab `__, upload the Jupyter notebook, and run it from there. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_1T_fit.py: .. _example_one_temperature_fit: ================= 1 temperature fit ================= Quickly fit an experimental spectrum with a one-temperature model, directly from :py:class:`~radis.lbl.factory.SpectrumFactory`, with :py:meth:`~radis.lbl.factory.SpectrumFactory.fit_spectrum` The method requires a fitting model. An example model is provided in :py:mod:`radis.tools.fitting` : :py:func:`~radis.tools.fitting.LTEModel`. Other models can be used; such as in the :ref:`multi-temperature fit example` More advanced tools for interactive fitting of multi-dimensional, multi-slabs spectra can be found in :py:mod:`fitroom`. Finally, the :ref:`GPU-accelerated example` shows how to obtain real-time interactive spectra. .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: default from radis import SpectrumFactory, load_spec .. GENERATED FROM PYTHON SOURCE LINES 27-28 Here we get an experimental spectrum from RADIS test cases. Use your own instead. .. GENERATED FROM PYTHON SOURCE LINES 28-44 .. code-block:: default from radis.test.utils import getTestFile, setup_test_line_databases setup_test_line_databases() # fit range wlmin = 4167 wlmax = 4180 s_exp = ( load_spec(getTestFile("CO2_measured_spectrum_4-5um.spec")) .crop(wlmin, wlmax, "nm") .normalize() .sort() .offset(-0.2, "nm") ) .. GENERATED FROM PYTHON SOURCE LINES 45-47 Customize the :py:func:`~radis.tools.fitting.LTEModel` for our case: we add a slit (non fittable parameter) and normalize it .. GENERATED FROM PYTHON SOURCE LINES 47-61 .. code-block:: default from radis.tools.fitting import LTEModel def LTEModel_withslitnorm(factory, fit_parameters, fixed_parameters): s = LTEModel(factory, fit_parameters, fixed_parameters) # we could also have added a fittable parameter, such as an offset, # or made the slit width a fittable parameter. # ... any paramter in model_input will be fitted. # s.offset(model_input["offset"], 'nm') s.apply_slit(1.4, "nm") return s.take("radiance").normalize() .. GENERATED FROM PYTHON SOURCE LINES 62-63 using :py:meth:`~radis.lbl.factory.SpectrumFactory.fit_spectrum` .. GENERATED FROM PYTHON SOURCE LINES 63-100 .. code-block:: default import astropy.units as u sf = SpectrumFactory( wlmin * u.nm, wlmax * u.nm, wstep=0.001, # cm-1 pressure=1 * 1e-3, # bar cutoff=1e-25, isotope="1,2", path_length=10, # cm-1 mole_fraction=1, truncation=1, # cm-1 ) sf.warnings["MissingSelfBroadeningWarning"] = "ignore" sf.warnings["HighTemperatureWarning"] = "ignore" sf.load_databank("HITRAN-CO2-TEST") s_best, best = sf.fit_spectrum( s_exp.take("radiance"), model=LTEModel_withslitnorm, fit_parameters={ "Tgas": 300, # "offset": 0 }, bounds={ "Tgas": [300, 2000], # "offset": [-1, 1], }, plot=True, solver_options={ "maxiter": 15, # 👈 increase to let the fit converge "ftol": 1e-15, }, verbose=2, ) # plot_diff(s_exp, s_best) # , show_ruler=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_1T_fit_001.png :alt: plot 1T fit :srcset: /auto_examples/images/sphx_glr_plot_1T_fit_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_1T_fit_002.png :alt: plot 1T fit :srcset: /auto_examples/images/sphx_glr_plot_1T_fit_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Using database: HITRAN-CO2-TEST 'HITRAN-CO2-TEST': {'info': 'HITRAN 2016 database, CO2, 1 main isotope (CO2-626), bandhead: 2380-2398 cm-1 (4165-4200 nm)', 'path': ['/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/test/files/hitran_co2_626_bandhead_4165_4200nm.par'], 'format': 'hitran', 'parfuncfmt': 'hapi', 'levelsfmt': 'radis'} Generating cache file /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/test/files/hitran_co2_626_bandhead_4165_4200nm.h5 with metadata : {'last_modification': 'Sun Aug 28 21:55:18 2022', 'wavenum_min': 2380.019436, 'wavenum_max': 2399.965532} Reference databank (2391.79-2399.14cm-1) has 0 lines in range (2391.69-2399.15cm-1) for isotope 2. Change your range or isotope options /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/log.py:54: UserWarning: Reference databank (2391.79-2399.14cm-1) has 0 lines in range (2391.69-2399.15cm-1) for isotope 2. Change your range or isotope options Calculating Equilibrium Spectrum Physical Conditions ---------------------------------------- Tgas 300 K Trot 300 K Tvib 300 K isotope 1,2 mole_fraction 1 molecule CO2 overpopulation None path_length 10 cm pressure_mbar 1.0 mbar rot_distribution boltzmann self_absorption True state X vib_distribution boltzmann wavenum_max 2399.1537 cm-1 wavenum_min 2391.6923 cm-1 Computation Parameters ---------------------------------------- Tref 296 K add_at_used broadening_method voigt cutoff 1e-25 cm-1/(#.cm-2) dbformat hitran dbpath /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis... folding_thresh 1e-06 include_neighbouring_lines True memory_mapping_engine auto neighbour_lines 0 cm-1 optimization simple parfuncfmt hapi parsum_mode full summation pseudo_continuum_threshold 0 sparse_ldm auto truncation 1 cm-1 waveunit cm-1 wstep 0.001 cm-1 zero_padding -1 ---------------------------------------- /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning: Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff 0.03s - Spectrum calculated /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning: Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff ------------------------------ TYPICAL FIT CALCULATION TIME: Fit (in progress) profiler : spectrum_calculation 0.025s ████████████████ check_line_databank 0.001s reinitialize 0.002s █ copy_database 0.000s memory_usage_warning 0.002s █ reset_population 0.000s scaled_eq_linestrength 0.002s █ applied_linestrength_cutoff 0.001s calc_lineshift 0.001s calc_hwhm 0.003s ██ generate_wavenumber_arrays 0.000s calc_line_broadening 0.011s ███████ precompute_LDM_lineshapes 0.003s █ LDM_Initialized_vectors 0.000s LDM_closest_matching_line 0.000s LDM_Distribute_lines 0.005s ███ LDM_convolve 0.003s █ calc_other_spectral_quan 0.003s █ generate_spectrum_obj 0.000s ------------------------------ /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning: Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff Now starting the fitting process: --------------------------------- Tgas=1150.0, Residual: 0.0105 🏆 Tgas=1170.0, Residual: 0.0098 🏆 Tgas=1150.0, Residual: 0.0105 Tgas=1170.0, Residual: 0.0098 🏆 Tgas=2000.0, Residual: 0.0128 Tgas=1980.0, Residual: 0.0128 Tgas=1396.0, Residual: 0.0034 🏆 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1396.0, Residual: 0.0034 Tgas=1416.0, Residual: 0.0031 🏆 Tgas=1397.0, Residual: 0.0034 Tgas=1417.0, Residual: 0.0030 🏆 Tgas=1402.0, Residual: 0.0033 Tgas=1422.0, Residual: 0.0030 🏆 Tgas=1420.0, Residual: 0.0030 Tgas=1440.0, Residual: 0.0028 🏆 Tgas=1458.0, Residual: 0.0027 🏆 Tgas=1478.0, Residual: 0.0028 Init ['Tgas'] = [1150.][''] Final ['Tgas'] = [1458.][''] fun: 0.0027300270273360972 hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64> jac: array([3.1783589e-06]) message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL' nfev: 32 nit: 4 njev: 16 status: 0 success: True x: array([1457.50267416]) Best ['Tgas'] = [1457.50267416][''] reached at iteration 32/32 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.451 seconds) .. _sphx_glr_download_auto_examples_plot_1T_fit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1T_fit.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1T_fit.ipynb `