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.
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.
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.
18
* SparseToNonSparse.java
19
* Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
24
package weka.filters.unsupervised.instance;
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;
36
<!-- globalinfo-start -->
37
* An instance filter that converts all incoming sparse instances into non-sparse format.
39
<!-- globalinfo-end -->
41
* @author Len Trigg (len@reeltwo.com)
42
* @version $Revision: 1.5 $
44
public class SparseToNonSparse
46
implements UnsupervisedFilter, StreamableFilter {
48
/** for serialization */
49
static final long serialVersionUID = 2481634184210236074L;
52
* Returns a string describing this filter
54
* @return a description of the filter suitable for
55
* displaying in the explorer/experimenter gui
57
public String globalInfo() {
58
return "An instance filter that converts all incoming sparse instances"
59
+ " into non-sparse format.";
63
* Returns the Capabilities of this filter.
65
* @return the capabilities of this object
68
public Capabilities getCapabilities() {
69
Capabilities result = super.getCapabilities();
72
result.enableAllAttributes();
73
result.enable(Capability.MISSING_VALUES);
76
result.enableAllClasses();
77
result.enable(Capability.MISSING_CLASS_VALUES);
78
result.enable(Capability.NO_CLASS);
84
* Sets the format of the input instances.
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
92
public boolean setInputFormat(Instances instanceInfo) throws Exception {
94
super.setInputFormat(instanceInfo);
95
setOutputFormat(instanceInfo);
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.
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
110
public boolean input(Instance instance) {
112
if (getInputFormat() == null) {
113
throw new IllegalStateException("No input instance format defined");
119
Instance inst = null;
120
if (instance instanceof SparseInstance) {
121
inst = new Instance(instance.weight(), instance.toDoubleArray());
122
inst.setDataset(instance.dataset());
131
* Main method for testing this class.
133
* @param argv should contain arguments to the filter: use -h for help
135
public static void main(String [] argv) {
136
runFilter(new SparseToNonSparse(), argv);