(A Creative Blog Name Here)

Code, math, and other things I find useful

Fixing IPython plot windows after OS X upgrade

Note: These instructions are for the Conda Python environment manager. They should be adaptable to Virtualenv installations or others as well.

Often after installing OS X upgrades plots created in IPython open behind all windows and cannot be tabbed to. The following instructions should fix this:

  1. Remove the current python ...

Debugging Python Extensions With Valgrind

In order to get line numbers when using Valgrind to debug Python extensions written in c/c++ you need to compile the extension with both the -O0 and the -g flags.

Calling Rmath from cython (sometimes R just does it better)

Recently I was writing some code that required the quantile function of the chi-squared distribution. There is a function in ccipy that implements this function, however, calling a python function from cython is slow. The scipy implementation calls a function in the cephes library. Unfortunately, scipy compiles the cephes library ...

Cleaning Cython Build Files

Whenever I use a setup.py script to build Cython code a variety of files are created including a .so file, one or more .c files and perhaps others. Until today I would delete these by hand as I did not have the patience to delve into what a setup ...

Matplotlib 3D plots in IPython

Summary:

Assume that

import matplotlib.pyplot as plt

has been executed.

When using mplot3d in IPython you have to manually draw the axes which can be done using the plt.draw function (or plt.plot([],[])).
For some reason the axes are not automatically drawn as with 2d plots.

The full ...

Extracting keys from Python dict into interpreter namespace

I run a lot of experiments in ipython, the results of which I pickle and save to disk. When I reload the results with pickle I end up with a dict whose keys are the variable names that I saved. It is tedious to have to reference these values through ...

Cython and GSL made easy

I just found CythonGSL on Github that wraps the GSL library for use with Cython. Installation is simple

$ git clone git://github.com/twiecki/CythonGSL.git 
$ cd CythonGSL
$ python setup.py build
$ python setup.py install

I have also added this to my behemoth instructions on setting up python.

Python build targets

While writing the code for my last post on Cython I encountered something strange regarding distutils. The code linked to the GSL library which on my machine (running OSX) is a unversal library containing both 32- and 64-bit versions. When I run

$ python setup.py build_ext --inplace

distutils attempts to ...

Cythonizing instance methods

A large amount of my work involves writing MCMC samplers. The models I work with have a lot of parameters and the samplers require a lot of state to be recorded. Normally, I stuff all of the parameters and state into a Python class and implement the different steps of ...

IPython parallel

When I start IPython the first two commands I run are

%load_ext autoreload
%autoreload 2

which forces IPython to reload code before running it so that the newest version is always used. However, when developing code to run in parallel with the IPython.parallel module on a cluster started with ...