Friday, March 23, 2012

BCES code ported to Python!

Just ported the good old BCES linear regression algorithm (the four methods described in the Akritas & Bershady 1996 paper), originally written in Fortran 77, to Python!!

I am testing the python code and making sure it produces exactly the same answers as the old code.

More news soon...

Update (March 26th 2012): implemented the bootstrapping BCES!

Update (May 19th 2014): the BCES python code is now available as a Github project. Feel free to download it and even contribute improvements.

Thursday, March 22, 2012

Simple progress bar in terminal

If you need to incorporate a simple progress bar in your code, there is a module that does that and is very easy to use: fish.

The following script illustrates how to implement a simple progress bar which advances each time a loop counter increases.

 import fish  
 import time  
   
 steps=input('How many steps? ')  
   
 # Progress bar initialization  
 peixe = fish.ProgressFish(total=steps)       
   
 for i in range(steps):  
      # Progress bar  
      peixe.animate(amount=i)  
        
      time.sleep(0.1)  

Here is a screenshot of what the progress bar looks like in action:


Wednesday, March 7, 2012

How to install a scientific Python environment on Mac OS X Lion

My Mac OS X workstation was just updated to Lion and I had to reinstall Python and associated scientific tools for plotting, statistics etc. What a pain.

After many trial-and-error procedures I finally found a way to get a scientific Python environment (Python + Scipy + iPython + Numpy + matplotlib) working correctly on Mac OS X Lion. I am reporting the steps I carried out hoping that it will help other people.

You will need a Python installation (in my example I use the one that comes by default with OS X), gfortran and Xcode. Here are the steps:
  1. Install the requirements: Xcode which includes Python (via App Store), gfortran (via Macports), virtualenv, additional libraries required by matplotlib
  2. Create a python environment with virtualenv
  3. Install Numpy, Scipy, matplotlib, ipython with pip. Install readline with easy_install
  4. Create an alias in .profile or .bash_profile (depending on your shell) to run ipython
After these steps are completed, you will get a working Python environment for scientific analysis, visualization and statistics with Mac OS X Lion. 

Requirements
  1. Xcode
  2. Python 2.7, which comes pre-installed by default with OS X
  3. gfortran
  4. virtualenv
  5. additional libraries required by matplotlib (optional)

1. How to get the requirements working

gfortran

In my case, I installed it by installing MacPorts and installing GCC which comes with gfortran:

 sudo port install gcc44  

To make gfortran visible to the system I created an alias in /usr/local/bin:

 cd /usr/local/bin/  
 sudo ln -s /opt/local/bin/gfortran-mp-4.4 gfortran  

virtualenv

I went to web page that hosts virtualenv and downloaded virtualenv.py. You will use virtualenv.py below.


Additional libraries required by matplotlib (optional)

I use the graphical backend TkAgg, which requires the following additional libraries for matplotlib to work: tk, freetype, libpng. I installed them using macports:

sudo port install tk
sudo port install freetype
sudo port install libpng


2. Create a python environment with virtualenv

Create a directory stdpy (in my example) somewhere and issue the command

 /usr/bin/python virtualenv.py stdpy  

to create an isolated python environment based on the python provided by default with Mac OS X. This avoids trouble with mixing libraries. Activate the environment by running

 source stdpy/bin/activate  

You should now see a (stdpy) showing up in your terminal.

3. Install Numpy, Scipy, matplotlib, ipython with pip and readline with easy_install

After activating the python environment, let's proceed and install the additional modules with pip and easy_install:

 pip install numpy  
 pip install scipy  
 pip install matplotlib  
 pip install ipython  
 easy_install readline  

You may need to install additional libraries in order to get matplotlib compiled, depending on the kind of graphical backend that you choose. In my case, I use TkAgg which depends on Tk, freetype and libpng libraries which I installed via macports.

4. Create an alias in .profile or .bash_profile (depending on your shell) to run python

In my case I use Bash and I added the following line to the file .bash_profile in my home directory:

 alias ipy='source ~/stdpy/bin/activate && ipython --pylab'   

Now, when I open the terminal and issue the command

 ipy  

it will automatically activate the python environment and run ipython.





Changelog:

  • Aug. 18th 2012: added instructions about additional libraries in matplotlib
  • Sep. 1st 2012: made explanation about matplotlib dependencies clearer (hopefully)

Thursday, March 1, 2012

How to make Homebrew's Python the default version

I am trying out Homebrew and the Python that comes with it. I was having the problem that when I try to run Homebrew's python in the shell, I ended up running the outdated python that comes with OS X instead.

Here is how to make sure that Mac uses the homebrew python instead of the version shipped with OS X.

I am assuming you are using bash. To find out which shell you are using, type:
 echo $SHELL  
I am also assuming you installed homebrew in /usr/local (the default installation directory).

Now, edit the file .bash_profile (e.g. using nano) in your home directory and add the following line in the end of the file:

 # Homebrew Python instead of Apple's  
 export PATH=/usr/local/bin:/usr/local/share/python:$PATH  

Now restart your terminal and there you go!