## A pretty printer for C++ please

I often find myself in need of outputting into the command line (or a log) some formatted text. Things are fine until I need to output a 2D table of data. Googling around doesn’t come up with any existing library that allows me to quickly output stuff.

I even ask a question on Stackoverflow on this matter.

The general answer most people give is: write your own. Sure, I can write my own but I would prefer someone to have already written it so I can just use it. So, here I am presenting my newly written pretty printer library for C++. The first class available is the TablePrinter. It will print out your two dimensional data in a clean and beautiful manner.

The library has no other dependency and the interface is clean. Check it out at BPrinter Github project wiki.

Here is a quick look of what it outputs

+------------------------------------------------------------+
|                     Name|Age|                      Position|
+------------------------------------------------------------+
|                  Dat Chu| 25|            Research Assistant|
|                 John Doe| 26|        Professional Anonymity|
|                 Jane Doe|   |                              |
|                  Tom Doe|  7|                       Student|
+------------------------------------------------------------+

Categories: C++

## Pomodoro technique for XMonad users

I have recently switched to using XMonad as my window manager. I want to be a better developer and figure that the way to do so is to be more intimately comfortable with the keyboard (and custom configurations and scripts).

I am also an avid pomodoro technique practitioner. When I was in GNOME, I used Focus Booster which was a beautifully designed Pomodoro application. However, now that I am in XMonad, the only thing that is available in all my windows is the XMobar.

Thus, I decided to put together a script that works similarly to Focus Booster but to be used with my Xmobar. Check out my work at https://github.com/dattanchu/pymodoro . Comments for improvements are gladly accepted.

Here are some screenshots of how it looks (both at the start of a Pomodoro and near the end)

Although this small script was tested on Xmobar, it should work in dzen as well.

Categories: python

## Life achievement tracker

Game achievement system show the gamers the rope, keeps them coming back to the game and fosters a community around the game. I believe that this can carry well into real life. Thus, I will start creating achievement badges for things that I have proudly achieved in life. The idea is not to show off my achievements since there are many others whose achievements impact a much greater set of humanity than mine. I want to track these so that I can see where I am in life and to continue to work hard to complete my achievement set.
If you are interested in getting the Inkscape source file that I use, feel free to send me a message.
My first achievement is my recent bug fix for the OpenCV core. I feel quite happy that I had a chance to fix a bug for one of the best projects in this field. The OpenCV team is amazing and I am sure they would love to have more bug fixes submitted.

Categories: Uncategorized

## Debug a Python module built with Boost.Python within QtCreator

October 18, 2010 1 comment

After using Python for a while, it will be natural that one needs to write certain parts of the code in C++. The advantages of hybrid coding are many but it is not the topic of this post. Since QtCreator is the best IDE for C++ in Linux (IMHO), one might want to debug a Python module created with Boost.Python using QtCreator.

First, one needs a Python module built with C++, let’s called it RelightingLib in this example. Make sure to build this dynamic library under Debug mode. The file that holds this library will be RelightingLib.so .

Second, one needs a Python script that uses this module. In this example, we will use a simple file with the following content

import RelightingLib
import numpy as np
phong_light = RelightingLib.PhongLight(1)
phong_light.set_lights(np.array([0,0,1000]))


In a real project, one will use the unit test file for this library as the script file to run. Nevertheless, the script above is a good start. Let’s call this script testRelightingLib.py

Third, put a break point somewhere in the C++ code.

Fourth, start debugging in QtCreator: Menu => Debug => Start Debugging => Start and Debug External Application…

Executable: /usr/bin/python
Arguments: /path/to/your/testRelightingLib.py

In order to speed up this process, you can also create a Custom Executable Run Settings. Go to Projects (left navigation bar) => Run Settings => Add => Custom Executable

Name: RelightingLibTests
Executable: /usr/bin/python
Arguments: /path/to/your/testRelightingLib.py
Working Directory: \$BUILDDIR
Run in Terminal (not checked)

(optional)
Run Environment

Then, you can simply run this run setting in order to debug your dynamic library.

