Jupyter notebooks introduction

Commands

Command mode

Editing mode

(advanced) Special cell commands

Markdown

https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html

italic bold

Reasons to use matlab :

According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because bees don't care what humans think is impossible. Yellow, black. Yellow, black. Yellow, black. Yellow, black. Ooh, black and yellow! Let's shake it up a little. Barry! Breakfast is ready!
Ooming! Hang on a second. Hello? - Barry? - Adam? - Oan you believe this is happening? - I can't. I'll pick you up.

iΨ(x,t)t=H^Ψ(x,t)

Math in text : 1+2=4. Amazing

Images (drag and drop, or link to it)

image legend

Exporting

File > Download as

Attention ! Images are not embedded automatically in HTML documents ! https://stackoverflow.com/questions/32370281/how-to-embed-image-or-picture-in-jupyter-notebook-either-from-a-local-machine-o

(Advanced : use the nbextension "Export Embedded HTML" to automatically embed images in an "HTML embedded" export)

(Optional) theming

https://github.com/dunovank/jupyter-themes

pip3 install --user jupyterthemes
# Reload the terminal
jt -t onedork -tfs 12 -fs 12 -ofs 12
# Relaunch juypter notebook

(Optional) nbextensions

https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/index.html

pip3 install --user jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip3 install --user jupyter_nbextensions_configurator  # optional for GUI
jupyter nbextensions_configurator enable --user  # optional
# Relaunch juypter notebook

My favorite extensions : Hinterland, Export embeded HTML

(Optional) custom keyboard shortcuts

Help > Edit keyboard shortcuts

Running this notebook

Packages

Run the cell below to install them

We later also use ipywidgets, but it is not required to run this notebook completely

LaTeX

A working TeX install is required to generate plots which use the TeX renderer.

Using apt, the required packages are texlive texlive-latex-extra texlive-fonts-recommended dvipng cm-super

Using Google Colab notebooks

  1. clone the folder https://drive.google.com/drive/folders/1gywgGPu0yT72gXdTIB-iAykEOuDypr-K?usp=sharing into your own drive
  2. Open demo8 notebook.ipynb with the Google Colab app
  3. mount your Google Drive
  4. cd into the cloned directory
  5. install TeX dependencies
  6. restart the kernel (Runtime > Restart runtime)

