Sunday, October 29, 2006

Measure program execution time

To measure execution time of any program under UNIX (Linux, Mac X, Solaris) one can use time command. For example

>time ls

>real 0m0.006s
>user 0m0.001s
>sys 0m0.004s


Command time ls measures time used by ls program.

Saturday, October 28, 2006

Matlab: Working with text data files using Perl (2)

This post is continuation of the former post concerning Matlab and Perl work with text files.
Former program, reading rectangles.txt file can be of course written in Matlab, without any Perl help. My example with rectangles.txt is quite simple as far as text file is concerned; nevertheless, I used Perl to read and search this file in order to show how Perl can be used within Matlab.

Below I present Matlab code, that does exactly the same task as Perl script presented in my former post.


function [rect_1 rect_2 point]= getRects(filename)

rect_1=[];
rect_2=[];
point=[];

fid=fopen('rectangles.txt');

while true
tline = fgetl(fid);
if tline(1)=='#', continue;end;
if ~ischar(tline), break, end;

[file,theRest] = strtok(tline);
if ~strcmp(file,filename), continue; end;

RECTs= sscanf(theRest,'%f')';

rect_1=RECTs(1:4);
rect_2=RECTs(5:8);
point=RECTs(end-1:end);

end

fclose(fid);

The question now is which method (Matlab+Perl or Matlab only) is faster?

To check it I used tic;getRects('105aty.tiff'); toc; combination in Matlab both for full Matlab script and Matlab/Perl script. Each script was tested 100 times.

The results are:
Matlab only version : 0.18 (std=0.03) sec
Matlab+Perl version: 0.10 (std= 0.02) sec

It is clearly seen that connection Matlab+Perl to work with text data is considerably faster
that of Matlab only.

Appendix:
Test was made on:
Matlab 7.0.0 (R14) and Perl 5.6.1 running on Sun Fire V440 Server.
std - standard deviation

Friday, October 27, 2006

Matlab: Working with text data files using Perl (1)

Matlab is a great tool when you work with mathematic related software like image processing (assuming that you have appropriate toolboxes, like Image Processing Toolbox). It is very convenient high level programing language, in which you can do, I think, almost everything and many things that are almost impossible in other languages like (C, Java, Perl) are extremely easy here.

Nevertheless, there are few things that are easy and natural in those other languages, whereas in Matlab they become quite difficult. What really annoys me, and not only me, are: (i) lack of hash arrays, (ii) working with text files and (iii) lack of multi-thread work.

Factors (i) and (ii) are closely related. Everyone who has ever used Perl, Ruby or Python to work and mine text files knows that hashes are essential. Of course, they are useful to many other things, however based on my own experience, vast majority of operation of files were based on hash arrays usage. What is more, I have to say, that you can easily work with Matlab without hash tables, and I really did for a long time. But, when recently I needed to process (write/read/create/joining data from few files/extracting and looking for string and numeric data) text files in Matlab, lack of hash tables, and poor file handling operation in Matlab become very frustrating.

Fortunately there is Perl, which is excellent in dealing with text files. However, the most important thing about this, is the fact that Matlab has embedded support for using Perl scripts within Matlab programs using perl function:
Perl is included with MATLAB on Windows systems, and thus MATLAB users can run M-files containing the Perl function. On Unix systems, MATLAB just calls the Perl interpreter that's available with the OS - from Matlab help.
This function can save a lot of work, and make working with files in Matlab very fast, and convenient; in fact, as convenient as Perl is. At the same Matlab page we read:
It is sometimes beneficial to use Perl scripts instead of MATLAB code...
Indeed, it is very beneficial, especially if one works for example with text files and further in this post I am going to show one example how Perl can be used withing Matlab while working with txt files.

1. Data File

My test text file is called rectangles.txt. It contains file names (first column) and description of two rectangles and one point. Each rectangle is characterized by four values: [x y width height].
First rectangle is described in columns 2-6, second in 7-10. Two last colums (11,12) contain
position [x,y] of a point.

2. My task

Little script in Matlab (getRects('filename')), which takes file name, searches given file name in the file, and returns three vectors ( rect_1=[x y width height]; rect_2=[x y width height]; point=[x y]) based on info in the file, for instance:

[rect_1 rect_2 point] = getRects('001anv.tiff');
%GIVES:
rect_1 = [113 302.5 49.035541 58.514286];
rect_2 = [274 292.5 90.87837491 89.96484776];
point = [5.220703 4.375];

3. Method

To do my task, I will create simple Perl script, that will take file name form Matlab, search for data, and returns required vectors to Matlab.

4. Matlab script

Lets start from creating our Matlab script.

function [rect_1 rect_2 point]= getRects(filename)

%execute Perl script
a=perl('getRectPos.pl',filename,'rectangles.txt');
eval(a); %evaluate string returned from Perl

As can be seen the script is very easy. It just executes Perl program passing two arguments to it: (i) filename to be search in the file; (ii) 'rectangles.txt' - name of file to look from.

The interesting fact about this is the eval(a) operation. As described in my former post, Perl returns string representing its standard output. Hence, in order to get Matlab data structures and variables from Perl, it is required to return them as strings, and then execute that string using eval function.

5. Perl script

Finally, we come to the Perl script it self.

#File: getRectPos.pl

#Read arguments from Matlab
my $fileName = $ARGV[0];
my $fileWithRect = $ARGV[1];


#create piece of Matlab code - declaration
#of variables.
my $out="rect_1=[]; rect_2=[]; point=[];";

my %goldHash=&readFileToHash(
$fileWithRect);

if ( exists $goldHash{$
fileName}) {

my @r1 = @{$goldHash{$key}}[0..3]; #(x,y,w,h)
my @r2 = @{$goldHash{$key}}[4..7]; #(x,y,w,h)
my @p = @{$goldHash{$key}}[-2..-1]; #(x, y)

#create the rest of the Matlab code to be executed by eval
foreach my $v (@gmROI) { $out .= "Med(end+1)= $v; "; }
foreach my $v (@glROI) { $out .= "Lat(end+1)= $v; "; }
foreach my $v (@scales) { $out .= "Scales(end+1)= $v; "; }

}

#print string to Matlab
print $out;


sub readFileToHash {
#read file values to a hash table
#key value in this hash is file name (first column)
#data is all the rest columns: 2-12
my $fname = shift;

my %temp=();

open(FG,"<$fname") || die "Cannot find file ",$fname; while () {
chomp;
my ($k,my @rec) = split('\t',$_);
$temp{$k}=[@rec];
}
close FG;
return %temp;
}

6. Results

After executing Perl script, Matlab gets string to variable a. Example content of that string is:

a=
rect_1=[];rect_2=[];point=[];rect_1(end+1)= 104.000000; rect_1(end+1)= 303.000000; rect_1(end+1)= 49.035541; rect_1(end+1)= 58.514286; rect_2(end+1)= 282.964459; rect_2(end+1)= 294.000000; rect_2(end+1)= 49.035541; rect_2(end+1)= 58.514286; point(end+1)= 5.220703; point(end+1)= 4.375000;

after executing evel(a), in Matlab environment all required vectors are created (rect_1,rect_2,point).

7. Conclusion

Thanks to Perl and possibility of using it within Matlab many task concering text files can be solve more easily and more convenient.

Tuesday, October 24, 2006

Firefox 2.0 out now

I have just download Intel iMac version of Firefox 2.0 :-)

After maybe two minutes of use, I have to say that one thing about this browser is especially useful for me, and I think for most of the Internet users. I am talking of course about build in Spell Checking which is just great. Now I'm writing this fast post, and Firefox is looking after my spelling. It is much more convenient than using ABS button. Additionally I checked if there are any others languages in spell checking than English, and of course there are plenty of them, including my home one - Polish.


The next two features that are very seen instantly is refreshed look, and improved Tabbed Browsing. Now, each tab has its own close button, which is, I think, better than before.

Next, very useful new thing about Firefox is so-called Search Suggestions. Starting typing in search bar, gives a list of suggestion from search engines. For example from Google or Answers.




Of course there are many more new features than those for example: Session Restore, Live Titles, Live Bookmarks, Streamlined Interface, Phishing Protection, Protection from Spyware ..... List of all is here.



Postfix: message_size_limit

I had problem with Postfix mail server on my Ubuntu 4.10. The problem was simply, that I could not send mails with attachments greater than few MBs. The solution was to add one line to /etc/postfix/main.cf and restarting my mail server:

message_size_limit = 20240000

This increased my mail size to around 20MB.

Perl: Own module with my common functions

Lately, I also needed to create one file containg some of my functions that I use in many Perl programs. First my thought was to create some library or module, that I will include in my programs using just use key word.

I dicided to do my own package, and here I will present some more or less what I have fastly done to get what I wanted.

First I create module. The code goes in the file named MyModule.pm within the directory Utilities: that is, Utilities/MyModule.pm

  1. package Utilities::MyModule;
  2. use Exporter;
  3. @ISA = qw(Exporter);
  4. @EXPORT = qw(&myFun1 &amp;myFun2);

  5. sub myFun1 {
  6. print "Some action of myFun1";
  7. }
  8. sub myFun2 {
  9. print "Some action of myFun2";
  10. }
  11. 1;
Line 1 declares the package that the module will put its global variables and functions in.

Line 4 assigns the list (&myFun1 &myFun2) to the special, per-package array @EXPORT. When someone imports this module, variables and functions listed in that array are aliased into the caller's own package. That way they don't have to call the function Utilities::MyModule::myFun1() after the import. They can just write shuffle(23) instead. This won't happen if they load Utilities::MyModule with require Utilities::MyModule; only a use imports.

Lines 5 and 8 set up the package global functions to be exported.

Finally, line 11 is a simple 1, indicating the overall return value of the module. If the last evaluated expression in the module doesn't produce a true value, an exception will be raised.

After creating this module, you can import it simply by:
use Utilities::MyModule;

Matlab: java.lang.OutOfMemoryError

Matlab, as I wrote earlier, can use so called interfaces to work with other programming languages. One of them is Java SDK 1.4.2. Because of that, some time ago I was writing one Java application to work within Matlab. It was image processing application (java class) calculating fractal signatures. The problem with that application was that it used a lot of memory. And this was my problem. By defalut, Matlab, allows to use only 32MB of memory by java programs. When I exited that threshold I get error:

Java exception occurred:
java.lang.OutOfMemoryError: PermGen space

So I did a little of the Internet browsing and I found out that the solution to that problem was to create file java.opts in my working directory. In this file, one can put all options to java virtual machine. To increase my memory amount i had to put there:

-Xms128m
-Xmx128m
-XX:PermSize=128m
-XX:MaxPermSize=128m

Only with that option, my Matlab 7.0 and my java program worked. I also tried solution at official matlab site: here. But it didn't work. I had to use all four parameters in order to run my program.

Xemacs and mouse scroll on Mac X and Solaris

Lately I come across of a problem of how to make mouse scroll and two buttons available in XEmacs on Mac X 10.4. Many may know that on iMac there is one button mouse without scroll, but I was pretty annoyed by this fact so I changed Apples mouse to Microsoft's
mouse:-). Of course, it works perfectly, however there is problem with Xemcas which, by default, doesn't recognize actions associated with scroll and additional buttons. I found solution working on my Mac here.

The solution to my problems was to add to the end my Xemacs configuration file (Users/my_user_name/.xemacs/init.el) following lines:

(define-key global-map 'button4
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down 5)
(select-window curwin)
)))
(define-key global-map [(shift button4)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down 1)
(select-window curwin)
)))
(define-key global-map [(control button4)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down)
(select-window curwin)
)))

(define-key global-map 'button5
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up 5)
(select-window curwin)
)))
(define-key global-map [(shift button5)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up 1)
(select-window curwin)
)))
(define-key global-map [(control button5)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up)
(select-window curwin)
)))

This is what I was looking for, and what's the most important - it worked.

In addition to that, I did exactly the same thing to my XEmacs on my Solaris server (I use X11 to work remotely on Solaris from my Mac). It worked also.

Ruby, intersection of two arrays of objects

One, serious obstacle I have encounter working with Ruby, was how to perform intersection of two arrays of objects.

Ruby comes with overloaded operator &, using which intersection of two array is childish.

For example:

#!/usr/bin/env ruby

a=[1, 2, 3, 4]
b=[1, 2, 2 ,3 ,4, 5, 6]
c= a & b  #intersection of 'a' and 'b' arrays
print c

will produce: 1 2 3 4

And for example this: 

#!/usr/bin/env ruby

a=['aa', 'ac', 'acb', 'acbd']
b=[ 'ac', 'acb', 'bca', 'zwx']
c= a & b  #intersection of 'a' and 'b' arrays
puts c

will produce: ac acb  

Everything is OK, when you perform intersection of standard types, like String or Integers, etc.

However, there is problem when you create your own class, and try to perform intersection of  array of objects of your class.  

#!/usr/bin/env ruby

class MyClass #my own very simple class
   attr_reader :var1, :var2   
   def initialize(arg1,arg2)     
      @var1 = arg1
      @var2 = arg2
    end
    def to_s
        "#{var1}: #{var2}"
    end
end
a = [MyClass.new('house',3), MyClass.new('mouse',5)]
b = [MyClass.new('cat',3), MyClass.new('house',5), MyClass.new('mouse',5)]
c = a & b #INTERSECTION !!!
puts c

This, unfortunately gives nothing!

After a while I found solution to that. The solution is: you must overload eql? and hash functions in you class. Java programmers should now something, because as I remember in Java, you do something similar while working especially with Comparable interface.

So, to make intersection, final example my program looks like this:

#!/usr/bin/env ruby

class MyClass attr_reader :var1, :var2
    def initialize(arg1,arg2)
        @var1 = arg1
        @var2 = arg2
    end
    def to_s
        "#{var1}: #{var2}"
    end
    def eql? other #OVERLOADED eql? !!!
       other.kind_of?(self.class) && @var1 == other.var1
    end
    def hash #OVERLOADED hash !!!
        @var1.hash #use var1 hash value,
                   #as hash for MyClass
    end
end
a = [MyClass.new('house',3), MyClass.new('mouse',5)]
b = [MyClass.new('cat',3), MyClass.new('house',5), MyClass.new('mouse',5)]
c = a & b #INTERSECTION !!!
puts c

And this finaly produces what I wanted:

house: 3
mouse: 5

I used only standard hash value from @var1 in my class, because I was only interested in making intersection based on @var1, nevertheless there is noting against to make more complex hash and eql? function.  

NFS sever on Ubuntu 5.10

To setup nfs server on you Ubuntu, few steps are required.

  1. Install packages sudo apt-get install portmap nfs-common nfs-kernel-server
  2. Create, if not exists, file: /etc/exports where you indicate folders you want to share. For example I have in my exports (IP addresses have been change):
    /home/me/mynfs 196.17.227.221/255.255.255.0(rw,sync,all_squash)
    The above means I want to export the /home/me/mynfs directory to computer 196.17.227.221 with the options "read&write" &"sync"&amp;"all_squash".
  3. Start/restart you nfs server: /etc/init.d/nfs-kernel-server start
That should works. To check if you are really exporting something you can use:

showmount -e localhost

and if you see:

Export list for localhost:
/home/mwolski/mynfs
196.17.227.221/255.255.255.0

it means it work. However, there can be little problem caused by firewall. I had to enable all connection to port 2041, to allow external users to connect to my share. To chack if you can mount this share from other computer, just run showmount -e IP_OF_NFS_server.

To run NFS server it is enought.

Later I will descripe how to mount nfs share and also I try to figure out all those nfs options, you can put to /etc/exports.

Monday, October 23, 2006

Finally my blog is live again

Hi everyone!!
Finally, after few months of absence my blog returns to the Web. Unfortunately, my former blog is down, due to server problem; hence, I decided not to wait longer and to sing up for google blog.

First thing, that I have to do, is to recreate my posts from my former blog. It should not take log. Afterwards, I will put some new content. Hope you will enjoy.

Friday, October 06, 2006

Mac X 10: How to make screenshots

Basically there are to ways of doing screen shots on Mac X:

1. Basic
2. Using Grab program

Basic
To capture entire screen you press Command-Shift-3 and the Picture 1.png image file is created on your desktop. You can also use Command-Shift-4 to select only part of your screen, and just like before Picture.png file will be created. On the other hand using Command-Shift-Control-4 instead of file, the screen is copied to the clipboard.

Using Grab program
This program is in Applications -> Utilities folder.

I personally prefer the Basic way of doing screen shots. For me it is more convenient. Moreover I prefer png files over tiff files, which are created by Grab.