I am working on the file "Day_01/02_Numpy_Matplotlib/01_Matplotlib.ipynb".
The first task is:
What is Matplotlib?
matplotlib is a library for making 2D plots of arrays in Python. It is capable of producing
sophisticated 2D plots with high quality outputs.
matplotlib has some 3D plotting capabilities through mplot3d toolkit, but for sofisticated 3D visualization
it is better to use different packages
matplotlib is conceptually divided into three parts:

 the pylab interface is the set of functions provided by matplotlib.pylab which allow the user to create plots with code quite similar to MATLAB figure generating code.

 The matplotlib API is the set of classes that do the heavy lifting, creating and managing figures, text, lines, plots and so on.

 The backends are device-dependent drawing devices, aka renderers, that transform the frontend representation to hardcopy or a display device - not covered in this tutorial

Pyplot interface
Simple Plot
Can you write Python code for it?

%matplotlib inline

import numpy as np
import matplotlib.pylab as plt

x = [2, 3, 5, 7, 11,13,16]
y = [4, 9, 5, 9, 1,3,4.5]
plt.plot(x, y)


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
Debugging
iPython has a powerful debugger.  Let's see how it works a bit:
When iPython encounters an exception (in this case a ZeroDivisionError) it'll drop us into a pdb session if we use the %debug magic.  Commands:

? for "help"
? s for "help for command s"
l for "some more context"
s for "step into"
n for "step over"
c for "continue to next breakpoint"

You can also turn on automatic debugging.
Can you write Python code for it?


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
Exercise: write your own script that numbers input lines
Write a file, called lnum.py, such that the following cell works as shown (hint: don't forget about the executable bit!):
Can you write Python code for it?

%%script ./lnum.py
my first line
my second
more


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
Hints:

Useful function: sys.stdin.readlines()
Another useful function: enumerate()

You could use the notebook to query what these do...
Out and In
You can access the input and output of previous cells.
Can you write Python code for it?


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
I promised you latex!
You just need to surround equations in markdown with '$' signs.
$y = x^{2}$
$\frac{dN}{dE} = \frac{N_{\text peak}}{E_{\text peak}} (E/E_{\text peak})^{\gamma} (e^{1 - E/E_{\text peak}})^{\gamma+2}$
Note that it's using MathJax for the rendering so if you're offline, you might not get latex.
Running a remote file!
Note: do this after the plotting bit.
Can you write Python code for it?


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via
ctypes to segfault the Python interpreter:
Can you write Python code for it?

import sys
from ctypes import CDLL
# This will crash a Linux or Mac system; equivalent calls can be made on Windows
dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
libc = CDLL("libc.%s" % dll)
libc.time(-1)  # BOOM!!


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
Lots of useful features in the python debugger (and could probably do with a seperate lecture).  We'll save that for another day...
nbconvert
You can convert notebooks into lots of different formats with the nbconvert command (type 'ipython nbconvert' for all of the options).  Example:
Can you write Python code for it?

!ipython nbconvert mystuff.ipynb --to pdf


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
Managing the IPython Kernel
Code is run in a separate process called the IPython Kernel.  The Kernel can be interrupted or restarted.  Try running the following cell and then hit the "Stop" button in the toolbar above.
Can you write Python code for it?


I am working on the file "Day_01/03_Advanced_iPython/03_Advanced_iPython.ipynb".
The first task is:
My favs...
Can you write Python code for it?


I am working on the file "Day_01/03_Homework/Homework_01/00_Homework-Solutions.ipynb".
The first task is:
1. Use the "trends.csv" file and csv2rec() to import the data and reproduce this plot:
<img align='left' src="trends.png">
Can you write Python code for it?

# we can import the CSV data as a numpy rec array
from matplotlib.pylab import csv2rec
trends = csv2rec('trends.csv')

plot(trends.week_start, trends.spring_break, label='spring break')
plot(trends.week_start, trends.textbooks, label='texbooks')
plot(trends.week_start, trends.norad, label='norad')
plot(trends.week_start, trends.skiing, label='skiing')
legend()


I am working on the file "Day_01/03_Homework/Homework_01/00_Homework-Solutions.ipynb".
The first task is:
3. Which term has the largest scatter about its median value? Which term has the smallest scatter? The scatter around the median value can be found using:
$\sigma_{median}^2 = \sum (x_i - {\rm median}(x_i))^2$
Can you write Python code for it?

# study scatter about median values
def std_median(datums):
    return sqrt( sum( (datums - median(datums))**2 ) )

print "spring break: ",std_median(trends.spring_break)
print "textbooks: ",std_median(trends.textbooks)
print "skiing:",std_median(trends.skiing)
print "norad:",std_median(trends.norad)
print "global warming:",std_median(trends.global_warming)


I am working on the file "Day_01/03_Homework/Homework_01/00_Homework.ipynb".
The first task is:
1. Use the "trends.csv" file and csv2rec() to import the data and reproduce this plot:
<img align='left' src="trends.png">
Can you write Python code for it?

# we can import the CSV data as a numpy rec array
from matplotlib.pylab import csv2rec
trends = csv2rec('trends.csv')


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Breakout:
Start changing the number of grid points using our function and see what happens to our wave. What happens when you choose a large number? What happens when you choose a small number?
Can you write Python code for it?


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Breakout:
What happens when we change the second for loop to the other one? Why did we have to start at i=1?

(Extra) Breakout:
What happens to the wave if we repeat the calculation for $u$ and plot? Can you explain why this is so? Try changing the parameters to make the wave go backwards.

(Extra! Extra!) Breakout:
If we change nx to 81, what happens to our $u$ plot after it advances in time?
Step 2: Non-linear Convection
Non-linear convection only changes the constant velocity to a varying velocity (i.e., $c \rightarrow u$).
$$\frac{\partial{u}}{\partial{t}} + u\frac{\partial{u}}{\partial{x}} = 0$$
Using the same discretization schemes as in Step 1, forward diff for time and backward diff for space, we have the following discretization equation:
$$\frac{u_i^{n+1}-u_i^n}{\Delta t}+u_i^n\frac{u_i^n-u_{i-1}^n}{\Delta x} = 0$$
yielding for the $u_i^{n+1}$ term:
$$u_i^{n+1} = u_i^n - u_i^n\frac{\Delta t}{\Delta x}(u_i^n-u_{i-1}^n)$$
Homework:
The rest of this section is left up to you. Revise our for loop to include now that velocity term for the non-linear convection discretized equation to produce a similar plot like below.
<img src="hw1.png">
Step 3: CFL Condition
Can you write Python code for it?

def linearconv(nx):
    dx = 2./(nx-1)
    nt = 20
    dt = 0.025
    c = 1.

    u = np.ones(nx)
    u[.5/dx : 1/dx+1] = 2

    un = np.ones(nx)
    for n in range(nt):
        un = u.copy()
        for i in range(1,nx):
            u[i] = un[i] - c*(dt/dx)*(un[i] - un[i-1])

    plt.plot(np.linspace(0,2,nx), u);

linearconv(41) # 41 grid points, same as Step 1

linearconv(71)


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Breakout:
Why does this step-function (our initial velocity), have slanted lines? (Hint: What is the numpy linspace producing?)

Now, let's apply our finite-difference schemes to the convection equation given our new initial conditions to solve for our new velocity after some time has elapsed.
Can you write Python code for it?

un = np.ones(nx) #temporary n-th velocity array

for n in range(nt):
    un = u.copy() # store the previous time-step's values
    for i in range(1,nx):
    #for i in range(nx): # next breakout question
        u[i] = un[i] - c*(dt/dx)*(un[i] - un[i-1])


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Continued: Burgers' Equation
Can you write Python code for it?

###variable declarations
nx = 101
nt = 100
dx = 2*np.pi/(nx-1)
nu = .07
dt = dx*nu

x = np.linspace(0, 2*np.pi, nx)
un = np.empty(nx)
t = 0

u = np.asarray([ufunc(t, x0, nu) for x0 in x])
u

plt.figure(figsize=(11,7), dpi=100)
plt.plot(x, u, marker='o', lw=2)
plt.xlim([0,2*np.pi])
plt.ylim([0,10]);


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Note: This routine is actually quite inefficient, but we can improve this later.
Can you write Python code for it?


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Shown above, we see that this does not look like our original step-function wave. What happened? We broke stability (wave travels a distance in one time step, $\Delta t$, that is greater than our spatial step, $dx$). To maintain this, we need to enforce stability:
$$\sigma = \frac{u\Delta t}{\Delta x}\le \sigma_\text{max}$$
$\sigma$ is called the Courant number which $\sigma_\text{max}$ will ensure stability for our wave solution.
Can you write Python code for it?

def linearconv(nx):
    dx = 2./(nx-1)
    nt = 20
    c = 1.
    sigma = .5 # for nx=41, we get sigma=0.5 from dt/dx = 0.025/(2./(nx-1))

    dt = sigma*dx

    u = np.ones(nx)
    u[.5/dx : 1/dx+1] = 2

    un = np.ones(nx)
    for n in range(nt):
        un = u.copy()
        for i in range(1,nx):
            u[i] = un[i] - c*(dt/dx)*(un[i] - un[i-1])

    plt.plot(np.linspace(0,2,nx), u);

linearconv(85)

linearconv(201) # as we increase nx, our time window shortens due to a smaller dt


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Step 4: 1-D Diffusion
$$\frac{\partial{u}}{\partial{t}}=\nu\frac{\partial^2{u}}{\partial{x^2}}\text{,}\hspace{12pt}\text{the 1-D Diffusion equation.}$$
Since we now have a second-order differential, and to save some time, the discretized version of the diffusion equation is as follows:
$$\frac{u_{i}^{n+1}-u_{i}^{n}}{\Delta t}=\nu\frac{u_{i+1}^{n}-2u_{i}^{n}+u_{i-1}^{n}}{\Delta x^2}$$
and then arranged to solve for the only unknown ($u_i^{n+1}$),
$$u_{i}^{n+1}=u_{i}^{n}+\nu\frac{\Delta t}{\Delta x^2}(u_{i+1}^{n}-2u_{i}^{n}+u_{i-1}^{n})$$
Note: We used the Central Difference scheme on the second-order deriviative which is a combination of the Forward Difference and Backward Difference of the first derivative.
Can you write Python code for it?

nx = 41
dx = 2./(nx-1)
nt = 20
nu = 0.3 #the value of viscosity
sigma = .2 # notice the different sigma value
dt = sigma*dx**2/nu

u = np.ones(nx)
u[.5/dx : 1/dx+1] = 2

un = np.ones(nx)

for n in range(nt):
    un = u.copy()
    for i in range(1,nx-1):
        u[i] = un[i] + nu*dt/dx**2*(un[i+1]-2*un[i]+un[i-1])

plt.plot(np.linspace(0,2,nx), u);


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
Step 5: Burgers' Equation
$$\frac{\partial{u}}{\partial{t}}+u\frac{\partial{u}}{\partial{x}}=\nu\frac{\partial^2{u}}{\partial{x^2}}\text{,}\hspace{12pt}\text{Burgers' equation.}$$
which is a combination of non-linear convection and diffusion. Once again, we will draw from previous discretizations to obtain our discretized differential equation:
$$\frac{u_i^{n+1}-u_i^n}{\Delta t} + u_i^n \frac{u_i^n - u_{i-1}^n}{\Delta x} = \nu \frac{u_{i+1}^n - 2u_i^n + u_{i-1}^n}{\Delta x^2}$$
Yet again solving for the unknown $u_i^{n+1}$
$$u_i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u_{i-1}^n) + \nu \frac{\Delta t}{\Delta x^2}(u_{i+1}^n - 2u_i^n + u_{i-1}^n)$$
Whew! That looks scary, right?

Our initial condition for this problem is going to be:
\begin{eqnarray}
u(x,0) &=& -\frac{2 \nu}{\phi} \frac{\partial \phi}{\partial x} + 4 \\
\phi &=& \exp \bigg(\frac{-x^2}{4 \nu} \bigg) + \exp \bigg(\frac{-(x-2 \pi)^2}{4 \nu} \bigg)
\end{eqnarray}
This has an analytical solution, given by:
\begin{eqnarray}
u(x,t) &=& -\frac{2 \nu}{\phi} \frac{\partial \phi}{\partial x} + 4 \\
\phi &=& \exp \bigg(\frac{-(x-4t)^2}{4 \nu (t+1)} \bigg) + \exp \bigg(\frac{-(x-4t -2 \pi)^2}{4 \nu(t+1)} \bigg)
\end{eqnarray}
Our boundary condition will be:
$$u(0) = u(2\pi)$$
This is called a periodic boundary condition. Pay attention! This will cause you a bit of headache if you don't tread carefully.
Aside: SymPy
To save some time and frustrations, we are going to use a symbolic math library for Python. Let's explore it some:
Can you write Python code for it?

import numpy as np
import sympy

from sympy import init_printing
init_printing(use_latex=True) # output to be rendered as LaTeX

x,nu,t = sympy.symbols('x nu t')
phi = sympy.exp(-(x-4*t)**2/(4*nu*(t+1))) + sympy.exp(-(x-4*t-2*np.pi)**2/(4*nu*(t+1)))
phi

phiprime = phi.diff(x)
phiprime

print phiprime # shows Pythonic version

from sympy.utilities.lambdify import lambdify

u = -2*nu*(phiprime/phi)+4

# we are sending variables to a function
ufunc = lambdify((t, x, nu), u)
print ufunc(1,4,3)


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
This is a "saw-tooth function" to which we have applied our periodic boundary conditions. Steps 1 & 2 continually move the plot off the screen, but with periodic boundary conditions, it will wrap around to the front again.
Can you write Python code for it?

for n in range(nt):
    un = u.copy()
    for i in range(nx-1):
        u[i] = un[i] - un[i] * dt/dx *(un[i] - un[i-1]) + nu*dt/dx**2*\
                (un[i+1]-2*un[i]+un[i-1])
    u[-1] = un[-1] - un[-1] * dt/dx * (un[-1] - un[-2]) + nu*dt/dx**2*\
                (un[0]-2*un[-1]+un[-2])

u_analytical = np.asarray([ufunc(nt*dt, xi, nu) for xi in x])

plt.figure(figsize=(11,7), dpi=100)
plt.plot(x,u, marker='o', lw=2, label='Computational')
plt.plot(x, u_analytical, label='Analytical')
plt.xlim([0,2*np.pi])
plt.ylim([0,10])
plt.legend();


I am working on the file "Day_01/04_Scientific_Python_II/modeling.ipynb".
The first task is:
We need to give an initial wave which is a function of $x$ (remember, $u(x,0)=u_0(x)$). We can easily choose a step-function for the velocity:
$$u(x,0) = u_0(x) = \left{\begin{array}{ll}2 & : 0.5 \le x \le 1 \ 1 & : \text{elsewhere  in }(0,2)\end{array}\right}$$
Let's see this in Python!
Can you write Python code for it?

u = np.ones(nx)
u[.5/dx : 1./dx+1] = 2
print u

#visually
plt.plot(np.linspace(0, 2, nx), u);


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
A cubic interpolation can also be selected by providing the kind optional keyword argument:
Can you write Python code for it?

cubic_interp = interp1d(measured_time, measures, kind='cubic')
cubic_results = cubic_interp(computed_time)

plt.plot(measured_time, measures, 'o', ms=6, label='measures')
plt.plot(computed_time, linear_results, label='linear interp')
plt.plot(computed_time, cubic_results, label='cubic interp')
plt.legend()


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Curve Fitting
Suppose we have data sampled from f with some noise:
Can you write Python code for it?

xdata = np.linspace(-10, 10, num=20)
ydata = f(xdata) + np.random.randn(xdata.size)
plt.plot(xdata,ydata)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Define a function that can describe min and max temperatures. Hint: this function has to have a period of 1 year. Hint: include a time offset.
Can you write Python code for it?

temp = lambda t, a, b, c: b*np.sin(2.*math.pi*t+a)+c


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Fast Fourier Transforms (scipy.fftpack)
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Finding the minimum of a scalar function
Can you write Python code for it?

f = lambda x: x**2 + 10*np.sin(x)

x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Finding the roots of a scalar function
To find a root, i.e. a point where f(x) = 0, of the function f above we can use for example scipy.optimize.fsolve():
Can you write Python code for it?

root = optimize.fsolve(f, 1)  # our initial guess is 1
root


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
If a matrix has a determinant of zero LinAlg will raise an error. This is the definition of a Singular matrix (one for which an inverse does not exist).
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
If we don’t know the neighborhood of the global minimum to choose the initial point, we need to resort to costlier global optimization. To find the global minimum, the simplest algorithm is the brute force algorithm, in which the function is evaluated on each point of a given grid:
Can you write Python code for it?

grid = (-10, 10, 0.1)
xmin_global = optimize.brute(f, (grid,))
xmin_global


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
If we know that the random process belongs to a given family of random processes, such as normal processes, we can do a maximum-likelihood fit of the observations to estimate the parameters of the underlying distribution. Here we fit a normal process to the observed data:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Input / Output (scipy.io)
scipy has built-in functions to read and write to a wide variety of data formats, including Matlab, IDL, and netcdf.  Plain text and binary capabilites are available from numpy.  hdf5 has its own python module.
Can you write Python code for it?

from scipy import io
a = np.ones((3, 3))
io.savemat('file.mat', {'a': a}); # savemat expects a dictionary
data = io.loadmat('file.mat', struct_as_record=True)
data['a']


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Interpolation (scipy.interpolate)
The scipy.interpolate is useful for fitting a function from experimental data and thus evaluating points where no measure exists. The module is based on the FITPACK Fortran subroutines from the netlib project.
By imagining experimental data close to a sine function:
Can you write Python code for it?

measured_time = np.linspace(0, 1, 10)
noise = (np.random.random(10)*2 - 1) * 1e-1
measures = np.sin(2 * np.pi * measured_time) + noise
plt.plot(measured_time,measures)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
It is also called the percentile 50, because 50% of the observation are below it:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Linear Algebra Operations (scipy.linalg)
scipy.linalg contains all the functions in numpy.linalg plus some other more advanced ones not contained in numpy.linalg.
Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for numpy this is optional. Therefore, the scipy version might be faster depending on how numpy was installed.
Therefore, unless you don’t want to add scipy as a dependency to your numpy program, use scipy.linalg instead of numpy.linalg
The most commonly used methods are to calculate the determinant of a matrix (linalg.det) and to invert a matrix (linalg.inv)
Can you write Python code for it?

from scipy import linalg

arr1 = np.array([[1, 2], [3, 4]]); arr2 = np.array([[3, 2], [6, 4]]); arr3 = np.ones((3, 3))
print "arr 1 = \n",arr1
print "arr 2 = \n",arr2
print "arr 3 = \n",arr3

linalg.det(arr1), linalg.det(arr2)

linalg.det(arr3)

linalg.inv(arr1)

# np.allclose returns True if two arrays are element-wise equal within a tolerance
np.allclose(np.dot(arr1, linalg.inv(arr1)), np.eye(2))

print arr3
linalg.inv(arr3)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Measurements on images
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Note that only one root is found. Inspecting the plot of f reveals that there is a second root around -2.5. We find the exact value of it by adjusting our initial guess:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Note: In Scipy >= 0.11 unified interfaces to all minimization and root finding algorithms are available: scipy.optimize.minimize(), scipy.optimize.minimize_scalar() and scipy.optimize.root(). They allow comparing various algorithms easily through the method keyword.
Exercise: Curve fitting of temperature data
The temperature extremes in Alaska for each month, starting in January, are given by (in degrees Celcius):
| Temp | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
| ------------- |:-------------:| -----:|
| max: | 17 | 19 | 21 | 28 | 33 | 38 | 37 | 37 | 31 | 23 | 19 | 18 |
| min: | -62 | -59 | -56 | -46 | -32 | -18 | -9 | -13 | -25 | -46 | -52 | -58 |
Plot these temperature extremes.
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Now if we know the functional form of the function from which the samples were drawn (x^2 + sin(x) in this case) but not the amplitudes of the terms, we can find those by least squares curve fitting. First we have to define the function to fit:
Can you write Python code for it?

f2 = lambda x, a, b: a*x**2 + b*np.sin(x)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Now the high-frequency noise will be removed from the Fourier transformed signal:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Now we look for various information about the objects in the image:
Can you write Python code for it?

labels, nb = ndimage.label(mask)
nb

areas = ndimage.sum(mask, labels, xrange(1, labels.max()+1))
areas

maxima = ndimage.maximum(sig, labels, xrange(1, labels.max()+1))
maxima

ndimage.find_objects(labels==4)
sl = ndimage.find_objects(labels==4)

fig = plt.figure()
ax = fig.add_subplot(131)
ax.imshow(sig)
ax.axis('off')
ax.set_title("sig")
ax2 = fig.add_subplot(132)
ax2.imshow(mask)
ax2.axis('off')
ax2.set_title("mask")
ax3 = fig.add_subplot(133)
ax3.imshow(labels)
ax3.axis('off')
ax3.set_title("labels")


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Optimization and Fitting (scipy.optimize)
The scipy.optimize module provides useful algorithms for function minimization (scalar or multi-dimensional), curve fitting and root finding.
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Percentiles
The median is the value with half of the observations below, and half above:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Plot the result. Is the fit reasonable? If not, why?
Is the time offset for min and max temperatures the same within the fit accuracy?
Can you write Python code for it?

fig = plt.figure()
ax = fig.add_subplot(111)
x=np.linspace(-1,12,1000)
ax.plot(range(12), min, 'b.', label="min")
ax.plot(range(12), max, 'r.', label="max")
ax.plot(x, temp(x, *params), 'y--', label="min fit")
ax.plot(x, temp(x, *params2), 'g--', label="max fit")
ax.set_xlabel('Month')
ax.set_ylabel('Temperature ($^o C$)')
ax.set_xlim=[-1.,11.]
ax.set_ylim=[-80,60]


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Similarly, we can calculate the percentile 90:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The observer doesn’t know the signal frequency, only the sampling time step of the signal sig. The signal is supposed to come from a real function so the Fourier transform will be symmetric. The scipy.fftpack.fftfreq() function will generate the sampling frequencies and scipy.fftpack.fft() will compute the fast Fourier transform:
Can you write Python code for it?

sample_freq = fftpack.fftfreq(sig.size, d=time_step)
sig_fft = fftpack.fft(sig)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The percentile is an estimator of the CDF: cumulative distribution function.
Statistical tests
A statistical test is a decision indicator. For instance, if we have two sets of observations, that we assume are generated from Gaussian processes, we can use a T-test to decide whether the two sets of observations are significantly different:
Can you write Python code for it?

a = np.random.normal(0, 1, size=100)
b = np.random.normal(1, 1, size=10)
stats.ttest_ind(a, b)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The result can be viewed with:
Can you write Python code for it?

plt.figure()
plt.plot(time_vec, sig)
plt.plot(time_vec, main_sig, linewidth=3)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The resulting filtered signal can be computed by the scipy.fftpack.ifft() function:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The scipy.interpolate.interp1d class can build a linear interpolation function:
Can you write Python code for it?

from scipy.interpolate import interp1d
linear_interp = interp1d(measured_time, measures)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
The signal frequency can be found by:
Can you write Python code for it?

freq = freqs[power.argmax()]
freq, np.allclose(freq, 1./period)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Then the scipy.interpolate.linear_interp instance needs to be evaluated at the time of interest:
Can you write Python code for it?

computed_time = np.linspace(0, 1, 50)
linear_results = linear_interp(computed_time)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
Then we can use scipy.optimize.curve_fit() to find a and b:
Can you write Python code for it?

guess = [2, 2]
params, params_covariance = optimize.curve_fit(f2, xdata, ydata, guess)
params

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, f(x), 'b-', label="f(x)")
ax.plot(x, f2(x, *params), 'r--', label="Curve fit result")
xmins = np.array([xmin_global[0], xmin_local])
ax.plot(xmins, f(xmins), 'go', label="Minima")
roots = np.array([root, root2])
ax.plot(roots, f(roots), 'kv', label="Roots")
ax.set_xlabel('x')
ax.set_ylabel('f(x)')


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
This function has a global minimum around -1.3 and a local minimum around 3.8.
The general and efficient way to find a minimum for this function is to conduct a gradient descent starting from a given initial point. The BFGS algorithm is a good way of doing this:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
scipy.signal.detrend(): remove linear trend from signal:
Can you write Python code for it?

