Fitting GISAS in 2D

This example demonstrates fitting of 2D GISAS data.

Faked experimental data

The sample and instrument model is basically the same as in our basic GISAS simulation example, except that it depends on four external parameters: beam intensity, detector background, radius and height of the cylindrical nanoparticles.

Faked experimental data have been generated by the script devtools/fakeData/fake-gisas1.py .

The intensity for each detector pixel has been drawn at random from a Poisson distribution, with rate basically given by the fit model except for some imperfections that have been introduced on purpose to make the subsequent fitting more difficult and more realistic:

  • Refractive indices changed, and absorption strongly enhanced;
  • Cylinders replaced by a mixture of cones and segmented spheroids;
  • Beam wavelength and alpha_i slightly changed;
  • Detector x-axis skewed.

The faked data can be found at Examples/data/faked-gisas1.txt.gz .

Fit script

The fit script shown and discussed below displays its progress visually, and terminates with the following result:

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
#!/usr/bin/env python3
"""
Minimal working fit examples: finds radius of sphere in Born approximation.
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm


def get_simulation(params):
    """
    Returns GISAS simulation for given set of parameters.
    """
    radius = params["radius"]

    sphere = ba.Particle(ba.RefractiveMaterial("Particle", 6e-4, 2e-8),
                         ba.Sphere(radius))

    layer = ba.Layer(ba.RefractiveMaterial("Vacuum", 0, 0))
    layer.addLayout(ba.ParticleLayout(sphere))
    sample = ba.MultiLayer()
    sample.addLayer(layer)

    n = bp.simargs['n']
    beam = ba.Beam(1, 0.1*nm, 0.2*deg)
    detector = ba.SphericalDetector(n, -1 * deg, 1 * deg, n, 0, 2 * deg)
    simulation = ba.ScatteringSimulation(beam, sample, detector)

    return simulation


def real_data():
    """
    Generating "experimental" data by running simulation with default parameters.
    """
    simulation = get_simulation({'radius': 5 * nm})
    result = simulation.simulate()
    return result.array()


if __name__ == '__main__':
    bp.parse_args(sim_n=100)

    fit_objective = ba.FitObjective()
    fit_objective.addSimulationAndData(get_simulation, real_data())
    fit_objective.initPrint(10)

    params = ba.Parameters()
    params.add("radius", 4. * nm, min=0.01)

    minimizer = ba.Minimizer()
    result = minimizer.minimize(fit_objective.evaluate, params)
    fit_objective.finalize(result)
Examples/fit/scatter2d/fit2d.py

Explanation

The function get_sample is basically the same as in our basic GISAS simulation example, except that radius and height of the cylindrical disks are now supplied as external parameters. These parameters are passed through the function argument params which can be either a Python dict, or an instance of the BornAgain class Parameters.

The function get_simulation has the same function argument params, from which it takes beam intensity and background level. These two parameters are on a logarithmic scale.

The function start_parameters_1 is supplied along with the model in order to facilitate the benchmarking of different fith methods.

The FitObjective created here is used to put into correspondence the real data, represented by a numpy array, and the simulation, represented by the get_simulation callable. On every fit iteration FitObjective

  • will generate a new simulation object for a given set of fit parameter values using the get_simulation callable,
  • run it to obtain simulated detector intensities,
  • calculate $\chi^2$ between simulated and real data.

The method fit_objective.evaluate provided by the FitObjective class interface is used here as an objective function. The method accepts fit parameters and returns the $\chi^2$ value, calculated between experimental and simulated images for the given values of the fit parameters.

The method is passed to the minimizer together with the initial fit parameter values. The minimizer.minimize starts a fit that will continue further without user intervention until the minimum is found or the minimizer failed to converge. The rest of the code demonstrates how to access the fit results.