Sunday, December 20, 2009

SyntaxHighlighter 2.1 test ob blogger

/**
     * SyntaxHighlighter
     */
    function foo()
    {
        // it works!
    }
SyntaxHighlighter 2.1 failure! Maybe next time I'll find out how to make it work.

Monday, December 07, 2009

Easy installation of Microsoft Office 2007 in Fedora 12 using Wine

As far as I'm concerned, the only thing that forces me to use Windows or Mac from time to time is lack of Microsoft Office World, Excel and PowerPoint 2007 in Linux. In the web you can find info how to install it using wine in Ubuntu, but I could not find much about Fedora, especially Fedora 12. Here I present my experience of installing Office 2007 in Fedora 12. I must admit though, the what I'm going to write is based on my memory, so please feel free to comment if I skipped or made some mistakes.

First thing that we need to do is to install wine (in fedora 12 it is wine-1.1.32-1) sudo yum install wineNow we can install Microsoft Office 2007 from CD. I used Microsoft Office 2007 Enterprise and I install only Word, Excel and PowerPoint and some default common office tools. For the installation I did not had to do any tweaking of wine, or did not had to use winetricks to download any additional dlls!

After the installation, I could start Word and Excel, but not PowerPoint. Also Equation editor in Word did not work, since the windows with default equation formulas were empty!

To fix it, it was necessary to go to Libraries in winecfg and in "New override for library" select usp10 and then set it to native as seen below:


This was enough to make equations work and also enough to start PowerPoint, though I'm not sure of the former.

I also tried to install Microsoft Office 2007 Service Pack 2, but without success. To install this service packed I tried installing some stuff from winetricks, changing some dlls from build in to native, but nothing seemed to work.

In concussion, to install and run Microsoft World 2007, Excel 2007 and PowerPoint 2007 nothing special was necessary. Just install wine from repositories, and set usp10 library to native in winecfg window. However, as I said at the beginning I'm writing this from my memory and I hope I did not skip something. Nevertheless, I think this post might be useful to other Linux users.

Sunday, December 06, 2009

Installing xdebug in xampp (lampp) on fedora 12

xdebug is a PHP extension for powerful debugging. It supports stack and function traces, profiling information and memory allocation and script execution analysis.

By default xampp-linux-1.7.1 does not come with this extension. Therefore, it is necessary to install it.

First, assuming that there is already xampp-linux-1.7.1 installed in /opt/lampp it is necessary to download xampp development package. In my case it was xampp-linux-devel-1.7.1.tar.gz and unpack it
su
tar xvfz xampp-linux-devel-1.7.1.tar.gz -C /opt


Second, install autoconf package (as a root) yum install autoconf


Thrid, xdebug can be installed (as a root)/opt/lampp/bin/pecl install xdebugIf compilation was successful you should get Build process completed successfully
Installing '/opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.0.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=xdebug.so" to php.ini
Finally, localization of xdebug.so should be added to php.ini. In my case, xdebug.so was in opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/. This extension should be added using "zend_extension" and not "extension", therefore, you should ignore the prompt about adding "extension=xdebug.so" to php.ini. Hance, add the following line to /opt/lampp/etc/php.ini restart your lampp server
zend_extension="/opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"
If everything went wine, using phpinfo() you should get:

Testing Basic HTTP Authentication using PHPUnit in Zend Framework

In Zend Framework, there are few ways of restricting access to the protected areas of an application. The simplest and the fastest to implement is Basic HTTP Authentication. However, it must be remembered that it is not the most secure way, as it is using base64 coding that can be easily broken.

Although Zend_Auth reference guide shows how to implement Basic HTTP Authentication, it does not show how to test it using PHPUnit. Therefore, in this post, a simple example of a Zend project, along with the associated PHPUnit test case is presented. The full source code of the project is available for download. The project's name is httpautheg.

So, lets assume that we want to restrict access to the actions called protectedAction and editAction in IndexController. We also want a simple action helper, called HTTPAuth that will do the authentication. Finally, we want a test case for IndexController that will perform the tests of incorrect and correct authentications, so that we do not have to worry that during further work on the project we will break something without noticing it.

The directory tree of the httpautheg project





HTTPAuth action helper

The helper has only one function called doBasicHTTPAuth that does the authentication, and returns false or true if authentication failed or succeeded, respectively.
<?php
class My_Controller_Action_Helper_HTTPAuth extends Zend_Controller_Action_Helper_Abstract {
//put your code here

/**
* Perform Basic HTTP authentication
*
* @return boolean authentication successful or not
*/
public function doBasicHTTPAuth() {

$path = APPLICATION_PATH .'/configs/passwdBasic.txt';
$resolver = new Zend_Auth_Adapter_Http_Resolver_File($path);
$config = array(
'accept_schemes' => 'basic',
'realm' => 'Admin Area',
'digest_domains' => '/index',
'nonce_timeout' => 3600,
);
$adapter = new Zend_Auth_Adapter_Http($config);
$adapter->setBasicResolver($resolver);

$request = $this->getRequest();
$response = $this->getResponse();
$adapter->setRequest($request);
$adapter->setResponse($response);

$result = $adapter->authenticate();

if (!$result->isValid()) {
// Bad userame/password, or canceled password prompt
return false;
} else {
return true;
}
}

}


