Practice English Speaking&Listening with: Lecture 8

Difficulty: 0

So, so far you have done you have done some computation using python, but you have not

read some data or written some data using python. So, if you are doing some simulation

or if you solve a ordinary differential equation and if it takes a longer time, then you wont

be able to analyze that on the fly. So, what you need to you need to write those data in

some format, and then at later time you can analyze those data.

So, I will discuss these things today. So, there is one format called ASCII format, which

is the American standard code for information interchange. So, you can use a text file to

write a data and that is. So, this is ASCII there are different format of ASCII. So, text

file is one of them. So, I will discuss about this. So, for that you need to create an object

f and this is a function file. So, you write a you keep a name of a data dot txt. So, data

will be saved in this file and w stands for writing.

So, next line is f dot write, and I have written this is my data. So, you can write this string

will be written in that txt file, and next is to just say f dot close. So, this is the

simplest example to write something whatever you want to write.

Now, if you want to write some array. So, how you will do that and a please stop me

if you have any problem in understanding. So, for that I have a x s linspace everyone

knows linspace. So, it goes from minus 0.9 to 0.9 with 100 datasets, and y is this some

complicated function. So, this it is a simple reference, but it could be a very complicated

you can solve ordinary differential equation and you get some y. Then again I have created

an object f and here is the data dot txt and I have written this is my data. So, hash is

the comment. So, if you write something in python with hash that is taken as a comment

would python will ignore that, and slash and for the change of line. So, when you get those

data you can write some comment about that data.

Then I have run a loop for I in range len x, len x is the length of your x array and

write percentage f. Percentage f is the value of that data and for x(i), then slash t is

the tab then again percentage f for this y(i) and then slash n change in line. So, what

how loop will work. So, for x is equal to 0, it will write the x(0) value then a tab

then y(0) value then change of line then again the loop will be using and you will get all

set of it. I will just I will demonstrate this.

And next is to close that file and if you want to read that data, then you just have

some name data I have data np load txt and data dot txt. So, it will take whole 2d array.

And now you can. So, if you have if you have x and y and some data. So, it will. So, these

two vectors will be inside one array. So, now, you can use x colon zeros. So, it will

take this x data and next will be. So, one will take y data. So, I will demonstrate this,

it is visible. Almost.

So, space 0.9,0.9 y is x 6 minus x 4 plus 0.2 x square. So, if I want to plot this.

So, you have x comma y. So, now, I will write this data and read it and will test that is

whatever I have read is exactly gives this function or not. So, I created object f. So,

I can write some comment.

So, now it has written the x and y values. So, if I want to read this. So, data disc

dot txt has this is my data and then x values and y values, I want to load this.

So, let us call data and equal to load and if I want to read what is x; so colon 0.

So, it will give the x value. So, from 0.9 to minus 0.9 to point let us plot this.

So, this is x and this is y and this plotted with rp, so that building. So, it retrieves

the same data. .

R p is. So, r is stands for red and p is that point. So, my first curve was the blue curve

original curve, and then if I would have put r then you would not be able to see any difference.

So, it would overlap.

So, next is about binary file. So, another format is to write in binary file. So, in

binary it dumps the whole data in ones and zero. So, computers understand the binary

system. So, it writes everything in ones and zeros. So, it gives the full information on

the data. So, I will give you an example to show how it gives the full information. So,

how to write in binary? So, there are two there are many formats.

So, I explain the dot npy format, which is comes from numpy and another is very advanced

which is HDF 5 format. So, I will cover these two. So, first let us look at npy which is

very simple. So, let us have a data array. So, this v stack means. So, if you have array

x and array y then it will stack those vertically. So, if you x comma y comma z it will stay

all these arrays. So, it forms a two d array from x and y then save. So, this is my data.

So, you can keep any name comma data array. So, it will save this data array and that

is it, and to load this data you need to say data np load my data and np. So, this is the

extension is dot npy. So, just dot npy it will take there. So, it

is very easy than the ASCII dot txt file. So, I will give you example how does it gives

the full information.

So, let us delete x and y let call x is 1.3 1 by 3.029. So, x is this.

So, let us save this in ASCII. So, this much is reduced it has truncated up to 142. So,

it is keeping how much and one is point and 0 see these are 8characters. So, size of this

is 8 byte. So, you can check. So, just say ls minus lh. So, size is 8 byte and my information

is I do not have the full information it has not written the whole thing.

So, it is truncated up to this point. So, if I save in binary. So, np dot save and if

I want to read this I cannot read. So, its question mark and question mark. So, it is

written in binary, I cannot read it in doing cat or anything else. So, I need to read in

python. So, np dot load

and it returns the whole information. So, where is x; so it keeping whole x. So, you

have nothing is lost. So, if you do a some simulation say of a 1000 cube data and you

have 1000 cube differential equations. So, when you read this that initial condition

in with the ASCII and binary you will see the difference and you know that about chaos.

So, if the initial condition will be sensitive then it will reflect later. So, that is why

for doing a bigger simulation one should use binary file system rather than an ASCII file

system, where you keep the full information and nothing is lost. So, is it cleared up

to this point. So, next is HDF5. So, HDF5 is a very advanced version.

So, let us look how to write in HDF5. So, first you need to install the h 5 py package.

So, if you are using anaconda. So, you need to say conda install h 5 py. So, it will install

that package. So, this h this file is the object of h 5 py. So, I create right file

h 5 py file data set. So, as HDF 5 gives it is a file system. So, you can say many arrays

inside that. So, I say write file data underscore one. So, it will create space for one array

and data array. So, data array was that a stack x and y, I

could have also done data underscore two data underscore three and as many as I want then

write file dot close. So, it will just write those that data. So, this is how I write the

data not to read. So, I create read file and same data set then I read the data y read

file slash data underscore one. So, if I have other data then different underscore two three

whatever according to the name of that. Then I need to convert this data to numpy array

because in python we use numpy and then write file dot close. So, it will close the HDF

a bit. So, we will do one example for this.

So, I have written one code to create data this is a code where numpy import library

numpy, then another library h 5 py and I write from 0 to 5000 this step point o 1 and y is

just sin x, and again I create f for data dot txt and write it in this loop which I

have explained earlier. Next what I do the same data is written as HDF 5. So, I stack

this x and y write them in data set dot h 5 data underscore one this array is inserted

over here write dot close. So, same data is returned in ASCII as hd as well as it hdf.

So, let us run this code. So, we will see that is there any compression in saving ASCII

and compared to HDF 5. So, size of data dot txt is 116 mega byte, data dot set h5 76 mega


So, it is reduced and now I will load this data. So, it is ASCII. So, we will see how

much time does it take while reading ASCII data and while reading HDF 5 data.

So, I will introduce one import time it. So, time is the library. So, you do not need to

install that it is already installed it comes with the package of python.

So, here I write written start the timer. So, it will just start the clock and here

a data is loaded with np load dot txt and t, and x is same thing was I have described

earlier and then it is stopped.

So, and I will compute the time taken in this operation is I stop minus start will give

me the amount of time taken in seconds and the mean yes. So, it start. So, its take 39.27

seconds, and I will now load with HDF 5. So, this is the code for HDF 5. So, this is the

code for HDF 5. So, I introduce one more library h 5 py again I start then read file this data

set data underscore one, convert this data to numpy array then t x stop and it will print.

So, it will read the same data ASCII must take 39.2 second. So, any guess how much time

will it will take with as HDF 5. half.

Half 0.12 second; so when you will do some projects in this course later time, I had

to save in HDF 5. So, that will save your time, do not save in ASCII and then read with

ASCII. So, you that would not be so beneficial, while doing your project you will save some

big data. So, you will you will get about say 200 mb or something then save it in HDF

5 do not use ASCII for saving bigger data.

Now, I will do a little bit of visualization. So, when you have written those data then

you will analyze them. So, for analyzing you can use Mayavi. So, Mayavi we can create some

density plot and Iso contours. So, this was made by the Indian person IIT Bombay a prabhuramachandran

he is the creator of this Mayavi package. So, this is one of my research problem. So,

I will just demonstrate this. So, how to create? So, this is the hot fluid which is trying

to go up and the cold fluid who is trying to come down, these are the plume. So, if

you have this data and you want if you want to create here I have the ISO contours and

also the density plot.

So, how to use what you will do it is same something similar to what you use in matplotlib.

So, you just say Maya dot figure, this is the background color and the size 800 in comma

600 pixels and I have an array t. So, it is that is temperature. So, you could have different

array according to your problem and I set a color map say jet.

So, there are different color maps. So, you will be able to see later, this is for the

contour 3d. Now if I want to plot the density plot the source this scalar field t and I

write this surface src color mass spectrum, and opacity is 0.7. So, what does opacity

is move from 0 to 1. So, if you decrease the opacity you can see inside fine inside the

structures, and then you said the color bar. So, this is similar thing you have already

done with your contour plots of matplotlib. So, just any this is orientation. So, you

can change your orientation and number labels. So, I keep three and then Maya dot show similar

to plt dot.

So, I have a data dot t f r dot h file which of 128 mb and let us do this I show you therefore,.

So, forget about this. So, you should just take this Mayavi package. So, you need to

install Mayavi also. So, you need to say conda installed Mayavi then you will be able to

put Mayavi library, and just ignore this much. So, I take theta and I convert theta to t

and this is same what I have shown in the slides.

So, let us run this code so. So, now, you can visualize this, you can go inside and

see how the structures are formed.

So, when you take your project you may need to do not similar kind of problem, but something

close to that and you can produce some very nice plots, and that will be very useful in

presentation the colorful plots are always useful. So, this what I wanted to show regarding

todays class, I have some other if you want to use

color map. The color map; so you can. So, you have different

color maps. So, you can. So, I put jet you can put these many color maps.

So, with different color maps you can just keep on trying and get the best you can get.

So, I have just copied few examples from Mayavi side. So, just I will demonstrate this, then

like a spherical harmonics.

So, you can just see this orbital. So, I think task is there was problem with something nice.

So, you can make as many as and you can just play with these things. So, it has done all

the spherical harmonics. So, if you look at the code I will give you these codes. So,

it varies n from 1 to 6 with m, with range n and it will just take the spherical harmonics

from the from scipy packet.

So, do not worry about these codes so and another example of the magnetic field lines.

So, this is the magnetic field lines from a magnetic dipole.

So, you can try these at this link you will get the information about; if you want to

know more about numpy array npy sorry numpy save. So, that npy file system you can use

this link the other link is for h5 py and this is for the mayavi example gallery what

I have shown just now.

The Description of Lecture 8