Example usage for weka.experiment Experiment write

List of usage examples for weka.experiment Experiment write

Introduction

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

Prototype

public static void write(String filename, Experiment exp) throws Exception 

Source Link

Document

Writes the experiment to disk.

Usage

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

License:Open Source License

/**
 * Executes the flow item.//from   w  w w. ja  va  2 s .  co  m
 *
 * @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;
}

From source file:adams.gui.goe.WekaExperimentFileEditor.java

License:Open Source License

/**
 * Gets the custom editor component.//ww  w  .  jav  a2 s .co m
 *
 * @return       a value of type 'Component'
 */
protected JComponent createCustomEditor() {
    WekaExperimentFile currentFile;
    JPanel panel;

    m_PanelEditor = new BasePanel();
    m_PanelEditor.setLayout(new GridLayout(3, 1));
    m_PanelEditor.setBorder(BorderFactory.createEmptyBorder());

    // file
    m_PanelFile = new FileChooserPanel();
    currentFile = (WekaExperimentFile) getValue();
    if (currentFile == null)
        currentFile = new WekaExperimentFile(System.getProperty("user.dir"));
    m_PanelFile.setCurrent(currentFile);
    m_PanelFile.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            File file = m_PanelFile.getCurrent();
            m_ButtonEdit.setEnabled(!file.isDirectory());
        }
    });
    panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.add(m_PanelFile);
    m_PanelEditor.add(panel);

    // edit
    m_ButtonEdit = new BaseButton("Edit");
    m_ButtonEdit.setMnemonic('E');
    m_ButtonEdit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            PlaceholderFile file = new PlaceholderFile(m_PanelFile.getCurrent());
            Experiment exp = null;
            if (file.exists()) {
                try {
                    exp = Experiment.read(file.getAbsolutePath());
                } catch (Exception ex) {
                    ex.printStackTrace();
                    exp = null;
                }
            }
            if (exp == null)
                exp = newExperiment();
            SimpleSetupDialog dlg;
            if (m_PanelEditor.getParentDialog() != null)
                dlg = new SimpleSetupDialog(m_PanelEditor.getParentDialog());
            else
                dlg = new SimpleSetupDialog(m_PanelEditor.getParentFrame());
            dlg.setExperiment(exp);
            dlg.setLocationRelativeTo(dlg.getOwner());
            dlg.setVisible(true);
            try {
                Experiment.write(new PlaceholderFile(m_PanelFile.getCurrent()).getAbsolutePath(),
                        dlg.getExperiment());
            } catch (Exception ex) {
                ex.printStackTrace();
                GUIHelper.showErrorMessage(dlg.getOwner(), "Couldn't save WEKA experiment file:\n"
                        + m_PanelFile.getCurrent() + "\nReason:\n" + ex.getMessage(), "Save WEKA Experiment");
            }
        }
    });
    panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.add(m_ButtonEdit);
    m_PanelEditor.add(panel);

    // buttons
    m_ButtonOK = new BaseButton("OK");
    m_ButtonOK.setMnemonic('O');
    m_ButtonOK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setValue(new WekaExperimentFile(m_PanelFile.getCurrent()));
            m_PanelEditor.closeParent();
        }
    });
    m_ButtonCancel = new BaseButton("Cancel");
    m_ButtonCancel.setMnemonic('C');
    m_ButtonCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            m_PanelEditor.closeParent();
        }
    });
    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(m_ButtonOK);
    panel.add(m_ButtonCancel);
    m_PanelEditor.add(panel);

    return m_PanelEditor;
}