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 | * ResultMatrixSignificance.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 | |
---|
27 | /** |
---|
28 | <!-- globalinfo-start --> |
---|
29 | * Only outputs the significance indicators. Can be used for spotting patterns. |
---|
30 | * <p/> |
---|
31 | <!-- globalinfo-end --> |
---|
32 | * |
---|
33 | <!-- options-start --> |
---|
34 | * Valid options are: <p/> |
---|
35 | * |
---|
36 | * <pre> -mean-prec <int> |
---|
37 | * The number of decimals after the decimal point for the mean. |
---|
38 | * (default: 2)</pre> |
---|
39 | * |
---|
40 | * <pre> -stddev-prec <int> |
---|
41 | * The number of decimals after the decimal point for the mean. |
---|
42 | * (default: 2)</pre> |
---|
43 | * |
---|
44 | * <pre> -col-name-width <int> |
---|
45 | * The maximum width for the column names (0 = optimal). |
---|
46 | * (default: 0)</pre> |
---|
47 | * |
---|
48 | * <pre> -row-name-width <int> |
---|
49 | * The maximum width for the row names (0 = optimal). |
---|
50 | * (default: 40)</pre> |
---|
51 | * |
---|
52 | * <pre> -mean-width <int> |
---|
53 | * The width of the mean (0 = optimal). |
---|
54 | * (default: 0)</pre> |
---|
55 | * |
---|
56 | * <pre> -stddev-width <int> |
---|
57 | * The width of the standard deviation (0 = optimal). |
---|
58 | * (default: 0)</pre> |
---|
59 | * |
---|
60 | * <pre> -sig-width <int> |
---|
61 | * The width of the significance indicator (0 = optimal). |
---|
62 | * (default: 0)</pre> |
---|
63 | * |
---|
64 | * <pre> -count-width <int> |
---|
65 | * The width of the counts (0 = optimal). |
---|
66 | * (default: 0)</pre> |
---|
67 | * |
---|
68 | * <pre> -show-stddev |
---|
69 | * Whether to display the standard deviation column. |
---|
70 | * (default: no)</pre> |
---|
71 | * |
---|
72 | * <pre> -show-avg |
---|
73 | * Whether to show the row with averages. |
---|
74 | * (default: no)</pre> |
---|
75 | * |
---|
76 | * <pre> -remove-filter |
---|
77 | * Whether to remove the classname package prefixes from the |
---|
78 | * filter names in datasets. |
---|
79 | * (default: no)</pre> |
---|
80 | * |
---|
81 | * <pre> -print-col-names |
---|
82 | * Whether to output column names or just numbers representing them. |
---|
83 | * (default: no)</pre> |
---|
84 | * |
---|
85 | * <pre> -print-row-names |
---|
86 | * Whether to output row names or just numbers representing them. |
---|
87 | * (default: no)</pre> |
---|
88 | * |
---|
89 | * <pre> -enum-col-names |
---|
90 | * Whether to enumerate the column names (prefixing them with |
---|
91 | * '(x)', with 'x' being the index). |
---|
92 | * (default: no)</pre> |
---|
93 | * |
---|
94 | * <pre> -enum-row-names |
---|
95 | * Whether to enumerate the row names (prefixing them with |
---|
96 | * '(x)', with 'x' being the index). |
---|
97 | * (default: no)</pre> |
---|
98 | * |
---|
99 | <!-- options-end --> |
---|
100 | * |
---|
101 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
102 | * @version $Revision: 5346 $ |
---|
103 | */ |
---|
104 | public class ResultMatrixSignificance |
---|
105 | extends ResultMatrix { |
---|
106 | |
---|
107 | /** for serialization. */ |
---|
108 | private static final long serialVersionUID = -1280545644109764206L; |
---|
109 | |
---|
110 | /** |
---|
111 | * initializes the matrix as 1x1 matrix. |
---|
112 | */ |
---|
113 | public ResultMatrixSignificance() { |
---|
114 | this(1, 1); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * initializes the matrix with the given dimensions. |
---|
119 | * |
---|
120 | * @param cols the number of columns |
---|
121 | * @param rows the number of rows |
---|
122 | */ |
---|
123 | public ResultMatrixSignificance(int cols, int rows) { |
---|
124 | super(cols, rows); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * initializes the matrix with the values from the given matrix. |
---|
129 | * |
---|
130 | * @param matrix the matrix to get the values from |
---|
131 | */ |
---|
132 | public ResultMatrixSignificance(ResultMatrix matrix) { |
---|
133 | super(matrix); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Returns a string describing the matrix. |
---|
138 | * |
---|
139 | * @return a description suitable for |
---|
140 | * displaying in the experimenter gui |
---|
141 | */ |
---|
142 | public String globalInfo() { |
---|
143 | return "Only outputs the significance indicators. Can be used for spotting patterns."; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * returns the name of the output format. |
---|
148 | * |
---|
149 | * @return the display name |
---|
150 | */ |
---|
151 | public String getDisplayName() { |
---|
152 | return "Significance only"; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * returns the default of whether column names or numbers instead are printed. |
---|
157 | * |
---|
158 | * @return true if names instead of numbers are printed |
---|
159 | */ |
---|
160 | public boolean getDefaultPrintColNames() { |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * returns the default width for the row names. |
---|
166 | * |
---|
167 | * @return the width |
---|
168 | */ |
---|
169 | public int getDefaultRowNameWidth() { |
---|
170 | return 40; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * returns the default of whether std deviations are displayed or not. |
---|
175 | * |
---|
176 | * @return true if the std deviations are displayed |
---|
177 | */ |
---|
178 | public boolean getDefaultShowStdDev() { |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * sets whether to display the std deviations or not - always false! |
---|
184 | * |
---|
185 | * @param show ignored |
---|
186 | */ |
---|
187 | public void setShowStdDev(boolean show) { |
---|
188 | // ignore |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * returns the matrix as plain text. |
---|
193 | * |
---|
194 | * @return the matrix |
---|
195 | */ |
---|
196 | public String toStringMatrix() { |
---|
197 | StringBuffer result; |
---|
198 | String[][] cells; |
---|
199 | int i; |
---|
200 | int n; |
---|
201 | int nameWidth; |
---|
202 | String line; |
---|
203 | String colStr; |
---|
204 | int rows; |
---|
205 | |
---|
206 | result = new StringBuffer(); |
---|
207 | cells = toArray(); |
---|
208 | |
---|
209 | // pad names |
---|
210 | nameWidth = getColSize(cells, 0); |
---|
211 | for (i = 0; i < cells.length - 1; i++) |
---|
212 | cells[i][0] = padString(cells[i][0], nameWidth); |
---|
213 | |
---|
214 | // determine number of displayed rows |
---|
215 | rows = cells.length - 1; |
---|
216 | if (getShowAverage()) |
---|
217 | rows--; |
---|
218 | |
---|
219 | for (i = 0; i < rows; i++) { |
---|
220 | line = ""; |
---|
221 | colStr = ""; |
---|
222 | |
---|
223 | for (n = 0; n < cells[i].length; n++) { |
---|
224 | // the header of the column |
---|
225 | if (isMean(n) || isRowName(n)) |
---|
226 | colStr = cells[0][n]; |
---|
227 | |
---|
228 | if ( (n > 1) && (!isSignificance(n)) ) |
---|
229 | continue; |
---|
230 | |
---|
231 | // padding between cols |
---|
232 | if (n > 0) |
---|
233 | line += " "; |
---|
234 | // padding for "(" below dataset line |
---|
235 | if ( (i > 0) && (n > 1) ) |
---|
236 | line += " "; |
---|
237 | |
---|
238 | if (i == 0) { |
---|
239 | line += colStr; |
---|
240 | } |
---|
241 | else { |
---|
242 | if (n == 0) { |
---|
243 | line += cells[i][n]; |
---|
244 | } |
---|
245 | else if (n == 1) { |
---|
246 | line += colStr.replaceAll(".", " "); // base column has no significance! |
---|
247 | } |
---|
248 | else { |
---|
249 | line += cells[i][n]; |
---|
250 | // add blanks dep. on length of # |
---|
251 | line += colStr.replaceAll(".", " ").substring(2); |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | result.append(line + "\n"); |
---|
256 | |
---|
257 | // separator line |
---|
258 | if (i == 0) |
---|
259 | result.append(line.replaceAll(".", "-") + "\n"); |
---|
260 | } |
---|
261 | |
---|
262 | return result.toString(); |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * returns the header of the matrix as a string. |
---|
267 | * |
---|
268 | * @return the header |
---|
269 | * @see #m_HeaderKeys |
---|
270 | * @see #m_HeaderValues |
---|
271 | */ |
---|
272 | public String toStringHeader() { |
---|
273 | return new ResultMatrixPlainText(this).toStringHeader(); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * returns returns a key for all the col names, for better readability if |
---|
278 | * the names got cut off. |
---|
279 | * |
---|
280 | * @return the key |
---|
281 | */ |
---|
282 | public String toStringKey() { |
---|
283 | return new ResultMatrixPlainText(this).toStringKey(); |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * returns the summary as string. |
---|
288 | * |
---|
289 | * @return the summary |
---|
290 | */ |
---|
291 | public String toStringSummary() { |
---|
292 | return new ResultMatrixPlainText(this).toStringSummary(); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * returns the ranking in a string representation. |
---|
297 | * |
---|
298 | * @return the ranking |
---|
299 | */ |
---|
300 | public String toStringRanking() { |
---|
301 | return new ResultMatrixPlainText(this).toStringRanking(); |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Returns the revision string. |
---|
306 | * |
---|
307 | * @return the revision |
---|
308 | */ |
---|
309 | public String getRevision() { |
---|
310 | return RevisionUtils.extract("$Revision: 5346 $"); |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * for testing only. |
---|
315 | * |
---|
316 | * @param args ignored |
---|
317 | */ |
---|
318 | public static void main(String[] args) { |
---|
319 | ResultMatrix matrix; |
---|
320 | int i; |
---|
321 | int n; |
---|
322 | |
---|
323 | matrix = new ResultMatrixSignificance(3, 3); |
---|
324 | |
---|
325 | // set header |
---|
326 | matrix.addHeader("header1", "value1"); |
---|
327 | matrix.addHeader("header2", "value2"); |
---|
328 | matrix.addHeader("header2", "value3"); |
---|
329 | |
---|
330 | // set values |
---|
331 | for (i = 0; i < matrix.getRowCount(); i++) { |
---|
332 | for (n = 0; n < matrix.getColCount(); n++) { |
---|
333 | matrix.setMean(n, i, (i+1)*n); |
---|
334 | matrix.setStdDev(n, i, ((double) (i+1)*n) / 100); |
---|
335 | if (i == n) { |
---|
336 | if (i % 2 == 1) |
---|
337 | matrix.setSignificance(n, i, SIGNIFICANCE_WIN); |
---|
338 | else |
---|
339 | matrix.setSignificance(n, i, SIGNIFICANCE_LOSS); |
---|
340 | } |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | System.out.println("\n\n--> " + matrix.getDisplayName()); |
---|
345 | |
---|
346 | System.out.println("\n1. complete\n"); |
---|
347 | System.out.println(matrix.toStringHeader() + "\n"); |
---|
348 | System.out.println(matrix.toStringMatrix() + "\n"); |
---|
349 | System.out.println(matrix.toStringKey()); |
---|
350 | |
---|
351 | System.out.println("\n2. complete with std deviations\n"); |
---|
352 | matrix.setShowStdDev(true); |
---|
353 | System.out.println(matrix.toStringMatrix()); |
---|
354 | |
---|
355 | System.out.println("\n3. cols numbered\n"); |
---|
356 | matrix.setPrintColNames(false); |
---|
357 | System.out.println(matrix.toStringMatrix()); |
---|
358 | |
---|
359 | System.out.println("\n4. second col missing\n"); |
---|
360 | matrix.setColHidden(1, true); |
---|
361 | System.out.println(matrix.toStringMatrix()); |
---|
362 | |
---|
363 | System.out.println("\n5. last row missing, rows numbered too\n"); |
---|
364 | matrix.setRowHidden(2, true); |
---|
365 | matrix.setPrintRowNames(false); |
---|
366 | System.out.println(matrix.toStringMatrix()); |
---|
367 | |
---|
368 | System.out.println("\n6. mean prec to 3\n"); |
---|
369 | matrix.setMeanPrec(3); |
---|
370 | matrix.setPrintRowNames(false); |
---|
371 | System.out.println(matrix.toStringMatrix()); |
---|
372 | } |
---|
373 | } |
---|