net.sourceforge.openforecast.input
Class CSVBuilder

java.lang.Object
  extended by net.sourceforge.openforecast.input.AbstractBuilder
      extended by net.sourceforge.openforecast.input.CSVBuilder
All Implemented Interfaces:
Builder

public class CSVBuilder
extends AbstractBuilder

Defines a Builder that can be used to construct a DataSet from CSV (Comma Separated Values) file or input stream. This class makes for a quick and easy "import" of data from a variety of other applications such as Microsoft Excel.

The last value on each row is assumed to represent the dependent variable. For example, if the independent variables are represented by x1, x2, x3 and so on, and the dependent variable is represented by y, then a row should be of the form:

  x1, x2, ..., xi, y
 

For example, the following represents data points (1,3), (2,5), (3,6), and (4,7):

  1, 3
  2, 5
  3, 6
  4, 7
 

where the values 3, 5, 6 and 7 are the observed values of the dependent variable corresponding to the associated values of the independent variables with the values 1, 2, 3, and 4 respectively. By default, the independent variables - just one in this example - would be named "x1", ..., "xi", etc. To override this behavior, you can specify a "header row" containing names for the individual variables (the "columns" of data).

Using the previous example, if the xi represented time, we could define the data input as follows:

  time, observation
  1, 3
  2, 5
  3, 6
  4, 7
 

This would name the dependent variable in this case, "time", instead of the default, "x1".

Since:
0.4
Author:
Steven R. Gould

Constructor Summary
CSVBuilder(File file)
          Constructs a new CSVBuilder that reads its input from the named file.
CSVBuilder(File file, boolean hasHeaderRow)
          Constructs a new CSVBuilder that reads its input from the named file.
CSVBuilder(FileReader reader)
          Constructs a new CSVBuilder that reads its input from the named file input stream.
CSVBuilder(FileReader reader, boolean hasHeaderRow)
          Constructs a new CSVBuilder that reads its input from the named file input stream.
CSVBuilder(String filename)
          Constructs a new CSVBuilder that reads its input from the named file.
CSVBuilder(String filename, boolean hasHeaderRow)
          Constructs a new CSVBuilder that reads its input from the named file, and treats the first row of data as a header row containing field names.
 
Method Summary
 DataSet build()
          Retrieves a DataSet - a collection of DataPoints - from the current input source.
 
Methods inherited from class net.sourceforge.openforecast.input.AbstractBuilder
addVariable, getNumberOfVariables, getVariableName, setNumberOfVariables, setVariableName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVBuilder

public CSVBuilder(String filename)
           throws FileNotFoundException
Constructs a new CSVBuilder that reads its input from the named file. The fields will be named "x1", "x2", "x3", etc. unless the first non-comment row contains one or more non-numeric fields in which case the first row will be assumed to be a header row.

Parameters:
filename - the name of the CSV file to read the input from.
Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file.

CSVBuilder

public CSVBuilder(String filename,
                  boolean hasHeaderRow)
           throws FileNotFoundException
Constructs a new CSVBuilder that reads its input from the named file, and treats the first row of data as a header row containing field names.

Parameters:
filename - the name of the CSV file to read the input from.
hasHeaderRow - set to true if the CSV file has a header row.
Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file.

CSVBuilder

public CSVBuilder(File file)
           throws FileNotFoundException
Constructs a new CSVBuilder that reads its input from the named file. The fields will be named "x1", "x2", "x3", etc. unless the first non-comment row contains one or more non-numeric fields in which case the first row will be assumed to be a header row.

Parameters:
file - the File object specifying the CSV file to read the input from.
Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file.

CSVBuilder

public CSVBuilder(File file,
                  boolean hasHeaderRow)
           throws FileNotFoundException
Constructs a new CSVBuilder that reads its input from the named file. The fields will be named "x1", "x2", "x3", etc.

Parameters:
file - the File object specifying the CSV file to read the input from.
hasHeaderRow - set to true if the CSV file has a header row.
Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file.

CSVBuilder

public CSVBuilder(FileReader reader)
Constructs a new CSVBuilder that reads its input from the named file input stream. The fields will be named "x1", "x2", "x3", etc. unless the first non-comment row contains one or more non-numeric fields in which case the first row will be assumed to be a header row.

Parameters:
reader - the FileReader object specifying the CSV file reader to read the input from.

CSVBuilder

public CSVBuilder(FileReader reader,
                  boolean hasHeaderRow)
Constructs a new CSVBuilder that reads its input from the named file input stream. The fields will be named "x1", "x2", "x3", etc.

Parameters:
reader - the FileReader object specifying the CSV file reader to read the input from.
hasHeaderRow - set to true if the CSV file input stream has a header row.
Method Detail

build

public DataSet build()
              throws IOException
Retrieves a DataSet - a collection of DataPoints - from the current input source. The DataSet should contain all DataPoints defined by the input source.

In general, build will attempt to convert all lines/rows in the CSV input to data points. The exceptions are as follows:

Returns:
a DataSet built from the current input source.
Throws:
IOException - if an error occurred reading from the CSV file.


OpenForecast, Copyright (c) Steven Gould, 2002-2011