additional_ipython.rst
author Puneeth Chaganti <punchagan@gmail.com>
Wed, 22 Sep 2010 15:22:21 +0530
changeset 195 e8a251048213
parent 136 7f8b6a9fb61d
child 212 c3172a51b555
permissions -rw-r--r--
Comments and changes for lstsq script.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Additional Features of IPython
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     2
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     4
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     6
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     7
In this tutorial, we shall look at additional features of IPython that help us
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     8
to retreive the commands that we type on the interpreter and then save them
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     9
into a file and run it.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    10
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    11
Let us start ipython with pylab loaded, by typing
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    12
::
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    13
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    14
    $ ipython -pylab
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    15
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    16
on the terminal
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    17
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    18
{{{ shit to terminal and type ipython -pylab }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    19
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    20
We shall first make a plot and then view the history and save it.
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    21
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    22
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    23
    x = linspace(-2*pi, 2*pi, 100)
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    24
    plot(x, xsinx(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    25
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    26
xsin(x) is actually x * sin(x)
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    27
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    28
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    29
    plot(x, x*sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    30
    plot(x, sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    31
    xlabel("x")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    32
    ylabel("$f(x)$")   
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    33
    title("x and xsin")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    34
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    35
We now have the plot. Let us look at the commands that we have typed in. The
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    36
history can be retreived by using =%hist= command. Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    37
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    38
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    39
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    40
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    41
As you can see, it displays a list of recent commands that we typed. Every
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    42
command has a number in front, to specify in which order and when it was typed.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    43
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    44
Please note that there is a % sign before the hist command. This implies that 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    45
%hist is a command that is specific to IPython and not available in vannila 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    46
Python interpreter. These type of commands are called as magic commands.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    47
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    48
Also note that, the =%hist= itself is a command and is displayed as the most
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    49
recent command. This implies that anything we type in is stored as history, 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    50
irrespective of whether it is command or an error or IPython magic command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    51
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    52
If we want only the recent 5 to be displayed, we pass the number as an argument
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    53
to =%hist= command. Hence
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    54
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    55
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    56
    %hist 5 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    57
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    58
displays the recent 5 commands, inclusive of the =%hist= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    59
The default number is 40.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    60
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    61
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    62
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    63
%% 1 %% Read through the %hist documenatation and find out how can we list all
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    64
        the commands between 5 and 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    65
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    66
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    67
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    68
As we can see from =%hist= documentation,
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    69
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    70
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    71
    %hist 5 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    72
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    73
displays the commands from 5 to 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    74
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    75
Now that we have the history, we would like to save the required line of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    76
from history. This is possible by using the =%save= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    77
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    78
Before we do that, let us first look at history and identify what lines of code
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    79
we require.Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    80
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    81
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    82
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    83
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    84
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    85
{{{ point to the lines }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    86
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    87
The first command is linspace. But second command is a command that gave us an
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    88
error. Hence we do not need seconf. The commands from third to sixth are 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    89
required. The seventh command although is correct, we do not need it since we
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    90
are setting the title correctly in the eigthth command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    91
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    92
So we need first third to sixth and the eigthth command for our program.
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    93
Hence the syntax of =%save= is
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    94
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    95
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    96
    %save /home/fossee/plot_script.py 1 3-6 8
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    97
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    98
{{{ point to the output of the command }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    99
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   100
The command saves first and then third to sixth and eighth lines of code into
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   101
the specified file.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   102
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   103
The first argument to %save is the path of file to save the commands and the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   104
arguments there after are the commands to be saved in the given order.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   105
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   106
{{{ goto the file and open it and show it }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   107
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   108
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   109
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   110
%% 2 %% change the label on y-axis to "y" and save the lines of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   111
        accordingly
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   112
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   113
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   114
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   115
we use the command =ylabel= on interpreter as
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   116
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   117
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   118
    ylabel("y")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   119
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   120
and then do
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   121
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   122
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   123
    %save /home/fossee/example_plot.py 1 3-6 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   124
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   125
Now that we have the required lines of code in a file, let us learn how to run
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   126
the file as a python script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   127
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   128
We use the IPython magic command =%run= to do this. Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   129
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   130
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   131
   %run -i /home/fossee/plot_script.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   132
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   133
The script runs but we do not see the plot. This happens because we are running
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   134
a script and we are not in interactive mode anymore.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   135
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   136
Hence on your terminal type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   137
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   138
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   139
    show()
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   140
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   141
to show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   142
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   143
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   144
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   145
%% 3 %% Use %hist and %save and create a script that has show in it and run it
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   146
        to produce and show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   147
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   148
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   149
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   150
We first look at the history using
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   151
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   152
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   153
    %hist 20
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   154
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   155
Then save the script using
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   156
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   157
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   158
    %save /home/fossee/show_included.py 1 3-6 8 10 13
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   159
    %run -i /home/fossee/show_included.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   160
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   161
We get the desired plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   162
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   163
The reason for including a -i after run is to tell the interpreter that if any
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   164
name is not found in script, search for it in the interpreter. Hence all these
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   165
sin, plot, pi and show which are not available in script, are taken from the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   166
interpreter and used to run the script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   167
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   168
{{{ Show summary slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   169
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   170
This brings us to the end of the tutorial.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   171
we have looked at 
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   172
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   173
 * Retreiving history using =%hist= command
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   174
 * Vieweing only a part of history by passing an argument to %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   175
 * saving the required lines of code in required order using %save
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   176
 * using %run -i command to run the saved script
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   177
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   178
{{{ Show the "sponsored by FOSSEE" slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   179
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   180
#[Nishanth]: Will add this line after all of us fix on one.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   181
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   182
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   183
Hope you have enjoyed and found it useful.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   184
Thankyou
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   185
 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   186
.. Author              : Nishanth
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   187
   Internal Reviewer 1 : 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   188
   Internal Reviewer 2 : 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   189
   External Reviewer   :