Legacy #1: Temperature fit of CO2 spectrumΒΆ
Quickly fit an experimental spectrum with a one-temperature model,
directly from SpectrumFactory
,
with fit_legacy()
The method requires a fitting model. An example model is provided in radis.tools.fitting
:
LTEModel()
. Other models can be used; such as in
the multi-temperature fit example
More advanced tools for interactive fitting of multi-dimensional, multi-slabs
spectra can be found in fitroom
.
Finally, the GPU-accelerated example shows
how to obtain real-time interactive spectra.
from radis import SpectrumFactory, load_spec
Here we get an experimental spectrum from RADIS test cases. Use your own instead.
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")
)
Customize the LTEModel()
for our case: we add a slit
(non fittable parameter) and normalize it
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 parameter in model_input will be fitted.
s.offset(0, "nm") # or we could have used a fittable parameter below :
# s.offset(model_input["offset"], 'nm')
# Alternative: with a wavelength offset
# WARNING: very sensitive parameter
# copy_parameters = fit_parameters.copy()
# offset = copy_parameters["offset"]
# copy_parameters.pop("offset")
# s = LTEModel(factory, copy_parameters, fixed_parameters)
# s.offset(offset, 'nm')
# Comment:
# We could also have made the slit width a fittable parameter.
# ... any parameter in model_input will be fitted.
# Here we simply employ a fixed slit.
s.apply_slit(1.4, "nm")
return s.take("radiance").normalize()
using fit_legacy()
import astropy.units as u
sf = SpectrumFactory(
wlmin * u.nm,
wlmax * u.nm,
molecule="CO2",
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
verbose=0,
)
sf.warnings["MissingSelfBroadeningWarning"] = "ignore"
sf.warnings["HighTemperatureWarning"] = "ignore"
sf.load_databank(
"HITRAN-CO2-TEST"
) # see 'fetch_databank' below for a more general application
# sf.fetch_databank("hitemp") #use "hitemp" or another database
s_best, best = sf.fit_legacy(
s_exp.take("radiance"),
model=LTEModel_withslitnorm,
fit_parameters={
"Tgas": 1450,
# "offset": 0
},
bounds={
"Tgas": [300, 2000],
# "offset": [-1, 1],
},
plot=True,
solver_options={
"maxiter": 15, # π increase to let the fit converge
"ftol": 1e-15,
# "gtol": 1e-10,
# "eps":1e-5
},
verbose=2,
)
# plot_diff(s_exp, s_best) # , show_ruler=True)
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/db/molparam.py:252: FutureWarning:
The 'delim_whitespace' keyword in pd.read_csv is deprecated and will be removed in a future version. Use ``sep='\s+'`` instead
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/hitranapi.py:128: FutureWarning:
A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/api/tools.py:280: FutureWarning:
Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/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
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
------------------------------
TYPICAL FIT CALCULATION TIME:
Fit (in progress) profiler :
spectrum_calculation 0.026s ββββββββββββββββ
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.004s ββ
generate_wavenumber_arrays 0.000s
calc_line_broadening 0.012s βββββββ
precompute_LDM_lineshapes 0.002s β
LDM_Initialized_vectors 0.000s
LDM_closest_matching_line 0.000s
LDM_Distribute_lines 0.006s βββ
LDM_convolve 0.004s ββ
calc_other_spectral_quan 0.002s β
generate_spectrum_obj 0.000s
------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: LinestrengthCutoffWarning:
Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Now starting the fitting process:
---------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1150.0, Residual: 0.0105 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1170.0, Residual: 0.0098 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1150.0, Residual: 0.0105
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1170.0, Residual: 0.0098 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=2000.0, Residual: 0.0128
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1980.0, Residual: 0.0128
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1396.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1416.0, Residual: 0.0031 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1397.0, Residual: 0.0034
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1417.0, Residual: 0.0030 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1402.0, Residual: 0.0033
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1422.0, Residual: 0.0030 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1420.0, Residual: 0.0030
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1440.0, Residual: 0.0028 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1458.0, Residual: 0.0027 π
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Tgas=1478.0, Residual: 0.0028
/home/docs/checkouts/readthedocs.org/user_builds/radis/checkouts/latest/radis/misc/warning.py:427: PerformanceWarning:
'object' type column found in database, calculations and memory usage would be faster with a numeric type. Possible solution is to not use 'save_memory' and convert the columns to dtype.
Init ['Tgas'] = [1150.]['']
Final ['Tgas'] = [1458.]['']
message: CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL
success: True
status: 0
fun: 0.0027300270273361805
x: [ 1.458e+03]
nit: 4
jac: [ 3.178e-06]
nfev: 32
njev: 16
hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64>
Best ['Tgas'] = [1457.50267416][''] reached at iteration 32/32
Total running time of the script: (0 minutes 3.338 seconds)