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));
}
}
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);
}
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;
}
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;
}
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++;
}
Aggregations