IndexController.php

In the preDispatch() function, public actions that do not require authentication are specified. For all other actions in the controller, basic HTTP Authentication using the above helper. For this example 'index' and 'failedauth' actions are public, while actions named 'protected' and 'edit' require authentication.
<?php
class IndexController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
}
public function preDispatch() {
//any action not in this array will require Auth
$publicActions = array('index','failedauth');
$action = $this->getRequest()->getActionName();

if (!in_array($action, $publicActions)) {
//if requested action is non public one,
//then do authenticated
if ($this->_helper->_HTTPAuth->doBasicHTTPAuth() == false) {
$this->_forward('failedauth');
return;
}
}
}

public function indexAction() {
// public action

}
public function protectedAction() {
// protected action

}
public function editAction() {
// protected action

}

public function failedauthAction() {
//public action
}


}


PHPUnit test case IndexControllerTest.php

Testing of failed authentication is quite easy. The trick is with testing correct authentication, since we have to sent a correct HTTP header to the server. In our case, the username is 'admin' and the password is 'admin12', so before we dispatch the request for a protectedAction, we need to set a header as follows:$this->request->setHeader('Authorization','Basic YWRtaW46YWRtaW4xMg==');where “YWRtaW46YWRtaW4xMg==” is a result of base64_encode("admin:admin12") function.
The full code of the IndexControllerTest.php is <?php

class IndexControllerTest extends ControllerTestCase {

/**
* Check if we go to index controller
*/
public function testIndexController() {
$this->dispatch('/');
$this->assertController('index');
$this->assertAction('index');

}

/**
* Prived an array with protected actions
*
* @return array
*/
public function getProtectedActions() {
return array(
array('/index/protected'),
array('/index/edit'),

);
}

/**
* Going to /index/protected should result in Www-Authenticate header
* and since we do not send header with correct passwors and user
* we should be forwarded to failedauth action and see 'Failed authentification'
*
* @dataProvider getProtectedActions
*/
public function testAccessWithoutAuth($action='/index/protected') {

$this->dispatch($action);

$this->assertHeader('Www-Authenticate');
$this->assertAction('failedauth');
$this->assertQueryContentContains('h1', 'Failed Authentication');
}


/**
* Going to /index/index, but this time we send a header
* with some inccorect password:username, codded as base64_encode.
* Since username and password are inccorect we should get 'Www-Authenticate'
* header.
*
* @dataProvider getProtectedActions
*
*/
public function testAccessWithInccorectAuthCredentials($action='/index/protected') {

//send header with inccorect user and password, e.g.
//YWRtaW46d3JvbnRwYXNzd29yZA== is equal to base64_encode("admin:wrontpassword")
$this->request->setHeader('Authorization','Basic YWRtaW46d3JvbnRwYXNzd29yZA==');

$this->dispatch($action);
$this->assertHeader('Www-Authenticate');
$this->assertNotQueryContentContains('h1', 'Successful Authentication');

}


/**
* Going to /index/index, but this time we send a header
* with CORRECT password:username, codded as base64_encode.
* Since username and password are ccorect we should not get 'Www-Authenticate'
* header and be able to see protected content.
*
* @dataProvider getProtectedActions
*/
public function testAccessWithCorrectAuthCredentials($action='/index/protected') {

//send header with correct user and password i.e.
//YWRtaW46YWRtaW4xMg== is equal to base64_encode("admin:admin12")
$this->request->setHeader('Authorization','Basic YWRtaW46YWRtaW4xMg==');

$this->dispatch($action);
$this->assertNotHeader('Www-Authenticate');
$this->assertQueryContentContains('h1', 'Successful Authentication');

if ($action == '/index/protected') {
$this->assertQueryContentContains('h2', 'Protected action');
} elseif ($action == '/index/edit') {
$this->assertQueryContentContains('h2', 'Edit action');
}
}
}

Some example screenshots

After clicking “Go to protected action” we are ask for credentials:


Failed Authentication:


Successful access to protected action:


Execution of PHPUnit


Download the project (httpautheg.tar.bz2)

The project was done using ZendFramework-1.9.3PL1. For simplicity, the archive file contains the Zend library. Therefore, just download the archive, unpack it, change file rights if necessary and it should work. Assuming that the project directory is in your root path in the localhost, than it can be executed with url: http://localhost/httpautheg/public/. If xampp is used, it must be remembered that PHPUnit version that comes with xampp is too old and it must be upgraded to execute PHPUnit tests. To execute PHPUnits, a phpunit command must be used in a terminal - not a web browser.

Testing Digest HTTP Authentication

Testing Digest HTTP Authentication can be done in a similar way. The difference would be in setting a correct header when requesting for protected action. More details on Digest HTTP Authentication header is here.

Friday, December 04, 2009

Installing Abaqus 6.9 on CentOS 5.3

In the past post I was installing Abaqus 6.9 on CentOS 5.2. However, the CentOS was upgraded to 5.3 and I needed to also install newer version of Abaqus, i.e. Abaqus 6.9. So, how did it go?

