Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
64875f7a
Commit
64875f7a
authored
3 years ago
by
Wuttke, Joachim
Committed by
Wuttke, Joachim
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
import one example into another to avoid lengthy code duplication
parent
a5f34746
No related branches found
No related tags found
1 merge request
!366
rm some code duplication from examples
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Examples/fit55_SpecularIntro/FitWithUncertainties.py
+4
-89
4 additions, 89 deletions
Examples/fit55_SpecularIntro/FitWithUncertainties.py
with
4 additions
and
89 deletions
Examples/fit55_SpecularIntro/FitWithUncertainties.py
+
4
−
89
View file @
64875f7a
...
...
@@ -18,105 +18,20 @@ from matplotlib import pyplot as plt
import
bornagain
as
ba
from
bornagain
import
ba_fitmonitor
def
get_sample
(
params
):
"""
Creates a sample and returns it
:param params: a dictionary of optimization parameters
:return: the sample defined
"""
# substrate (Si)
si_sld_real
=
2.0704e-06
# \AA^{-2}
# layers' parameters
n_repetitions
=
10
# Ni
ni_sld_real
=
9.4245e-06
# \AA^{-2}
ni_thickness
=
70
*
ba
.
angstrom
# Ti
ti_sld_real
=
-
1.9493e-06
# \AA^{-2}
ti_thickness
=
params
[
"
ti_thickness
"
]
# defining materials
m_vacuum
=
ba
.
MaterialBySLD
()
m_ni
=
ba
.
MaterialBySLD
(
"
Ni
"
,
ni_sld_real
,
0
)
m_ti
=
ba
.
MaterialBySLD
(
"
Ti
"
,
ti_sld_real
,
0
)
m_substrate
=
ba
.
MaterialBySLD
(
"
SiSubstrate
"
,
si_sld_real
,
0
)
# vacuum layer and substrate form multi layer
vacuum_layer
=
ba
.
Layer
(
m_vacuum
)
ni_layer
=
ba
.
Layer
(
m_ni
,
ni_thickness
)
ti_layer
=
ba
.
Layer
(
m_ti
,
ti_thickness
)
substrate_layer
=
ba
.
Layer
(
m_substrate
)
multi_layer
=
ba
.
MultiLayer
()
multi_layer
.
addLayer
(
vacuum_layer
)
for
_
in
range
(
n_repetitions
):
multi_layer
.
addLayer
(
ti_layer
)
multi_layer
.
addLayer
(
ni_layer
)
multi_layer
.
addLayer
(
substrate_layer
)
return
multi_layer
def
get_real_data
(
filename
):
"""
Loading data from genx_interchanging_layers.dat
Returns a Nx2 array (N - the number of experimental data entries)
with first column being coordinates,
second one being values.
"""
real_data
=
np
.
loadtxt
(
filename
,
usecols
=
(
0
,
1
),
skiprows
=
3
)
# translating axis values from double incident angle (degs)
# to incident angle (radians)
real_data
[:,
0
]
*=
np
.
pi
/
360
global
expdata
expdata
=
real_data
.
copy
()
def
get_real_data_axis
():
"""
Get axis coordinates of the experimental data
:return: 1D array with axis coordinates
"""
return
expdata
[:,
0
]
def
get_real_data_values
():
"""
Get experimental data values as a 1D array
:return: 1D array with experimental data values
"""
return
expdata
[:,
1
]
def
get_simulation
(
params
):
"""
Create and return specular simulation with its instrument defined
"""
wavelength
=
1.54
*
ba
.
angstrom
# beam wavelength
simulation
=
ba
.
SpecularSimulation
()
scan
=
ba
.
AlphaScan
(
wavelength
,
get_real_data_axis
())
simulation
.
setScan
(
scan
)
simulation
.
setSample
(
get_sample
(
params
))
return
simulation
import
FitSpecularBasics
as
fsb
def
run_fitting
():
"""
Setup simulation and fit
"""
real_data
=
get_real_data_values
()
real_data
=
fsb
.
get_real_data_values
()
# setting artificial uncertainties (uncertainty sigma equals a half
# of experimental data value)
uncertainties
=
real_data
*
0.5
fit_objective
=
ba
.
FitObjective
()
fit_objective
.
addSimulationAndData
(
get_simulation
,
real_data
,
fit_objective
.
addSimulationAndData
(
fsb
.
get_simulation
,
real_data
,
uncertainties
)
plot_observer
=
ba_fitmonitor
.
PlotterSpecular
(
units
=
ba
.
Axes
.
RQ4
)
...
...
@@ -140,6 +55,6 @@ def run_fitting():
if
__name__
==
'
__main__
'
:
datadir
=
os
.
getenv
(
'
BORNAGAIN_EXAMPLE_DATA_DIR
'
,
''
)
data_fname
=
os
.
path
.
join
(
datadir
,
"
genx_interchanging_layers.dat.gz
"
)
get_real_data
(
data_fname
)
fsb
.
get_real_data
(
data_fname
)
run_fitting
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment