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 = GISASSimulation()
```

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 Chi2 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 example we simulate cylinders on top of substrate without interference. The fitting procedure looks for the cylinder's height and radius.
• Line 130 contains a call to `add_mask_to_simulation` function which applies masks to the detector in such a way, that simulated image looks like a Pac-Man from the ancient arcade game.
• In this function we start from masking the whole detector (Line 88) and then we unmask the area of an elliptic shape (Line 91) to simulate Pacman's head. Then we keep adding masks of different shapes to get the final picture.
Intensity Image:
Python Script:
```"""
"""

from __future__ import print_function
from matplotlib import pyplot as plt
import math
import random
import bornagain as ba
from bornagain import deg, angstrom, nm

"""
Build the sample representing cylinders on top of
substrate without interference.
"""
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)

cylinder = ba.Particle(m_particle, cylinder_ff)

particle_layout = ba.ParticleLayout()

air_layer = ba.Layer(m_air)

substrate_layer = ba.Layer(m_substrate, 0)
multi_layer = ba.MultiLayer()
return multi_layer

def get_simulation():
"""
Create and return GISAXS simulation with beam and detector defined
"""
simulation = ba.GISASSimulation()
simulation.setDetectorParameters(100, -1.0*deg, 1.0*deg,
100, 0.0*deg, 2.0*deg)
simulation.setBeamParameters(1.0*angstrom, 0.2*deg, 0.0*deg)
return simulation

def create_real_data():
"""
Generating "real" data by adding noise to the simulated data.
"""
sample = get_sample(5.0*nm, 10.0*nm)

simulation = get_simulation()
simulation.setSample(sample)

simulation.runSimulation()
real_data = simulation.getIntensityData()

# spoiling simulated data with the noise to produce "real" data
noise_factor = 0.5
for i in range(0, real_data.getTotalNumberOfBins()):
amplitude = real_data.getBinContent(i)
sigma = noise_factor*math.sqrt(amplitude)
noisy_amplitude = random.gauss(amplitude, sigma)
if noisy_amplitude<1.0:
noisy_amplitude = 1.0
real_data.setBinContent(i, noisy_amplitude)
return real_data

"""
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).

In the code below we put masks in such way that simulated image will look like
a Pac-Man from ancient arcade game.
"""

ba.Ellipse(0.0*deg, 1.0*deg, 0.5*deg, 0.5*deg), False)

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

# set mask for pacman's mouth
points = [[0.0*deg, 1.0*deg], [0.5*deg, 1.2*deg],
[0.5*deg, 0.8*deg], [0.0*deg, 1.0*deg]]

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

# other mask's shapes are possible too
# # rotated ellipse:
#                    1.0*deg, 0.5*deg, 45.0*deg), True)

def run_fitting():
"""
main function to run fitting
"""
simulation = get_simulation()
sample = get_sample()
simulation.setSample(sample)

# the core method of this example which adds masks to the simulation

real_data = create_real_data()

fit_suite = ba.FitSuite()
fit_suite.initPrint(10)
draw_observer = ba.DefaultFitObserver(draw_every_nth=10)
fit_suite.attachObserver(draw_observer)

# setting fitting parameters with starting values

# running fit
fit_suite.runFit()

print("Fitting completed.")
fit_suite.printResults()
print("chi2:", fit_suite.getChi2())
print("chi2:", fit_suite.getChi2())
for fitPar in fit_suite.fitParameters():
print(fitPar.name(), fitPar.value(), fitPar.error())

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

```
Tags: