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) 2009 University of Waikato, Hamilton, New Zealand |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.filters.unsupervised.attribute; |
---|
22 | |
---|
23 | import weka.core.Instances; |
---|
24 | import weka.filters.AbstractFilterTest; |
---|
25 | import weka.filters.Filter; |
---|
26 | |
---|
27 | import junit.framework.Test; |
---|
28 | import junit.framework.TestSuite; |
---|
29 | |
---|
30 | /** |
---|
31 | * Tests RemoveByName. Run from the command line with: <p/> |
---|
32 | * java weka.filters.unsupervised.attribute.RemoveByNameTest |
---|
33 | * |
---|
34 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
35 | * @version $Revision: 5853 $ |
---|
36 | */ |
---|
37 | public class RemoveByNameTest |
---|
38 | extends AbstractFilterTest { |
---|
39 | |
---|
40 | /** |
---|
41 | * Initializes the test. |
---|
42 | * |
---|
43 | * @param name the name of the test |
---|
44 | */ |
---|
45 | public RemoveByNameTest(String name) { |
---|
46 | super(name); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Creates a default RemoveByName. |
---|
51 | * |
---|
52 | * @return the filter |
---|
53 | */ |
---|
54 | public Filter getFilter() { |
---|
55 | return getFilter(RemoveByName.DEFAULT_EXPRESSION, false); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * returns a custom filter. |
---|
60 | * |
---|
61 | * @param expression the expression to use |
---|
62 | * @param invert whether to invert the matching sense |
---|
63 | * @return the configured filter |
---|
64 | */ |
---|
65 | protected Filter getFilter(String expression, boolean invert) { |
---|
66 | RemoveByName filter; |
---|
67 | |
---|
68 | filter = new RemoveByName(); |
---|
69 | filter.setExpression(expression); |
---|
70 | filter.setInvertSelection(invert); |
---|
71 | |
---|
72 | return filter; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Tests removing all attributes starting with "String". |
---|
77 | */ |
---|
78 | public void testTypical() { |
---|
79 | Instances result; |
---|
80 | |
---|
81 | m_Filter = getFilter("^String.*", false); |
---|
82 | |
---|
83 | // 1. with class attribute |
---|
84 | result = useFilter(); |
---|
85 | // Number of attributes will be two less, number of instances won't change |
---|
86 | assertEquals(m_Instances.numAttributes() - 2, result.numAttributes()); |
---|
87 | assertEquals(m_Instances.numInstances(), result.numInstances()); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Tests removing all attributes starting with "Nominal", one of them being |
---|
92 | * the class attribute. |
---|
93 | */ |
---|
94 | public void testTypicalWithClass() { |
---|
95 | Instances result; |
---|
96 | |
---|
97 | m_Instances.setClassIndex(1); // "NominalAtt1" |
---|
98 | m_Filter = getFilter("^Nominal.*", false); |
---|
99 | |
---|
100 | // 1. with class attribute |
---|
101 | result = useFilter(); |
---|
102 | // Number of attributes will be two less, number of instances won't change |
---|
103 | assertEquals(m_Instances.numAttributes() - 1, result.numAttributes()); |
---|
104 | assertEquals(m_Instances.numInstances(), result.numInstances()); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Tests removing all attributes but attributes ending with "Att2". |
---|
109 | */ |
---|
110 | public void testTypicalInverted() { |
---|
111 | Instances result; |
---|
112 | |
---|
113 | m_Filter = getFilter(".*Att2$", true); |
---|
114 | |
---|
115 | // 1. with class attribute |
---|
116 | result = useFilter(); |
---|
117 | // Number of attributes will be two less, number of instances won't change |
---|
118 | assertEquals(3, result.numAttributes()); |
---|
119 | assertEquals(m_Instances.numInstances(), result.numInstances()); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Returns a test suite. |
---|
124 | * |
---|
125 | * @return the test suite |
---|
126 | */ |
---|
127 | public static Test suite() { |
---|
128 | return new TestSuite(RemoveByNameTest.class); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Runs the test from commandline. |
---|
133 | * |
---|
134 | * @param args ignored |
---|
135 | */ |
---|
136 | public static void main(String[] args){ |
---|
137 | junit.textui.TestRunner.run(suite()); |
---|
138 | } |
---|
139 | } |
---|