Getting started with SciPy/Python

Scipy Links to an external site., is a lot like another program you might have heard of, matlab, but also has the nice feature that it's based on python which is a nice language to code in. Like everything else it has its pluses and minuses, but certainly for pedagogical purposes, it's a great tool. It lets you visualize using great graphics, and has a large number of numerical techniques built-in so that you can quickly analyze data and some theoretical idea that you might have. It's a nice front end to algorithms that are efficiently implemented in C and Fortran, but those low level gnarly details are nicely hidden from the user, most of the time.

Because it serves a scientific niche, it's not quite so simple to install as more popular software, but nevertheless, it can be successfully installed on many versions of Windows, Linux and Mac OSX.

However this is a moving target, and if you haven't updated your OS is a while (especially for macs) you will likely find that you can not install the software that we'll use. So the obvious solution is to update your operating system.

Another piece of software that you must get working is Visual Python Links to an external site.. This can also be installed along with Scipy but can be even trickier to do, especially with macs.

INSTALL SCIPY

Anaconda

At the moment, anaconda, http://continuum.io/downloads Links to an external site. works on recent operating systems, both Windows, and Macs. It will install scipy for you. To get it to install visual python, go to a command prompt (terminal on a Mac) and type:

conda install -c https://conda.binstar.org/mwcraig Links to an external site. vpython.

(Thanks to Stas Fridland for this advice.)

I have found that the code with animations (using matplotlib) don't show up properly on macs unless you take an additional step. Open up a terminal and go to your home directory:

 

cd

then see if .matplotlib exists by typiing

ls -a

and seeing if it's there.  If it doesn't exist, type:

mkdir .matplotlib

which will make the .matplotlib directory.

Once you have that directory, then type:

cd .matplotlib

and add this file names matplotlibrc Links to an external site. to that directory. Its contents are just: backend: WXAgg 

[You can also make a new file with vi (or more precisely vim). But if you've never used it, one way to do create this is to first create the file with

and the edit the file with TextEdit. You can do that the silly way everyone does on a mac through spotlight, or you can use the command line in terminal and type

open -a TextEdit matplotlibrc
]

With this graphics backend, graphs with matplotlib look OK. You should definitely test out the system to make sure that it does work. To do this go to Test Your System below and follow the instructions.

Windows Unix Emulator

For windows you need two things to get this properly working, as far as I can tell. The first is cygwin. Go to http://www.cygwin.com Links to an external site. and click on "Install or update". When it sets things up, it'll give you the option of how much you want to install. Don't be miserly about disk space. At the very top level of the tree, make sure that you choose to install everything! As of late, I've noticed that this can be slow and painful. Hopefully the situation will improve, but if not, you can install the gcc compiler for windows by itself, in a package call MinGW Links to an external site.

If you're a windows user, cygwin will give you a very nice command line interface with zillions of free useful tools like a C compiler (which scipy will need) and great editors. Once this is all in place, now go on to the next task which is installing python/scipy. There's is also bash on ubuntu Links to an external site., which is relatively new but looks promising. At this point, it might be a better option to try than cygwin, if you're not having much look with it.

Getting SciPy set up on Windows

For windows 7 and 8, go to https://code.google.com/p/pythonxy/wiki/Downloads Links to an external site. to install "pythonxy". It has tons of free goodies including scipy, and also has a very professional looking developing environment based on eclipse. I chose the Web Edition to do the download. It's also got a great 3d visualization software, http://vpython.org Links to an external site. that we'll use extensively and allows you to easily create real time 3d images of simulations and data.

Another way to install python/scipy, there is a company called enthought that will let you download this for free if you're an academic. Go to http://enthought.com/products/edudownload.php Links to an external site.. However there have been some issues recently getting this method to work, whereas pythonxy seems to work fine.

NOTE: If you already tried enthought/canopy and it failed, don't immediately try pythonxy. Uninstall canopy following these directions: https://support.enthought.com/entries/23580651-Uninstalling-Canopy Links to an external site. .

Vpython on Windows

At the moment visual python, vpython is not in the enthought distribution be we'll make extensive use of it, so download it from http://www.vpython.org/ Links to an external site. . It is however already included in pythonxy.

