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 | * GroovyClassifier.java |
---|
19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.scripting; |
---|
24 | |
---|
25 | import weka.classifiers.Classifier; |
---|
26 | import weka.classifiers.AbstractClassifier; |
---|
27 | import weka.core.Capabilities; |
---|
28 | import weka.core.Instance; |
---|
29 | import weka.core.Instances; |
---|
30 | import weka.core.Option; |
---|
31 | import weka.core.RevisionUtils; |
---|
32 | import weka.core.Utils; |
---|
33 | import weka.core.OptionHandler; |
---|
34 | import weka.core.scripting.Groovy; |
---|
35 | |
---|
36 | import java.io.File; |
---|
37 | import java.util.Enumeration; |
---|
38 | import java.util.Vector; |
---|
39 | |
---|
40 | /** |
---|
41 | <!-- globalinfo-start --> |
---|
42 | * A wrapper class for Groovy code. Even though the classifier is serializable, the trained classifier cannot be stored persistently. I.e., one cannot store a model file and re-load it at a later point in time again to make predictions. |
---|
43 | * <p/> |
---|
44 | <!-- globalinfo-end --> |
---|
45 | * |
---|
46 | <!-- options-start --> |
---|
47 | * Valid options are: <p/> |
---|
48 | * |
---|
49 | * <pre> -G <filename> |
---|
50 | * The Groovy module to load (full path) |
---|
51 | * Options after '--' will be passed on to the Groovy module.</pre> |
---|
52 | * |
---|
53 | * <pre> -D |
---|
54 | * If set, classifier is run in debug mode and |
---|
55 | * may output additional info to the console</pre> |
---|
56 | * |
---|
57 | <!-- options-end --> |
---|
58 | * |
---|
59 | * Options after "--" will be passed on to the Groovy module. |
---|
60 | * <p/> |
---|
61 | * In order to use <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>, |
---|
62 | * the jar containing all the classes must be present in the CLASSPATH. |
---|
63 | * This jar is normally found in the <i>embeddable</i> |
---|
64 | * sub-directory of the Groovy installation. |
---|
65 | * <p/> |
---|
66 | * Tested with Groovy 1.5.7. |
---|
67 | * |
---|
68 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
69 | * @version $Revision: 5987 $ |
---|
70 | * @see Groovy |
---|
71 | */ |
---|
72 | public class GroovyClassifier |
---|
73 | extends AbstractClassifier { |
---|
74 | |
---|
75 | /** for serialization. */ |
---|
76 | private static final long serialVersionUID = -9078371491735496175L; |
---|
77 | |
---|
78 | /** the Groovy module. */ |
---|
79 | protected File m_GroovyModule = new File(System.getProperty("user.dir")); |
---|
80 | |
---|
81 | /** the options for the Groovy module. */ |
---|
82 | protected String[] m_GroovyOptions = new String[0]; |
---|
83 | |
---|
84 | /** the loaded Groovy object. */ |
---|
85 | transient protected Classifier m_GroovyObject = null; |
---|
86 | |
---|
87 | /** |
---|
88 | * default constructor. |
---|
89 | */ |
---|
90 | public GroovyClassifier() { |
---|
91 | super(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Returns a string describing classifier. |
---|
96 | * |
---|
97 | * @return a description suitable for |
---|
98 | * displaying in the explorer/experimenter gui |
---|
99 | */ |
---|
100 | public String globalInfo() { |
---|
101 | return |
---|
102 | "A wrapper class for Groovy code. Even though the classifier is " |
---|
103 | + "serializable, the trained classifier cannot be stored persistently. " |
---|
104 | + "I.e., one cannot store a model file and re-load it at a later point " |
---|
105 | + "in time again to make predictions."; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Returns an enumeration describing the available options. |
---|
110 | * |
---|
111 | * @return an enumeration of all the available options. |
---|
112 | */ |
---|
113 | public Enumeration listOptions() { |
---|
114 | Vector result = new Vector(); |
---|
115 | |
---|
116 | result.addElement(new Option( |
---|
117 | "\tThe Groovy module to load (full path)\n" |
---|
118 | + "\tOptions after '--' will be passed on to the Groovy module.", |
---|
119 | "G", 1, "-G <filename>")); |
---|
120 | |
---|
121 | Enumeration en = super.listOptions(); |
---|
122 | while (en.hasMoreElements()) |
---|
123 | result.addElement(en.nextElement()); |
---|
124 | |
---|
125 | return result.elements(); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Parses a given list of options. |
---|
130 | * |
---|
131 | * @param options the list of options as an array of strings |
---|
132 | * @throws Exception if an option is not supported |
---|
133 | */ |
---|
134 | public void setOptions(String[] options) throws Exception { |
---|
135 | String tmpStr; |
---|
136 | |
---|
137 | m_GroovyOptions = new String[0]; |
---|
138 | |
---|
139 | tmpStr = Utils.getOption('G', options); |
---|
140 | if (tmpStr.length() != 0) |
---|
141 | setGroovyModule(new File(tmpStr)); |
---|
142 | else |
---|
143 | setGroovyModule(new File(System.getProperty("user.dir"))); |
---|
144 | |
---|
145 | setGroovyOptions(Utils.joinOptions(Utils.partitionOptions(options).clone())); |
---|
146 | |
---|
147 | super.setOptions(options); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Gets the current settings of the Classifier. |
---|
152 | * |
---|
153 | * @return an array of strings suitable for passing to |
---|
154 | * setOptions |
---|
155 | */ |
---|
156 | public String[] getOptions() { |
---|
157 | Vector<String> result; |
---|
158 | String[] options; |
---|
159 | int i; |
---|
160 | |
---|
161 | result = new Vector<String>(); |
---|
162 | |
---|
163 | result.add("-G"); |
---|
164 | result.add("" + getGroovyModule().getAbsolutePath()); |
---|
165 | |
---|
166 | options = super.getOptions(); |
---|
167 | for (i = 0; i < options.length; i++) |
---|
168 | result.add(options[i]); |
---|
169 | |
---|
170 | if (m_GroovyOptions.length > 0) { |
---|
171 | options = m_GroovyOptions; |
---|
172 | result.add("--"); |
---|
173 | for (i = 0; i < options.length; i++) |
---|
174 | result.add(options[i]); |
---|
175 | } |
---|
176 | |
---|
177 | return result.toArray(new String[result.size()]); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Returns the tip text for this property. |
---|
182 | * |
---|
183 | * @return tip text for this property suitable for |
---|
184 | * displaying in the explorer/experimenter gui |
---|
185 | */ |
---|
186 | public String GroovyModuleTipText() { |
---|
187 | return "The Groovy module to load and execute."; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Sets the Groovy module. |
---|
192 | * |
---|
193 | * @param value the Groovy module |
---|
194 | */ |
---|
195 | public void setGroovyModule(File value) { |
---|
196 | m_GroovyModule = value; |
---|
197 | initGroovyObject(); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Gets the Groovy module. |
---|
202 | * |
---|
203 | * @return the Groovy module |
---|
204 | */ |
---|
205 | public File getGroovyModule() { |
---|
206 | return m_GroovyModule; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Returns the tip text for this property. |
---|
211 | * |
---|
212 | * @return tip text for this property suitable for |
---|
213 | * displaying in the explorer/experimenter gui |
---|
214 | */ |
---|
215 | public String GroovyOptionsTipText() { |
---|
216 | return "The options for the Groovy module."; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Sets the Groovy module options. |
---|
221 | * |
---|
222 | * @param value the options |
---|
223 | */ |
---|
224 | public void setGroovyOptions(String value) { |
---|
225 | try { |
---|
226 | m_GroovyOptions = Utils.splitOptions(value).clone(); |
---|
227 | initGroovyObject(); |
---|
228 | } |
---|
229 | catch (Exception e) { |
---|
230 | m_GroovyOptions = new String[0]; |
---|
231 | e.printStackTrace(); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * Gets the Groovy module options. |
---|
237 | * |
---|
238 | * @return the options |
---|
239 | */ |
---|
240 | public String getGroovyOptions() { |
---|
241 | return Utils.joinOptions(m_GroovyOptions); |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * Returns default capabilities of the classifier. |
---|
246 | * |
---|
247 | * @return the capabilities of this classifier |
---|
248 | */ |
---|
249 | public Capabilities getCapabilities() { |
---|
250 | Capabilities result; |
---|
251 | |
---|
252 | if (m_GroovyObject == null) { |
---|
253 | result = new Capabilities(this); |
---|
254 | result.disableAll(); |
---|
255 | } else { |
---|
256 | result = m_GroovyObject.getCapabilities(); |
---|
257 | } |
---|
258 | |
---|
259 | result.enableAllAttributeDependencies(); |
---|
260 | result.enableAllClassDependencies(); |
---|
261 | |
---|
262 | return result; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * tries to initialize the groovy object and set its options. |
---|
267 | */ |
---|
268 | protected void initGroovyObject() { |
---|
269 | try { |
---|
270 | if (m_GroovyModule.isFile()) |
---|
271 | m_GroovyObject = (Classifier) Groovy.newInstance(m_GroovyModule, Classifier.class); |
---|
272 | else |
---|
273 | m_GroovyObject = null; |
---|
274 | |
---|
275 | if (m_GroovyObject != null) |
---|
276 | ((OptionHandler)m_GroovyObject).setOptions(m_GroovyOptions.clone()); |
---|
277 | } |
---|
278 | catch (Exception e) { |
---|
279 | m_GroovyObject = null; |
---|
280 | e.printStackTrace(); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Generates the classifier. |
---|
286 | * |
---|
287 | * @param instances set of instances serving as training data |
---|
288 | * @throws Exception if the classifier has not been generated successfully |
---|
289 | */ |
---|
290 | public void buildClassifier(Instances instances) throws Exception { |
---|
291 | if (!Groovy.isPresent()) |
---|
292 | throw new Exception("Groovy classes not in CLASSPATH!"); |
---|
293 | |
---|
294 | // try loading the module |
---|
295 | initGroovyObject(); |
---|
296 | |
---|
297 | // build the model |
---|
298 | if (m_GroovyObject != null) |
---|
299 | m_GroovyObject.buildClassifier(instances); |
---|
300 | else |
---|
301 | System.err.println("buildClassifier: No Groovy object present!"); |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Classifies a given instance. |
---|
306 | * |
---|
307 | * @param instance the instance to be classified |
---|
308 | * @return index of the predicted class |
---|
309 | * @throws Exception if an error occurred during the prediction |
---|
310 | */ |
---|
311 | public double classifyInstance(Instance instance) throws Exception { |
---|
312 | if (m_GroovyObject != null) |
---|
313 | return m_GroovyObject.classifyInstance(instance); |
---|
314 | else |
---|
315 | return Utils.missingValue(); |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Calculates the class membership probabilities for the given test instance. |
---|
320 | * |
---|
321 | * @param instance the instance to be classified |
---|
322 | * @return predicted class probability distribution |
---|
323 | * @throws Exception if class is numeric |
---|
324 | */ |
---|
325 | public double[] distributionForInstance(Instance instance) throws Exception { |
---|
326 | if (m_GroovyObject != null) |
---|
327 | return m_GroovyObject.distributionForInstance(instance); |
---|
328 | else |
---|
329 | return new double[instance.numClasses()]; |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * Returns a description of the classifier. |
---|
334 | * |
---|
335 | * @return a description of the classifier as a string. |
---|
336 | */ |
---|
337 | public String toString() { |
---|
338 | if (m_GroovyObject != null) |
---|
339 | return m_GroovyObject.toString(); |
---|
340 | else |
---|
341 | return "No Groovy module loaded."; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Returns the revision string. |
---|
346 | * |
---|
347 | * @return the revision |
---|
348 | */ |
---|
349 | public String getRevision() { |
---|
350 | return RevisionUtils.extract("$Revision: 5987 $"); |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Main method for testing this class. |
---|
355 | * |
---|
356 | * @param args the options |
---|
357 | */ |
---|
358 | public static void main(String [] args) { |
---|
359 | runClassifier(new GroovyClassifier(), args); |
---|
360 | } |
---|
361 | } |
---|