ipython: Future of Science

IPython shell

Our Top Suggestions to You

The ipython notebook is a new workbook type of document. Think of it a bit like a mathematica notebook, but improved in a number of ways. There are also a lot of tools that have come online recently that make sharing and tweaking existing notebooks simple and useful.

This notebook + more will be available after the talk.

Run with:

ipython notebook --pylab inline

$\LaTeX$ support

By the way, it supports inline $\LaTeX$ and full equations:

$$\alpha \equiv \frac{e^2}{\hbar c} $$

$$ x = a_0 + \cfrac{1}{a_1

      + \cfrac{1}{a_2
      + \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }$$

It's worth taking a look at the Wikipedia article on spectral lines if you're interested in getting an overview of the physics. It includes the helpful examples.

Continuous spectrum:

Emission spectrum:

Absorption spectrum:

Where the relationship between absorption and emission should be easily seen (they are the reverse of each other).

Of course, the real thing we see is much more complicated; it looks more like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent cursus sapien lacus, eu molestie elit. Ut congue, turpis ut volutpat eleifend, massa velit lobortis ipsum, id ullamcorper metus erat nec elit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis ut magna non turpis sollicitudin auctor dignissim vel nisi. Mauris lacinia turpis eget lacus convallis nec egestas neque faucibus. Nam rutrum cursus neque. Maecenas tortor felis, molestie et fermentum quis, vestibulum sed turpis. Suspendisse rutrum, lectus ut venenatis euismod, tortor risus cursus ante, a aliquam massa felis vel justo. Integer tincidunt quam at ante feugiat feugiat. Aenean facilisis facilisis arcu, quis laoreet nunc ornare sit amet. Nulla euismod est eget quam ultrices gravida. Nunc scelerisque tincidunt nisi, at faucibus orci sollicitudin a. Duis vitae pellentesque nisi.

Nunc sit amet turpis in justo adipiscing venenatis vel non felis. Sed nec justo augue, vel facilisis neque. Pellentesque mattis tristique lorem vitae consequat. Suspendisse risus neque, cursus non sagittis ac, consectetur quis lacus. Integer elementum, turpis in accumsan molestie, odio est adipiscing lectus, non pellentesque erat sem eu lacus. Praesent a justo massa, et ullamcorper elit. Nam imperdiet, arcu consequat tincidunt feugiat, enim metus ornare metus, sed fermentum erat augue ut sem. In nec libero tellus. Etiam elementum vulputate tempus. Etiam blandit odio et velit iaculis ac bibendum nisl rutrum. Suspendisse pretium rutrum tempor. Phasellus iaculis odio et velit malesuada accumsan. Nullam pharetra vestibulum turpis id molestie. Ut at libero at turpis porttitor pharetra. Quisque nec nunc nisi. Maecenas molestie viverra nulla.

Phasellus id adipiscing enim. Duis semper nulla vitae risus volutpat molestie. Integer tellus massa, blandit a tincidunt vel, cursus ut leo. Sed pellentesque dictum risus, ac sollicitudin quam aliquet sed. Praesent pretium vestibulum auctor. Mauris faucibus odio ac felis facilisis tempor. Proin eu magna vitae odio mollis facilisis at et est. Fusce elementum metus sit amet ligula rutrum ullamcorper.

Mauris in arcu non sapien fringilla ultricies et eu felis. Nulla hendrerit velit sed massa ornare eget venenatis mi tempor. Maecenas ut tellus lorem. Praesent eleifend nisl eu nibh consectetur nec sagittis felis ullamcorper. Quisque pharetra nunc ut sapien congue adipiscing convallis odio ultricies. Nullam vel sem turpis, eu venenatis leo. Integer rutrum, sapien iaculis malesuada tincidunt, lacus augue laoreet diam, non laoreet ante lectus eget lacus. Pellentesque egestas varius imperdiet. Vestibulum et mauris ante, sed auctor dolor. Maecenas porttitor faucibus tempor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Donec a ultrices ante. Maecenas dignissim vulputate scelerisque. Cras augue metus, sollicitudin et ornare venenatis, congue quis nulla. Phasellus at sem ac lectus laoreet luctus. Praesent sed lorem diam. Praesent ac ornare sem. Donec ut odio eu velit sodales tempor vel a sapien.

Libraries!

In [46]:
import numpy as np
import pylab as pl
In [49]:
print "hello hawthorn!"
hello hawthorn!
In [154]:
from IPython.display import YouTubeVideo
# look below!

Plotting!

In [155]:
#Here is a youtube video of Dave and I explaining the following code!

YouTubeVideo('1HSclOlW3nQ')
Out[155]:
In [88]:
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)
In [115]:
# Single physical cloud with following physical parameters

feature_centroid = 5315.3
feature_amplitude = 2.3
feature_sigma = 1.5


wavelength = np.linspace(5305., 5330., )
pl.plot(wavelength, GaussFunc(wavelength, feature_amplitude, feature_centroid, feature_sigma)) #show tab
Out[115]:
[<matplotlib.lines.Line2D at 0x10c7cbc50>]
In [135]:
# comments!
error = 0.05

exposure_one = {} # create a blank dictionary
exposure_one['wavelength'] = np.linspace(5305., 5330., num=40)
# smart line-continuation
exposure_one['flux'] = np.exp(-GaussFunc(exposure_one['wavelength'], feature_amplitude, feature_centroid, 
                              feature_sigma)) + np.random.normal(0, error, len(exposure_one['wavelength']))
exposure_one['error'] = np.ones_like(exposure_one['wavelength']) * error 
exposure_one['color'] = 'blue' 
exposure_one['label'] = 'exp 01'

exposure_two = {}
exposure_two['wavelength'] = np.linspace(5305., 5330., num=40)
exposure_two['flux'] = np.exp(-GaussFunc(exposure_two['wavelength'], feature_amplitude, feature_centroid, 
                      feature_sigma)) + np.random.normal(0, error, len(exposure_two['wavelength']))
exposure_two['error'] = np.ones_like(exposure_two['wavelength']) * error
exposure_two['color'] = 'green'
exposure_two['label'] = 'exp 02'
In [140]:
print exposure_two['flux']
[ 0.94499198  1.00954136  0.971235    1.01885901  0.98371828  1.01058178
  0.90712515  0.97644162  1.01851115  0.93210774  0.97991725  0.90388535
  0.52034844  0.27601661  0.24064394  0.05562805  0.1532537   0.09333654
  0.25329119  0.3874541   0.5320797   0.80127639  0.80326568  1.03400306
  1.07043673  0.89892248  1.00017245  1.05405606  0.94668394  0.98888399
  1.06374898  1.09581885  1.03638447  0.97075836  1.05025257  0.9900755
  0.97207472  1.00673306  1.03249482  0.96473522]
In [130]:
exposures = [exposure_one, exposure_two] # a list of dictionaries
In [141]:
exposures[0]
Out[141]:
{'color': 'blue',
 'error': array([ 0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,
        0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,
        0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,
        0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,  0.05,
        0.05,  0.05,  0.05,  0.05]),
 'flux': array([ 1.05794236,  1.03830009,  1.03500467,  0.99047029,  1.02907917,
        0.98033089,  0.96967209,  1.00930351,  1.04360832,  0.97732787,
        0.91578568,  0.82660863,  0.62253641,  0.30535047,  0.26436108,
        0.21307896,  0.08375021,  0.09149992,  0.14804059,  0.35441024,
        0.54166221,  0.82174282,  0.92167278,  0.94021463,  1.11553511,
        0.9480145 ,  0.98061066,  1.03047299,  1.0574776 ,  0.93308433,
        0.94931708,  1.08303421,  0.94899156,  0.99525836,  0.95467019,
        0.98808084,  0.99580691,  0.9336084 ,  0.95526332,  1.08318217]),
 'label': 'exp 01',
 'wavelength': array([ 5305.        ,  5305.64102564,  5306.28205128,  5306.92307692,
        5307.56410256,  5308.20512821,  5308.84615385,  5309.48717949,
        5310.12820513,  5310.76923077,  5311.41025641,  5312.05128205,
        5312.69230769,  5313.33333333,  5313.97435897,  5314.61538462,
        5315.25641026,  5315.8974359 ,  5316.53846154,  5317.17948718,
        5317.82051282,  5318.46153846,  5319.1025641 ,  5319.74358974,
        5320.38461538,  5321.02564103,  5321.66666667,  5322.30769231,
        5322.94871795,  5323.58974359,  5324.23076923,  5324.87179487,
        5325.51282051,  5326.15384615,  5326.79487179,  5327.43589744,
        5328.07692308,  5328.71794872,  5329.35897436,  5330.        ])}
In [132]:
pl.rcParams['figure.figsize'] = 12, 8  # plotsize
In [152]:
for exposure in exposures:
    pl.errorbar(exposure['wavelength'], exposure['flux'], yerr=exposure['error'], color=exposure['color'], 
                label=exposure['label'], linewidth=2.0)
    pl.scatter(exposure['wavelength'], exposure['flux'], color=exposure['color'], linewidth=2.0)
    
pl.legend(loc=4)
pl.xlabel("Wavelength", fontsize=18)
pl.ylabel("Flux", fontsize=18)
pl.title("Cool Idea", fontsize=20)
Out[152]:
<matplotlib.text.Text at 0x10efa5990>

Plotting inspiration

Various ways of plotting. Look here http://matplotlib.org/gallery.html and find plots that are similar to what you want to find.

In [54]:
%load http://matplotlib.org/mpl_examples/pylab_examples/polar_legend.py
In [57]:
#!/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')

################ Edit Here ####################
r = np.arange(0, 8.0, 0.01) # 8

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()

Notes

To edit an ipython notebook, you need to have this stuff installed. However, you can download an ipython notebook and use nbconvert to convert into pure python

Sidenote: install packages via

pip install -U package_name

If you don't have pip use:

easy_install pip 

If none of these work, you might have to do:

sudo pip install -U package_name