Monday, November 17, 2014

Passing lists in numba

If you plan to @jit() a Python function using numba, and one of the arguments is a list, it will be treated as an object, and the jit-ted function will probably be slower than the original. Instead, explicitly specify the argument data types (e.g. int32, double, unit64, etc.) within the @jit([data types]) declaration and, importantly, when calling the function, remember to convert the list into a numpy array of the data type specified in the jit declaration. For all your efforts, you should be rewarded with a good speed-up (if you have long-running loops).

Wednesday, November 12, 2014

Check whether the fonts are embedded in a PDF document

To check whether the fonts are embedded in a PDF document, do the following.

On Ubuntu

Run pdffonts mydoc.pdf at the terminal. It will show a table listing all fonts in the PDF document, and the 'emb' field is 'yes' if the font is indeed embedded.


Open the document in Adobe Reader, and select File...Properties. Under the 'Fonts' tab, you can see the list of fonts. Embedded fonts are followed by the phrase 'Embedded' or 'Embedded subset' within brackets.

Wednesday, October 15, 2014

Debugging in iPython

To debug a function func in module, do the following at the iPython prompt.
from IPython.core.debugger import Pdb
ipdb = Pdb()
import mod
ipdb.runcall(mod.func, [args, for, func])

This will start the iPython debugger at the first line of func.

Saturday, September 27, 2014

Code formatting in Blogger

I found this answer on stackoverflow about how to format source code in blogs on blogger, which pointed this tutorial with detailed instructions. It worked for me with Python! Many Thanks to David Craft and Alex Gorbatchev.

Also note the useful tip here about loading only the format styles that you need for your blog, to speed up page load.

How-to (for Python)

Add the following to Blogger template above the </head> tag.

In the blog editor, switch to HTML mode and insert code within <pre> as follows.

You can find more brushes here.

A matplotlib example

The following matplotlib script shows some cases where the matplotlib defaults were not sufficient for the job, and how to customize the relevant properties. The main tweaks were:
  • changing the font type from Type 3 to True Type
  • scaling the y values
  • using latex syntax in the labels
  • changing the fontsize for axis labels, axis ticks, and legends
  • two legends
  • using proxy objects (lines or patches) for the legends
  • using axis coordinates for locating the legend
  • semi-transparent legends and grid lines
Here is the python code and the resulting figure:

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.patches as mpatches
import numpy as np
import json
import sys
import os

# change font type to True Type to avoid Type 3 fonts
# (which are not allowed by some conferences)

mpl.rcParams['pdf.fonttype'] = 42

# x = 1-d array with x values
# ys = six 1-d arrays with y values (one for each line we want to plot).
#      Of these 3 are of one kind, and the other 3 are of another kind.
# xlabel, ylabel = labels for the x and y axis

x, ys, xlabel, ylabel = get_plot_info(datafile)

# I want to scale the y values so that the y axis is easier to read.

ys = ys / 10   # ys is a numpy array

# The plot will be shrunk when it is included in the paper, so that the 
# default fontsize becomes too small. Select a larger fontsize globally.

fontsize = 30

# Plot the first three lines in blue with different line styles;
# then plot the other three in green with similar line styles.

plt.plot(x, ys[0], 'b-', lw=3)
plt.plot(x, ys[1], 'b--', lw=3)
plt.plot(x, ys[2], 'b:', lw=3)
plt.plot(x, ys[3], 'g-', lw=3)
plt.plot(x, ys[4], 'g--', lw=3)
plt.plot(x, ys[5], 'g:', lw=3)

# Set axis labels with larger fontsize.
# Mention the scaling done to the y values.

plt.xlabel(xlabel, fontsize=fontsize)
plt.ylabel(ylabel + r' $\div 10$', fontsize=fontsize)  # latex syntax works!

# Increase the fontsize of the axis ticks

ax = plt.gca()
for labx in ax.get_xticklabels():
for laby in ax.get_yticklabels():

# If you want the lines to reach the left and right extremities of the graph,
# reset the x-limits.
ax.set_xlim( (min(x), max(x)) )

# Instead of showing 6 entries in the legend (one for each of the 6 lines),
# we show one legend for the color code, and another for the line style.
# ('MCTM-...' are methods, and 'en' etc. are languages for which we ran
# the method.)

# first legend (for color code)

blue_patch = mpatches.Patch(color='blue')
green_patch = mpatches.Patch(color='green')

# loc=(x,y) are the coordinates of the lower left corner of the legend,
# where (0,0) if the lower left corner of the axes, and (1,1) is the
# upper right corner.
# framealpha is the transparency level (0=transparent, 1=opaque).

leg1 = plt.legend((blue_patch, green_patch), ('MCTM-DSGNP','MCTM-D'), 
                  loc=(.3,.6), fontsize=fontsize, framealpha=.5)

# second legend (for line style)

# plot empty arrays in black (to avoid the colors used above).

l_en, = plt.plot([], [], 'k-', lw=3, label='en')
l_hi, = plt.plot([], [], 'k--', lw=3, label='hi')
l_hir, = plt.plot([], [], 'k:', lw=3, label='hir')
plt.legend((l_en, l_hi, l_hir), ('en','hi', 'hir'), 
                  loc='lower right', fontsize=fontsize, framealpha=.5)

# Add grid lines, but make them semi-transparent (otherwise they appear too
# prominent when the figure is shrunk).


# The large fontsize pushes axis labels out of the figure; but matplotlib
# offers a function to auto-correct this.


# The saving format is chosen automatically based on the file extension.

Sunday, February 16, 2014

Removing old linux kernel images in Ubuntu

To see how many linux kernel image versions you have, do ls /lib/modules. You will see a list of directories (e.g. 2.6.32-51-generic, 2.6.32-52-generic, etc.)---one directory per kernel image version.

Note down the versions that you want to remove (usually, you should keep the last two versions---the last one for normal booting, and the previous one as a fallback).

Open Synaptic. Search for the version number (e.g. "2.6.32-51"). Scroll down the entries for the kernel image and kernel headers, e.g.
  • linux-image-2.6.32-51-generic
  • linux-headers-2.6.32-51-generic
  • linux-headers-2.6.32-51
Usually three entries of the kind mentioned above will be shown as installed in Synaptic. Choose each one, and "Mark for Complete Removal". Apply the changes.

After the Synaptic finishes applying the changes, you can see that the corresponding directories have been deleted from /lib/modules. The next time you start Ubuntu, the removed kernel images will not appear in the (GRUB) boot menu.

Wednesday, February 5, 2014

Using bc in calculations

To use the linux command bc for calculation in shell programs, do the following:
  • Add the line scale=2 to the file ~/.bcrc.
  • Then use var1=`echo "($var2+$var3)/$var4" | bc` to set var1.
  • The scale parameter tells bc to use 2 decimal digits.