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/>. */ /* * WekaInstancesStatistic.java * Copyright (C) 2010-2013 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.core.Index; import adams.core.QuickInfoHelper; import adams.core.base.BaseString; import adams.data.spreadsheet.SpreadSheet; import adams.data.statistics.AbstractArrayStatistic; import adams.data.statistics.ArrayMean; import adams.data.statistics.StatUtils; import adams.data.weka.WekaAttributeIndex; import adams.flow.core.Token; import weka.core.Instances; /** <!-- globalinfo-start --> * Generates statistics from a weka.core.Instances object.<br> * The actor just uses the internal format (double array) and does not check whether the attributes are actually numeric. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * weka.core.Instances<br> * - generates:<br> * adams.data.spreadsheet.SpreadSheet<br> * <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: WekaInstancesStatistic * </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 execution at this level gets stopped in case this * actor encounters an error; the error gets propagated; useful for critical * actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console; Note: the enclosing * actor handler must have this enabled as well. * default: false * </pre> * * <pre>-type <ROW_BY_INDEX|COLUMN_BY_INDEX|COLUMN_BY_REGEXP> (property: dataType) * Whether to retrieve rows or columns from the Instances object. * default: COLUMN_BY_INDEX * </pre> * * <pre>-location <adams.core.base.BaseString> [-location ...] (property: locations) * The locations of the data, depending on the chosen data type that can be * either indices, attribute names or regular expressions on the attribute * names. * default: * </pre> * * <pre>-statistic <adams.data.statistics.AbstractArrayStatistic> (property: statistic) * The statistic to generate from the data. * default: adams.data.statistics.ArrayMean * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class WekaInstancesStatistic extends AbstractTransformer { /** for serialization. */ private static final long serialVersionUID = -8493694755948450901L; /** the type of data to get from the Instances object (rows or columns). */ protected WekaInstancesStatisticDataType m_DataType; /** the array of indices/regular expressions. */ protected BaseString[] m_Locations; /** the statistic to generate. */ protected AbstractArrayStatistic m_Statistic; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Generates statistics from a weka.core.Instances object.\n" + "The actor just uses the internal format (double array) and does " + "not check whether the attributes are actually numeric."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("type", "dataType", WekaInstancesStatisticDataType.COLUMN_BY_INDEX); m_OptionManager.add("location", "locations", new BaseString[0]); m_OptionManager.add("statistic", "statistic", new ArrayMean()); } /** * Sets what type of data to retrieve from the Instances object. * * @param value the type of conversion */ public void setDataType(WekaInstancesStatisticDataType value) { m_DataType = value; reset(); } /** * Returns what type of data to retrieve from the Instances object. * * @return the type of conversion */ public WekaInstancesStatisticDataType getDataType() { return m_DataType; } /** * 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 dataTypeTipText() { return "Whether to retrieve rows or columns from the Instances object."; } /** * Sets the locations of the data (indices/regular expressions on attribute name). * * @param value the locations of the data */ public void setLocations(BaseString[] value) { m_Locations = value; reset(); } /** * Returns the locations of the data (indices/regular expressions on attribute name). * * @return the locations of the data */ public BaseString[] getLocations() { return m_Locations; } /** * 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 locationsTipText() { return "The locations of the data, depending on the chosen data type that " + "can be either indices, attribute names or regular expressions on the attribute names."; } /** * Sets the statistic to use. * * @param value the statistic */ public void setStatistic(AbstractArrayStatistic value) { m_Statistic = value; reset(); } /** * Returns the statistic in use. * * @return the statistic */ public AbstractArrayStatistic getStatistic() { return m_Statistic; } /** * 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 statisticTipText() { return "The statistic to generate from the data."; } /** * Returns a quick info about the actor, which will be displayed in the GUI. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { return QuickInfoHelper.toString(this, "statistic", m_Statistic); } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->weka.core.Instances.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { Instances.class }; } /** * Returns the class of objects that it generates. * * @return <!-- flow-generates-start -->adams.data.spreadsheet.SpreadSheet.class<!-- flow-generates-end --> */ public Class[] generates() { return new Class[] { SpreadSheet.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; SpreadSheet sheet; Instances data; int i; int n; Index index; AbstractArrayStatistic stat; result = null; try { sheet = null; data = (Instances) m_InputToken.getPayload(); stat = m_Statistic.shallowCopy(true); for (i = 0; i < m_Locations.length; i++) { switch (m_DataType) { case ROW_BY_INDEX: index = new Index(m_Locations[i].stringValue()); index.setMax(data.numInstances()); stat.add(StatUtils.toNumberArray(data.instance(index.getIntIndex()).toDoubleArray())); break; case COLUMN_BY_INDEX: index = new WekaAttributeIndex(m_Locations[i].stringValue()); ((WekaAttributeIndex) index).setData(data); stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(index.getIntIndex()))); break; case COLUMN_BY_REGEXP: for (n = 0; n < data.numAttributes(); n++) { if (data.attribute(n).name().matches(m_Locations[i].stringValue())) { stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(n))); break; } } break; default: throw new IllegalStateException("Unhandled data type: " + m_DataType); } } sheet = stat.calculate().toSpreadSheet(); } catch (Exception e) { result = handleException("Error generating the statistic: ", e); sheet = null; } if (sheet != null) m_OutputToken = new Token(sheet); return result; } }