📄 clusterer.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. *//* * Clusterer.java * Copyright (C) 2004 Stefan Mutter * */package weka.gui.beans;import java.util.Vector;import java.util.Enumeration;import java.util.Hashtable;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JTextField;import java.awt.BorderLayout;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.InputEvent;import java.awt.*;import java.io.Serializable;import java.io.Reader;import java.io.BufferedReader;import java.io.FileReader;import java.io.File;import javax.swing.ImageIcon;import javax.swing.SwingConstants;import java.beans.EventSetDescriptor;import weka.core.Instance;import weka.core.Instances;import weka.clusterers.*;import weka.filters.Filter;import weka.filters.unsupervised.attribute.Remove;//import weka.classifiers.*;//import weka.classifiers.rules.ZeroR;import weka.gui.Logger;/** * Bean that wraps around weka.clusterers * * @author <a href="mailto:mutter@cs.waikato.ac.nz">Stefan Mutter</a> * @version $Revision: 1.3 $ * @see JPanel * @see BeanCommon * @see Visible * @see WekaWrapper * @see Serializable * @see UserRequestAcceptor * @see TrainingSetListener * @see TestSetListener */public class Clusterer extends JPanel implements BeanCommon, Visible, WekaWrapper, EventConstraints, Serializable, UserRequestAcceptor, TrainingSetListener, TestSetListener{ protected BeanVisual m_visual = new BeanVisual("Clusterer", BeanVisual.ICON_PATH+"EM.gif", BeanVisual.ICON_PATH+"EM_animated.gif"); private static int IDLE = 0; private static int BUILDING_MODEL = 1; private static int CLUSTERING = 2; private int m_state = IDLE; private Thread m_buildThread = null; /** * Global info for the wrapped classifier (if it exists). */ protected String m_globalInfo; /** * Objects talking to us */ private Hashtable m_listenees = new Hashtable(); /** * Objects listening for batch clusterer events */ private Vector m_batchClustererListeners = new Vector(); /** * Objects listening for graph events */ private Vector m_graphListeners = new Vector(); /** * Objects listening for text events */ private Vector m_textListeners = new Vector(); /** * Holds training instances for batch training. */ private Instances m_trainingSet; private transient Instances m_testingSet; private weka.clusterers.Clusterer m_Clusterer = new EM(); private transient Logger m_log = null; private Double m_dummy = new Double(0.0); /** * Global info (if it exists) for the wrapped classifier * * @return the global info */ public String globalInfo() { return m_globalInfo; } /** * Creates a new <code>Clusterer</code> instance. */ public Clusterer() { setLayout(new BorderLayout()); add(m_visual, BorderLayout.CENTER); setClusterer(m_Clusterer); } /** * Set the clusterer for this wrapper * * @param c a <code>weka.clusterers.Clusterer</code> value */ public void setClusterer(weka.clusterers.Clusterer c) { boolean loadImages = true; if (c.getClass().getName(). compareTo(m_Clusterer.getClass().getName()) == 0) { loadImages = false; } else { // clusterer has changed so any batch training status is now // invalid m_trainingSet = null; } m_Clusterer = c; String clustererName = c.getClass().toString(); clustererName = clustererName.substring(clustererName. lastIndexOf('.')+1, clustererName.length()); if (loadImages) { if (!m_visual.loadIcons(BeanVisual.ICON_PATH+clustererName+".gif", BeanVisual.ICON_PATH+clustererName+"_animated.gif")) { useDefaultVisual(); } } m_visual.setText(clustererName); // get global info m_globalInfo = KnowledgeFlowApp.getGlobalInfo(m_Clusterer); } /** * Returns true if this clusterer has an incoming connection that is * a batch set of instances * * @return a <code>boolean</code> value */ public boolean hasIncomingBatchInstances() { if (m_listenees.size() == 0) { return false; } if (m_listenees.containsKey("trainingSet") || m_listenees.containsKey("testSet") || m_listenees.containsKey("dataSet")) { return true; } return false; } /** * Get the clusterer currently set for this wrapper * * @return a <code>weka.clusterers.Clusterer</code> value */ public weka.clusterers.Clusterer getClusterer() { return m_Clusterer; } /** * Sets the algorithm (clusterer) for this bean * * @param algorithm an <code>Object</code> value * @exception IllegalArgumentException if an error occurs */ public void setWrappedAlgorithm(Object algorithm) { if (!(algorithm instanceof weka.clusterers.Clusterer)) { throw new IllegalArgumentException(algorithm.getClass()+" : incorrect " +"type of algorithm (Clusterer)"); } setClusterer((weka.clusterers.Clusterer)algorithm); } /** * Returns the wrapped clusterer * * @return an <code>Object</code> value */ public Object getWrappedAlgorithm() { return getClusterer(); } /** * Accepts a training set and builds batch clusterer * * @param e a <code>TrainingSetEvent</code> value */ public void acceptTrainingSet(final TrainingSetEvent e) { if (e.isStructureOnly()) { // no need to build a clusterer, instead just generate a dummy // BatchClustererEvent in order to pass on instance structure to // any listeners BatchClustererEvent ce = new BatchClustererEvent(this, m_Clusterer, new DataSetEvent(this, e.getTrainingSet()), e.getSetNumber(), e.getMaxSetNumber(),1); notifyBatchClustererListeners(ce); return; } if (m_buildThread == null) { try { if (m_state == IDLE) { synchronized (this) { m_state = BUILDING_MODEL; } m_trainingSet = e.getTrainingSet(); final String oldText = m_visual.getText(); m_buildThread = new Thread() { public void run() { try { if (m_trainingSet != null) { m_visual.setAnimated(); m_visual.setText("Building clusters..."); if (m_log != null) { m_log.statusMessage("Clusterer : building clusters..."); } buildClusterer(); if(m_batchClustererListeners.size() > 0){ BatchClustererEvent ce = new BatchClustererEvent(this, m_Clusterer, new DataSetEvent(this, e.getTrainingSet()), e.getSetNumber(), e.getMaxSetNumber(),1); notifyBatchClustererListeners(ce); } if (m_Clusterer instanceof weka.core.Drawable && m_graphListeners.size() > 0) { String grphString = ((weka.core.Drawable)m_Clusterer).graph(); int grphType = ((weka.core.Drawable)m_Clusterer).graphType(); String grphTitle = m_Clusterer.getClass().getName(); grphTitle = grphTitle.substring(grphTitle. lastIndexOf('.')+1, grphTitle.length()); grphTitle = "Set " + e.getSetNumber() + " (" +e.getTrainingSet().relationName() + ") " +grphTitle; GraphEvent ge = new GraphEvent(Clusterer.this, grphString, grphTitle, grphType); notifyGraphListeners(ge); } if (m_textListeners.size() > 0) { String modelString = m_Clusterer.toString(); String titleString = m_Clusterer.getClass().getName(); titleString = titleString. substring(titleString.lastIndexOf('.') + 1, titleString.length()); modelString = "=== Clusterer model ===\n\n" + "Scheme: " +titleString+"\n" + "Relation: " + m_trainingSet.relationName() + ((e.getMaxSetNumber() > 1) ? "\nTraining Fold: "+e.getSetNumber() :"") + "\n\n" + modelString; titleString = "Model: " + titleString; TextEvent nt = new TextEvent(Clusterer.this, modelString, titleString); notifyTextListeners(nt); } } } catch (Exception ex) { ex.printStackTrace(); } finally { m_visual.setText(oldText); m_visual.setStatic(); m_state = IDLE; if (isInterrupted()) { // prevent any clusterer events from being fired m_trainingSet = null; if (m_log != null) { m_log.logMessage("Build clusterer interrupted!"); m_log.statusMessage("OK"); } } else { // save header m_trainingSet = new Instances(m_trainingSet, 0); } if (m_log != null) { m_log.statusMessage("OK"); } block(false); } } }; m_buildThread.setPriority(Thread.MIN_PRIORITY); m_buildThread.start(); // make sure the thread is still running before we block // if (m_buildThread.isAlive()) { block(true); // } m_buildThread = null; m_state = IDLE; } } catch (Exception ex) { ex.printStackTrace(); } } } /** * Accepts a test set for a batch trained clusterer * * @param e a <code>TestSetEvent</code> value */ public void acceptTestSet(TestSetEvent e) { if (m_trainingSet != null) { try { if (m_state == IDLE) { synchronized(this) { m_state = CLUSTERING; } m_testingSet = e.getTestSet(); if (m_trainingSet.equalHeaders(m_testingSet)) { BatchClustererEvent ce = new BatchClustererEvent(this, m_Clusterer, new DataSetEvent(this, e.getTestSet()), e.getSetNumber(), e.getMaxSetNumber(),0); notifyBatchClustererListeners(ce); } m_state = IDLE; } } catch (Exception ex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -