| 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.filters.AbstractFilterTest; |
|---|
| 26 | import weka.filters.Filter; |
|---|
| 27 | |
|---|
| 28 | import junit.framework.Test; |
|---|
| 29 | import junit.framework.TestSuite; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * Tests AddClassification. Run from the command line with: <p/> |
|---|
| 33 | * java weka.filters.supervised.attribute.AddClassificationTest |
|---|
| 34 | * |
|---|
| 35 | * @author FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 36 | * @version $Revision: 1.2 $ |
|---|
| 37 | */ |
|---|
| 38 | public class AddClassificationTest |
|---|
| 39 | extends AbstractFilterTest { |
|---|
| 40 | |
|---|
| 41 | public AddClassificationTest(String name) { |
|---|
| 42 | super(name); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** Creates a default AddClassification */ |
|---|
| 46 | public Filter getFilter() { |
|---|
| 47 | return new AddClassification(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Called by JUnit before each test method. This implementation creates |
|---|
| 52 | * the default filter to test and loads a test set of Instances. |
|---|
| 53 | * |
|---|
| 54 | * @throws Exception if an error occurs reading the example instances. |
|---|
| 55 | */ |
|---|
| 56 | protected void setUp() throws Exception { |
|---|
| 57 | super.setUp(); |
|---|
| 58 | |
|---|
| 59 | m_Instances.deleteAttributeType(Attribute.STRING); |
|---|
| 60 | m_Instances.setClassIndex(0); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * sets up the filter and performs the test |
|---|
| 65 | * |
|---|
| 66 | * @param num whether the class is numeric or nominal |
|---|
| 67 | * @param cl whether the classification is to be output |
|---|
| 68 | * @param dist whether the distribution is to be output |
|---|
| 69 | * @param error whether the error flag is to be output |
|---|
| 70 | * @param remove whether to remove the old class attribute |
|---|
| 71 | */ |
|---|
| 72 | protected void performTest(boolean num, boolean cl, boolean dist, boolean error, boolean remove) { |
|---|
| 73 | Instances icopy; |
|---|
| 74 | int numAtts; |
|---|
| 75 | |
|---|
| 76 | // setup dataset |
|---|
| 77 | if (num) |
|---|
| 78 | m_Instances.setClassIndex(1); |
|---|
| 79 | else |
|---|
| 80 | m_Instances.setClassIndex(0); |
|---|
| 81 | icopy = new Instances(m_Instances); |
|---|
| 82 | |
|---|
| 83 | // setup filter |
|---|
| 84 | m_Filter = getFilter(); |
|---|
| 85 | if (num) |
|---|
| 86 | ((AddClassification) m_Filter).setClassifier(new weka.classifiers.trees.M5P()); |
|---|
| 87 | else |
|---|
| 88 | ((AddClassification) m_Filter).setClassifier(new weka.classifiers.trees.J48()); |
|---|
| 89 | |
|---|
| 90 | ((AddClassification) m_Filter).setOutputClassification(cl); |
|---|
| 91 | ((AddClassification) m_Filter).setOutputDistribution(dist); |
|---|
| 92 | ((AddClassification) m_Filter).setOutputErrorFlag(error); |
|---|
| 93 | ((AddClassification) m_Filter).setRemoveOldClass(remove); |
|---|
| 94 | |
|---|
| 95 | numAtts = icopy.numAttributes(); |
|---|
| 96 | if (cl) |
|---|
| 97 | numAtts++; |
|---|
| 98 | if (dist) |
|---|
| 99 | numAtts += icopy.numClasses(); |
|---|
| 100 | if (error) |
|---|
| 101 | numAtts++; |
|---|
| 102 | if (remove) |
|---|
| 103 | numAtts--; |
|---|
| 104 | |
|---|
| 105 | Instances result = useFilter(); |
|---|
| 106 | assertEquals(result.numAttributes(), numAtts); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * performs the application with no options set |
|---|
| 111 | */ |
|---|
| 112 | public void testDefault() { |
|---|
| 113 | Instances icopy = new Instances(m_Instances); |
|---|
| 114 | |
|---|
| 115 | m_Filter = getFilter(); |
|---|
| 116 | Instances result = useFilter(); |
|---|
| 117 | assertEquals(result.numAttributes(), icopy.numAttributes()); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * performs the application with no options set (Nominal class) |
|---|
| 122 | */ |
|---|
| 123 | public void testNoneNominal() { |
|---|
| 124 | performTest(false, false, false, false, false); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * performs the application with only error flag set (Nominal class) |
|---|
| 129 | */ |
|---|
| 130 | public void testErrorFlagNominal() { |
|---|
| 131 | performTest(false, false, false, true, false); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * performs the application with only classification set (Nominal class) |
|---|
| 136 | */ |
|---|
| 137 | public void testClassificationNominal() { |
|---|
| 138 | performTest(false, true, false, false, false); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * performs the application with only distribution set (Nominal class) |
|---|
| 143 | */ |
|---|
| 144 | public void testDistributionNominal() { |
|---|
| 145 | performTest(false, false, true, false, false); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * performs the application with no options set (Nominal class) |
|---|
| 150 | */ |
|---|
| 151 | public void testNoneNumeric() { |
|---|
| 152 | performTest(true, false, false, false, false); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * performs the application with only error flag set (Numeric class) |
|---|
| 157 | */ |
|---|
| 158 | public void testErrorFlagNumeric() { |
|---|
| 159 | performTest(true, false, false, true, false); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * performs the application with only classification set (Numeric class) |
|---|
| 164 | */ |
|---|
| 165 | public void testClassificationNumeric() { |
|---|
| 166 | performTest(true, true, false, false, false); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * performs the application with only distribution set (Numeric class) |
|---|
| 171 | */ |
|---|
| 172 | public void testDistributionNumeric() { |
|---|
| 173 | performTest(true, false, true, false, false); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public static Test suite() { |
|---|
| 177 | return new TestSuite(AddClassificationTest.class); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * performs the application with only classification set (Nominal class) |
|---|
| 182 | * and removal of the old class attribute |
|---|
| 183 | */ |
|---|
| 184 | public void testClassificationRemoveNominal() { |
|---|
| 185 | performTest(false, true, false, false, true); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * performs the application with only classification set (numeric class) |
|---|
| 190 | * and removal of the old class attribute |
|---|
| 191 | */ |
|---|
| 192 | public void testClassificationRemoveNumeric() { |
|---|
| 193 | performTest(true, true, false, false, true); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | public static void main(String[] args){ |
|---|
| 197 | junit.textui.TestRunner.run(suite()); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * performs the application with only removal of the old class attribute |
|---|
| 202 | * (nominal) |
|---|
| 203 | */ |
|---|
| 204 | public void testClassificationOnlyRemoveNominal() { |
|---|
| 205 | performTest(false, false, false, false, true); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * performs the application with only removal of the old class attribute |
|---|
| 210 | * (numeric) |
|---|
| 211 | */ |
|---|
| 212 | public void testClassificationOnlyRemoveNumeric() { |
|---|
| 213 | performTest(true, false, false, false, true); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|