Fitting

lys supports fitting 1D data. You can start fitting by pressing Ctrl+F on the graph including 1D data.

Fitting 1D data

  1. Create test data for fitting by the code below:

    import numpy
    x = np.linspace(0, 1, 101)
    y1 = 9 * x + 2 + np.random.rand(101)
    y2 = 12 * x + 1 + np.random.rand(101)
    display(Wave(y1, x), Wave(y2,x))
    
  2. Press Ctrl+F on the created graph to open fitting window.

../_images/image1.png
  1. Right click the white box and select “Add Function”.

../_images/image2.png
  1. In the dialog box, select “Linear” and click “OK” to add linear function (y = ax + b).

../_images/image3.png
  1. Check “Append” result in the side bar. The green line is shown in the graph. Enter “5” as the “a” parameter of “Linear” function (This is initial guess).

../_images/image4.png
  1. Click “Fit”. Then fitting results are shown. It is noted that b should be around 2.5 because np.random.rand gives values between 0 to 1.

../_images/image5.png
  1. Right click the function (above “Linear” in the treeview) and select “Copy Function”.

../_images/image6.png
  1. Select “wave1” in the combo box.

  2. Right click the white box and select “Paste Function”.

../_images/image7.png
  1. Fit the second data by clicking “Fit” button.

Limit fitting region

  1. Create test data for fittin by the code below:

    import numpy
    x = np.linspace(0, 1, 101)
    y = 5 * x + 1 + np.random.rand(101)
    display(Wave(y, x))
    
  2. Press Ctrl+F to open fitting window.

  3. Select region by dragging the cursor from around 0.3 to 0.8 of the horizontal axis in the graph.

../_images/region1.png
  1. Click “Load from Graph” in “x axis” group. The region you selected is loaded in the box.

../_images/region2.png
  1. Fit the data by linear function. Only the data between x = 0.3 to 0.8 is fitted.

../_images/region3.png

Parameters plot

  1. Create sequence of data for fitting by the code below:

    import numpy
    x = np.linspace(0, 1, 101)
    ys = [a * x + np.random.rand(101) for a in [1,2,3,4,5]]
    display(*[Wave(y) for y in ys]])
    
  2. Press Ctrl+F to open fitting window. Fit the first data by linear function.

../_images/param1.png
  1. Check “Apply to all” and click “Fit”. Click “Yes” in the two dialog box.

../_images/param2.png
  1. You can confirm that all curves are fitted by linear function by changing the item in the “Target” box.

../_images/param3.png
  1. Right click “a” parameter in “Linear” function and select “Plot this parameter”.

../_images/param4.png
  1. Fitting result (“a”) for all five data is shown as a graph.

../_images/param5.png

Add new function

  1. You can define your original function for fitting. Select “Python”-“Create new .py file” in the main menu.

../_images/newFunc1.png
  1. Enter arbitrary name for the file (Here we use “func_fit”).

../_images/newFunc2.png
  1. Define new function by pasting the code below:

    from lys import *
    
    def quad(x, a, b, c):
        return a*x**2 + b*x + c
    
    registerFittingFunction(quad)
    
  2. Save the .py file by pressing Ctrl+S

  3. Make a test data by calling the code below from the command line:

    import numpy as np
    x = np.linspace(0,1,101)
    y = 3*x**2 + 2*x + 3 + np.random.rand(101)
    display(Wave(y,x))
    
  4. Open fitting function by pressing Ctrl+F.

  5. Select “Add Function” by right clicking the white box in the sidebar.

../_images/newFunc3.png
  1. You can find “quad” function in the dialog box. Select “quad”.

  2. Click “Fit” button. You can fit the data by quadratic function.

../_images/newFunc4.png