Monday, October 31, 2011

[Py] The Python Challenge -- Level 05

Here is a note of my solution to solve Level 05 of The Python Challenge.

The main library used is the pickle.

Sunday, October 30, 2011

[Py] The Python Challenge -- Level 04

Here is a note of my solution to solve Level 04 of The Python Challenge.

The main library used is the urllib.parse to parse the url and the urllib.request for retrieving the web page source.

Thursday, October 27, 2011

[Py] The Python Challenge -- Level 03

Here is a note of my solution to solve Level 03 of The Python Challenge.

The main library used is the urllib.request for retrieving the web page source and the re for regular expression operations.

[Py] The Python Challenge -- Level 02

Here is a note of my solution to solve Level 02 of The Python Challenge.

The main library used is the urllib.request. In Python 2.x, it is just urllib.

Wednesday, October 26, 2011

[Py] The Python Challenge -- Level 01

Here is a note of my solution to solve Level 01 of The Python Challenge.

Used functions: ord() and chr().

Thursday, October 20, 2011

pss: searching your source code in command line

When I was working on my Ubuntu with Vim to write or to view source code, I had no simple way to find certain functions scattering in the files by their name or key words.

Now I have one useful tool which is named pss written by Eli Bendersky. The tool is written in Python and that is attractive to me, because I can read the source code and maybe learn something from it. :-)

Download: https://bitbucket.org/eliben/pss/downloads

Okular: Annotations are stored in local directory

This is just a quick note.

When using Okular to view the PDF files, you can add various annotations in the documents. These annotations, however, are not embedded in the PDF files. Instead, they are stored in your local directory. In my Ubuntu PC, the path is

~/.kde/share/apps/okular/docdata

The annotations are stored in individual XML files for the corresponding annotated PDF files.


Thursday, September 15, 2011

Using Boost accumulators to calculate the variance

Boost seems a promising set of C++ library, but I found it's not easy for me to figure out its usage. After serveral trials, I finally got the variance calculated.

Here is the testing code I used (in VS2005):

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/variance.hpp>

int main(){
    using namespace boost::accumulators;
    accumulator_set< double, stats<tag::variance> > acc_variance;

    for (int i = 0; i < 10; i++){
        std::cout << i << ", ";
        acc_variance(i);
    }

    std::cout << std::endl << "Variance = " 
        << variance(acc_variance) << std::endl;

    return 0;
}

The output is:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Variance = 8.25

When I tried to varify the result, I use MS Excel but got unexpected result. The variances given by Boost and Excel are different!

Did I make any mistakes? Oh, I didn't realise the variance calculated by Boost function was the population variance, and the VAR function of Excel gave the sample variance. To have population variance in Excel, you need VARP, not VAR.


---
Ref: Variance calcs in the stats library and in Excel

Using Git as a working logger

I am working on a project, which is a experimental program coded and maintained by myself.

It is redundant and seems stupid to copy the whole working folder whenever I want to change the code to another version. Especially when the folder contains not only source code but some other documents or videos of testing results.

``I need a version control tool.'' The idea came to my mind.

``But I've never used or been familiar with any.'' The truth is cruel.

Then I thought of Git, a term which I might saw several times when surfing webpages.

Because the project I am working on is written and compiled with Visual Studio 2005, I started to search how to use Git with VS2005 and found Git Extensions.

Wednesday, August 31, 2011

VLC outputs black screen while the audio works fine

My VLC video player gives strange behaviour these days. When playing video, there is only sound with black screen. I once though the video files were broken, but they can be viewed by using QuickTime. So, the problem must be in VLC.

I searched on the Web and didn't find exact issue I encountered. Then I found this post, but the solution didn't work for me and didn't make sense to me. However, the post remind me about preference settings, and I thought of there was a post I glanced mentioned about video card. Oh! I've changed my video card several weeks ago. It might be the cause! I looked into the ``Video Settings'' of preference menu, and unchecked the item named ``Accelerated video output (Overlay)''. It works! :-)


Monday, July 25, 2011

[Py] Example code of FAST corner detection

Edward Rosten developed the FAST corner detection algorithm and gives several versions of source code. Python code is one of them. I downloaded the Python source code but found there was no example to illustrate the basic usage. Fortunately, the Matlab code has one example m-file. Based on the m-file, I tried to write a Python version and it works.

Tuesday, May 31, 2011

Tile Figures with Montage (ImageMagick)

Suppose you have several figures for articles ,papers, blogs, or anything else. You want to combine these figures into a bigger frame, and they have gaps between each other. How? I had used Inkscape to import the figures, arrange them, and export the final result. No, don't do that again. It is not necessary to launch your image editors with GUI to accomplish this kind of work. I found the most convenient  way is to use some commands offered by the magic ImageMagick.

Good examples have been given in ``ImageMagick v6 Examples -- Montage, Arrays of Images,'' and the following is my own test with some notes.

Sunday, May 22, 2011

Tetris in Command Line

Ha ha! That's really funny. I never think about even sed can be used to write games such as Tetris!

I just wondered how to play Tetris in command line. Googled it and found several results with source code. There are three of them I've played:
  1. Tetris for the command line with Python Curses (in Python)
  2. Play Tetris at the command line (in sed)
  3. tt: Tetris for Terminals (in C)
To compile tt, you may need to install ncurses, or you will get the following errors:
utils.c:32:20: error: curses.h: No such file or directory
utils.c:33:18: error: term.h: No such file or directory

In Ubuntu, just install the ncurses-dev.

Thursday, May 05, 2011

[talk] Thanks to You

I attended a class about how to write English emails yesterday. At the end of the class, the lecturer asked us to write an email for exercise. Below is what I wrote. A personal thank to you who have contributed to the open source community.

Dear Mr. and Ms. Hackers,

I am writing to thank you for your great and amazing works
to the open source community.

With so many brilliant tools you've created and kindly released,
many end-users, including me, are allowed to do more useful
things with free [of] charge and, more important[ly], with the freedom
to choose software.

Furthermore, I've learnt about many interesting and useful skills from
the source code released by you.
That's great and I hope I could be capable of contributing back to the
community in the future.
Thank you all for your great works.

Sincerely yours,


THK

Thursday, April 28, 2011

Great tool for LaTeX users working with MS Word -- Bibtex4Word

I was writing conference papers which have only templates in MS Word, and was wondering how to cite the references and to produce the bibliography in Word as easy as that in LaTeX. Then I found Bibtex4Word.

Most MS Word users might type the bibliography list manually, and it is really a tedious work. Inconsistent formatting style might be a main problem of manually typing the bibliography. The format should be done automatically, and should be changed easily to other styles. Just all the natural things in LaTeX.

Bibtex4Word can do the work pretty nice. I have installed and tested with it. Other programs needed are:
  • JabRef: to produce and to manage the bib file of your references
  • MikeTeX: to provide BibTeX for Bibtex4Word so that it can output formatted bibliography list
No need to worry about the formatting is really great. :-)

Wednesday, April 20, 2011

[talk] Talk to myself...

  • Work hard when you are at work.
  • Don't promise too much, but once promised, don't think too much. Just do it.
  • No matter what is in front of you, calm down.
  • The only way to solve the problem is to solve it.
  • A problem is not only a problem, it's also a challenge, and could be a chance, an opportunity.
  • Knowledge, skill, and working hard.

Tuesday, March 08, 2011

[Py] Use ``import cv'' in Ubuntu

At the beginning of my OpenCV learning with Python, I noticed one thing which confused me but I didn't give it further attentions. Some examples import the OpenCV modules by using ``import cv'' and some use ``import opencv.'' The first one has never worked to me, so I've adopted ``import opencv'' until today.

The problems were not only about the import approach. Although I found I can import opencv.cv module, which looked similar to the so-called cv module, there had been other problems about the function names. I noticed that many functions given by the opencv.cv module have the ``cv'' prefix, while the same functions given directly by the cv module don't. For example, opencv.cv offers open.cv.cvSomeFunction() while in cv it is cv.SomeFunction(). Based on the observation, I suspected that my installation of OpenCV an/or Python bindings are not totally right.

Thursday, March 03, 2011

Trials of pycam example code (2)

I've downloaded pycam and tested some of the sample codes. There were some errors when I tried to run the examples, such as DrawAHat.py and eyeLocator.py [1]. The error messages were as follows.

Monday, February 28, 2011

[Py] Use cProfile to check the run time performance of a program

When I was trying to figure out the usage about get_fps of pygame.time.Clock, I read about the thread:

And I noticed that they were talking about ``cProfile,'' so I went to the document about it:

[Py] Using get_fps function of pygame

There are modules of pygame which provide time related functions. One of them is the get_fps which can be used to know the fps of executing program.