Saturday, March 29, 2014

1. Introduction to IPython environment

These series of tutorials will be about Audio Programming in Python. As Python is a general-purpose language most of the information can be used as basis for any field. First I will introduce IPython environment and then Python language fundamentals and also some important libraries. Today I will start on IPython Environment.




I will use Anaconda Python package. It includes the Python language, many libraries, and different tools such as IPython.




By going to this address (http://continuum.io/downloads) you can get links to the install files for the major operating systems.




Since I have 64-bit windows, I downloaded the last link for 64-bit Windows. The good things about Anaconda is that despite the differences in installation, the final program is almost identical.




Clicking on the downloaded file will get you this welcome dialog. Now you can press Install and follow the prompts.




At the end of the process, we should get a successful installation message.




If we go to the Start and open All Programs, we will find the Anaconda folder which will contain the list of programs. The first program, Anaconda Command Prompt, is the most important as it is the usual way of starting other Anaconda programs as well as many other things within Anaconda. However you can also click on them to launch it.




The first thing is to enter these two command in the Anaconda Command Terminal. These will get new versions if they are available.




In the list of Anaconda applications, one is the Launcher. Clicking on Launcher will give this. It shows three of the common applications in the Anaconda system. However I prefer the Anaconda Command Prompt as it allows for passing of arguments. I will show example of this later.




This command on the terminal will start IPython in its preferred graphical environment.




Here is the IPython program. It shows the version of Python, the version of IPython and some help information. I also write the Hello World program. For the print function, parenthesis are not required, but is a good practice to write code which is compatible with Python 3.




This is another Anaconda Command Prompt command for starting IPython. The arguments are for inline plotting. This means graphs will be shown in the main window and a separate window will not open.




This is example of inline plotting. First the graphical library is loaded. Next a list with 3 values is created. Finally that list is plotted.




Adding a question mark or two question marks to an object will give help, documentation or even source code depending on how much information IPython can find by searching the files.




Magic functions are used to make the shell more interactive and helpful. Magic functions start with the percentage symbol. Three magic functions are to reset the shell, list all variables in the memory, as well as run a script. I will show examples of each of them.




Actually the percentage symbol is not needed in the default configuration. You can turn off this feature with %automagic off statement.




Using Spyder available in the Anaconda folder, I will write a 4-line script. Spyder is a popular Python Integrated Development Environment. It has a useful documentation window. It offers two kinds of shell: original Python and IPython.




In this example I use the magic %run to execute the file shown. It outputs a simple figure. We can use %who to see what are the variables in memory. Next we use %reset to clear all variables, so now %who call will indicate there is nothing in memory.




A very useful feature is tab autocompletion. Pressing tab after typing something will give suggestions. Earlier we saw %who returned the variables in memory. One of the variables was plt (an alias for matplotlib.pyplot module). By typing plt dot and pressing tab, I get list of functions inside this module. By typing more and then pressing tab, the suggestions narrow. Finally if you type enough and press tab, it will autocomplete since there is only 1 suggestion. I will later show how this is helpful in file navigation.




Besides the plotting library matplotlib, numpy is a very important library. Numpy stands for numerical Python and is the most important mathematical library of Python. It's useful features include data structures. It includes many mathematical functions. However most mathematical functions are in another related library (scipy). It allows for vectorized math, that is element-wise operations on arrays. It has optimized C language objects which have thoroughly tested.




I do not have to use Spyder to write Python scripts. I can use any text processor such as Windows Notepad. I wrote a 6-line program in Notepad++ (http://sourceforge.net/projects/notepad-plus/). This program uses the numpy functions linspace to create a linear line. Next I used the sin() function, available in the numpy module to create one period of the sine wave. Finally it is plotted.




This shows the Sine graph over one period.




%whos shows more information than %who.




We can also check variables. For example the variable x shows a line, composed of 50 elements, which goes from 0 to 1.




This is the video of Tutorial 1.




2 comments:

  1. This is great - amazing find! It's a shame it's out of date already. Any chance of an update?

    ReplyDelete

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.