Macs

With the last couple of releases of OSX, 10.11 and 10.12, people have had luck with the anaconda instructions above.

Older Mac OS instructions

 

Anaconda appears to have had problems playing nice with vpython: https://groups.google.com/forum/#!topic/vpython-users/_BkzxuorIas Links to an external site.

but as of late, I've found that my instructions adding matplotlibrc seemed to cure these.

Linux

For linux users, I've found that it's system dependent. I've managed to always get a working version of SciPy and Visual Python installed on ubuntu, so I'll give you instructions for those. I've got it working on many versions of ubuntu, 12.04, 13.04, 14.04, and 16.04.

After installation, you may see an update manager window with a list of software that needs to be updated. Do the updates.

Now that ubuntu has given up on gnome, it's a bit rougher then it used to be. First go get the Synaptic Package Manager. You can get this various ways, In 12.04 you click on the party-bag icon on the left hand side, "Ubuntu Software Center" and type "Synaptic" in the search bar. The second way, that most people prefer is to use the terminal application. At the very top left there is this icon that's purple with a white circular thing, that you can click on. Start to type in the text box. It'll search for what you're looking for. So start to type "terminal". It'll show you the terminal application. Open that up. Now you can issue commands much more efficiently. So to get synaptic, you go to a terminal and type:

 sudo apt-get update 

 

 

 sudo apt-get install synaptic.  

 

Once you've installed synaptic, you can fire it up (using the purple/white circle icon as before). It's a really nice way to search and install new software. You'll need the root password. The other way is to use the terminal.

Then search for scipy and install python-scipy. Also search for matplotlib and install python-matplotlib. Install python-visual the same way. On 12.04, search for libgtkglextmm and install libgtkglextmm-x11-1.2-dev. You don't need to do this on 13.04 or 14.04. By the way, you can do all this from a terminal instead more easily by typing

 sudo apt-get install python-scipy python-matplotlib python-visual libgtkglextmm-x11-1.2-dev 

I also recommend you download a working version of vim, not the minimal one that comes with ubuntu. Search for vim and install. The files are quite small. You might also want to experiment with emacs. That can be installed the same way.

 

There are some video driver problems that I have run into, but following the suggestions athttp://www.vpython.org/contents/download_linux.html Links to an external site. such as reverting to older packages, has fixed them.

How to install linux on a PC

If you have 20GB of free space and you're not running windows 8, there is an easy way to get linux on your PC. Install wubi: http://www.ubuntu.com/download/desktop/windows-installer Links to an external site. which will install ubuntu 12.04 or 12.10. Other versions can also be installed but you can get 12.04 to run both scipy and vpython.

Last Resort

If all else fails, you can, for free install a virtual machine running ubuntu on your mac or PC. I'll concentrate on Mac's here because they have been the most difficult in the past. Actually, this really isn't such a last resort. With increased transfer rates, and more memory and RAM, this is a pretty reasonable way of running the code.

You can go to https://www.virtualbox.org/wiki/Downloads Links to an external site. to download the version of VirtualBox corresponding to your OS. However I tried this on a mac and found that the resulting VM was painfully slow. I'd instead recommend going to OSX Hosts https://www.virtualbox.org/wiki/Download_Old_Builds_4_2 Links to an external site. and downloading the version corresponding to your OS. Next is very important:

YOU DON'T NEED TO INSTALL UBUNTU FROM SCRATCH!

You can either download a 1.2 GB image of ubuntu and install all the other software yourself or download my 1.9GB image of ubuntu 14.04 that is ready to go with the code for this class and software already on it: ubuntu14.04_biophysics_vbox.zip Links to an external site. .

Downloading Ubuntu and Installing Scipy etc

There are images of ubuntu already available. At the moment here is an image for ubuntu 14.04 that works: https://virtualboximages.com/Ubuntu+14.04.01+amd64+Desktop+VirtualBox+VDI Links to an external site. (This image is free!.) When you download it, it will be packaged as a "rar" file.

