Example usage for com.google.common.collect ImmutableTable column

List of usage examples for com.google.common.collect ImmutableTable column

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableTable column.

Prototype

@Override
public ImmutableMap<R, V> column(C columnKey) 

Source Link

Usage

From source file:org.opennms.netmgt.jasper.analytics.TrendLine.java

@Override
public void filter(RowSortedTable<Integer, String, Double> table) throws RScriptException {
    Preconditions.checkArgument(table.containsColumn("Timestamp"),
            "Data source must have a 'Timestamp' column.");

    // Determine the index of the first and last non-NaN values
    // Assume the values between these are contiguous
    Point rowsWithValues = DataSourceUtils.getRowsWithValues(table, m_config.getInputColumn());
    int firstRowWithValues = rowsWithValues.x;
    int lastRowWithValues = rowsWithValues.y;

    // Make sure we have some samples
    int numSampleRows = lastRowWithValues - firstRowWithValues;
    if (numSampleRows < 1) {
        LOG.error("Insufficent values in column for trending. Excluding trend from data source.");
        return;/*from  w  ww. ja v  a 2  s  .c o  m*/
    }

    // Determine the step size
    Date lastTimestamp = new Date(table.get(lastRowWithValues, "Timestamp").longValue());
    long stepInMs = (long) (table.get(lastRowWithValues, "Timestamp")
            - table.get(lastRowWithValues - 1, "Timestamp"));

    // Num steps ahead
    int numStepsAhead = (int) Math.floor(m_config.getSecondsAhead() * 1000 / stepInMs);
    numStepsAhead = Math.max(1, numStepsAhead);

    // Script arguments
    Map<String, Object> arguments = Maps.newHashMap();
    arguments.put("inputColumn", m_config.getInputColumn());
    arguments.put("polynomialOrder", m_config.getPolynomialOrder());
    // Array indices in R start at 1
    arguments.put("firstIndex", firstRowWithValues + 1);
    arguments.put("lastIndex", lastRowWithValues + 1);

    // Calculate the trend line/curve
    RScriptExecutor executor = new RScriptExecutor();
    RScriptOutput output = executor.exec(PATH_TO_R_SCRIPT, new RScriptInput(table, arguments));
    ImmutableTable<Integer, String, Double> outputTable = output.getTable();

    // Convert the result to a polynomial
    Polynomial poly = new Polynomial(outputTable.column("x").values().toArray(new Double[0]));

    // Calculate the value of the polynomial for all of the samples
    // and the requested number of steps ahead
    for (int i = firstRowWithValues; i <= (lastRowWithValues + numStepsAhead); i++) {
        if (i >= lastRowWithValues) {
            table.put(i, "Timestamp",
                    (double) new Date(lastTimestamp.getTime() + stepInMs * (i - lastRowWithValues)).getTime());
        }
        double x = table.get(i, "Timestamp");
        table.put(i, m_config.getOutputColumn(), poly.eval(x));
    }
}