Simultaneous fit of two datasets

In this example we demonstrate how to fit two datasets simultaneously.

Suppose that we have a sample measured twice for two different incident angles. We are going to fit both datasets simultaneously to find the unknown sample parameters.

To do this, we define one dataset (a pair of real data and corresponding simulation builder) for the first incidence angle and another pair for the second incidence angle. We add both pairs to the FitObjective and run the fit as usual.

  • In the given script we simulate hemi-ellipsoids on top of a substrate without interference. At lines 184-186 we define 3 fitting parameters: radius_a and height are parameters to find, radius_b is fixed.
  • At lines 171-145 we define two fixed incident alpha angles equal to $0.1^{\circ}$ and $0.4^{\circ}$ and generate two arrays with experimental data.
  • The functions defined at lines 55 and 60 represent simulation builders to use together with the experimental data.
  • The FitObjective is initialized on lines 174-176 with two simulation/data pairs.
  • The majority of the code is located in a custom PlotObserver class (defined in line 85, and invoked at lines 180, 181), which plots the fit progress for the two datasets every 10th iteration.

Fit window

  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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
Fitting example: simultaneous fit of two datasets
"""

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import bornagain as ba
from bornagain import deg, angstrom, nm


def get_sample(params):
    """
    Returns a sample with uncorrelated cylinders and pyramids.
    """
    radius_a = params["radius_a"]
    radius_b = params["radius_b"]
    height = params["height"]

    m_air = ba.HomogeneousMaterial("Air", 0.0, 0.0)
    m_substrate = ba.HomogeneousMaterial("Substrate", 6e-6, 2e-8)
    m_particle = ba.HomogeneousMaterial("Particle", 6e-4, 2e-8)

    formfactor = ba.FormFactorHemiEllipsoid(radius_a, radius_b, height)
    particle = ba.Particle(m_particle, formfactor)

    layout = ba.ParticleLayout()
    layout.addParticle(particle)

    air_layer = ba.Layer(m_air)
    air_layer.addLayout(layout)

    substrate_layer = ba.Layer(m_substrate)
    multi_layer = ba.MultiLayer()
    multi_layer.addLayer(air_layer)
    multi_layer.addLayer(substrate_layer)
    return multi_layer


def get_simulation(params):
    """
    Returns a GISAXS simulation with beam and detector defined.
    """
    incident_angle = params["incident_angle"]

    simulation = ba.GISASSimulation()
    simulation.setDetectorParameters(50, -1.5*deg, 1.5*deg,
                                     50, 0.0*deg, 2.0*deg)
    simulation.setBeamParameters(1.0*angstrom, incident_angle, 0.0*deg)
    simulation.setBeamIntensity(1e+08)
    simulation.setSample(get_sample(params))
    return simulation


def simulation1(params):
    params["incident_angle"] = 0.1*deg
    return get_simulation(params)


def simulation2(params):
    params["incident_angle"] = 0.4*deg
    return get_simulation(params)


def create_real_data(incident_alpha):
    """
    Generating "real" data by adding noise to the simulated data.
    """
    params = {'radius_a': 5.0*nm, 'radius_b': 6.0*nm,
              'height': 8.0*nm, "incident_angle": incident_alpha}

    simulation = get_simulation(params)
    simulation.runSimulation()

    # retrieving simulated data in the form of numpy array
    real_data = simulation.result().array()

    # spoiling simulated data with the noise to produce "real" data
    noise_factor = 0.1
    noisy = np.random.normal(real_data, noise_factor*np.sqrt(real_data))
    noisy[noisy < 0.1] = 0.1
    return noisy


class PlotObserver():
    """
    Draws fit progress every nth iteration. Real data, simulated data
    and chi2 map will be shown for both datasets.
    """
    def __init__(self):
        self.fig = plt.figure(figsize=(12.8, 10.24))
        self.fig.canvas.draw()

    def __call__(self, fit_objective):
        self.update(fit_objective)

    @staticmethod
    def plot_dataset(fit_objective, canvas):
        for i_dataset in range(0, fit_objective.fitObjectCount()):
            real_data = fit_objective.experimentalData(i_dataset)
            simul_data = fit_objective.simulationResult(i_dataset)
            chi2_map = fit_objective.relativeDifference(i_dataset)

            zmax = real_data.histogram2d().getMaximum()

            plt.subplot(canvas[i_dataset*3])
            ba.plot_colormap(real_data, title="\"Real\" data - #"+str(i_dataset+1),
                             zmin=1.0, zmax=zmax, zlabel="")
            plt.subplot(canvas[1+i_dataset*3])
            ba.plot_colormap(simul_data, title="Simulated data - #"+str(i_dataset+1),
                             zmin=1.0, zmax=zmax, zlabel="")
            plt.subplot(canvas[2+i_dataset*3])
            ba.plot_colormap(chi2_map, title="Chi2 map - #"+str(i_dataset+1),
                             zmin=0.001, zmax=10.0, zlabel="")

    @staticmethod
    def display_fit_parameters(fit_objective):
        """
        Displays fit parameters, chi and iteration number.
        """
        plt.title('Parameters')
        plt.axis('off')

        iteration_info = fit_objective.iterationInfo()

        plt.text(0.01, 0.85, "Iterations  " + '{:d}'.
                 format(iteration_info.iterationCount()))
        plt.text(0.01, 0.75, "Chi2       " + '{:8.4f}'.format(iteration_info.chi2()))
        for index, params in enumerate(iteration_info.parameters()):
            plt.text(0.01, 0.55 - index * 0.1,
                     '{:30.30s}: {:6.3f}'.format(params.name(), params.value))

    @staticmethod
    def plot_fit_parameters(fit_objective):
        """
        Displays fit parameters, chi and iteration number.
        """
        plt.axis('off')

        iteration_info = fit_objective.iterationInfo()

        plt.text(0.01, 0.95, "Iterations  " + '{:d}'.
                 format(iteration_info.iterationCount()))
        plt.text(0.01, 0.70, "Chi2       " + '{:8.4f}'.format(iteration_info.chi2()))
        for index, params in enumerate(iteration_info.parameters()):
            plt.text(0.01, 0.30 - index * 0.3,
                     '{:30.30s}: {:6.3f}'.format(params.name(), params.value))

    def update(self, fit_objective):
        self.fig.clf()

        # we divide figure to have 3x3 subplots, with two first rows occupying
        # most of the space
        canvas = matplotlib.gridspec.GridSpec(
            3, 3, width_ratios=[1, 1, 1], height_ratios=[4, 4, 1])
        canvas.update(left=0.05, right=0.95, hspace=0.5, wspace=0.2)

        self.plot_dataset(fit_objective, canvas)
        plt.subplot(canvas[6:])
        self.plot_fit_parameters(fit_objective)

        plt.draw()
        plt.pause(0.01)


def run_fitting():
    """
    main function to run fitting
    """

    data1 = create_real_data(0.1 * deg)
    data2 = create_real_data(0.4 * deg)

    fit_objective = ba.FitObjective()
    fit_objective.addSimulationAndData(simulation1, data1, 1.0)
    fit_objective.addSimulationAndData(simulation2, data2, 1.0)
    fit_objective.initPrint(10)

    # creating custom observer which will draw fit progress
    plotter = PlotObserver()
    fit_objective.initPlot(10, plotter.update)

    params = ba.Parameters()
    params.add("radius_a", 4.*nm, min=2.0, max=10.0)
    params.add("radius_b", 6.*nm, vary=False)
    params.add("height", 4.*nm, min=2.0, max=10.0)

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


if __name__ == '__main__':
    run_fitting()
    plt.show()
multiple_datasets.py