Introduction to the SciPy stack and IPython Notebooks

GridKA School 2017

Thomas Keck

The SciPy Stack

Python-based ecosystem of open-source software for mathematics, science, and engineering

The SciPy stack includes:

   IPython Interactive python shell

   NumPy N-dimensional array package and data format used in SciPy

   Matplotlib Plotting and data visualization

   SciPy Scientific computing: Integration, Optimization, Statistics, ...

   Pandas Data structures & data analysis

   Sympy Symbolic mathematics

IPython is an interactive Python shell

The project was split up into:

   IPython focuses on interactive Python

   Jupyter contains the language agnostic parts

Useful IPython Features

see http://ipython.readthedocs.io/en/stable/interactive/index.html for a complete list

Documentation

In [2]:
import math
math.sqrt?
Docstring:
sqrt(x)

Return the square root of x.
Type:      builtin_function_or_method

Profiling

In [3]:
%timeit math.sqrt(2)
The slowest run took 23.18 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 259 ns per loop
In [4]:
from math import sqrt
%timeit sqrt(2)
The slowest run took 24.61 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 179 ns per loop
In [5]:
%timeit 2**0.5
The slowest run took 76.18 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 24.8 ns per loop

Debugging

In [6]:
%pdb
a = math.sqrt(-1)
print(a)
Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-4c17f9ae18c9> in <module>()
      1 get_ipython().magic('pdb')
----> 2 a = math.sqrt(-1)
      3 print(a)

ValueError: math domain error
> <ipython-input-6-4c17f9ae18c9>(2)<module>()
      1 get_ipython().magic('pdb')
----> 2 a = math.sqrt(-1)
      3 print(a)

ipdb> c

Plotting

In [7]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.arange(10), np.sqrt(np.arange(10)))
Out[7]:
[<matplotlib.lines.Line2D at 0x7fb78a68bc50>]

Execute Shell Commands

In [8]:
!cat /etc/passwd | head -n 2
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

Exercise

  1. Create a new cell below, change the cell type to markdown and write your favorite formula using latex
  2. Change the plot above so it uses blue circle markers instead of a line, look into the documentation of plt.plot to do so.
  3. Get a list of all users registered on this system by reading in the file /etc/passwd using a shell command
  4. Is there a difference between the speed of different arithmetic operations in pure Python?