📄 logisticdataview.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-25
*/
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.io.File;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import weka.classifiers.functions.Logistic;
import weka.core.Instances;
import com.prudsys.pdm.Adapters.Weka.WekaClassifier;
import com.prudsys.pdm.Adapters.Weka.WekaCoreAdapter;
import com.prudsys.pdm.Adapters.Weka.WekaSupervisedMiningModel;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.SortingDataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.operation.result.renderer.DataCellRenderer;
import eti.bi.alphaminer.patch.standard.operation.operator.RegressionOperator;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IBIObject;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.NumberFormatter;
/**
* * Take RegressionOperator as input. Output a JPanel shown all the records in
* the operator's MiningStoredData set. The posterior distribution probability
* is also shown. The Residual Value can be added later if necessary.
*
* @author TWang On Jan 25, 2005.
*
*/
public class LogisticDataView extends ResultView {
/**
*
*/
private static final long serialVersionUID = 1L;
// JTable that shows the data
private JTable m_DataTable;
private String[] m_DataTableHeader;
private Object[][] m_DataTableContent;
private Class[] m_DataTableType;
// JScrollPane that contains JTable
private JScrollPane m_ScrollPane;
// Weka and Xelopse data structures
private RegressionOperator m_ClusteringOperator;
private weka.classifiers.Classifier m_WekaClassifier;
private MiningAttribute[] m_Attributes;
private MiningStoredData m_MiningStoredData;
private MiningDataSpecification m_MetaData;
private MiningAttribute m_TargetMiningAttribute;
/**
* @param a_ClusteringModel
* @throws Exception
*/
public LogisticDataView(RegressionOperator a_RegressionOperator, MiningAttribute[] a_MingAttributes)
throws Exception {
super(Resource.srcStr("DataView"));
m_ViewType = ResultView.TYPE_DATA;
m_ClusteringOperator = a_RegressionOperator;
m_Attributes = a_MingAttributes;
// The condition actually is also judged in the Operator
IBIObject aBIObject = m_ClusteringOperator.getOutputBIObject();
if (aBIObject == null || aBIObject.getBIModel() == null || aBIObject.getBIData() == null) {
throw new SysException("The OutputBIObject in the ClusteringOperator is NULL");
}
IBIModel aBIModel = aBIObject.getBIModel();
IBIData aBIData = aBIObject.getBIData();
// Get the weka classifier
WekaSupervisedMiningModel supervisedMiningModel = (WekaSupervisedMiningModel) aBIModel.getMiningModel();
WekaClassifier wekaClassifier = (WekaClassifier) supervisedMiningModel.getClassifier();
m_WekaClassifier = (weka.classifiers.Classifier) wekaClassifier.getWekaClassifier();
if (!(m_WekaClassifier instanceof Logistic)) {
throw new SysException("The Classifier is not an instance of weka.classifiers.functions.Logistic");
}
// Get the MiningStoredData and MetaData
m_MiningStoredData = aBIData.getMiningStoredData();
m_MetaData = aBIData.getMetaData();
// Get the target MiningAttribute
m_TargetMiningAttribute = m_MetaData.getMiningAttribute(supervisedMiningModel.getTarget().getName());
m_ScrollPane = new JScrollPane();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
this.add(m_ScrollPane, BorderLayout.CENTER);
createView();
}
/*
* Create the view.
*/
public void createView() throws Exception {
if (m_WekaClassifier == null) {
throw new SysException("The Weka Classifier is NULL.");
} else {
createDataTable(m_ScrollPane);
m_ScrollPane.setPreferredSize(new Dimension(200, 73));
m_ScrollPane.getViewport().add(m_DataTable);
m_ScrollPane.getViewport().setBackground(Color.WHITE);
}
}
/**
* 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 JTable and attach it in the ScrollPane.
*
* @param a_ScrollPane
* @throws Exception
*/
@SuppressWarnings("unchecked")
private void createDataTable(JScrollPane a_ScrollPane) throws Exception {
// Two columns more to display index, and post probability.
// The Residual Value may be added later.
int column = m_Attributes.length + 2;
m_DataTableType = new Class[column];
m_DataTableHeader = new String[column];
MiningAttribute attribute = null;
// Create JTable header and JTable class type.
for (int coloumIndex = 0; coloumIndex < column; coloumIndex++) {
if (coloumIndex == 0) {
m_DataTableType[coloumIndex] = Integer.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("REGRESSION_INDEX");
continue;
}
if (coloumIndex == column - 1) {
m_DataTableType[coloumIndex] = String.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("REGRESSION_POST");
continue;
}
attribute = m_Attributes[coloumIndex - 1];
m_DataTableHeader[coloumIndex] = attribute.getName();
m_DataTableType[coloumIndex] = String.class;
if (attribute instanceof NumericAttribute) {
int dataType = ((NumericAttribute) attribute).getDataType();
if (dataType == NumericAttribute.DOUBLE)
m_DataTableType[coloumIndex] = Double.class;
else if (dataType == NumericAttribute.FLOAT)
m_DataTableType[coloumIndex] = Float.class;
else if (dataType == NumericAttribute.INTEGER)
m_DataTableType[coloumIndex] = Integer.class;
else if (dataType == NumericAttribute.BOOLEAN)
m_DataTableType[coloumIndex] = Boolean.class;
} else if (attribute instanceof CategoricalAttribute) {
int dataType = ((CategoricalAttribute) attribute).getDataType();
if (dataType == CategoricalAttribute.BOOLEAN)
m_DataTableType[coloumIndex] = Boolean.class;
else
m_DataTableType[coloumIndex] = String.class;
}
}
// Fill the JTable content.
if (m_MiningStoredData != null) {
ArrayList list = m_MiningStoredData.getMiningVectors();
Instances instances = transform(m_MiningStoredData);
Object[][] content = new Object[list.size()][column];
Vector<Comparable> allVec = null;
MiningVector vec = null;
for (int i = 0; i < list.size(); i++) {
vec = (MiningVector) list.get(i);
allVec = vec.toVector();
double[] m_PosProb = m_WekaClassifier.distributionForInstance(instances.instance(i));
ArrayList values = ((CategoricalAttribute) m_TargetMiningAttribute).getValues();
String posProb = "";
for (int j = 0; j < m_PosProb.length; j++) {
posProb = posProb.concat(weka.core.Utils.roundDouble(m_PosProb[j],
NumberFormatter.MAX_FRACTION_DIGIT)
+ " --> (" + values.get(j).toString() + "); ");
}
// Insert the index column
allVec.insertElementAt(new Integer(i + 1), 0);
// Add the probability column
allVec.addElement(posProb);
content[i] = allVec.toArray();
}
m_DataTableContent = content;
}
// Create Table
m_DataTable = new JTable();
// m_DataTable.setModel(new DataGridModel(m_DataTableContent,
// m_DataTableHeader, m_DataTableType));
SortingDataGridModel model = new SortingDataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType);
m_DataTable.setModel(model);
m_DataTable.setDefaultRenderer(String.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Short.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Long.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Integer.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Double.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Float.class, new DataCellRenderer(true));
model.addMouseListenerToHeader(m_DataTable);
setColumnWidth();
}
public void setColumnWidth() {
TableColumnModel tcm = m_DataTable.getColumnModel();
int count = tcm.getColumnCount();
for (int i = 1; i < count; i++) {
tcm.getColumn(i).setPreferredWidth(80);
}
m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// Make the last column large enough
tcm.getColumn(tcm.getColumnCount() - 1).setMinWidth(400);
tcm.getColumn(0).setCellRenderer(m_DataTable.getTableHeader().getDefaultRenderer());
}
/**
* Exort the data table into an excel file.
*
* Called by the OperatorResult class. The subclass of OperatorResult must
* call m_SelectedView.export() explictly if it overwirtes the export()
* function of OperatorResult.
*/
public void export() throws AppException, SysException {
// Use user home directory
File directory = new File(System.getProperty("user.dir"));
// Create and initialize file chooser for excel
JFileChooser chooser = new JFileChooser(directory);
chooser.setDialogTitle(Resource.srcStr("FileExport"));
chooser.setFileFilter(ExcelExporter.FILTER);
chooser.setSelectedFile(ExcelExporter.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) {
// Create the excel exporter with the excel file
// extension enforced to be .xls
ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
// ExcelExporter aExporter = new
// ExcelExporter(m_MiningStoredData, exportFile, true);
aExporter.export();
} else {
returnVal = chooser.showSaveDialog(this);
}
}
} else {
// Create the excel exporter with the excel file extension
// enforced to be .xls
ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
// ExcelExporter aExporter = new
// ExcelExporter(m_MiningStoredData, exportFile, true);
aExporter.export();
}
}
// tyleung 20/4/2005>>
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -