import numpy as np
We only scratched the surface of the many useful features built into IPython. Something we didn't discuss is IPython's library of
magic commands. These commands add a lot of useful interactive
functionality. One such magic command is %quickref
, which displays a helpful IPython reference text.
%quickref
Use %quickref
to find another magic that allows you to time the execution of Python code. Then use that magic to time the following code:
(Hint: you can get help on magic commands just like other Python functions, e.g. %quickref?
.)
%timeit np.array(range(50))
10000 loops, best of 3: 33.5 us per loop
%timeit np.arange(50)
100000 loops, best of 3: 2.09 us per loop
%%timeit
x = np.arange(1000)
x_r = x[::-1]
np.interp([5.5, 6.6], x, np.ones_like(x))
10000 loops, best of 3: 54.1 us per loop
%lsmagic
Available line magics: %alias %alias_magic %autocall %automagic %bookmark %cd %clear %colors %config %connect_info %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %man %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%bash %%capture %%file %%perl %%prun %%ruby %%script %%sh %%sx %%system %%timeit Automagic is ON, % prefix IS NOT needed for line magics.
%logstop?
4 + 5
9
print _
9
In[10]
u'4 + 5'
Out[10]
9
4 + 5
%history -g pyfits
141/23: import pyfits tmg = pyfits.open('/grp/hst/cdbs/mtab/wa31653am_tmg.fits') 146/1: import pyfits 146/2: pyfits.open('pix.fits') 150/2: import pyfits data = pyfits.getdata('../data/pix.fits') 150/7: hdr = pyfits.getheader('pix.fits') print hdr 150/8: hdr = pyfits.getheader('../data/pix.fits') print hdr 151/1: import pyfits 151/2: f = pyfits.open('/grp/hst/cdbs/jref/w3m17171j_imp.fits') 150/13: pyfits.__version__ 150/15: hdr = pyfits.getheader('../data/pix.fits') hdr 154/2: import pyfits data = pyfits.getdata('../data/pix.fits') 154/6: hdr = pyfits.getheader('../data/pix.fits') hdr 154/9: pyfits.__version__ 154/10: pyfits.__file__ 158/2: hdr = pyfits.getheader('../data/pix.fits') hdr 158/4: import pyfits data = pyfits.getdata('../data/pix.fits') 158/8: hdr = pyfits.getheader('../data/pix.fits') hdr 185/1: import pyfits from scipy import ndimage from scipy import signal import numpy as np 185/3: j = pyfits.getdata('/astro/ferguson1/ferguson/python_course/hlsp_hudf09_hst_wfc3ir_hudf09_F125W_v1_sci.fits') h = pyfits.getdata('/astro/ferguson1/ferguson/python_course/hlsp_hudf09_hst_wfc3ir_hudf09_F160W_v1_sci.fits') weight = pyfits.getdata('/astro/ferguson1/ferguson/python_course/hlsp_hudf09_hst_wfc3ir_hudf09_F160W_v1_wht.fits') 190/11: import pyfits data = pyfits.getdata('../data/pix.fits') 190/15: hdr = pyfits.getheader('../data/pix.fits') hdr 200/13: import pyfits data = pyfits.getdata('../data/pix.fits') 200/18: hdr = pyfits.getheader('../data/pix.fits') 201/11: import pyfits data = pyfits.getdata('../data/pix.fits') 201/16: hdr = pyfits.getheader('../data/pix.fits') 213/10: import pyfits data = pyfits.getdata('../data/pix.fits') 218/20: %pylab inline import pyfits image=pyfits.getdata('pix.fits') 218/21: %pylab inline import pyfits image=pyfits.getdata('../data/pix.fits') 220/1: %pylab inline import pyfits 220/2: data = pyfits.getdata('../data/pix.fits') 222/9: import pyfits 222/10: data = pyfits.getdata('../gunning/scientific-python-training-2012-master/data/pix.fits') 222/11: data = pyfits.getdata('../data/pix.fits') 228/1: import pyfits import numpy as np import pylab 228/2: data = pyfits.getdata('../data/pix.fits') 231/4: import pyfits data = pyfits.getdata('../data/pix.fits') 236/4: import pyfits data = pyfits.getdata('../data/pix.fits') 237/7: import pyfits data = pyfits.getdata('../data/pix.fits') 238/6: import pyfits data = pyfits.getdata('../data/pix.fits') 239/11: %history -g 'pyfits' 239/12: %history -g 'pyfits' -l 5 239/13: %history -l 5 -g 'pyfits' 239/14: %history -g 'pyfits.getdata' 16: %history -g pyfits
Read in ../data/pix.fits
, scale and offset the data to range from 0 to 1000, and plot the column or row with the maximum value. Annotate your steps using IPython Notebook text cells and include a LaTeX equation that illustrates the transformation you've programmed in.
import pyfits
data = pyfits.getdata('../data/pix.fits')
data.shape
(512, 512)
data = data.astype(np.float)
data = np.float(data)
data = (data - data.min()) / (data.max() - data.min()) * 1000.
data.min(), data.max()
(0.0, 1000.0)
np.where(data == data.max())
(array([188]), array([347]))
data[_]
array([ 1000.])
w = __
row = w[0][0]
col = w[1][0]
print row, col
188 347
data.argmax()
96603
np.unravel_index(data.argmax(), data.shape)
(188, 347)
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
plot(data[188])
[<matplotlib.lines.Line2D at 0x106d966d0>]
plot(data[:, 347])
[<matplotlib.lines.Line2D at 0x1077243d0>]
plot(data[256])
[<matplotlib.lines.Line2D at 0x106dd01d0>]
imshow(data)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x10864c638>
rcParamsDefault['image.origin']
'upper'
pylab.__file__
'/Users/mrdavis/Ureka/python/lib/python2.7/site-packages/matplotlib/pylab.pyc'
a = np.arange(10)
print a
[0 1 2 3 4 5 6 7 8 9]
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.arange(10, 20)
c = np.arange(20, 30)
a, b, c
(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29]))
a
b
c
array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
%lsmagic
Available line magics: %alias %alias_magic %autocall %automagic %bookmark %cd %clear %colors %config %connect_info %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %man %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%bash %%capture %%file %%perl %%prun %%ruby %%script %%sh %%sx %%system %%timeit Automagic is ON, % prefix IS NOT needed for line magics.
%whos?
%whos
Variable Type Data/Info ------------------------------- a ndarray 10: 10 elems, type `int64`, 80 bytes b ndarray 10: 10 elems, type `int64`, 80 bytes c ndarray 10: 10 elems, type `int64`, 80 bytes col int64 347 data ndarray 512x512: 262144 elems, type `float64`, 2097152 bytes (2 Mb) pyfits module <module 'pyfits' from '/U<...>hon/pyfits/__init__.pyc'> row int64 188 w tuple n=2
%who
a b c col data pyfits row w
%%file name_of_file.py
print 'Hello, STSCI!'
Overwriting name_of_file.py
%run name_of_file.py
Hello, STSCI!