Search in sources :

Example 76 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class LinePlotter method calculateDots.

/**
 * Calculates the screen coordinates (dots) for the lines and puts them in a
 * large {@link org.knime.base.node.viz.plotter.scatter.DotInfoArray}, which
 * is passed to the
 * {@link org.knime.base.node.viz.plotter.line.LinePlotterDrawingPane}.
 */
protected void calculateDots() {
    if (!(getDrawingPane() instanceof ScatterPlotterDrawingPane)) {
        return;
    }
    if (m_columnNames == null) {
        return;
    }
    if (getDataProvider() != null && getDataProvider().getDataArray(getDataArrayIdx()) != null) {
        DataArray array = getDataProvider().getDataArray(getDataArrayIdx());
        int nrOfRows = array.size();
        // set the empty dots to delete the old ones
        // if we have no columns to display
        ((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(new DotInfo[0]));
        // first store them in a list to avoid keep tracking of indices
        List<DotInfo> dotList = new ArrayList<DotInfo>();
        for (String col : m_columnNames) {
            int colIdx = array.getDataTableSpec().findColumnIndex(col);
            Color c = m_colorMapping.get(col);
            if (c == null) {
                c = Color.black;
            }
            ColorAttr color = ColorAttr.getInstance(c);
            // store the last point with valid value for interpolation
            Point p1 = new Point(-1, -1);
            Point p2;
            List<DotInfo> missingValues = new ArrayList<DotInfo>();
            // create the dots
            for (int row = 0; row < nrOfRows; row++) {
                DataCell cell = array.getRow(row).getCell(colIdx);
                int y = -1;
                DotInfo dot;
                int x = getMappedXValue(new StringCell(array.getRow(row).getKey().getString()));
                if (!cell.isMissing()) {
                    y = getMappedYValue(cell);
                    if (missingValues.size() > 0) {
                        // we have some missing values in between,
                        // thus we have to interpolate
                        p2 = new Point(x, y);
                        DotInfo[] interpolated = interpolate(p1, p2, missingValues);
                        // and add them
                        for (DotInfo p : interpolated) {
                            dotList.add(p);
                        }
                        // and clear the list again
                        missingValues.clear();
                    }
                    p1 = new Point(x, y);
                    dot = new DotInfo(x, y, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    dot.setXDomainValue(new StringCell(array.getRow(row).getKey().getString()));
                    dot.setYDomainValue(cell);
                    dotList.add(dot);
                } else if (!m_interpolate) {
                    // LOGGER.debug("missing value");
                    dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    dotList.add(dot);
                } else {
                    // interpolate
                    dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    missingValues.add(dot);
                }
            }
            // un-interpolated at the end, we add them anyway
            if (!missingValues.isEmpty()) {
                DotInfo[] interpolated = interpolate(p1, null, missingValues);
                // and add them
                for (DotInfo p : interpolated) {
                    dotList.add(p);
                }
                // and clear the list again
                missingValues.clear();
            }
        }
        DotInfo[] dots = new DotInfo[dotList.size()];
        dotList.toArray(dots);
        ((LinePlotterDrawingPane) getDrawingPane()).setNumberOfLines(nrOfRows);
        ((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(dots));
    }
}
Also used : DotInfo(org.knime.base.node.viz.plotter.scatter.DotInfo) Color(java.awt.Color) ArrayList(java.util.ArrayList) ColorAttr(org.knime.core.data.property.ColorAttr) Point(java.awt.Point) ScatterPlotterDrawingPane(org.knime.base.node.viz.plotter.scatter.ScatterPlotterDrawingPane) DataArray(org.knime.base.node.util.DataArray) Point(java.awt.Point) StringCell(org.knime.core.data.def.StringCell) DotInfoArray(org.knime.base.node.viz.plotter.scatter.DotInfoArray) DataCell(org.knime.core.data.DataCell)

Example 77 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class LinePlotter method calculateCoordinates.

/**
 * Determines the overall minimum and maximum value of all selected columns.
 *
 * @param array
 *            the data to visualize
 */
private void calculateCoordinates(final DataArray array) {
    Set<DataCell> rowKeys = new LinkedHashSet<DataCell>(array.size());
    double minY = Double.POSITIVE_INFINITY;
    double maxY = Double.NEGATIVE_INFINITY;
    for (DataRow row : array) {
        rowKeys.add(new StringCell(row.getKey().getString()));
        for (String column : m_columnNames) {
            int colIdx = array.getDataTableSpec().findColumnIndex(column);
            if (colIdx == -1) {
                initColumnNames(array);
                calculateCoordinates(array);
                break;
            }
            DataCell cell = row.getCell(colIdx);
            if (cell.isMissing()) {
                continue;
            }
            double value = ((DoubleValue) cell).getDoubleValue();
            minY = Math.min(minY, value);
            maxY = Math.max(maxY, value);
        }
    }
    createNominalXCoordinate(rowKeys);
    setPreserve(false);
    createYCoordinate(minY, maxY);
// setPreserve(true);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringCell(org.knime.core.data.def.StringCell) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) Point(java.awt.Point)

Example 78 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class RegexSplitNodeModel method createColumnRearranger.

@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals internals) throws InvalidSettingsException {
    AtomicInteger errorCounter = new AtomicInteger();
    if (m_settings == null) {
        throw new InvalidSettingsException("Not configuration available.");
    }
    final int colIndex = spec.findColumnIndex(m_settings.getColumn());
    if (colIndex < 0) {
        throw new InvalidSettingsException("No such column in input table: " + m_settings.getColumn());
    }
    DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    if (!colSpec.getType().isCompatible(StringValue.class)) {
        throw new InvalidSettingsException("Selected column does not " + "contain strings");
    }
    final Pattern p = m_settings.compile();
    int count = 0;
    String patternS = p.pattern();
    boolean isNextSpecial = false;
    boolean isPreviousAParenthesis = false;
    // escaped parentheses "\(" or non-capturing groups "(?"
    for (int i = 0; i < patternS.length(); i++) {
        switch(patternS.charAt(i)) {
            case '\\':
                isNextSpecial = !isNextSpecial;
                isPreviousAParenthesis = false;
                break;
            case '(':
                count += isNextSpecial ? 0 : 1;
                isPreviousAParenthesis = !isNextSpecial;
                isNextSpecial = false;
                break;
            case '?':
                if (isPreviousAParenthesis) {
                    count -= 1;
                }
            // no break;
            default:
                isNextSpecial = false;
                isPreviousAParenthesis = false;
        }
    }
    final int newColCount = count;
    final DataColumnSpec[] newColSpecs = new DataColumnSpec[count];
    for (int i = 0; i < newColCount; i++) {
        String name = DataTableSpec.getUniqueColumnName(spec, "split_" + i);
        newColSpecs[i] = new DataColumnSpecCreator(name, StringCell.TYPE).createSpec();
    }
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    rearranger.append(new AbstractCellFactory(newColSpecs) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell[] getCells(final DataRow row) {
            DataCell[] result = new DataCell[newColCount];
            Arrays.fill(result, DataType.getMissingCell());
            DataCell c = row.getCell(colIndex);
            if (c.isMissing()) {
                return result;
            }
            String s = ((StringValue) c).getStringValue();
            Matcher m = p.matcher(s);
            if (m.matches()) {
                int max = m.groupCount();
                if (m.groupCount() > newColCount) {
                    errorCounter.incrementAndGet();
                    max = newColCount;
                }
                for (int i = 0; i < max; i++) {
                    // group(0) will return the entire string and is not
                    // included in groupCount, see Matcher API for details
                    String str = m.group(i + 1);
                    if (str != null) {
                        // null for optional groups "(...)?"
                        result[i] = new StringCell(str);
                    }
                }
                return result;
            } else {
                errorCounter.incrementAndGet();
                return result;
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void afterProcessing() {
            // propagate error count
            internals.getConfig().addInt(CONFIG_KEY_ERRORCOUNT, errorCounter.get());
        }
    });
    return rearranger;
}
Also used : Pattern(java.util.regex.Pattern) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Matcher(java.util.regex.Matcher) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue)

Example 79 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class RoundDoubleCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    DataCell[] newCells = new DataCell[m_colIndexToRound.length];
    int noCols = row.getNumCells();
    int nextIndexToRound = 0;
    int currIndexToRound = -1;
    // walk through all columns and round if specified
    for (int i = 0; i < noCols; i++) {
        // are available).
        if (nextIndexToRound < m_colIndexToRound.length) {
            currIndexToRound = m_colIndexToRound[nextIndexToRound];
        }
        // if value needs to be rounded
        if (i == currIndexToRound) {
            final DataCell outCell;
            if (row.getCell(i).isMissing()) {
                outCell = DataType.getMissingCell();
            } else {
                double value = ((DoubleValue) row.getCell(i)).getDoubleValue();
                // check for infinity or nan
                if (Double.isInfinite(value) || Double.isNaN(value)) {
                    switch(m_outputType) {
                        case Double:
                            // this isn't nice as we shouldn't have NaN and Inf in the input ...
                            // but that's a problem somewhere else
                            outCell = new DoubleCell(value);
                            break;
                        default:
                            outCell = new StringCell(new Double(value).toString());
                    }
                } else {
                    // do not use constructor, see AP-7016
                    BigDecimal bd = BigDecimal.valueOf(value).stripTrailingZeros();
                    switch(m_numberMode) {
                        case DECIMAL_PLACES:
                            bd = bd.setScale(m_precision, m_roundingMode);
                            break;
                        case SIGNIFICANT_FIGURES:
                            bd = bd.round(new MathContext(m_precision, m_roundingMode));
                            break;
                        default:
                            throw new IllegalStateException();
                    }
                    outCell = m_outputType.createCell(bd);
                }
            }
            // increment index of included column indices
            newCells[nextIndexToRound++] = outCell;
        }
    }
    return newCells;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 80 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class ListFiles method addLocationToContainer.

/**
 * Adds a File to the table.
 *
 * @param file
 */
private void addLocationToContainer(final URL url) throws UnsupportedEncodingException, URISyntaxException {
    DataCell[] row = new DataCell[2];
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        row[0] = new StringCell(Paths.get(url.toURI()).toString());
    } else {
        row[0] = new MissingCell("URL is remote and does not have a local location");
    }
    row[1] = new StringCell(url.toString());
    m_dc.addRowToTable(new DefaultRow(RowKey.createRowKey(m_currentRowID), row));
    m_currentRowID++;
}
Also used : StringCell(org.knime.core.data.def.StringCell) MissingCell(org.knime.core.data.MissingCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

StringCell (org.knime.core.data.def.StringCell)176 DataCell (org.knime.core.data.DataCell)130 DoubleCell (org.knime.core.data.def.DoubleCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)65 IntCell (org.knime.core.data.def.IntCell)55 DataRow (org.knime.core.data.DataRow)52 DataTableSpec (org.knime.core.data.DataTableSpec)49 ArrayList (java.util.ArrayList)41 DataColumnSpec (org.knime.core.data.DataColumnSpec)37 RowKey (org.knime.core.data.RowKey)36 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)26 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)26 DataType (org.knime.core.data.DataType)22 LinkedHashSet (java.util.LinkedHashSet)21 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)16 LinkedHashMap (java.util.LinkedHashMap)15 Test (org.junit.Test)15 HashMap (java.util.HashMap)11