Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FP-python-examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
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
Frank Sauerburger
FP-python-examples
Commits
16f3ea4d
Verified
Commit
16f3ea4d
authored
6 years ago
by
Frank Sauerburger
Browse files
Options
Downloads
Patches
Plain Diff
Fix typos
parent
f989af27
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#1627
passed
6 years ago
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
README.md
+27
-21
27 additions, 21 deletions
README.md
with
27 additions
and
21 deletions
README.md
+
27
−
21
View file @
16f3ea4d
...
...
@@ -46,8 +46,8 @@ good solution for windows users since it provides all required packages.
# Prerequisites and About the Tutorial
This tutorial assumes that you have some experience with python, which
includes variable assignment, function calling and function definition, if statements and
for loops. The tutorial uses only the very basics, such as variable assignments
includes variable assignment, function calling and function definition,
`
if
`
statements and
`
for
`
loops. The tutorial uses only the very basics, such as variable assignments
and function calls, but it is certainly advisable to know about control
structures.
...
...
@@ -58,14 +58,14 @@ switch to python.
This tutorial is divided into several examples, which might depend on each
other. The examples show code snippets which you are supposed to copy to a text
editor. The scripts can then be executed in a terminal. Besides this modus
editor
and save to a file
. The scripts can then be executed in a terminal. Besides this modus
operandi, you are invited to use
the interactive mode of python or ipython instead and copy the code directly
to the python interpreter. Recently
[
jupyter
](
http://jupyter.org/
)
notebooks have
become very popular. I recommend you to try out these different options and
choose the one most suited for you.
This repository does not contain ready-made python example scripts or plots. The
*
Side remark:
This repository does not contain ready-made python example scripts or plots. The
only code resource is this README file. The idea behind this is, that there
shouldn't be duplications of code snippets, which will be out-of-sync eventually.
The repository is set up, such that
...
...
@@ -74,7 +74,7 @@ the README and executes them with the
[
doxec
](
https://gitlab.sauerburger.com/frank/doxec
)
package. This means you can
download
[
ready-made scripts and
plots
](
https://gitlab.sauerburger.com/frank/FP-python-examples/-/jobs/artifacts/master/download?job=doxec_test
)
produced by the continues integration task.
produced by the continues integration task.
*
Let's get going!
...
...
@@ -91,7 +91,7 @@ import math
print
(
"
Example 1:
"
)
# Strings can be formatted with the % operator. The placeholder %g prints a
# floating point numbers as decimal or
with
exponent depending on its
# floating point numbers as decimal or
in
exponent
notation(3e9)
depending on its
# magnitude.
print
(
"
Square root of 2 = %g
"
%
math
.
sqrt
(
2
))
```
...
...
@@ -113,14 +113,14 @@ The standard data structure to store numerical data is a numpy array. Numpy
arrays are defined in the numpy package and are implemented in a very
efficient way.
To get stared with numpy arrays create a file
`np_arrays.py`
and add all lines
To get stared with numpy arrays
,
create a file
`np_arrays.py`
and add all lines
listed in this section. The first line should be an import statement.
<!-- write np_arrays.py -->
```
python
# Import the numpy library.
import
numpy
as
np
```
In this example, we create
a
numpy array
`numbers`
containing my favorite numbers from
In this example, we create
the
numpy array
`numbers`
containing my favorite numbers from
the python list
`[4, 9, 16, 36, 49]`
.
<!-- append np_arrays.py -->
```
python
...
...
@@ -141,7 +141,7 @@ Since the resulting variable `roots` is also a numpy array, we can perform
similar operations on this variable.
<!-- append np_arrays.py -->
```
python
#
Perform
other c
alucal
tions for each element separately.
#
Execute
other c
omputa
tions for each element separately.
something_else
=
1.5
*
roots
-
4
```
Numpy arrays overload the typical arithmetic operations, such that the above
...
...
@@ -149,7 +149,7 @@ statement benefits from numpys efficient, vectorized (i.e. performing the same
operation on may values) implementation. You should always think about a way to
use such vectorized statements, and try to avoid manually looping over all the
values. Using a python loop to run over $
`10^3`
$ values is probably fine, but you
don't want to wait for a python loop iterating over 10^
6
or 10^9 values.
don't want to wait for a python loop iterating over 10^
8
or 10^9 values.
Finally, add a print statement to check that all the calculations have been
carried out as expected.
...
...
@@ -172,7 +172,7 @@ introduction. It is definitely worth glancing at the
# Plotting Functions
One major aspect of data analysis is the visualization of data. This includes the
generation of diagrams and plots. You can use the powerful
library
matplotlib to
generation of diagrams and plots. You can use the powerful matplotlib
library
to
create high-quality plots in a python environment. The goal of this example is to plot
the cropped parabola
...
...
@@ -184,6 +184,7 @@ the cropped parabola
```
which looks like this:

Create the file
`func_plot.py`
and add the following lines.
...
...
@@ -214,7 +215,7 @@ x = np.linspace(-2.5, 3, 200)
```
We can easily calculate the square of all these values with
`x**2`
. Cropping the
right part is a bit more
complex
. First we create an index array of
`1`
's and
right part is a bit more
tricky
. First we create an index array of
`1`
's and
`0`
's, which
indicate whether $
`x \geq 2`
$. This index array has the same length as our
$
`x`
$-grid. The first elements of the index array are
`0`
's, since the
...
...
@@ -278,10 +279,10 @@ cropped_parabola.png
-->
# Plotting Data Points
A typical task in the advanced laboratory
might be
to compare measured data
A typical task in the advanced laboratory
is
to compare measured data
points to an expected function. Let's assume the expected function is the cropped
parabola $
`f(x)`
$ from the previous example. We will use random data points in
this example since we haven't actually measured real data
,
which is expected to
this example since we haven't actually measured real data which is expected to
follow $
`f(x)`
$.
This example is based on the code from the previous example. Copy the file from
...
...
@@ -365,11 +366,13 @@ After running `data_plot.py`, you should have a plot similar to this.
# Reading, Plotting and Fitting Experimental Data
We are given with experimental data from a radioactive decay in this example.
The experimental setup consisted of a radioactive probe, a detector, and a
multi-channel-analyzer. The recorded data in
`decay.txt`
consist of two
multi-channel-analyzer. The recorded data in
[
`decay.txt`
](
https://gitlab.sauerburger.com/frank/FP-python-examples/blob/master/decay.txt
)
consist of two
tab-separated columns. The first column is called channel. Each channel
corresponds to a certain energy range. The multi-channel-analyzer maintains a
counter for each channel. A detected decay causes the multi-channel-analyzer to increment the internal counter which
corresponds to the energy of the measured decay. The second column store
d
corresponds to the energy of the measured decay. The second column store
s
these counts. Open the file with your favorite text editor and have a look at the
data.
...
...
@@ -399,7 +402,7 @@ import matplotlib.pyplot as plt
```
To inspect the provided data, we can plot the raw data points first. Numpy
provides the function
`
load
txt`
, which reads a whitespace-separated file into a
provides the function
`
genfrom
txt`
, which reads a whitespace-separated file into a
numpy array. The function returns a two-dimensional array. The outer array has
one entry for each line in the text file. The inner array has two entries in our
case, one for the
...
...
@@ -413,7 +416,7 @@ the square roots of the number of events.
<!-- append decay.py -->
```
python
# Read both columns from the text file.
channel
,
count
=
np
.
load
txt
(
"
decay.txt
"
).
transpose
()
channel
,
count
=
np
.
genfrom
txt
(
"
decay.txt
"
).
transpose
()
# Calculate the uncertainty on the number of events per channel.
s_count
=
np
.
sqrt
(
count
)
...
...
@@ -438,6 +441,7 @@ The plot of the raw data is shown below. The plot shows the channel on the
$
`x`
$-axis and the number of events per bin on the $
`y`
$-axis. From the
experimental setup, we expect a
linearly rising background plus a Gaussian peak.

Judging from the plot, it looks like the assumed model could describe the data. We
...
...
@@ -534,6 +538,7 @@ $ python3 decay.py
```
-->
The result should look like this.

Usually, we want to measure some quantity with an experimental setup. For this,
...
...
@@ -557,12 +562,13 @@ print() # print blank line
A $
`\chi^2`
$-test can be performed to assess the goodness of this fit. In
a counting experiment like this one, we can rely on scipy's
`chisquare`
, which
returns the $
`\chi^2`
$ and the $
`p`
$-value. The
`chisquare`
method assumes, that
the uncertainties are the square root of the expected number of events. If this is not the
the uncertainties are the square root
s
of the expected number of events. If this is not the
case, we have to compute the $
`\chi^2`
$ manually. The following example shows
both, the usage of
`chisquare`
and the manual computation. The print statements
for both methods produce the same output. Please note that
we have five degrees of freedom since we have five free parameters in our
model.
our model has five degrees of freedom since we have five free parameters. The
total number of degrees of freedom is the number of data points reduced by the
number of degrees of freedom of our model.
<!-- append decay.py -->
```
python
...
...
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