Search in sources :

Example 6 with NumericAttribute

use of smile.data.NumericAttribute in project smile by haifengl.

the class RESParser method parse.

/**
     * Parse a RES dataset from an input stream.
     * @param name the name of dataset.
     * @param stream the input stream of data.
     * @throws java.io.IOException
     */
public AttributeDataset parse(String name, InputStream stream) throws IOException, ParseException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String line = reader.readLine();
    if (line == null) {
        throw new IOException("Empty data source.");
    }
    String[] tokens = line.split("\t", -1);
    int p = (tokens.length - 2) / 2;
    line = reader.readLine();
    if (line == null) {
        throw new IOException("Premature end of file.");
    }
    String[] samples = line.split("\t", -1);
    if (samples.length != tokens.length - 1) {
        throw new IOException("Invalid sample description header.");
    }
    Attribute[] attributes = new Attribute[p];
    for (int i = 0; i < p; i++) {
        attributes[i] = new NumericAttribute(tokens[2 * i + 2], samples[2 * i + 1]);
    }
    line = reader.readLine();
    if (line == null) {
        throw new IOException("Premature end of file.");
    }
    int n = Integer.parseInt(line);
    if (n <= 0) {
        throw new IOException("Invalid number of rows: " + n);
    }
    AttributeDataset data = new AttributeDataset(name, attributes);
    for (int i = 0; i < n; i++) {
        line = reader.readLine();
        if (line == null) {
            throw new IOException("Premature end of file.");
        }
        tokens = line.split("\t", -1);
        if (tokens.length != samples.length + 1) {
            throw new IOException(String.format("Invalid number of elements of line %d: %d", i + 4, tokens.length));
        }
        double[] x = new double[p];
        for (int j = 0; j < p; j++) {
            x[j] = Double.valueOf(tokens[2 * j + 2]);
        }
        Datum<double[]> datum = new Datum<>(x);
        datum.name = tokens[1];
        datum.description = tokens[0];
        data.add(datum);
    }
    reader.close();
    return data;
}
Also used : AttributeDataset(smile.data.AttributeDataset) Datum(smile.data.Datum) InputStreamReader(java.io.InputStreamReader) Attribute(smile.data.Attribute) NumericAttribute(smile.data.NumericAttribute) IOException(java.io.IOException) NumericAttribute(smile.data.NumericAttribute) BufferedReader(java.io.BufferedReader)

Aggregations

NumericAttribute (smile.data.NumericAttribute)6 IOException (java.io.IOException)5 Attribute (smile.data.Attribute)5 AttributeDataset (smile.data.AttributeDataset)5 Datum (smile.data.Datum)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)1 DateAttribute (smile.data.DateAttribute)1 NominalAttribute (smile.data.NominalAttribute)1 StringAttribute (smile.data.StringAttribute)1