use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class AddEmptyRowsNodeModel method createNewRowsTable.
private BufferedDataTable createNewRowsTable(final DataTableSpec inSpec, final long rowCount, final ExecutionContext subExec) throws CanceledExecutionException {
DataCell[] cells = new DataCell[inSpec.getNumColumns()];
for (int c = 0; c < cells.length; c++) {
DataType type = inSpec.getColumnSpec(c).getType();
if (type.isASuperTypeOf(DoubleCell.TYPE)) {
if (m_config.isUseMissingDouble()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new DoubleCell(m_config.getFillValueDouble());
}
} else if (type.isASuperTypeOf(IntCell.TYPE)) {
if (m_config.isUseMissingInt()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new IntCell(m_config.getFillValueInt());
}
} else if (type.isASuperTypeOf(StringCell.TYPE)) {
if (m_config.isUseMissingString()) {
cells[c] = DataType.getMissingCell();
} else {
cells[c] = new StringCell(m_config.getFillValueString());
}
} else {
cells[c] = DataType.getMissingCell();
}
}
BufferedDataContainer cont = subExec.createDataContainer(inSpec);
for (long i = 0; i < rowCount; i++) {
RowKey key = new RowKey(m_config.getNewRowKeyPrefix() + i);
subExec.setProgress(i / (double) rowCount, "Creating row \"" + key + "\", " + i + "/" + rowCount);
subExec.checkCanceled();
cont.addRowToTable(new DefaultRow(key, cells));
}
cont.close();
return cont.getTable();
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ClassAttributeModel method createDataRows.
/**
* {@inheritDoc}
*/
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
final List<String> sortedClassVal = AttributeModel.sortCollection(m_recsCounterByClassVal.keySet());
if (sortedClassVal == null) {
return;
}
final StringCell attributeNameCell = new StringCell(getAttributeName());
for (final String classVal : sortedClassVal) {
final StringCell classCell = new StringCell(classVal);
final List<DataCell> cells = new LinkedList<>();
cells.add(attributeNameCell);
cells.add(DataType.getMissingCell());
cells.add(classCell);
cells.add(new IntCell(getNoOfRecs4ClassValue(classVal)));
if (!ignoreMissing) {
cells.add(new IntCell(getNoOfMissingVals()));
}
cells.add(DataType.getMissingCell());
cells.add(DataType.getMissingCell());
dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class NominalAttributeModel method createDataRows.
/**
* {@inheritDoc}
*/
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
final List<String> sortedClassVal = AttributeModel.sortCollection(m_classValues.keySet());
if (sortedClassVal == null) {
return;
}
final List<String> sortedAttrValues = AttributeModel.sortCollection(m_attributeVals);
final StringCell attributeNameCell = new StringCell(getAttributeName());
for (final String attrVal : sortedAttrValues) {
final StringCell attributeValueCell = new StringCell(attrVal);
for (final String classVal : sortedClassVal) {
final StringCell classCell = new StringCell(classVal);
final NominalClassValue classValue = m_classValues.get(classVal);
final List<DataCell> cells = new LinkedList<>();
cells.add(attributeNameCell);
cells.add(attributeValueCell);
cells.add(classCell);
cells.add(new IntCell(classValue.getNoOfRows4AttributeValue(attrVal)));
if (!ignoreMissing) {
cells.add(new IntCell(classValue.getNoOfMissingValueRecs()));
}
cells.add(DataType.getMissingCell());
cells.add(DataType.getMissingCell());
dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
}
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class PolyRegLearnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[0];
DataTableSpec inSpec = inTable.getDataTableSpec();
final int colCount = inSpec.getNumColumns();
String[] selectedCols = computeSelectedColumns(inSpec);
Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
m_colSelected = new boolean[colCount];
for (int i = 0; i < colCount; i++) {
m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
}
final int rowCount = inTable.getRowCount();
String[] temp = new String[m_columnNames.length + 1];
System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
temp[temp.length - 1] = m_settings.getTargetColumn();
FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
final DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
// handle the optional PMML input
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
PortObjectSpec[] outputSpec = configure((inPMMLPort == null) ? new PortObjectSpec[] { inData[0].getSpec(), null } : new PortObjectSpec[] { inData[0].getSpec(), inPMMLPort.getSpec() });
Learner learner = new Learner((PMMLPortObjectSpec) outputSpec[0], 0d, m_settings.getMissingValueHandling() == MissingValueHandling.fail, m_settings.getDegree());
try {
PolyRegContent polyRegContent = learner.perform(inTable, exec);
m_betas = fillBeta(polyRegContent);
m_meanValues = polyRegContent.getMeans();
ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
PortObject[] bdt = new PortObject[] { createPMMLModel(inPMMLPort, inSpec), exec.createColumnRearrangeTable(inTable, crea, exec.createSilentSubExecutionContext(.2)), polyRegContent.createTablePortObject(exec.createSubExecutionContext(0.2)) };
m_squaredError /= rowCount;
if (polyRegContent.getWarningMessage() != null) {
setWarningMessage(polyRegContent.getWarningMessage());
}
double[] stdErrors = PolyRegViewData.mapToArray(polyRegContent.getStandardErrors(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptStdErr());
double[] tValues = PolyRegViewData.mapToArray(polyRegContent.getTValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptTValue());
double[] pValues = PolyRegViewData.mapToArray(polyRegContent.getPValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptPValue());
m_viewData = new PolyRegViewData(m_meanValues, m_betas, stdErrors, tValues, pValues, m_squaredError, polyRegContent.getAdjustedRSquared(), m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
return bdt;
} catch (ModelSpecificationException e) {
final String origWarning = getWarningMessage();
final String warning = (origWarning != null && !origWarning.isEmpty()) ? (origWarning + "\n") : "" + e.getMessage();
setWarningMessage(warning);
final ExecutionContext subExec = exec.createSubExecutionContext(.1);
final BufferedDataContainer empty = subExec.createDataContainer(STATS_SPEC);
int rowIdx = 1;
for (final String column : m_columnNames) {
for (int d = 1; d <= m_settings.getDegree(); ++d) {
empty.addRowToTable(new DefaultRow("Row" + rowIdx++, new StringCell(column), new IntCell(d), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
}
}
empty.addRowToTable(new DefaultRow("Row" + rowIdx, new StringCell("Intercept"), new IntCell(0), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
double[] nans = new double[m_columnNames.length * m_settings.getDegree() + 1];
Arrays.fill(nans, Double.NaN);
m_betas = new double[nans.length];
// Mean only for the linear tags
m_meanValues = new double[nans.length / m_settings.getDegree()];
m_viewData = new PolyRegViewData(m_meanValues, m_betas, nans, nans, nans, m_squaredError, Double.NaN, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
empty.close();
ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
BufferedDataTable rearrangerTable = exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6));
PMMLPortObject model = createPMMLModel(inPMMLPort, inTable.getDataTableSpec());
PortObject[] bdt = new PortObject[] { model, rearrangerTable, empty.getTable() };
return bdt;
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ClusterAssignerNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final PMMLPortObject port, final DataTableSpec inSpec) throws InvalidSettingsException {
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.ClusteringModel);
if (models.isEmpty()) {
String msg = "No Clustering Model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLClusterTranslator trans = new PMMLClusterTranslator();
port.initializeModelTranslator(trans);
ComparisonMeasure measure = trans.getComparisonMeasure();
List<Prototype> prototypes = new ArrayList<Prototype>();
String[] labels = trans.getLabels();
double[][] protos = trans.getPrototypes();
for (int i = 0; i < protos.length; i++) {
double[] prototype = protos[i];
prototypes.add(new Prototype(prototype, new StringCell(labels[i])));
}
ColumnRearranger colre = new ColumnRearranger(inSpec);
colre.append(new ClusterAssignFactory(measure, prototypes, createNewOutSpec(inSpec), findLearnedColumnIndices(inSpec, trans.getUsedColumns())));
return colre;
}
Aggregations