~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/CheckScheme.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * CheckScheme.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.core;
 
24
 
 
25
import java.util.Enumeration;
 
26
import java.util.Random;
 
27
import java.util.StringTokenizer;
 
28
import java.util.Vector;
 
29
 
 
30
/**
 
31
 * Abstract general class for testing schemes in Weka. Derived classes are
 
32
 * also used for JUnit tests.
 
33
 *
 
34
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
35
 * @version $Revision: 1.3 $
 
36
 * @see TestInstances
 
37
 */
 
38
public abstract class CheckScheme
 
39
  extends Check {
 
40
  
 
41
  /** a class for postprocessing the test-data */
 
42
  public static class PostProcessor {
 
43
    /**
 
44
     * Provides a hook for derived classes to further modify the data. Currently,
 
45
     * the data is just passed through.
 
46
     * 
 
47
     * @param data      the data to process
 
48
     * @return          the processed data
 
49
     */
 
50
    public Instances process(Instances data) {
 
51
      return data;
 
52
    }
 
53
  }
 
54
  
 
55
  /** The number of instances in the datasets */
 
56
  protected int m_NumInstances = 20;
 
57
  
 
58
  /** the number of nominal attributes */
 
59
  protected int m_NumNominal = 2;
 
60
  
 
61
  /** the number of numeric attributes */
 
62
  protected int m_NumNumeric = 1;
 
63
  
 
64
  /** the number of string attributes */
 
65
  protected int m_NumString = 1;
 
66
  
 
67
  /** the number of date attributes */
 
68
  protected int m_NumDate = 1;
 
69
  
 
70
  /** the number of relational attributes */
 
71
  protected int m_NumRelational = 1;
 
72
  
 
73
  /** the number of instances in relational attributes (applies also for bags
 
74
   * in multi-instance) */
 
75
  protected int m_NumInstancesRelational = 10;
 
76
  
 
77
  /** for generating String attributes/classes */
 
78
  protected String[] m_Words = TestInstances.DEFAULT_WORDS;
 
79
  
 
80
  /** for generating String attributes/classes */
 
81
  protected String m_WordSeparators = TestInstances.DEFAULT_SEPARATORS;
 
82
  
 
83
  /** for post-processing the data even further */
 
84
  protected PostProcessor m_PostProcessor = null;
 
85
  
 
86
  /** whether classpath problems occurred */
 
87
  protected boolean m_ClasspathProblems = false;
 
88
  
 
89
  /**
 
90
   * Returns an enumeration describing the available options.
 
91
   *
 
92
   * @return an enumeration of all the available options.
 
93
   */
 
94
  public Enumeration listOptions() {
 
95
    Vector result = new Vector();
 
96
    
 
97
    Enumeration en = super.listOptions();
 
98
    while (en.hasMoreElements())
 
99
      result.addElement(en.nextElement());
 
100
    
 
101
    result.addElement(new Option(
 
102
        "\tThe number of instances in the datasets (default 20).",
 
103
        "N", 1, "-N <num>"));
 
104
 
 
105
    result.addElement(new Option(
 
106
        "\tThe number of nominal attributes (default 2).",
 
107
        "nominal", 1, "-nominal <num>"));
 
108
    
 
109
    result.addElement(new Option(
 
110
        "\tThe number of values for nominal attributes (default 1).",
 
111
        "nominal-values", 1, "-nominal-values <num>"));
 
112
    
 
113
    result.addElement(new Option(
 
114
        "\tThe number of numeric attributes (default 1).",
 
115
        "numeric", 1, "-numeric <num>"));
 
116
    
 
117
    result.addElement(new Option(
 
118
        "\tThe number of string attributes (default 1).",
 
119
        "string", 1, "-string <num>"));
 
120
    
 
121
    result.addElement(new Option(
 
122
        "\tThe number of date attributes (default 1).",
 
123
        "date", 1, "-date <num>"));
 
124
    
 
125
    result.addElement(new Option(
 
126
        "\tThe number of relational attributes (default 1).",
 
127
        "relational", 1, "-relational <num>"));
 
128
    
 
129
    result.addElement(new Option(
 
130
        "\tThe number of instances in relational/bag attributes (default 10).",
 
131
        "num-instances-relational", 1, "-num-instances-relational <num>"));
 
132
    
 
133
    result.addElement(new Option(
 
134
        "\tThe words to use in string attributes.",
 
135
        "words", 1, "-words <comma-separated-list>"));
 
136
    
 
137
    result.addElement(new Option(
 
138
        "\tThe word separators to use in string attributes.",
 
139
        "word-separators", 1, "-word-separators <chars>"));
 
140
    
 
141
    return result.elements();
 
142
  }
 
143
  
 
144
  /**
 
145
   * Parses a given list of options. 
 
146
   *
 
147
   * @param options the list of options as an array of strings
 
148
   * @throws Exception if an option is not supported
 
149
   */
 
150
  public void setOptions(String[] options) throws Exception {
 
151
    String      tmpStr;
 
152
    
 
153
    super.setOptions(options);
 
154
    
 
155
    tmpStr = Utils.getOption('N', options);
 
156
    if (tmpStr.length() != 0)
 
157
      setNumInstances(Integer.parseInt(tmpStr));
 
158
    else
 
159
      setNumInstances(20);
 
160
    
 
161
    tmpStr = Utils.getOption("nominal", options);
 
162
    if (tmpStr.length() != 0)
 
163
      setNumNominal(Integer.parseInt(tmpStr));
 
164
    else
 
165
      setNumNominal(2);
 
166
    
 
167
    tmpStr = Utils.getOption("numeric", options);
 
168
    if (tmpStr.length() != 0)
 
169
      setNumNumeric(Integer.parseInt(tmpStr));
 
170
    else
 
171
      setNumNumeric(1);
 
172
    
 
173
    tmpStr = Utils.getOption("string", options);
 
174
    if (tmpStr.length() != 0)
 
175
      setNumString(Integer.parseInt(tmpStr));
 
176
    else
 
177
      setNumString(1);
 
178
    
 
179
    tmpStr = Utils.getOption("date", options);
 
180
    if (tmpStr.length() != 0)
 
181
      setNumDate(Integer.parseInt(tmpStr));
 
182
    else
 
183
      setNumDate(1);
 
184
    
 
185
    tmpStr = Utils.getOption("relational", options);
 
186
    if (tmpStr.length() != 0)
 
187
      setNumRelational(Integer.parseInt(tmpStr));
 
188
    else
 
189
      setNumRelational(1);
 
190
    
 
191
    tmpStr = Utils.getOption("num-instances-relational", options);
 
192
    if (tmpStr.length() != 0)
 
193
      setNumInstancesRelational(Integer.parseInt(tmpStr));
 
194
    else
 
195
      setNumInstancesRelational(10);
 
196
    
 
197
    tmpStr = Utils.getOption("words", options);
 
198
    if (tmpStr.length() != 0)
 
199
      setWords(tmpStr);
 
200
    else
 
201
      setWords(new TestInstances().getWords());
 
202
    
 
203
    if (Utils.getOptionPos("word-separators", options) > -1) {
 
204
      tmpStr = Utils.getOption("word-separators", options);
 
205
      setWordSeparators(tmpStr);
 
206
    }
 
207
    else {
 
208
      setWordSeparators(TestInstances.DEFAULT_SEPARATORS);
 
209
    }
 
210
  }
 
211
  
 
212
  /**
 
213
   * Gets the current settings of the CheckClassifier.
 
214
   *
 
215
   * @return an array of strings suitable for passing to setOptions
 
216
   */
 
217
  public String[] getOptions() {
 
218
    Vector        result;
 
219
    String[]      options;
 
220
    int           i;
 
221
    
 
222
    result = new Vector();
 
223
    
 
224
    options = super.getOptions();
 
225
    for (i = 0; i < options.length; i++)
 
226
      result.add(options[i]);
 
227
    
 
228
    result.add("-N");
 
229
    result.add("" + getNumInstances());
 
230
    
 
231
    result.add("-nominal");
 
232
    result.add("" + getNumNominal());
 
233
    
 
234
    result.add("-numeric");
 
235
    result.add("" + getNumNumeric());
 
236
    
 
237
    result.add("-string");
 
238
    result.add("" + getNumString());
 
239
    
 
240
    result.add("-date");
 
241
    result.add("" + getNumDate());
 
242
    
 
243
    result.add("-relational");
 
244
    result.add("" + getNumRelational());
 
245
    
 
246
    result.add("-words");
 
247
    result.add("" + getWords());
 
248
    
 
249
    result.add("-word-separators");
 
250
    result.add("" + getWordSeparators());
 
251
    
 
252
    return (String[]) result.toArray(new String[result.size()]);
 
253
  }
 
254
  
 
255
  /**
 
256
   * sets the PostProcessor to use
 
257
   * 
 
258
   * @param value       the new PostProcessor
 
259
   * @see #m_PostProcessor
 
260
   */
 
261
  public void setPostProcessor(PostProcessor value) {
 
262
    m_PostProcessor = value;
 
263
  }
 
264
  
 
265
  /**
 
266
   * returns the current PostProcessor, can be null
 
267
   * 
 
268
   * @return            the current PostProcessor
 
269
   */
 
270
  public PostProcessor getPostProcessor() {
 
271
    return m_PostProcessor;
 
272
  }
 
273
  
 
274
  /**
 
275
   * returns TRUE if the classifier returned a "not in classpath" Exception
 
276
   * 
 
277
   * @return    true if CLASSPATH problems occurred
 
278
   */
 
279
  public boolean hasClasspathProblems() {
 
280
    return m_ClasspathProblems;
 
281
  }
 
282
  
 
283
  /**
 
284
   * Begin the tests, reporting results to System.out
 
285
   */
 
286
  public abstract void doTests();
 
287
  
 
288
  /**
 
289
   * Sets the number of instances to use in the datasets (some classifiers
 
290
   * might require more instances).
 
291
   *
 
292
   * @param value the number of instances to use
 
293
   */
 
294
  public void setNumInstances(int value) {
 
295
    m_NumInstances = value;
 
296
  }
 
297
  
 
298
  /**
 
299
   * Gets the current number of instances to use for the datasets.
 
300
   *
 
301
   * @return the number of instances
 
302
   */
 
303
  public int getNumInstances() {
 
304
    return m_NumInstances;
 
305
  }
 
306
  
 
307
  /**
 
308
   * sets the number of nominal attributes
 
309
   * 
 
310
   * @param value       the number of nominal attributes
 
311
   */
 
312
  public void setNumNominal(int value) {
 
313
    m_NumNominal = value;
 
314
  }
 
315
  
 
316
  /**
 
317
   * returns the current number of nominal attributes
 
318
   * 
 
319
   * @return            the number of nominal attributes
 
320
   */
 
321
  public int getNumNominal() {
 
322
    return m_NumNominal;
 
323
  }
 
324
  
 
325
  /**
 
326
   * sets the number of numeric attributes
 
327
   * 
 
328
   * @param value       the number of numeric attributes
 
329
   */
 
330
  public void setNumNumeric(int value) {
 
331
    m_NumNumeric = value;
 
332
  }
 
333
  
 
334
  /**
 
335
   * returns the current number of numeric attributes
 
336
   * 
 
337
   * @return            the number of numeric attributes
 
338
   */
 
339
  public int getNumNumeric() {
 
340
    return m_NumNumeric;
 
341
  }
 
342
  
 
343
  /**
 
344
   * sets the number of string attributes
 
345
   * 
 
346
   * @param value       the number of string attributes
 
347
   */
 
348
  public void setNumString(int value) {
 
349
    m_NumString = value;
 
350
  }
 
351
  
 
352
  /**
 
353
   * returns the current number of string attributes
 
354
   * 
 
355
   * @return            the number of string attributes
 
356
   */
 
357
  public int getNumString() {
 
358
    return m_NumString;
 
359
  }
 
360
  
 
361
  /**
 
362
   * sets the number of data attributes
 
363
   * 
 
364
   * @param value       the number of date attributes
 
365
   */
 
366
  public void setNumDate(int value) {
 
367
    m_NumDate = value;
 
368
  }
 
369
  
 
370
  /**
 
371
   * returns the current number of date attributes
 
372
   * 
 
373
   * @return            the number of date attributes
 
374
   */
 
375
  public int getNumDate() {
 
376
    return m_NumDate;
 
377
  }
 
378
  
 
379
  /**
 
380
   * sets the number of relational attributes
 
381
   * 
 
382
   * @param value       the number of relational attributes
 
383
   */
 
384
  public void setNumRelational(int value) {
 
385
    m_NumRelational = value;
 
386
  }
 
387
  
 
388
  /**
 
389
   * returns the current number of relational attributes
 
390
   * 
 
391
   * @return            the number of relational attributes
 
392
   */
 
393
  public int getNumRelational() {
 
394
    return m_NumRelational;
 
395
  }
 
396
  
 
397
  /**
 
398
   * sets the number of instances in relational/bag attributes to produce
 
399
   * 
 
400
   * @param value       the number of instances
 
401
   */
 
402
  public void setNumInstancesRelational(int value) {
 
403
    m_NumInstancesRelational = value;
 
404
  }
 
405
  
 
406
  /**
 
407
   * returns the current number of instances in relational/bag attributes to produce
 
408
   * 
 
409
   * @return            the number of instances
 
410
   */
 
411
  public int getNumInstancesRelational() {
 
412
    return m_NumInstancesRelational;
 
413
  }
 
414
 
 
415
  /**
 
416
   * turns the comma-separated list into an array
 
417
   * 
 
418
   * @param value       the list to process
 
419
   * @return            the list as array
 
420
   */
 
421
  protected static String[] listToArray(String value) {
 
422
    StringTokenizer     tok;
 
423
    Vector              list;
 
424
    
 
425
    list = new Vector();
 
426
    tok = new StringTokenizer(value, ",");
 
427
    while (tok.hasMoreTokens())
 
428
      list.add(tok.nextToken());
 
429
    
 
430
    return (String[]) list.toArray(new String[list.size()]);
 
431
  }
 
432
  
 
433
  /**
 
434
   * turns the array into a comma-separated list
 
435
   * 
 
436
   * @param value       the array to process
 
437
   * @return            the array as list
 
438
   */
 
439
  protected static String arrayToList(String[] value) {
 
440
    String      result;
 
441
    int         i;
 
442
    
 
443
    result = "";
 
444
    
 
445
    for (i = 0; i < value.length; i++) {
 
446
      if (i > 0)
 
447
        result += ",";
 
448
      result += value[i];
 
449
    }
 
450
    
 
451
    return result;
 
452
  }
 
453
  
 
454
  /**
 
455
   * returns a string representation of the attribute type
 
456
   * 
 
457
   * @param type        the attribute type to get a string rerpresentation for
 
458
   * @return            the string representation
 
459
   */
 
460
  public static String attributeTypeToString(int type) {
 
461
    String      result;
 
462
    
 
463
    switch (type) {
 
464
      case Attribute.NUMERIC:
 
465
        result = "numeric";
 
466
        break;
 
467
        
 
468
      case Attribute.NOMINAL:
 
469
        result = "nominal";
 
470
        break;
 
471
        
 
472
      case Attribute.STRING:
 
473
        result = "string";
 
474
        break;
 
475
        
 
476
      case Attribute.DATE:
 
477
        result = "date";
 
478
        break;
 
479
        
 
480
      case Attribute.RELATIONAL:
 
481
        result = "relational";
 
482
        break;
 
483
 
 
484
      default:
 
485
        result = "???";
 
486
    }
 
487
    
 
488
    return result;
 
489
  }
 
490
  
 
491
  /**
 
492
   * Sets the comma-separated list of words to use for generating strings. The
 
493
   * list must contain at least 2 words, otherwise an exception will be thrown.
 
494
   * 
 
495
   * @param value                       the list of words
 
496
   * @throws IllegalArgumentException   if not at least 2 words are provided
 
497
   */
 
498
  public void setWords(String value) {
 
499
    if (listToArray(value).length < 2)
 
500
      throw new IllegalArgumentException("At least 2 words must be provided!");
 
501
    
 
502
    m_Words = listToArray(value);
 
503
  }
 
504
  
 
505
  /**
 
506
   * returns the words used for assembling strings in a comma-separated list.
 
507
   * 
 
508
   * @return            the words as comma-separated list
 
509
   */
 
510
  public String getWords() {
 
511
    return arrayToList(m_Words);
 
512
  }
 
513
 
 
514
  /**
 
515
   * sets the word separators (chars) to use for assembling strings.
 
516
   * 
 
517
   * @param value       the characters to use as separators
 
518
   */
 
519
  public void setWordSeparators(String value) {
 
520
    m_WordSeparators = value;
 
521
  }
 
522
  
 
523
  /**
 
524
   * returns the word separators (chars) to use for assembling strings.
 
525
   * 
 
526
   * @return            the current separators
 
527
   */
 
528
  public String getWordSeparators() {
 
529
    return m_WordSeparators;
 
530
  }
 
531
  
 
532
  /**
 
533
   * Compare two datasets to see if they differ.
 
534
   *
 
535
   * @param data1 one set of instances
 
536
   * @param data2 the other set of instances
 
537
   * @throws Exception if the datasets differ
 
538
   */
 
539
  protected void compareDatasets(Instances data1, Instances data2)
 
540
    throws Exception {
 
541
    
 
542
    if (!data2.equalHeaders(data1)) {
 
543
      throw new Exception("header has been modified");
 
544
    }
 
545
    if (!(data2.numInstances() == data1.numInstances())) {
 
546
      throw new Exception("number of instances has changed");
 
547
    }
 
548
    for (int i = 0; i < data2.numInstances(); i++) {
 
549
      Instance orig = data1.instance(i);
 
550
      Instance copy = data2.instance(i);
 
551
      for (int j = 0; j < orig.numAttributes(); j++) {
 
552
        if (orig.isMissing(j)) {
 
553
          if (!copy.isMissing(j)) {
 
554
            throw new Exception("instances have changed");
 
555
          }
 
556
        } else if (orig.value(j) != copy.value(j)) {
 
557
          throw new Exception("instances have changed");
 
558
        }
 
559
        if (orig.weight() != copy.weight()) {
 
560
          throw new Exception("instance weights have changed");
 
561
        }         
 
562
      }
 
563
    }
 
564
  }
 
565
  
 
566
  /**
 
567
   * Add missing values to a dataset.
 
568
   *
 
569
   * @param data the instances to add missing values to
 
570
   * @param level the level of missing values to add (if positive, this
 
571
   * is the probability that a value will be set to missing, if negative
 
572
   * all but one value will be set to missing (not yet implemented))
 
573
   * @param predictorMissing if true, predictor attributes will be modified
 
574
   * @param classMissing if true, the class attribute will be modified
 
575
   */
 
576
  protected void addMissing(Instances data, int level,
 
577
      boolean predictorMissing, boolean classMissing) {
 
578
    
 
579
    int classIndex = data.classIndex();
 
580
    Random random = new Random(1);
 
581
    for (int i = 0; i < data.numInstances(); i++) {
 
582
      Instance current = data.instance(i);
 
583
      for (int j = 0; j < data.numAttributes(); j++) {
 
584
        if (((j == classIndex) && classMissing) ||
 
585
            ((j != classIndex) && predictorMissing)) {
 
586
          if (Math.abs(random.nextInt()) % 100 < level)
 
587
            current.setMissing(j);
 
588
        }
 
589
      }
 
590
    }
 
591
  }
 
592
  
 
593
  /**
 
594
   * Provides a hook for derived classes to further modify the data. 
 
595
   * 
 
596
   * @param data        the data to process
 
597
   * @return            the processed data
 
598
   * @see #m_PostProcessor
 
599
   */
 
600
  protected Instances process(Instances data) {
 
601
    if (getPostProcessor() == null)
 
602
      return data;
 
603
    else
 
604
      return getPostProcessor().process(data);
 
605
  }
 
606
}