📄 experimentlogoperator.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.operator;import edu.udo.cs.yale.operator.ResultObjectAdapter;import edu.udo.cs.yale.operator.parameter.*;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.gui.Plotter;import java.util.StringTokenizer;import java.util.List;import java.util.LinkedList;import java.util.Iterator;import java.io.FileWriter;import java.io.PrintWriter;import java.io.IOException;import java.awt.Component;/** * This operator records almost arbitrary data. It can written to a file which can be * read e.g. by gnuplot or plotted by the GUI.<br/> * * Parameters in the list <code>log</code> are interpreted as follows: The <var>key</var> gives * the name for the column name (e.g. for use in the plotter). The <var>value</var> specifies * where to retrieve the value from. This is best explained by an example: * <ul> * <li>If the value is <code>operator.Evaluator.value.absolute</code>, the ExperimentLogOperator looks * up the operator with the name <code>Evaluator</code>. If this operator is a * {@link edu.udo.cs.yale.operator.performance.PerformanceEvaluator}, it has a value named * <var>absolute</var> which gives the absoulte error of the last evaluation. This value * is queried by the ExperimentLogOperator</li> * <li>If the value is <code>operator.SVMLearner.parameter.C</code>, the ExperimentLogOperator looks * up the parameter <var>C</var> of the operator named <code>SVMLearner</code>.</li> * </ul> * Each time the ExperimentLogOperator is applied, all the values and parameters specified by the * list <var>log</var> are collected and stored in a data row. When the experiment finishes, the * operator writes the collected data rows to a file (if specified). In GUI mode a 2D plot is automatically * generated and displayed in the result viewer. * <br/> * Please refer to * section {@yale.ref sec:parameter_optimization|Advanced experiments/Parameter and performance analysis} * for an example application. * * @yale.xmlclass ExperimentLog * @yale.todo Use IOObjects for logging as well (e.g. {@link edu.udo.cs.yale.operator.performance.PerformanceVector}) * @author Simon * @version $Id: ExperimentLogOperator.java,v 2.12 2003/08/24 22:42:33 mierswa Exp $ */public class ExperimentLogOperator extends Operator { private static final Class[] OUTPUT_CLASSES = { }; private String[] valueNames; private Object fetchValue(String name) { StringTokenizer reader = new StringTokenizer(name, "."); String type = reader.nextToken(); if (type.equals("operator")) { String opName = reader.nextToken(); Operator operator = getExperiment().getOperator(opName); if (operator != null) { type = reader.nextToken(); if (type.equals("value")) { String valueName = reader.nextToken(); double value = operator.getValue(valueName); if (Double.isNaN(value)) { LogService.logMessage(getName()+": No such value in '"+name+"'", LogService.WARNING); return null; } return new Double(value); } else if (type.equals("parameter")) { String parameterName = reader.nextToken(); Object value = operator.getParameter(parameterName); if (value == null) { LogService.logMessage(getName()+": No such parameter in '"+name+"'", LogService.WARNING); return null; } return value; } else { LogService.logMessage(getName()+": Unknown token '"+type+"' in '"+name+"'",LogService.WARNING); return null; } } else { LogService.logMessage(getName()+": Unknown operator '"+opName+"' in '"+name+"'",LogService.WARNING); return null; } } else { LogService.logMessage(getName()+": Unknown token '"+type+"' in '"+name+"'", LogService.WARNING); return null; } } public void experimentStarts() { List parameters = getParameterList("log"); String columnNames[] = new String[parameters.size()]; valueNames = new String[parameters.size()]; Iterator i = parameters.iterator(); int j = 0; while (i.hasNext()) { Object[] parameter = (Object[])i.next(); columnNames[j] = (String)parameter[0]; valueNames[j] = (String)parameter[1]; j++; } getExperiment().getStatistics(getName()).init(columnNames); } public Class[] getInputClasses() { return new Class[0]; } public Class[] getOutputClasses() { return OUTPUT_CLASSES; } public IOObject[] apply() { Object[] row = new Object[valueNames.length]; for (int i = 0; i < valueNames.length; i++) { row[i] = fetchValue(valueNames[i]); } getExperiment().getStatistics(getName()).add(row); return new IOObject[] {}; } public void experimentFinished() { super.experimentFinished(); String filename = getParameterAsString("filename"); if (filename != null) { LogService.logMessage(getName()+": writing data to '"+filename+"'", LogService.INIT); try { PrintWriter out = new PrintWriter(new FileWriter(getExperiment().resolveFileName(filename))); getExperiment().getStatistics(getName()).write(out); out.close(); } catch (IOException e) { LogService.logMessage(getName() + ": Could not write to file ["+filename+"]", LogService.ERROR); } } } public List getParameterTypes() { List types = super.getParameterTypes(); types.add(new ParameterTypeFile("filename", "File to save the data to.", true)); types.add(new ParameterTypeList("log", "List of key value pairs where the key is the column name and the value specifies the experiment value to log.", new ParameterTypeValue("column_name", "operator.OPERATORNAME.[value|parameter].VALUE_NAME"))); return types; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -