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

📄 clusteringgraphview.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.
 */

/*
 * Created on 2005-1-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package eti.bi.alphaminer.patch.standard.operation.result.view;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import weka.core.Instances;

import com.prudsys.pdm.Adapters.Weka.WekaCoreAdapter;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Models.Clustering.Cluster;
import com.prudsys.pdm.Models.Clustering.ClusteringMiningModel;

import eti.bi.alphaminer.operation.result.export.JPEGImageExporter;
import eti.bi.alphaminer.operation.result.view.AttributeViewPanel;
import eti.bi.alphaminer.operation.result.view.GraphView;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;

/**
 * Take ClusteringMiningModel and its MiningAttribute[] as inputs.
 * Output a JPanel showing the graphical view of of each attribute.
 * 
 * @author TWang  	Jan. 20, 2005. 
 */
public class ClusteringGraphView extends GraphView {  
	 		
	 		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
			// Java Scroll Pane that contains the graph
	 		private JScrollPane m_ScrollPane; 
	 		private JPanel m_JPanel;
	 		
	 		// The ClusteringMiningModel generated by the xelopes library
	 	 	private ClusteringMiningModel m_ClusteringModel;
	 	 	private MiningAttribute[] m_Attributes;
	 	 	private Cluster[] m_Clusters = null;	 
	 	 	
	 	 	// The visualization of the attribute values
	 	 	private AttributeViewPanel[][] m_AttVisualizePanel = null;
	 	 	
	 		/**
	 		 * Create a ScrollPane with a GridBagLayout to hold the visualization graph.
	 		 * 
	 		 * @param a_ClusteringModel
	 		 * @param a_Attributes
	 		 * @throws SysException
	 		 * @throws MiningException
	 		 * @throws InterruptedException
	 		 */
	 		public ClusteringGraphView(ClusteringMiningModel a_ClusteringModel, MiningAttribute[] a_Attributes) throws SysException, MiningException, InterruptedException{
	 			super(Resource.srcStr("GraphView"));
	 		    
	 			m_ClusteringModel = a_ClusteringModel; 
	 			m_Attributes = a_Attributes;
	 			
	 			m_Clusters = m_ClusteringModel.getClusters();	 
	 			
	 			m_AttVisualizePanel = new AttributeViewPanel[m_Clusters.length + 1][m_Attributes.length + 1];
	 			for (int i = 0; i < m_Clusters.length + 1; i++){
	 				for (int j = 0; j <  m_Attributes.length + 1; j++){
	 					m_AttVisualizePanel[i][j] = new AttributeViewPanel();
	 				} 
	 			}
	 		 
	 			m_ScrollPane = new JScrollPane();
	 			m_JPanel = new JPanel(new GridBagLayout());
	 			m_JPanel.setBackground(Color.WHITE);
	 			m_JPanel.setPreferredSize(new Dimension(400, 600));
	 			m_ScrollPane.setViewportView(m_JPanel);
	 			
	 			m_ScrollPane = new JScrollPane(); 
	 			
	 			this.setLayout(new BorderLayout());
	 			this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
	 			this.add(m_ScrollPane, BorderLayout.CENTER); 
	 		  
	 			createView();
	 		 
	 	 	} 
	 		
	 		/**
	 		 * Helper calss to transform Xelopes MingStoredData into WEKA Instances.
	 		 * 
	 		 * @param a_InputMiningStoredData
	 		 * @return
	 		 * @throws MiningException
	 		 */
	 		public Instances transform(MiningStoredData a_InputMiningStoredData) throws MiningException
	 		{ 
	 			Instances wekaInstances;
	 			try {
	 				// Reset the cursor of the MiningStoredData set, so the transform starts from
	 				// the first reord. Otherwise, the returned object might be NULL.
	 				// By TWang. Jan 25, 2005.
	 				a_InputMiningStoredData.reset();
	 				wekaInstances = (Instances) WekaCoreAdapter.PDMMiningInputStream2WekaInstances(a_InputMiningStoredData); 
	 			} catch (Exception e) { 
	 				e.printStackTrace();
	 				throw new MiningException("Can not transform from MiningStoredData to Instances.");
	 			}
	 			return wekaInstances;
	 		}
	 		
	 		/** 
	 		 * Create the view. 
	 		 */
	 		public void createView() throws SysException, MiningException, InterruptedException
	 		{
	 			if (m_ClusteringModel == null){
	 				throw new SysException("clustering model is null");
	 			}
	 			if (m_ClusteringModel != null)
	 			{   
	 				createGraph(); 
	 					
	 				m_ScrollPane.setPreferredSize(new Dimension(200, 73));
	 				m_ScrollPane.getViewport().add(m_JPanel);
	 				m_ScrollPane.getViewport().setBackground(Color.WHITE);  
	 			}
	 		}
	 		
	 		/**
	 		 * @param  
	 		 * @throws MiningException
	 		 * @throws SysException
	 		 * @throws InterruptedException
	 		 */
	 		@SuppressWarnings("unchecked")
			private void createGraph( ) throws MiningException, SysException, InterruptedException {  
	 			
	 			if (m_ClusteringModel == null){
	 				throw new SysException("clustering model is null");
	 			}
	 			
	 			int clusterIndex;
	 			int totalRecordNum = 0;
	 			Instances instances[] = new Instances[m_Clusters.length]; 
	 		    
	 			// calculate the total records number, and get the instances.	 			
	 			for (clusterIndex = 0; clusterIndex < m_Clusters.length; clusterIndex++) { 
	 				int recordsNum = m_Clusters[clusterIndex].getContainedVectors().size();
	 				totalRecordNum += recordsNum;  
	 				ArrayList instancesArrayList = new ArrayList(m_Clusters[clusterIndex].getContainedVectors());
	 				if (instancesArrayList.size() >0){
	 					instances[clusterIndex] = transform(new MiningStoredData(instancesArrayList)); 
	 				} 
	 	 		}
	 			
	 			// calculate the percentage
	 			GridBagConstraints c = new GridBagConstraints();
	 			 
	 			for (clusterIndex = 0; clusterIndex < m_Clusters.length; clusterIndex++) {
	 				JPanel jPanel= new JPanel(new BorderLayout());
	 				
	 				JLabel clusterNameLabel = new JLabel(m_Clusters[clusterIndex].getName());
	 				int recordsNum = m_Clusters[clusterIndex].getContainedVectors().size(); 	 				
	 				long percent = Math.round(100.0 * recordsNum / totalRecordNum); 
	 				JLabel recordsNumLabel = new JLabel("(" + (new Integer(recordsNum)).toString() + ", " +  (new Long(percent)).toString() + "%)");
	 				
	 				jPanel.add(clusterNameLabel, BorderLayout.NORTH);
	 			 	jPanel.add(recordsNumLabel, BorderLayout.CENTER); 
	 				jPanel.setBackground(Color.WHITE);
	 				
		 			    c.gridx = clusterIndex + 1;
		 				c.gridy = 0;
		 				c.ipady = 0;
 		 			//	c.weightx = 1.0; 
		 				c.anchor = GridBagConstraints.CENTER;
		 				c.insets = new Insets(0, 0, 0, 0);
		 				m_JPanel.add(jPanel, c);	
	 			}
	 			
	 			
	 			// add the attribute name column
	 			for (int j=0; j< m_Attributes.length; j++){
	 				JPanel attJPanel = new JPanel();
	 				attJPanel.add(new JLabel(m_Attributes[j].getName()));
	 				attJPanel.setBackground(Color.WHITE);
	 			 
		 			    c.gridx = 0;
		 				c.gridy = j+1; 
		 				c.ipadx = 0;
		 				//c.weighty = 1.0;
		 				c.anchor = GridBagConstraints.CENTER;
		 				c.insets = new Insets(0, 0, 0, 0);
		 				m_JPanel.add(attJPanel, c);	
	 			} 
	 			
	 			for (clusterIndex = 0; clusterIndex < m_Clusters.length ; clusterIndex++) {  
	 				// add the attribute nodes 
	 				for (int j=0; j< m_Attributes.length; j++){
	 				  m_AttVisualizePanel[clusterIndex][j].setInstances(instances[clusterIndex]);
	 				  MiningAttribute attribute = m_Attributes[j];
	 				  if (attribute instanceof NumericAttribute){
	 				 	// Calculate the value represented by the longest bar.
	 				 	double max = 0;
	 				 	for (int tempIndex = 0; tempIndex < m_Clusters.length; tempIndex ++){
	 				 		if(instances[tempIndex] != null){
	 				 			max = Math.max((double)(instances[tempIndex].attributeStats(j).numericStats.mean), max);
	 				 		}
	 				 	}
	 				 	 
	 					// Let the visualization grid panel display the specified attribute.
	 				 	m_AttVisualizePanel[clusterIndex][j].viewAttribute(j, max);
	 				 	
	 				 	c.fill = GridBagConstraints.NONE;
	 				 	c.gridx = clusterIndex + 1;
		 				c.gridy = j + 1;
		 				
		 				c.ipadx = 50;
		 				c.ipady = 100;
		 				
		 				c.weightx = 1.0;
		 				c.weighty = 1.0;
		 				c.anchor = GridBagConstraints.CENTER;
		 				c.insets = new Insets(0, 0, 10, 0);
		 				m_JPanel.add(m_AttVisualizePanel[clusterIndex][j], c);	
	 				 } 
	 		         else if (attribute instanceof CategoricalAttribute){
	 		         	// Calculate the value represented by the longest bar of each category.
	 		         	double max = 0;
	 				 	for (int tempIndex = 0; tempIndex < m_Clusters.length; tempIndex ++){
	 				 		for(int tempCatIndex = 0; tempCatIndex < ((CategoricalAttribute)attribute).getValues().size(); tempCatIndex ++){
	 				 			if(instances[tempIndex] != null){
	 				 				max = Math.max((double)(instances[tempIndex].attributeStats(j).nominalCounts[tempCatIndex]), max);	
	 				 			}
	 				 		}
	 				 	}
	 				 	 
	 				 	// Let the visualization grid panel display the specified attribute.
	 				 	m_AttVisualizePanel[clusterIndex][j].viewAttribute(j, max);	 		         	
	 		          	c.fill = GridBagConstraints.BOTH; 
	 		          	
		 				c.gridx = clusterIndex + 1;
		 				c.gridy = j + 1;
		 				
		 				c.weightx = 1.0;
		 				c.weighty = 1.0;
		 				c.anchor = GridBagConstraints.CENTER;
		 				c.insets = new Insets(0, 5, 0, 5);
		 				m_JPanel.add(m_AttVisualizePanel[clusterIndex][j], c);	 
	 				  }
	 				}
	 			}  
	 		}
	 	  	         		
	 	  		  

