Commit 7cffc9b2 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

Merge branch 'ex2' into 'develop'

Examples; round parameters, etc

See merge request !10
parents 6a6a3f87 8cb37c04
Pipeline #33418 passed with stage
in 19 minutes and 17 seconds
#!/usr/bin/env python3
"""
Basic GISAS 2D fitting example.
The model is in gisas_model1.
Fake experimental data are generated by gisas_fake1.
"""
import gisas_model1 as model
import bornagain as ba
import numpy as np
from matplotlib import pyplot as plt
def run_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())
for fitPar in result.parameters():
print(fitPar.name(), fitPar.value, fitPar.error)
# Save simulation image corresponding to the best fit parameters
np.savetxt("fit.txt", fit_objective.simulationResult().array())
if __name__ == '__main__':
run_fitting()
plt.show()
"""
Parametric model of a GISAS simulation.
The idealized sample model consists of dilute cylinders on a substrate.
"""
import bornagain as ba
from bornagain import deg, nm
mat_vacuum = ba.HomogeneousMaterial("Vacuum", 0, 0)
mat_substrate = ba.HomogeneousMaterial("Substrate", 6e-6, 2e-8)
mat_particle = ba.HomogeneousMaterial("Particle", 6e-4, 2e-8)
def get_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)
return sample
def get_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)']))
return simulation
def start_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)
return params
#!/usr/bin/env python3
"""
Fitting example: 4 parameters fit for mixture of cylinders and prisms on top
of substrate.
"""
import bornagain as ba
from bornagain import deg, angstrom, nm
import numpy as np
from matplotlib import pyplot as plt
def get_sample(params):
"""
Returns a sample with uncorrelated cylinders and prisms on a substrate.
"""
cylinder_height = params["cylinder_height"]
cylinder_radius = params["cylinder_radius"]
prism_height = params["prism_height"]
prism_base_edge = params["prism_base_edge"]
# defining materials
m_vacuum = ba.HomogeneousMaterial("Vacuum", 0, 0)
m_substrate = ba.HomogeneousMaterial("Substrate", 6e-6, 2e-8)
m_particle = ba.HomogeneousMaterial("Particle", 6e-4, 2e-8)
# collection of particles
cylinder_ff = ba.FormFactorCylinder(cylinder_radius, cylinder_height)
cylinder = ba.Particle(m_particle, cylinder_ff)
prism_ff = ba.FormFactorPrism3(prism_base_edge, prism_height)
prism = ba.Particle(m_particle, prism_ff)
layout = ba.ParticleLayout()
layout.addParticle(cylinder, 0.5)
layout.addParticle(prism, 0.5)
# vacuum layer with particles and substrate form multi layer
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):
"""
Returns a GISAXS simulation with beam and detector defined
"""
simulation = ba.GISASSimulation()
simulation.setDetectorParameters(100, -1*deg, 1*deg, 100, 0, 2*deg)
simulation.setBeamParameters(1*angstrom, 0.2*deg, 0)
simulation.beam().setIntensity(1e+08)
simulation.setSample(get_sample(params))
return simulation
def create_real_data():
"""
Generating "experimental" data by running simulation with certain parameters.
The data is saved on disk in the form of numpy array.
"""
# default sample parameters
params = {
'cylinder_height': 5*nm,
'cylinder_radius': 5*nm,
'prism_height': 5*nm,
'prism_base_edge': 5*nm
}
# retrieving simulated data in the form of numpy array
simulation = get_simulation(params)
simulation.runSimulation()
real_data = simulation.result().array()
# spoiling simulated data with noise to produce "real" data
np.random.seed(0)
noise_factor = 0.1
noisy = np.random.normal(real_data, noise_factor*np.sqrt(real_data))
noisy[noisy < 0.1] = 0.1
np.savetxt("basic_fitting_tutorial_data.txt.gz", real_data)
def load_real_data():
"""
Loads experimental data from file
"""
return np.loadtxt("basic_fitting_tutorial_data.txt.gz", dtype=float)
def run_fitting():
"""
Setup simulation and fit
"""
real_data = load_real_data()
fit_objective = ba.FitObjective()
fit_objective.addSimulationAndData(get_simulation, real_data)
# Print fit progress on every n-th iteration.
fit_objective.initPrint(10)
# Plot fit progress on every n-th iteration. Will slow down fit.
fit_objective.initPlot(10)
params = ba.Parameters()
params.add("cylinder_height", 4.*nm, min=0.01)
params.add("cylinder_radius", 6.*nm, min=0.01)
params.add("prism_height", 4.*nm, min=0.01)
params.add("prism_base_edge", 6.*nm, min=0.01)
minimizer = ba.Minimizer()
result = minimizer.minimize(fit_objective.evaluate, params)
fit_objective.finalize(result)
print("Fitting completed.")
print("chi2:", result.minValue())
for fitPar in result.parameters():
print(fitPar.name(), fitPar.value, fitPar.error)
# saving simulation image corresponding to the best fit parameters
# np.savetxt("data.txt", fit_objective.simulationResult().array())
if __name__ == '__main__':
# uncomment line below to regenerate "experimental" data file
# create_real_data()
run_fitting()
plt.show()
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment