⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plot2d.java

📁 wekaUT是 university texas austin 开发的基于weka的半指导学习(semi supervised learning)的分类器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *    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., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *    Plot2D.java *    Copyright (C) 2000 Mark Hall * */package weka.gui.visualize;import weka.gui.*;import weka.core.Instances;import weka.core.Instance;import weka.core.Attribute;import weka.core.FastVector;import weka.core.Utils;import java.util.Random;import java.awt.Insets;import java.awt.Font;import java.awt.event.InputEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseMotionAdapter;import java.awt.event.MouseEvent;import java.awt.Dimension;import java.awt.BorderLayout;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.SwingConstants;import javax.swing.JFrame;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import javax.swing.JViewport;import javax.swing.JSlider;import java.awt.Color;import java.awt.FontMetrics;import java.awt.Graphics;/** * This class plots datasets in two dimensions. It can also plot * classifier errors and clusterer predictions. *  * @author Mark Hall (mhall@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class Plot2D extends JPanel {  /* constants for shape types */  public static final int MAX_SHAPES = 5;  public static final int ERROR_SHAPE = 1000;  public static final int MISSING_SHAPE = 2000;  public static final int CONST_AUTOMATIC_SHAPE = -1;  public static final int X_SHAPE = 0;  public static final int PLUS_SHAPE = 1;  public static final int DIAMOND_SHAPE = 2;  public static final int TRIANGLEUP_SHAPE = 3;  public static final int TRIANGLEDOWN_SHAPE = 4;  public static final int DEFAULT_SHAPE_SIZE = 2;  /** Default colour for the axis */  private Color m_axisColour = Color.green;  /** Default colour for the plot background */  private Color m_backgroundColour = Color.black;  /** The plots to display */  protected FastVector m_plots = new FastVector();  /** The master plot */  protected PlotData2D m_masterPlot = null;  /** The name of the master plot */  protected String m_masterName = "master plot";  /** The instances to be plotted */  protected Instances m_plotInstances=null;  /** An optional "compainion" of the panel. If specified, this      class will get to do its thing with our graphics context      before we do any drawing. Eg. the visualize panel may need      to draw polygons etc. before we draw plot axis and data points */  protected Plot2DCompanion m_plotCompanion=null;  /** For popping up text info on data points */  private JFrame m_InstanceInfo = null;  private JTextArea m_InstanceInfoText = new JTextArea();  /** The list of the colors used */  protected FastVector m_colorList;  /** default colours for colouring discrete class */  protected Color [] m_DefaultColors = {Color.blue,					Color.red,					Color.green,					Color.cyan,					Color.pink,					new Color(255, 0, 255),					Color.orange,					new Color(255, 0, 0),					new Color(0, 255, 0),					Color.white};  /** Indexes of the attributes to go on the x and y axis and the attribute      to use for colouring and the current shape for drawing */  protected int m_xIndex=0;  protected int m_yIndex=0;  protected int m_cIndex=0;  protected int m_sIndex=0;  /** Holds the min and max values of the x, y and colouring attributes    over all plots */  protected double m_maxX;  protected double m_minX;  protected double m_maxY;  protected double m_minY;  protected double m_maxC;  protected double m_minC;      /** Axis padding */  private final int m_axisPad = 5;  /** Tick size */  private final int m_tickSize = 5;  /**the offsets of the axes once label metrics are calculated */  private int m_XaxisStart=0;  private int m_YaxisStart=0;  private int m_XaxisEnd=0;  private int m_YaxisEnd=0;  /** if the user resizes the window, or the attributes selected for      the attributes change, then the lookup table for points needs      to be recalculated */  private boolean m_plotResize = true;    /** if the user changes attribute assigned to an axis */  protected boolean m_axisChanged = false;  /** An array used to show if a point is hidden or not.   * This is used for speeding up the drawing of the plot panel   * although I am not sure how much performance this grants over   * not having it.   */  private int[][] m_drawnPoints;  /** Font for labels */  private Font m_labelFont;  protected FontMetrics m_labelMetrics=null;   /** the level of jitter */  private int m_JitterVal=0;  /** random values for perterbing the data points */  private Random m_JRand = new Random(0);  /** lookup table for plotted points */  private double [][] m_pointLookup=null;  /** Constructor */  public Plot2D() {    setProperties();    this.setBackground(m_backgroundColour);    m_InstanceInfoText.setFont(new Font("Monospaced", Font.PLAIN,12));    m_InstanceInfoText.setEditable(false);    m_drawnPoints = new int[this.getWidth()][this.getHeight()];    /** Set up some default colours */    m_colorList = new FastVector(10);    for (int noa = m_colorList.size(); noa < 10; noa++) {      Color pc = m_DefaultColors[noa % 10];      int ija =  noa / 10;      ija *= 2;       for (int j=0;j<ija;j++) {	pc = pc.darker();      }            m_colorList.addElement(pc);    }  }  /**   * Set the properties for Plot2D   */  private void setProperties() {    if (VisualizeUtils.VISUALIZE_PROPERTIES != null) {      String thisClass = this.getClass().getName();      String axisKey = thisClass+".axisColour";      String backgroundKey = thisClass+".backgroundColour";      String axisColour = VisualizeUtils.VISUALIZE_PROPERTIES.	getProperty(axisKey);      if (axisColour == null) {	System.err.println("Warning: no configuration property found in "			   +VisualizeUtils.PROPERTY_FILE			   +" for "+axisKey);      } else {	System.err.println("Setting axis colour to: "+axisColour);	m_axisColour = VisualizeUtils.processColour(axisColour, m_axisColour);      }      String backgroundColour = 	VisualizeUtils.VISUALIZE_PROPERTIES.getProperty(backgroundKey);      if (backgroundColour == null) {	System.err.println("Warning: no configuration property found in "			   +VisualizeUtils.PROPERTY_FILE			   +" for "+backgroundKey);      } else {	System.err.println("Setting background colour to: "+backgroundColour);	m_backgroundColour = VisualizeUtils.processColour(backgroundColour, 							  m_backgroundColour);      }    }  }  /**    * This will check the values of the screen points passed and make sure    * that they land on the screen   * @param x1 The x coord.   * @param y1 The y coord.   */  private boolean checkPoints(double x1, double y1) {    if (x1 < 0 || x1 > this.getSize().width || y1 < 0 	|| y1 > this.getSize().height) {      return false;    }    return true;  }  /**   * Set a companion class. This is a class that might want   * to render something on the plot before we do our thing. Eg,   * Malcolm's shape drawing stuff needs to happen before we plot   * axis and points   * @param p a companion class   */  public void setPlotCompanion(Plot2DCompanion p) {    m_plotCompanion = p;  }  /**   * Set level of jitter and repaint the plot using the new jitter value   * @param j the level of jitter   */  public void setJitter(int j) {    if (m_plotInstances.numAttributes() > 0 	&& m_plotInstances.numInstances() > 0) {      if (j >= 0) {	m_JitterVal = j;	m_JRand = new Random(m_JitterVal);	//      if (m_pointLookup != null) {	m_drawnPoints = new int[m_XaxisEnd - m_XaxisStart + 1]	  [m_YaxisEnd - m_YaxisStart + 1];	updatePturb();	//      }	this.repaint();      }    }  }  /**   * Set a list of colours to use when colouring points according   * to class values or cluster numbers   * @param cols the list of colours to use   */  public void setColours (FastVector cols) {    m_colorList = cols;  }  /**   * Set the index of the attribute to go on the x axis   * @param x the index of the attribute to use on the x axis   */  public void setXindex(int x) {    m_xIndex = x;    for (int i=0;i<m_plots.size();i++) {      ((PlotData2D)m_plots.elementAt(i)).setXindex(m_xIndex);    }    determineBounds();    if (m_JitterVal != 0) {      updatePturb();    }    m_axisChanged = true;    this.repaint();  }      /**   * Set the index of the attribute to go on the y axis   * @param y the index of the attribute to use on the y axis   */  public void setYindex(int y) {    m_yIndex = y;    for (int i=0;i<m_plots.size();i++) {      ((PlotData2D)m_plots.elementAt(i)).setYindex(m_yIndex);    }    determineBounds();    if (m_JitterVal != 0) {      updatePturb();    }    m_axisChanged = true;    this.repaint();  }  /**   * Set the index of the attribute to use for colouring   * @param c the index of the attribute to use for colouring   */  public void setCindex(int c) {    m_cIndex = c;    for (int i=0;i<m_plots.size();i++) {      ((PlotData2D)m_plots.elementAt(i)).setCindex(m_cIndex);    }    determineBounds();    m_axisChanged = true;    this.repaint();  }  /**   * Return the list of plots   * @return the list of plots   */  public FastVector getPlots() {    return m_plots;  }  /**   * Get the master plot   * @return the master plot   */  public PlotData2D getMasterPlot() {    return m_masterPlot;  }  /**    * Return the current max value of the attribute plotted on the x axis   * @return the max x value   */  public double getMaxX() {    return m_maxX;  }  /**    * Return the current max value of the attribute plotted on the y axis   * @return the max y value   */  public double getMaxY() {    return m_maxY;  }  /**    * Return the current min value of the attribute plotted on the x axis   * @return the min x value   */  public double getMinX() {    return m_minX;  }    /**    * Return the current min value of the attribute plotted on the y axis   * @return the min y value   */  public double getMinY() {    return m_minY;  }  /**    * Return the current max value of the colouring attribute   * @return the max colour value   */  public double getMaxC() {    return m_maxC;  }    /**    * Return the current min value of the colouring attribute   * @return the min colour value   */  public double getMinC() {    return m_minC;  }      /**   * Sets the master plot from a set of instances   * @param inst the instances   * @exception exception Exception if instances could not be set   */  public void setInstances(Instances inst) throws Exception {    System.err.println("Setting Instances");    PlotData2D tempPlot = new PlotData2D(inst);    tempPlot.setPlotName("master plot");    setMasterPlot(tempPlot);  }  /**   * Set the master plot.   * @param master the plot to make the master plot   * @exception Exception if the plot could not be set.   */  public void setMasterPlot(PlotData2D master) throws Exception {    if (master.m_plotInstances == null) {      throw new Exception("No instances in plot data!");    }    removeAllPlots();    m_masterPlot = master;    m_plots.addElement(m_masterPlot);    m_plotInstances = m_masterPlot.m_plotInstances;        m_xIndex=0;    m_yIndex=0;    m_cIndex=0;        determineBounds();  }  /**   * Clears all plots   */  public void removeAllPlots() {    m_masterPlot = null;    m_plotInstances = null;    m_plots = new FastVector();    m_xIndex = 0; m_yIndex = 0; m_cIndex = 0;  }  /**   * Add a plot to the list of plots to display   * @param newPlot the new plot to add   * @exception Exception if the plot could not be added   */  public void addPlot(PlotData2D newPlot) throws Exception {    if (newPlot.m_plotInstances == null) {      throw new Exception("No instances in plot data!");    }    if (m_masterPlot != null) {      if (m_masterPlot.m_plotInstances.	  equalHeaders(newPlot.m_plotInstances) == false) {	throw new Exception("Plot2D :Plot data's instances are incompatable "			    +" with master plot");      }    } else {      m_masterPlot = newPlot;      m_plotInstances = m_masterPlot.m_plotInstances;    }    m_plots.addElement(newPlot);    setXindex(m_xIndex);    setYindex(m_yIndex);    setCindex(m_cIndex);  }  /**   * Set up fonts and font metrics   * @param gx the graphics context   */  private void setFonts(Graphics gx) {    if (m_labelMetrics == null) {      m_labelFont = new Font("Monospaced", Font.PLAIN, 12);      m_labelMetrics = gx.getFontMetrics(m_labelFont);    }    gx.setFont(m_labelFont);  }  /**   * Pops up a window displaying attribute information on any instances   * at a point+-plotting_point_size (in panel coordinates)   *   * @param x the x value of the clicked point   * @param y the y value of the clicked point   * @param newFrame true if instance info is to be displayed in a   * new frame.   */  public void searchPoints(int x, int y, final boolean newFrame) {    if (m_masterPlot.m_plotInstances != null) {      int longest=0;      for (int j=0;j<m_masterPlot.m_plotInstances.numAttributes();j++) {	if (m_masterPlot.m_plotInstances.attribute(j).name().length() > 	    longest) {	  longest = m_masterPlot.m_plotInstances.attribute(j).name().length();	}      }      StringBuffer insts = new StringBuffer();       for (int jj=0;jj<m_plots.size();jj++) {	PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(jj));		for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) {	  if (temp_plot.m_pointLookup[i][0] != Double.NEGATIVE_INFINITY) {	    double px = temp_plot.m_pointLookup[i][0] + 	      temp_plot.m_pointLookup[i][2];	    double py = temp_plot.m_pointLookup[i][1] + 	      temp_plot.m_pointLookup[i][3];	    //	    double size = temp_plot.m_pointLookup[i][2];	    double size = temp_plot.m_shapeSize[i];	    if ((x >= px-size) && (x <= px+size) &&		(y >= py-size) && (y <= py+size)) {	      {		insts.append("\nPlot : "+temp_plot.m_plotName			     +"\nInstance: "+i+"\n");		for (int j=0;j<temp_plot.m_plotInstances.numAttributes();j++) {		  for (int k = 0;k < 			 (longest-temp_plot.m_plotInstances.			  attribute(j).name().length()); k++) {		    insts.append(" ");		  }		  insts.append(temp_plot.m_plotInstances.attribute(j).name());  		  insts.append(" : ");		  		  if (temp_plot.m_plotInstances.instance(i).isMissing(j)) {		    insts.append("Missing");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -