Statistics
| Revision:

root / tmp / org.txm.ca.core / src / org / txm / ca / core / functions / CA.java @ 743

History | View | Annotate | Download (22.3 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate: 2016-09-19 10:31:00 +0200 (Mon, 19 Sep 2016) $
25
// $LastChangedRevision: 3298 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.ca.core.functions;
29

    
30
import java.io.File;
31
import java.io.FileOutputStream;
32
import java.io.OutputStreamWriter;
33
import java.io.Writer;
34
import java.util.ArrayList;
35
import java.util.List;
36

    
37
import org.txm.ca.core.messages.CACoreMessages;
38
import org.txm.ca.core.preferences.CAPreferences;
39
import org.txm.ca.core.statsengine.r.functions.FactoMineRCA;
40
import org.txm.ca.core.statsengine.r.functions.ICA;
41
import org.txm.chartsengine.core.results.ChartResult;
42
import org.txm.core.messages.TXMCoreMessages;
43
import org.txm.core.results.Parameter;
44
import org.txm.core.results.TXMParameters;
45
import org.txm.lexicaltable.core.functions.LexicalTable;
46
import org.txm.statsengine.core.StatException;
47
import org.txm.utils.logger.Log;
48

    
49
/**
50
 * Correspondence Analysis of a lexical table.
51
 *
52
 * @author mdecorde
53
 * @author sjacquot
54
 * @author sloiseau
55
 *  
56
 */
57
public class CA extends ChartResult {
58

    
59
        /** The ca. */
60
        private ICA r_ca;
61

    
62
        float cex = 0.7f;
63

    
64
        String colfilter="cos2 0.0", rowfilter="cos2 0.0";
65

    
66
        /** The colnames. */
67
        String[] colnames = null;
68

    
69
        /** The coltabledata. */
70
        private ArrayList<List<Object>> coltabledata;
71

    
72
        /** The coltitles. */
73
        String[] coltitles; // of the colinfo table
74

    
75
        String pattern = "(cos2|contrib) (([0-9]+\\.[0-9]+)|([0-9]+))";
76

    
77
        /** The rownames. */
78
        String[] rownames = null;
79

    
80
        /** The rowtabledata. */
81
        private List<List<Object>> rowtabledata;
82

    
83
        /** The rowtitles. */
84
        String[] rowtitles; // of the rowinfo table
85

    
86
        /** The use Factomine R: change this only if you want to use the default R CA package. */
87
        boolean useFactoMineR = true;
88

    
89

    
90

    
91

    
92
        /**
93
         * First dimension.
94
         */
95
        @Parameter(key=CAPreferences.FIRST_DIMENSION, type=Parameter.RENDERING)
96
        protected int firstDimension = 1;
97

    
98
        /**
99
         * Second dimension.
100
         */
101
        @Parameter(key=CAPreferences.SECOND_DIMENSION, type=Parameter.RENDERING)
102
        protected int secondDimension = 2;
103

    
104
        /**
105
         * To show/hide individuals.
106
         */
107
        @Parameter(key=CAPreferences.SHOW_INDIVIDUALS, type=Parameter.RENDERING)
108
        protected boolean showIndividuals;
109

    
110
        /**
111
         * To show/hide variables.
112
         */
113
        @Parameter(key=CAPreferences.SHOW_VARIABLES, type=Parameter.RENDERING)
114
        protected boolean showVariables;
115

    
116
        /**
117
         * To show/hide the point shapes or only the labels.
118
         */
119
        @Parameter(key=CAPreferences.SHOW_POINT_SHAPES, type=Parameter.RENDERING)
120
        protected boolean showPointShapes;
121

    
122

    
123

    
124

    
125
        /**
126
         * Creates a new empty CA.
127
         *
128
         * @param lexicalTable the lexical table
129
         * @throws StatException stat exception if the structure can not be created in R statistics engine.
130
         */
131
        public CA(LexicalTable lexicalTable) throws StatException {
132
                super(lexicalTable);
133
        }
134

    
135
        @Override
136
        public void clean() {
137
                // TODO Auto-generated method stub
138
        }
139

    
140
        @Override
141
        protected boolean _compute() throws Exception {
142
                try {
143
                        if (this.useFactoMineR) {
144
                                this.r_ca = new FactoMineRCA(this.getLexicalTable().getData());
145
                        }
146
                        else {
147
                                this.r_ca = new org.txm.ca.core.statsengine.r.functions.CA(this.getLexicalTable().getData());
148
                        }
149

    
150
                        this.r_ca.compute();
151

    
152
                        // in case the lexical table changes
153
                        this.getColNames();
154
                        // in case the lexical table changes
155
                        this.getRowNames();
156

    
157
                        return true;
158
                } catch(Exception e) {
159
                        System.out.println("Error: CA not computed: "+e);
160
                        Log.printStackTrace(e);
161
                        return false;
162
                }
163

    
164
        }
165

    
166
        public void displayOptions(String col, String row, float cex) {
167
                this.cex = Math.abs(cex);
168
                if (col.matches(pattern)) {
169
                        this.colfilter = col;
170
                }
171
                else {
172
                        System.out.println("Columns filter malformed follow this pattern " + pattern);
173
                }
174

    
175
                if (row.matches(pattern)) {
176
                        this.rowfilter = row;
177
                }
178
                else {
179
                        System.out.println("Rows filter malformed follow this pattern " + pattern);
180
                }
181
        }
182

    
183
        /**
184
         * Export columns infos into a CSV FILE.
185
         *
186
         * @param file the CSV file
187
         * @param encoding the encoding
188
         * @param colseparator the column separator
189
         * @param textseparator the text separator
190
         */
191
        //FIXME: to move in an exporter extension
192
        @Deprecated
193
        public boolean exportColInfos(File file, String encoding, String colseparator, String textseparator) {
194
                try {
195
                        Writer writer = new OutputStreamWriter(new FileOutputStream(file), encoding);
196
                        String[] titles =  getColInfosTitles();
197
                        for (int i  = 0 ; i < titles.length ; i++) {
198
                                String title = titles[i];
199
                                if (i < titles.length -1)
200
                                        writer.write(textseparator+ title + textseparator+ colseparator);
201
                                else
202
                                        writer.write(textseparator+ title + textseparator+ colseparator);
203
                        }
204
                        writer.write("\n"); //$NON-NLS-1$
205
                        Object[] infos = getColInfos();
206
                        for (int i  = 0 ; i < infos.length ; i++) {
207
                                List<Object> list = (List<Object>) infos[i];
208
                                writer.write(textseparator + list.get(0) + textseparator + colseparator);
209
                                for (int j = 1 ; j < list.size() ; j++) {
210
                                        writer.write(list.get(j) + colseparator);
211
                                }
212
                                writer.write("\n"); //$NON-NLS-1$
213
                        }
214
                        writer.flush();
215
                        writer.close();
216
                } catch (Exception e) {
217
                        org.txm.utils.logger.Log.printStackTrace(e);
218
                        return false;
219
                }
220
                return true;
221
        }
222

    
223
        /**
224
         * Export row infos into a CSV file.
225
         *
226
         * @param file the CSV File
227
         * @param encoding the encoding
228
         * @param colseparator the column separator
229
         * @param textseparator the text separator
230
         */
231
        //FIXME: to move in an exporter extension
232
        @Deprecated
233
        public void exportRowInfos(File file, String encoding, String colseparator, String textseparator)
234
        {
235
                try {
236
                        Writer writer = new OutputStreamWriter(new FileOutputStream(file), encoding);
237
                        String[] titles =  getRowInfosTitles();
238
                        for (int i  = 0 ; i < titles.length ; i++) {
239
                                String title = titles[i];
240
                                if (i < titles.length -1) {
241
                                        writer.write(textseparator+ title + textseparator+ colseparator);
242
                                }
243
                                else {
244
                                        writer.write(textseparator+ title + textseparator+ colseparator);
245
                                }
246
                        }
247
                        writer.write("\n"); //$NON-NLS-1$
248
                        Object[] infos = getRowInfos();
249
                        for (int i  = 0 ; i < infos.length ; i++) {
250
                                List<Object> list = (List<Object>) infos[i];
251
                                writer.write(textseparator + list.get(0) + textseparator + colseparator);
252
                                for(int j = 1 ; j < list.size() ; j++) {
253
                                        writer.write(list.get(j) + colseparator);
254
                                }
255
                                writer.write("\n"); //$NON-NLS-1$
256
                        }
257
                        writer.flush();
258
                        writer.close();
259
                } catch (Exception e) {
260
                        org.txm.utils.logger.Log.printStackTrace(e);
261
                }
262
        }
263

    
264
        /**
265
         * Gets the CA R object.
266
         *
267
         * @return the ca
268
         */
269
        public org.txm.ca.core.statsengine.r.functions.ICA getCA() {
270
                return r_ca;
271
        }
272

    
273
        public float getCex() {
274
                return cex;
275
        }
276

    
277

    
278
        /**
279
         * 
280
         * @return the column filter -> only used by FactoMineR
281
         */
282
        public String getColFilter() {
283
                return colfilter;
284
        }
285

    
286
        /**
287
         * Gets the col all infos as Array of arrays.
288
         *
289
         * @return the col infos
290
         */
291
        public Object[] getColInfos() {
292

    
293
                if (coltabledata != null) {
294
                        return coltabledata.toArray();
295
                }
296

    
297
                double[] sv = null;
298
                double[] mass = null;
299
                double[][] colcoords = null;
300
                double[][] colcontribs = null;
301
                double[][] colcos2s = null;
302
                double[] coldist = null;
303

    
304
                try {
305
                        sv = getValeursPropres();
306
                        mass = r_ca.getColsMass();
307
                        colcoords = r_ca.getColsCoords();
308
                        colcontribs = r_ca.getColContrib();
309
                        colcos2s = r_ca.getColCos2();
310
                        coldist = r_ca.getColDist();
311
                        //add dist
312
                        getColNames();
313
                } catch (StatException e) {
314
                        org.txm.utils.logger.Log.printStackTrace(e);
315
                        return null;
316
                }
317

    
318
                coltabledata = new ArrayList<List<Object>>(colnames.length);
319

    
320
                double[] cos2 = new double[3];
321
                for (int i = 0; i < colnames.length; i++) {
322
                        List<Object> entry = new ArrayList<Object>(4);
323

    
324
                        cos2[0] = colcos2s[i][0];
325
                        cos2[1] = colcos2s[i][1];
326
                        cos2[2] = colcos2s[i][2];
327

    
328
                        entry.add(colnames[i]); // Factor
329
                        entry.add(""); //separator //$NON-NLS-1$
330
                        entry.add(new Double(cos2[0] + cos2[1])); // Q12
331
                        entry.add(new Double(cos2[0] + cos2[2])); // Q23
332
                        entry.add(new Double(cos2[2] + cos2[1])); // Q13
333
                        entry.add(new Double(mass[i] * 100.0)); // mass
334

    
335
                        entry.add(coldist[i]); // dist
336

    
337
                        for (int c = 0; c < 3; c++) { // 3 first axis
338
                                entry.add(""); //separator //$NON-NLS-1$
339
                                entry.add(colcontribs[i][c]); // contrib
340
                                entry.add(cos2[c]); // cos²
341
                        }
342
                        entry.add(""); //separator //$NON-NLS-1$
343
                        for (int c = 0; c < 3; c++) { // 3 first axis
344
                                entry.add(colcoords[i][c]); // coords
345
                        }
346
                        entry.add("");//separator //$NON-NLS-1$
347
                        coltabledata.add(entry);
348
                }
349

    
350
                return coltabledata.toArray();
351
        }
352

    
353
        /**
354
         * Get the column infos titles array.
355
         *
356
         * @return the col infos titles
357
         */
358
        public String[] getColInfosTitles() {
359
                if (coltitles == null) {
360
                        List<String> colnames = new ArrayList<String>();
361
                        colnames.add(TXMCoreMessages.common_cols); // entry
362
                        colnames.add(" "); //$NON-NLS-1$
363
                        colnames.add(CACoreMessages.common_q12); // Q12
364
                        colnames.add(CACoreMessages.common_q13); // Q13
365
                        colnames.add(CACoreMessages.common_q23); // Q23
366
                        colnames.add(CACoreMessages.common_mass); // Mass
367
                        colnames.add(CACoreMessages.common_dist); // Dist
368
                        for (int c = 0; c < 3; c++) { // seulement les 3 premiers axes
369
                                colnames.add(" "); // separator //$NON-NLS-1$
370
                                colnames.add(CACoreMessages.common_cont+ (c + 1)); // ContribN
371
                                colnames.add(CACoreMessages.common_cos2+ (c + 1)); // Cos²N
372
                        }
373
                        colnames.add(" "); // separator //$NON-NLS-1$
374
                        colnames.add(CACoreMessages.common_c1); // coord axis 1
375
                        colnames.add(CACoreMessages.common_c2); // coord axis 2
376
                        colnames.add(CACoreMessages.common_c3); // coord axis 3
377
                        colnames.add(""); //$NON-NLS-1$
378

    
379
                        coltitles = colnames.toArray(new String[] {});
380
                }
381
                return coltitles;
382
        }
383

    
384
        /**
385
         * Gets the column names as array.
386
         *
387
         * @return the col names
388
         * @throws StatException the stat exception
389
         */
390
        public String[] getColNames() throws StatException {
391
                if (colnames == null) {
392
                        colnames = getLexicalTable().getColNames().asStringsArray();
393
                }
394
                return colnames;
395
        }
396

    
397
        /**
398
         * Gets the columns coordinates as array.
399
         *
400
         * @return the columns coordinates
401
         */
402
        public double[][] getColsCoords() {
403
                return this.r_ca.getColsCoords();
404
        }
405

    
406
        /**
407
         * Gets the columns distances as array.
408
         *
409
         * @return the cols dist
410
         * @throws StatException the stat exception
411
         */
412
        public double[] getColsDist() throws StatException {
413
                return this.r_ca.getColDist();
414
        }
415

    
416
        /**
417
         * Get the columns inertia as array.
418
         *
419
         * @return the cols inertia
420
         * @throws StatException the stat exception
421
         */
422
        public double[] getColsInertia() throws StatException {
423
                return this.r_ca.getColsInertia();
424
        }
425

    
426
        /**
427
         * Gets the cols mass.
428
         *
429
         * @return the cols mass
430
         * @throws StatException the stat exception
431
         */
432
        public double[] getColsMass() throws StatException {
433
                return this.r_ca.getColsMass();
434
        }
435

    
436
        /**
437
         * Gets the number of columns.
438
         * @return the number of columns
439
         */
440
        public int getColumnsCount()        {
441
                return this.r_ca.getColumnsCount();
442
        }
443

    
444

    
445
        @Override
446
        public String[] getExportTXTExtensions() {
447
                return new String[]{"*.txt"};
448
        }
449

    
450
        /**
451
         * Gets the first dimension.
452
         * @return the first dimension
453
         */
454
        public int getFirstDimension() {
455
                return firstDimension;
456
        }
457

    
458
        /**
459
         * Gets the lexical table used to compute the CA.
460
         *
461
         * @return the lexical table
462
         */
463
        public LexicalTable getLexicalTable() {
464
                return (LexicalTable) this.parent;
465
        }
466

    
467
        /**
468
         * Get the row contributions as array.
469
         *
470
         * @return the row contrib
471
         * @throws StatException 
472
         */
473
        public double[][] getRowContrib() throws StatException {
474
                return r_ca.getRowContrib();
475
        }
476

    
477
        /**
478
         * Get the row cos2 as array.
479
         *
480
         * @return the row cos2
481
         * @throws StatException 
482
         */
483
        public double[][] getRowCos2() throws StatException {
484
                return r_ca.getRowCos2();
485
        }
486

    
487
        public String getRowFilter() {
488
                return rowfilter;
489
        }
490

    
491
        /**
492
         * Get the row informations as array of arrays.
493
         *
494
         * @return the row infos
495
         */
496
        public Object[] getRowInfos() {
497

    
498
                if (rowtabledata != null)
499
                        return rowtabledata.toArray();
500

    
501
                double[] mass = null;
502
                double[] dist = null;
503
                double[][] rowcoords = null;
504
                double[][] rowcontribs = null;
505
                double[][] rowcos2s = null;
506

    
507
                try {
508
                        mass = r_ca.getRowsMass();
509
                        rowcoords = r_ca.getRowsCoords();
510
                        rowcontribs = r_ca.getRowContrib();
511
                        rowcos2s = r_ca.getRowCos2();
512
                        dist = r_ca.getRowDist();
513
                        getRowNames();
514
                } catch (StatException e) {
515
                        org.txm.utils.logger.Log.printStackTrace(e);
516
                        return null;
517
                }
518

    
519
                rowtabledata = new ArrayList<List<Object>>(rownames.length);
520

    
521
                double[] cos2 = new double[3];
522
                for (int i = 0; i < rownames.length; i++) {
523
                        double distt = dist[i];
524

    
525
                        cos2[0] = rowcos2s[i][0];
526
                        cos2[1] = rowcos2s[i][1];
527
                        cos2[2] = rowcos2s[i][2];
528

    
529
                        List<Object> entry = new ArrayList<Object>(4);
530
                        entry.add(rownames[i]); // Factor
531
                        entry.add(""); // separator //$NON-NLS-1$
532
                        entry.add(new Double(cos2[0] + cos2[1])); // Q12
533
                        entry.add(new Double(cos2[0] + cos2[2])); // Q23
534
                        entry.add(new Double(cos2[2] + cos2[1])); // Q13
535

    
536
                        entry.add(new Double(mass[i]) * 100.0); // mass
537

    
538
                        entry.add(distt); // dist
539
                        for (int c = 0; c < 3; c++) { // 3 first axis
540
                                entry.add(""); // separator //$NON-NLS-1$
541
                                entry.add(rowcontribs[i][c] * 100.0); // contrib
542
                                entry.add(cos2[c]); // cos²
543
                        }
544
                        entry.add(""); //separator //$NON-NLS-1$
545
                        for (int c = 0; c < 3; c++) { // 3 first axis
546
                                entry.add(rowcoords[i][c]); // coords
547
                        }
548
                        entry.add("");// separator //$NON-NLS-1$
549
                        rowtabledata.add(entry);
550
                }
551

    
552
                return rowtabledata.toArray();
553
        }
554

    
555
        /**
556
         * Get the row infos titles as array.
557
         *
558
         * @return the row infos titles
559
         */
560
        public String[] getRowInfosTitles() {
561
                if (rowtitles == null) {
562
                        List<String> colnames = new ArrayList<String>();
563
                        colnames.add(TXMCoreMessages.common_rows); // entry
564
                        colnames.add(" "); //$NON-NLS-1$
565
                        colnames.add(CACoreMessages.common_q12); // Q12
566
                        colnames.add(CACoreMessages.common_q13); // Q13
567
                        colnames.add(CACoreMessages.common_q23); // Q23
568
                        colnames.add(CACoreMessages.common_mass); // Mass
569
                        colnames.add(CACoreMessages.common_dist); // Dist
570
                        for (int c = 0; c < 3; c++) { // seulement les 3 premiers axes
571
                                colnames.add(" "); // separator //$NON-NLS-1$
572
                                colnames.add(CACoreMessages.common_cont+ (c + 1)); // ContribN
573
                                colnames.add(CACoreMessages.common_cos2+ (c + 1)); // Cos²N
574
                        }
575
                        colnames.add(" "); // separator //$NON-NLS-1$
576
                        colnames.add(CACoreMessages.common_c1); // coord axis 1
577
                        colnames.add(CACoreMessages.common_c2); // coord axis 2
578
                        colnames.add(CACoreMessages.common_c3); // coord axis 3
579
                        colnames.add(""); //$NON-NLS-1$
580

    
581
                        rowtitles = colnames.toArray(new String[] {});
582
                }
583
                return rowtitles;
584
        }
585

    
586
        /**
587
         * Get the row names as array.
588
         *
589
         * @return the row names
590
         * @throws StatException the stat exception
591
         */
592
        public String[] getRowNames() throws StatException {
593
                if (rownames == null) {
594
                        rownames = getLexicalTable().getRowNames().asStringsArray();
595
                }
596
                return rownames;
597
        }
598

    
599
        /**
600
         * Get the rows coordinates as array of arrays
601
         *
602
         * @return the rows coords
603
         */
604
        public double[][] getRowsCoords() {
605
                return this.r_ca.getRowsCoords();
606
        }
607

    
608
        /**
609
         * Gets the number of rows.
610
         * @return the number of rows
611
         */
612
        public int getRowsCount()        {
613
                return this.r_ca.getRowsCount();
614
        }
615

    
616
        /**
617
         * Get the rows distances as array.
618
         *
619
         * @return the rows dist
620
         * @throws StatException the stat exception
621
         */
622
        public double[] getRowsDist() throws StatException {
623
                return this.r_ca.getRowDist();
624
        }
625

    
626
        /**
627
         * Get the rows inertia as array.
628
         *
629
         * @return the rows inertia
630
         * @throws StatException the stat exception
631
         */
632
        public double[] getRowsInertia() throws StatException {
633
                return this.r_ca.getRowsInertia();
634
        }
635

    
636
        /**
637
         * Get the rows mass as array.
638
         *
639
         * @return the rows mass
640
         * @throws StatException the stat exception
641
         */
642
        public double[] getRowsMass() throws StatException {
643
                return this.r_ca.getRowsMass();
644
        }
645

    
646
        //        /**
647
        //         * step 1.
648
        //         *
649
        //         * @throws StatException the stat exception
650
        //         * @throws CqiClientException the cqi client exception
651
        //         */
652
        //        public boolean stepLexicalTable() throws Exception
653
        //        {
654
        //                table = LexicalTableFactory.getLexicalTable(partition, pAnalysisProperty, fmin);
655
        //                if (table == null) {
656
        //                        System.out.println("Error: Lexical table is null.");
657
        //                        return false;
658
        //                }
659
        //                partition.addResult(table);
660
        //                table.addResult(this);
661
        //                
662
        //                return true;
663
        //        }
664

    
665
        //        /**
666
        //         * step 2.
667
        //         */
668
        //        public boolean stepSortTableLexical()
669
        //        {
670
        //                try {
671
        //                        table.getData().filter(vmax, fmin);
672
        //                } catch (Exception e) {
673
        //                        System.out.println("Error: "+e.getLocalizedMessage());
674
        //                        org.txm.utils.logger.Log.printStackTrace(e);
675
        //                        return false;
676
        //                }
677
        //                return true;
678
        //        }
679

    
680
        /**
681
         * Gets the second dimension.
682
         * @return the second dimension
683
         */
684
        public int getSecondDimension() {
685
                return secondDimension;
686
        }
687

    
688
        @Override
689
        public String getSimpleName() {
690
                return getParent().getSimpleName() + " (" + this.firstDimension + "," + this.secondDimension + ")";
691
        }
692

    
693
        @Override
694
        public String getDetails() {
695
                return "FMin = " + this.getLexicalTable().getFMin();
696
        }
697

    
698
        @Override
699
        public String getName() {
700
                return this.getParent().getSimpleName() + ": " + this.getSimpleName();
701
        }
702

    
703

    
704

    
705
        /**
706
         * Get the singular values as array.
707
         *
708
         * @return the singular values
709
         * @throws StatException the stat exception
710
         */
711
        public double[] getSingularValues() throws StatException {
712
                return r_ca.getSingularValues();
713
        }
714

    
715
        /**
716
         * Get the singular values infos as List of Lists
717
         *
718
         * @return the singular values infos
719
         */
720
        public List<List<Object>> getSingularValuesInfos()
721
        {
722
                List<List<Object>> svtabledata = new ArrayList<List<Object>>();
723

    
724
                double[] sv = null;
725
                try {
726
                        sv = getValeursPropres();
727
                } catch (StatException e) {
728
                        org.txm.utils.logger.Log.printStackTrace(e);
729
                        return svtabledata;
730
                }
731

    
732
                double sigmaVp;
733
                try {
734
                        sigmaVp = getValeursPropresSum();
735
                } catch (StatException e) {
736
                        org.txm.utils.logger.Log.printStackTrace(e);
737
                        return svtabledata;
738
                }
739

    
740
                double cumul = 0;
741
                double percent = 0;
742

    
743
                for (int i = 0; i < sv.length; i++) {
744
                        percent = 100 * sv[i] / sigmaVp;
745
                        cumul += percent;
746

    
747
                        List<Object> entry = new ArrayList<Object>(4);
748
                        entry.add(new Integer(i + 1));        // Factor
749
                        entry.add(new Double(sv[i]));        // Singular value
750
                        entry.add(percent);                                // Percent
751
                        entry.add(cumul);                                // Cumul
752
                        entry.add(percent);                                // For bar plot direct drawing in the table
753

    
754
                        entry.add(""); // add an empty column to fix a display bug in table in Linux
755

    
756
                        svtabledata.add(entry);
757
                }
758

    
759
                return svtabledata;
760
        }
761

    
762
        /**
763
         * Gets the symbol.
764
         *
765
         * @return the R symbol of the CA result
766
         */
767
        public String getSymbol() {
768
                return r_ca.getSymbol();
769
        }
770

    
771
        /**
772
         * Gets the sqrt of singular values as array.
773
         *
774
         * @return the valeurs propres
775
         * @throws StatException the stat exception
776
         */
777
        public double[] getValeursPropres() throws StatException {
778
                return r_ca.getEigenvalues();
779
        }
780

    
781
        /**
782
         * Gets the 'valeurs propres' sum.
783
         *
784
         * @return the valeurs propres sum
785
         * @throws StatException the stat exception
786
         */
787
        public double getValeursPropresSum() throws StatException {
788
                double sum = 0;
789
                for (double d : getValeursPropres()) {
790
                        sum += d;
791
                }
792
                return sum;
793
        }
794

    
795

    
796
        /**
797
         * Exports CA R data to a file
798
         *
799
         * @param outfile the outfile
800
         * @param encoding the encoding
801
         * @return true, if successful
802
         */
803
        //FIXME: to move in an exporter extension
804
        @Deprecated
805
        public boolean toTxt(File outfile, String encoding) {
806
                return toTxt(outfile, encoding, "\t", ""); //$NON-NLS-1$ //$NON-NLS-2$
807
        }
808

    
809
        /**
810
         * Exports CA R data to a file
811
         *
812
         * @param outfile the outfile
813
         * @param encoding the encoding
814
         * @param colseparator the colseparator
815
         * @param txtseparator the txtseparator
816
         * @return true, if successful
817
         */
818
        //FIXME: to move in an exporter extension
819
        @Deprecated
820
        public boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) {
821
                acquireSemaphore();
822
                boolean rez = r_ca.toTxt(outfile, encoding, colseparator, txtseparator);
823
                releaseSemaphore();
824

    
825
                return rez;
826
        }
827

    
828

    
829
        /**
830
         * Checks if the CA uses the FactoMineR R package.
831
         * @return <code>true</code> if the CA uses the FactoMineR R package otherwise <code>false</code>
832
         */
833
        public boolean useFactoMineR() {
834
                return this.useFactoMineR;
835
        }
836

    
837
        @Override
838
        public boolean canCompute() {
839
                return 
840
                                this.parent != null
841
                                && this.getLexicalTable().getNColumns() > 3
842
                                && this.firstDimension > 0
843
                                && this.secondDimension > 0
844
                                && this.firstDimension != this.secondDimension
845
                                ;
846
        }
847

    
848
        @Override
849
        public boolean saveParameters() {
850
                // nothing to do
851
                return true;
852
        }
853

    
854
        @Override
855
        public boolean setParameters(TXMParameters parameters) {
856
                // TODO Auto-generated method stub
857
                System.err.println("CA.setParameters(): not yet implemented.");
858
                return true;
859
        }
860

    
861
        @Override
862
        public boolean loadParameters() {
863
                // nothing to do
864
                return true;
865
        }
866

    
867
        /**
868
         * @return the showIndividuals
869
         */
870
        public boolean isShowIndividuals() {
871
                return showIndividuals;
872
        }
873

    
874
        /**
875
         * @param showIndividuals the showIndividuals to set
876
         */
877
        public void setShowIndividuals(boolean showIndividuals) {
878
                this.showIndividuals = showIndividuals;
879
        }
880

    
881
        /**
882
         * @return the showVariables
883
         */
884
        public boolean isShowVariables() {
885
                return showVariables;
886
        }
887

    
888
        /**
889
         * @param showVariables the showVariables to set
890
         */
891
        public void setShowVariables(boolean showVariables) {
892
                this.showVariables = showVariables;
893
        }
894

    
895
        /**
896
         * @return the showPointShapes
897
         */
898
        public boolean isShowPointShapes() {
899
                return showPointShapes;
900
        }
901

    
902
        /**
903
         * @param showPointShapes the showPointShapes to set
904
         */
905
        public void setShowPointShapes(boolean showPointShapes) {
906
                this.showPointShapes = showPointShapes;
907
        }
908

    
909
        /**
910
         * @param firstDimension the firstDimension to set
911
         */
912
        public void setFirstDimension(int firstDimension) {
913
                this.firstDimension = firstDimension;
914
        }
915

    
916
        /**
917
         * @param secondDimension the secondDimension to set
918
         */
919
        public void setSecondDimension(int secondDimension) {
920
                this.secondDimension = secondDimension;
921
        }
922
}