Fitting with masks

In this example we demonstrate how to mask certain areas on the detector image to exclude their influence on the fitting procedure. This can be done by invoking the method addMask on a simulation object.

simulation = ScatteringSimulation()
simulation.addMask(Rectangle(x1, y1, x2, y2), mask_value)

where Rectangle is related to the shape of the mask in detector coordinates, mask_value can be either True (area is excluded from the simulation and fit) or False (area will stay in the simulation and will be taken into account in $\chi^2$ calculations during the fit). There can be an arbitrary number of masks of various shapes added to the simulation one after another. Each subsequent mask overrides the previously defined mask_value in the given area.

  • In the given script we simulate cylinders on top of a substrate without interference. The fitting procedure looks for the cylinder’s height and radius.
  • Line 74 contains a call to add_mask_to_simulation function which applies the masks to the detector in such a way, that the simulated image looks like a Pac-Man from the ancient arcade game.
  • In this function we start from masking the whole detector (line 89) and then we unmask the area of an elliptic shape (line 92) to simulate Pacman’s head. Then we keep adding masks of different shapes to get the final picture.

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
#!/usr/bin/env python3
"""
Fitting example: fit with masks
"""

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


def get_masked_simulation(params, add_masks=True):
    simulation = model.get_simulation(params)
    if add_masks:
        add_mask_to_simulation(simulation)
    return simulation


def add_mask_to_simulation(simulation):
    """
    Here we demonstrate how to add masks to the simulation.
    Only unmasked areas will be simulated and then used during the fit.

    Masks can have different geometrical shapes (ba.Rectangle, ba.Ellipse, Line)
    with the mask value either "True" (detector bin is excluded from the simulation)
    or False (will be simulated).

    Every subsequent mask overrides previously defined mask in this area.

    In the code below we put masks in such way that simulated image will look like
    a Pac-Man from ancient arcade game.
    """
    # mask all detector (put mask=True to all detector channels)
    simulation.maskAll()

    # set mask to simulate pacman's head
    simulation.addMask(ba.Ellipse(0, 1*deg, 0.5*deg, 0.5*deg), False)

    # set mask for pacman's eye
    simulation.addMask(ba.Ellipse(0.11*deg, 1.25*deg, 0.05*deg, 0.05*deg),
                       True)

    # set mask for pacman's mouth
    points = [[0*deg, 1*deg], [0.5*deg, 1.2*deg], [0.5*deg, 0.8*deg],
              [0*deg, 1*deg]]
    simulation.addMask(ba.Polygon(points), True)

    # giving pacman something to eat
    simulation.addMask(
        ba.Rectangle(0.45*deg, 0.95*deg, 0.55*deg, 1.05*deg), False)
    simulation.addMask(
        ba.Rectangle(0.61*deg, 0.95*deg, 0.71*deg, 1.05*deg), False)
    simulation.addMask(
        ba.Rectangle(0.75*deg, 0.95*deg, 0.85*deg, 1.05*deg), False)


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

    fit_objective = ba.FitObjective()
    fit_objective.addSimulationAndData(get_masked_simulation, real_data, 1)
    fit_objective.initPrint(10)
    observer = ba_fitmonitor.PlotterGISAS()
    fit_objective.initPlot(10, observer)

    params = ba.Parameters()
    params.add("radius", 6.*nm, min=4, max=8)
    params.add("height", 9.*nm, min=8, max=12)

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

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