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. A lot of the most exciting science and engineering projects extensively use python now, such as in AI.
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. It might not be necessary, so if you are loath to do that, I've provided some options.
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.
INSTALL SCIPY
Anaconda on Mac OS X
At the moment, anaconda, https://www.anaconda.com/distribution/ Links to an external site. works on recent operating systems, Windows, Linux, and Macs.
Now the way you will install this software will depend on the version of Mac OS X you are running.
For the most recent version, High Sierra, and Mojave:
You can choose either Python 2 or Python 3 to install from Anaconda. For the software to all be compatible, please choose Python 3., Do not rely on the version of python on your mac. That path will likely not be fruitful. It's best to download Anaconda. Now the trick here is that the recent version of python3 in anaconda is too recent! Yes, software developers often decide to make previous software incompatible with newer versions. This is most unfortunate, but there is a way of getting around this because Anaconda has kept earlier versions of software in it's repository. You can then create new virtual environments with the older but more useful packages. So fire up a terminal (command-shift opens Spotlight, then start typing terminal). You will be using terminal a lot in this course. Once you have a terminal type the following:
conda create --name p3 python=3.6
The machine might think for a while and ask you a question or two, but hopefully it won't bomb out.
This creates a new environment that I've called "p3" but you could call it "camel", "teapot", or any other word that you think that you'll remember and is easy to type.
Now you want to jump into this new environment, so type:
source activate p3
Now you'll see a the name of your environment (p3) in your command line prompt. If you want to return to your normal version of anaconda python, your type "source activate". And then, if you've messed up the installation process and want to start from scratch, you can type "conda env remove -n p3" and start over again.
OK, now you're in your new python 3.6 environment. To install everything else you'll need, type:
conda install -c vpython vpython scipy=1.1.0 numpy=1.15.0 matplotlib pip
You can use the cut and past features of your mac so you don't mistype anything here. It needs to be precisely this.
You're halfway there. Now you need to type:
pip install git+https://github.com/piannucci/weave@python3
Again, this needs to be precisely this and you need to type these commands in the order given. Believe me, I spent a lot of time trying to simplify this process and get it right and have tested it out on both High Sierra and Mojave, and it worked in both cases.
Things still won't work! To get graphic animations to show up, you need to install a startup file:
Code with animations (using matplotlib) don't show up properly on macs unless you take an additional step. Open up a terminal if you haven't already and go to your home directory:
cd
then see if .matplotlib exists by typing
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
to change to this directory, and add this file name matplotlibrc Links to an external site. to that directory. Its contents are just:
backend: TkAgg
[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.
For OS X El Capitan:
Type
conda install -c vpython vpython scipy=1.0.0 numpy=1.10.4 matplotlib
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.)
Now the instructions for getting graphics animations to work are almost the same as above but the contents of the matplotlibrc file should instead be:
backend: WXAgg
Even Older Mac OS instructions!
(It's unlikely anyone would be needing these)
Windows 10
Anaconda works on windows, so you'd think that you should be able to follow the directions above for the Mac to get it to work. No such luck! There seems to be a bug that doesn't allow weave to work with the cygwin gcc compiler. But all is not lost! There is now a linux subsystem in windows 10, and if you use that, you can get everything to work fine (so do not install anaconda). I'll explain how below.
There is a cool feature built into Windows 10 that you can enable. Here are detailed instructions Links to an external site. on how to do it. The important thing that you need to do is to install Ubuntu 16.04 LTS, NOT Ubuntu 18.04! To install 16.04 from the Microsoft app store (don't worry, it's free), search for Ubuntu 16.04 LTS. Don't install the default Ubuntu app! It'll take a while to install, but if you follow the directions, you should get a "bash" terminal that behaves just like you were in linux!
But the catch is that this can to nifty command line apps, but it can't do graphics. For that you'll need to install a server. I used the Xming server Links to an external site. and it worked fine. Here Links to an external site. are some detailed instructions on how to do that. Aside from installing the Xming server (the defaults seem to work fine), you need to type this crucial line into the bash terminal:
export DISPLAY=:0
Every time you open up a bash terminal, you'll need that instruction. Now you need to go get the software that we'll be using. This is identical to the instructions below:
sudo apt-get update
sudo apt-get install python-scipy python-matplotlib python-visual
(Don't try to install the graphical synaptic package manager).
Now you're ready to test out your code. But to access your program files, including your download directory (or folder), you need to go to your bash terminal and type
cd /mnt/c/Users
If you type
ls
it should list your username there. cd to that name, and you're back in your own user directory (i.e. folder). You can access your Downloads directory, and proceed to the testing section below and download a big zipball of the course programs. This version uses the python 2 code tree in the zip file.
For the last homework project of the course, you'll also need to install two more things so you might as well do them now:
sudo apt install unzip
sudo apt-get install make
Linux
For Linux, you can use the same anaconda method as well, however there is an even easier technique. I assume that if you're using Linux, you already know a lot about operating systems. What I tell you about is ubuntu, which is a flavor of Debian linux. Unfortunately if you're running Ubuntu 18.04, they no longer have vpython as a package. But 16.04 does and it's still being supported. Earlier versions from 12.04 on, work as well.
What I recommend doing is downloading the Synaptics Package Manager. If you like the terminal, type
sudo apt-get update
sudo apt-get install synaptic
But you can do it graphically as well. If you do that, it's just a matter of installing a few packages through their WYSIWYG. Search for visual python. If you scroll down the list of results, you'll see Visual Python (sometimes called vpython) right there. Click on it, and select "Mark for installation". Then click "Apply". It'll install that. After that, install matplotlib the same way. Then python's "weave". That should be it!
If you dislike WYSIWYG's just type:
sudo apt-get install python-scipy python-matplotlib python-visual
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.
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. But you probably shouldn't do this because:
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 (not recommended) 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.
and a more recent ubuntu 16.04 version of 3 GB: 180VM.zip Links to an external site.
If you choose one of those preinstalled image file, it's zip format. You can unzip with "unzip filename.zip" (command line), or a number of other tools. (It'll take a while.)
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 for the (14.04 image) and 2GB for the 16.04 one. If you have 8GB or more on your host you should easily be able to afford 2GB for your VM, if not, shut down a lot of your other processes. 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 14.04 is adminuser and for 16.04 is osboxes.org . 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. To make it full screen you need to install "Guest Additions" on Virtualbox. You'd think this could be made simpler. There's a lot of documentation but this is one of the simpler explanations Links to an external site. on how to do this. Then you can share files between Links to an external site. your host and guest as well.
If you don't want to try to get guest additions to work, you can resize the virtualbox window, and it will run in pretty low resolution. But I found that it works fine except some of the simulations colors look like an acid trip. You can still tell what's going on most of the time.
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 the zip file of all programs used in this course, both for python 2 installations (i.e. older ones or on a VM) and newer python 3 version.
You can see how it works in elect.py (in the hw6 directory) that uses visual python and quart_dend.py (in the hw4 folder) that uses matplotlib . But for anaconda on a mac (see the yellow highlighted text below).
To unzip the file, you can have it do so automatically or do what I prefer: go to a terminal and type:
unzip code_for_students_py.zip
Now change (cd) to the correct folder, and type this in a terminal, do not try clicking on the file!
python quart_dend.py
and a screen should pop up with a growing dendrite.
Now cd to the correct folder and 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). If you're running an earlier version of anaconda, like on El Capitan, you'll have to type pythonw instead of python.
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. I expect that this won't be easy for a lot of people, but once you've got these programs working, then it should work for at least most of the remaining ones.
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.
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
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.