use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class CAIMDiscretizationNodeModel method createResultTable.
/**
* Creates {@link BufferedDataTable} from a given input table and an
* appropriate {@link DiscretizationScheme}. The result table has replaced
* columns according to the {@link DiscretizationScheme}.
*
* @param exec the context from which to create the
* {@link BufferedDataTable}
* @param table the input data table
* @param discretizationModel the {@link DiscretizationModel} that contains
* the mapping from numerical intervals to nominal String values
* for the included columns
* @return the discretized input data
*/
public static BufferedDataTable createResultTable(final ExecutionContext exec, final BufferedDataTable table, final DiscretizationModel discretizationModel) {
DiscretizationScheme[] dSchemes = discretizationModel.getSchemes();
final String[] includedColumnNames = discretizationModel.getIncludedColumnNames();
// filter the schemes so that only schemes for columns are included
// which are also included in the table
dSchemes = filterNotKnownSchemes(dSchemes, includedColumnNames, table.getDataTableSpec());
DataTableSpec originalTableSpec = table.getDataTableSpec();
DataColumnSpec[] newColumnSpecs = new DataColumnSpec[originalTableSpec.getNumColumns()];
// remembers if an column index is included or not
boolean[] included = new boolean[newColumnSpecs.length];
int counter = 0;
for (DataColumnSpec originalColumnSpec : originalTableSpec) {
// if the column is included for discretizing, change the spec
if (isIncluded(originalColumnSpec, includedColumnNames) > -1) {
// creat a nominal string column spec
newColumnSpecs[counter] = new DataColumnSpecCreator(originalColumnSpec.getName(), StringCell.TYPE).createSpec();
included[counter] = true;
} else {
// add it as is
newColumnSpecs[counter] = originalColumnSpec;
included[counter] = false;
}
counter++;
}
// create the new table spec
DataTableSpec newTableSpec = new DataTableSpec(newColumnSpecs);
// create the result table
BufferedDataContainer container = exec.createDataContainer(newTableSpec);
// discretize the included column values
double rowCounter = 0;
double numRows = table.size();
for (DataRow row : table) {
if (rowCounter % 200 == 0) {
exec.setProgress(rowCounter / numRows);
}
int i = 0;
DataCell[] newCells = new DataCell[row.getNumCells()];
int includedCounter = 0;
for (DataCell cell : row) {
if (included[i]) {
// check for missing values
if (cell.isMissing()) {
newCells[i] = cell;
} else {
// transform the value to the discretized one
double value = ((DoubleValue) cell).getDoubleValue();
String discreteValue = dSchemes[includedCounter].getDiscreteValue(value);
newCells[i] = new StringCell(discreteValue);
}
includedCounter++;
} else {
newCells[i] = cell;
}
i++;
}
container.addRowToTable(new DefaultRow(row.getKey(), newCells));
rowCounter++;
}
container.close();
return container.getTable();
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class InteractiveHiLiteCollectorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
m_data = inData[0];
if (m_annotationMap.isEmpty()) {
return new PortObject[] { m_data };
}
DataTableSpec inSpec = (DataTableSpec) m_data.getSpec();
final DataColumnSpec[] cspecs = createSpecs(inSpec);
ColumnRearranger cr = new ColumnRearranger(inSpec);
cr.append(new CellFactory() {
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
if (m_annotationMap.isEmpty()) {
return new DataCell[0];
}
DataCell[] cells = new DataCell[m_lastIndex + 1];
for (int i = 0; i < cells.length; i++) {
Map<Integer, String> map = m_annotationMap.get(row.getKey());
if (map == null) {
cells[i] = DataType.getMissingCell();
} else {
String str = map.get(i);
if (str == null) {
cells[i] = DataType.getMissingCell();
} else {
cells[i] = new StringCell(str);
}
}
}
return cells;
}
@Override
public DataColumnSpec[] getColumnSpecs() {
return cspecs;
}
/**
* {@inheritDoc}
*/
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor em) {
em.setProgress((double) curRowNr / rowCount);
}
});
return new BufferedDataTable[] { exec.createColumnRearrangeTable((BufferedDataTable) m_data, cr, exec) };
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ColumnRowFilterPanel method boundsChanged.
/**
* Called when user changes the values for the lower or upper bounds.
*/
protected void boundsChanged() {
// check if the entered value somehow goes along with the selected col.
setErrMsg("");
if (m_tSpec == null) {
return;
}
if (getSelectedColumnName() == null) {
return;
}
if (!m_useRange.isSelected()) {
return;
}
DataCell lowBound = null;
DataCell hiBound = null;
try {
lowBound = getLowerBoundCell();
hiBound = getUpperBoundCell();
} catch (InvalidSettingsException ise) {
setErrMsg(ise.getMessage());
return;
}
if ((lowBound == null) && (hiBound == null)) {
setErrMsg("Specify at least one range boundary");
return;
}
if ((lowBound != null) && (hiBound != null)) {
DataValueComparator comp;
comp = DataType.getCommonSuperType(lowBound.getType(), hiBound.getType()).getComparator();
if (comp.compare(hiBound, lowBound) == -1) {
setErrMsg("The lower bound must be smaller than the" + " upper bound");
return;
}
}
if (((lowBound != null) && (lowBound instanceof StringCell)) || ((hiBound != null) && (hiBound instanceof StringCell))) {
setErrMsg("Warning: String comparison is used for " + "range checking. May not work as expected!");
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ColumnRowFilterPanel method getBoundCell.
/* method used from the above */
private DataCell getBoundCell(final JTextField editField, final String name) throws InvalidSettingsException {
if (editField.getText().length() <= 0) {
return null;
}
String colName = getSelectedColumnName();
if ((colName == null) || (colName.length() == 0)) {
throw new InvalidSettingsException("Invalid columns selection");
}
if (m_tSpec != null) {
final DataType origType = m_tSpec.getColumnSpec(colName).getType();
final DataType cType;
if (m_deepFiltering.isSelected() && origType.isCollectionType()) {
cType = origType.getCollectionElementType();
} else {
cType = origType;
}
if (cType.isCompatible(IntValue.class)) {
// first try making of an IntCell
try {
int lb = Integer.parseInt(editField.getText());
return new IntCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid integer.");
}
} else if (cType.isCompatible(LongValue.class)) {
try {
long lb = Long.parseLong(editField.getText());
return new LongCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid number.");
}
} else if (cType.isCompatible(DoubleValue.class)) {
try {
double lb = Double.parseDouble(editField.getText());
return new DoubleCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: enter a valid " + "float number");
}
} else {
return new StringCell(editField.getText());
}
} else {
// if we got no column type
return new StringCell(editField.getText());
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class SubsetMatcherNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
final BufferedDataTable subsetTable = inData[0];
final DataTableSpec subsetTableSpec = subsetTable.getSpec();
final int subsetColIdx = subsetTableSpec.findColumnIndex(m_subsetCol.getStringValue());
// the comparator that should be used to sort the subset AND the
// set list
final Comparator<DataCell> comparator = subsetTableSpec.getColumnSpec(subsetColIdx).getType().getComparator();
final BufferedDataTable setTable = inData[1];
final DataTableSpec setTableSpec = setTable.getSpec();
final int setIDColIdx;
final DataColumnSpec setIDSpec;
if (m_setIDCol.useRowID()) {
setIDColIdx = -1;
setIDSpec = null;
} else {
setIDColIdx = setTableSpec.findColumnIndex(m_setIDCol.getStringValue());
setIDSpec = setTableSpec.getColumnSpec(setIDColIdx);
}
final int transColIdx = setTableSpec.findColumnIndex(m_setCol.getStringValue());
final boolean appendSetCol = m_appendSetListCol.getBooleanValue();
// create the data container
final DataTableSpec resultSpec = createTableSpec(setIDSpec, setTableSpec.getColumnSpec(transColIdx), subsetTableSpec.getColumnSpec(subsetColIdx), appendSetCol);
m_dc = exec.createDataContainer(resultSpec);
final long subsetRowCount = subsetTable.size();
if (subsetRowCount == 0) {
setWarningMessage("Empty subset table found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final long setRowCount = setTable.size();
if (setRowCount == 0) {
setWarningMessage("Empty set table found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final double totalRowCount = subsetRowCount + setRowCount * SET_PROCESSING_FACTOR;
final ExecutionMonitor subsetExec = exec.createSubProgress(subsetRowCount / totalRowCount);
// create the rule model
exec.setMessage("Generating subset base...");
final SubsetMatcher[] sortedMatcher = createSortedMatcher(subsetExec, subsetTable, subsetColIdx, comparator);
subsetExec.setProgress(1.0);
if (sortedMatcher.length < 1) {
setWarningMessage("No item sets found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final ExecutionMonitor setExec = exec.createSubProgress((setRowCount * SET_PROCESSING_FACTOR) / totalRowCount);
// create the matching processes
exec.setMessage("Processing sets... ");
// initialize the thread pool for parallelization of the set
// analysis
final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL.createSubPool(1);
for (final DataRow row : setTable) {
exec.checkCanceled();
DataCell setIDCell;
if (setIDColIdx < 0) {
final RowKey key = row.getKey();
setIDCell = new StringCell(key.getString());
} else {
setIDCell = row.getCell(setIDColIdx);
}
final DataCell setCell = row.getCell(transColIdx);
if (!(setCell instanceof CollectionDataValue)) {
setExec.setProgress(m_setCounter.incrementAndGet() / (double) setRowCount);
m_skipCounter.incrementAndGet();
continue;
}
final CollectionDataValue setList = (CollectionDataValue) setCell;
if (setList.size() < 1) {
// skip empty sets
setExec.setProgress(m_setCounter.incrementAndGet() / (double) setRowCount);
m_skipCounter.incrementAndGet();
continue;
}
// submit for each set a job in the thread pool
pool.enqueue(createRunnable(setExec, setRowCount, setIDCell, setList, appendSetCol, comparator, sortedMatcher, m_maxMismatches.getIntValue()));
}
// wait until all jobs are finished before closing the container
// and returning the method
pool.waitForTermination();
exec.setMessage("Creating data table...");
m_dc.close();
if (m_skipCounter.intValue() > 0) {
setWarningMessage("No matching subsets found for " + m_skipCounter + " out of " + setRowCount + " sets");
}
exec.setProgress(1.0);
return new BufferedDataTable[] { m_dc.getTable() };
}
Aggregations