source: src/main/java/weka/experiment/ResultMatrixGnuPlot.java @ 25

Last change on this file since 25 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 11.3 KB
RevLine 
[4]1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ResultMatrixGnuPlot.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.experiment;
24
25import weka.core.RevisionUtils;
26import weka.core.Utils;
27import weka.core.Version;
28
29/**
30 <!-- globalinfo-start -->
31 * Generates output for a data and script file for GnuPlot.
32 * <p/>
33 <!-- globalinfo-end -->
34 *
35 <!-- options-start -->
36 * Valid options are: <p/>
37 *
38 * <pre> -mean-prec &lt;int&gt;
39 *  The number of decimals after the decimal point for the mean.
40 *  (default: 2)</pre>
41 *
42 * <pre> -stddev-prec &lt;int&gt;
43 *  The number of decimals after the decimal point for the mean.
44 *  (default: 2)</pre>
45 *
46 * <pre> -col-name-width &lt;int&gt;
47 *  The maximum width for the column names (0 = optimal).
48 *  (default: 50)</pre>
49 *
50 * <pre> -row-name-width &lt;int&gt;
51 *  The maximum width for the row names (0 = optimal).
52 *  (default: 50)</pre>
53 *
54 * <pre> -mean-width &lt;int&gt;
55 *  The width of the mean (0 = optimal).
56 *  (default: 0)</pre>
57 *
58 * <pre> -stddev-width &lt;int&gt;
59 *  The width of the standard deviation (0 = optimal).
60 *  (default: 0)</pre>
61 *
62 * <pre> -sig-width &lt;int&gt;
63 *  The width of the significance indicator (0 = optimal).
64 *  (default: 0)</pre>
65 *
66 * <pre> -count-width &lt;int&gt;
67 *  The width of the counts (0 = optimal).
68 *  (default: 0)</pre>
69 *
70 * <pre> -show-stddev
71 *  Whether to display the standard deviation column.
72 *  (default: no)</pre>
73 *
74 * <pre> -show-avg
75 *  Whether to show the row with averages.
76 *  (default: no)</pre>
77 *
78 * <pre> -remove-filter
79 *  Whether to remove the classname package prefixes from the
80 *  filter names in datasets.
81 *  (default: no)</pre>
82 *
83 * <pre> -print-col-names
84 *  Whether to output column names or just numbers representing them.
85 *  (default: no)</pre>
86 *
87 * <pre> -print-row-names
88 *  Whether to output row names or just numbers representing them.
89 *  (default: no)</pre>
90 *
91 * <pre> -enum-col-names
92 *  Whether to enumerate the column names (prefixing them with
93 *  '(x)', with 'x' being the index).
94 *  (default: no)</pre>
95 *
96 * <pre> -enum-row-names
97 *  Whether to enumerate the row names (prefixing them with
98 *  '(x)', with 'x' being the index).
99 *  (default: no)</pre>
100 *
101 <!-- options-end -->
102 *
103 * @author FracPete (fracpete at waikato dot ac dot nz)
104 * @version $Revision: 5346 $
105 */
106public class ResultMatrixGnuPlot
107  extends ResultMatrix {
108
109  /** for serialization. */
110  private static final long serialVersionUID = -234648254944790097L;
111 
112  /**
113   * initializes the matrix as 1x1 matrix.
114   */
115  public ResultMatrixGnuPlot() {
116    this(1, 1);
117  }
118
119  /**
120   * initializes the matrix with the given dimensions.
121   *
122   * @param cols        the number of columns
123   * @param rows        the number of rows
124   */
125  public ResultMatrixGnuPlot(int cols, int rows) {
126    super(cols, rows);
127  }
128
129  /**
130   * initializes the matrix with the values from the given matrix.
131   *
132   * @param matrix      the matrix to get the values from
133   */
134  public ResultMatrixGnuPlot(ResultMatrix matrix) {
135    super(matrix);
136  }
137 
138  /**
139   * Returns a string describing the matrix.
140   *
141   * @return            a description suitable for
142   *                    displaying in the experimenter gui
143   */
144  public String globalInfo() {
145    return "Generates output for a data and script file for GnuPlot.";
146  }
147
148  /**
149   * returns the name of the output format.
150   *
151   * @return            the display name
152   */
153  public String getDisplayName() {
154    return "GNUPlot";
155  }
156
157  /**
158   * removes the stored data but retains the dimensions of the matrix.
159   */
160  public void clear() {
161    super.clear();
162    LEFT_PARENTHESES = "";
163    RIGHT_PARENTHESES = "";
164  }
165
166  /**
167   * returns the default width for the row names.
168   *
169   * @return            the width
170   */
171  public int getDefaultRowNameWidth() {
172    return 50;
173  }
174
175  /**
176   * returns the default width for the column names.
177   *
178   * @return            the width
179   */
180  public int getDefaultColNameWidth() {
181    return 50;
182  }
183
184  /**
185   * returns the default of whether column names are prefixed with the index.
186   *
187   * @return            true if the names are prefixed
188   */
189  public boolean getDefaultEnumerateColNames() {
190    return false;
191  }
192
193  /**
194   * returns the default of whether row names are prefixed with the index.
195   *
196   * @return            true if the names are prefixed
197   */
198  public boolean getDefaultEnumerateRowNames() {
199    return false;
200  }
201 
202  /**
203   * returns the header of the matrix as a string.
204   *
205   * @return            the header
206   * @see               #m_HeaderKeys
207   * @see               #m_HeaderValues
208   */
209  public String toStringHeader() {
210    return new ResultMatrixPlainText(this).toStringHeader();
211  }
212
213  /**
214   * returns the matrix in CSV format.
215   *
216   * @return            the matrix
217   */
218  public String toStringMatrix() {
219    StringBuffer        result;
220    String[][]          cells;
221    int                 i;
222    int                 n;
223    String              line;
224    String              title;
225    String              generated;
226
227    result = new StringBuffer();
228    cells  = toArray();
229
230    // generation comment
231    generated = "# generated by WEKA " + Version.VERSION + "\n";
232
233    // data
234    result.append("\n");
235    result.append("##################\n");
236    result.append("# file: plot.dat #\n");
237    result.append("##################\n");
238    result.append(generated);
239    result.append("# contains the data for the plot\n");
240    // key for x-axis
241    result.append("\n");
242    result.append("# key for the x-axis\n");
243    for (i = 1; i < cells.length - 1; i++)
244      result.append("# " + i + " - " + cells[i][0] + "\n");
245    // the data itself
246    result.append("\n");
247    result.append("# data for the plot\n");
248    for (i = 1; i < cells.length - 1; i++) {
249      result.append(Integer.toString(i));
250      for (n = 1; n < cells[i].length; n++) {
251        if (isSignificance(n))
252          continue;
253        result.append(" ");
254        result.append(Utils.quote(cells[i][n]));
255      }
256      result.append("\n");
257    }
258    result.append("#######\n");
259    result.append("# end #\n");
260    result.append("#######\n");
261
262    // script
263    result.append("\n");
264    result.append("##################\n");
265    result.append("# file: plot.scr #\n");
266    result.append("##################\n");
267    result.append(generated);
268    result.append("# script to plot the data\n");
269    result.append("\n");
270    result.append("# display it in a window:\n");
271    result.append("set terminal x11\n");
272    result.append("set output\n");
273    result.append("\n");
274    result.append("# to display all data rows:\n");
275    result.append("set xrange [0:" + ((cells.length - 2) + 1) + "]\n");
276    result.append("\n");
277    result.append("# axis labels, e.g.:\n");
278    result.append("#set xlabel \"Datasets\"\n");
279    result.append("#set ylabel \"Accuracy in %\"\n");
280    result.append("\n");
281    result.append("# the plot commands\n");
282    n = 1;
283    i = 0;
284    while (i < cells[0].length - 1) {
285      i++;
286
287      if (isSignificance(i))
288        continue;
289
290      n++;
291     
292      // plot
293      if (i == 1)
294        line = "plot";
295      else
296        line = "replot";
297      line += " \"plot.dat\"";
298
299      // title
300      title = "title \"" + cells[0][i] + "\"";
301     
302      // columns
303      line += " using 1:" + n;
304      if (getShowStdDev()) {
305        n++;
306        i++;
307        // errorbars
308        line += ":" + n;
309      }
310     
311      // options
312      line += " with";
313      if (getShowStdDev())
314        line += " yerrorbars";
315      else
316        line += " lines";
317      line += " " + title;
318     
319      result.append(line + "\n");
320    }
321    result.append("\n");
322    result.append("# generate ps:\n");
323    result.append("#set terminal postscript\n");
324    result.append("#set output \"plot.ps\"\n");
325    result.append("#replot\n");
326    result.append("\n");
327    result.append("# generate png:\n");
328    result.append("#set terminal png size 800,600\n");
329    result.append("#set output \"plot.png\"\n");
330    result.append("#replot\n");
331    result.append("\n");
332    result.append("# wait for user to hit <Return>\n");
333    result.append("pause -1\n");
334    result.append("#######\n");
335    result.append("# end #\n");
336    result.append("#######\n");
337   
338    return result.toString();
339  }
340
341  /**
342   * returns returns a key for all the col names, for better readability if
343   * the names got cut off.
344   *
345   * @return            the key
346   */
347  public String toStringKey() {
348    return new ResultMatrixPlainText(this).toStringKey();
349  }
350
351  /**
352   * returns the summary as string.
353   *
354   * @return            the summary
355   */
356  public String toStringSummary() {
357    return new ResultMatrixPlainText(this).toStringSummary();
358  }
359
360  /**
361   * returns the ranking in a string representation.
362   *
363   * @return            the ranking
364   */
365  public String toStringRanking() {
366    return new ResultMatrixPlainText(this).toStringRanking();
367  }
368 
369  /**
370   * Returns the revision string.
371   *
372   * @return            the revision
373   */
374  public String getRevision() {
375    return RevisionUtils.extract("$Revision: 5346 $");
376  }
377
378  /**
379   * for testing only.
380   *
381   * @param args        ignored
382   */
383  public static void main(String[] args) {
384    ResultMatrix        matrix;
385    int                 i;
386    int                 n;
387   
388    matrix = new ResultMatrixGnuPlot(3, 3);
389
390    // set header
391    matrix.addHeader("header1", "value1");
392    matrix.addHeader("header2", "value2");
393    matrix.addHeader("header2", "value3");
394   
395    // set values
396    for (i = 0; i < matrix.getRowCount(); i++) {
397      for (n = 0; n < matrix.getColCount(); n++) {
398        matrix.setMean(n, i, (i+1)*n);
399        matrix.setStdDev(n, i, ((double) (i+1)*n) / 100);
400        if (i == n) {
401          if (i % 2 == 1)
402            matrix.setSignificance(n, i, SIGNIFICANCE_WIN);
403          else
404            matrix.setSignificance(n, i, SIGNIFICANCE_LOSS);
405        }
406      }
407    }
408
409    System.out.println("\n\n--> " + matrix.getDisplayName());
410   
411    System.out.println("\n1. complete\n");
412    System.out.println(matrix.toStringHeader() + "\n");
413    System.out.println(matrix.toStringMatrix() + "\n");
414    System.out.println(matrix.toStringKey());
415   
416    System.out.println("\n2. complete with std deviations\n");
417    matrix.setShowStdDev(true);
418    System.out.println(matrix.toStringMatrix());
419   
420    System.out.println("\n3. cols numbered\n");
421    matrix.setPrintColNames(false);
422    System.out.println(matrix.toStringMatrix());
423   
424    System.out.println("\n4. second col missing\n");
425    matrix.setColHidden(1, true);
426    System.out.println(matrix.toStringMatrix());
427   
428    System.out.println("\n5. last row missing, rows numbered too\n");
429    matrix.setRowHidden(2, true);
430    matrix.setPrintRowNames(false);
431    System.out.println(matrix.toStringMatrix());
432   
433    System.out.println("\n6. mean prec to 3\n");
434    matrix.setMeanPrec(3);
435    matrix.setPrintRowNames(false);
436    System.out.println(matrix.toStringMatrix());
437  }
438}
Note: See TracBrowser for help on using the repository browser.