Just like before, the first problem was: Unable to determine Linux Distribution. But this time it was neccessery to put "Red Hat Enterprise Linux Server release 5.0 (Tikanga)" into /etc/redhat-release. To change this file root permissions are required.

After that there were no more problems with the installation or the execution of abaqus 6.9.

After the installation was finished I just original content (i.e. "CentOS release 5.3 (Final)") of /etc/redhat-release back.

Thursday, December 03, 2009

PHP: Generation of MD5 hash for HTTP digest access authentication

HTTP Digest access authentication is one of the agreed methods a web server can use to negotiate credentials with a web user (using the HTTP protocol). Digest authentication is intended to supersede unencrypted use of the Basic access authentication, allowing user identity to be established securely without having to send a password in plaintext over the network. Digest authentication is basically an application of MD5 cryptographic hashing with usage of nonce values to prevent cryptanalysis.

For example, lets assume that we want to allow a user called "adminuser" access a realm called "Admin Realm" with a password "secretpassword". Using php command the MD5 hash for this can be generated using:php -r 'echo MD5("adminuser:Admin Realm:secretpassword")."\n";'This gives the following MD5 hash:3228e0b5f8ae5ffb249d16125baffe63Therefore, for example when using Zend_Auth in Zend Framework, a file e.g. 'files/passwd.txt' with the username,realm and password that has to go into a resolver Zend_Auth_Adapter_Http_Resolver_File can containadminuser:Admin Realm:3228e0b5f8ae5ffb249d16125baffe63


In case of basic authentication, in 'files/passwd.txt' we would have password in a plain textadminuser:Admin Realm:secretpassword

Thursday, November 26, 2009

Zend Framework: Returning pdf file from an action controller

Lets assume that we have an action called getpdfAction in a Zend Controller. When we execute the action in a browser (e.g. http:://www.oursite.com/somezfcontroller/getpdf), the Zend Application by default will render view associated with the action and if necessary layout. However, when we want to have a pdf file returned or any other file from the action this behaviour is not needed. So, before we read a pdf for returning, we have to disable view script and layout rendering. This can be done as in the example getpdfAction function below:public function getpdfAction() {

//Disable rendering of view script and layout
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();


// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('some_pdf_file.pdf');
}

Paired t-test

I always forget what P value must be to reject or not to reject null hypothesis in paired t-test. So lets explain by the example.

H0 - null hypothesis - there is no
significant difference
between method A and B
H1 - alternative hypothesis - there is difference
(two tail test)

For example [with 5% (a=0.05) level of significance ]:

Based on the above results I can say that: since P=0.009103483 and this is lover than a=0.05 (P<0.05), I can claim that:

I'm 95% (a=0.05) sure that there is significant
difference between A and B, because (P<0.05).


On the other hand, we can have:

In this case P=0.649507752, hence I can claim that:

I'm 95% (a=0.05) sure that there is no significant
difference between A and B, because (P>0.05).

Above paired t-test was performed in Excel.

Wednesday, November 18, 2009

Octave is getting faster!


GNU Octave "is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language. "

In numerical calculations speed is very important. I remember that Octave 2.x, although very useful, used to be slow. What about now when there is Octave 3.2? To answer to this question I decided to compare the time performance of this new Octave release with the performances of versions 3.0 and 2.9 using custom made benchmark script. The tests were run on Intel Mac OS X 10.4.11 (Intel Core Duo 2GHz, 2GB RAM).

 The results of 21 small tests for matrix and I/O operations are below or can be downloaded from here (gnumeric file):

The bar plot of normalized geometric mean of the 21 tests is as follows (lower is better):

The graph shows that Octave 3.2 is a bit faster than the two previous versions! Additionally, since the time values used were normalized against times obtained using MATLAB 7.4.0.287 (R2007a) on Intel Mac OS X 10.4.11 (Intel Core Duo 2GHz, 2GB RAM), it can be seen that Octave 3.2 is faster than the MATLAB R2007a. However, based on the data in the table it can be seen that the lowest performance of the Octave, as compared to Matlab is, for loop (row 13 in the table). The Octave 3.2 was nearly 700 times slower than the Matlab in execution of nested for loops! At the same time Matlab was about 1000 times slower than Octave 3.2 (but not Octave 2.9 and 3.0) in creation of sparse matrices (row 14 in the table)!!!

Should we be very excited about this! Probably not very much, since my analysis has a number of limitations. The main one is that the question whether Octave 3.2 is faster than the older versions should be answered using some real statistical approach e.g. Mann-Whiten U tests or Ordinary least products (OLP) regression analysis, influence of outliners (row 13 and 14) should be investigated etc. There is also a question whether the differences in the performance obtained are statistically significant, or whether Octave 3.2 is statistically significantly faster/slower that Matlab?

Nonetheless, I think that the results obtained clearly show a trend of increased performance for Octave.

I'm also planning to repeat the above tests for Octave 3.2, but this time with inclusion of image processing functions such as: image creation, rotation, resize, image saving etc. I could not do this for is post, because there were problems compiling image package for Octave 2.9 and 3.0 on my Mac.

Wednesday, November 11, 2009

Linux Mint: Positive first impression


