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()
|