from IPython.display import Image
Image("IPy_header.png")
My life trajectory (physically) so far: http://bl.ocks.org/jbwhit/d03db0ca560da7c445f7.
%run talktools
cd
or ls
never used linuxThe ipython notebook runs a webserver locally on your computer. Which you connect to via your browser. The browser then runs a python kernel so you execute actual python code in your browser.
This sounds like a gimmick. It is not.
This presentation includes examples from many places, including a few from a talk that I gave with David Lagattuta at Swinburne last year.
ipython notebook + gist.github.com + nbviewer == easily shareable and reproducible work
Email that link to anyone in the world who has a browser. No python, no ipython, nothing needs to be installed. The barrier to sharing the analysis here is about as close to zero as we can get.
First of all -- don't try to take notes, just let the amazing wash over you.
Several Ideas and Themes
As Steven said, it's important to know your instrument.
Example -- bash and latex
# Create a [list]
days_of_the_week = ['Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday', ]
days_of_the_week
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Simple for-loop
for day in days_of_the_week:
print day
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
# Double for-loop
for day in days_of_the_week:
for letter in day:
print letter,
M o n d a y T u e s d a y W e d n e s d a y T h u r s d a y F r i d a y S a t u r d a y S u n d a y
for day in days_of_the_week:
for letter in day:
print letter.lower(),
m o n d a y t u e s d a y w e d n e s d a y t h u r s d a y f r i d a y s a t u r d a y s u n d a y
letters = [letter for day in days_of_the_week
for letter in day]
letters = [letter for day in days_of_the_week for letter in day]
print letters
['M', 'o', 'n', 'd', 'a', 'y', 'T', 'u', 'e', 's', 'd', 'a', 'y', 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y', 'F', 'r', 'i', 'd', 'a', 'y', 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y', 'S', 'u', 'n', 'd', 'a', 'y']
sorted_letters = sorted([x.lower() for x in letters])
print sorted_letters
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'e', 'e', 'e', 'f', 'h', 'i', 'm', 'n', 'n', 'n', 'o', 'r', 'r', 'r', 's', 's', 's', 's', 's', 't', 't', 't', 'u', 'u', 'u', 'u', 'w', 'y', 'y', 'y', 'y', 'y', 'y', 'y']
unique_sorted_letters = sorted(set(sorted_letters))
print "There are", len(unique_sorted_letters), "unique letters in the days of the week."
print "They are:", ''.join(unique_sorted_letters)
There are 15 unique letters in the days of the week. They are: adefhimnorstuwy
def first_three(input_string):
"""Takes an input string and returns the first 3 characters."""
return input_string[:3]
[first_three(day) for day in days_of_the_week]
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
def last_N(input_string, number=2):
"""Takes an input string and returns the last N characters."""
return input_string[-number:]
[last_N(day, 153) for day in days_of_the_week if len(day) > 6]
['Tuesday', 'Wednesday', 'Thursday', 'Saturday']
from math import pi
print [str(round(pi, i)) for i in xrange(2, 9)]
['3.14', '3.142', '3.1416', '3.14159', '3.141593', '3.1415927', '3.14159265']
list_of_lists = [[i, round(pi, i)] for i in xrange(2, 9)]
print list_of_lists
[[2, 3.14], [3, 3.142], [4, 3.1416], [5, 3.14159], [6, 3.141593], [7, 3.1415927], [8, 3.14159265]]
You know you're doing it wrong when it starts to look like C.
There are generally good reasons that things become "best practices" -- and just because you don't see the reasons for it, doesn't mean you shouldn't adopt them.
Style Guide for Python Code: http://www.python.org/dev/peps/pep-0008/
# Let this be a warning to you!
# If you see python code like the following in your work:
for x in range(len(list_of_lists)):
print "Decimals:", list_of_lists[x][0],
print "expression:", list_of_lists[x][1]
Decimals: 2 expression: 3.14 Decimals: 3 expression: 3.142 Decimals: 4 expression: 3.1416 Decimals: 5 expression: 3.14159 Decimals: 6 expression: 3.141593 Decimals: 7 expression: 3.1415927 Decimals: 8 expression: 3.14159265
# Change it to look more like this:
for decimal, rounded_pi in list_of_lists:
print "Decimals:", decimal, "expression:", rounded_pi
print list_of_lists
Decimals: 2 expression: 3.14 Decimals: 3 expression: 3.142 Decimals: 4 expression: 3.1416 Decimals: 5 expression: 3.14159 Decimals: 6 expression: 3.141593 Decimals: 7 expression: 3.1415927 Decimals: 8 expression: 3.14159265 [[2, 3.14], [3, 3.142], [4, 3.1416], [5, 3.14159], [6, 3.141593], [7, 3.1415927], [8, 3.14159265]]
You won't regret it.
As scientists, we're often going to need to plot results and use numbers to do analysis.
import numpy as np
import matplotlib.pyplot as plt
# The following line is an ipython notebook trick
%matplotlib inline
plt.rcParams['figure.figsize'] = 12, 8 # plotsize
x = np.arange(10000)
print "x -> ", x # notice the smart printing
print "x[:] -> ", x[:]
print "x[0] -> ", x[0] # first element
print "x[0:5] -> ", x[0:5] # first 5 elements
print "x[-1] -> ", x[-1] # last element
x -> [ 0 1 2 ..., 9997 9998 9999] x[:] -> [ 0 1 2 ..., 9997 9998 9999] x[0] -> 0 x[0:5] -> [0 1 2 3 4] x[-1] -> 9999
# A bit more complicated slicing
print x[-5:] # last five elements
print x[-5:-2] #
print x[-5:-1] # last 4 elements (not final value)
[9995 9996 9997 9998 9999] [9995 9996 9997] [9995 9996 9997 9998]
# Single physical cloud with following physical parameters
def GaussFunc(x, amplitude, centroid, sigma):
"""Takes an array, and calculates a Gaussian with the following parameters. """
return amplitude * np.exp(-0.5 * ((x - centroid) / sigma)**2)
feature_centroid = 5315.3
feature_amplitude = 2.3
feature_sigma = 1.5
wavelength = np.linspace(5305., 5330., 120)
tau = GaussFunc(wavelength, feature_amplitude, feature_centroid, feature_sigma)
flux = np.exp(-tau)
sigma = 0.05
noise = np.random.randn(len(flux)) * sigma
observed = flux + noise
error = np.ones_like(wavelength) * sigma
plt.plot(wavelength, flux)
plt.plot(wavelength, observed)
[<matplotlib.lines.Line2D at 0x104701b90>]
np.savetxt("example.txt", np.transpose((wavelength, observed, flux, error, noise)))
# np.random.randn?
%less example.txt
wave = []
observed_flux = []
error = []
for line in open("example.txt", 'r'):
wave.append(line[0])
observed_flux.append(line[1])
error.append(line[3])
wave[:10] # Whoops!
['5', '5', '5', '5', '5', '5', '5', '5', '5', '5']
wave = []
observed_flux = []
error = []
for line in open("example.txt", 'r'):
line = line.split()
wave.append(line[0])
observed_flux.append(line[1])
error.append(line[3])
wave[:10] # Dang! still strings!
['5.305000000000000000e+03', '5.305210084033613384e+03', '5.305420168067226768e+03', '5.305630252100840153e+03', '5.305840336134453537e+03', '5.306050420168066921e+03', '5.306260504201680305e+03', '5.306470588235293690e+03', '5.306680672268907983e+03', '5.306890756302521368e+03']
# If you see yourself doing this kind of thing...
wave = []
observed_flux = []
error = []
for line in open("example.txt", 'r'):
line = line.split()
wave.append(float(line[0]))
observed_flux.append(float(line[1]))
error.append(float(line[3]))
wave = np.array(wave)
observed_flux = np.array(observed_flux)
error = np.array(error)
# Do this instead
wave, observed_flux, error = np.loadtxt("example.txt", usecols=(0, 1, 3), unpack=True)
Look at the matplotlib gallery: http://matplotlib.org/gallery.html and find plots that are similar to what you want to do.
# Run this cell
%load http://matplotlib.org/mpl_examples/pylab_examples/polar_legend.py
# And it loads the code here, ready to run (obviously without this comment).
#!/usr/bin/env python
import numpy as np
from matplotlib.pyplot import figure, show, rc
# radar green, solid grid lines
rc('grid', color='#316931', linewidth=1, linestyle='-')
rc('xtick', labelsize=15)
rc('ytick', labelsize=15)
# force square figure and square axes looks better for polar, IMO
fig = figure(figsize=(8,8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
r = np.arange(0, 3.0, 0.01)
theta = 2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3, label='a line')
ax.plot(0.5*theta, r, color='blue', ls='--', lw=3, label='another line')
ax.legend()
show()
Image("../../../../Screenshots/Screenshot 2014-07-18 14.51.30.png")
Jonathan Whitmore