Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.12.3"
hooks:
- id: black
language_version: python3.12
- id: ruff-format
- id: ruff-check
args: ["--fix", "--show-fixes"]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.7.0
rev: 1.9.1
hooks:
- id: nbqa-black
additional_dependencies: [black==23.9.1]
- id: nbqa-pyupgrade
additional_dependencies: [pyupgrade==2.7.3]
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ["--max-line-length=88", "--extend-ignore=E203"]
- id: nbqa-ruff-format
additional_dependencies: [ruff]

- repo: https://github.com/kynan/nbstripout
rev: 0.6.0
rev: 0.8.1
hooks:
- id: nbstripout
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ mamba activate toolbox


## Working with git
We are using `black` and `nbqa-black` as auto-formatters to ensure code quality.
To make sure these tools are running every time you make a commit, set up our
We are using `ruff` as auto-formatter and linter, and `nbqa-ruff-format` as auto-formatter
for notebooks to ensure code quality. To make sure these tools are running every
time you make a commit, set up our
``pre-commit hook`` via
```
$ pre-commit install
Expand All @@ -71,10 +72,10 @@ reject a commit if any of the checks fail. Any failing checks can then be solved
by fixing the issues reported by the `pre-commit hook`.


We use the github workflow in this repository, see <https://guides.github.com/introduction/flow/>.
We use the GitHub workflow in this repository, see <https://guides.github.com/introduction/flow/>.
In short, to contribute:

1. Create a new branch and switch to it using `git switch -c <name>`
1. Make changes and commit
1. Push the Branch using `git push -u origin <name>`
1. Open a Pull Request on github.
1. Open a Pull Request on GitHub.
9 changes: 5 additions & 4 deletions common/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from shlex import join

if len(sys.argv) < 3:
print("Usage: {} in.crop out.pdf".format(sys.argv[0]))
print(f"Usage: {sys.argv[0]} in.crop out.pdf")
exit(1)

pdfcrop = os.path.dirname(os.path.abspath(__file__)) + "/pdfcrop2.pl"
Expand All @@ -15,8 +15,9 @@
in_file = sys.argv[1]
out_file = sys.argv[2]

s = open(in_file, "r").read()
s = s.splitlines()
with open(in_file) as f:
s = f.read()
s = s.splitlines()

source = s[0]
page = s[1]
Expand All @@ -26,7 +27,7 @@
if len(s) > 3:
flags = s[3]
flags = flags.split()
auto = not "noauto" in flags
auto = "noauto" not in flags

cmd0 = ["pdfseparate", "-f", page, "-l", page, source, out_file]
print(join(cmd0))
Expand Down
11 changes: 6 additions & 5 deletions exercises-latex/13-python/loesung.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import matplotlib.pyplot as plt
import numpy as np
import uncertainties.unumpy as unp
from curve_fit import ucurve_fit
from uncertainties.unumpy import (
nominal_values as noms,
)
from uncertainties.unumpy import (
std_devs as stds,
)

from curve_fit import ucurve_fit


def make_qty(num, unit, exp="", figures=None):
"""Format an uncertainties ufloat as a \qty quantity"""
Expand All @@ -18,7 +19,7 @@ def make_qty(num, unit, exp="", figures=None):
else:
x = "{0:.{1:}f}".format(num, figures)

return r"\qty{{{}{}}}{{{}}}".format(x, exp, unit)
return rf"\qty{{{x}{exp}}}{{{unit}}}"


t, U, U_err = np.genfromtxt("data.txt", unpack=True)
Expand Down Expand Up @@ -72,7 +73,7 @@ def f(t, a, b, c, d):
t \mathbin{/} \unit{\milli\second} & \SetCell[c=2]{c} U \mathbin{/} \unit{\kilo\volt} & &
t \mathbin{/} \unit{\milli\second} & \SetCell[c=2]{c} U \mathbin{/} \unit{\kilo\volt} & \\
\midrule
"""
""" # noqa: E501

table_footer = r""" \bottomrule
\end{tblr}
Expand All @@ -84,7 +85,7 @@ def f(t, a, b, c, d):

with open("build/loesung-table.tex", "w") as f:
f.write(table_header)
for row in zip(t1, U1, t2, U2):
for row in zip(t1, U1, t2, U2, strict=False):
f.write(row_template.format(*row))
f.write("\n")
f.write(table_footer)
Expand Down
7 changes: 1 addition & 6 deletions exercises-latex/13-python/vorlage.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import matplotlib.pyplot as plt
import numpy as np
import uncertainties.unumpy as unp
from uncertainties.unumpy import (
nominal_values as noms,
std_devs as stds,
)

from curve_fit import ucurve_fit


def make_qty(num, unit, exp="", figures=None):
"""Format an uncertainties ufloat as a \qty quantity"""
Expand All @@ -18,4 +13,4 @@ def make_qty(num, unit, exp="", figures=None):
else:
x = "{0:.{1:}f}".format(num, figures)

return r"\qty{{{}{}}}{{{}}}".format(x, exp, unit)
return rf"\qty{{{x}{exp}}}{{{unit}}}"
2 changes: 1 addition & 1 deletion exercises-toolbox/1-python/5-readwrite/loesung1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
else:
filename = "test.txt"

with open(filename, "r") as f:
with open(filename) as f:
print(f.read())
# end solution
2 changes: 1 addition & 1 deletion exercises-toolbox/1-python/6-wordcount/loesung3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
counts = Counter(words)

for key, count in counts.most_common(20):
print("{}: {}".format(key, count))
print(f"{key}: {count}")
# end solution
6 changes: 3 additions & 3 deletions exercises-toolbox/1-python/7-fstrings/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# begin solution
print() # Für den Abstand zur letzten Lösung
# end solution
for metal, mass in zip(metals, masses):
for metal, mass in zip(metals, masses, strict=False):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, we should introduce the keyword argument in the python notebook.

print()
# begin solution
print(f"{metal}: {mass}")
Expand All @@ -51,14 +51,14 @@
# begin solution
# Für die 4.Aufgabe:
print() # Für den Abstand zur letzten Lösung
for metal, mass in zip(metals, masses):
for metal, mass in zip(metals, masses, strict=False):
print(f"{metal}: {mass:0.2f}")


# Zum Knobeln für Interessierte:
# Wie funktioniert das hier?
print() # Für den Abstand zur letzten Lösung
for metal, mass in zip(metals, masses):
for metal, mass in zip(metals, masses, strict=False):
offset = 11 - len(metal)
print(f"{metal}:", f"{mass:>{offset}.2f}")
# end solution
4 changes: 2 additions & 2 deletions exercises-toolbox/2-numpy/1-arrays/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
arrays = [a, b, c, d, e, f, g0, g1, h]

print("Aufgabe 1)")
for name, array in zip(array_names, arrays):
for name, array in zip(array_names, arrays, strict=False):
print(f"Array {name}: {array}")


# Aufgabe 2)
print("\nAufgabe 2)")
for name, array in zip(array_names, arrays):
for name, array in zip(array_names, arrays, strict=False):
print(f"Array {name}")
print(f"\t len({name}): {len(array)}")
print(f"\t {name}.shape: {array.shape}")
Expand Down
3 changes: 1 addition & 2 deletions exercises-toolbox/2-numpy/2-indexing/loesung.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#! /usr/bin/env python
# encoding: utf-8

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

field = np.array(
[
Expand Down
1 change: 0 additions & 1 deletion exercises-toolbox/2-numpy/5-functions/loesung.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env python
# encoding: utf-8

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/3-matplotlib/6/loesung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# begin solution
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 1, 100)

Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/3-matplotlib/7/loesung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# begin solution
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)

Expand Down
4 changes: 2 additions & 2 deletions exercises-toolbox/4-scipy/3-polyfit/loesung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# begin solution
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

# Generate data
rng = np.random.default_rng(210)
Expand All @@ -21,7 +21,7 @@ def f(x, a, b, c):
parameters, covariance_matrix = np.polyfit(x, y, deg=2, cov=True)
uncertainties = np.sqrt(np.diag(covariance_matrix))

for name, value, unc in zip("abc", parameters, uncertainties):
for name, value, unc in zip("abc", parameters, uncertainties, strict=False):
print(f"{name} = {value:.3f} ± {unc:.3f}")

fig = plt.figure(layout="constrained")
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/4-scipy/4-curve_fit/loesung.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

# begin solution
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/4-scipy/5-peakdetect/loesung.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

# begin solution

Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/4-scipy/6-beugung/loesung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


def theory(phi, A0, b):
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/5-uncertainties/1-formel/loesung.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# begin solution
from sympy import sin, var
from uncertainties import ufloat
from uncertainties.unumpy import sin as usin
from sympy import var, sin

x = ufloat(4.56, 0.2)
y = ufloat(2.11, 0.3)
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/5-uncertainties/3-curve_fit/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def f(x, a, b, c):
y = unp.uarray(y_0, y_err)
params = ucurve_fit(f, x, y)
print("a * cos(x * b) + c")
for char, p in zip("abc", params):
for char, p in zip("abc", params, strict=False):
print(f"{char} = {p}")

fig = plt.figure(layout="constrained")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def linleastsquares(functions, x_values, y_values):


def linear_combination(x, functions, params):
return np.sum([p * f(x) for p, f in zip(params, functions)], axis=0)
return np.sum([p * f(x) for p, f in zip(params, functions, strict=False)], axis=0)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/7-make/auswertung-bonus-loesung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


def theory(phi, A0, b):
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/7-make/auswertung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


def theory(phi, A0, b):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# begin solution
import numpy as np
from uncertainties.unumpy import nominal_values as noms
import matplotlib.pyplot as plt

import numpy as np
from curve_fit import ucurve_fit
from uncertainties.unumpy import nominal_values as noms


def Ur(r, A, B, C):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# begin solution
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

L, C, R = np.genfromtxt("geraetedaten.txt", unpack=True)
L *= 1e-3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from linregress import ulinregress
import matplotlib.pyplot as plt

# begin solution
import numpy as np
import uncertainties as unc
import uncertainties.unumpy as unp
from uncertainties.unumpy import nominal_values as noms, std_devs as stds
import matplotlib.pyplot as plt
from linregress import ulinregress
from uncertainties.unumpy import nominal_values as noms
from uncertainties.unumpy import std_devs as stds
Comment on lines +1 to +9
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mess with the automated template build?


Delta_t_0 = np.genfromtxt("Delta_t_0.txt")
N_0 = np.genfromtxt("N_0.txt")
Expand Down
2 changes: 1 addition & 1 deletion exercises-toolbox/8-all/corona/loesung.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt(
"./FB53-Coronafallzahlen.csv",
Expand Down
Loading