~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/experiment/ResultMatrixHTML.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * ResultMatrixHTML.java
 
19
 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.experiment;
 
24
 
 
25
import weka.core.Utils;
 
26
 
 
27
/**
 
28
 * This matrix is a container for the datasets and classifier setups and 
 
29
 * their statistics. It outputs the matrix in HTML.
 
30
 *
 
31
 *
 
32
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
33
 * @version $Revision: 1.3 $
 
34
 */
 
35
public class ResultMatrixHTML
 
36
  extends ResultMatrix {
 
37
 
 
38
  /** for serialization */
 
39
  private static final long serialVersionUID = 6672380422544799990L;
 
40
 
 
41
  /**
 
42
   * initializes the matrix as 1x1 matrix
 
43
   */
 
44
  public ResultMatrixHTML() {
 
45
    this(1, 1);
 
46
  }
 
47
 
 
48
  /**
 
49
   * initializes the matrix with the given dimensions
 
50
   */
 
51
  public ResultMatrixHTML(int cols, int rows) {
 
52
    super(cols, rows);
 
53
  }
 
54
 
 
55
  /**
 
56
   * initializes the matrix with the values from the given matrix
 
57
   * @param matrix      the matrix to get the values from
 
58
   */
 
59
  public ResultMatrixHTML(ResultMatrix matrix) {
 
60
    super(matrix);
 
61
  }
 
62
 
 
63
  /**
 
64
   * returns the name of the output format
 
65
   */
 
66
  public String getDisplayName() {
 
67
    return "HTML";
 
68
  }
 
69
 
 
70
  /**
 
71
   * removes the stored data but retains the dimensions of the matrix
 
72
   */
 
73
  public void clear() {
 
74
    super.clear();
 
75
    setRowNameWidth(25);
 
76
    setPrintColNames(false);
 
77
    setEnumerateColNames(true);
 
78
  }
 
79
  
 
80
  /**
 
81
   * returns the header of the matrix as a string
 
82
   * @see #m_HeaderKeys
 
83
   * @see #m_HeaderValues
 
84
   */
 
85
  public String toStringHeader() {
 
86
    return new ResultMatrixPlainText(this).toStringHeader();
 
87
  }
 
88
 
 
89
  /**
 
90
   * returns the matrix in an HTML table
 
91
   */
 
92
  public String toStringMatrix() {
 
93
    StringBuffer        result;
 
94
    String[][]          cells;
 
95
    int                 i;
 
96
    int                 n;
 
97
    int                 cols;
 
98
 
 
99
    result = new StringBuffer();
 
100
    cells  = toArray();
 
101
 
 
102
    result.append("<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n");
 
103
    
 
104
    // headings
 
105
    result.append("   <tr>");
 
106
    for (n = 0; n < cells[0].length; n++) {
 
107
      if (isRowName(n)) {
 
108
        result.append("<td><b>" + cells[0][n] + "</b></td>");
 
109
      }
 
110
      else if (isMean(n)) {
 
111
        if (n == 1)
 
112
          cols = 1;
 
113
        else
 
114
          cols = 2;
 
115
        if (getShowStdDev())
 
116
          cols++;
 
117
        result.append("<td align=\"center\" colspan=\"" + cols + "\">");
 
118
        result.append("<b>" + cells[0][n] + "</b>");
 
119
        result.append("</td>");
 
120
      }
 
121
    }
 
122
    result.append("</tr>\n");
 
123
      
 
124
    // data
 
125
    for (i = 1; i < cells.length; i++) {
 
126
      result.append("   <tr>");
 
127
      for (n = 0; n < cells[i].length; n++) {
 
128
        if (isRowName(n))
 
129
          result.append("<td>");
 
130
        else if (isMean(n) || isStdDev(n))
 
131
          result.append("<td align=\"right\">");
 
132
        else if (isSignificance(n))
 
133
          result.append("<td align=\"center\">");
 
134
        else
 
135
          result.append("<td>");
 
136
        
 
137
        // content
 
138
        if (cells[i][n].trim().equals(""))
 
139
          result.append("&nbsp;");
 
140
        else if (isStdDev(n))
 
141
          result.append("&plusmn;&nbsp;" + cells[i][n]);
 
142
        else
 
143
          result.append(cells[i][n]);
 
144
        
 
145
        result.append("</td>");
 
146
      }
 
147
      result.append("</tr>\n");
 
148
    }
 
149
    result.append("</table>\n");
 
150
    
 
151
    return result.toString();
 
152
  }
 
153
 
 
154
  /**
 
155
   * returns returns a key for all the col names, for better readability if
 
156
   * the names got cut off
 
157
   */
 
158
  public String toStringKey() {
 
159
    String          result;
 
160
    int             i;
 
161
 
 
162
    result =   "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n" 
 
163
             + "   <tr><td colspan=\"2\"><b>Key</b></td></tr>\n";
 
164
    for (i = 0; i < getColCount(); i++) {
 
165
      if (getColHidden(i))
 
166
        continue;
 
167
 
 
168
      result +=   "   <tr>"
 
169
                + "<td><b>(" + (i+1) + ")</b></td>"
 
170
                + "<td>" + removeFilterName(m_ColNames[i]) + "</td>" 
 
171
                + "</tr>\n";
 
172
    }
 
173
 
 
174
    result += "</table>\n";
 
175
 
 
176
    return result;
 
177
  }
 
178
 
 
179
  /**
 
180
   * returns the summary as string
 
181
   */
 
182
  public String toStringSummary() {
 
183
    String      result;
 
184
    String      titles;
 
185
    int         resultsetLength;
 
186
    int         i;
 
187
    int         j;
 
188
    String      content;
 
189
 
 
190
    if (m_NonSigWins == null)
 
191
      return "-summary data not set-";
 
192
    
 
193
    result = "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
 
194
    titles = "   <tr>";
 
195
    resultsetLength = 1 + Math.max((int)(Math.log(getColCount())/Math.log(10)),
 
196
                                   (int)(Math.log(getRowCount())/Math.log(10)));
 
197
 
 
198
    for (i = 0; i < getColCount(); i++) {
 
199
      if (getColHidden(i))
 
200
        continue;
 
201
      titles += "<td align=\"center\"><b>" + getSummaryTitle(i) + "</b></td>";
 
202
    }
 
203
    result +=   titles 
 
204
              + "<td><b>(No. of datasets where [col] &gt;&gt; [row])</b></td></tr>\n";
 
205
 
 
206
    for (i = 0; i < getColCount(); i++) {
 
207
      if (getColHidden(i))
 
208
        continue;
 
209
 
 
210
      result += "   <tr>";
 
211
 
 
212
      for (j = 0; j < getColCount(); j++) {
 
213
        if (getColHidden(j))
 
214
          continue;
 
215
 
 
216
        if (j == i)
 
217
          content = Utils.padLeft("-", resultsetLength * 2 + 3);
 
218
        else
 
219
          content = Utils.padLeft("" + m_NonSigWins[i][j] 
 
220
                                  + " (" + m_Wins[i][j] + ")",
 
221
                                  resultsetLength * 2 + 3);
 
222
        result += "<td>" + content.replaceAll(" ", "&nbsp;") + "</td>";
 
223
      }
 
224
 
 
225
      result += "<td><b>" + getSummaryTitle(i) + "</b> = " + removeFilterName(m_ColNames[i]) + "</td></tr>\n";
 
226
    }
 
227
 
 
228
    result += "</table>\n";
 
229
 
 
230
    return result;
 
231
  }
 
232
 
 
233
  /**
 
234
   * returns the ranking in a string representation
 
235
   */
 
236
  public String toStringRanking() {
 
237
    int           biggest;
 
238
    int           width;
 
239
    String        result;
 
240
    int[]         ranking;
 
241
    int           i;
 
242
    int           curr;
 
243
 
 
244
    if (m_RankingWins == null)
 
245
      return "-ranking data not set-";
 
246
 
 
247
    biggest = Math.max(m_RankingWins[Utils.maxIndex(m_RankingWins)],
 
248
                       m_RankingLosses[Utils.maxIndex(m_RankingLosses)]);
 
249
    width = Math.max(2 + (int)(Math.log(biggest) / Math.log(10)),
 
250
                         ">-<".length());
 
251
    result = "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
 
252
    result +=  "   <tr>" 
 
253
             + "<td align=\"center\"><b>&gt;-&lt;</b></td>"
 
254
             + "<td align=\"center\"><b>&gt;</b></td>"
 
255
             + "<td align=\"center\"><b>&lt;</b></td>"
 
256
             + "<td><b>Resultset</b></td>"
 
257
             + "</tr>\n";
 
258
 
 
259
    ranking = Utils.sort(m_RankingDiff);
 
260
 
 
261
    for (i = getColCount() - 1; i >= 0; i--) {
 
262
      curr = ranking[i];
 
263
 
 
264
      if (getColHidden(curr))
 
265
        continue;
 
266
 
 
267
      result += "   <tr>"
 
268
        + "<td align=\"right\">" + m_RankingDiff[curr] + "</td>"
 
269
        + "<td align=\"right\">" + m_RankingWins[curr] + "</td>"
 
270
        + "<td align=\"right\">" + m_RankingLosses[curr] + "</td>"
 
271
        + "<td>" + removeFilterName(m_ColNames[curr]) + "</td>"
 
272
        + "<tr>\n";
 
273
    }
 
274
 
 
275
    result += "</table>\n";
 
276
 
 
277
    return result;
 
278
  }
 
279
 
 
280
  /**
 
281
   * for testing only
 
282
   */
 
283
  public static void main(String[] args) {
 
284
    ResultMatrix        matrix;
 
285
    int                 i;
 
286
    int                 n;
 
287
    
 
288
    matrix = new ResultMatrixHTML(3, 3);
 
289
    
 
290
    // set header
 
291
    matrix.addHeader("header1", "value1");
 
292
    matrix.addHeader("header2", "value2");
 
293
    matrix.addHeader("header2", "value3");
 
294
    
 
295
    // set values
 
296
    for (i = 0; i < matrix.getRowCount(); i++) {
 
297
      for (n = 0; n < matrix.getColCount(); n++) {
 
298
        matrix.setMean(n, i, (i+1)*n);
 
299
        matrix.setStdDev(n, i, ((double) (i+1)*n) / 100);
 
300
        if (i == n) {
 
301
          if (i % 2 == 1)
 
302
            matrix.setSignificance(n, i, SIGNIFICANCE_WIN);
 
303
          else
 
304
            matrix.setSignificance(n, i, SIGNIFICANCE_LOSS);
 
305
        }
 
306
      }
 
307
    }
 
308
 
 
309
    System.out.println("\n\n--> " + matrix.getDisplayName());
 
310
    
 
311
    System.out.println("\n1. complete\n");
 
312
    System.out.println(matrix.toStringHeader() + "\n");
 
313
    System.out.println(matrix.toStringMatrix() + "\n");
 
314
    System.out.println(matrix.toStringKey());
 
315
    
 
316
    System.out.println("\n2. complete with std deviations\n");
 
317
    matrix.setShowStdDev(true);
 
318
    System.out.println(matrix.toStringMatrix());
 
319
    
 
320
    System.out.println("\n3. cols numbered\n");
 
321
    matrix.setPrintColNames(false);
 
322
    System.out.println(matrix.toStringMatrix());
 
323
    
 
324
    System.out.println("\n4. second col missing\n");
 
325
    matrix.setColHidden(1, true);
 
326
    System.out.println(matrix.toStringMatrix());
 
327
    
 
328
    System.out.println("\n5. last row missing, rows numbered too\n");
 
329
    matrix.setRowHidden(2, true);
 
330
    matrix.setPrintRowNames(false);
 
331
    System.out.println(matrix.toStringMatrix());
 
332
    
 
333
    System.out.println("\n6. mean prec to 3\n");
 
334
    matrix.setMeanPrec(3);
 
335
    matrix.setPrintRowNames(false);
 
336
    System.out.println(matrix.toStringMatrix());
 
337
  }
 
338
}