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

📄 outliertreatmentoperator.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.
 */

package eti.bi.alphaminer.patch.standard.operation.operator;


import java.util.Vector;


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.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.OneToOne.TreatOutlierAttributeValue;

import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.core.transform.XelopesTransformAction;
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.BIModel;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.ValueValidator;

/**
 * ReplacementOperator is a kind of Operator
 */
public class OutlierTreatmentOperator extends TransformOperator {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param a_CaseID
	 * @param a_CaseWindow
	 * @param aOperatorInfo
	 */
	public OutlierTreatmentOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
		super(a_CaseID, aNodeInfo, aCaseHandler);
		// TODO Auto-generated constructor stub
	}

	NumericAttribute m_TargetNumAttr = null; 
    
    private final static String CATEG = "Categorical";
	private final static String NUM = "Numeric";
	
    double m_PreLowerBound = Double.NEGATIVE_INFINITY;
    double m_PreUpperBound = Double.POSITIVE_INFINITY;
        
    double m_UpperBound = 10.0;
	double m_LowerBound = -10.0;
	
	/**
	 * Set node id and update operator text of the Outlier Treatment 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 Outlier Treatment 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, AppException, SysException 
	{
	    
	    /* Get input bi object from parent node */
   		Operator parentOp = (Operator)a_Parents.elementAt(0);
   		setInputBIObject(parentOp.getOutputBIObject());
   		IBIData aInputBIData = getInputBIObject().getBIData();

   		
   		/* Get input bi object from parent node */
   		validateParameters(aInputBIData.getMetaData(),a_OperatorNode);
//	    String aTargetAttrName = a_OperatorNode.getParameterValue("target");
   		   		
	   
   		/* Prepare output data model */ 
   		BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
   		@SuppressWarnings("unused") BIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(), IBIModel.TYPE_CLASSIFIER);
   		
   		
   		/* Execute transform */
   		MiningTransformationFactory mtf = new MiningTransformationFactory();
   		TreatOutlierAttributeValue rep = prepareOutlierTreatment(aInputBIData.getMetaData(),a_OperatorNode);
		mtf.addOneToOneMapping(rep);
		MiningTransformationStep mts = mtf.createMiningTransformationStep();
		XelopesTransformAction aTransformAction = new XelopesTransformAction(m_CaseID, m_NodeID, mts);
		MiningStoredData aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
   		
		String sourceName = (String) a_OperatorNode.getParameterValue("target");
		if (aOutputMiningStoredData.getMetaData().getMiningAttribute(sourceName) instanceof NumericAttribute)
		{
			m_TargetNumAttr = (NumericAttribute)aOutputMiningStoredData.getMetaData().getMiningAttribute(sourceName);
			
			// Modify the attribute in the output metadata
			if(m_TargetNumAttr !=null){
				@SuppressWarnings("unused") String treatment = (String) a_OperatorNode.getParameterValue("outlier");
				
				// If outlers are treated as extreme values, set the max/min as the new bounds
//				if (treatment.equals(TreatOutlierAttributeValue.OUTLIER_TREATMENT_METHOD_asExtremeValues))
//				{
//					double min = Double.parseDouble(a_OperatorNode.getParameterValue("min"));
//					double max = Double.parseDouble(a_OperatorNode.getParameterValue("max"));
//					m_TargetNumAttr.setLowerBound(min);
//				    m_TargetNumAttr.setUpperBound(max);
//				}else
//				{
				// If outliers are treated as missing value, set the lower/upper bouds as the new bounds
				    m_TargetNumAttr.setLowerBound(m_LowerBound);
				    m_TargetNumAttr.setUpperBound(m_UpperBound);
//				}
			}
		}
		
   		/* Set Output Mining Data */
		aOutputBIData.setMiningStoredData(aOutputMiningStoredData);
		aOutputBIData.copyTransformActionHistory(aInputBIData.getTransformActionHistory());
		aOutputBIData.addTransformActionHistory(aTransformAction);
