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

📄 normalizeoperator.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 2004-10-8
 *
 * 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.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.MiningInputStream;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Models.Statistics.SimpleStats;
import com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.OneToOne.LinearNormal;
import com.prudsys.pdm.Transform.OneToOne.ZetNormal;

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.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;

/**
 * @author fxu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class NormalizeOperator extends TransformOperator {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

	private double [] m_MeanValues;
    private double [] m_DevValues;
    private double [] m_MinValues;
    private double [] m_MaxValues;

    public final static int LINEAR_NORMAL = 0;
    public final static int ZET_NORMAL = 1;
	
	/**
	 * Set node id and update operator text of the Normalization 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 Normalization 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 LinearNormal prepareLinearNormal(MiningDataSpecification a_MetaData, IOperatorNode a_Node, MiningStoredData a_InputMiningStoredData)throws MiningException{
		LinearNormal linearNormal = new LinearNormal();
		
		String sourceName;
//		String outlier = null;
		String value = null;
		double lowerBound;
		double upperBound;
		double min=0.0;
		double max=0.0;
		int index;
				
		sourceName = (String) a_Node.getParameterValue("target");
		MiningAttribute sourceAttr = a_MetaData.getMiningAttribute(sourceName);
		index = a_MetaData.getAttributeIndex(sourceAttr);
		
		//caculate the mean and standard deviation of the dataset
		calcStatValues(a_InputMiningStoredData);
		
		//needa to be modified when there's user input
		min = m_MinValues[index];
		max = m_MaxValues[index];		
	
//		outlier = a_Node.getParameterValue("outlier");
			
			 
//		if(outlier.equals(LinearNormal.OUTLIER_TREATMENT_METHOD_asExtremeValues)){
//		    value = a_Node.getParameterValue("min");
//		    min = Double.parseDouble(value);
//		   
//		    value = a_Node.getParameterValue("max");
//		    max = Double.parseDouble(value);
//		}
	   
		value = (String) a_Node.getParameterValue("lowerBound");
		lowerBound = Double.parseDouble(value);
			   
		value = (String) a_Node.getParameterValue("upperBound");
		upperBound = Double.parseDouble(value);
	   	
	     	
	   	linearNormal.setSourceName(sourceName);
	   	linearNormal.setTargetName(sourceName);
	   	linearNormal.setOutliers(LinearNormal.OUTLIER_TREATMENT_METHOD_asMissingValues);
	   	linearNormal.setLowerBound(lowerBound);
	   	linearNormal.setUpperBound(upperBound);
	   
	   	//if (outlier.equals(LinearNormal.OUTLIER_TREATMENT_METHOD_asExtremeValues)){
   	    linearNormal.setMin(min);
   	    linearNormal.setMax(max);
	   	//}	  
	   
	   	return linearNormal;
	}
	
	public ZetNormal prepareZetNormal(MiningDataSpecification a_MetaData, IOperatorNode a_Node, MiningStoredData a_InputMiningStoredData) throws MiningException, AppException
	{		
	    ZetNormal zetNormal = new ZetNormal();
	    String sourceName = null;
	    int index;
	    
	    sourceName = (String) a_Node.getParameterValue("target");
	   	    
	    MiningAttribute sourceAttr = a_MetaData.getMiningAttribute(sourceName);
		index = a_MetaData.getAttributeIndex(sourceAttr);
		
		//caculate the mean and standard deviation of the dataset
		calcStatValues(a_InputMiningStoredData);
		
		//needa to be modified when there's user input
		double dev = m_DevValues[index];
		double mean = m_MeanValues[index];		
		
		zetNormal.setSourceName(sourceName);
		zetNormal.setTargetName(sourceName);
		zetNormal.setDeviation(dev);
		zetNormal.setMean(mean);
		return zetNormal;		
	}

	public boolean hasResult()
	{
		if (m_OutputBIObject != null)
		{
			return (m_OutputBIObject.hasResult(BIObject.DATA));
		}else
		{
			return false;
		}
	}

	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 aTargetAttrName = a_OperatorNode.getParameterValue("target");
	 	int mode = Integer.parseInt((String) a_OperatorNode.getParameterValue("mode"));
	   
	 	
		/* Prepare output mining data */
//		aOutputMiningStoredData.reset();
		BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
		
		
		/* execute this node */
		MiningTransformationFactory mtf = new MiningTransformationFactory();
		XelopesTransformAction aTransformAction = null;
		
		MiningStoredData aOutputMiningStoredData;
		if(mode==ZET_NORMAL){
		    ZetNormal zetNormal = prepareZetNormal(aInputBIData.getMetaData(),a_OperatorNode, aInputBIData.getMiningStoredData());
		    mtf.addOneToOneMapping(zetNormal);
			MiningTransformationStep mts = mtf.createMiningTransformationStep();
			aTransformAction = new XelopesTransformAction(m_CaseID, m_NodeID, mts);
			aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
		}else {
		    LinearNormal linearNormal = prepareLinearNormal(aInputBIData.getMetaData(),a_OperatorNode, aInputBIData.getMiningStoredData());
		    mtf.addOneToOneMapping(linearNormal);
			MiningTransformationStep mts = mtf.createMiningTransformationStep();
			aTransformAction = new XelopesTransformAction(m_CaseID, m_NodeID, mts);
			aOutputMiningStoredData = aTransformAction.transform(aInputBIData.getMiningStoredData());
		}


		/* 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();
		
	}
	
	 private void calcStatValues(MiningInputStream a_InputStream) throws MiningException {

	     // Calculate simple statistics:
	     SimpleStats sist = new SimpleStats();
	     sist.setInputStream(a_InputStream);
	     sist.runCalculation(true);

	     // Fill arrays of mean and deviation values:
	     MiningDataSpecification metaData = a_InputStream.getMetaData();
	     int nAtt  = metaData.getAttributesNumber();
	     m_MeanValues = new double[nAtt];
	     m_DevValues = new double[nAtt];
	     m_MinValues = new double[nAtt];
         m_MaxValues = new double[nAtt];
         
	     for (int i = 0; i < nAtt; i++) {
	       MiningAttribute att = metaData.getMiningAttribute(i);
	       if (att instanceof NumericAttribute) {
	         m_MeanValues[i]= sist.getCalculatedValue(att, SimpleStats.STAT_MEAN );
	         m_DevValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_DEVIATION );
	         m_MinValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MIN);
             m_MaxValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MAX);
	       };
	     };
	   }
	
	 private void validateParameters(MiningDataSpecification a_MetaData, IOperatorNode a_Node, MiningStoredData a_InputMiningStoredData)throws AppException{
	    	
		@SuppressWarnings("unused") String outlier = null;
		String value = null;
		@SuppressWarnings("unused") String min = null;
		@SuppressWarnings("unused") String max = null;
		String lowerBound = null;
		String upperBound = null;
		MiningAttribute mAtt = null;
		int mode;
		boolean valid = true;
		String message ="";
		
		value = (String) a_Node.getParameterValue("target");
		if(value == null){
		    message += "Please select a numeric attribute\n"; 
		    throw new AppException(message);
		}
	    else {
			mAtt = a_MetaData.getMiningAttribute(value);
	        if(mAtt == null || !(mAtt instanceof NumericAttribute)){
	            message += "Please select a numeric attribute\n";
	        	throw new AppException(message);
	        }
		}    
        
		 value  = (String) a_Node.getParameterValue("mode");
		    if(value ==null  || !ValueValidator.isNumeric(value)){
		        message += "Please select a normalization method\n";
		        throw new AppException(message);
		    }   
		    
	    mode = Integer.valueOf(value).intValue();
	    
	    if(mode == LINEAR_NORMAL){
//			outlier = a_Node.getParameterValue("outlier");
//			if(!(outlier != null
//			        || outlier.equals(LinearNormal.OUTLIER_TREATMENT_METHOD_asExtremeValues)
//			        || outlier.equals(LinearNormal.OUTLIER_TREATMENT_METHOD_asIs)
//			        || outlier.equals(LinearNormal.OUTLIER_TREATMENT_METHOD_asMissingValues)
//		        ))
//			{
//			    message += "Please select an outlier treatment\n"; 
//			    throw new AppException(message);
//			}
			    

//		    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 if(mode != ZET_NORMAL){
	        message += "Please select a normalization method\n";
	        throw new AppException(message);
	    }
	    
//	   	if(!message.equals(""))
//	   	    throw new AppException(message);
	     
	 }
	 
	 
		
			
		
	
}

⌨️ 快捷键说明

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