Done. Happy debugging. This is tested working on QtCreator 2.0.1 on Ubuntu 10.10 x86-64.

UPDATE: You can use the same technique to debug any C++ code you call from Python. I tested and this worked with calling functions in an .so file from Python using ctypes.
UPDATE: If you need to import modules that are in non-standard locations (e.g. your source folder), you can add the PYTHONPATH environment variable to the environment that you debugger runs in.

Known problems

• If you have gdb reverse debugger feature turned on, you will run into problems in the debug code whenever you need to do a cout. Turn off reverse debugger to fix this.
Categories: C++, python

## RQ Decomposition from QR Decomposition

I was trying to decompose a projection matrix into an intrinsic matrix, an extrinsic matrix and a translation vector. One of my attempt requires RQ decomposition of the projection matrix. I found that the RQ decomposition of Scipy does not handle non-square matrix so I implement RQ decomposition using QR decomposition.

The original code is in MATLAB in a thread.


'''
Created on Jan 28, 2010

@author: Dat Chu
'''
import numpy as np
from scipy.linalg.decomp import qr

def rq(A):
'''Implement rq decomposition using QR decomposition

From Wikipedia,
The RQ decomposition transforms a matrix A into the product of an upper triangular matrix R (also known as right-triangular) and an orthogonal matrix Q. The only difference from QR decomposition is the order of these matrices.
QR decomposition is Gram-Schmidt orthogonalization of columns of A, started from the first column.
RQ decomposition is Gram-Schmidt orthogonalization of rows of A, started from the last row.
'''
A = np.asarray(A)

m, n = A.shape

# Reverse the rows
reversed_A = np.flipud(A)

# Make rows into column, then find QR
Q, R = qr(np.transpose(reversed_A))

# The returned R is flipped updown, left right of transposed R
R = np.flipud(np.transpose(R))
R[:,0:m-1] = R[:,m-1:0:-1]

# The returned Q is the flipped up-down of transposed Q
Q = np.transpose(Q)
Q[0:m-1, :] = Q[m-1:0:-1, :]

return R, Q


Categories: Numpy

## Tukey Window in Numpy

January 29, 2006 1 comment

I wanted to find an implementation of Tukey Window for Numpy but I couldn’t find one so I wrote my own. I tested the output vs MATLAB tukeywin(128,0.5), tukeywin(256,0.5) and tukeywin(512,0.5). The results are correct up to the 5th decimal value. That is

self.assertAlmostEqual(w[i], self.tukeywin_128[i], 5)


Here is the code

import numpy as np
# Not to be confused with functions to be used on the Windows OS
# These window functions are similar to those found in the Windows toolbox of MATLAB
# Note that numpy has a couple of Window functions already:
# See: hamming, bartlett, blackman, hanning, kaiser

def tukeywin(window_length, alpha=0.5):
'''The Tukey window, also known as the tapered cosine window, can be regarded as a cosine lobe of width \alpha * N / 2
that is convolved with a rectangle window of width (1 - \alpha / 2). At \alpha = 1 it becomes rectangular, and
at \alpha = 0 it becomes a Hann window.

We use the same reference as MATLAB to provide the same results in case users compare a MATLAB output to this function
output

Reference
---------
http://www.mathworks.com/access/helpdesk/help/toolbox/signal/tukeywin.html

'''
# Special cases
if alpha <= 0:
return np.ones(window_length) #rectangular window
elif alpha >= 1:
return np.hanning(window_length)

# Normal case
x = np.linspace(0, 1, window_length)
w = np.ones(x.shape)

# first condition 0 <= x < alpha/2
first_condition = x<alpha/2
w[first_condition] = 0.5 * (1 + np.cos(2*np.pi/alpha * (x[first_condition] - alpha/2) ))

# second condition already taken care of

# third condition 1 - alpha / 2 <= x <= 1
third_condition = x>=(1 - alpha/2)
w[third_condition] = 0.5 * (1 + np.cos(2*np.pi/alpha * (x[third_condition] - 1 + alpha/2)))

return w


Categories: Numpy