source: tags/MetisMQIDemo/src/test/java/weka/classifiers/functions/supportVector/AbstractKernelTest.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 26.4 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 * Copyright (C) 2006 University of Waikato
19 */
20
21package weka.classifiers.functions.supportVector;
22
23import weka.core.Attribute;
24import weka.core.CheckGOE;
25import weka.core.CheckOptionHandler;
26import weka.core.Instances;
27import weka.core.OptionHandler;
28import weka.core.CheckScheme.PostProcessor;
29import weka.test.Regression;
30
31import junit.framework.TestCase;
32
33/**
34 * Abstract Test class for Kernels. Internally it uses the class
35 * <code>CheckKernel</code> to determine success or failure of the
36 * tests. It follows basically the <code>testsPerClassType</code> method.
37 *
38 * @author FracPete (fracpete at waikato dot ac dot nz)
39 * @version $Revision: 1.3 $
40 *
41 * @see CheckKernel
42 * @see CheckKernel#testsPerClassType(int, boolean, boolean)
43 * @see PostProcessor
44 */
45public abstract class AbstractKernelTest 
46  extends TestCase {
47 
48  /** The Kernel to be tested */
49  protected Kernel m_Kernel;
50
51  /** For testing the Kernel */
52  protected CheckKernel m_Tester;
53
54  /** whether Kernel handles weighted instances */
55  protected boolean m_weightedInstancesHandler;
56
57  /** whether Kernel handles multi-instance data */
58  protected boolean m_multiInstanceHandler;
59
60  /** the number of classes to test with testNClasses()
61   * @see #testNClasses() */
62  protected int m_NClasses;
63
64  /** whether to run CheckKernel in DEBUG mode */
65  protected boolean DEBUG = false;
66
67  /** the attribute type with the lowest value */
68  protected final static int FIRST_CLASSTYPE = Attribute.NUMERIC;
69
70  /** the attribute type with the highest value */
71  protected final static int LAST_CLASSTYPE = Attribute.RELATIONAL;
72 
73  /** wether Kernel can predict nominal attributes (array index is attribute type of class) */
74  protected boolean[] m_NominalPredictors;
75 
76  /** wether Kernel can predict numeric attributes (array index is attribute type of class) */
77  protected boolean[] m_NumericPredictors;
78 
79  /** wether Kernel can predict string attributes (array index is attribute type of class) */
80  protected boolean[] m_StringPredictors;
81 
82  /** wether Kernel can predict date attributes (array index is attribute type of class) */
83  protected boolean[] m_DatePredictors;
84 
85  /** wether Kernel can predict relational attributes (array index is attribute type of class) */
86  protected boolean[] m_RelationalPredictors;
87 
88  /** whether Kernel handles missing values */
89  protected boolean[] m_handleMissingPredictors;
90
91  /** whether Kernel handles class with only missing values */
92  protected boolean[] m_handleMissingClass;
93
94  /** whether Kernel handles class as first attribute */
95  protected boolean[] m_handleClassAsFirstAttribute;
96
97  /** whether Kernel handles class as second attribute */
98  protected boolean[] m_handleClassAsSecondAttribute;
99 
100  /** the results of the regression tests */
101  protected String[] m_RegressionResults;
102 
103  /** the OptionHandler tester */
104  protected CheckOptionHandler m_OptionTester;
105 
106  /** for testing GOE stuff */
107  protected CheckGOE m_GOETester;
108 
109  /**
110   * Constructs the <code>AbstractKernelTest</code>. Called by subclasses.
111   *
112   * @param name the name of the test class
113   */
114  public AbstractKernelTest(String name) { 
115    super(name); 
116  }
117
118  /**
119   * returns a custom PostProcessor for the CheckKernel datasets, currently
120   * only null.
121   *
122   * @return            a custom PostProcessor, if necessary
123   * @see PostProcessor
124   */
125  protected PostProcessor getPostProcessor() {
126    return null;
127  }
128 
129  /**
130   * configures the CheckKernel instance used throughout the tests
131   *
132   * @return    the fully configured CheckKernel instance used for testing
133   */
134  protected CheckKernel getTester() {
135    CheckKernel result;
136   
137    result = new CheckKernel();
138    result.setSilent(true);
139    result.setKernel(m_Kernel);
140    result.setNumInstances(20);
141    result.setDebug(DEBUG);
142    result.setPostProcessor(getPostProcessor());
143   
144    return result;
145  }
146 
147  /**
148   * Configures the CheckOptionHandler uses for testing the optionhandling.
149   * Sets the Kernel return from the getKernel() method.
150   *
151   * @return    the fully configured CheckOptionHandler
152   * @see       #getKernel()
153   */
154  protected CheckOptionHandler getOptionTester() {
155    CheckOptionHandler          result;
156   
157    result = new CheckOptionHandler();
158    if (getKernel() instanceof OptionHandler)
159      result.setOptionHandler((OptionHandler) getKernel());
160    else
161      result.setOptionHandler(null);
162    result.setUserOptions(new String[0]);
163    result.setSilent(true);
164   
165    return result;
166  }
167 
168  /**
169   * Configures the CheckGOE used for testing GOE stuff.
170   * Sets the Kernel returned from the getKernel() method.
171   *
172   * @return    the fully configured CheckGOE
173   * @see       #getKernel()
174   */
175  protected CheckGOE getGOETester() {
176    CheckGOE            result;
177   
178    result = new CheckGOE();
179    result.setObject(getKernel());
180    result.setSilent(true);
181   
182    return result;
183  }
184 
185  /**
186   * Called by JUnit before each test method. This implementation creates
187   * the default Kernel to test and loads a test set of Instances.
188   *
189   * @exception Exception if an error occurs reading the example instances.
190   */
191  protected void setUp() throws Exception {
192    m_Kernel       = getKernel();
193    m_Tester       = getTester();
194    m_OptionTester = getOptionTester();
195    m_GOETester    = getGOETester();
196
197    m_weightedInstancesHandler     = m_Tester.weightedInstancesHandler()[0];
198    m_multiInstanceHandler         = m_Tester.multiInstanceHandler()[0];
199    m_NominalPredictors            = new boolean[LAST_CLASSTYPE + 1];
200    m_NumericPredictors            = new boolean[LAST_CLASSTYPE + 1];
201    m_StringPredictors             = new boolean[LAST_CLASSTYPE + 1];
202    m_DatePredictors               = new boolean[LAST_CLASSTYPE + 1];
203    m_RelationalPredictors         = new boolean[LAST_CLASSTYPE + 1];
204    m_handleMissingPredictors      = new boolean[LAST_CLASSTYPE + 1];
205    m_handleMissingClass           = new boolean[LAST_CLASSTYPE + 1];
206    m_handleClassAsFirstAttribute  = new boolean[LAST_CLASSTYPE + 1];
207    m_handleClassAsSecondAttribute = new boolean[LAST_CLASSTYPE + 1];
208    m_RegressionResults            = new String[LAST_CLASSTYPE + 1];
209    m_NClasses                     = 4;
210
211    // initialize attributes
212    checkAttributes(true,  false, false, false, false, false);
213    checkAttributes(false, true,  false, false, false, false);
214    checkAttributes(false, false, true,  false, false, false);
215    checkAttributes(false, false, false, true,  false, false);
216    checkAttributes(false, false, false, false, true,  false);
217   
218    // initialize missing values handling
219    for (int i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
220      // does the scheme support this type of class at all?
221      if (!canPredict(i))
222        continue;
223     
224      // 20% missing
225      m_handleMissingPredictors[i] = checkMissingPredictors(i, 20, false);
226      m_handleMissingClass[i]      = checkMissingClass(i, 20, false);
227    }
228  }
229
230  /** Called by JUnit after each test method */
231  protected void tearDown() {
232    m_Kernel       = null;
233    m_Tester       = null;
234    m_OptionTester = null;
235    m_GOETester    = null;
236
237    m_weightedInstancesHandler     = false;
238    m_NominalPredictors            = new boolean[LAST_CLASSTYPE + 1];
239    m_NumericPredictors            = new boolean[LAST_CLASSTYPE + 1];
240    m_StringPredictors             = new boolean[LAST_CLASSTYPE + 1];
241    m_DatePredictors               = new boolean[LAST_CLASSTYPE + 1];
242    m_RelationalPredictors         = new boolean[LAST_CLASSTYPE + 1];
243    m_handleMissingPredictors      = new boolean[LAST_CLASSTYPE + 1];
244    m_handleMissingClass           = new boolean[LAST_CLASSTYPE + 1];
245    m_handleClassAsFirstAttribute  = new boolean[LAST_CLASSTYPE + 1];
246    m_handleClassAsSecondAttribute = new boolean[LAST_CLASSTYPE + 1];
247    m_RegressionResults            = new String[LAST_CLASSTYPE + 1];
248    m_NClasses                     = 4;
249  }
250
251  /**
252   * Used to create an instance of a specific Kernel.
253   *
254   * @return a suitably configured <code>Kernel</code> value
255   */
256  public abstract Kernel getKernel();
257
258  /**
259   * checks whether at least one attribute type can be handled with the
260   * given class type
261   *
262   * @param type      the class type to check for
263   * @return          true if at least one attribute type can be predicted with
264   *                  the given class
265   */
266  protected boolean canPredict(int type) {
267    return    m_NominalPredictors[type]
268           || m_NumericPredictors[type]
269           || m_StringPredictors[type]
270           || m_DatePredictors[type]
271           || m_RelationalPredictors[type];
272  }
273
274  /**
275   * returns a string for the class type
276   *
277   * @param type        the class type
278   * @return            the class type as string
279   */
280  protected String getClassTypeString(int type) {
281    return CheckKernel.attributeTypeToString(type);
282  }
283
284  /**
285   * tests whether the Kernel can handle certain attributes and if not,
286   * if the exception is OK
287   *
288   * @param nom         to check for nominal attributes
289   * @param num         to check for numeric attributes
290   * @param str         to check for string attributes
291   * @param dat         to check for date attributes
292   * @param rel         to check for relational attributes
293   * @param allowFail   whether a junit fail can be executed
294   * @see CheckKernel#canPredict(boolean, boolean, boolean, boolean, boolean, boolean, int)
295   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
296   */
297  protected void checkAttributes(boolean nom, boolean num, boolean str, 
298                                 boolean dat, boolean rel,
299                                 boolean allowFail) {
300    boolean[]     result;
301    String        att;
302    int           i;
303
304    // determine text for type of attributes
305    att = "";
306    if (nom)
307      att = "nominal";
308    else if (num)
309      att = "numeric";
310    else if (str)
311      att = "string";
312    else if (dat)
313      att = "date";
314    else if (rel)
315      att = "relational";
316   
317    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
318      result = m_Tester.canPredict(nom, num, str, dat, rel, m_multiInstanceHandler, i);
319      if (nom)
320        m_NominalPredictors[i] = result[0];
321      else if (num)
322        m_NumericPredictors[i] = result[0];
323      else if (str)
324        m_StringPredictors[i] = result[0];
325      else if (dat)
326        m_DatePredictors[i] = result[0];
327      else if (rel)
328        m_RelationalPredictors[i] = result[0];
329
330      if (!result[0] && !result[1] && allowFail)
331        fail("Error handling " + att + " attributes (" + getClassTypeString(i) 
332            + " class)!");
333    }
334  }
335
336  /**
337   * tests whether the Kernel can handle different types of attributes and
338   * if not, if the exception is OK
339   *
340   * @see #checkAttributes(boolean, boolean, boolean, boolean, boolean, boolean)
341   */
342  public void testAttributes() {
343    // nominal
344    checkAttributes(true,  false, false, false, false, true);
345    // numeric
346    checkAttributes(false, true,  false, false, false, true);
347    // string
348    checkAttributes(false, false, true,  false, false, true);
349    // date
350    checkAttributes(false, false, false, true,  false, true);
351    // relational
352    if (!m_multiInstanceHandler)
353      checkAttributes(false, false, false, false, true,  true);
354  }
355
356  /**
357   * tests whether the scheme declares a serialVersionUID.
358   */
359  public void testSerialVersionUID() {
360    boolean[]     result;
361
362    result = m_Tester.declaresSerialVersionUID();
363
364    if (!result[0])
365      fail("Doesn't declare serialVersionUID!");
366  }
367
368  /**
369   * tests whether the Kernel handles instance weights correctly
370   *
371   * @see CheckKernel#instanceWeights(boolean, boolean, boolean, boolean, boolean, boolean, int)
372   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
373   */
374  public void testInstanceWeights() {
375    boolean[]     result;
376    int           i;
377   
378    if (m_weightedInstancesHandler) {
379      for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
380        // does the Kernel support this type of class at all?
381        if (!canPredict(i))
382          continue;
383       
384        result = m_Tester.instanceWeights(
385            m_NominalPredictors[i], 
386            m_NumericPredictors[i], 
387            m_StringPredictors[i], 
388            m_DatePredictors[i], 
389            m_RelationalPredictors[i], 
390            m_multiInstanceHandler, 
391            i);
392
393        if (!result[0])
394          System.err.println("Error handling instance weights (" + getClassTypeString(i) 
395              + " class)!");
396      }
397    }
398  }
399
400  /**
401   * tests whether Kernel handles N classes
402   *
403   * @see CheckKernel#canHandleNClasses(boolean, boolean, boolean, boolean, boolean, boolean, int)
404   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
405   * @see #m_NClasses
406   */
407  public void testNClasses() {
408    boolean[]     result;
409
410    if (!canPredict(Attribute.NOMINAL))
411      return;
412
413    result = m_Tester.canHandleNClasses(
414        m_NominalPredictors[Attribute.NOMINAL],
415        m_NumericPredictors[Attribute.NOMINAL],
416        m_StringPredictors[Attribute.NOMINAL],
417        m_DatePredictors[Attribute.NOMINAL],
418        m_RelationalPredictors[Attribute.NOMINAL],
419        m_multiInstanceHandler,
420        m_NClasses);
421
422    if (!result[0] && !result[1])
423      fail("Error handling " + m_NClasses + " classes!");
424  }
425
426  /**
427   * checks whether the Kernel can handle the class attribute at a given
428   * position (0-based index, -1 means last).
429   *
430   * @param type        the class type
431   * @param position    the position of the class attribute (0-based, -1 means last)
432   * @return            true if the Kernel can handle it
433   */
434  protected boolean checkClassAsNthAttribute(int type, int position) {
435    boolean[]     result;
436    String        indexStr;
437   
438    result = m_Tester.canHandleClassAsNthAttribute(
439        m_NominalPredictors[type], 
440        m_NumericPredictors[type], 
441        m_StringPredictors[type], 
442        m_DatePredictors[type], 
443        m_RelationalPredictors[type], 
444        m_multiInstanceHandler, 
445        type,
446        position);
447
448    if (position == -1)
449      indexStr = "last";
450    else
451      indexStr = (position + 1) + ".";
452   
453    if (!result[0] && !result[1])
454      fail("Error handling class as " + indexStr + " attribute (" 
455          + getClassTypeString(type) + " class)!");
456   
457    return result[0];
458  }
459
460  /**
461   * Tests whether the Kernel can handle class attributes as Nth
462   * attribute. In case of multi-instance Kernels it performs no tests,
463   * since the multi-instance data has a fixed format (bagID,bag,class).
464   *
465   * @see CheckKernel#canHandleClassAsNthAttribute(boolean, boolean, boolean, boolean, boolean, boolean, int, int)
466   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
467   */
468  public void testClassAsNthAttribute() {
469    int           i;
470   
471    // multi-Instance data has fixed format!
472    if (m_multiInstanceHandler)
473      return;
474   
475    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
476      // does the Kernel support this type of class at all?
477      if (!canPredict(i))
478        continue;
479     
480      // first attribute
481      m_handleClassAsFirstAttribute[i] = checkClassAsNthAttribute(i, 0);
482
483      // second attribute
484      m_handleClassAsSecondAttribute[i] = checkClassAsNthAttribute(i, 1);
485    }
486  }
487
488  /**
489   * tests whether the Kernel can handle zero training instances
490   *
491   * @see CheckKernel#canHandleZeroTraining(boolean, boolean, boolean, boolean, boolean, boolean, int)
492   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
493   */
494  public void testZeroTraining() {
495    boolean[]     result;
496    int           i;
497   
498    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
499      // does the Kernel support this type of class at all?
500      if (!canPredict(i))
501        continue;
502     
503      result = m_Tester.canHandleZeroTraining(
504          m_NominalPredictors[i], 
505          m_NumericPredictors[i], 
506          m_StringPredictors[i], 
507          m_DatePredictors[i], 
508          m_RelationalPredictors[i], 
509          m_multiInstanceHandler, 
510          i);
511
512      if (!result[0] && !result[1])
513        fail("Error handling zero training instances (" + getClassTypeString(i) 
514            + " class)!");
515    }
516  }
517
518  /**
519   * checks whether the Kernel can handle the given percentage of
520   * missing predictors
521   *
522   * @param type        the class type
523   * @param percent     the percentage of missing predictors
524   * @param allowFail   if true a fail statement may be executed
525   * @return            true if the Kernel can handle it
526   */
527  protected boolean checkMissingPredictors(int type, int percent, boolean allowFail) {
528    boolean[]     result;
529   
530    result = m_Tester.canHandleMissing(
531        m_NominalPredictors[type], 
532        m_NumericPredictors[type], 
533        m_StringPredictors[type], 
534        m_DatePredictors[type], 
535        m_RelationalPredictors[type], 
536        m_multiInstanceHandler, 
537        type,
538        true,
539        false,
540        percent);
541
542    if (allowFail) {
543      if (!result[0] && !result[1])
544        fail("Error handling " + percent + "% missing predictors (" 
545            + getClassTypeString(type) + " class)!");
546    }
547   
548    return result[0];
549  }
550
551  /**
552   * tests whether the Kernel can handle missing predictors (20% and 100%)
553   *
554   * @see CheckKernel#canHandleMissing(boolean, boolean, boolean, boolean, boolean, boolean, int, boolean, boolean, int)
555   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
556   */
557  public void testMissingPredictors() {
558    int           i;
559   
560    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
561      // does the Kernel support this type of class at all?
562      if (!canPredict(i))
563        continue;
564     
565      // 20% missing
566      checkMissingPredictors(i, 20, true);
567
568      // 100% missing
569      if (m_handleMissingPredictors[i])
570        checkMissingPredictors(i, 100, true);
571    }
572  }
573
574  /**
575   * checks whether the Kernel can handle the given percentage of
576   * missing class labels
577   *
578   * @param type        the class type
579   * @param percent     the percentage of missing class labels
580   * @param allowFail   if true a fail statement may be executed
581   * @return            true if the Kernel can handle it
582   */
583  protected boolean checkMissingClass(int type, int percent, boolean allowFail) {
584    boolean[]     result;
585   
586    result = m_Tester.canHandleMissing(
587        m_NominalPredictors[type], 
588        m_NumericPredictors[type], 
589        m_StringPredictors[type], 
590        m_DatePredictors[type], 
591        m_RelationalPredictors[type], 
592        m_multiInstanceHandler, 
593        type,
594        false,
595        true,
596        percent);
597
598    if (allowFail) {
599      if (!result[0] && !result[1])
600        fail("Error handling " + percent + "% missing class labels (" 
601            + getClassTypeString(type) + " class)!");
602    }
603   
604    return result[0];
605  }
606
607  /**
608   * tests whether the Kernel can handle missing class values (20% and
609   * 100%)
610   *
611   * @see CheckKernel#canHandleMissing(boolean, boolean, boolean, boolean, boolean, boolean, int, boolean, boolean, int)
612   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
613   */
614  public void testMissingClass() {
615    int           i;
616   
617    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
618      // does the Kernel support this type of class at all?
619      if (!canPredict(i))
620        continue;
621     
622      // 20% missing
623      checkMissingClass(i, 20, true);
624
625      // 100% missing
626      if (m_handleMissingClass[i])
627        checkMissingClass(i, 100, true);
628    }
629  }
630
631  /**
632   * tests whether the Kernel correctly initializes in the
633   * buildKernel method
634   *
635   * @see CheckKernel#correctBuildInitialisation(boolean, boolean, boolean, boolean, boolean, boolean, int)
636   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
637   */
638  public void testBuildInitialization() {
639    boolean[]     result;
640    int           i;
641   
642    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
643      // does the Kernel support this type of class at all?
644      if (!canPredict(i))
645        continue;
646     
647      result = m_Tester.correctBuildInitialisation(
648          m_NominalPredictors[i], 
649          m_NumericPredictors[i], 
650          m_StringPredictors[i], 
651          m_DatePredictors[i], 
652          m_RelationalPredictors[i], 
653          m_multiInstanceHandler, 
654          i);
655
656      if (!result[0] && !result[1])
657        fail("Incorrect build initialization (" + getClassTypeString(i) 
658            + " class)!");
659    }
660  }
661
662  /**
663   * tests whether the Kernel alters the training set during training.
664   *
665   * @see CheckKernel#datasetIntegrity(boolean, boolean, boolean, boolean, boolean, boolean, int, boolean, boolean)
666   * @see CheckKernel#testsPerClassType(int, boolean, boolean)
667   */
668  public void testDatasetIntegrity() {
669    boolean[]     result;
670    int           i;
671   
672    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
673      // does the Kernel support this type of class at all?
674      if (!canPredict(i))
675        continue;
676     
677      result = m_Tester.datasetIntegrity(
678          m_NominalPredictors[i], 
679          m_NumericPredictors[i], 
680          m_StringPredictors[i], 
681          m_DatePredictors[i], 
682          m_RelationalPredictors[i], 
683          m_multiInstanceHandler, 
684          i,
685          m_handleMissingPredictors[i],
686          m_handleMissingClass[i]);
687
688      if (!result[0] && !result[1])
689        fail("Training set is altered during training (" 
690            + getClassTypeString(i) + " class)!");
691    }
692  }
693
694  /**
695   * Builds a model using the current Kernel using the given data and
696   * returns the produced output.
697   *
698   * @param data        the instances to test the Kernel on
699   * @return            a String containing the output of the Kernel.
700   */
701  protected String useKernel(Instances data) throws Exception {
702    Kernel kernel = null;
703    StringBuffer text = new StringBuffer();
704   
705    try {
706      kernel = Kernel.makeCopy(m_Kernel);
707    } 
708    catch (Exception e) {
709      e.printStackTrace();
710      fail("Problem setting up to use Kernel: " + e);
711    }
712
713    kernel.buildKernel(data);
714    for (int n = 0; n < data.numInstances(); n++) {
715      for (int i = n; i < data.numInstances(); i++) {
716        text.append(
717            (n+1) + "-" + (i+1) + ": " 
718            + kernel.eval(n, i, data.instance(i)) + "\n");
719      }
720    }
721   
722    return text.toString();
723  }
724 
725  /**
726   * Provides a hook for derived classes to further modify the data. Currently,
727   * the data is just passed through.
728   *
729   * @param data        the data to process
730   * @return            the processed data
731   */
732  protected Instances process(Instances data) {
733    return data;
734  }
735
736  /**
737   * Runs a regression test -- this checks that the output of the tested
738   * object matches that in a reference version. When this test is
739   * run without any pre-existing reference output, the reference version
740   * is created.
741   */
742  public void testRegression() throws Exception {
743    int         i;
744    boolean     succeeded;
745    Regression  reg;
746    Instances   train;
747   
748    // don't bother if not working correctly
749    if (m_Tester.hasClasspathProblems())
750      return;
751   
752    reg = new Regression(this.getClass());
753    succeeded = false;
754    train = null;
755   
756    for (i = FIRST_CLASSTYPE; i <= LAST_CLASSTYPE; i++) {
757      // does the Kernel support this type of class at all?
758      if (!canPredict(i))
759        continue;
760       
761      train = m_Tester.makeTestDataset(
762          42, m_Tester.getNumInstances(), 
763          m_NominalPredictors[i] ? 2 : 0,
764          m_NumericPredictors[i] ? 1 : 0, 
765          m_StringPredictors[i] ? 1 : 0,
766          m_DatePredictors[i] ? 1 : 0,
767          m_RelationalPredictors[i] ? 1 : 0,
768          2, 
769          i,
770          m_multiInstanceHandler);
771 
772      try {
773        m_RegressionResults[i] =   CheckKernel.attributeTypeToString(i) 
774                                 + " class:\n" 
775                                 + useKernel(train);
776        succeeded = true;
777        reg.println(m_RegressionResults[i]);
778      }
779      catch (Exception e) {
780        String msg = e.getMessage().toLowerCase();
781        if (msg.indexOf("not in classpath") > -1)
782          return;
783
784        m_RegressionResults[i] = null;
785      }
786    }
787   
788    if (!succeeded) {
789      fail("Problem during regression testing: no successful predictions for any class type");
790    }
791
792    try {
793      String diff = reg.diff();
794      if (diff == null) {
795        System.err.println("Warning: No reference available, creating."); 
796      } else if (!diff.equals("")) {
797        fail("Regression test failed. Difference:\n" + diff);
798      }
799    } 
800    catch (java.io.IOException ex) {
801      fail("Problem during regression testing.\n" + ex);
802    }
803  }
804 
805  /**
806   * tests the listing of the options
807   */
808  public void testListOptions() {
809    if (m_OptionTester.getOptionHandler() != null) {
810      if (!m_OptionTester.checkListOptions())
811        fail("Options cannot be listed via listOptions.");
812    }
813  }
814 
815  /**
816   * tests the setting of the options
817   */
818  public void testSetOptions() {
819    if (m_OptionTester.getOptionHandler() != null) {
820      if (!m_OptionTester.checkSetOptions())
821        fail("setOptions method failed.");
822    }
823  }
824 
825  /**
826   * tests whether there are any remaining options
827   */
828  public void testRemainingOptions() {
829    if (m_OptionTester.getOptionHandler() != null) {
830      if (!m_OptionTester.checkRemainingOptions())
831        fail("There were 'left-over' options.");
832    }
833  }
834 
835  /**
836   * tests the whether the user-supplied options stay the same after setting.
837   * getting, and re-setting again.
838   *
839   * @see       #getOptionTester()
840   */
841  public void testCanonicalUserOptions() {
842    if (m_OptionTester.getOptionHandler() != null) {
843      if (!m_OptionTester.checkCanonicalUserOptions())
844        fail("setOptions method failed");
845    }
846  }
847 
848  /**
849   * tests the resetting of the options to the default ones
850   */
851  public void testResettingOptions() {
852    if (m_OptionTester.getOptionHandler() != null) {
853      if (!m_OptionTester.checkSetOptions())
854        fail("Resetting of options failed");
855    }
856  }
857 
858  /**
859   * tests for a globalInfo method
860   */
861  public void testGlobalInfo() {
862    if (!m_GOETester.checkGlobalInfo())
863      fail("No globalInfo method");
864  }
865 
866  /**
867   * tests the tool tips
868   */
869  public void testToolTips() {
870    if (!m_GOETester.checkToolTips())
871      fail("Tool tips inconsistent");
872  }
873}
Note: See TracBrowser for help on using the repository browser.