cvxpy, cvxopt and convex optimization

cvxpy module is a nice wrapper around cvxopt that follows paradigm of a disciplined convex programming.
Its a bit slower, but has a very similar syntax as cvx toolbox for Matlab (cvx in Matlab is great!).
Here are some notes:
In cvxpy max() function accepts only one arguement while in cvxopt it behaves differently depending on either there is a variable passed among arguements. Use numpy.maximum() function in cvxpy to do element wise maximum of two vectors.

Posted in convex optimization, Machine Learning | Leave a comment

Merging in mercurial with vimdiff

Put the lines below in .hg/hgrc file of a repository:
merge = vimdiff
vimdiff.executable = vim
vimdiff.args = -d $base $local $output $other +close +close

Now whenever you do:
hg pull
hg update
and if there are conflicting changes to be resolved, mercurial will open vim in diff mode with two windows. Left window will show contents of a local file. Right window will show contents of a file being pulled. I usually work in the left window stepping through colored blocks which highlight the conflicts between the two files. When stepping through these blocks I use the two commands below:
This command changes the block of code in local file to match corresponding block in pulled file.
This command tells mercurial to use block of code in local file for the final version
Each time you navigate to the colored block of code and use one of the above commands, the conflict is resolved and coloring disappears. Repeat this until there are no more conflicts and then quit vim with
: xa
If something goes terribly wrong during merge use
: cq
This command will quit vim telling mercurial that conflicts are not resolved yet. Then repeat with
hg resolve list_files
To see a list of files to be resolved:
hg resolve -l

Posted in Mercurial, Productivity, Programming, UNIX/Linux, Vim | Leave a comment

Running script in background on linux

If we want to run a script from terminal and be able to close the terminal without killing a script (because terminal would be a parent process, so if it dies, its children die automatically), here is the command:
nohup python > python_script.out &
This will direct the stdout stream to file python_script.out
nohup tells OS to run program immune to hangups.
& tells OS to run program in background.
If we want to see updates coming python_script.out while the script runs in background, we can use a handy command:
tail -10f python_script.out

Posted in Bash, Productivity, Programming, Python, UNIX/Linux, Vim | Tagged , , | Leave a comment

Installing Scipy on Linux Red Hat

Tried to install scipy using easy_install (also pip). Installation failed with error:
Atlas ( libraries not found.
sudo yum install scipy
works seamlessly

Posted in EC2, Machine Learning, Programming, Python | Leave a comment

Resolving import errors in Python

If you get an error saying something like ‘Can not import SomeModule”, the first thing to check is that package (which is a regular directory with added file) is in PYTHONPATH.
For example, suppose you get an error when executing
from util import SafeMath

I would open python shell to check if its in PYTHONPATH and then to see which modules/files are seen by interpreter in case of potential name conflicts:

import sys

The above will display all directories in PYTHONPATH.
import util

The above will display all files/modules found in the package

Posted in Python | Leave a comment

Switching back and forth between tabs and spaces for Python indentation

It seems that majority of python developers prefer using 4 spaces for indentation. However, some people still like using tabs.
I am using python.vim script written by Eric Mc Sween. The file stays in .vim/indent directory.
There is a line saying
setlocal expandtab
This is when you want to use 4 spaces.
If you want to use tabs change it to:
setlocal noexpandtab

Posted in Uncategorized | Leave a comment

Problems with character encoding when piping output from a python script to a file

Suppose my_unicode_string is a Python unicode string. If you get an error when you do the following:


and the error is:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 9: ordinal not in range(128)

To get rid of the error, you need to explicitly specify encoding:

Posted in Programming, Python | Leave a comment