From: Allan Cornet
Date: Thu, 10 Jan 2008 14:47:43 +0000 (+0000)
Subject: add a third parameter to pca to disable graphical outputs
X-Git-Tag: 5.0-alpha-1~1615
X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=commitdiff_plain;h=3a6b58dd6170ffbbaea201bc7795089312d63d7b
add a third parameter to pca to disable graphical outputs
add test (cannot create .ref :( )
user request
---
diff --git a/scilab/modules/statistics/help/en_US/pca.xml b/scilab/modules/statistics/help/en_US/pca.xml
index a62c553..e515ba9 100644
--- a/scilab/modules/statistics/help/en_US/pca.xml
+++ b/scilab/modules/statistics/help/en_US/pca.xml
@@ -8,7 +8,7 @@
Principal components analysis
- [lambda,facpr,comprinc] = pca(x,N)
+ [lambda,facpr,comprinc] = pca(x,N,disable_graphics)
@@ -34,6 +34,13 @@
+
+
+ disable_graphics
+
+ : a boolean. %t to disable output graphics outputs. this parameter is optional.
+
+
lambda
@@ -96,6 +103,13 @@ maximal.
+
+
+
+
Carlos Klimann
diff --git a/scilab/modules/statistics/macros/pca.sci b/scilab/modules/statistics/macros/pca.sci
index fc948ae..ae95ac8 100644
--- a/scilab/modules/statistics/macros/pca.sci
+++ b/scilab/modules/statistics/macros/pca.sci
@@ -1,4 +1,4 @@
-function [lambda,facpr,comprinc]=pca(x,N)
+function [lambda,facpr,comprinc]=pca(varargin)
//
//This function performs several computations known as
//"principal component analysis". It includes drawing of
@@ -46,25 +46,45 @@ function [lambda,facpr,comprinc]=pca(x,N)
//author: carlos klimann
//
//date: 2002-02-05
-//commentary fixed 2003-19-24
-//
+//commentary fixed 2003-19-24 ??
+// update disable graphics output Allan CORNET 2008
+// request user
[lhs,rhs]=argn(0)
- if rhs==0 then
- error('pca must have at least one parameter'),
+
+ x = [];
+ N = [];
+ no_graphics = %f;
+
+ select rhs,
+ case 1 then
+ x = varargin(1);
+ N=[1 2];
+ case 2 then
+ x = varargin(1);
+ N = varargin(2);
+ case 3 then
+ x = varargin(1);
+ N = varargin(2);
+ no_graphics = varargin(3);
+ else
+ error(sprintf("%s: Wrong number of input argument(s).\n","pca"));
+ end
+
+ if ( type(no_graphics) <> 4 ) then
+ error(sprintf("%s: Wrong type for third input argument: Boolean expected.\n" ,"pca"));
end
+
if x==[] then
- lambda=%nan
- facpr=%nan
- comprinc=%nan,
- return,
+ lambda=%nan;
+ facpr=%nan;
+ comprinc=%nan;
+ return;
end
+
[rowx colx]=size(x)
- if rhs==2 then
- if size(N,'*')<>2 then error('Second parameter has bad dimensions'), end,
- if max(N)>colx then disp('Graph demand out of bounds'), return, end
- else
- N=[1 2],
- end
+ if size(N,'*')<>2 then error('Second parameter has bad dimensions'), end,
+ if max(N)>colx then disp('Graph demand out of bounds'), return, end
+
xbar=sum(x,'r')/rowx
y=x-ones(rowx,1)*xbar
std=(sum(y .^2,'r')) .^ .5
@@ -75,37 +95,45 @@ function [lambda,facpr,comprinc]=pca(x,N)
facpr=facpr(:,order)
comprinc=x*facpr
-
- w=winsid();if w==[] then w=0, else w=max(w)+1,end
- fig1=scf(w)
-
- rc= (ones(colx,1)* sqrt((lambda(N,1))')) .* facpr(:,order(N))
-
- rango=rank(V)
- ra=[1:rango]'
- if rango<=1 then return, end
- plot2d(-rc(ra,1),rc(ra,2),style=-10)
- legend('(r(c1,xj),r(c2,xj)')
- ax=gca();ax.x_location="middle";ax.y_location = "middle";
- blue=color('blue')
- for k=1:rango,
- xstring(rc(k,1),rc(k,2),'X'+string(k)),
- e=gce();e.foreground=blue;
- end
- title(' -Correlations Circle- ')
- fig2=scf(w+1);
- plot2d3([0;ra;rango+1]',[0; lambda(ra,2);0])
- plot2d(ra,lambda(ra,2),style=9)
- ax=gca(); ax.grid=[31 31]
- plot2d3([0;ra;rango+1]',[0; lambda(ra,2);0])
- plot2d(ra,lambda(ra,2),style=9)
- for k=1:rango,
- xstring(k,0,'l'+string(k)),
- e=gce();e.font_style=1
- end
- title(' -Eigenvalues (in percent)- ')
- ylabel('%')
-
+ if ~no_graphics then
+ w = winsid();
+ if (w == []) then
+ w = 0;
+ else
+ w = max(w)+1;
+ end
+ fig1 = scf(w);
+
+ rc = (ones(colx,1)* sqrt((lambda(N,1))')) .* facpr(:,order(N)) ;
+ rango = rank(V);
+ ra = [1:rango]';
+ if ( rango <= 1 ) then return, end
+ plot2d(-rc(ra,1),rc(ra,2),style=-10);
+ legend('(r(c1,xj),r(c2,xj)');
+ ax=gca();
+ ax.x_location="middle";
+ ax.y_location = "middle";
+ blue=color('blue')
+ for k=1:rango,
+ xstring(rc(k,1),rc(k,2),'X'+string(k));
+ e=gce();
+ e.foreground=blue;
+ end
+ title(' -Correlations Circle- ');
+ fig2 = scf(w+1);
+ plot2d3([0;ra;rango+1]',[0; lambda(ra,2);0]);
+ plot2d(ra,lambda(ra,2),style=9);
+ ax=gca();
+ ax.grid=[31 31];
+ plot2d3([0;ra;rango+1]',[0; lambda(ra,2);0])
+ plot2d(ra,lambda(ra,2),style=9)
+ for k=1:rango,
+ xstring(k,0,'l'+string(k)),
+ e=gce();e.font_style=1
+ end
+ title(' -Eigenvalues (in percent)- ')
+ ylabel('%')
+ end
endfunction
diff --git a/scilab/modules/statistics/tests/benchmarks/bench_pca.tst b/scilab/modules/statistics/tests/benchmarks/bench_pca.tst
new file mode 100644
index 0000000..5924bdd
--- /dev/null
+++ b/scilab/modules/statistics/tests/benchmarks/bench_pca.tst
@@ -0,0 +1,12 @@
+//==============================================================================
+// Benchmark for sva function
+// Copyright INRIA 2007
+//==============================================================================
+
+// <-- BENCH NB RUN : 100 -->
+stacksize(80000000);
+A = rand(1000,100,'n');
+
+// <-- BENCH START -->
+B=pca(A,[1 2],%t);
+// <-- BENCH END -->
diff --git a/scilab/modules/statistics/tests/unit_tests/pca.tst b/scilab/modules/statistics/tests/unit_tests/pca.tst
new file mode 100644
index 0000000..a19ba3c
--- /dev/null
+++ b/scilab/modules/statistics/tests/unit_tests/pca.tst
@@ -0,0 +1,20 @@
+// <-- TEST WITH GRAPHIC -->
+// =============================================================================
+// Tests for pca function
+// Scilab Team
+// Copyright INRIA
+// =============================================================================
+if execstr('pca()','errcatch')==0 then pause,end
+// =============================================================================
+a=rand(100,10,'n');
+[r1,r2,r3] = pca(a);
+// =============================================================================
+[ra,rb,rc] = pca(a, [1 2]);
+if ( or(r1 <> ra) | or(r2 <> rb) | or(r3 <> rc)) then pause,end
+// =============================================================================
+[ra,rb,rc] = pca(a, [1 2],%t);
+if ( or(r1 <> ra) | or(r2 <> rb) | or(r3 <> rc)) then pause,end
+// =============================================================================
+[ra,rb,rc] = pca(a, [1 2],%f);
+if ( or(r1 <> ra) | or(r2 <> rb) | or(r3 <> rc) ) then pause,end
+// =============================================================================