{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fit data\n\nThis demo shows how data fitting can be performed using the ``itom.dataObject`` and ``itom.algorithms``.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom itom import dataObject\nfrom itom import algorithms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Polynomial of order 2 in x- and y-direction\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def polyFuncOrder2x2(x: float, y:float) -> float:\n return 2.5 * x ** 2 + -1.7 * y ** 2 + 1.3 * x * y + 0.7 * x - 0.3 * y + 3.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vectorize this function to be evaluated over an array of x and y-coordinates\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = np.vectorize(polyFuncOrder2x2)\n\n[X, Y] = np.meshgrid(np.arange(-10, 10.5, 0.5), np.arange(-10, 10.5, 0.5))\nZ = f(X, Y)\ntotal = np.prod(Z.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First create a 2d polynomial fit with order x = 2 and order y = 2.\nZ_ must be a regular grid where the x- and y- values are\ndefined by its axisScales and axisOffsets attributes. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Z_ = dataObject(Z)\nZ_.axisScales = (0.5, 0.5)\nZ_.axisOffsets = (20, 20)\n\ncoeffs = algorithms.polyfitWeighted2D(Z_, 2, 2)\nprint(\"coefficients: \", coeffs)\n\n# Reconstruct the fitted sphere using the determined coefficients.\n# First, create a ``dataObject`` with the desired size, scaling and offset for the\n# the grid of x- and y- values. The z-values are then calculated.\nZ_reconstruction = Z_.copy()\nZ_reconstruction[:, :] = float(\"nan\")\n\nalgorithms.polyval2D(Z_reconstruction, coeffs, 2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Randomly select a number of samples unique values in the range ``[0,total)``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "samples = 100\nrandomUniqueValues = np.random.choice(total, samples)\nX2 = dataObject([1, samples], \"float64\")\nY2 = dataObject([1, samples], \"float64\")\nZ2 = dataObject([1, samples], \"float64\")\nc = Z.shape[1]\n\nfor i in range(samples):\n idx = randomUniqueValues[i]\n X2[0, i] = X[int(idx / c), idx % c]\n Y2[0, i] = Y[int(idx / c), idx % c]\n Z2[0, i] = Z[int(idx / c), idx % c]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Determine the polyonimal coefficients only using the random samples.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "coeffs2 = algorithms.polyfitWeighted2DSinglePoints( X2, Y2, Z2, 2, 2)\n# coeffs and coeffs2 must be the same!\nprint(\"fitted coefficient: \", coeffs2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And reconstruct the entire surface for X and Y values.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Z2_reconstruction = dataObject()\nalgorithms.polyval2DSinglePoints(\n dataObject(X),\n dataObject(Y),\n Z2_reconstruction,\n coeffs2,\n 2,\n 2,\n)\n\nsample_reconstruction = dataObject()\nalgorithms.polyval2DSinglePoints(X2, Y2, sample_reconstruction, coeffs2, 2, 2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }