This example demonstrates fitting of 2D GISAS data.
Faked experimenta data
The sample and instrument model is basically the same as
in our basic GISAS simulation example,
except that it depends on four external parameters:
beam intensity, detector background, radius and height of the cylindrical nanoparticles.
The intensity for each detector pixel has been drawn at random
from a Poisson distribution, with rate basically given by the fit model
except for some imperfections that have been introduced on purpose
to make the subsequent fitting more difficult and more realistic:
Refractive indices changed, and absorption strongly enhanced;
Cylinders replaced by a mixture of cones and segmented spheroids;
"""
Parametric model of a GISAS simulation.
The idealized sample model consists of dilute cylinders on a substrate.
"""importbornagainasbafrombornagainimportdeg,nmmat_vacuum=ba.HomogeneousMaterial("Vacuum",0,0)mat_substrate=ba.HomogeneousMaterial("Substrate",6e-6,2e-8)mat_particle=ba.HomogeneousMaterial("Particle",6e-4,2e-8)defget_sample(params):cylinder_height=params["cylinder_height"]cylinder_radius=params["cylinder_radius"]ff=ba.FormFactorCylinder(cylinder_radius,cylinder_height)cylinder=ba.Particle(mat_particle,ff)layout=ba.ParticleLayout()layout.addParticle(cylinder)layer_1=ba.Layer(mat_vacuum)layer_1.addLayout(layout)layer_2=ba.Layer(mat_substrate)sample=ba.MultiLayer()sample.addLayer(layer_1)sample.addLayer(layer_2)returnsampledefget_simulation(params):beam=ba.Beam(10**params['lg(intensity)'],0.1*nm,ba.Direction(0.2*deg,0))det=ba.SphericalDetector(100,-1.5*deg,1.5*deg,100,0,3*deg)sample=get_sample(params)simulation=ba.GISASSimulation(beam,sample,det)simulation.setBackground(ba.ConstantBackground(10**params['lg(background)']))returnsimulationdefstart_parameters_1():params=ba.Parameters()params.add("lg(intensity)",5)params.add("lg(background)",1)params.add("cylinder_height",6.*nm,min=0.01)params.add("cylinder_radius",6.*nm,min=0.01)returnparams
gisas_model1.py
Explanation
The function get_sample is basically the same as
in our basic GISAS simulation example,
except that radius and height of the cylindrical disks are now
supplied as external parameters.
These parameters are passed through the function argument params
which can be either a Python dict,
or an instance of the BornAgain class Parameters
.
The function get_simulation has the same function argument params,
from which it takes beam intensity and background level.
These two parameters are on a logarithmic scale.
The function start_parameters_1 is supplied along with the model
in order to facilitate the benchmarking of different fith methods.
#!/usr/bin/env python3"""
Basic GISAS 2D fitting example.
The model is in gisas_model1.
Fake experimental data are generated by gisas_fake1.
"""importgisas_model1asmodelimportbornagainasbaimportnumpyasnpfrommatplotlibimportpyplotaspltdefrun_fitting():real_data=np.loadtxt("../data/faked-gisas1.txt.gz",dtype=float)fit_objective=ba.FitObjective()fit_objective.addSimulationAndData(model.get_simulation,real_data)fit_objective.initPrint(10)# Print on every 10th iteration.fit_objective.initPlot(10)# Plot on every 10th iteration. Slow!minimizer=ba.Minimizer()params=model.start_parameters_1()result=minimizer.minimize(fit_objective.evaluate,params)fit_objective.finalize(result)print("Fit completed.")print("chi2:",result.minValue())forfitParinresult.parameters():print(fitPar.name(),fitPar.value,fitPar.error)# Save simulation image corresponding to the best fit parametersnp.savetxt("fit.txt",fit_objective.simulationResult().array())if__name__=='__main__':run_fitting()plt.show()
fit_gisas.py
Explanation
This script loads the (faked) experimental data,
and performs a fit.
The FitObjective created here is used to put into correspondence the real data,
represented by a numpy array, and the simulation, represented by the get_simulation callable.
On every fit iteration FitObjective
will generate a new simulation object
for a given set of fit parameter values using the get_simulation callable,
run it to obtain simulated detector intensities,
calculate $\chi^2$ between simulated and real data.
The method fit_objective.evaluate provided by the FitObjective class interface is used here as an objective function.
The method accepts fit parameters and returns the $\chi^2$ value,
calculated between experimental and simulated images for the given values of the fit parameters.
The method is passed to the minimizer together with the initial fit parameter values.
The minimizer.minimize starts a fit that will
continue further without user intervention until the minimum
is found or the minimizer failed to converge.
The rest of the code demonstrates how to access the fit results.