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 | * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.filters.supervised.attribute; |
---|
22 | |
---|
23 | import weka.core.Attribute; |
---|
24 | import weka.core.Instances; |
---|
25 | import weka.core.SelectedTag; |
---|
26 | import weka.core.TestInstances; |
---|
27 | import weka.filters.AbstractFilterTest; |
---|
28 | import weka.filters.Filter; |
---|
29 | |
---|
30 | import junit.framework.Test; |
---|
31 | import junit.framework.TestSuite; |
---|
32 | |
---|
33 | /** |
---|
34 | * Tests PLSFilter. Run from the command line with: <p/> |
---|
35 | * java weka.filters.supervised.attribute.PLSFilterTest |
---|
36 | * |
---|
37 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
38 | * @version $Revision: 1.3 $ |
---|
39 | */ |
---|
40 | public class PLSFilterTest |
---|
41 | extends AbstractFilterTest { |
---|
42 | |
---|
43 | /** the default number of attributes to generate (apart from class) */ |
---|
44 | protected final static int NUM_ATTS = 5; |
---|
45 | |
---|
46 | /** the number of numeric attributes in the test dataset */ |
---|
47 | protected final static int NUM_NUMERIC_ATTS = 20; |
---|
48 | |
---|
49 | public PLSFilterTest(String name) { |
---|
50 | super(name); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Creates a default PLSFilter |
---|
55 | * |
---|
56 | * @return the configured filter |
---|
57 | */ |
---|
58 | public Filter getFilter() { |
---|
59 | return getFilter(NUM_ATTS, PLSFilter.ALGORITHM_PLS1); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Creates a PLSFilter according to the parameters |
---|
64 | * |
---|
65 | * @param numAtts the number of attributes to generate |
---|
66 | * @param algorithm the algorithm to use |
---|
67 | * @return the configured filter |
---|
68 | */ |
---|
69 | public Filter getFilter(int numAtts, int algorithm) { |
---|
70 | PLSFilter filter = new PLSFilter(); |
---|
71 | |
---|
72 | filter.setNumComponents(numAtts); |
---|
73 | filter.setReplaceMissing(true); |
---|
74 | filter.setPreprocessing(new SelectedTag(PLSFilter.PREPROCESSING_CENTER, PLSFilter.TAGS_PREPROCESSING)); |
---|
75 | filter.setAlgorithm(new SelectedTag(algorithm, PLSFilter.TAGS_ALGORITHM)); |
---|
76 | |
---|
77 | return filter; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * returns data generated for the FilteredClassifier test |
---|
82 | * |
---|
83 | * @return the dataset for the FilteredClassifier |
---|
84 | * @throws Exception if generation of data fails |
---|
85 | */ |
---|
86 | protected Instances getFilteredClassifierData() throws Exception{ |
---|
87 | TestInstances test; |
---|
88 | Instances result; |
---|
89 | |
---|
90 | test = new TestInstances(); |
---|
91 | test.setNumNominal(0); |
---|
92 | test.setNumNumeric(NUM_NUMERIC_ATTS); |
---|
93 | test.setClassType(Attribute.NUMERIC); |
---|
94 | |
---|
95 | result = test.generate(); |
---|
96 | |
---|
97 | return result; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Called by JUnit before each test method. This implementation creates |
---|
102 | * the default filter to test and generates a test set of Instances. |
---|
103 | * |
---|
104 | * @throws Exception if an error occurs reading the example instances. |
---|
105 | */ |
---|
106 | protected void setUp() throws Exception { |
---|
107 | super.setUp(); |
---|
108 | |
---|
109 | TestInstances test = new TestInstances(); |
---|
110 | test.setNumNominal(0); |
---|
111 | test.setNumNumeric(NUM_NUMERIC_ATTS); |
---|
112 | test.setClassType(Attribute.NUMERIC); |
---|
113 | m_Instances = test.generate(); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * performs a test |
---|
118 | * |
---|
119 | * @param algorithm the algorithm to use |
---|
120 | */ |
---|
121 | protected void performTest(int algorithm) { |
---|
122 | Instances icopy = new Instances(m_Instances); |
---|
123 | |
---|
124 | m_Filter = getFilter(NUM_ATTS, algorithm); |
---|
125 | Instances result = useFilter(); |
---|
126 | assertEquals(result.numAttributes(), NUM_ATTS + 1); |
---|
127 | assertEquals(result.numInstances(), icopy.numInstances()); |
---|
128 | |
---|
129 | m_Filter = getFilter(NUM_ATTS*2, algorithm); |
---|
130 | result = useFilter(); |
---|
131 | assertEquals(result.numAttributes(), NUM_ATTS*2 + 1); |
---|
132 | assertEquals(result.numInstances(), icopy.numInstances()); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * performs a test on PLS1 |
---|
137 | */ |
---|
138 | public void testPLS1() { |
---|
139 | performTest(PLSFilter.ALGORITHM_PLS1); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * performs a test on SIMPLS |
---|
144 | */ |
---|
145 | public void testSIMPLS() { |
---|
146 | performTest(PLSFilter.ALGORITHM_SIMPLS); |
---|
147 | } |
---|
148 | |
---|
149 | public static Test suite() { |
---|
150 | return new TestSuite(PLSFilterTest.class); |
---|
151 | } |
---|
152 | |
---|
153 | public static void main(String[] args){ |
---|
154 | junit.textui.TestRunner.run(suite()); |
---|
155 | } |
---|
156 | } |
---|