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 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 |
12 |
21 {{{ Show the outline for this tutorial }}} |
13 {{{ Show the outline for this tutorial }}} |
22 |
14 |
23 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 |
24 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 |
25 sequences. |
17 load each column of data into a sequence. |
26 |
18 |
27 {{{ switch back to the terminal }}} |
19 {{{ switch back to the terminal }}} |
28 |
20 |
29 As usual, let us start IPython, using |
21 As usual, let us start IPython, using |
30 :: |
22 :: |
31 |
23 |
32 ipython -pylab |
24 ipython -pylab |
33 |
25 |
34 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 |
35 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. |
36 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``. |
37 |
29 |
38 #[punch: do we need a slide for showing the path?] |
30 {{{ Navigate to the path in the OS, open the file and show it }}} |
39 |
31 |
40 We use the ``cat`` command to see the contents of this file. |
32 .. #[punch: do we need a slide for showing the path?] |
41 |
33 |
42 #[punch: should we show the cat command here? seems like a good place |
34 .. We use the ``cat`` command to see the contents of this file. |
43 to do it] :: |
|
44 |
35 |
45 cat /home/fossee/primes.txt |
36 .. #[punch: should we show the cat command here? seems like a good place |
|
37 to do it] :: |
|
38 |
|
39 cat /home/fossee/primes.txt |
|
40 |
|
41 .. #[Nishanth]: A problem for windows users. |
|
42 Should we simply open the file and show them the data |
|
43 so that we can be fine with GNU/Linux ;) and windows? |
46 |
44 |
47 Now let us read this list into the variable ``primes``. |
45 Now let us read this list into the variable ``primes``. |
48 :: |
46 :: |
49 |
47 |
50 primes = loadtxt('/home/fossee/primes.txt') |
48 primes = loadtxt('/home/fossee/primes.txt') |
69 This is how we look at the contents of the file, ``pendulum.txt`` |
67 This is how we look at the contents of the file, ``pendulum.txt`` |
70 :: |
68 :: |
71 |
69 |
72 cat /home/fossee/pendulum.txt |
70 cat /home/fossee/pendulum.txt |
73 |
71 |
|
72 .. #[Nishanth]: The first column is L values and second is T values |
|
73 from a simle pelculum experiment. |
|
74 Since you are using the variable names later in the |
|
75 script. |
|
76 Not necessary but can be included also. |
|
77 |
74 Let us, now, read the data into the variable ``pend``. Again, it is |
78 Let us, now, read the data into the variable ``pend``. Again, it is |
75 assumed that the file is in ``/home/fossee/`` |
79 assumed that the file is in ``/home/fossee/`` |
76 :: |
80 :: |
77 |
81 |
78 pend = loadtxt('/home/fossee/pendulum.txt') |
82 pend = loadtxt('/home/fossee/pendulum.txt') |
88 two separate, simple sequences. |
92 two separate, simple sequences. |
89 :: |
93 :: |
90 |
94 |
91 L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True) |
95 L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True) |
92 |
96 |
|
97 .. #[Nishanth]: It has a sequence of items in which each item contains |
|
98 two values. first is l and second is t |
|
99 |
93 Let us now, print the variables L and T, to see what they contain. |
100 Let us now, print the variables L and T, to see what they contain. |
94 :: |
101 :: |
95 |
102 |
96 print L |
103 print L |
97 print T |
104 print T |
98 |
105 |
|
106 .. #[Nishanth]: Stress on ``unpack=True`` ?? |
|
107 |
99 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 |
100 from the data file, ``pendulum.txt``, and they are both simple |
109 from the data file, ``pendulum.txt``, and they are both simple |
101 sequences. |
110 sequences. ``unpack=True`` has given us the two columns in to two |
|
111 separate sequences instead of one complex sequence. |
102 |
112 |
103 {{{ show the slide with loadtxt --- other features }}} |
113 {{{ show the slide with loadtxt --- other features }}} |
104 |
114 |
105 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`` |
106 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 |