Java tutorial
/* * 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 3 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, see <http://www.gnu.org/licenses/>. */ /* * WekaTrainTestSetEvaluator.java * Copyright (C) 2009-2017 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.flow.container.WekaEvaluationContainer; import adams.flow.container.WekaTrainTestSetContainer; import adams.flow.core.Token; import adams.flow.provenance.ActorType; import adams.flow.provenance.Provenance; import adams.flow.provenance.ProvenanceContainer; import adams.flow.provenance.ProvenanceInformation; import adams.flow.provenance.ProvenanceSupporter; import weka.classifiers.Evaluation; import weka.classifiers.evaluation.output.prediction.Null; import weka.core.Instances; /** <!-- globalinfo-start --> * Trains a classifier on an incoming training dataset (from a container) and then evaluates it on the test set (also from a container).<br> * The classifier setup being used in the evaluation is a callable 'Classifier' actor. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * adams.flow.container.WekaTrainTestSetContainer<br> * - generates:<br> * adams.flow.container.WekaEvaluationContainer<br> * <br><br> * Container information:<br> * - adams.flow.container.WekaTrainTestSetContainer: Train, Test, Seed, FoldNumber, FoldCount<br> * - adams.flow.container.WekaEvaluationContainer: Evaluation, Model, Prediction output * <br><br> <!-- flow-summary-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-name <java.lang.String> (property: name) * The name of the actor. * default: WekaTrainTestSetEvaluator * </pre> * * <pre>-annotation <adams.core.base.BaseAnnotation> (property: annotations) * The annotations to attach to this actor. * default: * </pre> * * <pre>-skip <boolean> (property: skip) * If set to true, transformation is skipped and the input token is just forwarded * as it is. * default: false * </pre> * * <pre>-stop-flow-on-error <boolean> (property: stopFlowOnError) * If set to true, the flow gets stopped in case this actor encounters an error; * useful for critical actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console. * default: false * </pre> * * <pre>-output <weka.classifiers.evaluation.output.prediction.AbstractOutput> (property: output) * The class for generating prediction output; if 'Null' is used, then an Evaluation * object is forwarded instead of a String. * default: weka.classifiers.evaluation.output.prediction.Null * </pre> * * <pre>-always-use-container <boolean> (property: alwaysUseContainer) * If enabled, always outputs an evaluation container. * default: false * </pre> * * <pre>-classifier <adams.flow.core.CallableActorReference> (property: classifier) * The callable classifier actor to train and evaluate on the test data. * default: WekaClassifierSetup * </pre> * * <pre>-no-predictions <boolean> (property: discardPredictions) * If enabled, the collection of predictions during evaluation is suppressed, * wich will conserve memory. * default: false * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class WekaTrainTestSetEvaluator extends AbstractCallableWekaClassifierEvaluator implements ProvenanceSupporter { /** for serialization. */ private static final long serialVersionUID = -1092101024095887007L; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Trains a classifier on an incoming training dataset (from a container) " + "and then evaluates it on the test set (also from a container).\n" + "The classifier setup being used in the evaluation is a callable 'Classifier' actor."; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ @Override public String classifierTipText() { return "The callable classifier actor to train and evaluate on the test data."; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->adams.flow.container.WekaTrainTestSetContainer.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { WekaTrainTestSetContainer.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances train; Instances test; weka.classifiers.Classifier cls; Evaluation eval; WekaTrainTestSetContainer cont; result = null; test = null; try { // cross-validate classifier cls = getClassifierInstance(); if (cls == null) throw new IllegalStateException("Classifier '" + getClassifier() + "' not found!"); cont = (WekaTrainTestSetContainer) m_InputToken.getPayload(); train = (Instances) cont.getValue(WekaTrainTestSetContainer.VALUE_TRAIN); test = (Instances) cont.getValue(WekaTrainTestSetContainer.VALUE_TEST); cls.buildClassifier(train); initOutputBuffer(); m_Output.setHeader(train); eval = new Evaluation(train); eval.setDiscardPredictions(m_DiscardPredictions); eval.evaluateModel(cls, test, m_Output); // broadcast result if (m_Output instanceof Null) { m_OutputToken = new Token(new WekaEvaluationContainer(eval, cls)); } else { if (m_AlwaysUseContainer) m_OutputToken = new Token( new WekaEvaluationContainer(eval, cls, m_Output.getBuffer().toString())); else m_OutputToken = new Token(m_Output.getBuffer().toString()); } } catch (Exception e) { m_OutputToken = null; result = handleException("Failed to evaluate: ", e); } if (m_OutputToken != null) { if (m_OutputToken.getPayload() instanceof WekaEvaluationContainer) { if (test != null) ((WekaEvaluationContainer) m_OutputToken.getPayload()) .setValue(WekaEvaluationContainer.VALUE_TESTDATA, test); } updateProvenance(m_OutputToken); } return result; } /** * Updates the provenance information in the provided container. * * @param cont the provenance container to update */ public void updateProvenance(ProvenanceContainer cont) { if (Provenance.getSingleton().isEnabled()) { if (m_InputToken.hasProvenance()) cont.setProvenance(m_InputToken.getProvenance().getClone()); cont.addProvenance(new ProvenanceInformation(ActorType.EVALUATOR, m_InputToken.getPayload().getClass(), this, m_OutputToken.getPayload().getClass())); } } }