t = np.linspace(0, 5, 100)
x = t + np.random.normal(size=100)

plt.plot(t, x, linewidth=3)
plt.plot(t, signal.detrend(x), linewidth=3)


I am working on the file "Day_02/00_Scipy/00_scipy.ipynb".
The first task is:
scipy.signal.resample(): resample a signal to n points using FFT.
Can you write Python code for it?

t = np.linspace(0, 5, 100)
x = np.sin(t)

plt.plot(t, x, linewidth=3)
plt.plot(t[::2], signal.resample(x, 50), 'ko')


I am working on the file "Day_02/00_Scipy/01_scipy_Breakout.ipynb".
The first task is:
1) Crop the image to remove the lower panel with measure information. Then re-plot the trimmed image.
2) Slightly filter the image with a median filter in order to refine its histogram. Check how the histogram changes.
Use ndimage.median_filter to filter the image and plt.hist to make separate histograms of the filtered and unfiltered pixel distributions.
3) Using the histogram of the filtered image, determine thresholds that allow to define masks for sand pixels, glass pixels and bubble pixels.
For example, you can isolate bubble pixels by creating a mask (array of booleans) this way:
bubble = filtdat <= 0.2
To combine conditions you can np.logical_and().
4) Display an image in which the three phases are colored with three different colors. You can do this by using you existing masks for the sand, glass and bubble pixels to create a new ndarray in which each type is assigned a different integer (1, 2, 3).
For example glass.astype(np.int) will create an array in which the integer 1 is assigned to all elements in the array glass which have the value of True.
You can attribute labels to all bubbles and sand grains, and remove from the sand mask grains that are smaller than 10 pixels. To do so, use ndimage.sum or np.bincount to compute the grain sizes.
Can you write Python code for it?

sand_labels, sand_nb = ndimage.label(sand)
sand_areas = np.array(ndimage.sum(sand, sand_labels, np.arange(sand_labels.max()+1)))
mask = sand_areas > 100
remove_small_sand = mask[sand_labels.ravel()].reshape(sand_labels.shape)


I am working on the file "Day_02/00_Scipy/01_scipy_Breakout.ipynb".
The first task is:
The following code will open the image file MV_HFV_012.png and display it.  This Scanning Element Microscopy image shows a glass sample (light gray) with some bubbles (on black) and unmolten sand grains (dark gray).
Can you write Python code for it?

img = plt.imread("MV_HFV_012.png")
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(img, cmap=cm.gray)
ax.axis('off')


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
1) Crop the image to remove the lower panel with measure information. Then re-plot the trimmed image.
Can you write Python code for it?

trim = img[:-60,:]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(trim, cmap=cm.gray)
ax.axis('off')


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
2) Slightly filter the image with a median filter in order to refine its histogram. Check how the histogram changes.
Use ndimage.median_filter to filter the image and plt.hist to make separate histograms of the filtered and unfiltered pixel distributions.
Can you write Python code for it?

filtdat = ndimage.median_filter(trim, size=(7,7))
thist = plt.hist(np.ravel(trim), bins=np.linspace(0,1,256), histtype="step", label="Unfiltered")
fhist = plt.hist(np.ravel(filtdat), bins=np.linspace(0,1,256), histtype="step", label="Filtered")
plt.legend()


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
3) Using the histogram of the filtered image, determine thresholds that allow to define masks for sand pixels, glass pixels and bubble pixels.
For example, you can isolate bubble pixels by creating a mask (array of booleans) this way:
bubble = filtdat <= 0.2
To combine conditions you can np.logical_and().
Can you write Python code for it?

bubble = filtdat <= 0.2
sand = np.logical_and(filtdat > 0.2, filtdat <= 0.45)
glass = filtdat > 0.45


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
4) Display an image in which the three phases are colored with three different colors. You can do this by using you existing masks for the sand, glass and bubble pixels to create a new ndarray in which each type is assigned a different integer (1, 2, 3).
For example glass.astype(np.int) will create an array in which the integer 1 is assigned to all elements in the array glass which have the value of True.
Can you write Python code for it?

phases = glass.astype(np.int) + 2*sand.astype(np.int) + 3*bubble.astype(np.int)
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(phases)
ax.axis('off')
ax2 = fig.add_subplot(122)
ax2.imshow(filtdat, cmap=cm.gray)
ax2.axis('off')


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
5) Finally, we can compute the mean and median size of bubbles.
Can you write Python code for it?

bubbles_labels, bubbles_nb = ndimage.label(bubble)
bubbles_areas = np.bincount(bubbles_labels.ravel())[1:]
mean_bubble_size = bubbles_areas.mean()
median_bubble_size = np.median(bubbles_areas)
mean_bubble_size, median_bubble_size


I am working on the file "Day_02/00_Scipy/02_scipy_Breakout-solutions.ipynb".
The first task is:
You can attribute labels to all bubbles and sand grains, and remove from the sand mask grains that are smaller than 10 pixels. To do so, use ndimage.sum or np.bincount to compute the grain sizes.
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/04_Breakout_trapezoid_rule_solution.ipynb".
The first task is:
3
Verify that both functions above are correct by showing that they produces correct values
for a simple integral such as $\int_0^3 x^2$.
Can you write Python code for it?

exact = 9.0
x = np.linspace(0, 3, 50)
y = x**2

print exact
print trapz(x, y)

def f(x): return x**2
print trapzf(f, 0, 3, 50)


I am working on the file "Day_02/00_Scipy/04_Breakout_trapezoid_rule_solution.ipynb".
The first task is:
An illustration using matplotlib and scipy
We define a function with a little more complex look
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/04_Breakout_trapezoid_rule_solution.ipynb".
The first task is:
Choose a region to integrate over and take only a few points in that region
Can you write Python code for it?

a, b = 1, 9
xint = x[logical_and(x>=a, x<=b)][::30]
yint = y[logical_and(x>=a, x<=b)][::30]


