1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you will be able to |
|
5 |
|
6 .. + Read data from files, containing a single column of data using the |
|
7 .. ``loadtxt`` command. |
|
8 .. + Read multiple columns of data, separated by spaces or other |
|
9 .. delimiters. |
|
10 |
|
11 |
|
12 .. Prerequisites |
|
13 .. ------------- |
|
14 |
|
15 .. 1. getting started with ``ipython`` |
|
16 |
|
17 .. #[Anand: author and internal reviewer not mentioned] |
|
18 .. Author : Puneeth Changanti |
|
19 Internal Reviewer : Nishanth Amuluru |
|
20 External Reviewer : |
|
21 Language Reviewer : Bhanukiran |
|
22 Checklist OK? : <06-11-2010 Anand, OK> [2010-10-05] |
|
23 |
|
24 Script |
|
25 ------ |
|
26 |
|
27 {{{ Show the slide containing title }}} |
|
28 |
|
29 Hello Friends. Welcome to this tutorial on loading data from files. |
|
30 |
|
31 {{{ Screen shows welcome slide }}} |
|
32 |
|
33 We often require to plot points obtained from experimental |
|
34 observations. In this tutorial we shall learn to read data from files |
|
35 and save it into sequences that can later be used to plot. |
|
36 |
|
37 {{{ Show the outline for this tutorial }}} |
|
38 |
|
39 We shall use the ``loadtxt`` command to load data from files. We will |
|
40 be looking at how to read a file with multiple columns of data and |
|
41 load each column of data into a sequence. |
|
42 |
|
43 {{{ switch back to the terminal }}} |
|
44 |
|
45 As usual, let us start IPython, using |
|
46 :: |
|
47 |
|
48 ipython -pylab |
|
49 |
|
50 Now, Let us begin with reading the file primes.txt, which contains |
|
51 just a list of primes listed in a column, using the loadtxt command. |
|
52 The file, in our case, is present in ``/home/fossee/primes.txt``. |
|
53 |
|
54 {{{ Navigate to the path in the OS, open the file and show it }}} |
|
55 |
|
56 .. #[punch: do we need a slide for showing the path?] |
|
57 |
|
58 .. We use the ``cat`` command to see the contents of this file. |
|
59 |
|
60 .. #[punch: should we show the cat command here? seems like a good place |
|
61 to do it] :: |
|
62 |
|
63 cat /home/fossee/primes.txt |
|
64 |
|
65 .. #[Nishanth]: A problem for windows users. |
|
66 Should we simply open the file and show them the data |
|
67 so that we can be fine with GNU/Linux ;) and windows? |
|
68 |
|
69 Now let us read this list into the variable ``primes``. |
|
70 :: |
|
71 |
|
72 primes = loadtxt('/home/fossee/primes.txt') |
|
73 |
|
74 ``primes`` is now a sequence of primes, that was listed in the file, |
|
75 ``primes.txt``. |
|
76 |
|
77 We now type, ``print primes`` to see the sequence printed. |
|
78 |
|
79 We observe that all of the numbers end with a period. This is so, |
|
80 because these numbers are actually read as ``floats``. We shall learn |
|
81 about them, later. |
|
82 |
|
83 Now, let us use the ``loadtxt`` command to read a file that contains |
|
84 two columns of data, ``pendulum.txt``. This file contains the length |
|
85 of the pendulum in the first column and the corresponding time period |
|
86 in the second. Note that ``loadtxt`` needs both the columns to have |
|
87 equal number of rows. |
|
88 |
|
89 .. Following is an exercise that you must do. |
|
90 |
|
91 .. %%1%% Use the ``cat`` command to view the contents of this file. |
|
92 |
|
93 .. Please, pause the video here. Do the exercise and then continue. |
|
94 |
|
95 .. This is how we look at the contents of the file, ``pendulum.txt`` |
|
96 .. :: |
|
97 |
|
98 .. cat /home/fossee/pendulum.txt |
|
99 |
|
100 .. #[Nishanth]: The first column is L values and second is T values |
|
101 from a simple pendulum experiment. |
|
102 Since you are using the variable names later in the |
|
103 script. |
|
104 Not necessary but can be included also. |
|
105 |
|
106 Let us, now, read the data into the variable ``pend``. Again, it is |
|
107 assumed that the file is in ``/home/fossee/`` |
|
108 :: |
|
109 |
|
110 pend = loadtxt('/home/fossee/pendulum.txt') |
|
111 |
|
112 Let us now print the variable ``pend`` and see what's in it. |
|
113 :: |
|
114 |
|
115 print pend |
|
116 |
|
117 Notice that ``pend`` is not a simple sequence like ``primes``. It has |
|
118 two sequences, containing both the columns of the data file. Let us |
|
119 use an additional argument of the ``loadtxt`` command, to read it into |
|
120 two separate, simple sequences. |
|
121 :: |
|
122 |
|
123 L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True) |
|
124 |
|
125 .. #[Nishanth]: It has a sequence of items in which each item contains |
|
126 two values. first is l and second is t |
|
127 |
|
128 Let us now, print the variables L and T, to see what they contain. |
|
129 :: |
|
130 |
|
131 print L |
|
132 print T |
|
133 |
|
134 .. #[Nishanth]: Stress on ``unpack=True`` ?? |
|
135 |
|
136 Notice, that L and T now contain the first and second columns of data |
|
137 from the data file, ``pendulum.txt``, and they are both simple |
|
138 sequences. ``unpack=True`` has given us the two columns into two |
|
139 separate sequences instead of one complex sequence. |
|
140 |
|
141 {{{ show the slide with loadtxt --- other features }}} |
|
142 |
|
143 In this tutorial, we have learnt the basic use of the ``loadtxt`` |
|
144 command, which is capable of doing a lot more than we have used it for |
|
145 until now. Let us look at an example, but before that do this |
|
146 exercise. |
|
147 |
|
148 %%1%% Read the file ``pendulum_semicolon.txt`` which contains the same |
|
149 data as ``pendulum.txt``, but the columns are separated by semi-colons |
|
150 instead of spaces. Use the IPython help to see how to do this. |
|
151 |
|
152 Please, pause the video here. Do the exercise and then continue. |
|
153 |
|
154 {{{ switch back to the terminal }}} |
|
155 :: |
|
156 |
|
157 L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True, delimiter=';') |
|
158 |
|
159 print L |
|
160 |
|
161 print T |
|
162 |
|
163 This brings us to the end of this tutorial. |
|
164 |
|
165 {{{ show the summary slide }}} |
|
166 |
|
167 You should now be able to do the following, comfortably. |
|
168 |
|
169 + Read data from files, containing a single column of data using the |
|
170 ``loadtxt`` command. |
|
171 + Read multiple columns of data, separated by spaces or other |
|
172 delimiters. |
|
173 |
|
174 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
175 |
|
176 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
177 |
|
178 Hope you have enjoyed and found it useful. |
|
179 Thank you! |
|
180 |
|