~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/filters/unsupervised/instance/SparseToNonSparse.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

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