(uncomment the cells below and run them if you're running from Google Colab)

Magneto-optic effects - Lummer Gehreke interferometer

interf setup

interf rays

μBB=hcsa12d1n21

Goal : estimate μB

You'll learn :

Data : data/interferometer/parallel.csv, data/interferometer/transverse.csv

First plots

Plotting : https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html

Matplotlib recognizes most math expressions (https://matplotlib.org/stable/tutorials/text/mathtext.html), but can be configured to use LaTeX for rendering (https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html)

We use raw strings r'asdf' instead of normal strings 'asdf', because LaTeX commands such as '\text' would be interpreted as TAB ext. Alternatively, escape the escape character : '\\text'

Linear fit

https://numpy.org/doc/stable/reference/generated/numpy.polyfit.html

Compute Bohr magneton

The square root of the diagonals gives the standard deviation on each coefficient.

Note : taking the diagonal elements of the covariance matrix is justified by P. H. Richter, Estimating Errors in Least-Squares Fitting, page 8, url https://ipnpr.jpl.nasa.gov/progress_report/42-122/122E.pdf

Compute the result and print it

https://realpython.com/python-formatted-output/

Relative error

A note on the quality of a fit

There's a lot of talk around "r-coefficients", and many different answers online give unclear (or even plain wrong) information. Here is a little summary.

Residuals and coefficient of determination R2

How well does the linear fit predict the data ?

https://en.wikipedia.org/wiki/Coefficient_of_determination#Definitions

It is sometimes wished to obtain the residuals of the fit SSres, which is possible by using full=True in the polyfit call, or by directly computing (yi are data points, fi are the fitted points)

SSres=i(yifi)2

The total sum of squares (y¯ is the mean of the data)

SStot=i(yiy¯)2

The coefficient of determination R2 is then defined as

R2=1SSres/SStot

To compute, either do it manually or use scipy.stats.linregress (https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html)

Personal note : the 2 has nothing to do with a square, but is rather a notation, because we are working with sums of squares. R2 can be negative, making this a terrible, confusing notation and I hate it.

Pearson correlation coefficient r

How does this quantity affect another quantity ?

https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#For_a_sample

Let xi,yi be the data samples, and x¯,y¯ their respective means.

rxy=i(xix¯)(yiy¯)i(xix¯)2i(yiy¯)2

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html

From : https://en.wikipedia.org/wiki/Coefficient_of_determination#As_squared_correlation_coefficient

In linear least squares multiple regression with an estimated intercept term, R2 equals the square of the Pearson correlation coefficient between the observed y and modeled (predicted) f data values of the dependent variable.

TL;DR : for linear fits, r2=R2, but this is not true in general !!

Display the fit

poly1d : https://numpy.org/doc/stable/reference/generated/numpy.poly1d.html

Colors : https://matplotlib.org/stable/gallery/color/named_colors.html
Linestyles : https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html
Legend : https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html

The exponent formatting e24 and the error is not nice, we'll come back to it

Add the errors !

Two methods

Using uncertainties

unp.uarray basically is np.array but using ufloat instead of float

We need to recover numpy arrays using unp.nominal_values and unp.std_devs

We can now make use of the ufloat type to hold μB !

https://pythonhosted.org/uncertainties/user_guide.html#printing

A plot with errorbars

We can now plot the errors and format the legend nicely

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.errorbar.html

Multiple lines on the same plot

Loading all files in a directory which match a pattern

There are many ways of listing all files in a directory in python (https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory). Here I use glob from the standard library, which I find most useful as it supports wildcards

I also use os.path from the standard library to extract the filenames

Note : ax.plot returns an array of lines, here I know that array has only one element, so I can simply unpack it as line, = ax.plot(...) instead of line = ax.plot(...)[0]

I use this line to coordinate the colors of the plots

Was this overkill for two lines ? Yes, but now you can easily extend this to 300 data files !

Automating the legend cases with lookup dict

I use a dict to create a lookup table so I know how to generate my legend

Using LATEX

Cold plasma - Langmuir probe characteristic curve

uwu

Goal : plot the characteristic tension-current curve of the Langmuir probe

You'll learn :

Data : data/plasma/f=70 amp=5.0 p=1.0e-3 d=8.0 I_fil=44 U_grid=0 which=p.csv

Log scale

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_yscale.html

Haha, gaussian smoothing go BRRRRR

https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_filter1d.html

Yeet the hysterisis

Indexing : https://numpy.org/doc/stable/reference/arrays.indexing.html

(Advanced) Regex expressions

overengineer everything

You might have noticed the name of the data file f=70 amp=5.0 p=1.0e-3 d=8.0 I_fil=44 U_grid=0 which=p.csv contains the parameters of the experiment.

We can recover them automatically by

Because we like to overengineer everything, we use regex

Demo : https://regex101.com/r/bDAlcH/1

regex101

Chaotic motorized pendulum

owo

Goal : demonstrate chaotic behavior

You'll learn :

Data : data/pendulum/*.csv

Boolean masking

Now we plot all values corresponding to t[0,50]

(Note 1 : we could also use ax.set_xlim, but that doesn't pad the line inside the figure)

(Note 2 : in reality, the last output argument caught in *_ (3rd column of the .csv file) corresponds to the time derivative of theta, but here we differentiate manually for demonstration purposes)

Using numerical derivatives to sync the signal

Problem : the signals are all slightly offset

Solution : use the derivative to sync the signals, to after the point at which the mass is travelling quickly enough

https://numpy.org/doc/stable/reference/generated/numpy.gradient.html

The signals now start similarly, but we need to offset them !

Fourier transform

By using a Fourier transform, we can gain insight into the periodicity of the signal

https://numpy.org/doc/stable/reference/routines.fft.html

My first Fourier transform

  1. Compute the real Discrete Fourier Transform (implemented with the FFT ("Fast Fourier Transform") algorithm) using np.fft.rfft
  2. Compute the range of frequencies, using np.fft.rfftfreq. It depends on the number of points (len(t)) and the time difference between two points, computed using np.diff.

Removing the mean

The big spike at the middle corresponds to the mean of the signal. Recall (or don't, I won't judge) the definition of the Fourier transform :

F[f](k)=Rdf(x)ei2πkxdx

and the definition of the mean of a 1D signal :

f=limLL1L/2L/2f(x)dx

For a finite 1D signal, the domain of integration is finite of length L<, and evaluating the Fourier transform at k=0 reduces to F[f](0)=Lf

Removing the mean using np.mean removes this peak.

We also rescale the x axis because higher frequencies are dominated by the lower frequencies

Solid state physics - energy bands of one-dimensional graphene monoribbons

bands

Yes, this is an excuse to re-implement matlab code I was forced to write into python code

Goal : compute the energy bands as a function of the wavevector

You'll learn :

Compute the Hamiltonian

Interactive visualizations

Plot eigenvalues (energies) for different eigenvectors

Electrostatics - dielectric cylinder

Given Maxwell's equation D=ρlib with D=ϵ0ϵrE and potential E=ϕ, as well as some boundary conditions, we can obtain the following equation :

{(ϵ0ϵrϕ(x))=ρlib(x)if x in the volumeϕ(x)=V0if x on the boundary

In this case, we study an infinite vertical cylinder of radius R, and defining b[0,R] a point of discontinuity, we impose

ϵr(r)={1if r[0,b]2[1+arctan(2π(rb)R)]if r[b,R]ρlib(r)={ϵ0a0[r2b2(2rb3)+1]if r[0,b] 0if r[b,R]

Using variational forms and finite differences (c.f. course Physique Numérique II given by Prof Laurent Villard), this reduces to solving a system of linear equations

Aϕ=ξ

Goals :

You'll learn :

Define and plot ϵr and ρlib

Problem : we can't call eps_r or rho_lib with an array because of the if/else statements

Solution : vectorize it ! (https://numpy.org/doc/stable/reference/generated/numpy.vectorize.html)

(Note : an alternative is using np.where (https://numpy.org/doc/stable/reference/generated/numpy.where.html), which implements conditional branching on arrays)

Compute the matrix and solve

Compute polarization charges