use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class PMMLRuleSetPredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec original = (DataTableSpec) inSpecs[DATA_INDEX];
ColumnRearranger rearranger = new ColumnRearranger(original);
PMMLPortObjectSpec portObjectSpec = (PMMLPortObjectSpec) inSpecs[MODEL_INDEX];
List<DataColumnSpec> activeColumnList = portObjectSpec.getActiveColumnList();
List<DataColumnSpec> notFound = new ArrayList<DataColumnSpec>();
for (DataColumnSpec dataColumnSpec : activeColumnList) {
if (original.containsName(dataColumnSpec.getName())) {
DataColumnSpec origSpec = original.getColumnSpec(dataColumnSpec.getName());
if (!origSpec.getType().equals(dataColumnSpec.getType())) {
notFound.add(dataColumnSpec);
}
} else {
notFound.add(dataColumnSpec);
}
}
if (!notFound.isEmpty()) {
StringBuilder sb = new StringBuilder("Incompatible to the table, the following columns are not present, or have a wrong type:");
for (DataColumnSpec dataColumnSpec : notFound) {
sb.append("\n ").append(dataColumnSpec);
}
throw new InvalidSettingsException(sb.toString());
}
List<DataColumnSpec> targetCols = portObjectSpec.getTargetCols();
final DataType dataType = targetCols.isEmpty() ? StringCell.TYPE : targetCols.get(0).getType();
DataColumnSpecCreator specCreator;
if (m_doReplaceColumn.getBooleanValue()) {
String col = m_replaceColumn.getStringValue();
specCreator = new DataColumnSpecCreator(col, dataType);
} else {
specCreator = new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(original, m_outputColumn.getStringValue()), dataType);
}
SingleCellFactory dummy = new SingleCellFactory(specCreator.createSpec()) {
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
throw new IllegalStateException();
}
};
if (m_addConfidence.getBooleanValue()) {
rearranger.append(new SingleCellFactory(new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(rearranger.createSpec(), m_confidenceColumn.getStringValue()), DoubleCell.TYPE).createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
throw new IllegalStateException();
}
});
}
if (m_doReplaceColumn.getBooleanValue()) {
rearranger.replace(dummy, m_replaceColumn.getStringValue());
} else {
rearranger.append(dummy);
}
return new DataTableSpec[] { rearranger.createSpec() };
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class HistogramColumn method createColumnRearranger.
/**
* Creates the rearranger that adds the histograms.
*
* @param data The input data table that contains the columns referred by {@code histograms} keys.
* @param stats The statistics table to be adjusted.
* @param histograms The histograms.
* @param columns The columns to be described.
* @return The {@link ColumnRearranger}.
*/
ColumnRearranger createColumnRearranger(final BufferedDataTable data, final BufferedDataTable stats, final Map<Integer, HistogramNumericModel> histograms, final int maxBinCount, final String... columns) {
ColumnRearranger rearranger = new ColumnRearranger(stats.getDataTableSpec());
final DataColumnSpec spec = createHistogramColumnSpec();
rearranger.append(new SingleCellFactory(true, spec) {
String[] m_sortedColumns = columns.clone();
{
Arrays.sort(m_sortedColumns);
}
@Override
public DataCell getCell(final DataRow row) {
if (Arrays.binarySearch(m_sortedColumns, row.getKey().getString()) < 0) {
return DataType.getMissingCell();
}
final int columnIndex = data.getSpec().findColumnIndex(row.getKey().getString());
final HistogramNumericModel histogramData = histograms.get(Integer.valueOf(columnIndex));
if (histogramData == null) {
// Wrong bounds
return DataType.getMissingCell();
}
assert columnIndex == histogramData.getColIndex() : "Expected: " + columnIndex + ", but got: " + histogramData.getColIndex();
return createImageCell(histogramData, false);
}
});
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ModifyTimeNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) {
final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndices = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
// determine the data type of output
DataType dataType;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_REMOVE)) {
dataType = LocalDateCellFactory.TYPE;
} else {
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_CHANGE)) {
dataType = LocalDateTimeCellFactory.TYPE;
} else {
if (m_timeZone.useZone()) {
dataType = ZonedDateTimeCellFactory.TYPE;
} else {
dataType = LocalDateTimeCellFactory.TYPE;
}
}
}
int i = 0;
for (final String includedCol : includeList) {
if (inSpec.getColumnSpec(includedCol).getType().equals(ZonedDateTimeCellFactory.TYPE) && m_modifyAction.getStringValue().equals(MODIFY_OPTION_CHANGE)) {
dataType = ZonedDateTimeCellFactory.TYPE;
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includedCol, dataType);
final SingleCellFactory cellFac = createCellFactory(dataColumnSpecCreator.createSpec(), includeIndices[i++], m_timeZone.getZone());
rearranger.replace(cellFac, includedCol);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includedCol + m_suffix.getStringValue(), dataType);
final SingleCellFactory cellFac = createCellFactory(dataColSpec, includeIndices[i++], m_timeZone.getZone());
rearranger.append(cellFac);
}
}
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ModifyDateNodeModel method createColumnRearranger.
/**
* @param inSpec table input spec
* @return the CR describing the output
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) {
final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndices = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
// determine the data type of output
DataType dataType;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_REMOVE)) {
dataType = LocalTimeCellFactory.TYPE;
} else {
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_CHANGE)) {
dataType = LocalDateTimeCellFactory.TYPE;
} else {
if (m_timeZone.useZone()) {
dataType = ZonedDateTimeCellFactory.TYPE;
} else {
dataType = LocalDateTimeCellFactory.TYPE;
}
}
}
final ZoneId zone = m_timeZone.getZone();
int i = 0;
for (final String includedCol : includeList) {
if (inSpec.getColumnSpec(includedCol).getType().equals(ZonedDateTimeCellFactory.TYPE) && m_modifyAction.getStringValue().equals(MODIFY_OPTION_CHANGE)) {
dataType = ZonedDateTimeCellFactory.TYPE;
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includedCol, dataType);
final SingleCellFactory cellFac = createCellFactory(dataColumnSpecCreator.createSpec(), includeIndices[i++], zone);
rearranger.replace(cellFac, includedCol);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includedCol + m_suffix.getStringValue(), dataType);
final SingleCellFactory cellFac = createCellFactory(dataColSpec, includeIndices[i++], zone);
rearranger.append(cellFac);
}
}
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class DateShiftConfigure method getTimeBasedValueCellFactory.
/**
* @param spec the previous data table spec
* @param col1Idx the column index of the numerical column to add
* @param g the time field to modify (as defined by calendar constants)
* @param conf the configuration object
* @param time the configured time as Calendar
* @return the cell factory
*/
public static SingleCellFactory getTimeBasedValueCellFactory(final DataTableSpec spec, final int col1Idx, final int g, final DateShiftConfigure conf, final Calendar time) {
return new SingleCellFactory(createOutputColumnSpec(spec, conf.getNewColumnName().getStringValue())) {
/**
* Value for the new column is based on the values of two column of the row (first and second date column),
* the selected granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(col1Idx);
if ((cell1.isMissing())) {
return DataType.getMissingCell();
}
Calendar c = (Calendar) time.clone();
c.add(g, ((IntValue) cell1).getIntValue());
return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
}
};
}
Aggregations