I am working on the file "Day_02/00_Scipy/04_Breakout_trapezoid_rule_solution.ipynb".
The first task is:
In practice, we don't need to implement numerical integration ourselves, as scipy has both basic trapezoid
rule integrators and more sophisticated ones. Here we illustrate both:
Can you write Python code for it?

from scipy.integrate import quad, trapz
integral, error = quad(f, 1, 9)
print "The integral is:", integral, "+/-", error
print "The trapezoid approximation with", len(xint), "points is:", trapz(yint, xint)


I am working on the file "Day_02/00_Scipy/04_Breakout_trapezoid_rule_solution.ipynb".
The first task is:
Plot both the function and the area below it in the trapezoid approximation
Can you write Python code for it?

plot(x, y, lw=2)
axis([0, 10, 0, 140])
fill_between(xint, 0, yint, facecolor='gray', alpha=0.4)
text(0.5 * (a + b), 30,r"$\int_a^b f(x)dx$", horizontalalignment='center', fontsize=20);


I am working on the file "Day_02/00_Scipy/scipy-ODE-DampedHarmonicOscillator.ipynb".
The first task is:
A system of ODEs are usually formulated on standard form before it is attacked numerically. The standard form is:
$y' = f(y, t)$
where
$y = [y_1(t), y_2(t), ..., y_n(t)]$
and $f$ is some function that gives the derivatives of the function $y_i(t)$. To solve an ODE we need to know the function $f$ and an initial condition, $y(0)$.
Note that higher-order ODEs can always be written in this form by introducing new variables for the intermediate derivatives.
Once we have defined the Python function f and array y_0 (that is $f$ and $y(0)$ in the mathematical formulation), we can use the odeint function as:
y_t = odeint(f, y_0, t)

where t is and array with time-coordinates for which to solve the ODE problem. y_t is an array with one row for each point in time in t, where each column corresponds to a solution y_i(t) at that point in time.
We will see how we can implement f and y_0 in Python code in the examples below.
Example: Damped harmonic oscillator
ODE problems are important in computational physics, so we will look at one more example: the damped harmonic oscillation. This problem is well described on the wiki page: http://en.wikipedia.org/wiki/Damping
The equation of motion for the damped oscillator is:
$\displaystyle \frac{\mathrm{d}^2x}{\mathrm{d}t^2} + 2\zeta\omega_0\frac{\mathrm{d}x}{\mathrm{d}t} + \omega^2_0 x = 0$
where $x$ is the position of the oscillator, $\omega_0$ is the frequency, and $\zeta$ is the damping ratio. To write this second-order ODE on standard form we introduce $p = \frac{\mathrm{d}x}{\mathrm{d}t}$:
$\displaystyle \frac{\mathrm{d}p}{\mathrm{d}t} = - 2\zeta\omega_0 p - \omega^2_0 x$
$\displaystyle \frac{\mathrm{d}x}{\mathrm{d}t} = p$
In the implementation of this example we will add extra arguments to the RHS function for the ODE, rather than using global variables as we did in the previous example. As a consequence of the extra arguments to the RHS, we need to pass an keyword argument args to the odeint function:
Can you write Python code for it?

def dy(y, t, zeta, w0):
    """
    The right-hand side of the damped oscillator ODE
    """
    x, p = y[0], y[1]

    dx = p
    dp = -2 * zeta * w0 * p - w0**2 * x

    return [dx, dp]

# initial state:
y0 = [1.0, 0.0]

# time coodinate to solve the ODE for
t = linspace(0, 10, 1000)
w0 = 2*pi*1.0

# solve the ODE problem for three different values of the damping ratio

y1 = odeint(dy, y0, t, args=(0.0, w0)) # undamped
y2 = odeint(dy, y0, t, args=(0.2, w0)) # under damped
y3 = odeint(dy, y0, t, args=(1.0, w0)) # critial damping
y4 = odeint(dy, y0, t, args=(5.0, w0)) # over damped

fig, ax = plt.subplots()
ax.plot(t, y1[:,0], 'k', label="undamped", linewidth=0.25)
ax.plot(t, y2[:,0], 'r', label="under damped")
ax.plot(t, y3[:,0], 'b', label=r"critical damping")
ax.plot(t, y4[:,0], 'g', label="over damped")
ax.legend();


I am working on the file "Day_02/00_Scipy/scipy-ODE-DampedHarmonicOscillator.ipynb".
The first task is:
SciPy provides two different ways to solve ODEs: An API based on the function odeint, and object-oriented API based on the class ode. Usually odeint is easier to get started with, but the ode class offers some finer level of control.
Here we will use the odeint functions. For more information about the class ode, try help(ode). It does pretty much the same thing as odeint, but in an object-oriented fashion.
To use odeint, first import it from the scipy.integrate module
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/scipy-ODE-DoublePendulum.ipynb".
The first task is:
Example: Damped harmonic oscillator
ODE problems are important in computational physics, so we will look at one more example: the damped harmonic oscillation. This problem is well described on the wiki page: http://en.wikipedia.org/wiki/Damping
The equation of motion for the damped oscillator is:
$\displaystyle \frac{\mathrm{d}^2x}{\mathrm{d}t^2} + 2\zeta\omega_0\frac{\mathrm{d}x}{\mathrm{d}t} + \omega^2_0 x = 0$
where $x$ is the position of the oscillator, $\omega_0$ is the frequency, and $\zeta$ is the damping ratio. To write this second-order ODE on standard form we introduce $p = \frac{\mathrm{d}x}{\mathrm{d}t}$:
$\displaystyle \frac{\mathrm{d}p}{\mathrm{d}t} = - 2\zeta\omega_0 p - \omega^2_0 x$
$\displaystyle \frac{\mathrm{d}x}{\mathrm{d}t} = p$
In the implementation of this example we will add extra arguments to the RHS function for the ODE, rather than using global variables as we did in the previous example. As a consequence of the extra arguments to the RHS, we need to pass an keyword argument args to the odeint function:
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/scipy-ODE-DoublePendulum.ipynb".
The first task is:
SciPy provides two different ways to solve ODEs: An API based on the function odeint, and object-oriented API based on the class ode. Usually odeint is easier to get started with, but the ode class offers some finer level of control.
Here we will use the odeint functions. For more information about the class ode, try help(ode). It does pretty much the same thing as odeint, but in an object-oriented fashion.
To use odeint, first import it from the scipy.integrate module
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/scipy-ODE-DoublePendulum.ipynb".
The first task is:
Simple annimation of the pendulum motion. We will see how to make better animation in Lecture 4.
Can you write Python code for it?

from IPython.display import display, clear_output
import time

fig, ax = plt.subplots(figsize=(4,4))

for t_idx, tt in enumerate(t[:200]):

    x1 = + L * sin(x[t_idx, 0])
    y1 = - L * cos(x[t_idx, 0])

    x2 = x1 + L * sin(x[t_idx, 1])
    y2 = y1 - L * cos(x[t_idx, 1])

    ax.cla()
    ax.plot([0, x1], [0, y1], 'r.-')
    ax.plot([x1, x2], [y1, y2], 'b.-')
    ax.set_ylim([-1.5, 0.5])
    ax.set_xlim([1, -1])

    clear_output()
    display(fig)

    time.sleep(0.1)


I am working on the file "Day_02/00_Scipy/scipy-ODE-DoublePendulum.ipynb".
The first task is:
The equations of motion of the pendulum are given on the wiki page:
${\dot \theta_1} = \frac{6}{m\ell^2} \frac{ 2 p_{\theta_1} - 3 \cos(\theta_1-\theta_2) p_{\theta_2}}{16 - 9 \cos^2(\theta_1-\theta_2)}$
${\dot \theta_2} = \frac{6}{m\ell^2} \frac{ 8 p_{\theta_2} - 3 \cos(\theta_1-\theta_2) p_{\theta_1}}{16 - 9 \cos^2(\theta_1-\theta_2)}.$
${\dot p_{\theta_1}} = -\frac{1}{2} m \ell^2 \left [ {\dot \theta_1} {\dot \theta_2} \sin (\theta_1-\theta_2) + 3 \frac{g}{\ell} \sin \theta_1 \right ]$
${\dot p_{\theta_2}} = -\frac{1}{2} m \ell^2 \left [ -{\dot \theta_1} {\dot \theta_2} \sin (\theta_1-\theta_2) +  \frac{g}{\ell} \sin \theta_2 \right]$
To make the Python code simpler to follow, let's introduce new variable names and the vector notation: $x = [\theta_1, \theta_2, p_{\theta_1}, p_{\theta_2}]$
${\dot x_1} = \frac{6}{m\ell^2} \frac{ 2 x_3 - 3 \cos(x_1-x_2) x_4}{16 - 9 \cos^2(x_1-x_2)}$
${\dot x_2} = \frac{6}{m\ell^2} \frac{ 8 x_4 - 3 \cos(x_1-x_2) x_3}{16 - 9 \cos^2(x_1-x_2)}$
${\dot x_3} = -\frac{1}{2} m \ell^2 \left [ {\dot x_1} {\dot x_2} \sin (x_1-x_2) + 3 \frac{g}{\ell} \sin x_1 \right ]$
${\dot x_4} = -\frac{1}{2} m \ell^2 \left [ -{\dot x_1} {\dot x_2} \sin (x_1-x_2) +  \frac{g}{\ell} \sin x_2 \right]$
Can you write Python code for it?

g = 9.82
L = 0.5
m = 0.1

def dx(x, t):
    """
    The right-hand side of the pendulum ODE
    """
    x1, x2, x3, x4 = x[0], x[1], x[2], x[3]

    dx1 = 6.0/(m*L**2) * (2 * x3 - 3 * cos(x1-x2) * x4)/(16 - 9 * cos(x1-x2)**2)
    dx2 = 6.0/(m*L**2) * (8 * x4 - 3 * cos(x1-x2) * x3)/(16 - 9 * cos(x1-x2)**2)
    dx3 = -0.5 * m * L**2 * ( dx1 * dx2 * sin(x1-x2) + 3 * (g/L) * sin(x1))
    dx4 = -0.5 * m * L**2 * (-dx1 * dx2 * sin(x1-x2) + (g/L) * sin(x2))

    return [dx1, dx2, dx3, dx4]

# choose an initial state
x0 = [pi/4, pi/2, 0, 0]

# time coodinate to solve the ODE for: from 0 to 10 seconds
t = linspace(0, 10, 250)

# solve the ODE problem
x = odeint(dx, x0, t)

# plot the angles as a function of time

fig, axes = plt.subplots(1,2, figsize=(12,4))
axes[0].plot(t, x[:, 0], 'r', label="theta1")
axes[0].plot(t, x[:, 1], 'b', label="theta2")

x1 = + L * sin(x[:, 0])
y1 = - L * cos(x[:, 0])

x2 = x1 + L * sin(x[:, 1])
y2 = y1 - L * cos(x[:, 1])

axes[1].plot(x1, y1, 'r', label="pendulum1")
axes[1].plot(x2, y2, 'b', label="pendulum2")
axes[1].set_ylim([-1, 0])
axes[1].set_xlim([1, -1]);


I am working on the file "Day_02/00_Scipy/scipy_Practice-solutions.ipynb".
The first task is:
Construct histogram from data
There are several histrogram commands: hist() will be fine here, but note the syntax below. Also note that the bin edges are returned, so that there will nbins+1 of these.
Can you write Python code for it?

nbins = 50; # Is this a good choice?

n, bins, patches = hist(zz,nbins)

x = bins[0:nbins] + (bins[2]-bins[1])/2; # Convert bin edges to centres, chopping the last


I am working on the file "Day_02/00_Scipy/scipy_Practice-solutions.ipynb".
The first task is:
Load data from file
Can you write Python code for it?

zz = np.loadtxt('wiggleZ_DR1_z.dat',dtype='float'); # Load WiggleZ redshifts

np.min(zz) # Check bounds

np.max(zz)


I am working on the file "Day_02/00_Scipy/scipy_Practice.ipynb".
The first task is:
Check bounds
Can you write Python code for it?


I am working on the file "Day_02/00_Scipy/scipy_Practice.ipynb".
The first task is:
Construct histogram from data
There are several histrogram commands: hist() will be fine here, but note the syntax below. Also note that the bin edges are returned, so that there will nbins+1 of these.
Can you write Python code for it?

nbins = 50; # Is this a good choice?

n, bins, patches = hist() # With hist, one needs to (spuriously) request the patch objects as well

x = bins[0:nbins] + (bins[2]-bins[1])/2; # Convert bin edges to centres, chopping the last


I am working on the file "Day_02/00_Scipy/scipy_Practice.ipynb".
The first task is:
Interpolate histogram output -> p(z); n.b. that you can also use numerical quadrature to get $P(z)$ directly.
Can you write Python code for it?

# Import the function you need
from scipy.interpolate import interp1d

# Build an interpolation function for p(z) that accepts an arbitrary redshift z

z = linspace(0,2,100); plot(z,p(z)) # Test your interpolation function out


I am working on the file "Day_02/00_Scipy/scipy_Practice.ipynb".
The first task is:
Load data from file
Can you write Python code for it?

zz = np.loadtxt('wiggleZ_DR1_z.dat',dtype='float'); # Load WiggleZ redshifts


I am working on the file "Day_02/00_Scipy/scipy_Practice.ipynb".
The first task is:
Use numerical integration to get $P(z) = \int_0^\infty p(z') dz'$
Can you write Python code for it?

# Import the function you need
from scipy import integrate

Pz = lambda : ... # Use integrate inside a lambda function to define P(z)?

total = Pz(5) # Get normalisation constant by evaluating P(z->\infty)

total # Check that this worked


I am working on the file "Day_02/01_Development_I/Development_I.ipynb".
The first task is:
.
./package_name
./package_name/docs
./package_name/LICENSE.txt
./package_name/README.md
./package_name/setup.py
./package_name/requirements.txt
./package_name/package_name
./package_name/package_name/__init__.py

PyPA Sample Project gives a sample setup.py file that we can explore:
Can you write Python code for it?


I am working on the file "Day_02/01_Development_I/Development_I.ipynb".
The first task is:
Breakout!
Start plaing with some docstrings inside classes, functions and at the top of your scripts. See how to generate different __doc__ statements.

Sphinx
Sphinx is a preferred choice for Python documentation. There is an autodoc feature that generates generic documentation and can pull code's docstrings as well. A quick reStructuredText Primer is also available.

reStructuredText markup (rst)
Read the Docs free hosting

A little Python  documentation humor...

EXTERIOR: DAGOBAH -- DAY
      With Yoda strapped to his back, Luke climbs up one of
   the many thick vines that grow in the swamp until he
   reaches the Dagobah statistics lab. Panting heavily, he
   continues his exercises -- grepping, installing new
   packages, logging in as root, and writing replacements for
   two-year-old shell scripts in Python.

YODA: Code!  Yes.  A programmer's strength flows from code
     maintainability.  But beware of Perl.  Terse syntax... more
     than one way to do it...  default variables.  The dark side
     of code maintainability are they.  Easily they flow, quick
     to join you when code you write.  If once you start down the
     dark path, forever will it dominate your destiny, consume
     you it will.
LUKE: Is Perl better than Python?
YODA: No... no... no.  Quicker, easier, more seductive.
LUKE: But how will I know why Python is better than Perl?
YODA: You will know.  When your code you try to read six months
     from now.

Part 2 - Version Control & Repositories
Within a project's evolution, management of revisions, significant releases, and collaborative development becomes too large to keep renaming files or creating duplicates.
Semantic Versioning
Version Control via Git
Some really good, simple guides that we are going to use:

git - the simple guide
visual git concepts

More Resources:

git
think-like-a-git
git-tower book
gitref
git immersion

Other Version Control Systems:

Mercurial
Apache Subversion (svn)
Concurrent Versions System (CVS)

Repository Hosting
You can keep repositories local...
Can you write Python code for it?

import modu
# now we access modu's scripts via modu.{}
from modu import modu
# here we access the modu module inside the modu package


I am working on the file "Day_02/01_Development_I/Development_I.ipynb".
The first task is:
Virtual Environments (virtualenv & virtualenvwrapper)

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them.
Can you write Python code for it?


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
Here we have two species: a dog and a cat. Both are animals. The Dog class and the Cat class inherit the Animal class. They have a talk() method, which gives different output for them.
Encapsolation
In computer programming languages, encapsulation is used to refer to one of three related but distinct ideas:

A language mechanism for restricting direct access to some of the object's components.
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
A programming convention for respecting the intended scope of the information

Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object-oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.
The second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.
Python doesn’t have any mechanisms that would effectively restrict you from accessing a variable or calling a member method.  All of this is a matter of culture and convention.
Data Encapsolation
| Name | Notation | Behaviour
| :- | :- | :-
| name | Public | Can be accessed from inside and outside
| _name | Protected | Like a public member, but they shouldn't be directly accessed from outside.
| __name | Private | Can't be seen and accessed from outside
If an identifier is only preceded by one underscore character, it is a protected member. Protected members can be accessed like public members from outside of class.
It can be seen in the following example:
Can you write Python code for it?

class Encapsulation(object):
    def __init__(self, a, b, c):
        self.public = a
        self._protected = b
        self.__private = c


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
In the code example, we have a Circle class. We define three new methods.

area()
setRadius()
getRadius()

The area() method returns the area of a circle.  The getRadius() method returns the current radius.
Special Methods
Classes in Python programming language can implement certain operations with special method names. These methods are not called directly, but by a specific language syntax. This is similar to what is known as operator overloading in C++ or Ruby.
Can you write Python code for it?

class Book:
   def __init__(self, title, author, pages):
      print "A book is created"
      self.title = title
      self.author = author
      self.pages = pages

   def __str__(self):
      return "Title:%s , author:%s, pages:%s " % \
              (self.title, self.author, self.pages)

   def __len__(self):
      return self.pages

   def __del__(self):
      print "A book is destroyed"

book = Book("Inside Steve's Brain", "Leander Kahney", 304)

print book
print len(book)
del book


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
In this code example, we have a Dog class. The special method init() is called automatically right after the object has been created.

def init(self, name):

Each method in a class definition begins with a reference to the instance object . It is by convention named self. There is nothing special about the self name. We could name it this, for example. The name is the argument. The value is passed during the class instantiation.

self.name = name

Here we pass an attribute to an instance object.

phydoux = Dog('Phydoux')
lucky = Dog('Lucky')

Here we instantiate two objects: dogs Phydoux and Lucky. The number of arguments must correspond to the __init__() method of the class definition. The 'Phydoux' and 'Lucky' strings become the name parameter of the __init__() method.
The attributes can be assigned dynamically, not just during initialization. This shows the next example.
Can you write Python code for it?

class Dynamic:
   pass

d = Dynamic()
d.name = "Dynamic"
print d.name


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
In this example, we have two classes: Animal and Dog. The Animal is the base class, the Dog is the derived class. The derived class inherits the functionality of the base class. It is shown by the eat() method. The derived class modifies existing behaviour of the base class, shown by the whoAmI() method. Finally, the derived class extends the functionality of the base class, by defining a new bark() method.
We put the ancestor classes in round brackets after the name of the descendant class. If the derived class provides its own __init__() method, it must explicitly call the base class __init__() method.
Polymorphism
The polymorphism is the process of using an operator or function in different ways for different data input. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.
Can you write Python code for it?

class Animal:
   def __init__(self, name=''):
      self.name = name

   def talk(self):
      pass

class Cat(Animal):
   def talk(self):
      print "Meow!"

class Dog(Animal):
   def talk(self):
      print "Woof!"

a = Animal()
a.talk()

c = Cat("Missy")
c.talk()

d = Dog("Rocky")
d.talk()


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
Note, that if you try accessing ____private__ from outside the class, you’ll get an error. But you can still stumble upon something like this:
Can you write Python code for it?


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
Python programs can have different styles

Simple, short scripts can just be straight-line code -- Do this one thing! BAM!
A somewhat longer task might have a recurring sub-task: a function
If more functions appear, they might be grouped into one file (a module)
Classes come in when higher levels of structure are needed ...

Ok ... so what's a Class?

A "Class" is a code structure that contains ...

Data in the form of Fields, a.k.a. "attributes", and
Procedures, a.k.a. "methods"

One way to think of these is:

An object's attributes represent its state
Its methods represent its behavior

Everything in Python is an object. Objects are the basic building blocks of a Python OOP program.
When should I write my own classes?

When you have some functions that are passing one or more parameters around
When you have some functions that need to access the same "state" information
When you are developing a Graphical User Interface (GUI)
When you need to represent and process some highly structured data, e.g.:
a document or web page
data about a real-world "thing" (either man-made or natural)

The class keyword
The previous objects were mostly built-in objects of the Python programming language. The user defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. From classes we construct instances. An instance is a specific object created from a particular class. For example, Phydoux might be an instance of a Dog class.
Can you write Python code for it?

# first.py

class First:
   pass

fr = First()

print type(fr)
print type(First)


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
The following interactive sessions shows the behaviour of public, protected and private members:
Can you write Python code for it?

x = Encapsulation(11,13,17)

x.public

x._protected

x._protected = 23

x._protected

x.__private


I am working on the file "Day_02/01_ObjectOrientedProgramming/00_Object_Oriented_Programming.ipynb".
The first task is:
We define and create an empty Dynamic class.  The line of code:

d.name = "Dynamic"

creates a new name attribute, which is very different that other languages where you have to declaire the variables up front.
Methods
Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods are essential in encapsulation concept of the OOP paradigm. For example, we might have a connect() method in our AccessDatabase class. We need not to be informed how exactly the method connect connects to the database. We only know that it is used to connect to a database. This is essential in dividing responsibilities in programming, especially in large applications.
Can you write Python code for it?

class Circle:
   pi = 3.141592

   def __init__(self, radius=1):
      self.radius = radius

   def area(self):
      return self.radius * self.radius * Circle.pi

   def setRadius(self, radius):
      self.radius = radius

   def getRadius(self):
      return self.radius

c = Circle()

c.setRadius(5)
print c.getRadius()
print c.area()


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Assert invariants
Give immediate feedback -- most implementations halt the system.  These are unrecoverable errors.
Many languages allow them to be turned on/off at either compile or run time.
Can you write Python code for it?

i=8
if i%3 == 0:
    print 1
elif i%3 == 1:
    print 2
else:
    assert i%3 == 2
    print 3


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Example
Let's make a factorial function.
Can you write Python code for it?

%%file myfactorial.py

def factorial2(n):
    """ Details to come ...
    """

    raise NotImplementedError

def test():
     from math import factorial
     for x in range(10):
         print ".",
         assert factorial2(x) == factorial(x), \
                "My factorial function is incorrect for n = %i" % x


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Exceptions

Caught during run-time
Can you write Python code for it?


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Let's test it again...
Can you write Python code for it?


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
NumPy Testing
Can you write Python code for it?

import numpy as np
np.testing.assert_almost_equal(2, math.sqrt(2)**2)

x=1.000001
y=1.000002
np.testing.assert_almost_equal(x,y, decimal=5)


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Raising exceptions
Can you write Python code for it?

def newfunction():
    raise NotImplementedError

newfunction()


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
Test as you code
Type checking
Can you write Python code for it?

#i=raw_input("Please enter an integer: ")
i="123"
if not isinstance(i,int):
    print "Casting ", i, " to integer."
    i=int(i)
else:
    print "already an int:",i


I am working on the file "Day_02/02_Development_II/02_Development_II.ipynb".
The first task is:
What if we consider x and y almost equal?  Can we modify our assertion?
Can you write Python code for it?


%%file bats_tap_ex

@test "no arguments prints usage instructions" {
  run bats
  [ $status -eq 1 ]
  [ $(expr "${lines[1]}" : "Usage:") -ne 0 ]
}

@test "-v and --version print version number" {
  run bats -v
  [ $status -eq 0 ]
  [ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ]
}

!bats bats_tap_ex --tap


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
An important aside: conflict management
While git is very good at merging, if two different branches modify the same file in the same location, it simply can't decide which change should prevail.  At that point, human intervention is necessary to make the decision.  Git will help you by marking the location in the file that has a problem, but it's up to you to resolve the conflict.  Let's see how that works by intentionally creating a conflict.
We start by creating a branch and making a change to our experiment file:
Can you write Python code for it?

%%bash
cd test

git branch trouble
git checkout trouble
echo "This is going to be a problem..." >> experiment.txt
git commit -a -m"Changes in the trouble branch"


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
And git rm works in a similar fashion.
Local user, branching
What is a branch?  Simply a label for the 'current' commit in a sequence of ongoing commits:

There can be multiple branches alive at any point in time; the working directory is the state of a special pointer called HEAD.  In this example there are two branches, master and testing, and testing is the currently active branch since it's what HEAD points to:

Once new commits are made on a branch, HEAD and the branch label move with the new commits:

This allows the history of both branches to diverge:

But based on this graph structure, git can compute the necessary information to merge the divergent branches back and continue with a unified line of development:

Let's now illustrate all of this with a concrete example.  Let's get our bearings first:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
And how you will edit text files (it will often ask you to edit messages and other information, and thus wants to know how you like to edit your files):
Can you write Python code for it?

%%bash
# Put here your preferred editor. If this is not set, git will honor
# the $EDITOR environment variable
git config --global core.editor /usr/bin/nano  # Yes, I still use nano

# On Windows Notepad will do in a pinch, I recommend Notepad++ as a free alternative
# On the mac, you can set nano or emacs as a basic option

# And while we're at it, we also turn on the use of color, which is very useful
git config --global color.ui "auto"


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
And now we can ask git what is different:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
And now we go back to the master branch, where we change the same file:
Can you write Python code for it?

%%bash
cd test

git checkout master
echo "More work on the master branch..." >> experiment.txt
git commit -a -m"Mainline work"


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
At this point, we go into the file with a text editor, decide which changes to keep, and make a new commit that records our decision.  I've now made the edits, in this case I decided that both pieces of text were useful, but integrated them with some changes:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Github offers in its help pages instructions on how to configure the credentials helper for Linux and Windows.
Stage 1: Local, single-user, linear workflow
Simply type git to see a full list of all the 'core' commands.  We'll now go through most of these via small practical exercises:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
In the commit above, we  used the -m flag to specify a message at the command line.  If we don't do that, git will open the editor we specified in our configuration above and require that we enter a message.  By default, git refuses to record changes that don't have a message to go along with them (though you can obviously 'cheat' by using an empty or meaningless string: git only tries to facilitate best practices, it's not your nanny).
git log: what has been committed so far
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Let's see the remote situation again:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Let's see what git has put into our file:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Let's then make our new commit:
Can you write Python code for it?

%%bash
cd test

git commit -a -m"Completed merge of trouble, fixing conflicts along the way"
git slog


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Note that these changes must be committed too, to become permanent!  In git's world, until something hasn't been committed, it isn't permanently recorded anywhere.
Can you write Python code for it?

%%bash
cd test

git commit -a -m"I like this new name better"
echo "Let's look at the log again:"
git slog


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Now let's fetch that work from machine #1:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Now we put this new work up on the github server so it's available from the internet
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Set git to use the credential memory cache so we don't have to retype passwords too frequently. On OSX, you should run the following (note that this requires git version 1.7.10 or newer):
Can you write Python code for it?

%%bash
git config --global credential.helper osxkeychain
# Set the cache to timeout after 2 hours (setting is in seconds)
git config --global credential.helper 'cache --timeout=7200'


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Since the above cell didn't produce any output after the git remote -v call, it means we have no remote repositories configured.  We will now proceed to do so.  Once logged into GitHub, go to the new repository page and make a repository called test.  Do not check the box that says Initialize this repository with a README, since we already have an existing repository here.  That option is useful when you're starting first at Github and don't have a repo made already on a local computer.
We can now follow the instructions from the next page:
Can you write Python code for it?

%%bash
cd test

git remote add origin https://github.com/kialio/test.git

%%bash
cd test

git push -u origin master


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
So now let's see what happens if we try to merge the trouble branch into master:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
Sometimes it's handy to see a very summarized version of the log:
Can you write Python code for it?

%%bash
cd test

git log --oneline --topo-order --graph


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
We can now ask git about what happened with status:
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
We can now see this repository publicly on github.
Let's see how this can be useful for backup and syncing work between two different computers.  I'll simulate a 2nd computer by working in a different directory...
Can you write Python code for it?

%%bash

# Here I clone my 'test' repo but with a different name, test2, to simulate a 2nd computer
git clone https://github.com/kialio/test.git test2
cd test2
pwd
git remote -v


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
git add: tell git about this new file
Can you write Python code for it?


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
git commit: permanently record our changes in git's database
For now, we are always going to call git commit either with the -a option or with specific filenames (git commit file1 file2...).  This delays the discussion of an aspect of git called the index (often referred to also as the 'staging area') that we will cover later.  Most everyday work in regular scientific practice doesn't require understanding the extra moving parts that the index involves, so on a first round we'll bypass it.  Later on we will discuss how to use it to achieve more fine-grained control of what and how git records our actions.
Can you write Python code for it?

%%bash
cd test

git commit -a -m"This is our first commit"


I am working on the file "Day_02/02_GitDevelopment/VersionControl.ipynb".
The first task is:
git log revisited
First, let's see what the log shows us now:
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
A $\bf postcondition$ is a system state that is guaranteed to be true after a function finishes executing. Postconditions should be placed after all state-changing expressions. They give the user peace of mind that the function has, in fact, fufilled its promises and things may proceed.
Can you write Python code for it?

NumberTypes = (int, long, float)

def K2C(tK):
    """
    Convert degrees K to C.
    Argument: tK is a temperature in degrees K.
    Preconditions: (tK is int or long int or float) and (tK > 0)
    Postconditions: (result is a float in Celsius)
    """
    assert isinstance(tK, NumberTypes), 'tK must be numeric'
    assert tK > 0, 'K2C: tK is negative, which is not allowed'

    result = tK - 273.15 # conversion to Celsius

    assert isinstance(result, float), 'the result must be a float'
    return result

K2C(288)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Assertions, preconditions, postconditions
A $\bf precondition$ is a logical assertion of some system state that must be true (or false) before some other code block can be executed correctly.
Functions can be written to validate their inputs, to determine whether they satisfy all the algorithm's preconditions. In python, this test can be done using if-statements for very complicated preconditions, but simpler cases can be handled using the assert($...$) statement.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
At first, recursion seems like complete magick. It's as if the program DOESN'T DO ANYTHING yet it computes the answer. There are no iteration variables, no intermediate results (it seems) -- just the answer. But a little reflection on how python executes this function might shed some light. There are intermediate results that are held on the execution stack. Those results are the values the variable n is given each time TOTAL is called. (Remember that n is $\it local$ to the instance of its invocation, so every invocation of TOTAL gets its own private copy of n. For TOTAL(3) the execution stack looks like this
TOTAL(3) = TOTAL(2) + 3
TOTAL(2) = TOTAL(1) + 2
TOTAL(1) = TOTAL(0) + 1
TOTAL(0) = 0 so
TOTAL(1) = TOTAL(0) + 1 = 0 + 1 = 1
TOTAL(2) = TOTAL(1) + 2 = 1 + 2 = 3
TOTAL(3) = TOTAL(2) + 3 = 3 + 3 = 6
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Base cases
All recursive functions have a $\bf base$ $\bf case$. A base case is a value (or values) the function returns without needing to resort to deeper recursion. Without a base case, the recursion never stops and the program gets in to an infinite loop. In the summation problem, the base case was zero. In the Fibonnaci problem, the base cases were one and zero. So, when you're designing a recursive algorithm, make sure you recognize the base case(s) and include in your code.
Exercise
Write two functions to compute the factorial function x!
$x!n = x!{n-1} \times n$
Write your first function using a For loop. Write your second function using recursion.
Harder exercise
Write a function to produce a Fibonnaci number without using recursion.
Capstone Example: Arclength
In summary, to this point we've seen how assertions and mindful documentation improve code reliability and readability, how we can write functions that take functions as arguments allows us to write very general-purpose code, and how recursion works. However, although the previous examples might be helpful to see how recursion works, they are possibly not completely convincing that it makes (at least some) tasks easier. This notebook concludes with a non-trivial example of a real-world numerical methods problem. We shall write a function that computes the arclength of an user-provided parametric curve with any finite number of dimensions.
Let's define arclength so we know what we're talking about. Arclength is the length a curve would have if all its curvature was removed without stretching it. If we had a real, physical curve (think about the railing of a spiral staircase), the arclength might be measured by laying a cloth tape on the curve and adding up the measures.
This intuitive definition leads to a formal analog. Like many other calculus problems, the approach is to approximate the exact with ever decreasing linear approximations that approach the curved reality in the limit. Here, we will approximate the arclengths of segments of a...
Can you write Python code for it?

x = np.linspace(0,2,50,endpoint=True)
plt.plot(x,x**2)
plt.xlabel('x')
plt.ylabel('y = $x^2$')
plt.title('A Parabola')


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Easy enough. To solve this problem recursively, let's write it as an iterated function, as above. Suppose we have a magick function named TOTAL(n) that returns the sum of $1...n$. Clearly
TOTAL(0) = 0
and we can solve the problem like this using the magick function
TOTAL(n) = TOTAL(n-1) + n
Can you write Python code for it?

def TOTAL(n):
    """
    compute the summation of [1...n]
    """
    if n == 0:
        return 0
    else:
        return TOTAL(n-1) + n

TOTAL(5)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Functions as arguments to other functions
Some functions take other functions as their arguments. For example, in SciPy there are functions that find minima of other functions, and (obviously) they need to know which function to work on.
scipy.optimize.minimize_scalar
So, let's load this to see how it works.
Can you write Python code for it?

import scipy
help(scipy.optimize.minimize_scalar)

'WAT '* 12


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Here's what the documentation string gives.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
If an assertion is true, nothing happens. All is well and execution proceeds without interuption.
If an assertion fails, python raises and exception.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
It happens that a closed-form solution exists for this problem: $s = \sqrt(17) + \frac{{\rm asinh} (4)}{4} \approx 4.64678$. Notice that the approximation is less than the true value. This is $always$ true (think about the triangle inequality), which means we can use that as an invariant in our code. The next iteration looks like this.
Can you write Python code for it?

x = np.linspace(0,2,50,endpoint=True)
x0 = 0
xm = 1
x1 = 2
plt.plot(x,x**2)
plt.plot([x0,xm,x1],[x0,xm**2,x1**2],'ro')
plt.plot([x0,xm,x1],[x0,xm**2,x1**2],'r-')
plt.xlabel('x')
plt.ylabel('y = $x^2$')
plt.title('A Parabola Approximated with a Two Line Segments')


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Let's plot a parametric function of two functions: $f(t) = {\sin t, \cos t}$ and, here, we'll use the numpy functions sin and cos
Can you write Python code for it?

def myPlot(xf, yf, npts = 50):
    T = np.linspace(0,1,npts)
    x = [xf(t) for t in T]
    y = [yf(t) for t in T]
    return plt.plot(x, y)

myPlot(np.sin, np.cos)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Parametric functions
We're heading towards writing a function, like minimize_scalar, that operates magickally on input functions. Our function will calculate the arclength of n-dimensional paramtetric curves. Super cool. So, let's review parametric functions now.
Let's plot a parametric function $f(t) = t^2$ for $t \in [0,1)$
Can you write Python code for it?

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

def myPlot(yf, npts = 50):
    T = np.linspace(0,1,npts)
    y = [yf(t) for t in T]
    return plt.plot(T, y)

myPlot(lambda t: t**2)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Recursion
Recursion is a programming style in which a problem is solved by first solving a smaller problem of the same type. Recursive problems often look like this:
$s_{i} = s_{i-1} + x$,
which means the next state $s_{i+1}$ equals the previous state $s_i$ plus something else that does not depend on the previous state $x$.
Example: find the sum of $n$ integers $1 + 2 + ...+ \, n$
A simple for loop does this easily.
Can you write Python code for it?

total = 0 # initialize the summand

n = 5

for i in range(1,n+1):
    total += i

total


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
Same code works for curves of any dimension
Can you write Python code for it?

arclenSegment([lambda t: t, lambda t: t**2, lambda t: t**3], 0.0, 2.0)

def arclen(curves, t0, t1, tol=0.001):
    assert(t0 < t1), 't0 must be less than t1'
    assert(tol > 0), 'tolerance must be positive'
    assert(len(curves) > 0), 'there must be at least one curve'

    dt = (t1 - t0)/2.0 # divide and conquer
    tm = t0 + dt
    if dt <= tol:
        return arclenSegment(curves, t0, t1)
    else:
        return arclen(curves, t0, tm, tol) + arclen(curves, tm, t1, tol)

arclen([lambda t: t, lambda t: t**2], 0.0, 2.0)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The arclength function takes an array of parametric lambda functions, t0, t1, and a convergence tolerance. Here's how we use a lambda function inside our routine.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The assertion statement has an optional additional expression that is printed by the exception.
Can you write Python code for it?

assert(8<5), 'wicked helpful assertion string'


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The beautiful thing about parametric functions is that they are functions (mathematically speaking) of the parameter, but not of the spatial coordinates. So we can have curves of any shape.
Can you write Python code for it?

T = np.linspace(0, 2*np.pi, 200)
pnts = np.array([parametricFunc(np.sin, np.cos, t) for t in T])
x=pnts[:,0]
y=pnts[:,1]
plt.plot(x,y)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The function itself is passed into weirdFunc. Here's other examples.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The issue here is the scipy is gignormous and it would take forever to load the whole thing -- and we only need the one routine. So, we need to load it specifically. Well, actually we load the optimize package in which the needed function resides.
Can you write Python code for it?

from scipy import optimize
help(scipy.optimize.minimize_scalar)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
The length of the approximation is $\sqrt((2-0)^2 + (4-0)^2)$
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
This agrees to the exact answer to at least five significant digits. Here's a three-dimensional helical spiral.
Can you write Python code for it?

arclen([lambda t: np.sin(2.0 * np.pi * t), lambda t: np.cos(2.0 * np.pi * t),lambda t: t,], 0.0, 2.0)


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
This alogrithm is wicked fast. If you try to compute f(1000) with the first-try implementation, python's stack goes too deep and throws and overflow exception -- it can't even solve the problem! Also notice how python quietly changed to arbitrary precision arithematic when the size of the number grew larger than what can be represented with machine arithemtic. Slicker than frog's fur.
Let's see how much faster this implementation is.
Can you write Python code for it?


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
This approximation is better than what we got from the first, as expected. To generalize,
$s \approx \sum_{i = 0}^{n-1} \sqrt( (x_{i+1} - x_i)^2 + (y_{i+1} - y_i)^2)$
in which the saavy reader will recognize Pythagoras's famous formula. In the limit we have
$s = \int_{t_0}^{t_1} \sqrt({\rm d} x^2 + {\rm d} y^2)$
Close inspection of this integral often invokes fear and loathing. Not only is it a single integral over two infintesimals, the infintesimals appear in a highly nonlinear way. This often leads to a hard calculus problem. By "hard" I mean "impossible". (For the mathematicians, try solving this integral for a circular quadrant to derive $p = 2\,\pi\, r$. Hint: remember the integral of the arcsine.)
Notice the integration limits. The defintion suggests that the infintesimals ought to be functions of the parameter $t$
$s = \int_{t_0}^{t_1} \sqrt({\rm d} x(t)^2 + {\rm d} y(t)^2)$
So, let's write a function that computes Pythagoras's formula from two points.
Can you write Python code for it?

NumberTypes = (int, long, float)
nptype = type(np.array([])) # let python tell me the name of the numpy array

def pythag(p1, p2):
    """
    Compute the straight-line distance from p1 to p2.
    Arguments:
        p1 is an numpy array of any dimension. It's elements are numeric.
        p2 is an numpy array of any dimension same as p1. It's elements are numeric.
    Preconditions:
        (p1 is not empty) and (p1's elements are numeric)
        (p2 is not empty) and (p2's elements are numeric) and (p2 has the same length as p1)
        p1 and p2 are one-dimensional (vectors, not matrices)
    Postcondition:
        (result is a non-negative) and (result is a float)
    """
    assert(isinstance(p1, nptype))
    assert(isinstance(p2, nptype))
    assert(len(p1) > 0)
    assert(len(p1) == len(p2))
    for i in range(len(p1)):
        assert(isinstance(p1[i], NumberTypes))
        assert(isinstance(p2[i], NumberTypes))

    result = np.sqrt(sum((p2 - p1)**2))

    assert(result >= 0) # this does both post conditions in one check
    return result

pythag(np.array([0,0]), np.array([2,4]))


I am working on the file "Day_02/07_WhiteBoard_III/Programming by contract, and functions of functions.ipynb".
The first task is:
We'll add in the type checking. It needs to go before the assertion about tK's value because that assertion itself would fail if tK isn't numeric!
Can you write Python code for it?

NumberTypes = (int, long, float)

def K2C(tK):
    """
    Convert degrees K to C.
    Argument: tK is a temperature in degrees K.
    Preconditions: (tK is an int or long int or float) and (tK > 0)
    """
    assert isinstance(tK, NumberTypes), 'tK must be numeric'
    assert tK > 0, 'tK is negative, which is not allowed'
    return tK - 273.15

K2C('hello world!')


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
Abusing this flexibility can lead to issues with code readibility – in fact, abusing white space is often one of the primary means of intentionally obfuscating code (which some people do for sport).
Using whitespace effectively can lead to much more readable code,
especially in cases where operators follow each other – compare the following two expressions for exponentiating by a negative number:
python
x=10**-2
to
python
x = 10 ** -2
I find the second version with spaces much more easily readable at a single glance.
Most Python style guides recommend using a single space around binary operators, and no space around unary operators.
We'll discuss Python's operators further in Basic Python Semantics: Operators.
Parentheses Are for Grouping or Calling
In the previous code snippet, we see two uses of parentheses.
First, they can be used in the typical way to group statements or mathematical operations:
Can you write Python code for it?


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
Python's use of meaningful whitespace leads to much more consistent and readable code than languages that do not enforce indentation of code blocks.
Whitespace Within Lines Does Not Matter
While the mantra of meaningful whitespace holds true for whitespace before lines (which indicate a code block), white space within lines of Python code does not matter.
For example, all three of these expressions are equivalent:
Can you write Python code for it?


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
The "()" after sort indicates that the function should be executed, and is required even if no arguments are necessary.
Strings!
One of the most simple and important aspects of python you will be using constantly, will be strings!
Strings are designated by single or double quotation marks.
``` python
"This is my python string"
'This is my second python string'
```
We will be using 'strings' throughout our whole python lifetime.
Things to remember:
1) Strings are not numbers. You'd have to convert them to a number.
Can you write Python code for it?

x = "4"
print("String: ", type(x),      x)

x = int(x)
print("Integer: ", type(x),    x)

x = float(x)
print("Float: ", type(x),    x)


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
The best way is to use the line continuation (within parentheses).
Semicolon Can Optionally Terminate a Statement
Sometimes it can be useful to put multiple statements on a single line.
The next portion of the script is
python
lower = []; upper = []
This shows the example of how the semicolon (;) familiar in C can be used optionally in Python to put two statements on a single line.
This is exactly the same as writing the below in python:
python
lower = []
upper = []
Using a semicolon to put multiple statements on a single line is generally discouraged by most Python style guides, though occasionally it proves convenient.
Indentation: Whitespace Matters!

we must use indentation (or more specific - 4 spaces!)

The top line will be the controlling statement.

indented code blocks are always preceded by a colon (:)

Anything indended below this top line will occur within it.

For example, the following two snippets will produce different results!
Can you write Python code for it?

x = 5
if x < 4:
    y = x * 2
    print(x)

x = 5
if x < 4:
    y = x * 2
print(x)


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
They can also be used to indicate that a function is being called.
Can you write Python code for it?

# example
L = [4,2,3,1]
L.sort()
print(L)


I am working on the file "Day_1/02-Basic-Python-Syntax.ipynb".
The first task is:
This script is a bit silly, but it compactly illustrates several of the important aspects of Python syntax.
Let's walk through it and discuss some of the syntactical features of Python
Comments Are Marked by #
The script starts with a comment (this means that python won't read this line....very useful!):
``` python
set the midpoint
`
Comments in Python are indicated by#``, and anything on the line following the numbers sign is ignored by the interpreter.
This means, for example, that you can have stand-alone comments like the one just shown, as well as inline comments that follow a statement. For example:
python
x += 2  # shorthand for x = x + 2
So above, Python reads the first bit of code, but ignores the 'comment'
End-of-Line Terminates a Statement
The next line in the script is
python
midpoint = 5
This is an assignment operation, where we've created a variable named midpoint and assigned it the value 5.
Notice that the end of this statement is simply marked by the end of the line.
In Python, if you'd like a statement to continue to the next line, it is possible to use the "\" marker to indicate this:
Can you write Python code for it?


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
Again, this makes perfect sense if you think of x and y as pointers, and the "=" operator as an operation that changes what the name points to.
You might wonder whether this pointer idea makes arithmetic operations in Python difficult to track, but Python is set up so that this is not an issue. Numbers, strings, and other simple types are immutable: you can't change their value – you can only change what values the variables point to.
So, for example, it's perfectly safe to do operations like the following:
Can you write Python code for it?

x = 10
y = x
x += 5  # add 5 to x's value, and assign it to x
print("x =", x)
print("y =", y)


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
Methods are like attributes, except they are functions that you can call using opening and closing parentheses.
For example, floating point numbers have a method called is_integer that checks whether the value is an integer:
Can you write Python code for it?

x = 4.5
x.is_integer()

x = 4.0
x.is_integer()


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
This behavior might seem confusing if you're wrongly thinking of variables as buckets that contain data.
But if you're correctly thinking of variables as pointers to objects, then this behavior makes sense.
Note also that if we use "=" to assign another value to x, this will not affect the value of y - assignment is simply a change of what object the variable points to:
Can you write Python code for it?

x = 'something else'
print(y)  # y is unchanged


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
We've created two variables x and y which both point to the same object.
Because of this, if we modify the list via one of its names, we'll see that the "other" list will be modified as well:
Can you write Python code for it?

print(y)

x.append(4) # append 4 to the list pointed to by x
print(y) # y's list is modified as well!


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
When we say that everything in Python is an object, we really mean that everything is an object – even the attributes and methods of objects are themselves objects with their own type information:
Can you write Python code for it?


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
While it might be expected for compound objects like lists to have attributes and methods, what is sometimes unexpected is that in Python even simple types have attached attributes and methods.
For example, numerical types have a real and imag attribute that returns the real and imaginary part of the value, if viewed as a complex number:
Can you write Python code for it?


I am working on the file "Day_1/03-Semantics-Variables.ipynb".
The first task is:
While users of statically-typed languages might miss the type-safety that comes with declarations like those found in FORTRAN or C, this dynamic typing is one of the pieces that makes Python so quick to write and easy to read.
Dangers of "variable as pointer" model
If we have two variable names pointing to the same mutable object, then changing one will change the other as well!
For example, let's create and modify a list:
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
Augmented assignment operator
|||||
|-|-|
|a += b| a -= b|a *= b| a /= b|
|a //= b| a %= b|a **= b|a &= b|
|a |= b| a ^= b|a <<= b| a >>= b|
For any operator "■", the expression a ■= b is equivalent to a = a ■ b.
One catch:
For mutable objects like lists, arrays, or DataFrames, these augmented assignment operations are subtly different: they modify the contents of the original object rather than creating a new object to store the result.
Comparison Operations
Another type of operation which can be very useful is comparison of different values.
For this, Python implements standard comparison operators, which return Boolean values True and False.
| Operation     | Description                       || Operation     | Description                          |
|---------------|-----------------------------------||---------------|--------------------------------------|
| a == b    | a equal to b              || a != b    | a not equal to b             |
| a < b     | a less than b             || a > b     | a greater than b             |
| a <= b    | a less than or equal to b || a >= b    | a greater than or equal to b |
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
Boolean Operations

Python provides operators to combine the values using the standard concepts of "and", "or", and "not".
They are literally called "and", "or", and "not".
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
Frist case: a and b point to different objects

Second case: they point to the same object.

As we saw in the previous section, Python variables are pointers. The "is" operator checks whether the two variables are pointing to the same container (object), rather than referring to what the container contains.
Often, you might be tempted to use "is" what they really mean is ==.
Membership operators
Membership operators check for membership within compound objects.
So, for example, we can write:
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
We can string-together multiple comparisons to check more complicated relationships:
Can you write Python code for it?

# check if a is between 15 and 30
a = 25
15 < a < 30


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
We can use these variables in expressions with any of the operators mentioned earlier.
For example, to add 2 to a we write:
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
We might want to update the variable a with this new value, e.g. a = a + 2.
Python includes built-in update operators for all of the arithmetic operations:
Can you write Python code for it?

a += 2  # equivalent to a = a + 2
print(a)


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
We will come back to the Boolean operations in the control flow statements section.
Boolean vs bitwise operators
When to use Boolean operators (and, or, not), and when to use bitwise operations (&, |, ~)?

Boolean operators: when you compute Boolean values (i.e., truth or falsehood) of entire statements.
Bitwise operations: when you operate on individual bits or components of the objects in question.

Identity and Membership Operators
Like and, or, and not, Python also contains prose-like operators  to check for identity and membership.
They are the following:
| Operator      | Description                                       |
|---------------|---------------------------------------------------|
| a is b    | True if a and b are identical objects     |
| a is not b| True if a and b are not identical objects |
| a in b    | True if a is a member of b                |
| a not in b| True if a is not a member of b            |
Identity Operators: "is" and "is not"
The identity operators, "is" and "is not" check for object identity.
Object identity is different than equality, as we can see here:
Can you write Python code for it?


I am working on the file "Day_1/04-Semantics-Operators.ipynb".
The first task is:
What do identical objects look like? Here is an example:
Can you write Python code for it?


I am working on the file "Day_1/05-Built-in-Scalar-Types.ipynb".
The first task is:
Alternatively, we can use the "j" suffix in expressions to indicate the imaginary part:
Can you write Python code for it?