Thursday, July 15, 2010

matplotlib basics


Simple Plot
import
matplotlib.pyplot as plt
plt.plot([1,2,3,4])
# plot a set of points (1,1) and (3,2)
plt.plot([1,3],[1,2],'ro')
# Or plot as a bunch of connect line segments
plt.plot([2,4,6],[2,4,6])
# set a label for the x and y axis
plt.ylabel('some numbers')
plt.xlabel('joe rocks')
# set the axis [xmin, xmax, ymin, ymax ]
plt.axis([0,6,0,5])
# 'show' the graph

plt.show()


Plot a function
As far as I can tell matplotlib cannot plot a continuous function.
Instead, create a range of distinct numbers:

import numpy as np
import matplotlib.pyplot as plt
# create a range of numbers from 0 to 5. increment by .1
x = np.arange(0.,5.,.1)
# apply a function to that
y = np.sin(x)
# plot with extra display params
plt.plot(x,y,linewidth=2.0, label='joeplot', color='blue')
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.axis([0,5,-1,1])
plt.grid(True)
plt.show()

Make a cool heatmap
import numpy as np
import matplotlib.pyplot as plt
#x = y = np.linspace(-5, 5, 12)
# create a grid of 12 X coordinates and 12 Y coordinates
# these coordinates will be used to represent specific locations on
# the grid. the meshgrid() basically creates a nice uniform coordinate grid
X,Y = np.meshgrid([1,2,3,4],[1,2,3])
# X,Y = np.meshgrid(x, y)
# these ravel functions basically make the 2d arrays created above into lists
x = X.ravel()
y = Y.ravel()
plt.subplot(111)
plt.hexbin(x,y,C=[1,2,3,3,2,2,3,3,2,3,3,4], gridsize=30)
cb = plt.colorbar()
cb.set_label('Heat Value')
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.grid(True)


http://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set

Friday, July 09, 2010

Case Study Notes: install pyqrcode in a virtualenv on OSX

Kind of a buggy install.

mkdir qrcode; cd qrcode
virtualenv --distribute --no-site-packages ve
source ./ve/bin/activate
echo "pil" > requirements.pip
echo "http://svn.apache.org/repos/asf/lucene/pylucene/trunk/jcc" >> requirements.pip

wget http://downloads.sourceforge.net/pyqrcode/pyqrcode-0.2.1.tar.gz?use_mirror=

cd pyqrcode-0.2.1

# EDIT pyqrcode-0.2.1/Makefile
# change the call to jcc from
GENERATE=python -m jcc --jar $(LIBFILE) \
# TO
GENERATE=python -m jcc.__main__ --jar $(LIBFILE) \

make
make egg

# it will create qrcode-0.2.1-py2.6-macosx-10.6-universal.egg
# Edit qrcode-0.2.1-py2.6-macosx-10.6-universal.egg/qrcode/__init__.py
# change the line
_qrcode._setExceptionTypes(JavaError, InvalidArgsError)
# TO
_qrcode._set_exception_types(JavaError, InvalidArgsError)

pip -E ./ve/ install qrcode-0.2.1-py2.6-macosx-10.6-universal.egg

# http://pyqrcode.sourceforge.net/
# http://www.mail-archive.com/pythonmac-sig@python.org/msg09864.html
# http://mail-archives.apache.org/mod_mbox/lucene-pylucene-dev/200904.mbox/%3C49EEECAC.7070606@cheimes.de%3E

Installing MySQL-python (MySQLdb) in a virtualenv on OSX 10.6

There were two problems with the MySQL-Python package that I had when installing on OS X 10.6
1) First, an error saying "EnvironmentError: mysql_config not found" when setup.py is run (through pip or easy_install)
2) Second, an error saying "ImportError: dynamic module does not define init function(init_mysql) when importing MySQLdb" when 'import MySQLdb" is issued

Causes:
1) This is caused by the build script not being able to find a MySQL program called mysql_config. This program is used to determine metadata about the mysql install. For example, the command "mysql_config --cflags" reports the flags used to build mysql.
2) This is caused by a mismatch between the architectures that MySQL was built with and the architecture that MySQL-python installer is trying to build and install MySQL-python as. To find what arch MySQL was built as, run
"mysql_config --cflags" (this was i386 in my case). OSX tries to build MySQL-python as x86_64. Therefore, there is a arch mismatch.

Solution:
1) Locate the mysql_config binary location and add the path to the MySQL-python/site.cfg file as a config directive: "mysql_config = /opt/local/bin/mysql_config5"
2) Use the arch x86_64 (64 bit) version of MySQL compiled through MacPorts, not a i386 (32 bit) version downloaded off the internet and installed as a dmg.

Summary:
Assuming the MacPorts version of MySQL is installed, both issues can be solved by adding the mysql_config = /opt/local/bin/mysql_config5 config directive to the MySQL-python/site.cfg file.

# SCRIPTED SOLUTION: Create a script called setup.sh and add the following lines
virtualenv --distribute --no-site-packages ve
source ./ve/bin/activate
pip install -E ./ve -r requirements.1.pip # other deps
export ARCHFLAGS="-arch x86_64"
pip install -E ./ve MySQL-python
echo "mysql_config = /opt/local/bin/mysql_config5" >> ./ve/build/MySQL-python/site.cfg
pip install -E ./ve MySQL-python


Resources:
http://groups.google.com/group/python-virtualenv/msg/cf4f3117faea476b?pli=1
http://birdhouse.org/blog/2009/02/21/python-mysql-connections-on-mac-os/
http://stackoverflow.com/questions/2111283/how-to-build-64-bit-python-on-os-x-10-6-only-64-bit-no-universal-nonsense