Skip to content
Snippets Groups Projects
Commit 64c3df7c authored by Mikhail Svechnikov's avatar Mikhail Svechnikov
Browse files

[from21] From21 ()

Merging branch 'from21'  into 'main'.

See merge request !1891
parents 68363fba 08b14521
No related branches found
No related tags found
1 merge request!1891From21
Pipeline #109593 passed
Showing
with 38 additions and 35 deletions
...@@ -7,7 +7,7 @@ weight = 40 ...@@ -7,7 +7,7 @@ weight = 40
Bayesian sampling of reflectometry models is a common tool in the analysis of specular reflectometry data. Bayesian sampling of reflectometry models is a common tool in the analysis of specular reflectometry data.
The Python programming language has a powerful infrastructure for this modelling, including packages such as [PyMC3](https://docs.pymc.io) and [PyStan](https://pystan.readthedocs.io/en/latest/). The Python programming language has a powerful infrastructure for this modelling, including packages such as [PyMC3](https://docs.pymc.io) and [PyStan](https://pystan.readthedocs.io/en/latest/).
Here, we will show how the [emcee](https://emcee.readthedocs.io/en/stable/) Python may be used to enable Bayesian sampling in BornAgain and the differential evoulation optimisation algorithm from the [scipy](https://www.scipy.org/scipylib/index.html). Here, we will show how the [emcee](https://emcee.readthedocs.io/en/stable/) Python may be used to enable Bayesian sampling in BornAgain and the differential evoulation optimisation algorithm from the [scipy](https://docs.scipy.org/doc/scipy/).
### Example script ### Example script
...@@ -16,7 +16,9 @@ To generate these images of the probability distributions of the parameters and ...@@ -16,7 +16,9 @@ To generate these images of the probability distributions of the parameters and
{{< figscg src="/files/fitted/CornerPlotBayes.png" width="500" class="center">}} {{< figscg src="/files/fitted/CornerPlotBayes.png" width="500" class="center">}}
{{< figscg src="/files/fitted/ReflectivityBayes.png" width="500" class="center">}} {{< figscg src="/files/fitted/ReflectivityBayes.png" width="500" class="center">}}
run [this script]({{% ref-ex "bayesian/likelihood_sampling.py" %}}). run this script
{{< show-ex file="bayesian/likelihood_sampling.py" >}}
### Explanation ### Explanation
...@@ -27,7 +29,7 @@ We know the scattering length densities for each, and that in total there are 10 ...@@ -27,7 +29,7 @@ We know the scattering length densities for each, and that in total there are 10
This sample is created in the `get_sample` function. This sample is created in the `get_sample` function.
Having built the sample, it is necessary to obtain the real experimental data. Having built the sample, it is necessary to obtain the real experimental data.
For the above code to work locally, the following [data file]({{% ref-ex "data/genx_interchanging_layers.dat.gz?inline=false" %}}) is required and the Python script (in particular the `get_real_data` function) should be adapted appropriately. For the above code to work locally, the data file {{% ref-data genx_alternating_layers.dat.gz %}} is required and the Python script (in particular the `get_real_data` function) should be adapted appropriately.
This function defined an uncertainty in the reflectivity of 10 %. This function defined an uncertainty in the reflectivity of 10 %.
The simulation is then defined in the `get_simulation` function, which is passed a series of angles, however, this may be modified to perform a Q-scan as necessary. The simulation is then defined in the `get_simulation` function, which is passed a series of angles, however, this may be modified to perform a Q-scan as necessary.
......
...@@ -5,7 +5,7 @@ weight = 40 ...@@ -5,7 +5,7 @@ weight = 40
## External Minimizers: Plotting Fit Progress ## External Minimizers: Plotting Fit Progress
In this example we are demonstrating how to run a typical fitting task in BornAgain using a third party minimizer while plotting the results. As in our [previous example](/ex/fit/extended/external-minimizer), we use lmfit for sake of illustration. In this example we are demonstrating how to run a typical fitting task in BornAgain using a third party minimizer while plotting the results. As in our [previous example](/ex/fit/external-minimizer), we use lmfit for sake of illustration.
To plot the fit progress, it is needed to use the lmfit iteration callback function. It will come handy to define the plotting callback function as a specialized class: To plot the fit progress, it is needed to use the lmfit iteration callback function. It will come handy to define the plotting callback function as a specialized class:
......
...@@ -10,7 +10,7 @@ In this example we are demonstrating how to allow for uncertainties during a Ref ...@@ -10,7 +10,7 @@ In this example we are demonstrating how to allow for uncertainties during a Ref
The reference data was generated with GENX, setting the thickness of the Ti layers equal to 3 nm. The reference data was generated with GENX, setting the thickness of the Ti layers equal to 3 nm.
This example follows closely the tutorial on [Fitting reflectometry data](/ex/fit/extended/fit-specular-data/). The main points to focus on here are the following: The main points to focus on here are the following:
- Added artificial uncertainties to the data being fitted - Added artificial uncertainties to the data being fitted
- Use of the the $RQ^4$ view for plotting - Use of the the $RQ^4$ view for plotting
......
...@@ -5,7 +5,7 @@ weight = 30 ...@@ -5,7 +5,7 @@ weight = 30
## Experiment at GALAXI ## Experiment at GALAXI
This is an example of a real data fit. We use our own measurements performed at the laboratory diffractometer [GALAXI](http://www.fz-juelich.de/jcns/jcns-2//DE/Leistungen/GALAXI/_node.html) in Forschungszentrum Jülich. This is an example of a real data fit. We use our own measurements performed at the laboratory diffractometer [GALAXI](https://www.fz-juelich.de/en/jcns/jcns-2/expertise/in-house-x-ray/galaxi) in Forschungszentrum Jülich.
{{< galleryscg >}} {{< galleryscg >}}
{{< figscg src="/img/draw/FitGALAXIData_setup.jpg" width="350px" caption="Real-space model">}} {{< figscg src="/img/draw/FitGALAXIData_setup.jpg" width="350px" caption="Real-space model">}}
...@@ -20,8 +20,6 @@ This is an example of a real data fit. We use our own measurements performed at ...@@ -20,8 +20,6 @@ This is an example of a real data fit. We use our own measurements performed at
* The real data is loaded from a tiff file into a histogram representing the detector's channels. * The real data is loaded from a tiff file into a histogram representing the detector's channels.
* The `run_fitting()` function contains the initialization of the fitting kernel: loading experimental data, assignment of fit pair, fit parameters selection (line 62). * The `run_fitting()` function contains the initialization of the fitting kernel: loading experimental data, assignment of fit pair, fit parameters selection (line 62).
{{< show-ex file="fit/scatter2d/expfit_galaxi.py" >}}
## Experiment description ## Experiment description
To successfully simulate and fit results of some real experiment it is important to have To successfully simulate and fit results of some real experiment it is important to have
...@@ -32,10 +30,7 @@ To successfully simulate and fit results of some real experiment it is important ...@@ -32,10 +30,7 @@ To successfully simulate and fit results of some real experiment it is important
### Experiment ### Experiment
As an example we will use our own measurements performed at the laboratory diffractometer [GALAXI](http://www.fz-juelich.de/jcns/jcns-2//DE/Leistungen/GALAXI/_node.html) in Forschungszentrum Jülich. As an example we will use our own measurements performed at the laboratory diffractometer [GALAXI](https://www.fz-juelich.de/en/jcns/jcns-2/expertise/in-house-x-ray/galaxi) in Forschungszentrum Jülich.
A complete example, containing less explanations but more code, can be found in
[Real life fit example: experiment at GALAXI](/ex/fit/extended/experiment-at-galaxi).
Our sample represents a 3-layer system (substrate, teflon and air) Our sample represents a 3-layer system (substrate, teflon and air)
with Ag nanoparticles sitting on top of the teflon layer. with Ag nanoparticles sitting on top of the teflon layer.
...@@ -125,3 +120,7 @@ data = img.data.astype("float64") ...@@ -125,3 +120,7 @@ data = img.data.astype("float64")
``` ```
The main requirement is that the shape of the numpy array coincides with the number of detector channels (i.e. `npx, npy = 981, 1043` for given example). The main requirement is that the shape of the numpy array coincides with the number of detector channels (i.e. `npx, npy = 981, 1043` for given example).
{{< show-ex file="fit/scatter2d/expfit_galaxi.py" >}}
Data to be fitted: {{% ref-data galaxi_data.tif.gz %}}
...@@ -26,8 +26,7 @@ to make the subsequent fitting more difficult and more realistic: ...@@ -26,8 +26,7 @@ to make the subsequent fitting more difficult and more realistic:
* Beam wavelength and alpha_i slightly changed; * Beam wavelength and alpha_i slightly changed;
* Detector x-axis skewed. * Detector x-axis skewed.
The faked data can be found at The faked data: {{% ref-data faked-gisas1.txt.gz %}}
{{% ref-ex "data/faked-gisas1.txt.gz" %}}.
## Fit script ## Fit script
......
...@@ -22,6 +22,6 @@ print(ba.MinimizerFactory().catalogueDetailsToString()) ...@@ -22,6 +22,6 @@ print(ba.MinimizerFactory().catalogueDetailsToString())
For more information, see the For more information, see the
[minimizer settings tutorial](/ex/fit/minimizers/index.md). [minimizer settings tutorial](/ref/fit/minimizers).
{{< show-ex file="fit/scatter2d/minimizer_settings.py" >}} {{< show-ex file="fit/scatter2d/minimizer_settings.py" >}}
...@@ -41,7 +41,7 @@ If no uncertainties are available, using the relative difference `fit_objective. ...@@ -41,7 +41,7 @@ If no uncertainties are available, using the relative difference `fit_objective.
If the relative difference is selected and uncertainties are provided, BornAgain automatically falls back to the above $\chi^2$ metric. If the relative difference is selected and uncertainties are provided, BornAgain automatically falls back to the above $\chi^2$ metric.
The fitting of polarized reflectometry data proceeds similar to the lines presented in The fitting of polarized reflectometry data proceeds similar to the lines presented in
[the tutorial on multiple datasets](/ex/fit/advanced/multiple-datasets). [the tutorial on multiple datasets](/ex/fit/multiple-datasets).
We need to add the reflectivity curves for the up-up and down-down channel We need to add the reflectivity curves for the up-up and down-down channel
to the fit objective: to the fit objective:
...@@ -98,3 +98,5 @@ python3 PolarizedSpinAsymmetryFit.py ...@@ -98,3 +98,5 @@ python3 PolarizedSpinAsymmetryFit.py
Here is the complete example: Here is the complete example:
{{< show-ex file="fit/specular/PolarizedSpinAsymmetryFit.py" >}} {{< show-ex file="fit/specular/PolarizedSpinAsymmetryFit.py" >}}
Data to be fitted: {{% ref-data MAFO_Saturated_mm.tab %}}, {{% ref-data MAFO_Saturated_pp.tab %}}
...@@ -33,7 +33,7 @@ magnetizations at 150K and 300K: $M_{s150} = M_{150K} / M_{300K}$. ...@@ -33,7 +33,7 @@ magnetizations at 150K and 300K: $M_{s150} = M_{150K} / M_{300K}$.
#### Magnetization model #### Magnetization model
To model a magnetic material, one can assign a magnetization vector to any material, as is demonstrated To model a magnetic material, one can assign a magnetization vector to any material, as is demonstrated
in the [basic polarized reflectometry tutorial](/ref/instr/pol/basic-polarized-reflectometry). in the [magnetic material tutorial](/ref/sample/material/magnetization).
When a non-vanishing magnetization vector is specified for at least one layer in a sample, When a non-vanishing magnetization vector is specified for at least one layer in a sample,
BornAgain will automatically utilize the polarized computational engine. BornAgain will automatically utilize the polarized computational engine.
This leads to lower performance as the computations are more invovled. This leads to lower performance as the computations are more invovled.
...@@ -148,4 +148,4 @@ As can be seen from the plot of the SLDs, the magnetization is indeed larger for ...@@ -148,4 +148,4 @@ As can be seen from the plot of the SLDs, the magnetization is indeed larger for
{{< show-ex file="fit/specular/Honeycomb_fit.py" >}} {{< show-ex file="fit/specular/Honeycomb_fit.py" >}}
Reference data: {{% ref-ex "data/honeycomb" %}} Data to be fitted: {{% ref-data honeycomb150m.dat %}}, {{% ref-data honeycomb150p.dat %}}, {{% ref-data honeycomb300m.dat %}}, {{% ref-data honeycomb300p.dat %}}
...@@ -97,7 +97,6 @@ python3 Pt_layer_fit.py ...@@ -97,7 +97,6 @@ python3 Pt_layer_fit.py
``` ```
a simulation is performed with our fit results and one should obtain the result shown above. a simulation is performed with our fit results and one should obtain the result shown above.
{{< show-ex file="fit/specular/Pt_layer_fit.py" >}} {{< show-ex file="fit/specular/Pt_layer_fit.py" >}}
Data to be fitted: {{% ref-ex "data/RvsQ_36563_36662.txt.gz" %}} Data to be fitted: {{% ref-data RvsQ_36563_36662.txt.gz %}}
...@@ -82,6 +82,3 @@ hist.save("result.int.gz") ...@@ -82,6 +82,3 @@ hist.save("result.int.gz")
Additional information can be found in the following pages: Additional information can be found in the following pages:
* [Plotting with axes in different units](/ex/result/export/axes-in-different-units) * [Plotting with axes in different units](/ex/result/export/axes-in-different-units)
* [SimulationResult C++ class reference](http://apps.jcns.fz-juelich.de/doxy/BornAgain/classSimulationResult.html)
* [Histogram1D C++ class reference](http://apps.jcns.fz-juelich.de/doxy/BornAgain/classHistogram1D.html)
* [Histogram2D C++ class reference](http://apps.jcns.fz-juelich.de/doxy/BornAgain/classHistogram2D.html)
...@@ -6,7 +6,7 @@ weight = 20 ...@@ -6,7 +6,7 @@ weight = 20
### Plotting with axes in different units ### Plotting with axes in different units
In this example we demonstrate how to plot intensity data with detector axes expressed in different units. It serves as a supporting example to the In this example we demonstrate how to plot intensity data with detector axes expressed in different units. It serves as a supporting example to the
[Accessing simulation results](/py/export/_index.md) tutorial. [Accessing simulation results](/ex/result/export/more) tutorial.
* The standard [Cylinders in DWBA](/ex/sim/gisas) * The standard [Cylinders in DWBA](/ex/sim/gisas)
sample is used to setup the simulation. sample is used to setup the simulation.
......
...@@ -8,8 +8,8 @@ weight = 15 ...@@ -8,8 +8,8 @@ weight = 15
This example demonstrates how to apply different roughness models This example demonstrates how to apply different roughness models
in a specular reflectivity calculation. The considered sample is in a specular reflectivity calculation. The considered sample is
exactly the same as the one described in the exactly the same as the one described in the
[reflectometry tutorial](/ref/sim/class/specular/_index.md), [reflectometry tutorial](/ref/sim/class/specular),
and the [basic roughness tutorial](/ref/sample/roughness/specular). and the [basic roughness tutorial](/ref/sample/roughness).
Hewever, now the computation is performed twice with the standard $tanh$ interface profile Hewever, now the computation is performed twice with the standard $tanh$ interface profile
and the Névot-Croce roughness model that arises from a Gaussian distribution of the and the Névot-Croce roughness model that arises from a Gaussian distribution of the
deviation from the mean-surface position. deviation from the mean-surface position.
......
...@@ -8,7 +8,7 @@ weight = 10 ...@@ -8,7 +8,7 @@ weight = 10
This example demonstrates how to compute reflected signal from This example demonstrates how to compute reflected signal from
a multilayered sample with surface roughness. All the experiment a multilayered sample with surface roughness. All the experiment
layout is exactly the same as the one described in layout is exactly the same as the one described in
[reflectometry tutorial](/ref/sim/class/specular/_index.md), [reflectometry tutorial](/ref/sim/class/specular),
but now all the layers (except the ambient media) have roughness on the top surface. The but now all the layers (except the ambient media) have roughness on the top surface. The
roughness is characterized by root-mean-square deviation from the mean surface position roughness is characterized by root-mean-square deviation from the mean surface position
$\sigma = 1$ nm. $\sigma = 1$ nm.
...@@ -16,7 +16,7 @@ $\sigma = 1$ nm. ...@@ -16,7 +16,7 @@ $\sigma = 1$ nm.
{{<figscg src="/img/auto/specular/SpecularSimulationWithRoughness.png" width="350px">}} {{<figscg src="/img/auto/specular/SpecularSimulationWithRoughness.png" width="350px">}}
When comparing the result of the simulation to the result obtained in the When comparing the result of the simulation to the result obtained in the
[reflectometry tutorial](/ref/sim/class/specular/_index.md), [reflectometry tutorial](/ref/sim/class/specular),
one can notice up to two orders of magnitude attenuation of the reflected signal due to one can notice up to two orders of magnitude attenuation of the reflected signal due to
the roughness of the sample. the roughness of the sample.
......
...@@ -10,8 +10,8 @@ by a sample that consists of 10 Ti/Ni double layers on a Si substrate. ...@@ -10,8 +10,8 @@ by a sample that consists of 10 Ti/Ni double layers on a Si substrate.
##### Full script ##### Full script
The same script has been used in the tutorial to explain [usage](/py/run.md) The same script has been used in the tutorial to explain [usage](/py/run)
and [syntax](/py/syntax.md) of BornAgain Python code. and [syntax](/py/syntax) of BornAgain Python code.
Root class reference: [SpecularSimulation](/ref/sim/class/specular). Root class reference: [SpecularSimulation](/ref/sim/class/specular).
......
...@@ -14,9 +14,13 @@ some point (e.g. Debian 12) disabled Pip, which terminates with ...@@ -14,9 +14,13 @@ some point (e.g. Debian 12) disabled Pip, which terminates with
`error: externally-managed-environment`. While this behavior can be `error: externally-managed-environment`. While this behavior can be
overridden by a special flag, we advise against. overridden by a special flag, we advise against.
#### Python in pyenv
Rather, we recommend escaping from Python version hell by using the Rather, we recommend escaping from Python version hell by using the
Python version manager [pyenv](https://github.com/pyenv/pyenv). Python version manager [pyenv](https://github.com/pyenv/pyenv).
Install the venerable GUI toolkit Tk (e.g. Debian package tk-dev).
Prepare the shell by adding Prepare the shell by adding
``` ```
export PYENV_ROOT="$HOME/.pyenv" export PYENV_ROOT="$HOME/.pyenv"
...@@ -40,4 +44,4 @@ which python # shows path in virtual environment ...@@ -40,4 +44,4 @@ which python # shows path in virtual environment
pip install numpy pip install numpy
``` ```
etc. etc.
For the full list of modules required by BornAgain, see the [modules](modules) page. For the full list of modules required by BornAgain, see the [modules](/installation/py/modules/) page.
...@@ -19,6 +19,7 @@ Furthermore, a small number of fit script examples require the Python modules ...@@ -19,6 +19,7 @@ Furthermore, a small number of fit script examples require the Python modules
* lmfit * lmfit
* scipy * scipy
* tqdm * tqdm
These are not required for _installing_ and _running_ most of BornAgain. These are not required for _installing_ and _running_ most of BornAgain.
However, they should be present when _building_ BornAgain However, they should be present when _building_ BornAgain
lest some tests will fail. lest some tests will fail.
...@@ -190,4 +190,4 @@ BornAgain fitting can also be done using other minimization packages. A short li ...@@ -190,4 +190,4 @@ BornAgain fitting can also be done using other minimization packages. A short li
+ [lmfit](https://lmfit.github.io/lmfit-py/) + [lmfit](https://lmfit.github.io/lmfit-py/)
+ [bumps](https://bumps.readthedocs.io/en/latest/) + [bumps](https://bumps.readthedocs.io/en/latest/)
In [this example](/ex/fit/extended/external-minimizer) we demonstrate how to use the `lmfit` minimizer for a typical fit of GISAS data. In [this example](/ex/fit/external-minimizer) we demonstrate how to use the `lmfit` minimizer for a typical fit of GISAS data.
...@@ -24,7 +24,7 @@ After creation of a beam, special properties can be set with ...@@ -24,7 +24,7 @@ After creation of a beam, special properties can be set with
beam.setFootprintFactor(footprint) beam.setFootprintFactor(footprint)
beam.setPolarization(polarization) beam.setPolarization(polarization)
``` ```
For the arguments, see [footprint](footprint) and [polarization](../polarized). For the arguments, see [footprint](footprint) and [polarization](../pol).
{{% children %}} {{% children %}}
...@@ -8,7 +8,7 @@ weight = 60 ...@@ -8,7 +8,7 @@ weight = 60
This example demonstrates beam angular spread effects in reflectivity computations. This example demonstrates beam angular spread effects in reflectivity computations.
It also offers a comparison with data generated using another well known code: GenX. It also offers a comparison with data generated using another well known code: GenX.
Further information about reflectometry simulations can be found in the Further information about reflectometry simulations can be found in the
[Reflectometry Simulation Tutorial](/ref/sim/class/specular/_index.md). [Reflectometry Simulation Tutorial](/ref/sim/class/specular).
The observed reflectometry signal can be affected either by a spread in the beam wavelength or in the incident angle. The observed reflectometry signal can be affected either by a spread in the beam wavelength or in the incident angle.
......
...@@ -47,7 +47,7 @@ The incident angle range was made rather small in this example ...@@ -47,7 +47,7 @@ The incident angle range was made rather small in this example
(from $0.0$ to $0.6$ degrees) in order to emphasize (from $0.0$ to $0.6$ degrees) in order to emphasize
the footprint impact at small incident angles. the footprint impact at small incident angles.
In other respects this example exactly matches the In other respects this example exactly matches the
[reflectometry simulation tutorial](/ref/sim/class/specular/_index.md). [reflectometry simulation tutorial](/ref/sim/class/specular).
{{< galleryscg >}} {{< galleryscg >}}
{{< figscg src="/img/auto/specular/FootprintCorrection.png" width="450px">}} {{< figscg src="/img/auto/specular/FootprintCorrection.png" width="450px">}}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment