📄 samplingoperator.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.io.File;
import java.util.Vector;
import weka.filters.unsupervised.instance.Resample;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningStoredData;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.core.transform.DataTransformAction;
import eti.bi.alphaminer.core.transform.StandardTransformAction;
import eti.bi.alphaminer.core.transform.WekaTransformAction;
import eti.bi.alphaminer.core.transform.filter.FirstNSampling;
import eti.bi.alphaminer.core.transform.filter.OneInNSampling;
import eti.bi.alphaminer.operation.operator.INodeInfo;
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.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.ValueValidator;
/**
* SamplingOperator is a kind of Operator
*/
public class SamplingOperator extends TransformOperator {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param a_CaseID
* @param a_CaseWindow
* @param aOperatorInfo
*/
public SamplingOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
super(a_CaseID, aNodeInfo, aCaseHandler);
// TODO Auto-generated constructor stub
}
public final static int RANDOM_SAMPLING = 0;
public final static int ONE_IN_N_SAMPLING = 1;
public final static int FIRST_N_SAMPLING = 2;
private int m_Mode = RANDOM_SAMPLING;
private int m_RandomSeed = 1;
private int m_SampleSize = 50;
private int m_OneInN_NValue = 1;
private int m_FirstN_NValue = 1;
/**
* Set node id and update operator text of the Sampling 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 Sampling 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 + "]");
}
/* (non-Javadoc)
* @see eti.bi.alphaminer.ui.operator.Operator#hasResult()
*/
public boolean hasResult() {
/* Normally the operator has to check if the output data object and/or the output
* model object are exist by calling m_OutputBIObject.hasResult(int) by passing BIObject.DATA
* and BIObject.MODEL respectively. You may check both existance or either one by simple modifying
* the logic below.
*/
if (m_OutputBIObject != null)
{
return (m_OutputBIObject.hasResult(BIObject.DATA));
}else
{
return false;
}
}
/* (non-Javadoc)
* @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 MiningException, SysException, AppException
{
/* Get input bi object from parent node */
Operator parentOp = (Operator)a_Parents.elementAt(0);
setInputBIObject(parentOp.getOutputBIObject());
IBIData aInputBIData = getInputBIObject().getBIData();
/* Get parameter from user input */
validateParameters(aInputBIData.getMetaData(),a_OperatorNode, aInputBIData.getMiningStoredData());
String value = (String) a_OperatorNode.getParameterValue("mode");
if(value !=null && ValueValidator.isNumeric(value)){
m_Mode = Integer.parseInt(value );
}
/* Prepare output mining data */
BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
/* Execute transform */
// MiningTransformationFactory mtf = new MiningTransformationFactory();
DataTransformAction aTransformAction =null;
MiningStoredData aOutputMiningStoredData = null;
switch (m_Mode){
case RANDOM_SAMPLING:
Resample resample = prepareRandomSample(aInputBIData.getMetaData(),a_OperatorNode);
// mtf.addMultipleToMultipleMapping(resample);
// MiningTransformationStep mts = mtf.createMiningTransformationStep();
File tempFile = aOutputBIData.getTempFile();
aTransformAction = new WekaTransformAction(m_CaseID, m_NodeID, tempFile.getAbsolutePath(), resample);
aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
break;
case ONE_IN_N_SAMPLING:
OneInNSampling oneInNSampling = prepareOneInNSampling(aInputBIData.getMetaData(),a_OperatorNode);
aTransformAction = new StandardTransformAction(m_CaseID, m_NodeID, oneInNSampling);
aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
break;
case FIRST_N_SAMPLING:
FirstNSampling firstNSampling = prepareFirstNSampling(aInputBIData.getMetaData(),a_OperatorNode);
aTransformAction = new StandardTransformAction(m_CaseID, m_NodeID, firstNSampling);
aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
break;
}
/* Set Output Mining Data */
aOutputBIData.setMiningStoredData(aOutputMiningStoredData);
aOutputBIData.copyTransformActionHistory(aInputBIData.getTransformActionHistory());
// aOutputBIData.addTransformActionHistory(aTransformAction);
// aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
aOutputBIData.copyTargetAttribute(aInputBIData.getTargetAttribute());
m_OutputBIObject.setBIData(aOutputBIData);
/* set run time parameter value to the node object (It needs to be stored in the BIML) */
//a_OperatorNode.setParameterValue("Temporary data", aOutputBIData.getTempBIDataPath());
/* Write the output BIData or BIModel to temp space for operations to retrieve */
//aOutputBIData.writeTempBIData();
}
public Resample prepareRandomSample(MiningDataSpecification a_MetaData, IOperatorNode a_Node)
{
Resample resample = new Resample();
// String value = null;
// int randomSeed = 1;
// double sampleSize = 10;
// value = a_Node.getParameterValue("randomSeed");
// randomSeed = Integer.parseInt(value);
//
// value = a_Node.getParameterValue("sampleSize");
// sampleSize = Double.parseDouble(value);
resample.setRandomSeed(m_RandomSeed);
resample.setSampleSizePercent(m_SampleSize);
return resample;
}
public FirstNSampling prepareFirstNSampling(MiningDataSpecification a_MetaData, IOperatorNode a_Node) throws MiningException{
FirstNSampling firstNSampling = new FirstNSampling();
// String value = null;
// int nValue = 1;
//
// value = a_Node.getParameterValue("firstNValue");
// if(value ==null)
// throw new MiningException ("Please specify all required parameters for Sampling transformation.");
// m_FirstN_NValue = Integer.parseInt(value);
firstNSampling.setN(m_FirstN_NValue);
return firstNSampling;
}
public OneInNSampling prepareOneInNSampling(MiningDataSpecification a_MetaData, IOperatorNode a_Node) throws MiningException{
OneInNSampling oneInNSampling = new OneInNSampling();
// String value = null;
// int nValue = 1;
//
// value = a_Node.getParameterValue("nValue");
// if(value ==null)
// throw new MiningException ("Please specify all required parameters for Sampling transformation.");
// m_OneInN_NValue = Integer.parseInt(value);
oneInNSampling.setN(m_OneInN_NValue);
return oneInNSampling;
}
private void validateParameters(MiningDataSpecification a_MetaData, IOperatorNode a_Node, MiningStoredData a_msd)
throws AppException
{
String value = null;
// int mode = RANDOM_SAMPLING;
String message = "";
value = (String) a_Node.getParameterValue("mode");
// if(value ==null || !ValueValidator.isNumeric(value)){
if(value !=null && !ValueValidator.isNumeric(value)){
message += "Please select a sampling method\n";
throw new AppException(message);
}
if(value !=null && ValueValidator.isNumeric(value)){
m_Mode = Integer.parseInt(value );
}
switch(m_Mode){
case ONE_IN_N_SAMPLING:
value = (String) a_Node.getParameterValue("nValue");
if(value != null &&
(!ValueValidator.isNumeric(value)
|| !ValueValidator.largerThan(value, 1, true)
|| !ValueValidator.smallerThan(value, a_msd.getVectorsNumber(), true))){
message += "n value should be an integer between 1 and "+ a_msd.getVectorsNumber()+"\n";
throw new AppException(message);
}
if(value !=null && ValueValidator.isInteger(value))
m_OneInN_NValue = Integer.parseInt(value);
break;
case FIRST_N_SAMPLING:
value = (String) a_Node.getParameterValue("firstNValue");
if(value != null &&
(!ValueValidator.isNumeric(value)
|| !ValueValidator.largerThan(value, 1, true)
|| !ValueValidator.smallerThan(value, a_msd.getVectorsNumber(), true))){
message += "n value should be an integer between 1 and "+ a_msd.getVectorsNumber()+"\n";
throw new AppException(message);
}
if(value !=null && ValueValidator.isInteger(value))
m_FirstN_NValue = Integer.parseInt(value);
break;
case RANDOM_SAMPLING:
value = (String) a_Node.getParameterValue("randomSeed");
if(value !=null &&
( !ValueValidator.isNumeric(value)
|| !ValueValidator.largerThan(value, 1, true))){
message += "Random seed should be an integer greater than or equal to 1\n";
throw new AppException(message);
}
if(value !=null && ValueValidator.isInteger(value))
m_RandomSeed = Integer.parseInt(value);
value = (String) a_Node.getParameterValue("sampleSize");
if(value !=null &&
( !ValueValidator.isNumeric(value)
|| !ValueValidator.largerThan(value, 0, true)
|| !ValueValidator.smallerThan(value, 100, true))){
message += "Sample size should be an integer between 0 and 100";
throw new AppException(message);
}
if(value !=null && ValueValidator.isInteger(value))
m_SampleSize = Integer.parseInt(value);
break;
default:
message += "Please select a sampling method\n";
throw new AppException(message);
}
// if (!message.equals("")) {
// throw new AppException(message);
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -