Commit e469e112 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

mv faking to devtools

parent a6c3ff8c
#!/usr/bin/env python3
"""
Fake GISAS data according to sample model from gisas_fit2d, with added noise.
"""
import gisas_model1 as model
import numpy as np
def fake_data():
"""
Generate fake "experimental" data, and save them as numpy array.
"""
params, bg = model.model_parameters_and_bg()
# Compute model distribution
simulation = model.get_simulation(params)
simulation.runSimulation()
theory = simulation.result().array()
# Draw noisy data
np.random.seed(0)
data = np.random.normal(theory, np.sqrt(theory)) + np.random.poisson(
bg, theory.shape)
# Save to numpy
np.savetxt("gisas-model1.txt.gz", data)
if __name__ == '__main__':
fake_data()
...@@ -12,14 +12,13 @@ from matplotlib import pyplot as plt ...@@ -12,14 +12,13 @@ from matplotlib import pyplot as plt
def run_fitting(): def run_fitting():
real_data = np.loadtxt("gisas-model1.txt.gz", dtype=float) real_data = np.loadtxt("../data/faked-gisas1.txt.gz", dtype=float)
fit_objective = ba.FitObjective() fit_objective = ba.FitObjective()
fit_objective.addSimulationAndData(model.get_simulation, real_data) fit_objective.addSimulationAndData(model.get_simulation, real_data)
fit_objective.initPrint(10) # Print on every 10th iteration. fit_objective.initPrint(10) # Print on every 10th iteration.
fit_objective.initPlot( fit_objective.initPlot(10) # Plot on every 10th iteration. Slow!
10) # Plot on every 10th iteration. Will slow down the fit.
minimizer = ba.Minimizer() minimizer = ba.Minimizer()
params = model.start_parameters_1() params = model.start_parameters_1()
......
...@@ -15,20 +15,19 @@ def get_sample(params): ...@@ -15,20 +15,19 @@ def get_sample(params):
cylinder_height = params["cylinder_height"] cylinder_height = params["cylinder_height"]
cylinder_radius = params["cylinder_radius"] cylinder_radius = params["cylinder_radius"]
# collection of particles
ff = ba.FormFactorCylinder(cylinder_radius, cylinder_height) ff = ba.FormFactorCylinder(cylinder_radius, cylinder_height)
cylinder = ba.Particle(mat_particle, ff) cylinder = ba.Particle(mat_particle, ff)
layout = ba.ParticleLayout() layout = ba.ParticleLayout()
layout.addParticle(cylinder) layout.addParticle(cylinder)
# vacuum layer with particles and substrate form multi layer layer_1 = ba.Layer(mat_vacuum)
vacuum_layer = ba.Layer(mat_vacuum) layer_1.addLayout(layout)
vacuum_layer.addLayout(layout) layer_2 = ba.Layer(mat_substrate)
substrate_layer = ba.Layer(mat_substrate, 0)
multi_layer = ba.MultiLayer() sample = ba.MultiLayer()
multi_layer.addLayer(vacuum_layer) sample.addLayer(layer_1)
multi_layer.addLayer(substrate_layer) sample.addLayer(layer_2)
return multi_layer return sample
def get_simulation(params): def get_simulation(params):
......
#!/usr/bin/env python3
"""
Fake GISAS data according to sample model from gisas_fit2d, with added noise.
"""
import bornagain as ba
from bornagain import deg, nm
import numpy as np
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 model_parameters():
return {
'lg(intensity)': 6,
'lg(background)': 0.5,
'cylinder_height': 4*nm,
'cylinder_radius': 5*nm,
}
def fake_data():
"""
Generate fake "experimental" data, and save them as numpy array.
"""
params = model_parameters()
# Compute model distribution
simulation = get_simulation(params)
simulation.runSimulation()
theory = simulation.result().array()
# Draw noisy data
np.random.seed(0)
data = np.random.poisson(theory)
# Save to numpy
np.savetxt("faked-gisas1.txt.gz", data)
if __name__ == '__main__':
fake_data()
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