Today, my Arch Linux died on a PC at work. Since I had not time to play with it, I decided to install something new. First I wanted to install Ubuntu 9.10, but usually after the installation of Ubuntu I have to spent time installing audio/video codes, flash player etc. I've heard that in Linux Mint all this stuff is pre-installed. I've also heard positive comments about this Ubuntu-base distro, so today I decided to give it a try.

I downloaded Mint 7 Gloria, made backup of my home folder form Arch and I did a fresh installation on my dual-screen PC.

In short: installation was very smooth and quite fast. After the installation, I could play my video and audio files, DVDs without any tweaking or installing any packages. The one thing that required installation was nvidia driver, but there were no problems with it. As a gnome user, the only thing that I found strange was the mintMenu at the bottom panel, no top panel and very dark default Mint theme. So I had to manually adjust these things. It just took few minutes.

Some screens:









In conclusion, my first impression of Linux Mint is very positive. Now I look forward to Mint 8!

Monday, November 09, 2009

Effect size of matched and unmatched data in Maltab

"In statistics, an effect size is a measure of the strength of the relationship between two variables in a statistical population, or a sample-based estimate of that quantity" from here.

The script below calculates effect size for matched (paired) and unmatched (unpaired) data as described in [1].

So, if C and E sets are matched than effect size d is 1.265. If C and E sets are unmatched than effect size d is also 1.265. The two sizes are equal for this example only.

References

[1] Dunlop, W. P., Cortina, J. M., Vaslow, J. B., & Burke, M. J. (1996).
Meta-analysis of experiments with matched groups or repeated measures designs. Psychological Methods, 1, 170-177.

Saturday, November 07, 2009

Useful Matlab / Octave script to make default function arguments easier


The lack of an easy way of making default function arguments in MATLAB (I have R2007a) was annoying me for a long time. Octave (current version 3.2), which is an open source equivalent of MATLAB is much better in terms of handling default function arguments.

What I mean, can be explained best by simple example.

So, lets define a very simple m file, called testDefaultArguments.m with the following function.
function testDefaultArguments(a=1,b=2)
fprintf(1,'a=%d, b=%d\n',a,b);
We see that we have a function called testDefaultArguments with two arguments a and b, and we assign default values to them, i.e. 1 and 2, respectively. Such syntax seems to be helpful, because you can execute testDefaultArguments function as follows:octave-3.2.3:40> testDefaultArguments()
a=1, b=2
octave-3.2.3:41> testDefaultArguments(3)
a=3, b=2
octave-3.2.3:42> testDefaultArguments(a=43)
a=43, b=2
octave-3.2.3:43> testDefaultArguments(a=43,5)
a=43, b=5
octave-3.2.3:44> testDefaultArguments(a=43,b=55)
a=43, b=55
So everything seems good, but what if you want to assign only value to b, and not to a?Lets tryoctave-3.2.3:47> testDefaultArguments(b=55)
a=55, b=2 %!!! a is 55 and not b !!! This is strange
So, this is not only strange, but can easily leads to bugs in one's code.

So this was Octave. What about MATLAB? The anwser is: Error! The function testDefaultArguments can't be executed in Matlab (at least in R2007a and R2008a) due to syntax errors!

So, to help myself with making default function arguments in both MATLAB and Octave I made getfunargs.m script, which makes working with default function arguments little easier (To be honest, this function is a modification of getargs.m script). I'm not going to explain how it works, instead I'm just going to show how it can be used.

Lets define function called testDefaultArguments2 which uses getfunargs:function testDefaultArguments2(varargin)

defaults = struct(...
'a',1,...
'b',2 ...
);

args = getfunargs(defaults, varargin);

fprintf(1,'a=%d, b=%d\n',args.a,args.b);
Now, the testDefaultArguments2 can be executed both in MATLAB and Octave as follows:octave-3.2.3:52> testDefaultArguments2()
a=1, b=2
octave-3.2.3:53> testDefaultArguments2('a',3)
a=3, b=2
octave-3.2.3:54> testDefaultArguments2('b',5)
a=1, b=5
octave-3.2.3:55> testDefaultArguments2('b',5,'a',2)
a=2, b=5
It can bee seen now, that the default arguments work as expected.

Thursday, November 05, 2009

Install Octave 3.2 with Image Processing package on Arch Linux


Octave is a great tool for programming mathematics. However, when I wanted to install it and its Image processing package on Arch Linux I got a problem: missing lgfortranbegin library.

To begin with I installed Octave using pacman as a root or sudo user (I prefer sudo)sudo pacman -Sy; pacman -S octave There was no problems with this.
Now, to install Image processing package it is necessary to do as follows:
To install a package, download the package file, and install it from the Octave prompt by typing pkg install package_file_name.tar.gz
where package_file_name.tar.gz is the name of the file you downloaded.
In my case the package_file_name.tar.gz was image-1.0.10.tar.gz and it can be downloaded form here

