Taking uncertainties into account

In this example we are demonstrating how to allow for uncertainties during a Reflectometry fitting job. The sample to fit consists of twenty Titanium-Nickel bilayers. Assuming that all Titanium layers have the same thickness, the goal is to find that thickness.

The reference data was generated with GENX, setting the thickness of the Ti layers equal to 3 nm.

This example follows closely the tutorial on Fitting reflectometry data. The main points to focus on here are the following:

  • Added artificial uncertainties to the data being fitted
  • Use of the the $RQ^4$ view for plotting
  • Use of $\chi^2$ with $L_1$ normalization as the objective metric
  • Setting a genetic algorithm as the minimizer
Fitting with uncertainties plot. Notice the $RQ^4$ scale of the Intensity axis

Fitting with uncertainties plot. Notice the $RQ^4$ scale of the Intensity axis

  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
"""
Example demonstrates how to fit specular data.
Our sample represents twenty interchanging layers of Ti and Ni. We will fit
thicknesses of all Ti layers, assuming them being equal.

Reference data was generated with GENX for ti layers' thicknesses equal to 3 nm

This example uses exactly the same data as in FitSpecularBasics. However this
time we will add artificial uncertainties and use RQ^4 view.
Besides we will set Chi squared with L1-normalization as the objective metric
and use genetic algorithm as the minimizer.
"""

import numpy as np
import bornagain as ba
from matplotlib import pyplot as plt
from os import path


def get_sample(params):
    """
    Creates a sample and returns it
    :param params: a dictionary of optimization parameters
    :return: the sample defined
    """

    # substrate (Si)
    si_sld_real = 2.0704e-06  # \AA^{-2}
    density_si = 0.0499 / ba.angstrom ** 3  # Si atomic number density

    # layers' parameters
    n_repetitions = 10
    # Ni
    ni_sld_real = 9.4245e-06  # \AA^{-2}
    ni_thickness = 70 * ba.angstrom
    # Ti
    ti_sld_real = -1.9493e-06  # \AA^{-2}
    ti_thickness = params["ti_thickness"]

    # defining materials
    m_air = ba.MaterialBySLD()
    m_ni = ba.MaterialBySLD("Ni", ni_sld_real, 0.0)
    m_ti = ba.MaterialBySLD("Ti", ti_sld_real, 0.0)
    m_substrate = ba.MaterialBySLD("SiSubstrate", si_sld_real, 0.0)

    # air layer and substrate form multi layer
    air_layer = ba.Layer(m_air)
    ni_layer = ba.Layer(m_ni, ni_thickness)
    ti_layer = ba.Layer(m_ti, ti_thickness)
    substrate_layer = ba.Layer(m_substrate)
    multi_layer = ba.MultiLayer()
    multi_layer.addLayer(air_layer)
    for i in range(n_repetitions):
        multi_layer.addLayer(ti_layer)
        multi_layer.addLayer(ni_layer)
    multi_layer.addLayer(substrate_layer)
    return multi_layer


def get_real_data():
    """
    Loading data from genx_interchanging_layers.dat
    Returns a Nx2 array (N - the number of experimental data entries)
    with first column being coordinates,
    second one being values.
    """
    if not hasattr(get_real_data, "data"):
        filename = "genx_interchanging_layers.dat.gz"
        filepath = path.join(path.dirname(path.realpath(__file__)), filename)
        real_data = np.loadtxt(filepath, usecols=(0, 1), skiprows=3)

        # translating axis values from double incident angle (degrees)
        # to incident angle (radians)
        real_data[:, 0] *= np.pi / 360
        get_real_data.data = real_data
    return get_real_data.data.copy()


def get_real_data_axis():
    """
    Get axis coordinates of the experimental data
    :return: 1D array with axis coordinates
    """
    return get_real_data()[:, 0]


def get_real_data_values():
    """
    Get experimental data values as a 1D array
    :return: 1D array with experimental data values
    """
    return get_real_data()[:, 1]


def get_simulation(params):
    """
    Create and return specular simulation with its instrument defined
    """
    wavelength = 1.54 * ba.angstrom  # beam wavelength

    simulation = ba.SpecularSimulation()
    scan = ba.AngularSpecScan(wavelength, get_real_data_axis())
    simulation.setScan(scan)
    simulation.setSample(get_sample(params))
    return simulation


def run_fitting():
    """
    Setup simulation and fit
    """

    real_data = get_real_data_values()
    # setting artificial uncertainties (uncertainty sigma equals a half
    # of experimental data value)
    uncertainties = real_data * 0.5

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

    plot_observer = ba.PlotterSpecular(units=ba.AxesUnits.RQ4)
    fit_objective.initPrint(10)
    fit_objective.initPlot(10, plot_observer)
    fit_objective.setObjectiveMetric("Chi2", "L1")

    params = ba.Parameters()
    params.add("ti_thickness", 50 * ba.angstrom,
               min=10 * ba.angstrom, max=60 * ba.angstrom)

    minimizer = ba.Minimizer()
    minimizer.setMinimizer("Genetic", "", "MaxIterations=40;PopSize=10")
    result = minimizer.minimize(fit_objective.evaluate, params)

    fit_objective.finalize(result)


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

Reference data