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 = GISASSimulation() simulation.addMask(Rectangle(x1, y1, x2, y2), mask_value)
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_simulationfunction 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.
""" Fitting example: fit with masks """ 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 def get_sample(radius=5*nm, height=10*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_ff = ba.FormFactorCylinder(radius, height) cylinder = ba.Particle(m_particle, cylinder_ff) particle_layout = ba.ParticleLayout() particle_layout.addParticle(cylinder) air_layer = ba.Layer(m_air) air_layer.addLayout(particle_layout) substrate_layer = ba.Layer(m_substrate, 0) multi_layer = ba.MultiLayer() multi_layer.addLayer(air_layer) multi_layer.addLayer(substrate_layer) 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