Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts

Saturday, February 4, 2012

Line Plot in R Using ggplot2

This post illustrates how to do the following using ggplot2:

  1. Draw a simple line plot.
  2. Change labels on the axes.
  3. Change the color of the plotted line and increase its thickness.
  4. Change the default background color of the area in which line is plotted.


Load ggplot2 package and create data for plotting




Line plot using ggplot2 with all default options


The output is:

The part aes(x=xcol, y=ycol) in above code specifies the aesthetic mapping, i.e., mapping between data and the things we can perceive on the plot. For instance, in the example above x-position on plot is mapped to data in column xcol and y-position on the plot is mapped to data in ycol (see Section 4.5 in Hadley Wickham's book on ggplot2) for more on aesthetic mappings.



Plot with custom labels for the axes

In the above plot the labels for the x and y axes are the same as the column names in the data. You can modify these labels if you wish. The following code shows how, using functions xlab() and ylab()


The output is:



Change color of the plotted line and increase thickness

You can change certain properties of the plotted line such as its color and thickness. To change its color, use the color property and to change its thickness use the size property. The default size is 0.5 and the default color is black. The following piece of code illustrates how this is done by specifying these as arguments of geom_line() function.


The output is:



Change the default background color of the plot area

The following code illustrates how to modify the background color:


The output is:

Note the use of opts(panel.background=theme_rect(fill='linen')) line to set the background color. A natural question is, which options other than panel.background can be specified using the opts() function? And, what are the possible theme values of these options? The list of options can be found on ggplot2 wiki page. You can also get the list of options and their default values by using theme_get() at the R prompt.

The following screenshot shows partial output from the theme_get()function:

I will end this post by combining all the different modifications we made above into a single script. The script and its output follow:


Output of the script:

Saturday, January 14, 2012

Drawing an SVG path using D3.js

In this post I show two ways of creating a simple SVG path using D3.js:

Method 1: the path data is specified in the same way as when using raw SVG (see here for more information).

Method 2: the path data is specified using path data generator method d3.svg.line() provided by D3.js

Method 1

I am assuming that you have the skeleton HTML file with link to d3.js. Add the following script to that HTML file. Open the file in the browser and you should see:

Method 2

The same path as you see above is drawn here. But instead of specifying the path as a string, as done above, I have used the d3.svg.line() method to generate it from the given set of points. As we expected, this code also gives:

Thursday, January 12, 2012

Creating SVG groups using D3.js - an example

Creating SVG groups with D3.js

The following piece of code gives an example of how to create SVG groups using D3.js, add graphical elements to these groups and simultaneously set attributes of elements belonging to a particular group. Scroll below to see the resulting figure.

The above code gives:

Wednesday, January 11, 2012

Understanding selectAll, data, enter, append sequence in D3.js

If you are new to D3.js and have looked at the various D3.js examples on the web to learn it, you have most probably come across a sequence of selectAll(), data(), enter() and append() statements as shown in Example 1 below. To a newcomer to D3.js, it is not obvious how these methods work. At least, initially, I did not find it easy to understand their functioning. If you are also having some trouble with understanding these methods and how they work together, I think the examples and explanations below will be helpful.

The key to understanding the working of these methods is in the following paragraph in Data Keys section on http://mbostock.github.com/d3/

"The key function also determines the enter and exit selections: the new data for which there is no corresponding key in the old data become the enter selection, and the old data for which there is no corresponding key in the new data become the exit selection. The remaining data become the default update selection."
and in Mike Bostock's response in this thread.

Don't worry if the above excerpt makes little sense at this point. Once you look at the examples and the explanation below, the meaning will become clearer. In all the examples below, we have used D3.js to create HTML paragraph elements. But the same principles apply for any other elements you create using D3.js.

Carefully study the following two examples and their outputs. Try to spot the difference between these examples and their outputs.




Example 1
 <!DOCTYPE html>
 <html>
 <head>
     <script type="text/javascript"
                src="http://mbostock.github.com/d3/d3.js">
     </script>
     <style type="text/css">
         p {
             font-size: 20px;
             color: orangered;
         }
     </style>
 </head>
 <body>
     <div id="example1"></div>
     <script type="text/javascript">
         pdata = [10,12,6,8,15];
 
         selectDIV = d3.select("#example1");
 
        selectDIV.selectAll("p")
             .data(pdata)
             .enter()
             .append("p")
             .text(function(d){return d;});
     </script>
 </body>
 </html>
Output of Example 1

Example 2
 <!DOCTYPE html>
 <html>
 <head>
     <script type="text/javascript" 
                src="http://mbostock.github.com/d3/d3.js"
     ></script>
     <style type="text/css">
         p {
             font-size: 20px;
             color: orangered;
         }
     </style>
 </head>
 <body>
     <div id="example2">
        <p>Already existing paragraph 1</p>
        <p>Already existing paragraph 2</p>
     </div>
     <script type="text/javascript">
         pdata = [10,12,6,8,15];
 
         selectDIV = d3.select("#example2");
 
        selectDIV.selectAll("p")
             .data(pdata)
             .enter()
             .append("p")
             .text(function(d){return d;});
     </script>
 </body>
 </html>
Output of Example 2

Already existing paragraph 1

Already existing paragraph 2


The only difference in the code in the two examples is the presence of two paragraph elements in Example 2 ("Already existing paragraph 1" and "Already existing paragraph 2") before the d3.js script is called. In the output of Example 2, you can see that numbers 10 and 12 do not show up, even thought they are present in the data array pdata. Why? This is what we look at now. Let us go through the script.

  1. Line selectDIV.selectAll("p") returns an array of all <p>...</p> elements in selectDIV.

    In Example 1 this array is empty. In Example 2, this array contains the two already existing paragraph elements.

  2. The line .data(pdata) specifies two things:
    (i) the data array and,
    (ii) the key function which assigns keys to the elements of the data array. These keys determine the enter selection — you can think of the enter selection as the elements in the data array that will be bound to nodes that you specify with the .enter().append() methods.

    You are probably thinking, "wait! I do not see any key function. And, how do the keys determine the enter selection?" Let us address these thoughts.

    As the creator of D3.js has said in this post, when a key function is not explicitly specified as an argument to the .data() method, it takes the default value of the index function. That is, the default key of an element in a data array is its index in the data array. For instance, since a key function has not been explicitly specified in Example 1, keys of elements 10, 12, 6, 8 and 15 are 0, 1, 2, 3, and 4 respectively.

    The keys determine the enter selection in the following manner: the keys of elements in the specified data array (which, in our examples is pdata) are compared with keys of the elements in the existing selection (the selection returned by .selectAll("p") method). Any element in the specified data array whose key is different from keys of all the the existing elements, becomes a part of the enter selection. If the key of a new element matches the key of one of the existing elements then it is NOT a part of the enter selection.

    Neither example above specifies an explicit key function. Hence, each elements key is its index in the array. In Example 1, selectAll("p") returns an empty array; there are no existing elements and consequently set of keys of existing elements is empty. The keys of elements in pdata, i.e., elements 10, 12, 6, 8 and 15 are 0, 1, 2, 3, and 4 respectively. Since all these have keys that are different from the keys of all the existing elements, they all become part of the enter selection.

    On the other hand, in Example 2, .selectAll("p") returns an array with two elements with keys 0 and 1 respectively. The keys of elements in pdata, i.e., elements 10, 12, 6, 8 and 15 are 0, 1, 2, 3, and 4 respectively.

    Since elements 10 and 12 have the same keys as keys of elements in the existing selection, 10 and 12 do not become part of the enter selection.
  3. Methods .enter.append("p") create as many <p>...</p> elements as the number of elements in the enter selection (see the previous step). The argument of .append() specifies the type of element to be created. Finally, .text() line adds the members of the enter selection to the paragraph elements created by .enter.append("p").

    In Example 2, since elements 10 and 12 are not part of the enter selection, no paragraph elements are created for them and they do not show up in the output.

Example 3 below is similar to Example 2. The only difference is that a key function is explicitly specified. Due to this, all elements in the pdata array show up in the output.


Example 3
 <!DOCTYPE html>
 <html>
 <head>
     <script type="text/javascript" 
                src="http://mbostock.github.com/d3/d3.js"
     ></script>
     <style type="text/css">
         p {
             font-size: 20px;
             color: orangered;
         }
     </style>
 </head>
 <body>
     <div id="example3">
        <p>Already existing paragraph 1</p>
        <p>Already existing paragraph 2</p>
     </div>
     <script type="text/javascript">
         pdata = [10,12,6,8,15];
 
         selectDIV = d3.select("#example3");
 
        selectDIV.selectAll("p")
             .data(pdata, function(d){return d;})
             .enter()
             .append("p")
             .text(function(d){return d;});
     </script>
 </body>
 </html>
Output of Example 3

Already existing paragraph 1

Already existing paragraph 2


Note the second argument in the .data() method in Example 3. We have specified the identity function as the key function. As a result, the keys associated with the elements are the values of the elements: Element 10 has a key of 10, 12 has a key of 12, 6 has a key of 6 and so on. None of these keys match the keys of elements in the existing selection, which consists of paragraph elements <p>Already existing paragraph 1</p> and <p>Already existing paragraph 2</p>. Hence in Example 3, unlike Example 2, since every new data element has a different key than those of the existing elements, it is a part of the enter selection and gets added to the document when .enter.append("p").text(function(d){return d;}) methods are called.

In a follow up to this post I will explain how selecAll().data().exit() sequence works. I have yet to write that. When I do, I will put a link here.

Tuesday, January 3, 2012

Using D3.js to draw a grid

Using D3.js to draw a grid In a previous post, entitled "Drawing a straight line with D3.js, you saw how to draw a single line. In this post, we will draw a grid, which requires us to draw multiple lines. We will see two ways of doing this:
  1. The first way uses the standard for loop of JavaScript to achieve this (in addition to the technique we discussed in the previous post). It does not use any feature specific to D3.js that we did not talk about in the previous post.
  2. The second way illustrates use of D3.js methods that make it possible to bind given data to graphical elements. In other words, it illustrates use of D3.js methods that add graphical elements automatically based on available data. When you are creating graphics to represent data that you already have, this approach will be more useful.

  1. Drawing a grid using for loops and D3.js
  2. The following code illustrates how to draw a grid using for loops. Create a DIV container in your HTML file, <div id="D3lines"></div> and add the following javascript code:
     // Select the DIV container "D3line" then
     // add an SVG element to it
    
     var width = 400;
     var height = 400;
     
     var lineGraph = d3.select("#D3lines")
         .append("svg:svg")
         .attr("width", width)    
         .attr("height", height); 
     
     // To draw a line use the "svg:line" element.
     // "svg:line" element requires 4 attributes (x1, y1, x2, and y2)
     // (x1,y1) are coordinates of the starting point. 
     // (x2,y2) are coordinates of the end point.
     // You also need to specify the stroke color.
    
     // Using for loop to draw multiple horizontal lines
     for (var j=25; j <= width-25; j=j+25) {
         lineGraph.append("svg:line")
             .attr("x1", 25)
             .attr("y1", j)
             .attr("x2", width-25)
             .attr("y2", j)
             .style("stroke", "rgb(6,120,155)")
             .style("stroke-width", 2);            
     };
     
     // Using for loop to draw multiple vertical lines
     for (var j=25; j <= height-25; j=j+25) {
         lineGraph.append("svg:line")
             .attr("x1", j)
             .attr("y1", 25)
             .attr("x2", j)
             .attr("y2", height-25)
             .style("stroke", "rgb(6,120,155)")
             .style("stroke-width", 2);            
     };
    
    This script gives:

    Note that in the for loops, the values of the line attributes (x1, y1, x2, y2) are specified when the lines are being generated.



  3. Drawing a grid using given coordinate data to generate the line elements.
  4. The following code illustrates how to use data that has already been created, to generate line elements. Create a DIV container <div id="D3grid_D3way"></div> and add the following script to your HTML file.
     // Select the DIV container "D3grid_D3way", then
     // add an SVG element to it
        
     var width = 400;
     var height = 400;
     
     var gridGraph = d3.select("#D3grid_D3way")
         .append("svg:svg")
         .attr("width", width)     // Set width of the SVG canvas
         .attr("height", height);   // Set height of the SVG canvas
     
     
     // the yaxiscoorddata gives the y coordinates
     // for horizontal lines ("x1" = 25 and, "x2"=width-25)
     var yaxiscoorddata = d3.range(25, height, 25);
     
     // the xaxiscoorddata gives the x coordinates
     // for vertical lines ("y1" = 25 and, "y2"=height-25)
     var xaxiscoorddata = d3.range(25, width, 25);
     
     
     // Using the xaxiscoorddata to generate vertical lines.
     gridGraph.selectAll("line.vertical")
     .data(xaxiscoorddata)
     .enter().append("svg:line")
     .attr("x1", function(d){return d;})
     .attr("y1", 25)
     .attr("x2", function(d){return d;})
     .attr("y2", height-25)
     .style("stroke", "rgb(6,120,155)")
     .style("stroke-width", 2);       
     
     
     // Using the yaxiscoorddata to generate horizontal lines.       
     gridGraph.selectAll("line.horizontal")
     .data(yaxiscoorddata)
     .enter().append("svg:line")
     .attr("x1", 25)
     .attr("y1", function(d){return d;})
     .attr("x2", width-25)
     .attr("y2", function(d){return d;})
     .style("stroke", "rgb(6,120,155)")
     .style("stroke-width", 2);
    
    This script gives the same grid as above.

    Function d3.range(start, stop, step) generates an array of numbers from start to stop (excluding stop) with the difference between the two successive numbers given by step. For example: d3.range(0,25,5) returns [0, 5, 10, 15, 20].

    To understand the magic from line gridGraph.selectAll("line.vertical") onwards, I suggest you see my other post on Understanding selectAll, data, enter, append sequence in D3.js. You can also see Jerome Cukier's post on understanding selections. Jerome gives a good explanation of how data is used to create graphical elements with an example in which he creates bar charts. Go to the post and scroll down to the section entitled, "Bonus: understanding selections".

Saturday, December 31, 2011

Drawing a straight line with D3.js

In this post you will see how to draw lines using D3.js. For those who have not heard about D3.js, it is a powerful JavaScript library that can be used for creating beautiful, scalable, static or interactive drawings, figures, plots, graphs, diagrams etc. in the browser. I have just started learning this library. I also have little experience in JavaScript. So, I thought creating such tutorials would make for good notes and opportunity to learn these two. The code below may not be the best code to achieve the results, nevertheless, if you are new to these things, it will certainly be helpful to get you started. I will start with the very basics.

We will first draw a simple line using D3 and then modify some its properties such as width and opacity. Note that we are actually using the D3.js to generate SVG code for drawing the line. If you don't know what SVG is, don't worry. We will pick it up as we go along.

  1. Create the basic HTML file that will contain the code. Note the DIV container with id = "D3line" . We will add the line to this DIV container. Also, see the header of the file. We have linked to the D3 repository to load the D3.js script. You will need to be connected to the internet for the following to work.
     <!DOCTYPE html>
     <html>
     <head>
         <title>Lines using D3.js</title>
         <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
     </head> 
     <body>   
         <div id="D3line"></div>
     </body>
     </html>
    
  2. Now add the following D3 script after the <div id="D3line"></div> line. Here we select the DIV container D3line, add an SVG element to it, and set its height and width attributes. We assign the SVG element to a variable that we name lineGraph.
     <script type="text/javascript">
     /*Select the DIV container "D3line" and add an SVG element to it*/
     var lineGraph = d3.select("#D3line")
         .append("svg:svg")
         .attr("width", 500)   
         .attr("height", 200); 
     </script>
    
  3. Now add the SVG line element to the SVG element that we added above, by adding the following script after the .attr("height", 400); line. As you can see, you need to specify four attributes to add a line: x and y coordinates of the starting point, (x1,y1), and end point x and y coordinates (x2, y2). Further, you need to specify the color for the line, which is value of the stroke element.
     // To draw a line use the "svg:line" element.
     // "svg:line" element requires 4 attributes (x1, y1, x2, and y2)
     // (x1,y1) are coordinates of the starting point. 
     // (x2,y2) are coordinates of the end point.
     // You also need to specify the stroke color.
     var myLine = lineGraph.append("svg:line")
         .attr("x1", 40)
         .attr("y1", 50)
         .attr("x2", 450)
         .attr("y2", 150)
         .style("stroke", "rgb(6,120,155)");
     
    Open the HTML file that you have so far in the browser and you should see a line such as in the following figure. Note that the origin in SVG is at the top left hand corner.

There, you are done! Before I wrap up this post, I will show how to change a couple of properties of this line. We will first increase the thickness of the line (using the stroke-width property) and then make it a little transparent (reduce its opacity) using the stroke-opacity property.

To increase the line width add:

  // Increase the line thickness/width
  myLine.style("stroke-width", 24);
This gives:
Finally, to make the line transparent add the following lines to the script.
 // Make the line a little transparent (decrease opacity)
 myLine.style("stroke-opacity", 0.6);
This gives:

Wednesday, December 28, 2011

Creating a sequence of dates in R

Creating a sequence of dates in R

To create a sequence of dates in R you can use the as.Date() function along with seq() function. Following are some examples. While your needs may be different, this will help you get started.

1. Creating a sequence of dates by specifying the start date and number of days

2. Creating a sequence of dates by specifying the start date and an end date

Friday, November 18, 2011

How to include HTML code snippets in an HTML file?

To have a quick reference at hand, today I was writing a note on how to include software code in my blog posts. I was listing things such as what HTML tags to use and how to use them. To make myself comfortable with HTML, I thought of writing the note as an HTML file. HTML provides <pre>. . .</pre> tags for including verbatim text in your HTML file. However, I quickly realized that any HTML code included in the <pre>. . .</pre> is not treated as verbatim text. It is executed as HTML code. I searched online and after some time, found the following solution.

Instead of using < and > to enclose HTML tags in the code snippet that you want to display, use &lt; and &gt;. For example, to display the <b>. . .</b> tags that you would use to get bold text, type:

    &lt;b&gt; . . .&lt;/b&gt;


Wednesday, January 12, 2011

Keep a "Comment and Markup" tool selected in Adobe Acrobat

Every once in a while I forget how to keep a commenting tool selected in Adobe Acrobat 9.0. And, when I do, I cannot quickly find the instructions on the web. Here are the instructions (I got them from the Adobe website):
  1. Select the tool you want to use (but don’t use it yet).
  2. Choose View > Toolbars > Properties Bar.
  3. Select Keep Tool Selected.

Sunday, January 9, 2011

String formatting in Python

You have written a piece of code in your favorite language Python.  The code does some fancy (or simple) calculations.  Now you want to output the results of these calculations in a format that is easier and more pleasant for humans to read.  Keep reading to find out about some options available in Python (version 2.6 and above) for basic formatting of the output produced using print statements. Specifically, below I discuss:

  1. how to set the column width of the column in which the output is printed;
  2. how to align the output
    • left, center or right align
    • how to align the digits such that the positive and negative numbers start in the same column
  3. how to set the precision of floating numbers
  4. some other examples that illustrate output of numbers with thousand separator commas, output of numbers as percentages etc.

Not all possibilities are discussed below. For more information, see http://www.python.org/dev/peps/pep-3101/

Suppose you have a list of tuples called lang_info where each tuple has the name of the language, the year it was developed (taken from this Wikipedia article) and TIOBE rating. You want simple text output of this information in the form of a table.



lang_info =  [('Fortran', 1954, 0.435), ('Cobol', 1959, 0.391),
              ('C', 1972, 16.076), ('C++', 1980, 9.014), 
              ('Python', 1991, 6.482), 
              ('Java', 1995, 17.99), ('C#', 2001, 6.687)]


If you use the code:

print "Language Year Developed TIOBE rating"
print "--------------------------------------"
for element in lang_info:
    print element[0], element[1], element[2]

you will get the output (call this output_1):

Language Year Developed TIOBE rating
--------------------------------------
Fortran 1954 0.435
Cobol 1959 0.391
C 1972 16.076
C++ 1980 9.014
Python 1991 6.482
Java 1995 17.99
C# 2001 6.687

This is not very easy or pleasant to read. Let us format it so that we get the output:

Language      Year Developed      TIOBE rating
----------------------------------------------
Fortran            1954                   0.43
Cobol              1959                   0.39
C                  1972                  16.08
C++                1980                   9.01
Python             1991                   6.48
Java               1995                  17.99
C#                 2001                   6.69

The first step is to replace the words and numbers (such as "Language", "Year Developed"), and the variables (element[0], element[1]) that we want to format by what are known as replacement fields.

So instead of:

print "Language Year Developed TIOBE rating"
print "--------------------------------------"
for element in lang_info:
    print element[0], element[1], element[2]

we have,

print "{0} {1} {2}".format("Language", "Year Developed",
                            "TIOBE rating")
print "-"*46
for element in lang_info:
    print "{0} {1} {2}".format(element[0], element[1], element[2])

The curly braces are known as replacement fields. The numbers within them are called field names and specify the position of the argument in the .format method that will replace that replacement field. For example, {1} will be replaced by argument in position 1 in the .format method. Running the above code gives us the same output as in output_1; this is because we have not applied any formatting to it. Next we type what are known as 'format specifiers' in the replacement fields. These are separated from the field name by a colon (:). The general form of the format specifier is

[[fill]align][sign][#][0][minimumwidth][.precision][type]

We will discuss only some of these flags (specifically, we will discuss align, sign, minimumwidth, .precision and type flags) and that too only some of the possible values for these flags.

First, note that all flags are optional. The following are some caveats:

  •  if you specify two or more flags they should be specified in the same order as shown in the general form of the format specifier.
  • if you want to use the fill, you must give a value for the align flag.

Let us first set the column width. The minimumwidth flag allows us to do this. It is an integer which specifies the width of the column. The following code:

print "{0:12} {1:16} {2:16}".format("Language", "Year Developed", 
                                     "TIOBE rating")
print "-"*46
for element in lang_info:
    print "{0:12} {1:16} {2:16}".format(element[0], element[1], 
                                       element[2])

gives the following output:

Language     Year Developed   TIOBE rating    
----------------------------------------------
Fortran                  1954             0.43
Cobol                    1959            0.391
C                        1972           16.076
C++                      1980            9.014
Python                   1991            6.482
Java                     1995            17.99
C#                       2001            6.687

This is already looking better, but still there is room for improvement. While the numbers are right aligned, the column titles (in second and third column) are left aligned. Let us change the alignment of each of these to center. We will use the align flag for this. Some possible values for the align flag are:

'^' -  for center alignment
'<' -  for left alignment 
'>' -  for right alignment

Applying the left alignment tag to the year column:

print "{0:12} {1:16} {2:16}".format("Language", "Year Developed", 
                                    "TIOBE rating")
print "-"*46
for element in lang_info:
    print "{0:12} {1:<16} {2:16}".format(element[0], element[1], 
                                         element[2])
gives the following output.

Language     Year Developed   TIOBE rating    
----------------------------------------------
Fortran      1954                         0.43
Cobol        1959                        0.391
C            1972                       16.076
C++          1980                        9.014
Python       1991                        6.482
Java         1995                        17.99
C#           2001                        6.687
Still not great. Let us center align the header of the second column, right align the header of the third column and center align the contents of the second column (the year column).

print "{0:<12} {1:^16} {2:>16}".format("Language", 
                                       "Year Developed", 
                                       "TIOBE rating")
print "-"*46
for element in lang_info:
    print "{0:12} {1:^16} {2:16}".format(element[0], element[1], 
                                         element[2])

This piece of code gives:

Language      Year Developed      TIOBE rating
----------------------------------------------
Fortran            1954                   0.43
Cobol              1959                  0.391
C                  1972                 16.076
C++                1980                  9.014
Python             1991                  6.482
Java               1995                  17.99
C#                 2001                  6.687
The last column does not look that great given different number of digits after the decimal point. We will use the .precision flag and the type flag to fix this:


precision is a whole number that specifies the number of digits you want to display after the decimal point.


type takes many different values (see  http://www.python.org/dev/peps/pep-3101/ for all different values that this flag takes). Here, we use f for fixed point number.

Hence, using

print "{0:<12} {1:^16} {2:>16}".format("Language", 
                                        "Year Developed", 
                                        "TIOBE rating")
print "-"*46
for element in lang_info:
    print "{0:12} {1:^16} {2:16.2f}".format(element[0], 
                                            element[1], 
                                            element[2])

we have,

Language      Year Developed      TIOBE rating
----------------------------------------------
Fortran            1954                   0.43
Cobol              1959                   0.39
C                  1972                  16.08
C++                1980                   9.01
Python             1991                   6.48
Java               1995                  17.99
C#                 2001                   6.69
That looks much nicer and is much easier to understand.


Another example using value n for the type flag.

import locale

# Setting the locale to US English
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

print "Number without formatting applied: 65739838"
print "Number with formatting applied: {0:n}".format(65739838)

The output of this code is:

Number without formatting applied: 65739838
Number with formatting applied: 65,739,838

I think the second is far easier to read than the first.

Changing the Color Scheme in Emacs: the color-theme extension

If you are new to Emacs, as I am, you are probably are a little bothered by how it looks out of the box. Those of us who are just learning to use Emacs in this day and age have most probably worked with other editors (such as Textmate) that provide more eye candy right out of the box. There is no reason to be disappointed. As I have mentioned in earlier posts, Emacs is infinitely extensible and there are great color themes available to make Emacs more beautiful and more pleasing to your eyes.

For modifying the color scheme of Emacs you can use the wonderful color-theme extension. I recently installed it to work with Emacs 23.2 on Mac OS X. I have explained the steps below. These may work for other versions of Emacs.

  1. Go to http://download.savannah.gnu.org/releases/color-theme/ and download color-theme-6.6.0.zip to your computer.

    For the purpose of discussion, suppose you save this zip file in your ~/Downloads folder.
  2. Unzip the downloaded zip file. This will result in a folder named
    color-theme-6.6.0 within your ~/Downloads folder (or wherever you saved the color-theme-6.6.0.zip file).

    You may prefer to keep the color-theme-6.6.0 extension folder where you saved the color-theme-6.6.0.zip file, or you may prefer to move it to some other place on your computer where you keep downloaded Emacs extensions. If you want to move it to another place on your computer, ensure that you move the entire color-theme-6.6.0 folder. (I read this tip on some forum and had planned to link from here. Unfortunately, I deleted it and cannot find it again.)
  3. Add the following lines to your Emacs init file (~/.emacs file).
    (add-to-list 'load-path "~/Downloads/color-theme-6.6.0")
        (require 'color-theme)
        (eval-after-load "color-theme"
        '(progn
        (color-theme-initialize)
        (color-theme-dark-blue2)))
    
    Note that ~/Downloads/color-theme-6.6.0 is the folder where you saved the color-theme-6.6.0 folder. Also, dark-blue2 in the last line of the above code snippet is the name of one of the many available color themes. You can replace this by the name of the theme you like (see below how to find theme names).
  4. Close Emacs and start it again and enjoy the beautiful colors.

If you want to see the other themes that came with the extension that you downloaded, you can use M-x color-theme-select. This will open a new buffer with a list of available color themes. You can change to a theme in this list by moving the cursor to that theme in the buffer and pressing Enter.

If you want to see what different themes look like before changing yours, you can also go here: http://color-theme-select.heroku.com/#color-theme-light

Wednesday, December 29, 2010

Finding command names and customizing key-bindings in Emacs

Of the zillion things that make Emacs a great editor, one is the ability to access all its functionality using the keyboard. However, you may find that a particular key combination is not convenient for you. For example, I find it inconvenient to use M-/ (pressing the ALT key and the / key together) for word completion. No problem! Of those zillion things, another one is the ability to modify the key bidings to your liking. I explain below how to assign word completion to S-SPC (that is, getting word completion by pressing Shift and Space keys together).

1.   The first step in customizing key bindings is finding the name of the command for a given key sequence. This is done using:

    C-h c key-sequence    

Note that you should not type the key-sequence, you have to press it the way you would for invoking the command. For example, to find the name of the command that is executed when M-/ is pressed, you cannot type letter M followed by a /, you have to press ALT and / together. This will return:

     M-/ runs the command dabbrev-expand     

in the minibuffer. There, you know the name of the command.

2.   The second step is to bind your custom key-sequence to this command. Suppose you want dabbrev-expand command to be executed when Shift and Space keys are pressed together. Do the following: open ~/.emacs (or, whichever is your Emacs init file) and add the following line to that file:

     (global-set-key (kbd "S-SPC") 'dabbrev-expand)     


3.   Close Emacs and restart. You can now use your custom key-sequence for the command you assigned to it.

If you are thinking, "okay, from your example I know that I have to use
S for Shift key and SPC for the Space key, but what do I use for some other keys, say the function keys. You can find useful information regarding this and a lot more about custom key bindings at http://xahlee.org/emacs/keyboard_shortcuts.html

Saturday, December 25, 2010

Changing commit message of a revision in Mercurial

About 2-3 months back I started using Mercurial for version control. If you have not heard of Mercurial before, this post will not be of any interest to you. Just know that Mercurial is great and I strongly recommend it if in your work you feel the need to keep different versions of a file at different points of time, and the way you do this is by saving the file under different names. See here for more information on Mercurial. For the rest, read below.

As I started with Mercurial, from the very beginning I had a feeling that one day I would make a mistake in recording the message that accompanies a commit. It seemed very likely that I would forget to make note of some important change I had made to a file. Of course, there was also the possibility of typos. This is what happened a couple of days back; I committed a revision with an incomplete and partly incorrect message. I had intended to do something, changed my mind while I was modifying the file, but wrote a message which reflected my original intentions and hit enter. What made the problem worse is that I did not realize this until I had made a couple of subsequent commits. I searched on Google to find how I could fix my mistake. I found a couple of ways, but they seemed a little confusing, especially given my lack of experience with version control systems. Here I explain the method that I felt was the easiest and most straightforward to use. This involves using the histedit extension for Mercurial. The extension can do more than just change a commit message, but I will focus only on this functionality, hoping that this will make the process clearer to follow.
  1. Install histedit.
    If you have already installed histedit, you can skip this step.
    1. Download histedit.
      • Open the terminal and navigate to the directory where you want to save histedit. For
        example, I have saved it in ~/Dropbox/Mercurial_Extensions.
      • At the terminal prompt type:
        hg clone https://bitbucket.org/durin42/histedit 
        This will download the histedit extension in your present working directory.
    2. Tell your Mercurial installation about the location of this extension.
      • Open .hgrc file.
        This file is usually located in your HOME directory. If so, navigate to your home folder by typing cd ~ at the terminal prompt and pressing enter.
      • Then type open .hgrc and press enter.
    3. Add the following lines in the .hgrc file.
      [extensions]
      histedit = ~/Dropbox/Mercurial_add_ons/histedit/hg_histedit.py
      
      Save the file.
      Note that you may have to modify ~/Dropbox/Mercurial_add_ons/
      part of the path to point to the directory where you downloaded
      and saved the histedit extension.
  2. Navigate to the directory where the hg repository whose history you want to change is located.
  3. Ensure that none of the files that Mercurial is tracking have been modified since the last commit. If there are modified files you have to commit the changes.
  4. At the terminal prompt type hg log and press enter. Note the changeset information of the revision whose commit message you want to modify. You will need this information in the next few steps.
  5. Suppose you want to change the commit message of
    changeset:  5:48f58ff8250e
    
    At the terminal prompt, either type:
    hg histedit -r 5
    
    and press enter or, type
    hg histedit 48f58ff8250e
    
    and press enter.

    This will open a file with a list of changesets in a text editor. The first changeset is the one that you specified. The list will look something like the one shown in the following image.


  6. In the first column of this file, replace the word pick with the word edit in the row corresponding to the changeset whose commit message you want to edit. Do not touch the other rows. Save the file at the default location and under the default filename suggested by your editor.
  7. At the terminal prompt type hg histedit --continue and press enter. A file will open in a text editor with your old commit message. Make any changes to this message and save the file (again, use the suggested file name and location). The old message in the revision history will be replaced by your corrected message.

Go here for more information on histedit.