source: src/main/java/weka/classifiers/EnsembleLibrary.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 11.2 KB
Line 
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 *    EnsembleLibrary.java
19 *    Copyright (C) 2006 Robert Jung
20 *
21 */
22
23package weka.classifiers;
24
25import weka.core.RevisionHandler;
26import weka.core.RevisionUtils;
27import weka.gui.ensembleLibraryEditor.LibrarySerialization;
28
29import java.beans.PropertyChangeListener;
30import java.beans.PropertyChangeSupport;
31import java.io.BufferedReader;
32import java.io.BufferedWriter;
33import java.io.File;
34import java.io.FileReader;
35import java.io.FileWriter;
36import java.io.InputStream;
37import java.io.Serializable;
38import java.io.Writer;
39import java.util.Iterator;
40import java.util.TreeSet;
41import java.util.Vector;
42
43import javax.swing.JComponent;
44import javax.swing.JOptionPane;
45
46/**
47 * This class represents a library of classifiers.  This class
48 * follows the factory design pattern of creating LibraryModels
49 * when asked.  It also has the methods necessary for saving
50 * and loading models from lists.
51 *
52 * @author  Robert Jung (mrbobjung@gmail.com)
53 * @version $Revision: 6041 $
54 */
55public class EnsembleLibrary
56  implements Serializable, RevisionHandler {
57
58  /** for serialization */
59  private static final long serialVersionUID = -7987178904923706760L;
60
61  /** The default file extension for model list files */
62  public static final String XML_FILE_EXTENSION = ".model.xml";
63
64  /** The flat file extension for model list files */
65  public static final String FLAT_FILE_EXTENSION = ".mlf";
66
67  /** the set of classifiers that constitute the library */
68  public TreeSet m_Models;
69
70  /** A helper class for notifying listeners when the library changes */
71  private transient PropertyChangeSupport m_LibraryPropertySupport = new PropertyChangeSupport(this);
72
73  /**
74   * Constructor is responsible for initializing the data
75   * structure hoilding all of the models
76   *
77   */
78  public EnsembleLibrary() {
79
80    m_Models = new TreeSet(new EnsembleLibraryModelComparator());
81  }
82
83  /**
84   * Returns the number of models in the ensemble library
85   *
86   * @return    the number of models
87   */
88  public int size() {
89    if (m_Models != null)
90      return m_Models.size();
91    else
92      return 0;
93  }
94
95  /**
96   * adds a LibraryModel to the Library
97   *
98   * @param model       the model to add
99   */
100  public void addModel(EnsembleLibraryModel model) {
101    m_Models.add(model);
102    if (m_LibraryPropertySupport != null)
103      m_LibraryPropertySupport.firePropertyChange(null, null, null);
104  }
105
106  /**
107   * adds a LibraryModel to the Library
108   *
109   * @param modelString the model to add
110   */
111  public void addModel(String modelString) {
112    m_Models.add(createModel(modelString));
113    m_LibraryPropertySupport.firePropertyChange(null, null, null);
114  }
115
116  /**
117   * removes a LibraryModel from the Library
118   *
119   * @param model       the model to remove
120   */
121  public void removeModel(EnsembleLibraryModel model) {
122    m_Models.remove(model);
123    m_LibraryPropertySupport.firePropertyChange(null, null, null);
124  }
125
126  /**
127   * creates a LibraryModel from a string representing the command
128   * line invocation
129   *
130   * @param classifier  the classifier to create a model from
131   * @return            the generated model
132   */
133  public EnsembleLibraryModel createModel(Classifier classifier) {
134    EnsembleLibraryModel model = new EnsembleLibraryModel(classifier);
135
136    return model;
137  }
138
139  /**
140   * This method takes a String argument defining a classifier and
141   * uses it to create a base Classifier.
142   *
143   * @param modelString the classifier string
144   * @return            the generated model
145   */
146  public EnsembleLibraryModel createModel(String modelString) {
147
148    String[] splitString = modelString.split("\\s+");
149    String className = splitString[0];
150
151    String argString = modelString.replaceAll(splitString[0], "");
152    String[] optionStrings = argString.split("\\s+");
153
154    EnsembleLibraryModel model = null;
155    try {
156      model = new EnsembleLibraryModel(AbstractClassifier.forName(className,
157            optionStrings));
158    } catch (Exception e) {
159      e.printStackTrace();
160    }
161    return model;
162  }
163
164  /**
165   * getter for the set of models in this library
166   *
167   * @return            the current models
168   */
169  public TreeSet getModels() {
170    return m_Models;
171  }
172
173  /**
174   * setter for the set of models in this library
175   *
176   * @param models      the models to use
177   */
178  public void setModels(TreeSet models) {
179    m_Models = models;
180    m_LibraryPropertySupport.firePropertyChange(null, null, null);
181  }
182
183  /**
184   * removes all models from the current library
185   */
186  public void clearModels() {
187    m_Models.clear();
188    m_LibraryPropertySupport.firePropertyChange(null, null, null);
189  }
190
191  /**
192   * Loads and returns a library from the specified file
193   *
194   * @param selectedFile        the file to load from
195   * @param dialogParent        the parent component
196   * @param library             will contain the data after loading
197   */
198  public static void loadLibrary(File selectedFile, JComponent dialogParent,
199      EnsembleLibrary library) {
200
201    try {
202      loadLibrary(selectedFile, library);
203    } catch (Exception ex) {
204      JOptionPane.showMessageDialog(dialogParent, "Error reading file '"
205          + selectedFile.getName() + "':\n" + ex.getMessage(),
206          "Load failed", JOptionPane.ERROR_MESSAGE);
207      System.err.println(ex.getMessage());
208    }
209  }
210
211  /**
212   * This method takes a model list file and a library object as arguments and
213   * Instantiates all of the models in the library list file.  It is assumed
214   * that the passed library was an associated working directory and can take
215   * care of creating the model objects itself.
216   *
217   * @param selectedFile        the file to load
218   * @param library             the library
219   * @throws Exception          if something goes wrong
220   */
221  public static void loadLibrary(File selectedFile, EnsembleLibrary library)
222    throws Exception {
223
224    //decide what type of model file list we are dealing with and
225    //then load accordingly
226
227    //deal with XML extension for xml files
228    if (selectedFile.getName().toLowerCase().endsWith(
229          EnsembleLibrary.XML_FILE_EXTENSION)) {
230
231      LibrarySerialization librarySerialization;
232
233      Vector classifiers = null;
234
235      try {
236        librarySerialization = new LibrarySerialization();
237        classifiers = (Vector) librarySerialization.read(selectedFile.getPath());
238      } catch (Exception e) {
239        e.printStackTrace();
240      }
241
242      //library.setClassifiers(classifiers);
243
244      for (Iterator it = classifiers.iterator(); it.hasNext();) {
245
246        EnsembleLibraryModel model = library.createModel((Classifier) it.next());
247        model.testOptions();
248        library.addModel(model);
249      }
250
251      //deal with MLF extesion for flat files
252    } else if (selectedFile.getName().toLowerCase().endsWith(
253          EnsembleLibrary.FLAT_FILE_EXTENSION)) {
254
255      BufferedReader reader = null;
256
257      reader = new BufferedReader(new FileReader(selectedFile));
258
259      String modelString;
260
261      while ((modelString = reader.readLine()) != null) {
262
263        EnsembleLibraryModel model = library.createModel(modelString);
264
265        if (model != null) {
266          model.testOptions();
267          library.addModel(model);
268        } else {
269          System.err.println("Failed to create model: " + modelString);
270        }
271      }
272
273      reader.close();
274    }
275  }
276
277  /**
278   * This method takes an XML input stream and a library object as arguments
279   * and Instantiates all of the models in the stream.  It is assumed
280   * that the passed library was an associated working directory and can take
281   * care of creating the model objects itself.
282   *
283   * @param stream              the XML stream to load
284   * @param library             the library
285   * @throws Exception          if something goes wrong
286   */
287  public static void loadLibrary(InputStream stream, EnsembleLibrary library)
288    throws Exception {
289
290    Vector classifiers = null;
291
292    try {
293      LibrarySerialization librarySerialization = new LibrarySerialization();
294      classifiers = (Vector) librarySerialization.read(stream);
295    }
296    catch (Exception e) {
297      e.printStackTrace();
298    }
299
300    for (int i = 0; i < classifiers.size(); i++) {
301      EnsembleLibraryModel model = library.createModel((Classifier) classifiers.get(i));
302      model.testOptions();
303      library.addModel(model);
304    }
305  }
306
307
308  /**
309   * Saves the given library in the specified file.  This saves only
310   * the specification of the models as a model list.
311   *
312   * @param selectedFile        the file to save to
313   * @param library             the library to save
314   * @param dialogParent        the component parent
315   */
316  public static void saveLibrary(File selectedFile, EnsembleLibrary library,
317      JComponent dialogParent) {
318
319    //save decide what type of model file list we are dealing with and
320    //then save accordingly
321
322    //System.out.println("writing to file: "+selectedFile.getPath());
323
324    //deal with XML extension for xml files
325    if (selectedFile.getName().toLowerCase().endsWith(
326          EnsembleLibrary.XML_FILE_EXTENSION)) {
327
328      LibrarySerialization librarySerialization;
329
330      Vector classifiers = new Vector();
331
332      for (Iterator it = library.getModels().iterator(); it.hasNext();) {
333        EnsembleLibraryModel model = (EnsembleLibraryModel) it.next();
334        classifiers.add(model.getClassifier());
335      }
336
337      try {
338        librarySerialization = new LibrarySerialization();
339        librarySerialization.write(selectedFile.getPath(), classifiers);
340      } catch (Exception e) {
341        e.printStackTrace();
342      }
343
344      //deal with MLF extesion for flat files
345    } else if (selectedFile.getName().toLowerCase().endsWith(
346          EnsembleLibrary.FLAT_FILE_EXTENSION)) {
347
348      Writer writer = null;
349      try {
350        writer = new BufferedWriter(new FileWriter(selectedFile));
351
352        Iterator it = library.getModels().iterator();
353
354        while (it.hasNext()) {
355          EnsembleLibraryModel model = (EnsembleLibraryModel) it.next();
356          writer.write(model.getStringRepresentation() + "\n");
357        }
358
359        writer.close();
360      } catch (Exception ex) {
361        JOptionPane.showMessageDialog(dialogParent,
362            "Error writing file '" + selectedFile.getName()
363            + "':\n" + ex.getMessage(), "Save failed",
364            JOptionPane.ERROR_MESSAGE);
365        System.err.println(ex.getMessage());
366      }
367    }
368  }
369
370  /**
371   * Adds an object to the list of those that wish to be informed when the
372   * library changes.
373   *
374   * @param listener a new listener to add to the list
375   */
376  public void addPropertyChangeListener(PropertyChangeListener listener) {
377
378    if (m_LibraryPropertySupport != null) {
379      m_LibraryPropertySupport.addPropertyChangeListener(listener);
380
381    }
382  }
383
384  /**
385   * Returns the revision string.
386   *
387   * @return            the revision
388   */
389  public String getRevision() {
390    return RevisionUtils.extract("$Revision: 6041 $");
391  }
392}
Note: See TracBrowser for help on using the repository browser.