InstancesResultListener.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 7k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    InstancesResultListener.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.experiment;
  22. import weka.core.Utils;
  23. import java.io.PrintWriter;
  24. import java.io.IOException;
  25. import java.io.File;
  26. import java.io.BufferedOutputStream;
  27. import java.io.FileOutputStream;
  28. import weka.core.OptionHandler;
  29. import java.util.Enumeration;
  30. import java.util.Vector;
  31. import weka.core.Option;
  32. import weka.core.Instances;
  33. import java.util.Hashtable;
  34. import weka.core.FastVector;
  35. import weka.core.Attribute;
  36. import weka.core.Instance;
  37. /**
  38.  * InstancesResultListener outputs the received results in arff format to
  39.  * a Writer. All results must be received before the instances can be
  40.  * written out.
  41.  *
  42.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  43.  * @version $Revision: 1.6 $
  44.  */
  45. public class InstancesResultListener extends CSVResultListener {
  46.   /** Stores the instances created so far, before assigning to a header */
  47.   protected transient FastVector m_Instances;
  48.   
  49.   /** Stores the attribute types for each column */
  50.   protected transient int [] m_AttributeTypes;
  51.   /** For lookup of indices given a string value for each nominal attribute */
  52.   protected transient Hashtable [] m_NominalIndexes;
  53.   /** Contains strings seen so far for each nominal attribute */ 
  54.   protected transient FastVector [] m_NominalStrings;
  55.   
  56.   /**
  57.    * Returns a string describing this result listener
  58.    * @return a description of the result listener suitable for
  59.    * displaying in the explorer/experimenter gui
  60.    */
  61.   public String globalInfo() {
  62.     return "Takes results from a result producer and assembles them into "
  63.       +"a set of instances.";
  64.   }
  65.   /**
  66.    * Prepare for the results to be received.
  67.    *
  68.    * @param rp the ResultProducer that will generate the results
  69.    * @exception Exception if an error occurs during preprocessing.
  70.    */
  71.   public void preProcess(ResultProducer rp) throws Exception {
  72.     m_RP = rp;
  73.     if ((m_OutputFile == null) || (m_OutputFile.getName().equals("-"))) {
  74.       m_Out = new PrintWriter(System.out, true);
  75.     } else {
  76.       m_Out = new PrintWriter(
  77.       new BufferedOutputStream(
  78.       new FileOutputStream(m_OutputFile)), true);
  79.     }
  80.     Object [] keyTypes = m_RP.getKeyTypes();
  81.     Object [] resultTypes = m_RP.getResultTypes();
  82.     m_AttributeTypes = new int [keyTypes.length + resultTypes.length];
  83.     m_NominalIndexes = new Hashtable [m_AttributeTypes.length];
  84.     m_NominalStrings = new FastVector [m_AttributeTypes.length];
  85.     m_Instances = new FastVector();
  86.     for (int i = 0; i < m_AttributeTypes.length; i++) {
  87.       Object attribute = null;
  88.       if (i < keyTypes.length) {
  89. attribute = keyTypes[i];
  90.       } else {
  91. attribute = resultTypes[i - keyTypes.length];
  92.       }
  93.       if (attribute instanceof String) {
  94. m_AttributeTypes[i] = Attribute.NOMINAL;
  95. m_NominalIndexes[i] = new Hashtable();
  96. m_NominalStrings[i] = new FastVector();
  97.       } else if (attribute instanceof Double) {
  98. m_AttributeTypes[i] = Attribute.NUMERIC;
  99.       } else {
  100. throw new Exception("Unknown attribute type in column " + (i + 1));
  101.       }
  102.     }
  103.   }
  104.   
  105.   /**
  106.    * Perform any postprocessing. When this method is called, it indicates
  107.    * that no more results will be sent that need to be grouped together
  108.    * in any way.
  109.    *
  110.    * @param rp the ResultProducer that generated the results
  111.    * @exception Exception if an error occurs
  112.    */
  113.   public void postProcess(ResultProducer rp) throws Exception {
  114.     if (m_RP != rp) {
  115.       throw new Error("Unrecognized ResultProducer sending results!!");
  116.     }
  117.     String [] keyNames = m_RP.getKeyNames();
  118.     String [] resultNames = m_RP.getResultNames();
  119.     FastVector attribInfo = new FastVector();
  120.     for (int i = 0; i < m_AttributeTypes.length; i++) {
  121.       String attribName = "Unknown";
  122.       if (i < keyNames.length) {
  123. attribName = "Key_" + keyNames[i];
  124.       } else {
  125. attribName = resultNames[i - keyNames.length];
  126.       }
  127.       
  128.       switch (m_AttributeTypes[i]) {
  129.       case Attribute.NOMINAL:
  130. if (m_NominalStrings[i].size() > 0) {
  131.   attribInfo.addElement(new Attribute(attribName,
  132.       m_NominalStrings[i]));
  133. } else {
  134.   attribInfo.addElement(new Attribute(attribName, (FastVector)null));
  135. }
  136. break;
  137.       case Attribute.NUMERIC:
  138. attribInfo.addElement(new Attribute(attribName));
  139. break;
  140.       case Attribute.STRING:
  141. attribInfo.addElement(new Attribute(attribName, (FastVector)null));
  142. break;
  143.       default:
  144. throw new Exception("Unknown attribute type");
  145.       }
  146.     }
  147.     Instances result = new Instances("InstanceResultListener", attribInfo, 
  148.      m_Instances.size());
  149.     for (int i = 0; i < m_Instances.size(); i++) {
  150.       result.add((Instance)m_Instances.elementAt(i));
  151.     }
  152.     m_Out.println(new Instances(result, 0));
  153.     for (int i = 0; i < result.numInstances(); i++) {
  154.       m_Out.println(result.instance(i));
  155.     }
  156.     
  157.     if (!(m_OutputFile == null) && !(m_OutputFile.getName().equals("-"))) {
  158.       m_Out.close();
  159.     }
  160.   }
  161.   /**
  162.    * Collects each instance and adjusts the header information.
  163.    *
  164.    * @param rp the ResultProducer that generated the result
  165.    * @param key The key for the results.
  166.    * @param result The actual results.
  167.    * @exception Exception if the result could not be accepted.
  168.    */
  169.   public void acceptResult(ResultProducer rp, Object[] key, Object[] result) 
  170.     throws Exception {
  171.     if (m_RP != rp) {
  172.       throw new Error("Unrecognized ResultProducer sending results!!");
  173.     }
  174.     
  175.     Instance newInst = new Instance(m_AttributeTypes.length);
  176.     for(int i = 0; i < m_AttributeTypes.length; i++) {
  177.       Object val = null;
  178.       if (i < key.length) {
  179. val = key[i];
  180.       } else {
  181. val = result[i - key.length];
  182.       }
  183.       if (val == null) {
  184. newInst.setValue(i, Instance.missingValue());
  185.       } else {
  186. switch (m_AttributeTypes[i]) {
  187. case Attribute.NOMINAL:
  188.   String str = (String) val;
  189.   Double index = (Double)m_NominalIndexes[i].get(str);
  190.   if (index == null) {
  191.     index = new Double(m_NominalStrings[i].size());
  192.     m_NominalIndexes[i].put(str, index);
  193.     m_NominalStrings[i].addElement(str);
  194.   }
  195.   newInst.setValue(i, index.doubleValue());
  196.   break;
  197. case Attribute.NUMERIC:
  198.   double dou = ((Double) val).doubleValue();
  199.   newInst.setValue(i, (double)dou);
  200.   break;
  201. default:
  202.   newInst.setValue(i, Instance.missingValue());
  203. }
  204.       }
  205.     }
  206.     m_Instances.addElement(newInst);
  207.   }
  208. } // InstancesResultListener