//		MiningAttribute aTargetAttribute = (MiningAttribute) aOutputBIData.getMetaData().getMiningAttribute(aTargetAttrName);
//		aOutputBIData.setTargetAttribute(aTargetAttribute);
		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 temp data */
		//aOutputBIData.writeTempBIData();   		
   	}
		
	
	public TreatOutlierAttributeValue prepareOutlierTreatment(MiningDataSpecification a_MetaData, IOperatorNode a_Node) 
		throws MiningException, SysException
	{
	    TreatOutlierAttributeValue outliers = new TreatOutlierAttributeValue();
		
		@SuppressWarnings("unused") String value = null;
		MiningAttribute mAtt =null;
		@SuppressWarnings("unused") int attributeIndex = 0;
		String sourceName = null;
		String treatment = null;
		@SuppressWarnings("unused") double min = 0.0;
		@SuppressWarnings("unused") double max = 100;
		
		
		
		sourceName = (String) a_Node.getParameterValue("target");
		try {
			// Clone a attribute as the assessment attribute
			mAtt = (MiningAttribute)a_MetaData.getMiningAttribute(sourceName).clone();
		} catch (CloneNotSupportedException e) {
			throw new SysException("Fail to clone MiningAttribute");
		}
	 
//		treatment = a_Node.getParameterValue("outlier");
	   	treatment = TreatOutlierAttributeValue.OUTLIER_TREATMENT_METHOD_asMissingValues;
	   	
	    outliers.setSourceName(sourceName);
	    outliers.setTargetName(sourceName);
	    outliers.setRemoveSourceAttribute(true);
        outliers.setAssessmentAttribute(mAtt);
	    outliers.setOutliers(treatment);
	   
	    if(mAtt!=null){
	        if (mAtt instanceof NumericAttribute) {
	            m_TargetNumAttr = (NumericAttribute)mAtt;
	            outliers.setSourceName(sourceName);
	            outliers.setAssessmentAttribute(mAtt);
	            	                       
//	            if(treatment.equals(TreatOutlierAttributeValue.OUTLIER_TREATMENT_METHOD_asExtremeValues)){
//	                min = Double.parseDouble(a_Node.getParameterValue("min"));
//		    	    outliers.setLowValue(min);
//		    	    
//		    	    max = Double.parseDouble(a_Node.getParameterValue("max"));
//		    	    outliers.setHighValue(max);
//		    	}
	            
	            m_LowerBound = Double.parseDouble((String) a_Node.getParameterValue("lowerBound"));
	    	    ((NumericAttribute)mAtt).setLowerBound(m_LowerBound);
	    	    m_UpperBound = Double.parseDouble((String) a_Node.getParameterValue("upperBound"));
	    	    ((NumericAttribute)mAtt).setUpperBound(m_UpperBound);
	        }
	    }
	    
		return outliers;
	}
	
	private void validateParameters(MiningDataSpecification a_MetaData, IOperatorNode a_Node) 
		throws AppException
	{
	    String sourceName = null;
	    String targetType = null;
		@SuppressWarnings("unused") String outlier = null;
		@SuppressWarnings("unused") String value = null;
		@SuppressWarnings("unused") String min = null;
		@SuppressWarnings("unused") String max = null;
		String lowerBound = null;
		String upperBound = null;
		MiningAttribute mAtt = null;
		@SuppressWarnings("unused") int mode;
		boolean valid = true;
		String message ="";
		
//		validate the target attribute
	    sourceName = (String) a_Node.getParameterValue("target");
	    if (sourceName == null){
	        message += Resource.srcStr("AttributeMessage");
	        throw new AppException(message);
	    }
	    else {
	        mAtt = a_MetaData.getMiningAttribute(sourceName);
	        if(mAtt == null) {
	            message += Resource.srcStr("AttributeMessage");
	            throw new AppException(message);
	        }
	    }
	 
	 
	    if(mAtt instanceof NumericAttribute){
	        targetType = (String) a_Node.getParameterValue("targetType");
	        
	        if(targetType ==null
                    || !targetType.equals(NUM)){
	                message += Resource.srcStr("AttributeMessage");
		            throw new AppException (message);	
            }
	        
//		    outlier = a_Node.getParameterValue("outlier");
//			if(outlier == null){
//			    message += "Please select an outlier treatment\n"; 
//			    throw new AppException(message);
//			}
//			if(outlier != null && outlier.equals(TreatOutlierAttributeValue.OUTLIER_TREATMENT_METHOD_asExtremeValues)){
//			    valid = true;
//			    
//			    min =a_Node.getParameterValue("min");
//			    if(min == null || !ValueValidator.isDouble(min)){
//			        message += "Minimum should be a double\n";
//			        valid = false;
//			        throw new AppException(message);
//			    }
//			    
//			    max = a_Node.getParameterValue("max"); 
//			    if(max == null || !ValueValidator.isDouble(max)){
//			        message += "Maximum should be a double\n";
//			        valid = false;
//			        throw new AppException(message);
//			    }
//			    
//			    if(valid){
//			        if(!ValueValidator.largerThan(max, Double.parseDouble(min), false)){
//			            message += "Maximum should not be larger than minimum\n";
//			            throw new AppException(message);
//			        }
//			    }
//			}
		   
			valid = true;
			
			lowerBound = (String) a_Node.getParameterValue("lowerBound");
			if(lowerBound == null || !ValueValidator.isDouble(lowerBound)){
		        message += "Lower bound should be a double\n";
		        valid = false;
		        throw new AppException(message);
			}    
			
			upperBound = (String) a_Node.getParameterValue("upperBound");
			if(upperBound==null || !ValueValidator.isDouble(upperBound)){
		        message += "Upper bound should be a double\n";
		        valid = false;
		        throw new AppException(message);
			}
			
			 if(valid){
		        if(!ValueValidator.largerThan(upperBound, Double.parseDouble(lowerBound), false)){
		            message += "Upper bound should be larger than lower bound\n";
		            throw new AppException(message);
		        }
		    }
		 
	    }
	    else {
	        targetType = (String) a_Node.getParameterValue("targetType");
	        
	        if(targetType ==null
                    || !targetType.equals(CATEG)){
	                message += Resource.srcStr("AttributeMessage");
		            throw new AppException (message);	
            }
	        
//	        outlier = a_Node.getParameterValue("outlier");
//			if(outlier == null ||
//			        !outlier.equals(TreatOutlierAttributeValue.OUTLIER_TREATMENT_METHOD_asMissingValues)){
//			    message += "Please select an outlier treatment\n"; 
//			    throw new AppException(message);
//			}
	    }
	    
//		if(!message.equals(""))
//	   	    throw new AppException(message);
	    
	
	}
}

⌨️ 快捷键说明

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