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
* SerializedClassifier.java
19
* Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
22
package weka.classifiers.misc;
24
import weka.classifiers.Classifier;
25
import weka.core.Capabilities;
26
import weka.core.Instance;
27
import weka.core.Instances;
28
import weka.core.Option;
29
import weka.core.SerializationHelper;
30
import weka.core.Utils;
31
import weka.core.Capabilities.Capability;
34
import java.util.Enumeration;
35
import java.util.Vector;
38
<!-- globalinfo-start -->
39
* A wrapper around a serialized classifier model. This classifier loads a serialized models and uses it to make predictions.<br/>
41
* Warning: since the serialized model doesn't get changed, cross-validation cannot bet used with this classifier.
43
<!-- globalinfo-end -->
45
<!-- options-start -->
46
* Valid options are: <p/>
49
* If set, classifier is run in debug mode and
50
* may output additional info to the console</pre>
52
* <pre> -model <filename>
53
* The file containing the serialized model.
58
* @author fracpete (fracpete at waikato dot ac dot nz)
59
* @version $Revision: 1.3 $
61
public class SerializedClassifier
64
/** for serialization */
65
private static final long serialVersionUID = 4599593909947628642L;
67
/** the serialized classifier model used for making predictions */
68
protected transient Classifier m_Model = null;
70
/** the file where the serialized model is stored */
71
protected File m_ModelFile = new File(System.getProperty("user.dir"));
74
* Returns a string describing classifier
76
* @return a description suitable for displaying in the
77
* explorer/experimenter gui
79
public String globalInfo() {
81
"A wrapper around a serialized classifier model. This classifier loads "
82
+ "a serialized models and uses it to make predictions.\n\n"
83
+ "Warning: since the serialized model doesn't get changed, cross-validation "
84
+ "cannot bet used with this classifier.";
88
* Gets an enumeration describing the available options.
90
* @return an enumeration of all the available options.
92
public Enumeration listOptions(){
96
result = new Vector();
98
enm = super.listOptions();
99
while (enm.hasMoreElements())
100
result.addElement(enm.nextElement());
102
result.addElement(new Option(
103
"\tThe file containing the serialized model.\n"
105
"model", 1, "-model <filename>"));
107
return result.elements();
111
* returns the options of the current setup
113
* @return the current options
115
public String[] getOptions(){
120
result = new Vector();
122
options = super.getOptions();
123
for (i = 0; i < options.length; i++)
124
result.add(options[i]);
126
result.add("-model");
127
result.add("" + getModelFile());
129
return (String[]) result.toArray(new String[result.size()]);
133
* Parses the options for this object. <p/>
135
<!-- options-start -->
136
* Valid options are: <p/>
139
* If set, classifier is run in debug mode and
140
* may output additional info to the console</pre>
142
* <pre> -model <filename>
143
* The file containing the serialized model.
148
* @param options the options to use
149
* @throws Exception if setting of options fails
151
public void setOptions(String[] options) throws Exception {
154
super.setOptions(options);
156
tmpStr = Utils.getOption("model", options);
157
if (tmpStr.length() != 0)
158
setModelFile(new File(tmpStr));
160
setModelFile(new File(System.getProperty("user.dir")));
164
* Returns the tip text for this property
166
* @return tip text for this property suitable for
167
* displaying in the explorer/experimenter gui
169
public String modelFileTipText() {
170
return "The serialized classifier model to use for predictions.";
174
* Gets the file containing the serialized model.
178
public File getModelFile() {
183
* Sets the file containing the serialized model.
185
* @param value the file.
187
public void setModelFile(File value) {
190
if (value.exists() && value.isFile()) {
194
catch (Exception e) {
195
throw new IllegalArgumentException("Cannot load model from file '" + value + "': " + e);
201
* Sets the fully built model to use, if one doesn't want to load a model
202
* from a file or already deserialized a model from somewhere else.
204
* @param value the built model
205
* @see #getCurrentModel()
207
public void setModel(Classifier value) {
212
* Gets the currently loaded model (can be null). Call buildClassifier method
213
* to load model from file.
215
* @return the current model
216
* @see #setModel(Classifier)
218
public Classifier getCurrentModel() {
223
* loads the serialized model if necessary, throws an Exception if the
224
* derserialization fails. Always propagates the current debug flag.
226
* @throws Exception if deserialization fails
228
protected void initModel() throws Exception {
230
m_Model = (Classifier) SerializationHelper.read(m_ModelFile.getAbsolutePath());
232
m_Model.setDebug(getDebug());
236
* Returns default capabilities of the base classifier.
238
* @return the capabilities of the base classifier
240
public Capabilities getCapabilities() {
243
// init model if necessary
247
catch (Exception e) {
248
System.err.println(e);
252
result = m_Model.getCapabilities();
254
result = new Capabilities(this);
257
for (Capability cap: Capability.values())
258
result.enableDependency(cap);
260
result.setOwner(this);
266
* Calculates the class membership probabilities for the given test
269
* @param instance the instance to be classified
270
* @return preedicted class probability distribution
271
* @throws Exception if distribution can't be computed successfully
273
public double[] distributionForInstance(Instance instance) throws Exception {
276
// init model if necessary
279
result = m_Model.distributionForInstance(instance);
285
* loads only the serialized classifier
287
* @param data the training instances
288
* @throws Exception if something goes wrong
290
public void buildClassifier(Instances data) throws Exception {
291
// init model if necessary
294
// can classifier handle the data?
295
getCapabilities().testWithFail(data);
299
* Returns a string representation of the classifier
301
* @return the string representation of the classifier
303
public String toString() {
306
if (m_Model == null) {
307
result = new StringBuffer("No model loaded yet.");
310
result = new StringBuffer();
311
result.append("SerializedClassifier\n");
312
result.append("====================\n\n");
313
result.append("File: " + getModelFile() + "\n\n");
314
result.append(m_Model.toString());
317
return result.toString();
321
* Runs the classifier with the given options
323
* @param args the commandline options
325
public static void main(String[] args) {
326
runClassifier(new SerializedClassifier(), args);