Search in sources :

Example 1 with NumericBinIndex

use of com.google.refine.browsing.util.NumericBinIndex in project OpenRefine by OpenRefine.

the class GetColumnsInfoCommand method getBinIndex.

private NumericBinIndex getBinIndex(Project project, Column column) {
    String expression = "value";
    String key = "numeric-bin:" + expression;
    Evaluable eval = null;
    try {
        eval = MetaParser.parse(expression);
    } catch (ParsingException e) {
    // this should never happen
    }
    NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
    if (index == null) {
        index = new NumericBinRowIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval));
        column.setPrecompute(key, index);
    }
    return index;
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable) NumericBinRowIndex(com.google.refine.browsing.util.NumericBinRowIndex) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) ParsingException(com.google.refine.expr.ParsingException) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable)

Example 2 with NumericBinIndex

use of com.google.refine.browsing.util.NumericBinIndex in project OpenRefine by OpenRefine.

the class RangeFacet method computeChoices.

@Override
public void computeChoices(Project project, FilteredRows filteredRows) {
    if (_eval != null && _errorMessage == null) {
        RowEvaluable rowEvaluable = getRowEvaluable(project);
        Column column = project.columnModel.getColumnByCellIndex(_cellIndex);
        String key = "numeric-bin:row-based:" + _expression;
        NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
        if (index == null) {
            index = new NumericBinRowIndex(project, rowEvaluable);
            column.setPrecompute(key, index);
        }
        retrieveDataFromBaseBinIndex(index);
        ExpressionNumericValueBinner binner = new ExpressionNumericValueBinner(rowEvaluable, index);
        filteredRows.accept(project, binner);
        retrieveDataFromBinner(binner);
    }
}
Also used : NumericBinRowIndex(com.google.refine.browsing.util.NumericBinRowIndex) Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) ExpressionNumericValueBinner(com.google.refine.browsing.util.ExpressionNumericValueBinner) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable) RowEvaluable(com.google.refine.browsing.util.RowEvaluable)

Example 3 with NumericBinIndex

use of com.google.refine.browsing.util.NumericBinIndex in project OpenRefine by OpenRefine.

the class RangeFacet method computeChoices.

@Override
public void computeChoices(Project project, FilteredRecords filteredRecords) {
    if (_eval != null && _errorMessage == null) {
        RowEvaluable rowEvaluable = getRowEvaluable(project);
        Column column = project.columnModel.getColumnByCellIndex(_cellIndex);
        String key = "numeric-bin:record-based:" + _expression;
        NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
        if (index == null) {
            index = new NumericBinRecordIndex(project, rowEvaluable);
            column.setPrecompute(key, index);
        }
        retrieveDataFromBaseBinIndex(index);
        ExpressionNumericValueBinner binner = new ExpressionNumericValueBinner(rowEvaluable, index);
        filteredRecords.accept(project, binner);
        retrieveDataFromBinner(binner);
    }
}
Also used : NumericBinRecordIndex(com.google.refine.browsing.util.NumericBinRecordIndex) Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) ExpressionNumericValueBinner(com.google.refine.browsing.util.ExpressionNumericValueBinner) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable) RowEvaluable(com.google.refine.browsing.util.RowEvaluable)

Example 4 with NumericBinIndex

use of com.google.refine.browsing.util.NumericBinIndex in project OpenRefine by OpenRefine.

the class GetColumnsInfoCommand method write.

private void write(Project project, Column column, JSONWriter writer) throws JSONException {
    NumericBinIndex columnIndex = getBinIndex(project, column);
    if (columnIndex != null) {
        writer.key("name");
        writer.value(column.getName());
        boolean is_numeric = columnIndex.isNumeric();
        writer.key("is_numeric");
        writer.value(is_numeric);
        writer.key("numeric_row_count");
        writer.value(columnIndex.getNumericRowCount());
        writer.key("non_numeric_row_count");
        writer.value(columnIndex.getNonNumericRowCount());
        writer.key("error_row_count");
        writer.value(columnIndex.getErrorRowCount());
        writer.key("blank_row_count");
        writer.value(columnIndex.getBlankRowCount());
        if (is_numeric) {
            writer.key("min");
            writer.value(columnIndex.getMin());
            writer.key("max");
            writer.value(columnIndex.getMax());
            writer.key("step");
            writer.value(columnIndex.getStep());
        }
    } else {
        writer.key("error");
        writer.value("error finding numeric information on the '" + column.getName() + "' column");
    }
}
Also used : NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex)

Example 5 with NumericBinIndex

use of com.google.refine.browsing.util.NumericBinIndex in project OpenRefine by OpenRefine.

the class GetScatterplotCommand method draw.

public void draw(OutputStream output, Project project, Engine engine, JSONObject o) throws IOException, JSONException {
    double min_x = 0;
    double min_y = 0;
    double max_x = 0;
    double max_y = 0;
    int columnIndex_x = 0;
    int columnIndex_y = 0;
    Evaluable eval_x = null;
    Evaluable eval_y = null;
    int size = (o.has(ScatterplotFacet.SIZE)) ? o.getInt(ScatterplotFacet.SIZE) : 100;
    double dot = (o.has(ScatterplotFacet.DOT)) ? o.getDouble(ScatterplotFacet.DOT) : 100;
    int dim_x = (o.has(ScatterplotFacet.DIM_X)) ? ScatterplotFacet.getAxisDim(o.getString(ScatterplotFacet.DIM_X)) : ScatterplotFacet.LIN;
    int dim_y = (o.has(ScatterplotFacet.DIM_Y)) ? ScatterplotFacet.getAxisDim(o.getString(ScatterplotFacet.DIM_Y)) : ScatterplotFacet.LIN;
    int rotation = (o.has(ScatterplotFacet.ROTATION)) ? ScatterplotFacet.getRotation(o.getString(ScatterplotFacet.ROTATION)) : ScatterplotFacet.NO_ROTATION;
    String color_str = (o.has(ScatterplotFacet.COLOR)) ? o.getString(ScatterplotFacet.COLOR) : "000000";
    Color color = new Color(Integer.parseInt(color_str, 16));
    String base_color_str = (o.has(ScatterplotFacet.BASE_COLOR)) ? o.getString(ScatterplotFacet.BASE_COLOR) : null;
    Color base_color = base_color_str != null ? new Color(Integer.parseInt(base_color_str, 16)) : null;
    String columnName_x = o.getString(ScatterplotFacet.X_COLUMN_NAME);
    String expression_x = (o.has(ScatterplotFacet.X_EXPRESSION)) ? o.getString(ScatterplotFacet.X_EXPRESSION) : "value";
    if (columnName_x.length() > 0) {
        Column x_column = project.columnModel.getColumnByName(columnName_x);
        if (x_column != null) {
            columnIndex_x = x_column.getCellIndex();
        }
    } else {
        columnIndex_x = -1;
    }
    try {
        eval_x = MetaParser.parse(expression_x);
    } catch (ParsingException e) {
        logger.warn("error parsing expression", e);
    }
    String columnName_y = o.getString(ScatterplotFacet.Y_COLUMN_NAME);
    String expression_y = (o.has(ScatterplotFacet.Y_EXPRESSION)) ? o.getString(ScatterplotFacet.Y_EXPRESSION) : "value";
    if (columnName_y.length() > 0) {
        Column y_column = project.columnModel.getColumnByName(columnName_y);
        if (y_column != null) {
            columnIndex_y = y_column.getCellIndex();
        }
    } else {
        columnIndex_y = -1;
    }
    try {
        eval_y = MetaParser.parse(expression_y);
    } catch (ParsingException e) {
        logger.warn("error parsing expression", e);
    }
    NumericBinIndex index_x = null;
    NumericBinIndex index_y = null;
    String col_x_name = o.getString(ScatterplotFacet.X_COLUMN_NAME);
    Column column_x = project.columnModel.getColumnByName(col_x_name);
    if (column_x != null) {
        columnIndex_x = column_x.getCellIndex();
        index_x = ScatterplotFacet.getBinIndex(project, column_x, eval_x, expression_x);
        min_x = index_x.getMin();
        max_x = index_x.getMax();
    }
    String col_y_name = o.getString(ScatterplotFacet.Y_COLUMN_NAME);
    Column column_y = project.columnModel.getColumnByName(col_y_name);
    if (column_y != null) {
        columnIndex_y = column_y.getCellIndex();
        index_y = ScatterplotFacet.getBinIndex(project, column_y, eval_y, expression_y);
        min_y = index_y.getMin();
        max_y = index_y.getMax();
    }
    if (index_x != null && index_y != null && index_x.isNumeric() && index_y.isNumeric()) {
        ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor(columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y, size, dim_x, dim_y, rotation, dot, color);
        if (base_color != null) {
            drawer.setColor(base_color);
            FilteredRows filteredRows = engine.getAllRows();
            filteredRows.accept(project, drawer);
            drawer.setColor(color);
        }
        {
            FilteredRows filteredRows = engine.getAllFilteredRows();
            filteredRows.accept(project, drawer);
        }
        ImageIO.write(drawer.getImage(), "png", output);
    } else {
        ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR), "png", output);
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) Color(java.awt.Color) ParsingException(com.google.refine.expr.ParsingException) ScatterplotDrawingRowVisitor(com.google.refine.browsing.facets.ScatterplotDrawingRowVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) BufferedImage(java.awt.image.BufferedImage)

Aggregations

NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)9 Column (com.google.refine.model.Column)6 ExpressionBasedRowEvaluable (com.google.refine.browsing.util.ExpressionBasedRowEvaluable)4 ParsingException (com.google.refine.expr.ParsingException)4 NumericBinRowIndex (com.google.refine.browsing.util.NumericBinRowIndex)3 ExpressionNumericValueBinner (com.google.refine.browsing.util.ExpressionNumericValueBinner)2 NumericBinRecordIndex (com.google.refine.browsing.util.NumericBinRecordIndex)2 RowEvaluable (com.google.refine.browsing.util.RowEvaluable)2 Evaluable (com.google.refine.expr.Evaluable)2 Color (java.awt.Color)2 IOException (java.io.IOException)2 FilteredRows (com.google.refine.browsing.FilteredRows)1 ScatterplotDrawingRowVisitor (com.google.refine.browsing.facets.ScatterplotDrawingRowVisitor)1 BufferedImage (java.awt.image.BufferedImage)1