|
1 ======== |
|
2 Script |
|
3 ======== |
|
4 |
|
5 Welcome to this tutorial on loading data from files. |
|
6 |
|
7 {{{ Screen shows welcome slide }}} |
|
8 |
|
9 Until now, all the plots we have made use analytic functions. We have |
|
10 been using analytic functions to generate a sequence of points and |
|
11 plotting them, against another sequence of points. But, this is not |
|
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 In this tutorial we shall learn to read data from files and save it |
|
19 into sequences that can later be used to plot. |
|
20 |
|
21 {{{ Show the outline for this tutorial }}} |
|
22 |
|
23 We shall use the ``loadtxt`` command to load data from files. We will |
|
24 be looking at how to get multiple columns of data into multiple |
|
25 sequences. |
|
26 |
|
27 {{{ switch back to the terminal }}} |
|
28 |
|
29 As usual, let us start IPython, using |
|
30 :: |
|
31 |
|
32 ipython -pylab |
|
33 |
|
34 Now, Let us begin with reading the file primes.txt, which contains |
|
35 just a list of primes listed in a column, using the loadtxt command. |
|
36 The file, in our case, is present in ``/home/fossee/primes.txt``. |
|
37 |
|
38 #[punch: do we need a slide for showing the path?] |
|
39 |
|
40 We use the ``cat`` command to see the contents of this file. |
|
41 |
|
42 #[punch: should we show the cat command here? seems like a good place |
|
43 to do it] :: |
|
44 |
|
45 cat /home/fossee/primes.txt |
|
46 |
|
47 Now let us read this list into the variable ``primes``. |
|
48 :: |
|
49 |
|
50 primes = loadtxt('/home/fossee/primes.txt') |
|
51 |
|
52 ``primes`` is now a sequence of primes, that was listed in the file, |
|
53 ``primes.txt``. |
|
54 |
|
55 We now type, ``print primes`` to see the sequence printed. |
|
56 |
|
57 We observe that all of the numbers end with a period. This is so, |
|
58 because these numbers are actually read as ``floats``. We shall learn |
|
59 about them, later. |
|
60 |
|
61 Now, let us use the ``loadtxt`` command to read a file that contains |
|
62 two columns of data, ``pendulum.txt``. This file contains the length |
|
63 of the pendulum in the first column and the corresponding time period |
|
64 in the second. |
|
65 |
|
66 %%1%% Pause the video here, and use the ``cat`` command to view the |
|
67 contents of this file and then resume the video. |
|
68 |
|
69 This is how we look at the contents of the file, ``pendulum.txt`` |
|
70 :: |
|
71 |
|
72 cat /home/fossee/pendulum.txt |
|
73 |
|
74 Let us, now, read the data into the variable ``pend``. Again, it is |
|
75 assumed that the file is in ``/home/fossee/`` |
|
76 :: |
|
77 |
|
78 pend = loadtxt('/home/fossee/pendulum.txt') |
|
79 |
|
80 Let us now print the variable ``pend`` and see what's in it. |
|
81 :: |
|
82 |
|
83 print pend |
|
84 |
|
85 Notice that ``pend`` is not a simple sequence like ``primes``. It has |
|
86 two sequences, containing both the columns of the data file. Let us |
|
87 use an additional argument of the ``loadtxt`` command, to read it into |
|
88 two separate, simple sequences. |
|
89 :: |
|
90 |
|
91 L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True) |
|
92 |
|
93 Let us now, print the variables L and T, to see what they contain. |
|
94 :: |
|
95 |
|
96 print L |
|
97 print T |
|
98 |
|
99 Notice, that L and T now contain the first and second columns of data |
|
100 from the data file, ``pendulum.txt``, and they are both simple |
|
101 sequences. |
|
102 |
|
103 {{{ show the slide with loadtxt --- other features }}} |
|
104 |
|
105 In this tutorial, we have learnt the basic use of the ``loadtxt`` |
|
106 command, which is capable of doing a lot more than we have used it for |
|
107 until now, for example |
|
108 |
|
109 %%2%% Pause the video here, and read the file |
|
110 ``pendulum_semicolon.txt`` which contains the same data as |
|
111 ``pendulum.txt``, but the columns are separated by semi-colons instead |
|
112 of spaces. Use the IPython help to see how to do this. Once you have |
|
113 finished, resume the video to look at the solution. |
|
114 |
|
115 {{{ switch back to the terminal }}} |
|
116 :: |
|
117 |
|
118 L, T = loadtxt('/home/fossee/pendulum.txt', unpack``True, delimiter``';') |
|
119 |
|
120 print L |
|
121 |
|
122 print T |
|
123 |
|
124 This brings us to the end of this tutorial. |
|
125 |
|
126 {{{ show the summary slide }}} |
|
127 |
|
128 You should now be able to do the following, comfortably. |
|
129 |
|
130 + Read data from files, containing a single column of data using the |
|
131 ``loadtxt`` command. |
|
132 + Read multiple columns of data, separated by spaces or other |
|
133 delimiters. |
|
134 |
|
135 Thank you! |
|
136 |
|
137 |