In [ ]:
pip install ipython-notebook
Create a local host for the notebook in the directory of interest by running the command:
In [ ]:
ipython notebook
Images, data files, etc. should be stored in a subdirectory of the Notebook host.
IPython Notebook: https://github.com/jvkersch/python-tutorial-files
An overview of basic Python syntax and data structures, including:
In [ ]:
# Mutable objects can be changed in place (e.g. lists),
# Immutable objects can NOT (e.g. ints, strings, tuples)
tup = ('a','0','@')
tup
In [ ]:
tup[0] = 8
In [ ]:
record = {}
record['first'] = 'Alan'
record['last'] = 'Turing'
record
In [ ]:
record.update({'workplace':'Bletchley Park'})
record
In [ ]:
# Comma after print statement removes implicit \n, prints to same line
for x in range(0,4):
print x,
In [ ]:
# xrange(#) more efficient than range(#), because:
# range() creates the whole sequence of numbers,
# while xrange() creates them as needed!
%timeit range(1000000)
In [ ]:
%timeit xrange(1000000)
In [ ]:
# Namespaces are evil
pi = 3.14
from numpy import pi
pi
cf. http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions
We can get help for a built-in Python function, such as range, with a single question mark:
In [ ]:
range?
...and we can read the source code of built-in functions by downloading the source from the Python.org Mercurial repositories): https://hg.python.org/
In [ ]:
# import inspect
# inspect.getsourcefile(range) # doesn't work for built-in functions
In [ ]:
# Python's built-in "Counter" class defines the control flow of iterators,
# used in functions such as "for i in range(0,10) ..."
class Counter(object):
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
'Returns itself as an iterator object'
return self
def __next__(self):
'Returns the next value till current is lower than high'
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
We can view the source code for a particular function, such as range, in the Python.org Mercurial Repository: https://hg.python.org/cpython/file/c6880edaf6f3/Objects/rangeobject.c
In [ ]:
for i in range(0,10):
print i*i
In [ ]:
# List comprehension (with filter)
[a*2 for a in range(0,10) if a>3]
In [ ]:
# The Zen of Python
import this
IPython Notebook: https://github.com/leriomaggio/numpy_euroscipy2015
In [ ]:
# We can infer the data type of an array structure (but not of int, list, etc.)
import numpy as np
a = np.array([1, 2, 3], dtype=np.int16)
a.dtype
In [ ]:
arev = a[::-1]
arev
In [ ]:
# Typecast variables into float, complex numbers,
b = np.float64(64)
c = np.complex(b)
print "R(c) = ", c.real
print "I(c) = ", c.imag
In [ ]:
# Specify type of array elements
x = np.ones(4, 'int8')
x
In [ ]:
# Wrap-around
x[0] = 256
x
In [ ]:
# Define a new record and create an array of corresponding data types
rt = np.dtype([('artist', np.str_, 40),('title', np.str_, 40), ('year', np.int16)])
music = np.array([('John Cage','4\'33\'\'',1952)], dtype=rt)
music
In [ ]:
# Flatten a matrix into a 1-D array
r = np.array([[1, 2, 3], [4, 5, 6]])
r.ravel()
In [ ]:
# Save a .csv file using arbitrary precision
M = np.random.rand(3,3)
np.savetxt("data/random-matrix2.csv", M, fmt='%.5f')
In [ ]:
# Create a matrix using list comprehension
coolmx = np.array([[10*j+i for i in range(6)] for j in range(6)])
coolmx
@ https://github.com/leriomaggio/numpy_euroscipy2015/blob/master/05_Memmapping.ipynb
In [ ]:
# Machine Learning in Python is a oneliner!
centroids, variance = vq.kmeans(data, 3)
#... after preparing the data and importing the scikit-learn package
"...a quick reference for new and old users and to provide also a set of exercices for those who teach."
@ https://github.com/pydata/numexpr (previously @ https://code.google.com/p/numexpr/)
JIT (Just-in-time) compilation for significant speed-up of numerial calculations
Multithreading to make use of multiple CPU cores
Can be used to evaluate expressions in NumPy and Pandas
cf. https://github.com/leriomaggio/numpy_euroscipy2015/blob/master/06_Numexpr.ipynb
In [ ]:
import numexpr as ne
import numpy as np
a = np.arange(1e8)
b = np.arange(1e8)
print "NumPy >> "
%timeit a**2 + b**2 + 2*a*b
In [ ]:
print "NumExpr >> "
%timeit ne.evaluate('a**2 + b**2 + 2*a*b')
https://www.euroscipy.org/2015/schedule/presentation/17/
@ https://github.com/ReScience/ReScience/wiki
"...an article about computational result is advertising, not scholarship. The actual scholarship is the full software environment, code and data that produced the result." (Buckheit & Donoho, 1995)
Papers are submitted to GitHub and can be reproduced by making a Pull Request from the ReScience repository.
The problem of reproducible science has long been a problem... https://github.com/ReScience/rescience.github.io/blob/master/00-about.md ...but now we have a potential solution.
https://www.euroscipy.org/2015/schedule/presentation/6/
https://github.com/hplgit/SpectralDNS-paper
https://www.euroscipy.org/2015/schedule/presentation/19/
Query data while an experiment is running.