📄 scoreoperator.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.
*/
package eti.bi.alphaminer.patch.standard.operation.operator;
import java.util.ArrayList;
import java.util.Vector;
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.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningSettings;
import com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.MultipleToMultiple.AddAttribute;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.core.transform.DataTransformAction;
import eti.bi.alphaminer.core.transform.IDataTransformAction;
import eti.bi.alphaminer.operation.operator.DeploymentOperator;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.InputOperator;
import eti.bi.alphaminer.operation.operator.ModelOperator;
import eti.bi.alphaminer.operation.operator.Operator;
import eti.bi.alphaminer.operation.operator.TransformOperator;
import eti.bi.alphaminer.vo.BIData;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IBIObject;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
/**
* ScoreOperator is to apply model built by modeling operator to data set.
*/
public class ScoreOperator extends DeploymentOperator {
/**
*
*/
private static final long serialVersionUID = 1L;
private static ArrayList<String> acceptParentsDefinitionID = new ArrayList<String>();
/**
* @param a_CaseID
* @param a_CaseWindow
* @param aOperatorInfo
*/
public ScoreOperator(String a_CaseID, INodeInfo aNodeInfo,
ICaseHandler aCaseHandler) {
super(a_CaseID, aNodeInfo, aCaseHandler);
// TODO Auto-generated constructor stub
}
// Predicted values after applying model
private Vector<String> m_Predicted;
/**
* Set node id and update operator text of the DecisionTreeOperator at the same time.
* @param a_NodeID ID of the node
*/
public void setNodeID(String a_NodeID) {
setLabel(getDescription() + " [" + a_NodeID + "]");
super.setNodeID(a_NodeID);
}
/**
* Set node id and update operator text of the DecisionTreeOperator at the same time.
* @param a_NodeID ID of the node
*/
public void setDescription(String a_Description) {
m_Description = a_Description;
setLabel(m_Description + " [" + m_NodeID + "]");
}
/*
* Check if this operator node already containts the temp result.
*
* @see eti.bi.alphaminer.ui.operator.Operator#hasResult()
*/
public boolean hasResult() {
if (m_OutputBIObject != null) {
return (m_OutputBIObject.hasResult(BIObject.DATA));
} else {
return false;
}
}
/*
* Execute this operator
*
* @see eti.bi.alphaminer.ui.operator.Operator#execute(eti.bi.alphaminer.vo.OperatorNode,
* java.util.Vector)
*/
public void execute(IOperatorNode a_OperatorNode, Vector a_Parents)
throws SysException, AppException, MiningException {
/* Get parameter from user input */
// No parameters for score operation
/*
* Get input bi model from modeling node and bi data from data node It
* assume one of the parent is modling node, the other must be data node
*/
Operator parentOp0 = (Operator) a_Parents.elementAt(0);
Operator parentOp1 = (Operator) a_Parents.elementAt(1);
// index to identify the input from modeling node and input from data
// source node
int indexModel = -1;
int indexData = -1;
Operator dataOperator = null;
if (parentOp0 == null || parentOp1 == null) {
throw new SysException("Invalid parent operator handle.");
}
if(parentOp0 instanceof ModelOperator) {
indexModel = 0;
}
else {
dataOperator = findDataSetRecursively(parentOp0);
indexData = 1;
}
if(parentOp1 instanceof ModelOperator) {
indexModel = 1;
}
else {
if(indexData==-1) {
dataOperator = findDataSetRecursively(parentOp1);
indexData = 0;
}
}
if(dataOperator==null) {
throw new SysException("No input data!");
}
if(indexModel==-1) {
throw new SysException("No input model!");
}
// Get Model and Data from the parent operators
IBIObject biObjectModel = ((Operator) a_Parents.elementAt(indexModel))
.getOutputBIObject();
IBIObject biObjectData = dataOperator.getOutputBIObject();
// Thow exception if any of them are null object
if (biObjectModel == null) {
throw new SysException("Null Model Object");
}
if (biObjectData == null) {
throw new SysException("Null Data Object");
}
// Put the model and data into the Input object
setInputBIObject(biObjectModel, indexModel);
setInputBIObject(biObjectData, indexData);
// Get inputModel
IBIModel inputBIModel = biObjectModel.getBIModel();
// Get scoring data from data input
IBIData inputBIData = biObjectData.getBIData();
if (inputBIModel == null) {
throw new SysException("No Model built for scoring");
}
if (inputBIData == null) {
throw new SysException("No data imported for scoring");
}
// Get the transformHistory and apply the transform action to the data
// set.
Vector<IDataTransformAction> transformHistory = null;
MiningAttribute targetAttribute = null;
// Get TransformActionHistory from training data
if (getInputBIObject(indexModel).getBIData() != null) {
transformHistory = getInputBIObject(indexModel).getBIData()
.getTransformActionHistory();
targetAttribute = getInputBIObject(indexModel).getBIData()
.getTargetAttribute();
}
if (transformHistory == null) {
throw new SysException("Transform action of the training is lost");
}
if (targetAttribute == null) {
// Do Nothing
// No need to check targetAttribute, as it would not be used in
// scoring
}
// Prepare output mining data
IBIData aOutputBIData = new BIData(getCaseID(), getNodeID());
MiningStoredData miningStoredData = inputBIData.getMiningStoredData();
if (miningStoredData == null) {
throw new SysException("Invalid scoring data");
}
MiningStoredData toData = miningStoredData;
MiningStoredData fromData = null;
// Loop the transformHistory, and apply the same transform actions to
// the scoring data source
for (int i = 0; i < transformHistory.size(); i++) {
Object obj = transformHistory.get(i);
if (obj instanceof DataTransformAction) {
DataTransformAction action = (DataTransformAction) obj;
fromData = toData;
try {
toData = action.transform(fromData);
} catch (Exception e) {
throw new SysException("Unable to transform Scoring Data.");
}
}
}
MiningStoredData outputMiningStoredData = null;
outputMiningStoredData = score(toData, inputBIModel);
// Set Output Mining Data
aOutputBIData.setMiningStoredData(outputMiningStoredData);
aOutputBIData.copyTransformActionHistory(inputBIData
.getTransformActionHistory());
aOutputBIData.copyTargetAttribute(inputBIData.getTargetAttribute());
m_OutputBIObject.setBIData(aOutputBIData);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -