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 | * ResultMatrixPlainText.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 output as plain text (for fixed width fonts). |
---|
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: 5)</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 ResultMatrixPlainText |
---|
106 | extends ResultMatrix { |
---|
107 | |
---|
108 | /** for serialization. */ |
---|
109 | private static final long serialVersionUID = 1502934525382357937L; |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes the matrix as 1x1 matrix. |
---|
113 | */ |
---|
114 | public ResultMatrixPlainText() { |
---|
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 ResultMatrixPlainText(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 ResultMatrixPlainText(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 output as plain text (for fixed width fonts)."; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * returns the name of the output format. |
---|
149 | * |
---|
150 | * @return the display name |
---|
151 | */ |
---|
152 | public String getDisplayName() { |
---|
153 | return "Plain Text"; |
---|
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 width for the counts. |
---|
167 | * |
---|
168 | * @return the width |
---|
169 | */ |
---|
170 | public int getDefaultCountWidth() { |
---|
171 | return 5; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * returns the header of the matrix as a string. |
---|
176 | * |
---|
177 | * @return the header |
---|
178 | * @see #m_HeaderKeys |
---|
179 | * @see #m_HeaderValues |
---|
180 | */ |
---|
181 | public String toStringHeader() { |
---|
182 | int i; |
---|
183 | int size; |
---|
184 | String[][] data; |
---|
185 | String result; |
---|
186 | |
---|
187 | result = ""; |
---|
188 | |
---|
189 | // fill in data |
---|
190 | data = new String[m_HeaderKeys.size()][2]; |
---|
191 | for (i = 0; i < m_HeaderKeys.size(); i++) { |
---|
192 | data[i][0] = m_HeaderKeys.get(i).toString() + ":"; |
---|
193 | data[i][1] = m_HeaderValues.get(i).toString(); |
---|
194 | } |
---|
195 | |
---|
196 | // pad |
---|
197 | size = getColSize(data, 0); |
---|
198 | for (i = 0; i < data.length; i++) |
---|
199 | data[i][0] = padString(data[i][0], size); |
---|
200 | |
---|
201 | // build result |
---|
202 | for (i = 0; i < data.length; i++) |
---|
203 | result += data[i][0] + " " + data[i][1] + "\n"; |
---|
204 | |
---|
205 | return result; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * returns the matrix as plain text. |
---|
210 | * |
---|
211 | * @return the matrix |
---|
212 | */ |
---|
213 | public String toStringMatrix() { |
---|
214 | StringBuffer result; |
---|
215 | String[][] cells; |
---|
216 | int i; |
---|
217 | int j; |
---|
218 | int n; |
---|
219 | int k; |
---|
220 | int size; |
---|
221 | String line; |
---|
222 | int indexBase; |
---|
223 | int indexSecond; |
---|
224 | StringBuffer head; |
---|
225 | StringBuffer body; |
---|
226 | StringBuffer foot; |
---|
227 | int[] startMeans; |
---|
228 | int[] startSigs; |
---|
229 | int maxLength; |
---|
230 | |
---|
231 | result = new StringBuffer(); |
---|
232 | head = new StringBuffer(); |
---|
233 | body = new StringBuffer(); |
---|
234 | foot = new StringBuffer(); |
---|
235 | cells = toArray(); |
---|
236 | startMeans = new int[getColCount()]; |
---|
237 | startSigs = new int[getColCount() - 1]; |
---|
238 | maxLength = 0; |
---|
239 | |
---|
240 | // pad numbers |
---|
241 | for (n = 1; n < cells[0].length; n++) { |
---|
242 | size = getColSize(cells, n, true, true); |
---|
243 | for (i = 1; i < cells.length - 1; i++) |
---|
244 | cells[i][n] = padString(cells[i][n], size, true); |
---|
245 | } |
---|
246 | |
---|
247 | // index of base column in array |
---|
248 | indexBase = 1; |
---|
249 | if (getShowStdDev()) |
---|
250 | indexBase++; |
---|
251 | |
---|
252 | // index of second column in array |
---|
253 | indexSecond = indexBase + 1; |
---|
254 | if (getShowStdDev()) |
---|
255 | indexSecond++; |
---|
256 | |
---|
257 | // output data (without "(v/ /*)") |
---|
258 | j = 0; |
---|
259 | k = 0; |
---|
260 | for (i = 1; i < cells.length - 1; i++) { |
---|
261 | if (isAverage(i)) |
---|
262 | body.append(padString("", maxLength).replaceAll(".", "-") + "\n"); |
---|
263 | line = ""; |
---|
264 | |
---|
265 | for (n = 0; n < cells[0].length; n++) { |
---|
266 | // record starts |
---|
267 | if (i == 1) { |
---|
268 | if (isMean(n)) { |
---|
269 | startMeans[j] = line.length(); |
---|
270 | j++; |
---|
271 | } |
---|
272 | |
---|
273 | if (isSignificance(n)) { |
---|
274 | startSigs[k] = line.length(); |
---|
275 | k++; |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | if (n == 0) { |
---|
280 | line += padString(cells[i][n], getRowNameWidth()); |
---|
281 | if (!isAverage(i)) |
---|
282 | line += padString("(" + |
---|
283 | Utils.doubleToString(getCount(getDisplayRow(i-1)), 0) + ")", |
---|
284 | getCountWidth(), true); |
---|
285 | else |
---|
286 | line += padString("", getCountWidth(), true); |
---|
287 | } |
---|
288 | else { |
---|
289 | // additional space before means |
---|
290 | if (isMean(n)) |
---|
291 | line += " "; |
---|
292 | |
---|
293 | // print cell |
---|
294 | if (getShowStdDev()) { |
---|
295 | if (isMean(n - 1)) { |
---|
296 | if (!cells[i][n].trim().equals("")) |
---|
297 | line += "(" + cells[i][n] + ")"; |
---|
298 | else |
---|
299 | line += " " + cells[i][n] + " "; |
---|
300 | } |
---|
301 | else |
---|
302 | line += " " + cells[i][n]; |
---|
303 | } |
---|
304 | else { |
---|
305 | line += " " + cells[i][n]; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | // add separator after base column |
---|
310 | if (n == indexBase) |
---|
311 | line += " |"; |
---|
312 | } |
---|
313 | |
---|
314 | // record overall length |
---|
315 | if (i == 1) |
---|
316 | maxLength = line.length(); |
---|
317 | |
---|
318 | body.append(line + "\n"); |
---|
319 | } |
---|
320 | |
---|
321 | // column names |
---|
322 | line = padString(cells[0][0], startMeans[0]); |
---|
323 | i = -1; |
---|
324 | for (n = 1; n < cells[0].length; n++) { |
---|
325 | if (isMean(n)) { |
---|
326 | i++; |
---|
327 | |
---|
328 | if (i == 0) |
---|
329 | line = padString(line, startMeans[i] - getCountWidth()); |
---|
330 | else if (i == 1) |
---|
331 | line = padString(line, startMeans[i] - " |".length()); |
---|
332 | else if (i > 1) |
---|
333 | line = padString(line, startMeans[i]); |
---|
334 | |
---|
335 | if (i == 1) |
---|
336 | line += " |"; |
---|
337 | |
---|
338 | line += " " + cells[0][n]; |
---|
339 | } |
---|
340 | } |
---|
341 | line = padString(line, maxLength); |
---|
342 | head.append(line + "\n"); |
---|
343 | head.append(line.replaceAll(".", "-") + "\n"); |
---|
344 | body.append(line.replaceAll(".", "-") + "\n"); |
---|
345 | |
---|
346 | // output wins/losses/ties |
---|
347 | if (getColCount() > 1) { |
---|
348 | line = padString(cells[cells.length - 1][0], startMeans[1]-2, true) + " |"; |
---|
349 | i = 0; |
---|
350 | for (n = 1; n < cells[cells.length - 1].length; n++) { |
---|
351 | if (isSignificance(n)) { |
---|
352 | line = padString( |
---|
353 | line, startSigs[i] + 1 - cells[cells.length - 1][n].length()); |
---|
354 | line += " " + cells[cells.length - 1][n]; |
---|
355 | i++; |
---|
356 | } |
---|
357 | } |
---|
358 | line = padString(line, maxLength); |
---|
359 | } |
---|
360 | else { |
---|
361 | line = padString(cells[cells.length - 1][0], line.length() - 2) + " |"; |
---|
362 | } |
---|
363 | foot.append(line + "\n"); |
---|
364 | |
---|
365 | // assemble output |
---|
366 | result.append(head.toString()); |
---|
367 | result.append(body.toString()); |
---|
368 | result.append(foot.toString()); |
---|
369 | |
---|
370 | return result.toString(); |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * returns returns a key for all the col names, for better readability if |
---|
375 | * the names got cut off. |
---|
376 | * |
---|
377 | * @return the key |
---|
378 | */ |
---|
379 | public String toStringKey() { |
---|
380 | String result; |
---|
381 | int i; |
---|
382 | |
---|
383 | result = "Key:\n"; |
---|
384 | for (i = 0; i < getColCount(); i++) { |
---|
385 | if (getColHidden(i)) |
---|
386 | continue; |
---|
387 | |
---|
388 | result += LEFT_PARENTHESES + (i+1) + RIGHT_PARENTHESES |
---|
389 | + " " + removeFilterName(m_ColNames[i]) + "\n"; |
---|
390 | } |
---|
391 | |
---|
392 | return result; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * returns the summary as string. |
---|
397 | * |
---|
398 | * @return the summary |
---|
399 | */ |
---|
400 | public String toStringSummary() { |
---|
401 | String result; |
---|
402 | String titles; |
---|
403 | int resultsetLength; |
---|
404 | int i; |
---|
405 | int j; |
---|
406 | |
---|
407 | if (m_NonSigWins == null) |
---|
408 | return "-summary data not set-"; |
---|
409 | |
---|
410 | result = ""; |
---|
411 | titles = ""; |
---|
412 | resultsetLength = 1 + Math.max((int)(Math.log(getColCount())/Math.log(10)), |
---|
413 | (int)(Math.log(getRowCount())/Math.log(10))); |
---|
414 | |
---|
415 | for (i = 0; i < getColCount(); i++) { |
---|
416 | if (getColHidden(i)) |
---|
417 | continue; |
---|
418 | titles += " " + Utils.padLeft("" + getSummaryTitle(i), |
---|
419 | resultsetLength * 2 + 3); |
---|
420 | } |
---|
421 | result += titles + " (No. of datasets where [col] >> [row])\n"; |
---|
422 | |
---|
423 | for (i = 0; i < getColCount(); i++) { |
---|
424 | if (getColHidden(i)) |
---|
425 | continue; |
---|
426 | |
---|
427 | for (j = 0; j < getColCount(); j++) { |
---|
428 | if (getColHidden(j)) |
---|
429 | continue; |
---|
430 | |
---|
431 | result += " "; |
---|
432 | if (j == i) |
---|
433 | result += Utils.padLeft("-", resultsetLength * 2 + 3); |
---|
434 | else |
---|
435 | result += Utils.padLeft("" + m_NonSigWins[i][j] |
---|
436 | + " (" + m_Wins[i][j] + ")", |
---|
437 | resultsetLength * 2 + 3); |
---|
438 | } |
---|
439 | |
---|
440 | result += " | " + getSummaryTitle(i) + " = " + getColName(i) + '\n'; |
---|
441 | } |
---|
442 | |
---|
443 | return result; |
---|
444 | } |
---|
445 | |
---|
446 | /** |
---|
447 | * returns the ranking in a string representation. |
---|
448 | * |
---|
449 | * @return the ranking |
---|
450 | */ |
---|
451 | public String toStringRanking() { |
---|
452 | int biggest; |
---|
453 | int width; |
---|
454 | String result; |
---|
455 | int[] ranking; |
---|
456 | int i; |
---|
457 | int curr; |
---|
458 | |
---|
459 | if (m_RankingWins == null) |
---|
460 | return "-ranking data not set-"; |
---|
461 | |
---|
462 | biggest = Math.max(m_RankingWins[Utils.maxIndex(m_RankingWins)], |
---|
463 | m_RankingLosses[Utils.maxIndex(m_RankingLosses)]); |
---|
464 | width = Math.max(2 + (int)(Math.log(biggest) / Math.log(10)), |
---|
465 | ">-<".length()); |
---|
466 | result = Utils.padLeft(">-<", width) + ' ' |
---|
467 | + Utils.padLeft(">", width) + ' ' |
---|
468 | + Utils.padLeft("<", width) + " Resultset\n"; |
---|
469 | |
---|
470 | ranking = Utils.sort(m_RankingDiff); |
---|
471 | |
---|
472 | for (i = getColCount() - 1; i >= 0; i--) { |
---|
473 | curr = ranking[i]; |
---|
474 | |
---|
475 | if (getColHidden(curr)) |
---|
476 | continue; |
---|
477 | |
---|
478 | result += Utils.padLeft("" + m_RankingDiff[curr], width) + ' ' |
---|
479 | + Utils.padLeft("" + m_RankingWins[curr], width) + ' ' |
---|
480 | + Utils.padLeft("" + m_RankingLosses[curr], width) + ' ' |
---|
481 | + removeFilterName(m_ColNames[curr]) + '\n'; |
---|
482 | } |
---|
483 | |
---|
484 | return result; |
---|
485 | } |
---|
486 | |
---|
487 | /** |
---|
488 | * Returns the revision string. |
---|
489 | * |
---|
490 | * @return the revision |
---|
491 | */ |
---|
492 | public String getRevision() { |
---|
493 | return RevisionUtils.extract("$Revision: 5346 $"); |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * for testing only. |
---|
498 | * |
---|
499 | * @param args ignored |
---|
500 | */ |
---|
501 | public static void main(String[] args) { |
---|
502 | ResultMatrix matrix; |
---|
503 | int i; |
---|
504 | int n; |
---|
505 | |
---|
506 | matrix = new ResultMatrixPlainText(3, 3); |
---|
507 | |
---|
508 | // set header |
---|
509 | matrix.addHeader("header1", "value1"); |
---|
510 | matrix.addHeader("header2", "value2"); |
---|
511 | matrix.addHeader("header2", "value3"); |
---|
512 | |
---|
513 | // set values |
---|
514 | for (i = 0; i < matrix.getRowCount(); i++) { |
---|
515 | for (n = 0; n < matrix.getColCount(); n++) { |
---|
516 | matrix.setMean(n, i, (i+1)*n); |
---|
517 | matrix.setStdDev(n, i, ((double) (i+1)*n) / 100); |
---|
518 | if (i == n) { |
---|
519 | if (i % 2 == 1) |
---|
520 | matrix.setSignificance(n, i, SIGNIFICANCE_WIN); |
---|
521 | else |
---|
522 | matrix.setSignificance(n, i, SIGNIFICANCE_LOSS); |
---|
523 | } |
---|
524 | } |
---|
525 | } |
---|
526 | |
---|
527 | System.out.println("\n\n--> " + matrix.getDisplayName()); |
---|
528 | |
---|
529 | System.out.println("\n1. complete\n"); |
---|
530 | System.out.println(matrix.toStringHeader() + "\n"); |
---|
531 | System.out.println(matrix.toStringMatrix() + "\n"); |
---|
532 | System.out.println(matrix.toStringKey()); |
---|
533 | |
---|
534 | System.out.println("\n2. complete with std deviations\n"); |
---|
535 | matrix.setShowStdDev(true); |
---|
536 | System.out.println(matrix.toStringMatrix()); |
---|
537 | |
---|
538 | System.out.println("\n3. cols numbered\n"); |
---|
539 | matrix.setPrintColNames(false); |
---|
540 | System.out.println(matrix.toStringMatrix()); |
---|
541 | |
---|
542 | System.out.println("\n4. second col missing\n"); |
---|
543 | matrix.setColHidden(1, true); |
---|
544 | System.out.println(matrix.toStringMatrix()); |
---|
545 | |
---|
546 | System.out.println("\n5. last row missing, rows numbered too\n"); |
---|
547 | matrix.setRowHidden(2, true); |
---|
548 | matrix.setPrintRowNames(false); |
---|
549 | System.out.println(matrix.toStringMatrix()); |
---|
550 | |
---|
551 | System.out.println("\n6. mean prec to 3\n"); |
---|
552 | matrix.setMeanPrec(3); |
---|
553 | matrix.setPrintRowNames(false); |
---|
554 | System.out.println(matrix.toStringMatrix()); |
---|
555 | } |
---|
556 | } |
---|