	 		public void export() throws SysException, AppException
	 	 	{
	 			// Use user home directory 
	 			File directory = new File(System.getProperty("user.dir"));
	 			
	 			// Create and initialize file chooser for jpg
	 			JFileChooser chooser = new JFileChooser(directory);
	 			chooser.setDialogTitle(Resource.srcStr("GraphExport"));
	 			chooser.setFileFilter(JPEGImageExporter.FILTER);
	 			chooser.setSelectedFile(JPEGImageExporter.DEFAULT_FILE);
	 	
	 			// pop up the file chooser dialog and return the file value
	 			int returnVal = chooser.showSaveDialog(this);	  
	 			if (returnVal == JFileChooser.APPROVE_OPTION) 
	 			{
	 				File exportFile = chooser.getSelectedFile();	
	 				
	 				//<<tyleung 20/4/2005
	 				if (exportFile.exists()) {
	 					int option = JOptionPane
	 							.showConfirmDialog(
	 									(Component) this,
	 									"The file  \""
	 											+ exportFile.getName()
	 											+ "\""
	 											+ " already exists. Do you want to replace the existing file?",//						
	 									"AlphaMiner", JOptionPane.YES_NO_OPTION,
	 									JOptionPane.QUESTION_MESSAGE);
	 					if (option != JOptionPane.CANCEL_OPTION) {
	 						if (option == JOptionPane.YES_OPTION) {
	 							
	 							   JPEGImageExporter aExporter = new JPEGImageExporter(m_JPanel, exportFile, true);
	 				 			   aExporter.export(); 
	 							
	 							
	 						}else{
	 							returnVal = chooser.showSaveDialog(this);
	 						}
	 					}
	 				}
	 				else {
	 				    JPEGImageExporter aExporter = new JPEGImageExporter(m_JPanel, exportFile, true);
	 				    aExporter.export();
	 				}
	 			} 
	 			//tyleung 20/4/2005>>	 					
	 	 	}
}

⌨️ 快捷键说明

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