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():
    labx.set_fontsize(fontsize)
for laby in ax.get_yticklabels():
    laby.set_fontsize(fontsize)


# 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)
plt.gca().add_artist(leg1)

# 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).

plt.grid(alpha=.5)


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

plt.tight_layout()


# The saving format is chosen automatically based on the file extension.
plt.savefig(figname+'.pdf')

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.
Here
  • The scale parameter tells bc to use 2 decimal digits.

Monday, November 25, 2013

Very Quick File Server

Just cd into the desired directory, and run python -m SimpleHTTPServer. You get the message "Serving HTTP on 0.0.0.0 port 8000 ...". Now go the browser and type localhost:8000, and start browsing files/folders in that directory. Share the link http://<your IP addr>:8000 to let your friends browse as well.

[Tested on Ubuntu 10.04 (64-bit) with Python 2.6.5.]

Wednesday, October 16, 2013

Piping stderr

To pipe stderr (instead of stdout) use `|&' instead of `|'. This works from Bash 4 onwards. It is shorthand for `2>&1 |', which redirects bytes sent to stderr (2) to stdout (1), and then pipes it (|).

[Courtesy stackoverflow.]

Saturday, October 5, 2013

Simple Java development setup

A simple setup for small Java development projects could be:
  • Create the source directory ($PROJ_HOME) which will contain all .java files.
  • Create the directory $PROJ_HOME/proj_name which will contain all the .class files.
  • Add $PROJ_HOME to the CLASSPATH.
  • During development, keep a terminal open with working directory $PROJ_HOME, and run "javac *.java -d ." to compile all source files and store the class files in proj_name.
  • Run a class from anywhere using java proj_name.ClassName [args].
In addition:
  • Store metadata files etc. required by the code in $PROJ_HOME/meta. If they need to be passed by command line arguments, you can pass $PROJ_HOME/meta/datafile.txt as an argument wherever you are running the class.
  • Store sample test data files to quickly test the code in $PROJ_HOME/test.