Class CSVBuilder
- java.lang.Object
-
- net.sourceforge.openforecast.input.AbstractBuilder
-
- 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
-
-
Constructor Summary
Constructors Constructor and Description CSVBuilder(java.io.File file)Constructs a new CSVBuilder that reads its input from the named file.CSVBuilder(java.io.File file, boolean hasHeaderRow)Constructs a new CSVBuilder that reads its input from the named file.CSVBuilder(java.io.FileReader reader)Constructs a new CSVBuilder that reads its input from the named file input stream.CSVBuilder(java.io.FileReader reader, boolean hasHeaderRow)Constructs a new CSVBuilder that reads its input from the named file input stream.CSVBuilder(java.lang.String filename)Constructs a new CSVBuilder that reads its input from the named file.CSVBuilder(java.lang.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
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description DataSetbuild()Retrieves a DataSet - a collection of DataPoints - from the current input source.
-
-
-
Constructor Detail
-
CSVBuilder
public CSVBuilder(java.lang.String filename) throws java.io.FileNotFoundExceptionConstructs 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:
java.io.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.java.lang.SecurityException- if a security manager exists and itscheckReadmethod denies read access to the file.
-
CSVBuilder
public CSVBuilder(java.lang.String filename, boolean hasHeaderRow) throws java.io.FileNotFoundExceptionConstructs 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:
java.io.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.java.lang.SecurityException- if a security manager exists and itscheckReadmethod denies read access to the file.
-
CSVBuilder
public CSVBuilder(java.io.File file) throws java.io.FileNotFoundExceptionConstructs 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:
java.io.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.java.lang.SecurityException- if a security manager exists and itscheckReadmethod denies read access to the file.
-
CSVBuilder
public CSVBuilder(java.io.File file, boolean hasHeaderRow) throws java.io.FileNotFoundExceptionConstructs 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:
java.io.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.java.lang.SecurityException- if a security manager exists and itscheckReadmethod denies read access to the file.
-
CSVBuilder
public CSVBuilder(java.io.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(java.io.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 java.io.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:
- Blank lines (lines containing only whitespace) will be ignored, and can be used for spacing in the input.
- Lines beginning with a '#' will be treated as comments, and will be ignored.
- If a header row is included - as specified in one of the constructors - then it will be treated as containing field/variable names for use by the DataSet.
- Returns:
- a DataSet built from the current input source.
- Throws:
java.io.IOException- if an error occurred reading from the CSV file.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG