Sunday, November 26, 2006

Ruby: one liners

Sometimes it is necessary to do some simple things, like replacing some words in given file, processing output of the Unix command. To such operation, usually writing a Ruby script is just unnecessary, due to possibility of creating one line so-called one liners programs, short script as Ruby command-line option: -e 'command' and -n , where:
  • -e 'command' - Executes command as one line of Ruby source. Several e’s are allowed, and the commands are treated as multiple lines in the same program. If programfile is omitted when e is present, execution stops after the e commands have been run. Programs 1.8 run using e have access to the old behavior of ranges and regular expressions in conditions—ranges of integers compare against the current input line number, and regular expressions match against $_.
  • -n - Assumes a while gets; ...; end loop around your program.

For example simple grep program can be implemented in Ruby as:

ruby -n- e "print if /blabla/" *.txt

Or, for instance, if one wants to save a list of all *.tif, *.TIF, *.tiff, *.TIFF files in given directory in lowercase:

ls -l | ruby -n -e ' puts split[8].downcase if $_.to_s.upcase =~/TIFF?/' > list.txt

Of course, those examples can be done without the need of Ruby, just by using standard Unix command, but sometimes it is easer to use e.g. Ruby (the same thing can be done using Perl, Python, awk,...) than to look for syntax of a given command. If one is familiar with e.g. Ruby, few separate Unix commands can be done in one Ruby command-line program.

Many other handy examples of ruby one liners can be found here.

Tuesday, November 07, 2006

Firefox 2.0: Session Restore

After more then a week of using Firefox 2.0 on my Intel Mac X 10.4 I can say it is nice, especially spell checking. Nonetheless, it is not super due to crashes.

For example, one of many websites that crashes Firefox 2.0 is . I do not know why, but I know that when it happens I have to use Safari to look for those 'bed' sites. Of course I do not say it is in general and true for every user of Firefox 2.0, I assume that it is only my Mac that has problem with this one website.

Despite this problem with some pages, it could be worse. The fact that it is not so totally bed, is new option in second edition of Firefox called Session Restore. From Firefox web side:

Now, with Session Restore, if Firefox has to restart or closes when it comes back you’ll pick up exactly where you left off. The windows and tabs you were using, the text you typed into forms, and the in-progress downloads you had running will all be restored. You can even set Firefox 2 to always restore your previous session instead of loading a home page, so you’ll never lose your place again.

And this is nice. Even thought my Firefox crashes, I do not loose all tabs and most importantly I do not have to go to pages again which sometimes may be difficult if you have many tabs open. I have them automatically restored.

Sunday, November 05, 2006

Matlab: Lack of multithread

In my everyday work I use Sun-Fire-V440 server. The main characteristics of this machine is the fact that it contains four processors, which is great. I use it mainly
to write image processing algorithms in Matlab 7.0, which are usually very time consuming and require wast amount of resources.

Despite the power of this machine, I cannot use it fully due to Matlab. This is because by definition it is only singe threaded application and there is on way of creating few threads. This of course causes that even thought given algorithm or application could be divided to e.g. four parallel threads, it cannot be done due to Matlab.

This drawback of Matlab is really frustrating, because Matlab uses fully only one processor out of four. On the other words, while one processors is fully loaded, three others do not do anything useful. As a result of that, my computations take four times more time to finish, compared with the situation if I could use all four processors in Matlab.

For instance, sometimes I write something in ANSI C, and there is no problem of creating threads; hence, my algorithms in ANSI C are four time faster than the same single threat algorithms on this Sun server, because they use all four processors.

Nevertheless, using single thread Matlab on four-processors server has some advantage. The main advantage of this is that I can run four separate Matlab instances, which I do often. For example, if I have one thousand images to process, I can run four matlab in four consoles, and I can tell that the first instance processes images from 1 to 250, second one processes 251 to 500, third and forth one 501-750 and 751 to 1000 respectively.
Moreover, I can run four Matlabs processing 1000 images each, but with different parameters. Consequently I can check my program with four different parameters and check their impact on the results simultaneously, rather than execute one version of program by another. This also uses four processors and I can do my work four times faster than on single Matlab instance.

Thanks to this, even though my application is single threaded, I can process 1000 images four time faster using four Matlabs than using one Matlab. This way, 100% of computation power of my server is used, and all four processor are doing something useful.

Below I present screen from top command, showing four Matlabs using in summary almost 100% of CPU power of my Sun.

Thursday, November 02, 2006

Mac X: Virtual desktops

Yesterday by a chance I went to Apple website and I found a sneak peak of Mac X 10.5 (Leopard).
I have to admit that it looks impressive; nevertheless, as far as I am concerned the only thing that I really look forward to is Spaces, or just simply virtual desktops, which by the way is great news.

I think I do not have to explain what virtual desktops are. Everyone who has every used Gnome or KDE in Linux should know it. The only thing I can say is that they are very, very convenient because:

They increase a user's ability to organize their windowed applications that are currently running by reducing clutter. - wikipedia

Lack of virtual desktops systems (free ones of course) on Mac X 10.4 used to annoy me a lot, because I use considerable amount of windowed application everyday at my work. Consequently, few months ago I started to look for some freeware application that provides virtual desktops for Intel iMac with Mac OS X 10.4. Nevertheless, at the time the only free program that I found did not work on Tiger - after starting it, my Mac just freezed. Unfortunately I do not remember the name of that program, or even where did I take it from.

Recently, due to sneak peak of Leopard I decided to give it one more try and to look for some free virtual desktop manager working on Tiger. And for my big surprise I have found it. It is called just Desktop Manager and its official site is here. I downloaded DesktopManager-0.5.3.dmg and I installed it and it worked!!!.

Thanks to this little program now I use six virtual desktop which is great. Finally I can open as many window application and consoles as I want, and I can manage through them very easily.

Bellow some pictures of my Mac and transition between desktops. Sorry for the quality, but I had to use digital camera to take them, because normal screen shots do not work with this.

Wednesday, November 01, 2006

Mac X: Ogg files and iTunes

Today I received some audio files in Ogg Vorbis format. I use Mac X 10.4, so I was very, very surprise that iTunes does not support Ogg files. As a result of that I did a little of reaserch and I found Quicktime Components Project which supposable is audio codec that adds Ogg Vorbis to QuickTime and iTunes. Unfortunately it did not help, thus my iTunes still does not support Ogg. Fastly I checked Whamb player and it worked.

Nevertheless I did not wanted to use Whamb - I just dont not like it - hence, the only choice I had (and the fastest because at the very moment I did not have time to struggle with that) was to decode my *.ogg files to mp3 files. To to this I had to use oggdec program which gave me wave files, and than using lame encoder I converted those *.wav files to mp3. Of course prior to this, I had to install all necessary stuff like oggdec and lame.

I wander why Apple does not support ogg? I thought it was free open source format?

Gimp: Changing language back to english

I do not know why, but one day my Gimp 2.2.10 on my Mac X changed its language from English to Japaneses. I do not know how it happened, but it happened.

As usually in such cases I did a little of web searching and I found some solutions. First proposed solution was to set LANG environment variable to 'en'. Of course I tried it, and of course it did not work.

Second solution that fortunately worked was to delete folder containing Japanese language in Gimp's directory. Hence, I deleted folder ja from:


After restarting Gimp I had it finally in English.

In conclusion, as far as my case is considered the solution to Gimp language problems is to delete given lang folder from directory.