📄 attributevisualizationpanel.java
字号:
/*
* 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.
*/
/*
* AttributeVisualizationPanel.java
* Copyright (C) 2003 Ashraf M. Kibriya
*
*/
package weka.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.io.FileReader;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import weka.core.AttributeStats;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.Utils;
/**
* Creates a panel that shows a visualization of an
* attribute in a dataset. For nominal attribute it
* shows a bar plot, with each bar corresponding to
* each nominal value of the attribute with its height
* equal to the frequecy that value appears in the
* dataset. For numeric attributes, it displays a
* histogram. The width of an interval in the
* histogram is calculated using Scott's(1979)
* method: <br>
* intervalWidth = Max(1, 3.49*Std.Dev*numInstances^(1/3))
* Then the number of intervals is calculated by: <br>
* intervals = max(1, Math.round(Range/intervalWidth);
*
* @author Ashraf M. Kibriya (amk14@cs.waikato.ac.nz)
* @version $Revision$
*/
public class AttributeVisualizationPanel extends JPanel {
Instances m_data;
AttributeStats as;
int attribIndex, maxValue;
int histBarCounts[];
int histBarClassCounts[][];
double m_barRange;
int classIndex;
Thread hc;
boolean threadRun=false;
JComboBox m_colorAttrib;
FontMetrics fm;
private Integer m_locker = new Integer(1);
//Image img;
/** Contains discrete colours for colouring for nominal attributes */
private FastVector m_colorList = new FastVector();
/** default colour list */
private static final Color [] m_defaultColors = {Color.blue,
Color.red,
Color.cyan,
new Color(75, 123, 130),
Color.pink,
Color.green,
Color.orange,
new Color(255, 0, 255),
new Color(255, 0, 0),
new Color(0, 255, 0),
};
public AttributeVisualizationPanel() {
this(false);
}
public AttributeVisualizationPanel(boolean showColouringOption) {
this.setFont( new Font("Default", Font.PLAIN, 9) );
fm = this.getFontMetrics( this.getFont() );
this.setToolTipText("");
FlowLayout fl= new FlowLayout(FlowLayout.LEFT);
this.setLayout(fl);
this.addComponentListener( new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
if(m_data!=null)
calcGraph();
}
});
m_colorAttrib = new JComboBox();
m_colorAttrib.addItemListener( new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if(ie.getStateChange()==ItemEvent.SELECTED) {
classIndex = m_colorAttrib.getSelectedIndex() - 1;
if (as != null) {
setAttribute(attribIndex);
}
}
}
});
if(showColouringOption) {
//m_colorAttrib.setVisible(false);
this.add(m_colorAttrib);
validate();
}
}
/**
* Sets the instances for use
*
* @param newins a set of Instances
*/
public void setInstances(Instances newins) {
attribIndex = 0;
m_data = newins;
as=null;
if(m_colorAttrib!=null) {
m_colorAttrib.removeAllItems();
m_colorAttrib.addItem("No class");
for(int i=0; i<m_data.numAttributes(); i++) {
m_colorAttrib.addItem(new String("Class: "+m_data.attribute(i).name()+" "+
((m_data.attribute(i).isNominal()) ? "(Nom)":"")));
}
if (m_data.classIndex() >= 0) {
m_colorAttrib.setSelectedIndex(m_data.classIndex() + 1);
} else {
m_colorAttrib.setSelectedIndex(m_data.numAttributes());
}
//if (m_data.classIndex() >= 0) {
// m_colorAttrib.setSelectedIndex(m_data.classIndex());
//}
}
if (m_data.classIndex() >= 0) {
classIndex = m_data.classIndex();
} else {
classIndex = m_data.numAttributes()-1;
}
this.repaint();
}
public JComboBox getColorBox() {
return m_colorAttrib;
}
/**
* Get the coloring index for the plot
*
* @return an <code>int</code> value
*/
public int getColoringIndex() {
return classIndex; //m_colorAttrib.getSelectedIndex();
}
/**
* Set the coloring index for the plot
*
* @param ci an <code>int</code> value
*/
public void setColoringIndex(int ci) {
classIndex = ci;
if(m_colorAttrib!=null)
m_colorAttrib.setSelectedIndex(ci + 1);
else
setAttribute(attribIndex);
}
/**
* Tells the panel which attribute to visualize.
*
* @param index The index of the attribute
*/
public void setAttribute(int index) {
synchronized (m_locker) {
threadRun = true;
//if(hc!=null && hc.isAlive()) hc.stop();
attribIndex = index;
as = m_data.attributeStats(attribIndex);
//classIndex = m_colorAttrib.getSelectedIndex();
}
calcGraph();
}
/*
* Recalculates the bar widths and heights required to display
* the barplot or histogram. Required usually when the component
* is resized.
*/
public void calcGraph() {
synchronized (m_locker) {
threadRun = true;
if(as.nominalCounts!=null) {
hc = new BarCalc();
hc.setPriority(Thread.MIN_PRIORITY);
hc.start();
}
else if(as.numericStats!=null) {
hc = new HistCalc();
hc.setPriority(Thread.MIN_PRIORITY);
hc.start();
} else {
histBarCounts = null;
histBarClassCounts = null;
this.repaint();
threadRun = false;
}
}
}
private class BarCalc extends Thread {
public void run() {
synchronized (m_locker) {
if((classIndex >= 0) && (m_data.attribute(classIndex).isNominal())) {
int histClassCounts[][];
histClassCounts = new int[m_data.attribute(attribIndex).numValues()]
[m_data.attribute(classIndex).numValues()+1];
maxValue = as.nominalCounts[0];
for(int i=0; i<m_data.attribute(attribIndex).numValues(); i++) {
if(as.nominalCounts[i]>maxValue)
maxValue = as.nominalCounts[i];
}
if(m_colorList.size()==0)
m_colorList.addElement(Color.black);
for(int i=m_colorList.size(); i<m_data.attribute(classIndex).numValues()+1; i++) {
Color pc = m_defaultColors[(i-1) % 10];
int ija = (i-1) / 10;
ija *= 2;
for (int j=0;j<ija;j++) {
pc = pc.darker();
}
m_colorList.addElement(pc);
}
for(int k=0; k<m_data.numInstances(); k++) {
//System.out.println("attrib: "+m_data.instance(k).value(attribIndex)+
// " class: "+m_data.instance(k).value(classIndex));
if(!m_data.instance(k).isMissing(attribIndex))
{
if(m_data.instance(k).isMissing(classIndex))
{
histClassCounts[(int)m_data.instance(k).value(attribIndex)][0]++;
}else
{
histClassCounts[(int)m_data.instance(k).value(attribIndex)][(int)m_data.instance(k).value(classIndex)+1]++;
}
}
}
//for(int i=0; i<histClassCounts.length; i++) {
//int sum=0;
//for(int j=0; j<histClassCounts[i].length; j++) {
// sum = sum+histClassCounts[i][j];
//}
//System.out.println("histCount: "+sum+" Actual: "+as.nominalCounts[i]);
//}
threadRun=false;
histBarClassCounts = histClassCounts;
//Image tmpImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
//drawGraph( tmpImg.getGraphics() );
//img = tmpImg;
AttributeVisualizationPanel.this.repaint();
}
else {
int histCounts[];
histCounts = new int[m_data.attribute(attribIndex).numValues()];
maxValue = as.nominalCounts[0];
for(int i=0; i<m_data.attribute(attribIndex).numValues(); i++) {
if(as.nominalCounts[i]>maxValue)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -