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 | * ResultMatrixCSV.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 in CSV ('comma-separated values') format. |
---|
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 ResultMatrixCSV |
---|
106 | extends ResultMatrix { |
---|
107 | |
---|
108 | /** for serialization. */ |
---|
109 | private static final long serialVersionUID = -171838863135042743L; |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes the matrix as 1x1 matrix. |
---|
113 | */ |
---|
114 | public ResultMatrixCSV() { |
---|
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 ResultMatrixCSV(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 ResultMatrixCSV(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 in CSV ('comma-separated values') format."; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * returns the name of the output format. |
---|
149 | * |
---|
150 | * @return the display name |
---|
151 | */ |
---|
152 | public String getDisplayName() { |
---|
153 | return "CSV"; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * removes the stored data but retains the dimensions of the matrix. |
---|
158 | */ |
---|
159 | public void clear() { |
---|
160 | super.clear(); |
---|
161 | LEFT_PARENTHESES = "["; |
---|
162 | RIGHT_PARENTHESES = "]"; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * returns the default width for the row names. |
---|
167 | * |
---|
168 | * @return the width |
---|
169 | */ |
---|
170 | public int getDefaultRowNameWidth() { |
---|
171 | return 25; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * returns the default of whether column names or numbers instead are printed. |
---|
176 | * |
---|
177 | * @return true if names instead of numbers are printed |
---|
178 | */ |
---|
179 | public boolean getDefaultPrintColNames() { |
---|
180 | return false; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * returns the default of whether column names are prefixed with the index. |
---|
185 | * |
---|
186 | * @return true if the names are prefixed |
---|
187 | */ |
---|
188 | public boolean getDefaultEnumerateColNames() { |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * returns the header of the matrix as a string. |
---|
194 | * |
---|
195 | * @return the header |
---|
196 | * @see #m_HeaderKeys |
---|
197 | * @see #m_HeaderValues |
---|
198 | */ |
---|
199 | public String toStringHeader() { |
---|
200 | return new ResultMatrixPlainText(this).toStringHeader(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * returns the matrix in CSV format. |
---|
205 | * |
---|
206 | * @return the matrix as string |
---|
207 | */ |
---|
208 | public String toStringMatrix() { |
---|
209 | StringBuffer result; |
---|
210 | String[][] cells; |
---|
211 | int i; |
---|
212 | int n; |
---|
213 | |
---|
214 | result = new StringBuffer(); |
---|
215 | cells = toArray(); |
---|
216 | |
---|
217 | for (i = 0; i < cells.length; i++) { |
---|
218 | for (n = 0; n < cells[i].length; n++) { |
---|
219 | if (n > 0) |
---|
220 | result.append(","); |
---|
221 | result.append(Utils.quote(cells[i][n])); |
---|
222 | } |
---|
223 | result.append("\n"); |
---|
224 | } |
---|
225 | |
---|
226 | return result.toString(); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * returns a key for all the col names, for better readability if |
---|
231 | * the names got cut off. |
---|
232 | * |
---|
233 | * @return the key |
---|
234 | */ |
---|
235 | public String toStringKey() { |
---|
236 | String result; |
---|
237 | int i; |
---|
238 | |
---|
239 | result = "Key,\n"; |
---|
240 | for (i = 0; i < getColCount(); i++) { |
---|
241 | if (getColHidden(i)) |
---|
242 | continue; |
---|
243 | |
---|
244 | result += LEFT_PARENTHESES + (i+1) + RIGHT_PARENTHESES |
---|
245 | + "," + Utils.quote(removeFilterName(m_ColNames[i])) + "\n"; |
---|
246 | } |
---|
247 | |
---|
248 | return result; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * returns the summary as string. |
---|
253 | * |
---|
254 | * @return the summary |
---|
255 | */ |
---|
256 | public String toStringSummary() { |
---|
257 | String result; |
---|
258 | String titles; |
---|
259 | int i; |
---|
260 | int j; |
---|
261 | String line; |
---|
262 | |
---|
263 | if (m_NonSigWins == null) |
---|
264 | return "-summary data not set-"; |
---|
265 | |
---|
266 | result = ""; |
---|
267 | titles = ""; |
---|
268 | |
---|
269 | for (i = 0; i < getColCount(); i++) { |
---|
270 | if (getColHidden(i)) |
---|
271 | continue; |
---|
272 | if (!titles.equals("")) |
---|
273 | titles += ","; |
---|
274 | titles += getSummaryTitle(i); |
---|
275 | } |
---|
276 | result += titles + ",'(No. of datasets where [col] >> [row])'\n"; |
---|
277 | |
---|
278 | for (i = 0; i < getColCount(); i++) { |
---|
279 | if (getColHidden(i)) |
---|
280 | continue; |
---|
281 | |
---|
282 | line = ""; |
---|
283 | for (j = 0; j < getColCount(); j++) { |
---|
284 | if (getColHidden(j)) |
---|
285 | continue; |
---|
286 | |
---|
287 | if (!line.equals("")) |
---|
288 | line += ","; |
---|
289 | |
---|
290 | if (j == i) |
---|
291 | line += "-"; |
---|
292 | else |
---|
293 | line += m_NonSigWins[i][j] |
---|
294 | + " (" + m_Wins[i][j] + ")"; |
---|
295 | } |
---|
296 | |
---|
297 | result += line + "," + getSummaryTitle(i) + " = " + removeFilterName(m_ColNames[i]) + '\n'; |
---|
298 | } |
---|
299 | |
---|
300 | return result; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * returns the ranking in a string representation. |
---|
305 | * |
---|
306 | * @return the ranking |
---|
307 | */ |
---|
308 | public String toStringRanking() { |
---|
309 | String result; |
---|
310 | int[] ranking; |
---|
311 | int i; |
---|
312 | int curr; |
---|
313 | |
---|
314 | if (m_RankingWins == null) |
---|
315 | return "-ranking data not set-"; |
---|
316 | |
---|
317 | result = ">-<,>,<,Resultset\n"; |
---|
318 | |
---|
319 | ranking = Utils.sort(m_RankingDiff); |
---|
320 | |
---|
321 | for (i = getColCount() - 1; i >= 0; i--) { |
---|
322 | curr = ranking[i]; |
---|
323 | |
---|
324 | if (getColHidden(curr)) |
---|
325 | continue; |
---|
326 | |
---|
327 | result += m_RankingDiff[curr] + "," |
---|
328 | + m_RankingWins[curr] + "," |
---|
329 | + m_RankingLosses[curr] + "," |
---|
330 | + removeFilterName(m_ColNames[curr]) + "\n"; |
---|
331 | } |
---|
332 | |
---|
333 | return result; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * Returns the revision string. |
---|
338 | * |
---|
339 | * @return the revision |
---|
340 | */ |
---|
341 | public String getRevision() { |
---|
342 | return RevisionUtils.extract("$Revision: 5346 $"); |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * for testing only. |
---|
347 | * |
---|
348 | * @param args ignored |
---|
349 | */ |
---|
350 | public static void main(String[] args) { |
---|
351 | ResultMatrix matrix; |
---|
352 | int i; |
---|
353 | int n; |
---|
354 | |
---|
355 | matrix = new ResultMatrixCSV(3, 3); |
---|
356 | |
---|
357 | // set header |
---|
358 | matrix.addHeader("header1", "value1"); |
---|
359 | matrix.addHeader("header2", "value2"); |
---|
360 | matrix.addHeader("header2", "value3"); |
---|
361 | |
---|
362 | // set values |
---|
363 | for (i = 0; i < matrix.getRowCount(); i++) { |
---|
364 | for (n = 0; n < matrix.getColCount(); n++) { |
---|
365 | matrix.setMean(n, i, (i+1)*n); |
---|
366 | matrix.setStdDev(n, i, ((double) (i+1)*n) / 100); |
---|
367 | if (i == n) { |
---|
368 | if (i % 2 == 1) |
---|
369 | matrix.setSignificance(n, i, SIGNIFICANCE_WIN); |
---|
370 | else |
---|
371 | matrix.setSignificance(n, i, SIGNIFICANCE_LOSS); |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | System.out.println("\n\n--> " + matrix.getDisplayName()); |
---|
377 | |
---|
378 | System.out.println("\n1. complete\n"); |
---|
379 | System.out.println(matrix.toStringHeader() + "\n"); |
---|
380 | System.out.println(matrix.toStringMatrix() + "\n"); |
---|
381 | System.out.println(matrix.toStringKey()); |
---|
382 | |
---|
383 | System.out.println("\n2. complete with std deviations\n"); |
---|
384 | matrix.setShowStdDev(true); |
---|
385 | System.out.println(matrix.toStringMatrix()); |
---|
386 | |
---|
387 | System.out.println("\n3. cols numbered\n"); |
---|
388 | matrix.setPrintColNames(false); |
---|
389 | System.out.println(matrix.toStringMatrix()); |
---|
390 | |
---|
391 | System.out.println("\n4. second col missing\n"); |
---|
392 | matrix.setColHidden(1, true); |
---|
393 | System.out.println(matrix.toStringMatrix()); |
---|
394 | |
---|
395 | System.out.println("\n5. last row missing, rows numbered too\n"); |
---|
396 | matrix.setRowHidden(2, true); |
---|
397 | matrix.setPrintRowNames(false); |
---|
398 | System.out.println(matrix.toStringMatrix()); |
---|
399 | |
---|
400 | System.out.println("\n6. mean prec to 3\n"); |
---|
401 | matrix.setMeanPrec(3); |
---|
402 | matrix.setPrintRowNames(false); |
---|
403 | System.out.println(matrix.toStringMatrix()); |
---|
404 | } |
---|
405 | } |
---|