.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\itom\dataProcessing\demo_FitData.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_itom_dataProcessing_demo_FitData.py: Fit data =========== This demo shows how data fitting can be performed using the ``itom.dataObject`` and ``itom.algorithms``. .. GENERATED FROM PYTHON SOURCE LINES 5-11 .. code-block:: Python import numpy as np from itom import dataObject from itom import algorithms .. GENERATED FROM PYTHON SOURCE LINES 13-14 Polynomial of order 2 in x- and y-direction .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: Python def polyFuncOrder2x2(x: float, y: float) -> float: return 2.5 * x**2 + -1.7 * y**2 + 1.3 * x * y + 0.7 * x - 0.3 * y + 3.2 .. GENERATED FROM PYTHON SOURCE LINES 19-20 Vectorize this function to be evaluated over an array of x and y-coordinates .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: Python f = np.vectorize(polyFuncOrder2x2) [X, Y] = np.meshgrid(np.arange(-10, 10.5, 0.5), np.arange(-10, 10.5, 0.5)) Z = f(X, Y) total = np.prod(Z.shape) .. GENERATED FROM PYTHON SOURCE LINES 27-30 First create a 2d polynomial fit with order x = 2 and order y = 2. Z_ must be a regular grid where the x- and y- values are defined by its axisScales and axisOffsets attributes. .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python Z_ = dataObject(Z) Z_.axisScales = (0.5, 0.5) Z_.axisOffsets = (20, 20) coeffs = algorithms.polyfitWeighted2D(Z_, 2, 2) print("coefficients: ", coeffs) # Reconstruct the fitted sphere using the determined coefficients. # First, create a ``dataObject`` with the desired size, scaling and offset for the # the grid of x- and y- values. The z-values are then calculated. Z_reconstruction = Z_.copy() Z_reconstruction[:, :] = float("nan") algorithms.polyval2D(Z_reconstruction, coeffs, 2, 2) .. rst-class:: sphx-glr-script-out .. code-block:: none coefficients: (3.200000000000888, -0.3000000000000551, -1.7000000000000066, 0.6999999999999961, 1.2999999999999994, 2.5) .. GENERATED FROM PYTHON SOURCE LINES 46-47 Randomly select a number of samples unique values in the range ``[0,total)``. .. GENERATED FROM PYTHON SOURCE LINES 47-60 .. code-block:: Python samples = 100 randomUniqueValues = np.random.choice(total, samples) X2 = dataObject([1, samples], "float64") Y2 = dataObject([1, samples], "float64") Z2 = dataObject([1, samples], "float64") c = Z.shape[1] for i in range(samples): idx = randomUniqueValues[i] X2[0, i] = X[int(idx / c), idx % c] Y2[0, i] = Y[int(idx / c), idx % c] Z2[0, i] = Z[int(idx / c), idx % c] .. GENERATED FROM PYTHON SOURCE LINES 61-62 Determine the polyonimal coefficients only using the random samples. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python coeffs2 = algorithms.polyfitWeighted2DSinglePoints(X2, Y2, Z2, 2, 2) # coeffs and coeffs2 must be the same! print("fitted coefficient: ", coeffs2) .. rst-class:: sphx-glr-script-out .. code-block:: none fitted coefficient: (3.2000000000000166, -0.30000000000000165, -1.699999999999999, 0.6999999999999973, 1.3000000000000007, 2.5000000000000004) .. GENERATED FROM PYTHON SOURCE LINES 67-68 And reconstruct the entire surface for X and Y values. .. GENERATED FROM PYTHON SOURCE LINES 68-80 .. code-block:: Python reconstruction = dataObject() algorithms.polyval2DSinglePoints( dataObject(X), dataObject(Y), reconstruction, coeffs2, 2, 2, ) sample_reconstruction = dataObject() algorithms.polyval2DSinglePoints(X2, Y2, sample_reconstruction, coeffs2, 2, 2) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_11_demos_itom_dataProcessing_demo_FitData.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_FitData.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_FitData.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_FitData.zip `