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 $\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.
• 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.
  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  """ Fitting example: fit with masks """ import numpy as np from matplotlib import pyplot as plt import bornagain as ba from bornagain import deg, angstrom, nm def get_sample(params): """ Build the sample representing cylinders on top of substrate without interference. """ radius = params["radius"] height = params["height"] m_vacuum = ba.HomogeneousMaterial("Vacuum", 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) layout = ba.ParticleLayout() layout.addParticle(cylinder) vacuum_layer = ba.Layer(m_vacuum) vacuum_layer.addLayout(layout) substrate_layer = ba.Layer(m_substrate, 0) multi_layer = ba.MultiLayer() multi_layer.addLayer(vacuum_layer) multi_layer.addLayer(substrate_layer) return multi_layer def get_simulation(params, add_masks=True): """ 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) simulation.beam().setIntensity(1e+08) simulation.setSample(get_sample(params)) if add_masks: add_mask_to_simulation(simulation) return simulation def create_real_data(): """ Generating "real" data by adding noise to the simulated data. """ params = {'radius': 5.0*nm, 'height': 10.0*nm} # retrieving simulated data in the form of numpy array simulation = get_simulation(params, add_masks=False) simulation.runSimulation() real_data = simulation.result().array() # spoiling simulated data with the noise to produce "real" data noise_factor = 0.1 noisy = np.random.normal(real_data, noise_factor*np.sqrt(real_data)) noisy[noisy < 0.1] = 0.1 return noisy 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.0*deg, 1.0*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.0*deg, 1.0*deg], [0.5*deg, 1.2*deg], [0.5*deg, 0.8*deg], [0.0*deg, 1.0*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) def run_fitting(): """ main function to run fitting """ real_data = create_real_data() fit_objective = ba.FitObjective() fit_objective.addSimulationAndData(get_simulation, real_data, 1.0) fit_objective.initPrint(10) fit_objective.initPlot(10) params = ba.Parameters() params.add("radius", 6.*nm, min=4.0, max=8.0) params.add("height", 9.*nm, min=8.0, max=12.0) minimizer = ba.Minimizer() result = minimizer.minimize(fit_objective.evaluate, params) fit_objective.finalize(result) if __name__ == '__main__': run_fitting() plt.show()