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

📄 numerictransformationoperator.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.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.Categorization;
import com.prudsys.pdm.Transform.OneToOne.Discretization;

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.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.ValueValidator;

/**
 * ReplacementOperator is a kind of Operator
 */
public class NumericTransformationOperator extends TransformOperator {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

	public final static int CATEGORIZATION = 0;

    public final static int DISCRETIZATION = 1;

    public final static int NUM_BINS = 3;

    public final static int FIXED_BIN_WIDTH = 4;

    @SuppressWarnings("unused")
	private final static int INIT = -1;

    double[] m_MinValues;

    double[] m_MaxValues;

 	/**
	 * Set node id and update operator text of the Numeric Transformation 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 Numeric Transformation 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 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 */
        BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
        @SuppressWarnings("unused") BIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(),
                IBIModel.TYPE_CLASSIFIER);

        /* Execute transform */
        MiningTransformationFactory mtf = new MiningTransformationFactory();

        switch (mode) {
        case CATEGORIZATION:
            Categorization categ = preparecCategorization(aInputBIData
                    .getMetaData(), a_OperatorNode);
            mtf.addOneToOneMapping(categ);

            break;
        case DISCRETIZATION:
            Discretization discret = preparecDiscretization(aInputBIData
                    .getMetaData(), a_OperatorNode, aInputBIData
                    .getMiningStoredData());
            mtf.addOneToOneMapping(discret);
            break;
        }
        ;

        MiningTransformationStep mts = mtf.createMiningTransformationStep();
        XelopesTransformAction aTransformAction = new XelopesTransformAction(
                m_CaseID, m_NodeID, mts);
        MiningStoredData 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();
    }

    public Categorization preparecCategorization(
            MiningDataSpecification a_MetaData, IOperatorNode a_Node)
            throws MiningException {
        Categorization mapping = new Categorization();

        String sourceName = null;

        //get the target attribute
        sourceName = (String) a_Node.getParameterValue("target");

        mapping.setSourceName(sourceName);
        mapping.setTargetName(sourceName);

        return mapping;
    }

    @SuppressWarnings("unchecked")
	public Discretization preparecDiscretization(
            MiningDataSpecification a_MetaData, IOperatorNode a_Node,
            MiningStoredData a_InputMiningStoredData) throws MiningException {
        int method = FIXED_BIN_WIDTH;

        String value = null;
        String sourceName = null;
        int index = 0;

        Discretization mapping = new Discretization();
        double[] bounds = null;
        Vector temp = new Vector();
        double min = 0;
        double max = 0;
        int numBins = -1;
        double binWidth = -1;

        //get the target attribute
        sourceName = (String) a_Node.getParameterValue("target");

        @SuppressWarnings("unused") MiningAttribute[] attr = a_MetaData.getAttributesArray();
        MiningAttribute mAtt = a_MetaData.getMiningAttribute(sourceName);
        index = a_MetaData.getAttributeIndex(mAtt);

        calcMinMaxValues(a_InputMiningStoredData);

        min = m_MinValues[index];
        max = m_MaxValues[index];

        //get the discretization method
        value = (String) a_Node.getParameterValue("method");
        if (value == null)
            throw new MiningException(
                    "Please specify all required parameters for Categorical Transformation transformation.");
        method = Integer.parseInt(value);

        if (method == NUM_BINS) {
            value = (String) a_Node.getParameterValue("numBins");
            if (value == null)
                throw new MiningException(
                        "Please specify all required parameters for Categorical Transformation transformation.");
            numBins = Integer.parseInt(value);
            
            if(numBins ==1){
                bounds = new double[0];

            }else if (numBins == 2){
                bounds = new double[1];
                bounds[0] = (max-min)/2+ min ;
            }else{
                binWidth = (max - min) / (numBins - 2);
                
                double next = min;
                while (next <= max && binWidth>0) {
                    temp.add(new Double(next));
                    next += binWidth;
                }

                bounds = new double[temp.size()];
                for (int i = 0; i < temp.size(); i++) {
                    bounds[i] = ((Double) temp.get(i)).doubleValue();
                }
                
              
            }
        } else {
            value = (String) a_Node.getParameterValue("fixedBinWidth");
            if (value == null)
                throw new MiningException(
                        "Please specify all required parameters for Categorical Transformation transformation.");
            binWidth = Double.parseDouble(value);
            
            double next = min;
            while (next <= max && binWidth>0) {
                temp.add(new Double(next));
                next += binWidth;
            }

            bounds = new double[temp.size()];
            for (int i = 0; i < temp.size(); i++) {
                bounds[i] = ((Double) temp.get(i)).doubleValue();
            }
          
        }

      

       
        mapping.setBounds(bounds);
        mapping.setSourceName(sourceName);
        mapping.setTargetName(sourceName);

        return mapping;
    }

    private void calcMinMaxValues(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_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_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 {
        String value = null;
        String sourceName = null;
        MiningAttribute mAtt = null;
        int mode;
        int method;
        @SuppressWarnings("unused") boolean valid = true;

        String message = "";

        //validate the target attribute
        sourceName = (String) a_Node.getParameterValue("target");
        if (sourceName == null) {
            message += "Please select a numeric attribute\n";
            throw new AppException(message);
            
        } else {
            mAtt = a_MetaData.getMiningAttribute(sourceName);
            if (mAtt == null || !(mAtt instanceof NumericAttribute)) {
                message += "Please select a numeric attribute\n";
                throw new AppException(message);
            }
        }

        //validate the mode
        value = (String) a_Node.getParameterValue("mode");
        if (value == null || !ValueValidator.isNumeric(value)) {
            message += "Please select a numeric transformation method\n";
            throw new AppException(message);
        }
        mode = Integer.valueOf(value).intValue();

        if (mode == DISCRETIZATION) {
            //get the discretization method
            value = (String) a_Node.getParameterValue("method");
            if (value == null) {
                message += "Please select a discretization method\n";
                throw new AppException(message);
            }

            method = Integer.parseInt(value);
            if (method == NUM_BINS) {
                value = (String) a_Node.getParameterValue("numBins");
                if (value == null || !ValueValidator.isInteger(value)
                        || !ValueValidator.largerThan(value, 1, true)) {
                    message += "No. of bins should be an integer greater than or equal to 1\n";
                    throw new AppException(message);
                }
                
            } else if (method == FIXED_BIN_WIDTH) {
                value = (String) a_Node.getParameterValue("fixedBinWidth");
                if (value == null 
                        || !ValueValidator.isDouble(value)
                        || !ValueValidator.largerThan(value, 0.0, false)) {
                    message += "Fixed bin width should be a double greater than 0\n";
                    throw new AppException(message);
                }
                
            } else {
                message += "Please select a discretization method\n";
                throw new AppException(message);
            }
            
        } else if (mode != CATEGORIZATION) {
            message += "Please select a numeric transformation method\n";
            throw new AppException(message);
        }

        //	    if(!message.equals("")){
        //	        throw new AppException(message);
        //	    }

    }

}

⌨️ 快捷键说明

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