loading-data-from-files.rst
changeset 199 680a0692529f
parent 192 3a839dca2bdb
equal deleted inserted replaced
198:e1b4d3199d94 199:680a0692529f
     4 
     4 
     5 Welcome to this tutorial on loading data from files. 
     5 Welcome to this tutorial on loading data from files. 
     6 
     6 
     7 {{{ Screen shows welcome slide }}}
     7 {{{ Screen shows welcome slide }}}
     8 
     8 
     9 Until now, all the plots we have made use analytic functions. We have
     9 We often require to plot points obtained from experimental
    10 been using analytic functions to generate a sequence of points and
    10 observations. In this tutorial we shall learn to read data from files
    11 plotting them, against another sequence of points. But, this is not
    11 and save it into sequences that can later be used to plot.
    12 what we do most often. We often require to plot points obtained from
       
    13 experimental observations.
       
    14 
       
    15 .. #[punch: the initial part of the paragraph may be removed, to make
       
    16     this a more generic LO?]
       
    17 
       
    18 .. #[Nishanth]: The paragraph can be removed.
       
    19 
       
    20 In this tutorial we shall learn to read data from files and save it
       
    21 into sequences that can later be used to plot.
       
    22 
    12 
    23 {{{ Show the outline for this tutorial }}} 
    13 {{{ Show the outline for this tutorial }}} 
    24 
    14 
    25 We shall use the ``loadtxt`` command to load data from files. We will
    15 We shall use the ``loadtxt`` command to load data from files. We will
    26 be looking at how to get multiple columns of data into multiple
    16 be looking at how to read a file with multiple columns of data and
    27 sequences.
    17 load each column of data into a sequence. 
    28 
       
    29 .. #[Nishanth]: can be "How to read a file with multiple columns of
       
    30                 data and load each column of data into a sequence."
       
    31 
    18 
    32 {{{ switch back to the terminal }}}
    19 {{{ switch back to the terminal }}}
    33 
    20 
    34 As usual, let us start IPython, using 
    21 As usual, let us start IPython, using 
    35 ::
    22 ::
    36 
    23 
    37   ipython -pylab 
    24   ipython -pylab 
    38 
    25 
    39 Now, Let us begin with reading the file primes.txt, which contains
    26 Now, Let us begin with reading the file primes.txt, which contains
    40 just a list of primes listed in a column, using the loadtxt command.
    27 just a list of primes listed in a column, using the loadtxt command.
    41 The file, in our case, is present in ``/home/fossee/primes.txt``.
    28 The file, in our case, is present in ``/home/fossee/primes.txt``. 
       
    29 
       
    30 {{{ Navigate to the path in the OS, open the file and show it }}}
    42 
    31 
    43 .. #[punch: do we need a slide for showing the path?]
    32 .. #[punch: do we need a slide for showing the path?]
    44 
    33 
    45 We use the ``cat`` command to see the contents of this file. 
    34 .. We use the ``cat`` command to see the contents of this file. 
    46 
    35 
    47 #[punch: should we show the cat command here? seems like a good place
    36 .. #[punch: should we show the cat command here? seems like a good place
    48 to do it] ::
    37    to do it] ::
    49 
    38 
    50   cat /home/fossee/primes.txt
    39      cat /home/fossee/primes.txt
    51 
    40 
    52 .. #[Nishanth]: A problem for windows users.
    41 .. #[Nishanth]: A problem for windows users.
    53                 Should we simply open the file and show them the data
    42                 Should we simply open the file and show them the data
    54                 so that we can be fine with GNU/Linux ;) and windows?
    43                 so that we can be fine with GNU/Linux ;) and windows?
    55 
    44 
   116 
   105 
   117 .. #[Nishanth]: Stress on ``unpack=True`` ??
   106 .. #[Nishanth]: Stress on ``unpack=True`` ??
   118 
   107 
   119 Notice, that L and T now contain the first and second columns of data
   108 Notice, that L and T now contain the first and second columns of data
   120 from the data file, ``pendulum.txt``, and they are both simple
   109 from the data file, ``pendulum.txt``, and they are both simple
   121 sequences.
   110 sequences. ``unpack=True`` has given us the two columns in to two
       
   111 separate sequences instead of one complex sequence. 
   122 
   112 
   123 {{{ show the slide with loadtxt --- other features }}}
   113 {{{ show the slide with loadtxt --- other features }}}
   124 
   114 
   125 In this tutorial, we have learnt the basic use of the ``loadtxt``
   115 In this tutorial, we have learnt the basic use of the ``loadtxt``
   126 command, which is capable of doing a lot more than we have used it for
   116 command, which is capable of doing a lot more than we have used it for
   133 finished, resume the video to look at the solution.
   123 finished, resume the video to look at the solution.
   134 
   124 
   135 {{{ switch back to the terminal }}}
   125 {{{ switch back to the terminal }}}
   136 ::
   126 ::
   137 
   127 
   138   L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True, delimiter=';')
   128   L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True, delimiter=';')
   139 
   129 
   140   print L
   130   print L
   141 
   131 
   142   print T
   132   print T
   143 
       
   144 .. #[Nishanth]: L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', ...)
       
   145 
   133 
   146 This brings us to the end of this tutorial. 
   134 This brings us to the end of this tutorial. 
   147 
   135 
   148 {{{ show the summary slide }}}
   136 {{{ show the summary slide }}}
   149 
   137 
   154   + Read multiple columns of data, separated by spaces or other
   142   + Read multiple columns of data, separated by spaces or other
   155     delimiters.
   143     delimiters.
   156 
   144 
   157 Thank you!   
   145 Thank you!   
   158 
   146 
   159