Example usage for weka.experiment CSVResultListener CSVResultListener

List of usage examples for weka.experiment CSVResultListener CSVResultListener

Introduction

In this page you can find the example usage for weka.experiment CSVResultListener CSVResultListener.

Prototype

public CSVResultListener() 

Source Link

Document

Sets temporary file.

Usage

From source file:adams.flow.sink.WekaExperimentGenerator.java

License:Open Source License

/**
 * Executes the flow item.//from   w  w w  . j  av  a2s  .c om
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Experiment exp;
    SplitEvaluator se;
    Classifier sec;
    CrossValidationResultProducer cvrp;
    RandomSplitResultProducer rsrp;
    PropertyNode[] propertyPath;
    DefaultListModel model;
    InstancesResultListener irl;
    CSVResultListener crl;

    result = null;

    if (m_ResultFile.isDirectory())
        result = "Result file points to a directory: " + m_ResultFile;
    else if (m_OutputFile.isDirectory())
        result = "Output file points to a directory: " + m_OutputFile;

    if (result == null) {
        exp = new Experiment();
        exp.setPropertyArray(new Classifier[0]);
        exp.setUsePropertyIterator(true);

        // classification or regression
        se = null;
        sec = null;
        if (m_ExperimentType == ExperimentType.CLASSIFICATION) {
            se = new ClassifierSplitEvaluator();
            sec = ((ClassifierSplitEvaluator) se).getClassifier();
        } else if (m_ExperimentType == ExperimentType.REGRESSION) {
            se = new RegressionSplitEvaluator();
            sec = ((RegressionSplitEvaluator) se).getClassifier();
        } else {
            throw new IllegalStateException("Unhandled experiment type: " + m_ExperimentType);
        }

        // crossvalidation or train/test split
        if (m_EvaluationType == EvaluationType.CROSS_VALIDATION) {
            cvrp = new CrossValidationResultProducer();
            cvrp.setNumFolds(m_Folds);
            cvrp.setSplitEvaluator(se);

            propertyPath = new PropertyNode[2];
            try {
                propertyPath[0] = new PropertyNode(se,
                        new PropertyDescriptor("splitEvaluator", CrossValidationResultProducer.class),
                        CrossValidationResultProducer.class);
                propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier", se.getClass()),
                        se.getClass());
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }

            exp.setResultProducer(cvrp);
            exp.setPropertyPath(propertyPath);

        } else if ((m_EvaluationType == EvaluationType.TRAIN_TEST_SPLIT_RANDOMIZED)
                || (m_EvaluationType == EvaluationType.TRAIN_TEST_SPLIT_ORDER_PRESERVED)) {
            rsrp = new RandomSplitResultProducer();
            rsrp.setRandomizeData(m_EvaluationType == EvaluationType.TRAIN_TEST_SPLIT_RANDOMIZED);
            rsrp.setTrainPercent(m_SplitPercentage);
            rsrp.setSplitEvaluator(se);

            propertyPath = new PropertyNode[2];
            try {
                propertyPath[0] = new PropertyNode(se,
                        new PropertyDescriptor("splitEvaluator", RandomSplitResultProducer.class),
                        RandomSplitResultProducer.class);
                propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier", se.getClass()),
                        se.getClass());
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }

            exp.setResultProducer(rsrp);
            exp.setPropertyPath(propertyPath);
        } else {
            throw new IllegalStateException("Unhandled evaluation type: " + m_EvaluationType);
        }

        // runs
        exp.setRunLower(1);
        exp.setRunUpper(m_Runs);

        // classifier
        exp.setPropertyArray((Classifier[]) m_InputToken.getPayload());

        // datasets (empty for the template)
        model = new DefaultListModel();
        exp.setDatasets(model);

        // result
        if (m_ResultFormat == ResultFormat.ARFF) {
            irl = new InstancesResultListener();
            irl.setOutputFile(new File(m_ResultFile.getAbsolutePath()));
            exp.setResultListener(irl);
        } else if (m_ResultFormat == ResultFormat.CSV) {
            crl = new CSVResultListener();
            crl.setOutputFile(new File(m_ResultFile.getAbsolutePath()));
            exp.setResultListener(crl);
        } else {
            throw new IllegalStateException("Unhandled result format: " + m_ResultFormat);
        }

        // save template
        try {
            Experiment.write(m_OutputFile.getAbsolutePath(), exp);
        } catch (Exception e) {
            result = handleException("Failed to save experiment to '" + m_OutputFile + "': ", e);
        }
    }

    return result;
}