Search in sources :

Example 6 with NumericBinIndex

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

the class ScatterplotFacet method initializeFromJSON.

@Override
public void initializeFromJSON(Project project, JSONObject o) throws JSONException {
    name = o.getString(NAME);
    l = size = (o.has(SIZE)) ? o.getInt(SIZE) : 100;
    dot = (o.has(DOT)) ? o.getInt(DOT) : 0.5d;
    dim_x = (o.has(DIM_X)) ? getAxisDim(o.getString(DIM_X)) : LIN;
    if (o.has(FROM_X) && o.has(TO_X)) {
        from_x = o.getDouble(FROM_X);
        to_x = o.getDouble(TO_X);
        selected = true;
    } else {
        from_x = 0;
        to_x = 1;
    }
    dim_y = (o.has(DIM_Y)) ? getAxisDim(o.getString(DIM_Y)) : LIN;
    if (o.has(FROM_Y) && o.has(TO_Y)) {
        from_y = o.getDouble(FROM_Y);
        to_y = o.getDouble(TO_Y);
        selected = true;
    } else {
        from_y = 0;
        to_y = 1;
    }
    rotation = (o.has(ROTATION)) ? getRotation(o.getString(ROTATION)) : NO_ROTATION;
    t = createRotationMatrix(rotation, l);
    color_str = (o.has(COLOR)) ? o.getString(COLOR) : "000000";
    color = new Color(Integer.parseInt(color_str, 16));
    columnName_x = o.getString(X_COLUMN_NAME);
    expression_x = o.getString(X_EXPRESSION);
    if (columnName_x.length() > 0) {
        Column x_column = project.columnModel.getColumnByName(columnName_x);
        if (x_column != null) {
            columnIndex_x = x_column.getCellIndex();
            NumericBinIndex index_x = ScatterplotFacet.getBinIndex(project, x_column, eval_x, expression_x);
            min_x = index_x.getMin();
            max_x = index_x.getMax();
        } else {
            errorMessage_x = "No column named " + columnName_x;
        }
    } else {
        columnIndex_x = -1;
    }
    try {
        eval_x = MetaParser.parse(expression_x);
    } catch (ParsingException e) {
        errorMessage_x = e.getMessage();
    }
    columnName_y = o.getString(Y_COLUMN_NAME);
    expression_y = o.getString(Y_EXPRESSION);
    if (columnName_y.length() > 0) {
        Column y_column = project.columnModel.getColumnByName(columnName_y);
        if (y_column != null) {
            columnIndex_y = y_column.getCellIndex();
            NumericBinIndex index_y = ScatterplotFacet.getBinIndex(project, y_column, eval_y, expression_y);
            min_y = index_y.getMin();
            max_y = index_y.getMax();
        } else {
            errorMessage_y = "No column named " + columnName_y;
        }
    } else {
        columnIndex_y = -1;
    }
    try {
        eval_y = MetaParser.parse(expression_y);
    } catch (ParsingException e) {
        errorMessage_y = e.getMessage();
    }
}
Also used : Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) Color(java.awt.Color) ParsingException(com.google.refine.expr.ParsingException)

Example 7 with NumericBinIndex

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

the class ScatterplotFacet method computeChoices.

@Override
public void computeChoices(Project project, FilteredRows filteredRows) {
    if (eval_x != null && eval_y != null && errorMessage_x == null && errorMessage_y == null) {
        Column column_x = project.columnModel.getColumnByCellIndex(columnIndex_x);
        NumericBinIndex index_x = getBinIndex(project, column_x, eval_x, expression_x, "row-based");
        Column column_y = project.columnModel.getColumnByCellIndex(columnIndex_y);
        NumericBinIndex index_y = getBinIndex(project, column_y, eval_y, expression_y, "row-based");
        retrieveDataFromBinIndices(index_x, index_y);
        if (IMAGE_URI) {
            if (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);
                filteredRows.accept(project, drawer);
                try {
                    image = serializeImage(drawer.getImage());
                } catch (IOException e) {
                    logger.warn("Exception caught while generating the image", e);
                }
            } else {
                image = EMPTY_IMAGE;
            }
        }
    }
}
Also used : Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) IOException(java.io.IOException)

Example 8 with NumericBinIndex

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

the class ScatterplotFacet method getBinIndex.

public static NumericBinIndex getBinIndex(Project project, Column column, Evaluable eval, String expression, String mode) {
    String key = "numeric-bin:" + mode + ":" + expression;
    if (eval == null) {
        try {
            eval = MetaParser.parse(expression);
        } catch (ParsingException e) {
            logger.warn("Error parsing expression", e);
        }
    }
    NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
    if (index == null) {
        index = "row-based".equals(mode) ? new NumericBinRowIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval)) : new NumericBinRecordIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval));
        column.setPrecompute(key, index);
    }
    return index;
}
Also used : NumericBinRecordIndex(com.google.refine.browsing.util.NumericBinRecordIndex) 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 9 with NumericBinIndex

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

the class ScatterplotFacet method computeChoices.

@Override
public void computeChoices(Project project, FilteredRecords filteredRecords) {
    if (eval_x != null && eval_y != null && errorMessage_x == null && errorMessage_y == null) {
        Column column_x = project.columnModel.getColumnByCellIndex(columnIndex_x);
        NumericBinIndex index_x = getBinIndex(project, column_x, eval_x, expression_x, "record-based");
        Column column_y = project.columnModel.getColumnByCellIndex(columnIndex_y);
        NumericBinIndex index_y = getBinIndex(project, column_y, eval_y, expression_y, "record-based");
        retrieveDataFromBinIndices(index_x, index_y);
        if (IMAGE_URI) {
            if (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);
                filteredRecords.accept(project, drawer);
                try {
                    image = serializeImage(drawer.getImage());
                } catch (IOException e) {
                    logger.warn("Exception caught while generating the image", e);
                }
            } else {
                image = EMPTY_IMAGE;
            }
        }
    }
}
Also used : Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) IOException(java.io.IOException)

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