adams.data.conversion.WekaInstancesToSpreadSheet.java Source code

Java tutorial

Introduction

Here is the source code for adams.data.conversion.WekaInstancesToSpreadSheet.java

Source

/*
 *   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/>.
 */

/**
 * WekaInstancesToSpreadSheet.java
 * Copyright (C) 2011-2016 University of Waikato, Hamilton, New Zealand
 */
package adams.data.conversion;

import adams.core.DateTimeMsec;
import adams.data.spreadsheet.DataRow;
import adams.data.spreadsheet.DefaultSpreadSheet;
import adams.data.spreadsheet.DenseDataRow;
import adams.data.spreadsheet.Row;
import adams.data.spreadsheet.SpreadSheet;
import adams.data.spreadsheet.SpreadSheetTypeHandler;
import adams.ml.data.Dataset;
import adams.ml.data.InstancesView;
import weka.core.Attribute;
import weka.core.Instances;

import java.util.Date;

/**
 <!-- globalinfo-start -->
 * Generates a spreadsheet from a weka.core.Instances object.
 * <br><br>
 <!-- globalinfo-end -->
 *
 <!-- options-start -->
 * <pre>-logging-level &lt;OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST&gt; (property: loggingLevel)
 * &nbsp;&nbsp;&nbsp;The logging level for outputting errors and debugging output.
 * &nbsp;&nbsp;&nbsp;default: WARNING
 * </pre>
 * 
 * <pre>-data-row-type &lt;adams.data.spreadsheet.DataRow&gt; (property: dataRowType)
 * &nbsp;&nbsp;&nbsp;The type of row to use for the data.
 * &nbsp;&nbsp;&nbsp;default: adams.data.spreadsheet.DenseDataRow
 * </pre>
 * 
 * <pre>-spreadsheet-type &lt;adams.data.spreadsheet.SpreadSheet&gt; (property: spreadSheetType)
 * &nbsp;&nbsp;&nbsp;The type of spreadsheet to use for the data.
 * &nbsp;&nbsp;&nbsp;default: adams.data.spreadsheet.SpreadSheet
 * </pre>
 * 
 <!-- options-end -->
 *
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision$
 */
public class WekaInstancesToSpreadSheet extends AbstractConversion implements SpreadSheetTypeHandler {

    /** for serialization. */
    private static final long serialVersionUID = -7728745365733721265L;

    /** the data row type to use. */
    protected DataRow m_DataRowType;

    /** the type of spreadsheet to use. */
    protected SpreadSheet m_SpreadSheetType;

    /**
     * Returns a string describing the object.
     *
     * @return          a description suitable for displaying in the gui
     */
    @Override
    public String globalInfo() {
        return "Generates a spreadsheet from a weka.core.Instances object.";
    }

    /**
     * Adds options to the internal list of options.
     */
    @Override
    public void defineOptions() {
        super.defineOptions();

        m_OptionManager.add("data-row-type", "dataRowType", new DenseDataRow());

        m_OptionManager.add("spreadsheet-type", "spreadSheetType", new DefaultSpreadSheet());
    }

    /**
     * Sets the type of data row to use.
     *
     * @param value   the type
     */
    public void setDataRowType(DataRow value) {
        m_DataRowType = value;
        reset();
    }

    /**
     * Returns the type of data row to use.
     *
     * @return      the type
     */
    public DataRow getDataRowType() {
        return m_DataRowType;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return       tip text for this property suitable for
     *          displaying in the GUI or for listing the options.
     */
    public String dataRowTypeTipText() {
        return "The type of row to use for the data.";
    }

    /**
     * Sets the type of spreadsheet to use.
     *
     * @param value   the type
     */
    public void setSpreadSheetType(SpreadSheet value) {
        m_SpreadSheetType = value;
        reset();
    }

    /**
     * Returns the type of spreadsheet to use.
     *
     * @return      the type
     */
    public SpreadSheet getSpreadSheetType() {
        return m_SpreadSheetType;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return       tip text for this property suitable for
     *          displaying in the GUI or for listing the options.
     */
    public String spreadSheetTypeTipText() {
        return "The type of spreadsheet to use for the data.";
    }

    /**
     * Returns the class that is accepted as input.
     *
     * @return      the class
     */
    @Override
    public Class accepts() {
        return weka.core.Instances.class;
    }

    /**
     * Returns the class that is generated as output.
     *
     * @return      the class
     */
    @Override
    public Class generates() {
        if (m_SpreadSheetType != null)
            return m_SpreadSheetType.getClass();
        else
            return SpreadSheet.class;
    }

    /**
     * Performs the actual conversion.
     *
     * @return      the converted data
     * @throws Exception   if something goes wrong with the conversion
     */
    @Override
    protected Object doConvert() throws Exception {
        SpreadSheet result;
        Instances data;
        Row row;
        int i;
        int n;
        String str;

        data = (Instances) m_Input;

        // special case for InstancesViews
        if (m_SpreadSheetType instanceof InstancesView) {
            result = new InstancesView((Instances) m_Input);
            return result;
        }

        // create header
        result = m_SpreadSheetType.newInstance();
        result.setDataRowClass(m_DataRowType.getClass());
        row = result.getHeaderRow();
        for (n = 0; n < data.numAttributes(); n++)
            row.addCell("" + n).setContent(data.attribute(n).name());
        if (result instanceof Dataset) {
            if (data.classIndex() != -1)
                ((Dataset) result).setClassAttribute(data.classIndex(), true);
        }

        // fill spreadsheet
        for (i = 0; i < data.numInstances(); i++) {
            row = result.addRow("" + i);

            for (n = 0; n < data.numAttributes(); n++) {
                if (data.instance(i).isMissing(n))
                    continue;
                if (data.attribute(n).type() == Attribute.DATE) {
                    row.addCell("" + n).setContent(new DateTimeMsec(new Date((long) data.instance(i).value(n))));
                } else if (data.attribute(n).type() == Attribute.NUMERIC) {
                    row.addCell("" + n).setContent(data.instance(i).value(n));
                } else {
                    str = data.instance(i).stringValue(n);
                    if (str.equals(SpreadSheet.MISSING_VALUE))
                        row.addCell("" + n).setContentAsString("'" + str + "'");
                    else
                        row.addCell("" + n).setContentAsString(str);
                }
            }
        }

        return result;
    }
}