source: src/main/java/weka/gui/explorer/AbstractPlotInstances.java @ 9

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

Import di weka.

File size: 4.7 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 * AbstractPlotInstances.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.explorer;
23
24import weka.core.Instances;
25import weka.core.OptionHandler;
26import weka.gui.visualize.PlotData2D;
27
28import java.io.Serializable;
29import java.util.Enumeration;
30import java.util.Vector;
31
32/**
33 * Abstract superclass for generating plottable instances.
34 *
35 * @author  fracpete (fracpete at waikato dot ac dot nz)
36 * @version $Revision: 5714 $
37 */
38public abstract class AbstractPlotInstances
39  implements Serializable, OptionHandler {
40
41  /** for serialization. */
42  private static final long serialVersionUID = 2375155184845160908L;
43
44  /** the full dataset. */
45  protected Instances m_Instances;
46 
47  /** the plotable instances. */
48  protected Instances m_PlotInstances;
49
50  /** whether processing has been finished up already. */
51  protected boolean m_FinishUpCalled;
52 
53  /**
54   * Initializes the container.
55   */
56  public AbstractPlotInstances() {
57    initialize();
58  }
59 
60  /**
61   * Initializes the member variables.
62   */
63  protected void initialize() {
64    m_Instances      = null;
65    m_PlotInstances  = null;
66    m_FinishUpCalled = false;
67  }
68
69  /**
70   * Returns an enumeration of all the available options.
71   *
72   * @return            an enumeration of all available options.
73   */
74  public Enumeration listOptions() {
75    return new Vector().elements();
76  }
77
78  /**
79   * Sets the OptionHandler's options using the given list. All options
80   * will be set (or reset) during this call (i.e. incremental setting
81   * of options is not possible).
82   *
83   * @param options     the list of options as an array of strings
84   * @throws Exception  if an option is not supported
85   */
86  public void setOptions(String[] options) throws Exception {
87  }
88
89  /**
90   * Gets the current option settings for the OptionHandler.
91   *
92   * @return the list of current option settings as an array of strings
93   */
94  public String[] getOptions() {
95    return new String[0];
96  }
97 
98  /**
99   * Sets up the structure for the plot instances.
100   */
101  protected abstract void determineFormat(); 
102 
103  /**
104   * Sets the instances that are the basis for the plot instances.
105   *
106   * @param value       the training data to initialize with
107   */
108  public void setInstances(Instances value) {
109    m_Instances = value;
110  }
111 
112  /**
113   * Returns the training data.
114   *
115   * @return            the training data
116   */
117  public Instances getInstances() {
118    return m_Instances;
119  }
120 
121  /**
122   * Default implementation only ensures that a dataset has been set.
123   */
124  protected void check() {
125    if (m_Instances == null)
126      throw new IllegalStateException("No instances set!");
127  }
128 
129  /**
130   * Performs checks, sets up the structure for the plot instances.
131   */
132  public void setUp() {
133    m_FinishUpCalled = false;
134   
135    check();
136    determineFormat();
137  }
138 
139  /**
140   * Performs optional post-processing.
141   */
142  protected void finishUp() {
143    m_FinishUpCalled = true;
144  }
145 
146  /**
147   * Returns the generated plot instances.
148   *
149   * @return            the instances to plot
150   */
151  public Instances getPlotInstances() {
152    if (!m_FinishUpCalled)
153      finishUp();
154   
155    return m_PlotInstances;
156  }
157  /**
158   * Assembles and returns the plot. The relation name of the dataset gets
159   * added automatically.
160   *
161   * @param name        the name of the plot
162   * @return            the plot
163   * @throws Exception  if plot generation fails
164   */
165  protected abstract PlotData2D createPlotData(String name) throws Exception;
166 
167  /**
168   * Assembles and returns the plot. The relation name of the dataset gets
169   * added automatically.
170   *
171   * @param name        the name of the plot
172   * @return            the plot
173   * @throws Exception  if plot generation fails
174   */
175  public PlotData2D getPlotData(String name) throws Exception {
176    if (!m_FinishUpCalled)
177      finishUp();
178   
179    return createPlotData(name);
180  }
181 
182  /**
183   * For freeing up memory. Plot data cannot be generated after this call!
184   */
185  public void cleanUp() {
186    m_Instances      = null;
187    m_PlotInstances  = null;
188    m_FinishUpCalled = false;
189  }
190}
Note: See TracBrowser for help on using the repository browser.