Fitting with polynomials

This notebook is available at arjunsavel/cortecs

Fitting with polynomials#

For our final fitting tutorial, we’ll review how to fit opacity functions with polynomials. This approach is generally the least performant of the three currently supported, though it likely performs well enough for very smoothly varying opacity functions (such as collisionally induced absorption).

Setting up the objects#

[1]:
import numpy as np
import sys
import os

sys.path.insert(0, os.path.abspath("../../src"))

import cortecs
from cortecs.opac.opac import *
from cortecs.fit.fit import *
from cortecs.fit.fit_pca import *
from cortecs.eval.eval import *
/home/docs/checkouts/readthedocs.org/user_builds/cortecs/checkouts/latest/src/cortecs/opac/io.py:11: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm
2024-03-13 00:08:07.528233: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-03-13 00:08:07.528282: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-03-13 00:08:07.529499: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-03-13 00:08:08.448320: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

We’ll be using the same Opac object as in the Quickstart.

[2]:
T_filename = "temperatures.npy"
P_filename = "pressures.npy"
wl_filename = "wavelengths.npy"

cross_sec_filename = "absorb_coeffs_C2H4.npy"

load_kwargs = {
    "T_filename": T_filename,
    "P_filename": P_filename,
    "wl_filename": wl_filename,
}
opac_obj = Opac(cross_sec_filename, loader="platon", load_kwargs=load_kwargs)

Now, we instantiate a Fitter with the polynomial method.

[3]:
fitter = Fitter(opac_obj, method="polynomial")
fitter
[3]:
<cortecs.fit.fit.Fitter at 0x7f9f572b7bd0>
[4]:
fitter.fit()
100%|██████████| 4616/4616 [00:17<00:00, 269.13it/s]

Let’s use an Evaluator to see how well we captured the opacity function.

[5]:
evaluator = Evaluator(opac_obj, fitter)
[6]:
temperature = 300.0
pressure = 100
wavelength = 2.99401875e-05

evaluator.eval(pressure, temperature, wavelength)
[6]:
Array(1.3987867e-05, dtype=float32)

Accuracy-wise, this doesn’t perform that well. This performance hit is partially because this opacity zeros out in many corners of parameter space, and the polynomial fit can’t account for those sharp edges.

Let’s check the evaluation time, as well.

[7]:
%%timeit

evaluator.eval(temperature, pressure, wavelength)
228 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
[8]:
vals, orig_vals, abs_diffs, percent_diffs = calc_metrics(
    fitter, tp_undersample_factor=2, plot=True
);
100%|██████████| 15/15 [00:15<00:00,  1.06s/it]
../../_images/pages_fitting_polynomial_15_1.png
../../_images/pages_fitting_polynomial_15_2.png

This is a bit slower than some of the other methods, too.

[9]:
np.median(np.abs(abs_diffs))
[9]:
5.591570457344734
[10]:
opac_obj.cross_section.nbytes / fitter.fitter_results[1].nbytes
[10]:
24.375
[ ]: