import time
import platform
print('Last updated: %s' %time.strftime('%d/%m/%Y'))
print('Created using Python', platform.python_version())
Last updated: 27/06/2014 Created using Python 3.4.1
%%file hello.py
def func_inside_script(x, y):
return x + y
print('Hello World')
Writing hello.py
We can run Python scripts in IPython via the %run magic command. For example, the Python script that we created in the Writing local files section.
%run hello.py
Hello World
func_inside_script(1, 2)
3
%timeit [x**2 for x in range(100)]
10000 loops, best of 3: 38.8 µs per loop
%timeit -r 5 -n 100 [x**2 for x in range(100)]
100 loops, best of 5: 39 µs per loop
By prepending a "!
" we can conveniently execute most of the system shell commands, below are just a few examples.
my_dir = 'new_dir'
!mkdir $my_dir
!pwd
!touch $my_dir'/some.txt'
!ls -l './new_dir'
!ls -l $my_dir | wc -l
/Users/sebastian/Desktop total 0 -rw-r--r-- 1 sebastian staff 0 Jun 27 10:11 some.txt 2
def some_func():
var = 'hello world'
for i in range(5):
print(i)
i / 0
return 'finished'
%debug
some_func()
> <ipython-input-1-3d5f00f75cf4>(5)some_func() 4 print(i) ----> 5 i / 0 6 return 'finished'
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
import math
def pdf(x, mu=0, sigma=1):
"""Calculates the normal distribution's probability density
function (PDF).
"""
term1 = 1.0 / ( math.sqrt(2*np.pi) * sigma )
term2 = np.exp( -0.5 * ( (x-mu)/sigma )**2 )
return term1 * term2
x = np.arange(0, 100, 0.05)
pdf1 = pdf(x, mu=5, sigma=2.5**0.5)
pdf2 = pdf(x, mu=10, sigma=6**0.5)
plt.plot(x, pdf1)
plt.plot(x, pdf2)
plt.title('Probability Density Functions')
plt.ylabel('p(x)')
plt.xlabel('random variable x')
plt.legend(['pdf1 ~ N($\mu=5$, $\sigma=2.5$)', 'pdf2 ~ N($\mu=10$, $\sigma=6$)'], loc='upper right')
plt.ylim([0,0.5])
plt.xlim([0,20])
plt.show()
Cython (see Cython's C-extensions for Python) is basically a hybrid between C and Python and can be pictured as compiled Python code with type declarations. Since we are working in an IPython notebook here, we can make use of the very convenient IPython magic: It will take care of the conversion to C code, the compilation, and eventually the loading of the function. Also, we are adding C type declarations; those type declarations are not necessary for using Cython, however, it will improve the performance of our code significantly.
%load_ext cythonmagic
%%cython
import numpy as np
cimport numpy as np
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef cython_lstsqr(x_ary, y_ary):
""" Computes the least-squares solution to a linear matrix equation. """
cdef double x_avg, y_avg, var_x, cov_xy,\
slope, y_interc, temp
cdef double[:] x = x_ary # memoryview
cdef double[:] y = y_ary
cdef unsigned long N, i
N = x.shape[0]
x_avg = 0
y_avg = 0
for i in range(N):
x_avg += x[i]
y_avg += y[i]
x_avg = x_avg/N
y_avg = y_avg/N
var_x = 0
cov_xy = 0
for i in range(N):
temp = (x[i] - x_avg)
var_x += temp**2
cov_xy += temp*(y[i] - y_avg)
slope = cov_xy / var_x
y_interc = y_avg - slope*x_avg
return (slope, y_interc)
building '_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df' extension C compiler: /usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/sebastian/miniconda3/envs/py34/include -arch x86_64 compile options: '-I/Users/sebastian/miniconda3/envs/py34/lib/python3.4/site-packages/numpy/core/include -I/Users/sebastian/miniconda3/envs/py34/include/python3.4m -c' clang: /Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.c /usr/bin/clang -bundle -undefined dynamic_lookup -L/Users/sebastian/miniconda3/envs/py34/lib -arch x86_64 /Users/sebastian/.ipython/cython/Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.o -L/Users/sebastian/miniconda3/envs/py34/lib -o /Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.so
import numpy as np
x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])
y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])
cython_lstsqr(x_ary, y_ary)
(1.1399825800539194, 2.0824398156005444)
There is also a convenient IPython magic command for compiling Fortran code. The Fortran magic uses NumPy's F2PY module for compiling and running the Fortran code. For more information, please see the 'Fortran magic's documentation'.
%install_ext https://raw.github.com/mgaitan/fortran_magic/master/fortranmagic.py
Installed fortranmagic.py. To use it, type: %load_ext fortranmagic
%load_ext fortranmagic
%%fortran
SUBROUTINE fortran_lstsqr(ary_x, ary_y, slope, y_interc)
! Computes the least-squares solution to a linear matrix equation. """
IMPLICIT NONE
REAL(8), INTENT(in), DIMENSION(:) :: ary_x, ary_y
REAL(8), INTENT(out) :: slope, y_interc
REAL(8) :: x_avg, y_avg, var_x, cov_xy, temp
INTEGER(8) :: N, i
N = SIZE(ary_x)
x_avg = SUM(ary_x) / N
y_avg = SUM(ary_y) / N
var_x = 0
cov_xy = 0
DO i = 1, N
temp = ary_x(i) - x_avg
var_x = var_x + temp**2
cov_xy = cov_xy + (temp*(ary_y(i) - y_avg))
END DO
slope = cov_xy / var_x
y_interc = y_avg - slope*x_avg
END SUBROUTINE fortran_lstsqr
Building module "_fortran_magic_a044885f2b0c0feac78a230b6b714e2b"... Constructing wrapper function "fortran_lstsqr"... slope,y_interc = fortran_lstsqr(ary_x,ary_y) Wrote C/API module "_fortran_magic_a044885f2b0c0feac78a230b6b714e2b" to file "/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2bmodule.c" Fortran 77 wrappers are saved to "/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2b-f2pywrappers.f"
import numpy as np
x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])
y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])
fortran_lstsqr(x_ary, y_ary)
(1.1313508052697814, 3.681685640167956)
To use any interpreter that is installed on your system:
%%script perl
print 'Hello, World!';
Hello, World!
Or use the magic command for the respective interpreter directly:
%%perl
print 'Hello, World!';
Hello, World!
%%ruby
puts "Hello, World!"
Hello, World!
%%bash
echo "Hello World!"
Hello World!
%%script R --no-save
cat("Goodbye, World!\n")
R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin10.8.0 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > cat("Goodbye, World!\n") Goodbye, World! >
def hello_world():
"""This is a hello world example function."""
print('Hello, World!')
%pdoc hello_world
%pdef hello_world
hello_world()
%psource math.mean()
Object `math.mean()` not found.
from math import sqrt
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-16-fdd1a06c836a> in <module>() ----> 1 from math import mean ImportError: cannot import name 'mean'