Saturday, August 25, 2012

Colors in bash

Bash is capable of generating a huge spectrum of colors for text. Here is a small code in shell script which generates all the 256 different colors bash can render:
for i in {0..255}; do printf "\x1b[38;5;${i}mCLR-${i}\t"; done; echo; reset;

The last echo is for printing a blank like after the output, without that the command prompt would appear at the end of the output. More import is reset present at the end, without which your terminal might not go back to the original settings.

In case you are wondering how to print colored output in terminal, please read:
Now you have a number to color mapping which you can use to specify your favorite color in config files of various command like applications like vi and tmux.

Sunday, July 22, 2012

ROC Curve

My recent work related to Text Classification got me introduced to ROC Curve which is a very effective way to compare classifiers against each other and decide on the cutoff value for classes.
Its best defined with a simple example. Say you have a binary classification problem. You have generated 3 train-test datasets from the original data. Say you are using Support Vector Machine (SVM) as the classifier. After training SVM on all the three sets, you want to select the "best" of these three. How do you go about it?

Here's where ROC comes into rescue. Sort the output of the SVM and compute True Positive %tage and False Positive %tage so far for each data point and plot this in x-y graph. The area under the curve will give you a way to measure the effectiveness of each classifier. Moreover you can use the nature of the graph to establish your positive and negative class boundary.

Here are a few links to know more about ROC curve:

Thursday, May 24, 2012

tmux, a Better Terminal Multiplexer than screen

Recently I encountered a program called tmux which claims to be better at "terminal multiplexing" than screen. I did not have any idea about "terminal multiplexing", but I had been using screen for quite a few years now. tmux promises to be better that screen according to the tmux sourceforge page. This got me interested.

I installed tmux, and have been using it since last 2 weeks. My experience has been great except for a minor hiccup between vi and tmux. I thought of writing a small tutorial, but I feel there are better and more detailed ones that I would have the patience to write; not to mention we all "stand on the shoulders of giants". So I am going to point you to the online resources:
Finally, tmux is quite good and it gives some features not in GNU screen. Unfortunately, tmux is not as popular as screen, so you should be able to use both. Here is my contribution to the community, a cheat sheet with popular commands for both tmux and screen together. Please note that I use Ctrl+a as my tmux hotkey rather than the default Ctrl+b. Here is the thing you have to add to your ~/.tmux.conf to achieve this:
unbind C-b
set-option -g prefix C-a
Copy-pasting in tmux is also quite easy. You can copy from any part of the command window. Go to the copy mode by Ctrl+a PageUp. Move around the terminal using arrow keys. Ctrl+Space to start marking the text from where you want to copy. Use arrow key to select text. Alt+w to copy the text. Paste text using Ctrl+a ].

Thanks for reading.

Wednesday, May 23, 2012

Reload .inputrc at runtime

If you have been playing around with bash for some time you must have changed the file .inputrc sometime. I have changed it several times mostly for Del key and auto-completion trick. However every time I change it I have to logout of bash for it to take effect. Here are two different ways to do the same without logging out of the shell.
  1. Keyboard shortcut : Ctrl+x followed by Ctrl+r
  2. Script : bind -f ~/.inputrc
Here is an excerpt from my .inputrc which many others might find useful:
"\e[1~": beginning-of-line          # home
"\e[3~": delete-char                # delete
"\e[4~": end-of-line                # end
"\e[5~": history-search-backward    # page-up
"\e[6~": history-search-forward     # page-down
Hope this helped you.

Tuesday, April 17, 2012

Brilliant 8-phase multi-quine in ruby

Wikipedia defines quine as:
A quine is a computer program which takes no input and produces a copy of its own source code as its only output.
In layman terms, it is a program that when executed outputs itself. Like a C program outputting its own source code. Kleene's Recursion Theorem proves the existence of quines in any Turing Complete Language. Its a good exercise to write one in your favorite language. Its fascinating to see quines.

Today morning my friend Umesh showed me this brilliant piece of ruby code that behaves as a Multi-quine. Link to original post by its author. Here's a listing of the same:



Save it as "test.rb" and run the following shell script:
while true;
do
    clear
    ruby test.rb | tee test2.rb
    mv test2.rb test.rb
    sleep 1
done

You will be able to see the picture changing after every 1 second and returning back to the original after 8 seconds. Note that when you run test.rb, it produces the output which is used for the next iteration. The shell script code lines 4 and 5 runs the code and uses the output of the code to replace the original code.

After being amazed by the beauty of this piece of code, I am not trying to figure out how exactly does it achieve this result. Someday I will post the same code with indentations and explanations in this blog.