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 | * ResultMatrixLatex.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 in LaTeX-syntax. |
---|
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: 0)</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 ResultMatrixLatex |
---|
106 | extends ResultMatrix { |
---|
107 | |
---|
108 | /** for serialization. */ |
---|
109 | private static final long serialVersionUID = 777690788447600978L; |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes the matrix as 1x1 matrix. |
---|
113 | */ |
---|
114 | public ResultMatrixLatex() { |
---|
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 ResultMatrixLatex(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 ResultMatrixLatex(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 in LaTeX-syntax."; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * returns the name of the output format. |
---|
149 | * |
---|
150 | * @return the display name |
---|
151 | */ |
---|
152 | public String getDisplayName() { |
---|
153 | return "LaTeX"; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * removes the stored data but retains the dimensions of the matrix. |
---|
158 | */ |
---|
159 | public void clear() { |
---|
160 | super.clear(); |
---|
161 | TIE_STRING = " "; |
---|
162 | WIN_STRING = "$\\circ$"; |
---|
163 | LOSS_STRING = "$\\bullet$"; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * returns the default of whether column names or numbers instead are printed. |
---|
168 | * |
---|
169 | * @return true if names instead of numbers are printed |
---|
170 | */ |
---|
171 | public boolean getDefaultPrintColNames() { |
---|
172 | return false; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * returns the default of whether column names are prefixed with the index. |
---|
177 | * |
---|
178 | * @return true if the names are prefixed |
---|
179 | */ |
---|
180 | public boolean getDefaultEnumerateColNames() { |
---|
181 | return true; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * returns the header of the matrix as a string. |
---|
186 | * |
---|
187 | * @return the header |
---|
188 | * @see #m_HeaderKeys |
---|
189 | * @see #m_HeaderValues |
---|
190 | */ |
---|
191 | public String toStringHeader() { |
---|
192 | return new ResultMatrixPlainText(this).toStringHeader(); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * returns the matrix as latex table. |
---|
197 | * |
---|
198 | * @return the matrix |
---|
199 | */ |
---|
200 | public String toStringMatrix() { |
---|
201 | StringBuffer result; |
---|
202 | String[][] cells; |
---|
203 | int i; |
---|
204 | int j; |
---|
205 | int n; |
---|
206 | int size; |
---|
207 | |
---|
208 | result = new StringBuffer(); |
---|
209 | cells = toArray(); |
---|
210 | |
---|
211 | result.append( "\\begin{table}[thb]\n\\caption{\\label{labelname}" |
---|
212 | + "Table Caption}\n"); |
---|
213 | if (!getShowStdDev()) |
---|
214 | result.append("\\footnotesize\n"); |
---|
215 | else |
---|
216 | result.append("\\scriptsize\n"); |
---|
217 | |
---|
218 | // output the column alignment characters |
---|
219 | // one for the dataset name and one for the comparison column |
---|
220 | if (!getShowStdDev()) { |
---|
221 | result.append( "{\\centering \\begin{tabular}{" |
---|
222 | + "l" // dataset |
---|
223 | + "" // separator |
---|
224 | + "r" // mean |
---|
225 | ); |
---|
226 | } else { |
---|
227 | // dataset, mean, std dev |
---|
228 | result.append( "{\\centering \\begin{tabular}{" |
---|
229 | + "l" // dataset |
---|
230 | + "" // separator |
---|
231 | + "r" // mean |
---|
232 | + "@{\\hspace{0cm}}" // separator |
---|
233 | + "c" // +/- |
---|
234 | + "@{\\hspace{0cm}}" // separator |
---|
235 | + "r" // stddev |
---|
236 | ); |
---|
237 | } |
---|
238 | |
---|
239 | for (j = 1; j < getColCount(); j++) { |
---|
240 | if (getColHidden(j)) |
---|
241 | continue; |
---|
242 | if (!getShowStdDev()) |
---|
243 | result.append( "r" // mean |
---|
244 | + "@{\\hspace{0.1cm}}" // separator |
---|
245 | + "c" // significance |
---|
246 | ); |
---|
247 | else |
---|
248 | result.append( "r" // mean |
---|
249 | + "@{\\hspace{0cm}}" // separator |
---|
250 | + "c" // +/- |
---|
251 | + "@{\\hspace{0cm}}" // separator |
---|
252 | + "r" // stddev |
---|
253 | + "@{\\hspace{0.1cm}}" // separator |
---|
254 | + "c" // significance |
---|
255 | ); |
---|
256 | } |
---|
257 | result.append("}\n\\\\\n\\hline\n"); |
---|
258 | if (!getShowStdDev()) |
---|
259 | result.append("Dataset & " + cells[0][1]); |
---|
260 | else |
---|
261 | result.append("Dataset & \\multicolumn{3}{c}{" + cells[0][1] + "}"); |
---|
262 | |
---|
263 | // now do the column names (numbers) |
---|
264 | for (j = 2; j < cells[0].length; j++) { |
---|
265 | if (!isMean(j)) |
---|
266 | continue; |
---|
267 | if (!getShowStdDev()) |
---|
268 | result.append("& " + cells[0][j] + " & "); |
---|
269 | else |
---|
270 | result.append("& \\multicolumn{4}{c}{" + cells[0][j] + "} "); |
---|
271 | } |
---|
272 | result.append("\\\\\n\\hline\n"); |
---|
273 | |
---|
274 | // change "_" to "-" in names |
---|
275 | for (i = 1; i < cells.length; i++) |
---|
276 | cells[i][0] = cells[i][0].replace('_', '-'); |
---|
277 | |
---|
278 | // pad numbers |
---|
279 | for (n = 1; n < cells[0].length; n++) { |
---|
280 | size = getColSize(cells, n); |
---|
281 | for (i = 1; i < cells.length; i++) |
---|
282 | cells[i][n] = padString(cells[i][n], size, true); |
---|
283 | } |
---|
284 | |
---|
285 | // output data (w/o wins/ties/losses) |
---|
286 | for (i = 1; i < cells.length - 1; i++) { |
---|
287 | if (isAverage(i)) |
---|
288 | result.append("\\hline\n"); |
---|
289 | for (n = 0; n < cells[0].length; n++) { |
---|
290 | if (n == 0) { |
---|
291 | result.append(padString(cells[i][n], getRowNameWidth())); |
---|
292 | } |
---|
293 | else { |
---|
294 | if (getShowStdDev()) { |
---|
295 | if (isMean(n - 1)) { |
---|
296 | if (!cells[i][n].trim().equals("")) |
---|
297 | result.append(" & $\\pm$ & "); |
---|
298 | else |
---|
299 | result.append(" & & "); |
---|
300 | } |
---|
301 | else |
---|
302 | result.append(" & "); |
---|
303 | } |
---|
304 | else { |
---|
305 | result.append(" & "); |
---|
306 | } |
---|
307 | result.append(cells[i][n]); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | result.append("\\\\\n"); |
---|
312 | } |
---|
313 | |
---|
314 | result.append("\\hline\n\\multicolumn{" + cells[0].length + "}{c}{$\\circ$, $\\bullet$" |
---|
315 | +" statistically significant improvement or degradation}" |
---|
316 | +"\\\\\n\\end{tabular} "); |
---|
317 | if (!getShowStdDev()) |
---|
318 | result.append("\\footnotesize "); |
---|
319 | else |
---|
320 | result.append("\\scriptsize "); |
---|
321 | |
---|
322 | result.append("\\par}\n\\end{table}" |
---|
323 | +"\n"); |
---|
324 | |
---|
325 | return result.toString(); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * returns returns a key for all the col names, for better readability if |
---|
330 | * the names got cut off. |
---|
331 | * |
---|
332 | * @return the key |
---|
333 | */ |
---|
334 | public String toStringKey() { |
---|
335 | String result; |
---|
336 | int i; |
---|
337 | |
---|
338 | result = "\\begin{table}[thb]\n\\caption{\\label{labelname}" |
---|
339 | + "Table Caption (Key)}\n"; |
---|
340 | result += "\\scriptsize\n"; |
---|
341 | result += "{\\centering\n"; |
---|
342 | result += "\\begin{tabular}{cl}\\\\\n"; |
---|
343 | for (i = 0; i < getColCount(); i++) { |
---|
344 | if (getColHidden(i)) |
---|
345 | continue; |
---|
346 | |
---|
347 | result += LEFT_PARENTHESES + (i+1) + RIGHT_PARENTHESES |
---|
348 | + " & " + removeFilterName(m_ColNames[i]).replace('_', '-') |
---|
349 | .replaceAll("\\\\", "\\\\textbackslash") |
---|
350 | + " \\\\\n"; |
---|
351 | } |
---|
352 | result += "\\end{tabular}\n"; |
---|
353 | result += "}\n"; |
---|
354 | result += "\\end{table}\n"; |
---|
355 | |
---|
356 | return result; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * returns the summary as string. |
---|
361 | * |
---|
362 | * @return the summary |
---|
363 | */ |
---|
364 | public String toStringSummary() { |
---|
365 | int resultsetLength; |
---|
366 | String result; |
---|
367 | String titles; |
---|
368 | int i; |
---|
369 | int j; |
---|
370 | |
---|
371 | if (m_NonSigWins == null) |
---|
372 | return "-summary data not set-"; |
---|
373 | |
---|
374 | resultsetLength = 1 + Math.max((int)(Math.log(getColCount())/Math.log(10)), |
---|
375 | (int)(Math.log(getRowCount())/Math.log(10))); |
---|
376 | result = ""; |
---|
377 | titles = ""; |
---|
378 | |
---|
379 | result += "{\\centering\n"; |
---|
380 | result += "\\begin{table}[thb]\n\\caption{\\label{labelname}" |
---|
381 | +"Table Caption}\n"; |
---|
382 | result += "\\footnotesize\n"; |
---|
383 | result += "\\begin{tabular}{l"; |
---|
384 | |
---|
385 | for (i = 0; i < getColCount(); i++) { |
---|
386 | if (getColHidden(i)) |
---|
387 | continue; |
---|
388 | |
---|
389 | titles += " &"; |
---|
390 | result += "c"; |
---|
391 | titles += ' ' + Utils.padLeft("" + getSummaryTitle(i), |
---|
392 | resultsetLength * 2 + 3); |
---|
393 | } |
---|
394 | result += "}\\\\\n\\hline\n"; |
---|
395 | result += titles + " \\\\\n\\hline\n"; |
---|
396 | |
---|
397 | for (i = 0; i < getColCount(); i++) { |
---|
398 | if (getColHidden(i)) |
---|
399 | continue; |
---|
400 | |
---|
401 | for (j = 0; j < getColCount(); j++) { |
---|
402 | if (getColHidden(j)) |
---|
403 | continue; |
---|
404 | |
---|
405 | if (j == 0) |
---|
406 | result += (char)((int)'a' + i % 26); |
---|
407 | |
---|
408 | if (j == i) |
---|
409 | result += " & - "; |
---|
410 | else |
---|
411 | result += "& " + m_NonSigWins[i][j] + " (" + m_Wins[i][j] + ") "; |
---|
412 | } |
---|
413 | result += "\\\\\n"; |
---|
414 | } |
---|
415 | |
---|
416 | result += "\\hline\n\\end{tabular} \\footnotesize \\par\n\\end{table}}"; |
---|
417 | |
---|
418 | return result; |
---|
419 | } |
---|
420 | |
---|
421 | /** |
---|
422 | * returns the ranking in a string representation. |
---|
423 | * |
---|
424 | * @return the ranking |
---|
425 | */ |
---|
426 | public String toStringRanking() { |
---|
427 | int biggest; |
---|
428 | int width; |
---|
429 | String result; |
---|
430 | int[] ranking; |
---|
431 | int i; |
---|
432 | int curr; |
---|
433 | |
---|
434 | if (m_RankingWins == null) |
---|
435 | return "-ranking data not set-"; |
---|
436 | |
---|
437 | biggest = Math.max(m_RankingWins[Utils.maxIndex(m_RankingWins)], |
---|
438 | m_RankingLosses[Utils.maxIndex(m_RankingLosses)]); |
---|
439 | width = Math.max(2 + (int)(Math.log(biggest) / Math.log(10)), |
---|
440 | ">-<".length()); |
---|
441 | result = "\\begin{table}[thb]\n\\caption{\\label{labelname}Table Caption" |
---|
442 | + "}\n\\footnotesize\n{\\centering \\begin{tabular}{rlll}\\\\\n\\hline\n"; |
---|
443 | result += "Resultset & Wins$-$ & Wins & Losses \\\\\n& Losses & & " |
---|
444 | + "\\\\\n\\hline\n"; |
---|
445 | |
---|
446 | ranking = Utils.sort(m_RankingDiff); |
---|
447 | for (i = getColCount() - 1; i >= 0; i--) { |
---|
448 | curr = ranking[i]; |
---|
449 | |
---|
450 | if (getColHidden(curr)) |
---|
451 | continue; |
---|
452 | |
---|
453 | result += "(" + (curr + 1) + ") & " |
---|
454 | + Utils.padLeft("" + m_RankingDiff[curr], width) |
---|
455 | + " & " + Utils.padLeft("" + m_RankingWins[curr], width) |
---|
456 | + " & " + Utils.padLeft("" + m_RankingLosses[curr], width) |
---|
457 | + "\\\\\n"; |
---|
458 | } |
---|
459 | |
---|
460 | result += "\\hline\n\\end{tabular} \\footnotesize \\par}\n\\end{table}"; |
---|
461 | |
---|
462 | return result; |
---|
463 | } |
---|
464 | |
---|
465 | /** |
---|
466 | * Returns the revision string. |
---|
467 | * |
---|
468 | * @return the revision |
---|
469 | */ |
---|
470 | public String getRevision() { |
---|
471 | return RevisionUtils.extract("$Revision: 5346 $"); |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * for testing only. |
---|
476 | * |
---|
477 | * @param args ignored |
---|
478 | */ |
---|
479 | public static void main(String[] args) { |
---|
480 | ResultMatrix matrix; |
---|
481 | int i; |
---|
482 | int n; |
---|
483 | |
---|
484 | matrix = new ResultMatrixLatex(3, 3); |
---|
485 | |
---|
486 | // set header |
---|
487 | matrix.addHeader("header1", "value1"); |
---|
488 | matrix.addHeader("header2", "value2"); |
---|
489 | matrix.addHeader("header2", "value3"); |
---|
490 | |
---|
491 | // set values |
---|
492 | for (i = 0; i < matrix.getRowCount(); i++) { |
---|
493 | for (n = 0; n < matrix.getColCount(); n++) { |
---|
494 | matrix.setMean(n, i, (i+1)*n); |
---|
495 | matrix.setStdDev(n, i, ((double) (i+1)*n) / 100); |
---|
496 | if (i == n) { |
---|
497 | if (i % 2 == 1) |
---|
498 | matrix.setSignificance(n, i, SIGNIFICANCE_WIN); |
---|
499 | else |
---|
500 | matrix.setSignificance(n, i, SIGNIFICANCE_LOSS); |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | System.out.println("\n\n--> " + matrix.getDisplayName()); |
---|
506 | |
---|
507 | System.out.println("\n1. complete\n"); |
---|
508 | System.out.println(matrix.toStringHeader() + "\n"); |
---|
509 | System.out.println(matrix.toStringMatrix() + "\n"); |
---|
510 | System.out.println(matrix.toStringKey()); |
---|
511 | |
---|
512 | System.out.println("\n2. complete with std deviations\n"); |
---|
513 | matrix.setShowStdDev(true); |
---|
514 | System.out.println(matrix.toStringMatrix()); |
---|
515 | |
---|
516 | System.out.println("\n3. cols numbered\n"); |
---|
517 | matrix.setPrintColNames(false); |
---|
518 | System.out.println(matrix.toStringMatrix()); |
---|
519 | |
---|
520 | System.out.println("\n4. second col missing\n"); |
---|
521 | matrix.setColHidden(1, true); |
---|
522 | System.out.println(matrix.toStringMatrix()); |
---|
523 | |
---|
524 | System.out.println("\n5. last row missing, rows numbered too\n"); |
---|
525 | matrix.setRowHidden(2, true); |
---|
526 | matrix.setPrintRowNames(false); |
---|
527 | System.out.println(matrix.toStringMatrix()); |
---|
528 | |
---|
529 | System.out.println("\n6. mean prec to 3\n"); |
---|
530 | matrix.setMeanPrec(3); |
---|
531 | matrix.setPrintRowNames(false); |
---|
532 | System.out.println(matrix.toStringMatrix()); |
---|
533 | } |
---|
534 | } |
---|