# Python scripting on BornAgain

file:///usr/share/doc/bornagain/html/py/index.html Recent content in Python scripting on BornAgain Hugo en-us

# Run a script

file:///usr/share/doc/bornagain/html/py/run.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/py/run.html <h2 id="running-a-script">Running a script</h2> <p>To get started with BornAgain scripting, run a first example.</p> <p>We assume that BornAgain and Python are <a href="file:///usr/share/doc/bornagain/html/deploy">installed</a> and that the Python interpreter can <code>import bornagain</code> (see <a href="file:///usr/share/doc/bornagain/html/py">preceding page</a>, &ldquo;Check Installation&rdquo;).</p> <p>We shall now run a first example script. This and all other example scripts can also be found in the BornAgain distribution, in directory <a href="https://jugit.fz-juelich.de/mlz/bornagain/-/tree/r23/auto/Examples">auto/Examples</a>.</p> <p>Download the following example script, using the link just below the code frame. Save the script under the name <code>AlternatingLayers1.py</code>.</p>

# Understand the syntax

file:///usr/share/doc/bornagain/html/py/syntax.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/py/syntax.html <h2 id="python-syntax-used-in-bornagain-scripts">Python syntax used in BornAgain scripts</h2> <p>We shall employ the same reflectometry script as on the <a href="run">preceding page</a> to explain some Python syntax used in BornAgain scripts.</p> <p>For easy reference, here again the full script:</p> <div class="highlight"><div class="chroma"> <table class="lntable"><tr><td class="lntd"> <pre tabindex="0" class="chroma"><code></code></pre></td> <td class="lntd"> <pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"></code></pre></td></tr></table> </div> </div> <a class="linktofile" href="https://jugit.fz-juelich.de/mlz/bornagain/-/tree/r23/auto/Examples/specular/AlternatingLayers1.py"/>auto/Examples/specular/AlternatingLayers1.py</a> <p> <p>We shall now explain the code.</p> <p>The <a href="https://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line</p> <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="ch">#!/usr/bin/env python3</span> </span></span></code></pre></div><p>makes the script executable under Unix-like operating systems, see the chapter on <a href="file:///usr/share/doc/bornagain/html/py/run">how to run</a> scripts from the command line. It is ignored otherwise, as is all text from the comment start character <code>#</code> to the end of the line.</p>

# Fitting

file:///usr/share/doc/bornagain/html/py/fit.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/py/fit.html <h2 id="overview">Overview</h2> <p>In fitting, we estimate the optimum parameters in a numerical model, specifically a scattering simulation, by minimizing the difference between simulated and reference data.</p> <p>BornAgain supports</p> <ul> <li>a variety of minimization algorithms;</li> <li>choice of fitting parameters, their properties and correlations;</li> <li>full control over objective function calculations, including the use of different normalizations and assignments of different masks and weights to different areas of the reference data;</li> <li>possibility to fit simultaneously an arbitrary number of data sets.</li> </ul> <p>In the following we will show how to fit using the BornAgain Python API. For fitting through the graphical user interface, see <a href="file:///usr/share/doc/bornagain/html/gui/gui-fitting">GUI &gt; Fitting</a>.</p>

# Upgrading

file:///usr/share/doc/bornagain/html/py/upgrading.html Mon, 01 Jan 0001 00:00:00 +0000 file:///usr/share/doc/bornagain/html/py/upgrading.html <h1 id="upgrading-python-scripts-after-bornagain-version-change">Upgrading Python scripts after BornAgain version change</h1> <p>As we are working on simplifying and unifying the BornAgain Python API, each major release of BornAgain brings some changes that breaks some scripts.</p> <p>With BornAgain-22, we provide for the first time a conversion script that replaces <em>some</em> of the outdated constructs. This script can be found in the BornAgain source tree at <a href="https://jugit.fz-juelich.de/mlz/bornagain/-/tree/r23/scripts/upgrade-21-to-22.py">scripts/upgrade-21-to-22.py</a>.</p>