Find background

This example demonstrates how to introduce a background in the simulation and fit its value. Here we are simulating cylinders on top of a substrate without interference. The function get_simulation requires 4 parameters:

  • the height of the cylinders
  • the radius of the cylinders
  • the value of the constant background
  • a scale factor for the beam’s intensity

The radius and height of the cylinders are passed to the function constructing the multi layer while the scale and background values are used to initialize the instrument.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""
Fitting example: looking for background and scale factors.

Real data contains some "unknown" background and scale factor.
In the fit we are trying to find cylinder radius and height,
scale and background factors.
"""

from matplotlib import pyplot as plt
import bornagain as ba
from bornagain import deg, nm, ba_fitmonitor
import model1_cylinders as model


def get_simulation(params):
    """
    Create and return GISAXS simulation with beam and detector defined
    """
    background = params["background"]
    scale = params["scale"]

    simulation = model.get_simulation(params)
    simulation.setBackground(ba.ConstantBackground(background))

    return simulation


def create_real_data():
    """
    Generating "real" data by adding noise, background and scale
    to the simulated data.
    Cylinder radius is set to 5nm, cylinder height to 10nm.
    During the fit we will try to find cylinder height and radius and
    scale, background factors.
    """

    params = {
        'radius': 5*nm,
        'height': 10*nm,
        'scale': 2,
        'background': 1000
    }

    simulation = get_simulation(params)
    result = simulation.simulate()

    # retrieving simulated data in the form of numpy array
    return result.array()


if __name__ == '__main__':
    real_data = create_real_data()

    fit_objective = ba.FitObjective()
    fit_objective.addSimulationAndData(get_simulation, real_data, 1)

    fit_objective.initPrint(10)
    observer = ba_fitmonitor.PlotterGISAS()
    fit_objective.initPlot(10, observer)

    params = ba.Parameters()
    params.add("radius", 5.*nm, vary=False)
    params.add("height", 9.*nm, min=8.*nm, max=12.*nm)
    params.add("scale", 1.5, min=1, max=3)
    params.add("background", 200, min=100, max=2000, step=100)

    minimizer = ba.Minimizer()
    result = minimizer.minimize(fit_objective.evaluate, params)
    fit_objective.finalize(result)

    plt.show()
Examples/fit/scatter2d/find_background.py