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: