001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * ResultMatrixCSV.java
019: * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.experiment;
024:
025: import weka.core.Utils;
026:
027: /**
028: * This matrix is a container for the datasets and classifier setups and
029: * their statistics. It outputs the data in CSV format.
030: *
031: *
032: * @author FracPete (fracpete at waikato dot ac dot nz)
033: * @version $Revision: 1.3 $
034: */
035: public class ResultMatrixCSV extends ResultMatrix {
036:
037: /** for serialization */
038: private static final long serialVersionUID = -171838863135042743L;
039:
040: /**
041: * initializes the matrix as 1x1 matrix
042: */
043: public ResultMatrixCSV() {
044: this (1, 1);
045: }
046:
047: /**
048: * initializes the matrix with the given dimensions
049: */
050: public ResultMatrixCSV(int cols, int rows) {
051: super (cols, rows);
052: }
053:
054: /**
055: * initializes the matrix with the values from the given matrix
056: * @param matrix the matrix to get the values from
057: */
058: public ResultMatrixCSV(ResultMatrix matrix) {
059: super (matrix);
060: }
061:
062: /**
063: * returns the name of the output format
064: */
065: public String getDisplayName() {
066: return "CSV";
067: }
068:
069: /**
070: * removes the stored data but retains the dimensions of the matrix
071: */
072: public void clear() {
073: super .clear();
074: setRowNameWidth(25);
075: setPrintColNames(false);
076: setEnumerateColNames(true);
077: LEFT_PARENTHESES = "[";
078: RIGHT_PARENTHESES = "]";
079: }
080:
081: /**
082: * returns the header of the matrix as a string
083: * @see #m_HeaderKeys
084: * @see #m_HeaderValues
085: */
086: public String toStringHeader() {
087: return new ResultMatrixPlainText(this ).toStringHeader();
088: }
089:
090: /**
091: * returns the matrix in CSV format
092: */
093: public String toStringMatrix() {
094: StringBuffer result;
095: String[][] cells;
096: int i;
097: int n;
098:
099: result = new StringBuffer();
100: cells = toArray();
101:
102: for (i = 0; i < cells.length; i++) {
103: for (n = 0; n < cells[i].length; n++) {
104: if (n > 0)
105: result.append(",");
106: result.append(Utils.quote(cells[i][n]));
107: }
108: result.append("\n");
109: }
110:
111: return result.toString();
112: }
113:
114: /**
115: * returns returns a key for all the col names, for better readability if
116: * the names got cut off
117: */
118: public String toStringKey() {
119: String result;
120: int i;
121:
122: result = "Key,\n";
123: for (i = 0; i < getColCount(); i++) {
124: if (getColHidden(i))
125: continue;
126:
127: result += LEFT_PARENTHESES + (i + 1) + RIGHT_PARENTHESES
128: + ","
129: + Utils.quote(removeFilterName(m_ColNames[i]))
130: + "\n";
131: }
132:
133: return result;
134: }
135:
136: /**
137: * returns the summary as string
138: */
139: public String toStringSummary() {
140: String result;
141: String titles;
142: int resultsetLength;
143: int i;
144: int j;
145: String line;
146:
147: if (m_NonSigWins == null)
148: return "-summary data not set-";
149:
150: result = "";
151: titles = "";
152: resultsetLength = 1 + Math.max(
153: (int) (Math.log(getColCount()) / Math.log(10)),
154: (int) (Math.log(getRowCount()) / Math.log(10)));
155:
156: for (i = 0; i < getColCount(); i++) {
157: if (getColHidden(i))
158: continue;
159: if (!titles.equals(""))
160: titles += ",";
161: titles += getSummaryTitle(i);
162: }
163: result += titles
164: + ",'(No. of datasets where [col] >> [row])'\n";
165:
166: for (i = 0; i < getColCount(); i++) {
167: if (getColHidden(i))
168: continue;
169:
170: line = "";
171: for (j = 0; j < getColCount(); j++) {
172: if (getColHidden(j))
173: continue;
174:
175: if (!line.equals(""))
176: line += ",";
177:
178: if (j == i)
179: line += "-";
180: else
181: line += m_NonSigWins[i][j] + " (" + m_Wins[i][j]
182: + ")";
183: }
184:
185: result += line + "," + getSummaryTitle(i) + " = "
186: + removeFilterName(m_ColNames[i]) + '\n';
187: }
188:
189: return result;
190: }
191:
192: /**
193: * returns the ranking in a string representation
194: */
195: public String toStringRanking() {
196: int biggest;
197: int width;
198: String result;
199: int[] ranking;
200: int i;
201: int curr;
202:
203: if (m_RankingWins == null)
204: return "-ranking data not set-";
205:
206: biggest = Math.max(
207: m_RankingWins[Utils.maxIndex(m_RankingWins)],
208: m_RankingLosses[Utils.maxIndex(m_RankingLosses)]);
209: width = Math.max(2 + (int) (Math.log(biggest) / Math.log(10)),
210: ">-<".length());
211: result = ">-<,>,<,Resultset\n";
212:
213: ranking = Utils.sort(m_RankingDiff);
214:
215: for (i = getColCount() - 1; i >= 0; i--) {
216: curr = ranking[i];
217:
218: if (getColHidden(curr))
219: continue;
220:
221: result += m_RankingDiff[curr] + "," + m_RankingWins[curr]
222: + "," + m_RankingLosses[curr] + ","
223: + removeFilterName(m_ColNames[curr]) + "\n";
224: }
225:
226: return result;
227: }
228:
229: /**
230: * for testing only
231: */
232: public static void main(String[] args) {
233: ResultMatrix matrix;
234: int i;
235: int n;
236:
237: matrix = new ResultMatrixCSV(3, 3);
238:
239: // set header
240: matrix.addHeader("header1", "value1");
241: matrix.addHeader("header2", "value2");
242: matrix.addHeader("header2", "value3");
243:
244: // set values
245: for (i = 0; i < matrix.getRowCount(); i++) {
246: for (n = 0; n < matrix.getColCount(); n++) {
247: matrix.setMean(n, i, (i + 1) * n);
248: matrix.setStdDev(n, i, ((double) (i + 1) * n) / 100);
249: if (i == n) {
250: if (i % 2 == 1)
251: matrix.setSignificance(n, i, SIGNIFICANCE_WIN);
252: else
253: matrix.setSignificance(n, i, SIGNIFICANCE_LOSS);
254: }
255: }
256: }
257:
258: System.out.println("\n\n--> " + matrix.getDisplayName());
259:
260: System.out.println("\n1. complete\n");
261: System.out.println(matrix.toStringHeader() + "\n");
262: System.out.println(matrix.toStringMatrix() + "\n");
263: System.out.println(matrix.toStringKey());
264:
265: System.out.println("\n2. complete with std deviations\n");
266: matrix.setShowStdDev(true);
267: System.out.println(matrix.toStringMatrix());
268:
269: System.out.println("\n3. cols numbered\n");
270: matrix.setPrintColNames(false);
271: System.out.println(matrix.toStringMatrix());
272:
273: System.out.println("\n4. second col missing\n");
274: matrix.setColHidden(1, true);
275: System.out.println(matrix.toStringMatrix());
276:
277: System.out
278: .println("\n5. last row missing, rows numbered too\n");
279: matrix.setRowHidden(2, true);
280: matrix.setPrintRowNames(false);
281: System.out.println(matrix.toStringMatrix());
282:
283: System.out.println("\n6. mean prec to 3\n");
284: matrix.setMeanPrec(3);
285: matrix.setPrintRowNames(false);
286: System.out.println(matrix.toStringMatrix());
287: }
288: }
|