.. 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 Click :ref:`here ` 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:: default 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:: default 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:: default 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:: default 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.1999999999995303, -0.2999999999999668, -1.6999999999999904, 0.6999999999999899, 1.3000000000000003, 2.499999999999999) .. 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:: default 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:: default 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.2000000000001094, -0.3000000000000037, -1.6999999999999997, 0.7000000000000003, 1.300000000000001, 2.499999999999998) .. GENERATED FROM PYTHON SOURCE LINES 67-68 And reconstruct the entire surface for X and Y values. .. GENERATED FROM PYTHON SOURCE LINES 68-79 .. code-block:: default Z2_reconstruction = dataObject() algorithms.polyval2DSinglePoints( dataObject(X), dataObject(Y), Z2_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.013 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-python :download:`Download Python source code: demo_FitData.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_FitData.ipynb `