source: src/test/java/weka/classifiers/meta/EnsembleSelectionTest.java @ 21

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

Import di weka.

File size: 5.6 KB
RevLine 
[4]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
21package weka.classifiers.meta;
22
23import weka.classifiers.AbstractClassifierTest;
24import weka.classifiers.Classifier;
25import weka.classifiers.meta.ensembleSelection.EnsembleSelectionLibrary;
26
27import java.io.File;
28
29import junit.framework.Test;
30import junit.framework.TestSuite;
31
32/**
33 * Tests EnsembleSelection. Run from the command line with:<p/>
34 * java weka.classifiers.meta.EnsembleSelectionTest
35 *
36 * @author FracPete (fracpete at waikato dot ac dot nz)
37 * @version $Revision: 1.1 $
38 */
39public class EnsembleSelectionTest 
40  extends AbstractClassifierTest {
41
42  /** Default root location, relative to the users home direcory. */
43  private final static String DEFAULT_ROOT = "ensembleSelection/setup-1";
44
45  /**
46   * The name of the system property that can be used to override the
47   * location of the reference root.
48   */
49  private static final String ROOT_PROPERTY = "weka.classifiers.meta.EnsembleSelection.root";
50
51  /** Stores the root location under which output files are stored. */
52  private static File ROOT;
53
54  /** the test example setup */
55  private final static String DEFAULT_SETUP = "weka/classifiers/meta/EnsembleSelectionTest.model.xml";
56
57  /**
58   * default constructor
59   *
60   * @param name    the name
61   */
62  public EnsembleSelectionTest(String name) { 
63    super(name); 
64  }
65
66  /**
67   * Returns a <code>File</code> corresponding to the root of the ensemble
68   * output directory.
69   *
70   * @return the ensemble root directory (always the first one).
71   */
72  private static File getRoot() {
73    if (ROOT == null) {
74      String root = System.getProperty(ROOT_PROPERTY);
75      if (root == null) {
76        root = System.getProperty("user.dir");
77        ROOT = new File(root, DEFAULT_ROOT);
78      }
79      else {
80        ROOT = new File(root);
81      }
82    }
83
84    return ROOT;
85  }
86
87  /**
88   * returns the root directory with the specified index
89   *
90   * @param index       the index for the root dir
91   * @return            the File representing the root dir
92   */
93  private static File getRoot(int index) {
94    File        result;
95    File        root;
96   
97    root   = getRoot();
98    result = new File(root.getAbsolutePath().replaceAll("-[0-9]*$", "-" + index));
99   
100    return result;
101  }
102 
103  /**
104   * returns the next available root directory
105   *
106   * @return            the next available root directory
107   */
108  private static File getNextRoot() {
109    int         i;
110    File        result;
111   
112    i = 0;
113    do {
114      i++;
115      result = getRoot(i);
116    }
117    while (result.exists());
118   
119    return result;
120  }
121 
122  /**
123   * returns the next available root directory and creates it if it doesn't
124   * exist yet
125   *
126   * @return            the next available root directory
127   */
128  private static File getNextRoot(boolean create) {
129    File        result;
130   
131    result = getNextRoot();
132   
133    if (!result.exists() && create)
134      result.mkdirs();
135   
136    return result;
137  }
138 
139  /**
140   * Deletes all files and subdirectories under dir. 
141   * Returns true if all deletions were successful.
142   * If a deletion fails, the method stops attempting
143   * to delete and returns false.
144   *
145   * @param dir         the directory to delete
146   * @return            true if successful
147   */
148  public static boolean deleteDir(File dir) {
149    int         i;
150    File[]      files;
151    boolean     ok;
152   
153    if (dir.isDirectory()) {
154      files = dir.listFiles();
155      for (i = 0; i < files.length; i++) {
156        ok = deleteDir(files[i]);
157        // problem deleting directory?
158        if (!ok)
159          return false;
160      }
161    }
162   
163    // The directory is now empty so delete it
164    return dir.delete();
165  }
166
167  /**
168   * removes all the temporary directories created during a test run
169   */
170  private void deleteDirs() throws Exception {
171    File        root;
172    int         i;
173   
174    root = getRoot();
175    if (root.exists()) {
176      i = 1;
177      do {
178        if (!deleteDir(root))
179          System.out.println(
180              "Couldn't delete output directory '" + root + "'!");
181        i++;
182        root = getRoot(i);
183      }
184      while (root.exists());
185    }
186  }
187 
188  /**
189   * Called by JUnit before each test method. This implementation creates
190   * the default classifier to test and loads a test set of Instances.
191   *
192   * @throws Exception if an error occurs reading the example instances.
193   */
194  protected void setUp() throws Exception {
195    super.setUp();
196   
197    // delete output directories
198    deleteDirs();
199  }
200
201  /** Creates a default EnsembleSelection */
202  public Classifier getClassifier() {
203    EnsembleSelection   cls;
204
205    cls = new EnsembleSelection();
206    cls.setWorkingDirectory(getNextRoot(true));
207    try {
208      cls.setLibrary(
209          new EnsembleSelectionLibrary(
210              ClassLoader.getSystemResourceAsStream(DEFAULT_SETUP)));
211    }
212    catch (Exception e) {
213      cls.setLibrary(null);
214      e.printStackTrace();
215    }
216
217    return cls;
218  }
219
220  public static Test suite() {
221    return new TestSuite(EnsembleSelectionTest.class);
222  }
223
224  public static void main(String[] args){
225    junit.textui.TestRunner.run(suite());
226  }
227}
Note: See TracBrowser for help on using the repository browser.