source: branches/MetisMQI/src/main/java/weka/filters/unsupervised/instance/SparseToNonSparse.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 4.3 KB
Line 
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/*
18 *    SparseToNonSparse.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.filters.unsupervised.instance;
25
26import weka.core.Capabilities;
27import weka.core.Instance;
28import weka.core.DenseInstance;
29import weka.core.Instances;
30import weka.core.RevisionUtils;
31import weka.core.SparseInstance;
32import weka.core.Capabilities.Capability;
33import weka.filters.Filter;
34import weka.filters.StreamableFilter;
35import weka.filters.UnsupervisedFilter;
36
37/**
38 <!-- globalinfo-start -->
39 * An instance filter that converts all incoming sparse instances into non-sparse format.
40 * <p/>
41 <!-- globalinfo-end -->
42 *
43 * @author Len Trigg (len@reeltwo.com)
44 * @version $Revision: 5987 $
45 */
46public class SparseToNonSparse 
47  extends Filter
48  implements UnsupervisedFilter, StreamableFilter {
49
50  /** for serialization */
51  static final long serialVersionUID = 2481634184210236074L;
52 
53  /**
54   * Returns a string describing this filter
55   *
56   * @return a description of the filter suitable for
57   * displaying in the explorer/experimenter gui
58   */
59  public String globalInfo() {
60    return "An instance filter that converts all incoming sparse instances"
61      + " into non-sparse format.";
62  }
63
64  /**
65   * Returns the Capabilities of this filter.
66   *
67   * @return            the capabilities of this object
68   * @see               Capabilities
69   */
70  public Capabilities getCapabilities() {
71    Capabilities result = super.getCapabilities();
72    result.disableAll();
73
74    // attributes
75    result.enableAllAttributes();
76    result.enable(Capability.MISSING_VALUES);
77   
78    // class
79    result.enableAllClasses();
80    result.enable(Capability.MISSING_CLASS_VALUES);
81    result.enable(Capability.NO_CLASS);
82   
83    return result;
84  }
85
86  /**
87   * Sets the format of the input instances.
88   *
89   * @param instanceInfo an Instances object containing the input instance
90   * structure (any instances contained in the object are ignored - only the
91   * structure is required).
92   * @return true if the outputFormat may be collected immediately
93   * @throws Exception if format cannot be processed
94   */
95  public boolean setInputFormat(Instances instanceInfo) throws Exception {
96
97    super.setInputFormat(instanceInfo);
98    setOutputFormat(instanceInfo);
99    return true;
100  }
101
102
103  /**
104   * Input an instance for filtering. Ordinarily the instance is processed
105   * and made available for output immediately. Some filters require all
106   * instances be read before producing output.
107   *
108   * @param instance the input instance
109   * @return true if the filtered instance may now be
110   * collected with output().
111   * @throws IllegalStateException if no input structure has been defined
112   */
113  public boolean input(Instance instance) {
114
115    if (getInputFormat() == null) {
116      throw new IllegalStateException("No input instance format defined");
117    }
118    if (m_NewBatch) {
119      resetQueue();
120      m_NewBatch = false;
121    }
122    Instance inst = null;
123    if (instance instanceof SparseInstance) {
124      inst = new DenseInstance(instance.weight(), instance.toDoubleArray());
125      inst.setDataset(instance.dataset());
126    } else {
127      inst = instance;
128    }
129    push(inst);
130    return true;
131  }
132 
133  /**
134   * Returns the revision string.
135   *
136   * @return            the revision
137   */
138  public String getRevision() {
139    return RevisionUtils.extract("$Revision: 5987 $");
140  }
141
142  /**
143   * Main method for testing this class.
144   *
145   * @param argv should contain arguments to the filter: use -h for help
146   */
147  public static void main(String [] argv) {
148    runFilter(new SparseToNonSparse(), argv);
149  }
150}
Note: See TracBrowser for help on using the repository browser.