/* * RapidMiner * * Copyright (C) 2001-2008 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ package com.rapidminer.operator.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.List; import com.rapidminer.datatable.DataTable; import com.rapidminer.datatable.GnuPlotDataTableHandler; import com.rapidminer.operator.IOObject; import com.rapidminer.operator.Operator; import com.rapidminer.operator.OperatorDescription; import com.rapidminer.operator.OperatorException; import com.rapidminer.parameter.ParameterType; import com.rapidminer.parameter.ParameterTypeFile; import com.rapidminer.parameter.ParameterTypeString; import com.rapidminer.tools.Tools; /** * Writes the data generated by a ProcessLogOperator to a file in gnuplot * format. * * @author Simon Fischer, Ingo Mierswa * @version $Id: GNUPlotOperator.java,v 1.8 2008/07/07 07:06:37 ingomierswa Exp $ */ public class GNUPlotOperator extends Operator { /** The parameter name for "The gnuplot file." */ public static final String PARAMETER_OUTPUT_FILE = "output_file"; /** The parameter name for "The name of the process log operator which produced the data table." */ public static final String PARAMETER_NAME = "name"; /** The parameter name for "The title of the plot." */ public static final String PARAMETER_TITLE = "title"; /** The parameter name for "The values of the x-axis." */ public static final String PARAMETER_X_AXIS = "x_axis"; /** The parameter name for "The values of the y-axis (for 3d plots)." */ public static final String PARAMETER_Y_AXIS = "y_axis"; /** The parameter name for "A whitespace separated list of values which should be plotted." */ public static final String PARAMETER_VALUES = "values"; /** The parameter name for "Additional parameters for the gnuplot header." */ public static final String PARAMETER_ADDITIONAL_PARAMETERS = "additional_parameters"; public Class<?>[] getInputClasses() { return new Class[0]; } public Class<?>[] getOutputClasses() { return new Class[0]; } public GNUPlotOperator(OperatorDescription description) { super(description); } public IOObject[] apply() throws OperatorException { String dataTableName = getParameterAsString(PARAMETER_NAME); if (!getProcess().dataTableExists(dataTableName)) { logError("Data table with name '" + dataTableName + "' does not exist."); return new IOObject[0]; } DataTable dataTable = getProcess().getDataTable(dataTableName); String[] valueNames = getParameterAsString(PARAMETER_VALUES).split(" "); int[] values = new int[valueNames.length]; for (int i = 0; i < values.length; i++) { values[i] = dataTable.getColumnIndex(valueNames[i]); if (values[i] == -1) { logError(getName() + ": No data column with name '" + valueNames[i] + "' exists."); return new IOObject[0]; } } String xAxisName = getParameterAsString(PARAMETER_X_AXIS); int xAxis = dataTable.getColumnIndex(xAxisName); if (xAxis == -1) { logError("No data column with name '" + xAxisName + "' exists."); return new IOObject[0]; } String yAxisName = getParameterAsString(PARAMETER_Y_AXIS); int yAxis = -1; if (yAxisName != null) { yAxis = dataTable.getColumnIndex(yAxisName); if (yAxis == -1) { logError("No data column with name '" + yAxisName + "' exists."); return new IOObject[0]; } } String additional = ""; if (isParameterSet(PARAMETER_TITLE)) additional += "set title \"" + getParameterAsString(PARAMETER_TITLE) + "\"" + Tools.getLineSeparator(); if (isParameterSet(PARAMETER_ADDITIONAL_PARAMETERS)) additional += getParameterAsString(PARAMETER_ADDITIONAL_PARAMETERS); File file = getParameterAsFile(PARAMETER_OUTPUT_FILE); log("Creating gnuplot file '" + file + "'"); PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file)); GnuPlotDataTableHandler handler = new GnuPlotDataTableHandler(dataTable); handler.writeGNUPlot(out, xAxis, yAxis, values, "linespoints", additional, null); } catch (IOException e) { logError("Cannot create output file: " + e.getMessage()); return new IOObject[0]; } finally { if (out != null) { out.close(); } } return new IOObject[0]; } public List<ParameterType> getParameterTypes() { List<ParameterType> types = super.getParameterTypes(); types.add(new ParameterTypeFile(PARAMETER_OUTPUT_FILE, "The gnuplot file.", "gnu", false)); types.add(new ParameterTypeString(PARAMETER_NAME, "The name of the process log operator which produced the data table.", false)); types.add(new ParameterTypeString(PARAMETER_TITLE, "The title of the plot.", "Created by RapidMiner")); types.add(new ParameterTypeString(PARAMETER_X_AXIS, "The values of the x-axis.", false)); ParameterType type = new ParameterTypeString(PARAMETER_Y_AXIS, "The values of the y-axis (for 3d plots).", true); type.setExpert(false); types.add(type); types.add(new ParameterTypeString(PARAMETER_VALUES, "A whitespace separated list of values which should be plotted.", false)); types.add(new ParameterTypeString(PARAMETER_ADDITIONAL_PARAMETERS, "Additional parameters for the gnuplot header.", true)); return types; } }