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.RevisionUtils; |
---|
26 | import weka.core.Utils; |
---|
27 | |
---|
28 | /** |
---|
29 | <!-- globalinfo-start --> |
---|
30 | * Generates the matrix output as HTML. |
---|
31 | * <p/> |
---|
32 | <!-- globalinfo-end --> |
---|
33 | * |
---|
34 | <!-- options-start --> |
---|
35 | * Valid options are: <p/> |
---|
36 | * |
---|
37 | * <pre> -mean-prec <int> |
---|
38 | * The number of decimals after the decimal point for the mean. |
---|
39 | * (default: 2)</pre> |
---|
40 | * |
---|
41 | * <pre> -stddev-prec <int> |
---|
42 | * The number of decimals after the decimal point for the mean. |
---|
43 | * (default: 2)</pre> |
---|
44 | * |
---|
45 | * <pre> -col-name-width <int> |
---|
46 | * The maximum width for the column names (0 = optimal). |
---|
47 | * (default: 0)</pre> |
---|
48 | * |
---|
49 | * <pre> -row-name-width <int> |
---|
50 | * The maximum width for the row names (0 = optimal). |
---|
51 | * (default: 25)</pre> |
---|
52 | * |
---|
53 | * <pre> -mean-width <int> |
---|
54 | * The width of the mean (0 = optimal). |
---|
55 | * (default: 0)</pre> |
---|
56 | * |
---|
57 | * <pre> -stddev-width <int> |
---|
58 | * The width of the standard deviation (0 = optimal). |
---|
59 | * (default: 0)</pre> |
---|
60 | * |
---|
61 | * <pre> -sig-width <int> |
---|
62 | * The width of the significance indicator (0 = optimal). |
---|
63 | * (default: 0)</pre> |
---|
64 | * |
---|
65 | * <pre> -count-width <int> |
---|
66 | * The width of the counts (0 = optimal). |
---|
67 | * (default: 0)</pre> |
---|
68 | * |
---|
69 | * <pre> -show-stddev |
---|
70 | * Whether to display the standard deviation column. |
---|
71 | * (default: no)</pre> |
---|
72 | * |
---|
73 | * <pre> -show-avg |
---|
74 | * Whether to show the row with averages. |
---|
75 | * (default: no)</pre> |
---|
76 | * |
---|
77 | * <pre> -remove-filter |
---|
78 | * Whether to remove the classname package prefixes from the |
---|
79 | * filter names in datasets. |
---|
80 | * (default: no)</pre> |
---|
81 | * |
---|
82 | * <pre> -print-col-names |
---|
83 | * Whether to output column names or just numbers representing them. |
---|
84 | * (default: no)</pre> |
---|
85 | * |
---|
86 | * <pre> -print-row-names |
---|
87 | * Whether to output row names or just numbers representing them. |
---|
88 | * (default: no)</pre> |
---|
89 | * |
---|
90 | * <pre> -enum-col-names |
---|
91 | * Whether to enumerate the column names (prefixing them with |
---|
92 | * '(x)', with 'x' being the index). |
---|
93 | * (default: no)</pre> |
---|
94 | * |
---|
95 | * <pre> -enum-row-names |
---|
96 | * Whether to enumerate the row names (prefixing them with |
---|
97 | * '(x)', with 'x' being the index). |
---|
98 | * (default: no)</pre> |
---|
99 | * |
---|
100 | <!-- options-end --> |
---|
101 | * |
---|
102 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
103 | * @version $Revision: 5346 $ |
---|
104 | */ |
---|
105 | public class ResultMatrixHTML |
---|
106 | extends ResultMatrix { |
---|
107 | |
---|
108 | /** for serialization. */ |
---|
109 | private static final long serialVersionUID = 6672380422544799990L; |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes the matrix as 1x1 matrix. |
---|
113 | */ |
---|
114 | public ResultMatrixHTML() { |
---|
115 | this(1, 1); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * initializes the matrix with the given dimensions. |
---|
120 | * |
---|
121 | * @param cols the number of columns |
---|
122 | * @param rows the number of rows |
---|
123 | */ |
---|
124 | public ResultMatrixHTML(int cols, int rows) { |
---|
125 | super(cols, rows); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * initializes the matrix with the values from the given matrix. |
---|
130 | * |
---|
131 | * @param matrix the matrix to get the values from |
---|
132 | */ |
---|
133 | public ResultMatrixHTML(ResultMatrix matrix) { |
---|
134 | super(matrix); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Returns a string describing the matrix. |
---|
139 | * |
---|
140 | * @return a description suitable for |
---|
141 | * displaying in the experimenter gui |
---|
142 | */ |
---|
143 | public String globalInfo() { |
---|
144 | return "Generates the matrix output as HTML."; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * returns the name of the output format. |
---|
149 | * |
---|
150 | * @return the display name |
---|
151 | */ |
---|
152 | public String getDisplayName() { |
---|
153 | return "HTML"; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * returns the default width for the row names. |
---|
158 | * |
---|
159 | * @return the width |
---|
160 | */ |
---|
161 | public int getDefaultRowNameWidth() { |
---|
162 | return 25; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * returns the default of whether column names or numbers instead are printed. |
---|
167 | * |
---|
168 | * @return true if names instead of numbers are printed |
---|
169 | */ |
---|
170 | public boolean getDefaultPrintColNames() { |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * returns the default of whether column names are prefixed with the index. |
---|
176 | * |
---|
177 | * @return true if the names are prefixed |
---|
178 | */ |
---|
179 | public boolean getDefaultEnumerateColNames() { |
---|
180 | return true; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * returns the header of the matrix as a string. |
---|
185 | * |
---|
186 | * @return the header |
---|
187 | * @see #m_HeaderKeys |
---|
188 | * @see #m_HeaderValues |
---|
189 | */ |
---|
190 | public String toStringHeader() { |
---|
191 | return new ResultMatrixPlainText(this).toStringHeader(); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * returns the matrix in an HTML table. |
---|
196 | * |
---|
197 | * @return the matrix |
---|
198 | */ |
---|
199 | public String toStringMatrix() { |
---|
200 | StringBuffer result; |
---|
201 | String[][] cells; |
---|
202 | int i; |
---|
203 | int n; |
---|
204 | int cols; |
---|
205 | |
---|
206 | result = new StringBuffer(); |
---|
207 | cells = toArray(); |
---|
208 | |
---|
209 | result.append("<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n"); |
---|
210 | |
---|
211 | // headings |
---|
212 | result.append(" <tr>"); |
---|
213 | for (n = 0; n < cells[0].length; n++) { |
---|
214 | if (isRowName(n)) { |
---|
215 | result.append("<td><b>" + cells[0][n] + "</b></td>"); |
---|
216 | } |
---|
217 | else if (isMean(n)) { |
---|
218 | if (n == 1) |
---|
219 | cols = 1; |
---|
220 | else |
---|
221 | cols = 2; |
---|
222 | if (getShowStdDev()) |
---|
223 | cols++; |
---|
224 | result.append("<td align=\"center\" colspan=\"" + cols + "\">"); |
---|
225 | result.append("<b>" + cells[0][n] + "</b>"); |
---|
226 | result.append("</td>"); |
---|
227 | } |
---|
228 | } |
---|
229 | result.append("</tr>\n"); |
---|
230 | |
---|
231 | // data |
---|
232 | for (i = 1; i < cells.length; i++) { |
---|
233 | result.append(" <tr>"); |
---|
234 | for (n = 0; n < cells[i].length; n++) { |
---|
235 | if (isRowName(n)) |
---|
236 | result.append("<td>"); |
---|
237 | else if (isMean(n) || isStdDev(n)) |
---|
238 | result.append("<td align=\"right\">"); |
---|
239 | else if (isSignificance(n)) |
---|
240 | result.append("<td align=\"center\">"); |
---|
241 | else |
---|
242 | result.append("<td>"); |
---|
243 | |
---|
244 | // content |
---|
245 | if (cells[i][n].trim().equals("")) |
---|
246 | result.append(" "); |
---|
247 | else if (isStdDev(n)) |
---|
248 | result.append("± " + cells[i][n]); |
---|
249 | else |
---|
250 | result.append(cells[i][n]); |
---|
251 | |
---|
252 | result.append("</td>"); |
---|
253 | } |
---|
254 | result.append("</tr>\n"); |
---|
255 | } |
---|
256 | result.append("</table>\n"); |
---|
257 | |
---|
258 | return result.toString(); |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * returns returns a key for all the col names, for better readability if |
---|
263 | * the names got cut off. |
---|
264 | * |
---|
265 | * @return the key |
---|
266 | */ |
---|
267 | public String toStringKey() { |
---|
268 | String result; |
---|
269 | int i; |
---|
270 | |
---|
271 | result = "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n" |
---|
272 | + " <tr><td colspan=\"2\"><b>Key</b></td></tr>\n"; |
---|
273 | for (i = 0; i < getColCount(); i++) { |
---|
274 | if (getColHidden(i)) |
---|
275 | continue; |
---|
276 | |
---|
277 | result += " <tr>" |
---|
278 | + "<td><b>(" + (i+1) + ")</b></td>" |
---|
279 | + "<td>" + removeFilterName(m_ColNames[i]) + "</td>" |
---|
280 | + "</tr>\n"; |
---|
281 | } |
---|
282 | |
---|
283 | result += "</table>\n"; |
---|
284 | |
---|
285 | return result; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * returns the summary as string. |
---|
290 | * |
---|
291 | * @return the summary |
---|
292 | */ |
---|
293 | public String toStringSummary() { |
---|
294 | String result; |
---|
295 | String titles; |
---|
296 | int resultsetLength; |
---|
297 | int i; |
---|
298 | int j; |
---|
299 | String content; |
---|
300 | |
---|
301 | if (m_NonSigWins == null) |
---|
302 | return "-summary data not set-"; |
---|
303 | |
---|
304 | result = "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n"; |
---|
305 | titles = " <tr>"; |
---|
306 | resultsetLength = 1 + Math.max((int)(Math.log(getColCount())/Math.log(10)), |
---|
307 | (int)(Math.log(getRowCount())/Math.log(10))); |
---|
308 | |
---|
309 | for (i = 0; i < getColCount(); i++) { |
---|
310 | if (getColHidden(i)) |
---|
311 | continue; |
---|
312 | titles += "<td align=\"center\"><b>" + getSummaryTitle(i) + "</b></td>"; |
---|
313 | } |
---|
314 | result += titles |
---|
315 | + "<td><b>(No. of datasets where [col] >> [row])</b></td></tr>\n"; |
---|
316 | |
---|
317 | for (i = 0; i < getColCount(); i++) { |
---|
318 | if (getColHidden(i)) |
---|
319 | continue; |
---|
320 | |
---|
321 | result += " <tr>"; |
---|
322 | |
---|
323 | for (j = 0; j < getColCount(); j++) { |
---|
324 | if (getColHidden(j)) |
---|
325 | continue; |
---|
326 | |
---|
327 | if (j == i) |
---|
328 | content = Utils.padLeft("-", resultsetLength * 2 + 3); |
---|
329 | else |
---|
330 | content = Utils.padLeft("" + m_NonSigWins[i][j] |
---|
331 | + " (" + m_Wins[i][j] + ")", |
---|
332 | resultsetLength * 2 + 3); |
---|
333 | result += "<td>" + content.replaceAll(" ", " ") + "</td>"; |
---|
334 | } |
---|
335 | |
---|
336 | result += "<td><b>" + getSummaryTitle(i) + "</b> = " + removeFilterName(m_ColNames[i]) + "</td></tr>\n"; |
---|
337 | } |
---|
338 | |
---|
339 | result += "</table>\n"; |
---|
340 | |
---|
341 | return result; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * returns the ranking in a string representation. |
---|
346 | * |
---|
347 | * @return the ranking |
---|
348 | */ |
---|
349 | public String toStringRanking() { |
---|
350 | String result; |
---|
351 | int[] ranking; |
---|
352 | int i; |
---|
353 | int curr; |
---|
354 | |
---|
355 | if (m_RankingWins == null) |
---|
356 | return "-ranking data not set-"; |
---|
357 | |
---|
358 | result = "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n"; |
---|
359 | result += " <tr>" |
---|
360 | + "<td align=\"center\"><b>>-<</b></td>" |
---|
361 | + "<td align=\"center\"><b>></b></td>" |
---|
362 | + "<td align=\"center\"><b><</b></td>" |
---|
363 | + "<td><b>Resultset</b></td>" |
---|
364 | + "</tr>\n"; |
---|
365 | |
---|
366 | ranking = Utils.sort(m_RankingDiff); |
---|
367 | |
---|
368 | for (i = getColCount() - 1; i >= 0; i--) { |
---|
369 | curr = ranking[i]; |
---|
370 | |
---|
371 | if (getColHidden(curr)) |
---|
372 | continue; |
---|
373 | |
---|
374 | result += " <tr>" |
---|
375 | + "<td align=\"right\">" + m_RankingDiff[curr] + "</td>" |
---|
376 | + "<td align=\"right\">" + m_RankingWins[curr] + "</td>" |
---|
377 | + "<td align=\"right\">" + m_RankingLosses[curr] + "</td>" |
---|
378 | + "<td>" + removeFilterName(m_ColNames[curr]) + "</td>" |
---|
379 | + "<tr>\n"; |
---|
380 | } |
---|
381 | |
---|
382 | result += "</table>\n"; |
---|
383 | |
---|
384 | return result; |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * Returns the revision string. |
---|
389 | * |
---|
390 | * @return the revision |
---|
391 | */ |
---|
392 | public String getRevision() { |
---|
393 | return RevisionUtils.extract("$Revision: 5346 $"); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * for testing only. |
---|
398 | * |
---|
399 | * @param args ignored |
---|
400 | */ |
---|
401 | public static void main(String[] args) { |
---|
402 | ResultMatrix matrix; |
---|
403 | int i; |
---|
404 | int n; |
---|
405 | |
---|
406 | matrix = new ResultMatrixHTML(3, 3); |
---|
407 | |
---|
408 | // set header |
---|
409 | matrix.addHeader("header1", "value1"); |
---|
410 | matrix.addHeader("header2", "value2"); |
---|
411 | matrix.addHeader("header2", "value3"); |
---|
412 | |
---|
413 | // set values |
---|
414 | for (i = 0; i < matrix.getRowCount(); i++) { |
---|
415 | for (n = 0; n < matrix.getColCount(); n++) { |
---|
416 | matrix.setMean(n, i, (i+1)*n); |
---|
417 | matrix.setStdDev(n, i, ((double) (i+1)*n) / 100); |
---|
418 | if (i == n) { |
---|
419 | if (i % 2 == 1) |
---|
420 | matrix.setSignificance(n, i, SIGNIFICANCE_WIN); |
---|
421 | else |
---|
422 | matrix.setSignificance(n, i, SIGNIFICANCE_LOSS); |
---|
423 | } |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | System.out.println("\n\n--> " + matrix.getDisplayName()); |
---|
428 | |
---|
429 | System.out.println("\n1. complete\n"); |
---|
430 | System.out.println(matrix.toStringHeader() + "\n"); |
---|
431 | System.out.println(matrix.toStringMatrix() + "\n"); |
---|
432 | System.out.println(matrix.toStringKey()); |
---|
433 | |
---|
434 | System.out.println("\n2. complete with std deviations\n"); |
---|
435 | matrix.setShowStdDev(true); |
---|
436 | System.out.println(matrix.toStringMatrix()); |
---|
437 | |
---|
438 | System.out.println("\n3. cols numbered\n"); |
---|
439 | matrix.setPrintColNames(false); |
---|
440 | System.out.println(matrix.toStringMatrix()); |
---|
441 | |
---|
442 | System.out.println("\n4. second col missing\n"); |
---|
443 | matrix.setColHidden(1, true); |
---|
444 | System.out.println(matrix.toStringMatrix()); |
---|
445 | |
---|
446 | System.out.println("\n5. last row missing, rows numbered too\n"); |
---|
447 | matrix.setRowHidden(2, true); |
---|
448 | matrix.setPrintRowNames(false); |
---|
449 | System.out.println(matrix.toStringMatrix()); |
---|
450 | |
---|
451 | System.out.println("\n6. mean prec to 3\n"); |
---|
452 | matrix.setMeanPrec(3); |
---|
453 | matrix.setPrintRowNames(false); |
---|
454 | System.out.println(matrix.toStringMatrix()); |
---|
455 | } |
---|
456 | } |
---|