If you'd prefer to download a 1.9GB file with everything already on it (and the only reason not to is because you might have troubling the larger file size), go get ubuntu14.04_biophysics_vbox.zip Links to an external site. .

Make a new directory (folder), say called "virtualbox". If you downloaded from virtualboximages it will be a rar file, the preinstalled file is zip format. Place the rar or zip file in there. Then on a mac, you can unrar the rar file. To get unrar, use homebrew that you should have already installed (see above) to do

 brew install unrar 

It should inflate the contents, and give you a bunch of files, including a huge .vdi file.

 

If you choose the preinstalled file, it's zip format. You can unzip with "unzip filename.zip" (command line), or a number of other tools.

Now when you fire up virtualbox, you can use your .vdi image to make a new "virtual machine". When creating a new virtual machine, choose an amount of memory that won't kill the performance of you host OS. 1GB is probably fine for most purposes. If you have 8GB or more on your host you could make this 2GB. When it asks about the "Hard Drive", DO NOT CHECK "Create a virtual hard drive now". Instead check "Use an existing virtual hard drive file". You then give it the .vdi file inside your virtualbox directory, and it'll use that image to make your virtual machine.

You turn it on, and inside a virtualbox window you'll see linux magically boot up inside of your host OS. You can then make it full screen and it behaves almost identically to a real machine running linux as mentioned above. NOTE: the password for these images is adminuser . You should also be able to make the VM window fit your entire screen. It acts quite close to running linux natively. Simulations are pretty snappy.

Now you can go ahead and install SciPy and Visual Python inside your virtual linux box as stated above. Or if you use the pre-installed 1.9GB zip file, you should be done with the installation.

 

VMware is another alternative to VirtualBox. If you had trouble with VirtualBox, lately I've seen that vmware will also work. 

Test Your System

Here Links to an external site. is a zip file of two scipy programs, elect.py that uses visual python and quart_dend.py that uses matplotlib called tst.zip. But for anaconda on a mac (see the yellow highlighted text below) here Download here is the slight variant of the code you should use, at least on a mac.

On both linux and a mac, get a terminal program move to an empty directory and unzip this file:

 unzip tst.zip 

Now type

 python quart_dend.py 

and a screen should pop up with a growing dendrite.

 

Now type

 python elect.py 

and a screen should pop up showing a red worm moving through an array of sticks (like on the home page).

 

On windows, by clicking on the files, you should be able to run the simulations. If you open a cygwin terminal and try to run it by command line, it probably will not work as is. What should work is if you go to Start, and find the program Enthought. Under Enthought you should see "PyLab". If you click on that, it will give you another shell terminal. If you now navigate to the right directory and type "run elect.py" after a few seconds, a vpython window should pop up with a read worm in an array of sticks (as above). You navigate using the "cd" shell command that is used on unix systems like Linux. PyLab is the same thing as ipython Links to an external site., which is also available in command line, for both Mac and Linux. It's a useful tool.

Both programs should work. If not you do not have them properly installed and won't be able to do the work in this class on your computer.

If the code here isn't showing up right (e.g. the window is black) then try the zip file here Download here instead. Especially if you're using anaconda on a mac. If that cures your problems, then you should download this alternate zip file Download zip file  and use that for the course, and not the ones other ones that are referenced in the homework assignments.  Also in anaconda when you're in a terminal, unfortunately you don't type "python" but "pythonw". With other installations I've come across, you don't have to do that.

Unix Shell Tutorials

http://www.ee.surrey.ac.uk/Teaching/Unix/ Links to an external site.

http://matt.might.net/articles/basic-unix/ Links to an external site.

http://manuals.its.virginia.edu/unixtut/index.html Links to an external site.

Python Tutorials

http://docs.python.org/tutorial/ Links to an external site.

http://www.sthurlow.com/python/ Links to an external site.

SciPy Cheat Sheet

http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html Links to an external site.

SciPy Documentation

http://www.scipy.org/doc/api_docs/ Links to an external site.

SciPy Tutorial

http://scipy-lectures.github.com/ Links to an external site.

http://www.scipy.org/Tentative_NumPy_Tutorial Links to an external site.