So, when I tried to install it I got the following erroroctave:1> pkg install image-1.0.10.tar.gz
warning: creating installation directory /usr/share/octave/packages
/usr/bin/ld: cannot find -lgfortranbegin
collect2: ld returned 1 exit status
make: *** [__spatial_filtering__.oct] Error 1
'make' returned the following error: make: Entering directory `/tmp/oct-9oWZxI/image-1.0.10/src'
mkoctfile __spatial_filtering__.cc
make: Leaving directory `/tmp/oct-9oWZxI/image-1.0.10/src'
error: called from `pkg>configure_make' in file /usr/share/octave/3.2.3/m/pkg/pkg.m near line 1253, column 2
error: called from:
error: /usr/share/octave/3.2.3/m/pkg/pkg.m at line 714, column 5
error: /usr/share/octave/3.2.3/m/pkg/pkg.m at line 287, column 7
As it can be seen lgfortranbegin is missing. The library belongs to fortran compiler and in Arch Linux it can be installed simply by sudo pacman -S gcc-fortranTo make sure that the library was installed I performed a search and I found it[marcin@arch ~]$ sudo find / -name "*fortranbegin*"
/usr/lib/gcc/i686-pc-linux-gnu/4.4.2/libgfortranbegin.a
After this there was no problems. I installed Image package as previously described and I checked if I got functions that I needed (e.g. imrotate, imresize) by: octave:1> pkg install image-1.0.10.tar.gz
octave:2> imr<tabulator>
imread imresize imrotate_Fourier
imremap imrotate
octave:2>
The installation was almost finished. I needed one more package, i.e. gnuplot to be able to display figuressudo pacman -S gnuplotFinally, I did some simple test to create an image, make a rotated version of it and display both of themoctave:8> I=randn(256);
octave:9> I2=imrotate(I,30);
octave:10> figure,imshow(I);
octave:11> figure,imshow(I2);


Conclusion

So in conclusion, to install octave along with Image package I needed to have gcc-fortran and gnuplot installed first:sudo pacman -S gcc-fortran gnuplot octaveThen follow general instruction on how to install Octave packages from here.

Wednesday, November 04, 2009

Performance benchmark script for MATLAB and Octave

The benchmark script evaluates MATLAB and Octave not only in execution times for matrix manipulation, but also for integration, 2D interpolation, solving nonlinear equation, reading/writing files, plot creating and saving, image manipulation (e.g. rotation, filtering, erosion).

The execution times of each test (27 tests for now) are normalized against times obtained using MATLAB 7.4.0.287 (R2007a) on Intel Mac OS X 10.4.11 (Intel Core Duo
2GHz, 2GB RAM).

As a results the script generates txt file with the time values (normalized and not normalized) obtained for each test.

The evaluate overall performance of MATLAB/Octave geometric and arithmetic means of the individual time values obtained are used.

The header
function res = mybench(varargin)
% res = mybench('noOfRepeats', 3,...
% 'normalize',true,...
% 'onlyTests',[],...
% 'imagep',true)
%
% Benchmark script for MATLAB and Octave.
% Tested on Matlab 2009a and Octave 3.2.2 for Windows.
%
% Execution time of basic matrix manipulation function is tested along with
% integration, solving nonlinear equation, image processing functions ( if
% available), saving/loading matrices to a file system, for loop,
% binary operation,etc. In total 27
% tests are performed (less if image processing functions not available).
%
% All results are normalized against the results obtained
% using MATLAB 7.4.0.287 (R2007a) on Intel Mac OS X 10.4.11 (Intel Core Duo
% 2GHz, 2GB RAM)
%
% At the end, arithmetic and geometric means of the times obtained are
% calculated. All results obtained are stored in a txt file named
% results_.txt.
%
% INPUT
% noOfRepeats - int - number of times each test is executed (default 3)
% normalize - boolean - normalize results (default true).
% onlyTests - int vector - do only tests given (default [], i.e. do all tests)
% imagep - boolean - do or not image processing testing
%
% OUTPUT
% res - struct - normalized geometric mean .
%
% EXAMPLES
% res = mybench(); %perform all tests with default settings.
% res = mybench('noOfRepeats',10); %perform all tests 10 times.
% res = mybench('onlyTests',[1,5,8]); %perform only tests 1,5 and 8.
% res = mybench('noOfRepeats', 1,'normalize',false); % repeat 1 time
% %each tests and do not
% %normalize results.
%
% Site: http:\\shortrecipes.blogspot.com
% Date: Nov 2009

The full code is here.
Example output from the script is as follows:



Friday, October 30, 2009

Comparing two methods of measurement in Matalb or Octave

How to compare two methods that measure the same variable? How to check if one method produces results constantly higher than the other method and if the difference is significant? How to determine whether new method, can replace the old one? The anwser to these question lies in the analysis of bias between the two method of measurement [1]. Below is a Matlab implementation of Ordinary least products (OLP) regression analysis [1] that can be used to anwser to these questions.

function res = mgr(X,Y,varargin)
% res = mgr(X,Y)
% Mean Geometric Regression (i.e. Ordinary least products (OLP) regression)
%
% Used to evaluate two measurement methods in terms of fixed and
% proportional bias as described in [1].
%
% INPUT
% X - vector of values calculated by the first method.
% Y - vector of values calculated by the second method.
% boolean - plot or not scattergram of the values
% string - label of x axis of scattergram
% string - label of y axis of scattergram
%
%
% EXAMPLE from [1]:
%
% M1=[132 138 144 146 148 152 158 130 162 168 172 174 180 180 188 194 194 200 200 204 210 210 216 220 220];
% M2=[130 134 132 140 150 144 150 122 160 150 160 178 168 174 186 172 182 178 196 188 180 196 210 190 202];
%
% res=mgr(M1,M2)
%res =
%
% a: 13.9506
% b: 0.8611
% a_CI95: [-6.9476 32.3364]
% b_CI95: [0.7576 0.9788]
%
% res=mgr(M1,M2,true); % produces scattergram
% res=mgr(M1,M2,true,'SBP(M1)','SBP(M2)'); % produces scattergram with x and y axis
% % labels of 'SBP(M1)' and 'SBP(M2)'
%
% NOTE:
% The above calculated values of a and b agree with the values in [1]
% Values of CI95 are marginally different, becouse, as expalined in [1],
% they are calculated using approximate formulas.
%
%REFERENCE:
%[1] J Ludbrook, Comparing methods of measurement, Clinical and
% Experimental Pharmacology and Physiology, 1997:24,193-203
%
%
%
Full code of the above script is here.
Script was tested on Matlab 2007a and in Octave 3.2. En example of the scattergram that is generated by the script is below. This scattergram is the same as an example given in [1].

References

[1] J Ludbrook, Comparing methods of measurement, Clinical and Experimental Pharmacology and Physiology, 1997:24,193-203.

Friday, October 23, 2009

Ten clicks to install Ubuntu 9.04

Update: My honest mistake: It should be Ubuntu 9.10, not 9.04. Thanks Tom!



Click 1

Click Enter for English language.

Clicks 2 and 3

Go to Install Ubuntu and Click Enter.

Click 4

Forward.


Click5

Forward.



Click 6

Forward


Click 7

Forward



Click 8

Forward (Typing username and password were not counted).


Click 9

Install


Waiting for installation to complete


Click 10

Restart Now and Ubuntu installation is finished.


So 10 clicks is enough to install Ubuntu 9.04 9.10. Is it much or not? You can compare it with the number of clicks in other systems [Image taken from Benchmarked: Ubuntu vs Vista vs Windows 7]:

Monday, October 19, 2009

How ImageMagick can save you time


ImageMagick is a great tool! Many times it has saved me from programming. Here is just one example of many applications of the ImageMagick that can save one's time.

Recently, I was asked to check whether a set of images (few hundreds of them) contains one particular image. So, basically I was given and image (called testImage.tiff), and I needed to check whether this image belongs to a set a few hundreds of images. En example of the images from the set is below:


So how to anwser this? You can do manual (i.e. visual comparison), you can also write some simple script in Python with PIL. All would take some time. But why to do this, if you can just write one for loop in bash with compare command from ImageMagick?

So first thing I did is to create the following folders:

The input folder contains all images from the set, the output folder will be where the results of my search will be stored, and the testImage.tiff is my test image.

So thanks to ImageMagick's, the only thing I had to do is to execute its compare command on all images from the set (i.e. input folder).  In other words, I compared, in a loop, an image from the set with the testImage.tiff and the results of the comparison went to output folder. Below is the bash for loop that was executed inside the input folder.
for f in *.tiff ; do
compare $f ../testImage.tiff ../output/$f ;
done
After loop finished, I could go to output folder, and quickly find out whether testImage.tiff is in the set:


As can be seen in the above picture, the testImage.tiff belongs this this set.

One limitation of this procedure is that I had to manually go through the output folder. But because all not matching images are in red, it was very quick to find, the one image that was not red. (if any). Additionally, if the testImage.tiff and images from the set were of different size or type additional code would be necessary.

So thanks to the ImageMagick, I could do the job in just a few minutes.

Sunday, October 18, 2009

xampp / lampp: upgrade PHPUnit in lampp 1.7.1

The default PHPUnit that ships with lampp 1.7.1 is not suited for use with Zend Framework 1.9. The reason is that the PHPUnit version in lampp is to low. So it is necessary to upgrade it using pear. However, before it can be done, pear version that comes with lampp 1.7.1 needs to be also upgraded. The pear executable is in /opt/lampp/bin so I went to this folder.

First PHPUnit channel must be added sudo ./pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded


Then pear chanels can be updated sudo ./pear update-channels

Then we can try to install PHPUnit:sudo ./pear install phpunit/PHPUnit
phpunit/PHPUnit requires PEAR Installer (version >= 1.8.1), installed version is 1.7.1
phpunit/PHPUnit requires package "pear/Image_GraphViz" (version >= 1.2.1), installed version is 1.1.0
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.5)
No valid packages found
install failed


To upgrade pear I used sudo ./pear upgrade PEAR To check if upgrade was successful I used ./pear -V
PEAR Version: 1.9.0
PHP Version: 5.2.9
Zend Engine Version: 2.2.0
Running on: Linux arch 2.6.31-ARCH #1 SMP PREEMPT Tue Oct 13 13:36:23 CEST 2009 i686


Before PHPUnit can be upgraded, pear/Image_GraphViz package must be upgraded first. So sudo ./pear upgrade pear/Image_GraphViz
downloading Image_GraphViz-1.2.1.tgz ...
Starting to download Image_GraphViz-1.2.1.tgz (4,872 bytes)
.....done: 4,872 bytes
upgrade ok: channel://pear.php.net/Image_GraphViz-1.2.1

and install PHPUnit ./pear install -a phpunit/PHPUnit
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.5)
downloading PHPUnit-3.4.1.tgz ...
Starting to download PHPUnit-3.4.1.tgz (326,659 bytes)
...................................................................done: 326,659 bytes
install ok: channel://pear.phpunit.de/PHPUnit-3.4.1

After this, when I could use PHPUnit with Zend Framework as described in a tutorial here.

Thursday, October 15, 2009

SPSS: create random binary variable

To create a random binary variable called e.g. new_var one can use the following code:COMPUTE new_var=RND(RV.UNIFORM(0,1)).
EXECUTE.
or

Wednesday, October 07, 2009

Xampp: SQLSTATE[HY000] [2002] Invalid argument

I'm currently developing a web application using Zend Framework 1.9.x. For this purpose I used Xampp for linux (i.e. lampp 1.7.2). Most of the time I was using arch linux with xampp 1.7.2 and there was no problem. Then I changed my os to Ubuntu 9.04 and I installed the same xampp 1.7.2. Interestingly, when I wanted to run my application under Ubuntu I got an errorMessage: SQLSTATE[HY000] [2002] Invalid argumentAfter googling I found that the reason was that in my php.ini (i.e. for xampp it is /opt/lampp/etc/php.ini) the variable pdo_mysql.default_socket was not set: Therefore I set it to /opt/lampp/var/mysql/mysql.sockOf course, I also had to restart Xamppsudo /opt/lampp/lampp restartIt is interesting why Xampp 1.7.2 worked under Arch Linux, but it did not work under Ubuntu 9.04?

Friday, October 02, 2009

Gnome desktop icons disappear

I have gnome 2.26.3 and sometimes all my icons on the desktop disappear. I found that in Gnome, program called nautilus manages it. So for my case it was enough to restart/start itnautilus --no-default-window &Nautilus is the same program that you use for browsing folders under Gnome.

Sunday, September 27, 2009

TOP PHP frameworks in terms of number of books published on a given framework

To begin with, I would like to say that I'm not new to PHP 4 and 5, but I would not call myself an expert. The reason is that for a few years now, PHP and web applications are things that I do only in my spare time. I also haven't used all the PHP frameworks, nor I'm the expert in any of them. I'm basically looking for a framework that is worth looking into and which would save me a time, as I don't want to spend much time on weekends coding e.g. only user authorization or registration form validation. So far, I did some small jobs using Prado, CakePHP and now I'm starting to learn Zend Framework. I also heard lots of good about CodeIgniter and Symfony. Since, I couldn't learn all of them, I just wanted to find some way of determining which of them seems to be the most popular. I decided to check how many books on a given framework are available in amazon.com?

I chose this criterion because for a person that wants to learn a framework, examples along with explanation can be valuable. Apart from official tutorials, quick start guides and reference manuals, that are available on the websites of the frameworks, books can provide good introduction along with example applications. Additionally, a number of books, also shows to some extend, how popular a given framework is. I would imaging that not many authors would write books about unpopular frameworks. Off course, there are many other possible criteria that can be used to rank or compare PHP frameworks, such as documentation, community support, performance, fast availability of updates etc., but I think that the number of books somehow translates to the the popularity of a framework.

Method of comparison

I went to amazon.com and I performed advanced searches for books on a given framework written in English and published after 2007. A year 2007 was chosen because books older than two years may contain highly outdated information. A framework name was a keyword. Than, the number of books found was counted.

En example of a search criteria used is given below:

Results

The results are as follows:
Zend Framework - 10 books (see the books)
Symfony - 9 books (see the books)
CakePHP - 3 books (see the books)
CodeIgniter - 2 book (see the books)
Prado - 0 books (see the books)

My personal opinion

From my perspective, the experience that I had with Prado (I used v. 3.1.4) was the worst one, although it's concept was interesting. I found it difficult to learn and use, because the documentation was sparse, there were not many tutorials, and even if I was wiling to invest some money and buy a book about it, I could not find any books about it.

After Prado I tried CakePHP (I was using v. 1.2). I found it very good, easy to learn and fast to use, as long as I adhered to all the naming conventions. Especially, I liked the ORM (Object-Relational Mapping) which was very useful and saved me a lot of time. The problem I had with it, was that it uses PHP 4, which already has been discontinued. Off course, sooner or later CakePHP will move to PHP 5, but I wanted to use a PHP 5. I think it would be better to use something in PHP 5, rather than something that is developed in a version of language that is already not supported. Off course, CakePHP runs smoothly on PHP 5. It is only CakePHP's core that does not use features of PHP 5. Additionally, I wanted to have more freedom when programming, and CakePHP does not allow for much of it due to it's "convention over configuration paradigm". But this is a price that you pay in CakePHP.

At the moment I'm learning Zend Framework v1.9 which is build using PHP 5. For now, I can say that it is definitely more difficult to learn at the beginning. The biggest issue that I had at the beginning, and still have but to a lesser extend, is a bootstrap class, which is difficult to understand. It really took me a long time to begin to understand how to use it at the simplest level. However, what I like is, that Zend Framework is less rigid than CakePHP, it uses PHP 5, and it has a vast number of tools (e.g. for working with PDF files or captcha) that CakePHP does not have.

I don't have experience with Symfony and CodeIgniter, so I cannot say anything apart from what I read in the Internet. CodeIgniter is considered to be faster than CakePHP and just like CakePHP, it is written for PHP 4, whereas Symfony is for PHP 5 only.

In conclusion, it seems that when you look only at the number of books on a given framework, Zend Framework is the winner with eight books. At the moment, I'm trying to get to know it, hopping that it will not be a waste of time. It must be remembered though, that there are many other criteria that can be used to compare PHP frameworks. However the final decision which framework to choose, if any, should be based on the specific needs of a project that we want to developed.

Tuesday, September 01, 2009

Arch linux: RGB to CMYK

To convert RGB files to CMYK files (for printing) one can install Separate+ Gimp plugin.

My gimp version was 2.6.6. I downloaded Separate+ plugin sources (now version 0.54) and unpacked them. Than I installed littleCMS library sudo pacman -S lcms. After that I compiled the Separate+ plugin and installed it: make
sudo make install
I also needed Adobe ICC Profiles that can be found here (does not meter that they are for Windows). I unpacked the downloaded archive and I copied the contents of RGB and CMYK folders to usr/share/color/icc/ folder. That's it!

Tuesday, July 28, 2009

Firefox: images don't load but work everywhere else

I had problem with one of my sites. When testing on firefox, some banners did not load. My site was sponsored so I had sponsors banners on my front page. The banners were located on the localhost i.e. <img alt="toyto logo" src="./images/sponsors/toyota.gif" />
<img alt="honda logo" src="./images/sponsors/honda.gif" />
Of course everything was fine in IE, Opera an Safari.

I found that my AdBlock Plus was blocking my banners because the following default filter in AdBlock Plus /[/^a-ik-z\d=+](get|web)?_?spons?(or(ed|s))?_?(links?(auto)?)?(pots?)?(\W|_|$)(?!.*sigalert)/The solution was to rename sponsors folder on my server to something else. I choose to rename it to friends. It worked.

Wednesday, July 22, 2009

bash: check size of all folders in a given directory

To list all folders in a given path

this can be usedfor f in ./* ; do if [ -d "$f" ]; then echo $f ; fi done;

To list sizes of all folders in a given path

To check the size of all folders in the current directory the above can be modified to use du -sh "$f" command: for f in * ; do if [ -d "$f" ]; then du -sh "$f" ; fi done;The command goes in a loop on every file in a current dir and if a given file is a directory, than the size is calculated.

To sort by folder size

for f in * ; do if [ -d "$f" ]; then du -s "$f" ; fi done | sort -nNote that in du command there is no -h option!

Monday, July 20, 2009

Using rsync to transfer files and directories from server

A file from a server to the local computer

rsync -v -e ssh W@server:~/some_file.tar.gz ~/local_dir

A directory from a server to the local computer

rsync -r -v -e ssh W@server:~/some_directory ~/local_dir Other info is here.

Thursday, July 09, 2009

VirtualBox on CentOS 5.3: port forwarding

I have installed VirtualBox 2.2.4 on a CentOS 5.3. As a guest OS I installed Ubuntu, and on that Ubuntu I made a LAMP server. So, what I wanted was to redirect all html requrest on port 80 of CentOS (host) to port 80 of Ubuntu (guest). This is similar problem to the previously described on Windows XP host. The difference is that this time my host was CentOS 5.3, and VirtualBox was running as a normal user, not as a root.

The first thing to do was to make VirtualBox to forward the ports. I did it by executing the following commands, as a normal user under which VirtulBox was running. VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol" TCP
VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort" 80
VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort" 80
where "ubuntu-server" is the name of Ubuntu guest.

Than I wanted to start it in a headless modeVBoxHeadless -s ubuntu-server but I was getting the following error:Error: failed to start machine. Error message: NAT#0: configuration error: failed to set up redirection of 80 to 10.0.2.15:80. Probably a conflict with existing services or other rules (VERR_NAT_REDIR_SETUP).
Unknown error creating VM (VERR_NAT_REDIR_SETUP)
This error is due to the fact that in Linux systems only root can bind to ports below 1024, and not a normal user!. There are two possible solutions. First is to run VirtualBox as a root, the second is to redirect port 80 to port 8080 in the CentOS and the make VirtualBox to get connection from port 8080 of CentOS. I choose the second option. Therefore, I did as follows:

VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol" TCP
VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort" 80
VBoxManage setextradata "ubuntu-server" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort" 8080
The above makes VirtualBox to redirect trafic from 8080 of CentOS to 80 of guest ubuntu-server. Than as a root I used iptables to redirect port 80 to 8080 in CentOS iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080To check if iptables command was succesfull I used iptables -t nat -L commandiptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 8080

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
If OK save your iptables settings usingsudo /etc/init.d/iptables saveHope it will be useful. I also used VirtualBox 2.2, rather then VirtualBox 3.0, but I think the procedure will be similar.

The schematic illustration of what I did is below: