📄 statistics.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;import edu.udo.cs.yale.gui.SwingTools;import edu.udo.cs.yale.tools.ParameterService;import edu.udo.cs.yale.tools.Tools;import javax.swing.Action;import javax.swing.AbstractAction;import javax.imageio.ImageIO;import java.awt.Image;import javax.swing.Icon;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.awt.event.ActionEvent;import java.awt.Component;import java.awt.Color;import java.text.DateFormat;import java.util.Date;import java.util.List;import java.util.LinkedList;import java.util.Iterator;import java.util.Comparator;import java.util.Collection;import java.util.SortedSet;import java.util.TreeSet;import java.io.*;/** A data list that contains Object arrays that record experiment results. Instances * of this class are automatically created by {@link Experiment#getStatistics()} and are * used mainly by the {@link edu.udo.cs.yale.operator.ExperimentLogOperator}. * * @version $Id: Statistics.java,v 2.7 2003/09/04 15:57:11 fischer Exp $ */public class Statistics { private static final String EXECUTABLE = System.getProperty("yale.tools.gnuplot.command"); private List data = new LinkedList(); private String name; private List listenerList = new LinkedList(); private String[] columns; public final Action SAVE_ACTION = new AbstractAction("Save...") { { putValue(SHORT_DESCRIPTION, "Save the statistics to disk."); } public void actionPerformed(ActionEvent e) { File file = SwingTools.chooseFile(null, null, false); try { if (file != null) { PrintWriter out = new PrintWriter(new FileWriter(file)); write(out); out.close(); } } catch (Exception ex) { SwingTools.showErrorMessage("Cannot write to file '"+file+"'", ex); } } }; public Statistics(String name) { this.name = name; } public void init(String[] columns) { data.clear(); this.columns = columns; fireEvent(); } public String getColumnName(int i) { return columns[i]; } public int getColumnIndex(String name) { for (int i = 0; i < columns.length; i++) { if (columns[i].equals(name)) return i; } return -1; } public int getNumberOfColumns() { return columns.length; } public String[] getColumnNames() { return columns; } public String getName() { return name; } public void add(Object[] row) { data.add(row); fireEvent(); } public Iterator iterator() { return data.iterator(); } public int getNumberOfRows() { return data.size(); } public void addStatisticsListener(StatisticsListener statisticsListener) { listenerList.add(statisticsListener); } public void removeStatisticsListener(StatisticsListener statisticsListener) { listenerList.remove(statisticsListener); } private void fireEvent() { Iterator i = listenerList.iterator(); while (i.hasNext()) { ((StatisticsListener)i.next()).statisticsUpdated(this); } } public void write(PrintWriter out) throws IOException { out.println("# Generated by "+ getName() + "["+getClass().getName()+"]"); for (int j = 0; j < getNumberOfColumns(); j++) { out.print((j != 0 ? "\t" : "# ") + getColumnName(j)); } out.println(); Iterator i = iterator(); while (i.hasNext()) { Object[] row = (Object[])i.next(); for (int j = 0; j < row.length; j++) { out.print((j != 0 ? "\t" : "") + row[j]); } out.println(); } out.flush(); } private static final Comparator ROW_COMPARATOR = new Comparator() { public int compare(Object o1, Object o2) { double[] row1 = (double[])o1; double[] row2 = (double[])o2; for (int i = 0; i < row1.length; i++) { if (row1[i] < row2[i]) return -1; else if (row1[i] > row2[i]) return +1; } return 0; }; }; private void write2DGNUPlotData(PrintStream out, int x, int y) { Collection plot = new TreeSet(ROW_COMPARATOR); Iterator i = data.iterator(); while (i.hasNext()) { Object[] row = (Object[])i.next(); if ((row[x] instanceof Double) && (row[y] instanceof Double)) { plot.add(new double[] {((Double)row[x]).doubleValue(), ((Double)row[y]).doubleValue()}); } } Iterator j = plot.iterator(); while (j.hasNext()) { double[] row = (double[])j.next(); out.println(row[0]+"\t"+row[1]); } } public void write3DGNUPlotData(PrintStream out, int x, int y, int z) { Collection plot = new TreeSet(ROW_COMPARATOR); Iterator i = data.iterator(); while (i.hasNext()) { Object[] row = (Object[])i.next(); if ((row[x] instanceof Double) && (row[y] instanceof Double) && (row[2] instanceof Double)) { plot.add(new double[] {((Double)row[x]).doubleValue(), ((Double)row[y]).doubleValue(), ((Double)row[z]).doubleValue()}); } } double oldX = Double.NaN; Iterator j = plot.iterator(); while (j.hasNext()) { double[] row = (double[])j.next(); if ((!Double.isNaN(oldX)) && (row[0] != oldX)) { out.println(); } out.println(row[0]+"\t"+row[1]+"\t"+row[2]); oldX = row[0]; } } private void writeGNUPlotHeader(PrintStream out, String xAxis, String yAxis, String zAxis, String additionalCommands, String terminal) { out.println("#!"+EXECUTABLE); out.println("# Generated by "+getClass() + " on "+DateFormat.getDateTimeInstance().format(new Date())); if (xAxis != null) out.println("set xlabel \""+xAxis+"\""); if (yAxis != null) out.println("set ylabel \""+yAxis+"\""); if (zAxis != null) out.println("set zlabel \""+zAxis+"\""); if (additionalCommands != null) out.println(additionalCommands); if (terminal != null) out.println("set terminal "+terminal); } public void writeGNUPlot(PrintStream out, int x, int y, int[] z, String additionalCommands, String terminal) { writeGNUPlotHeader(out, columns[x], (y != -1 ? columns[y] : columns[z[0]]), columns[z[0]], additionalCommands, terminal); if (y != -1) out.print("splot "); else out.print("plot "); for (int i = 0; i < z.length; i++) { if (i>0) out.print(", "); out.print("'-' title \""+columns[z[i]]+"\" with linespoints"); } out.println(); for (int i = 0; i < z.length; i++) { if (y != -1) write3DGNUPlotData(out, x, y, z[i]); else write2DGNUPlotData(out, x, z[i]); out.println("e"); } } private static Image createMessageImage(String message) { String[] lines = message.split("\n"); BufferedImage image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); int lineHeight = g.getFont().getSize(); g.setColor(Color.white); g.fillRect(0,0,640,480); g.setColor(Color.black); Icon icon = javax.swing.UIManager.getIcon("OptionPane.errorIcon"); if (icon != null) icon.paintIcon(null, g, 100-icon.getIconWidth()-11, 50-lineHeight); for (int i = 0; i < lines.length; i++) g.drawString(lines[i], 100, 50+i*lineHeight); return image; } public Image createGNUPlot(int x, int y, int[] z, String additionalCommands) { if (EXECUTABLE == null) { return createMessageImage("Property yale.tools.gnuplot.command is not set!"); } else { try { Process process = Runtime.getRuntime().exec(EXECUTABLE); //System.out.println(Tools.readOutput(new BufferedReader(new InputStreamReader(process.getErrorStream())))); PrintStream out = new PrintStream(process.getOutputStream()); //PrintStream out = System.out; writeGNUPlot(out, x, y, z, additionalCommands, "png"); out.close(); Image image = ImageIO.read(process.getInputStream()); process.getInputStream().close(); //process.getErrorStream().close(); //process.destroy(); return image; } catch (IOException e) { return createMessageImage("GNUPlot caused exception:\n"+e); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -