use of org.knime.core.data.RowKey in project knime-core by knime.
the class TimeMissValueNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
ExecutionContext createExec = exec.createSubExecutionContext(0.4);
DataTableSpec spec = inData[0].getDataTableSpec();
double maxRow = inData[0].getRowCount();
double currRow = 0;
Map<String, TSMissVHandler> nameToMVHandler = checkInputAndCreateHandlerMap(spec);
Map<String, Integer> nameToInt = findColumns(spec, nameToMVHandler);
if (nameToMVHandler.isEmpty()) {
return inData;
}
for (DataRow row : inData[0]) {
RowKey key = row.getKey();
for (String s : nameToInt.keySet()) {
nameToMVHandler.get(s).incomingValue(key, row.getCell(nameToInt.get(s)));
}
createExec.checkCanceled();
createExec.setProgress(++currRow / maxRow, "Preprocessing... Row " + row.getKey().getString());
}
for (String s : nameToMVHandler.keySet()) {
nameToMVHandler.get(s).close();
}
ExecutionContext builtExec = exec.createSubExecutionContext(0.6);
ColumnRearranger colR = createColumnRearranger(nameToMVHandler, nameToInt, inData[0].getDataTableSpec());
BufferedDataTable outTable = exec.createColumnRearrangeTable(inData[0], colR, builtExec);
return new BufferedDataTable[] { outTable };
}
use of org.knime.core.data.RowKey in project knime-core by knime.
the class StringManipulationVariableNodeModel method calculate.
/**
* @throws CompilationFailedException
* @throws InstantiationException
* @throws Exception
*/
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
settings.setInputAndCompile(new DataTableSpec());
// calculate the result
ColumnCalculator cc = new ColumnCalculator(settings, this);
DataCell calculate = null;
try {
calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
} catch (NoSuchElementException e) {
throw new InvalidSettingsException(e.getMessage());
}
String newVariableName;
Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
if (m_settings.isReplace()) {
newVariableName = m_settings.getColName();
CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
} else {
newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
}
// convert and push result as flow variable
CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
Class<? extends DataCell> cellType = calculate.getClass();
if (cellType.equals(IntCell.class)) {
pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
} else if (cellType.equals(DoubleCell.class)) {
pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
} else if (cellType.equals(StringCell.class)) {
pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
} else {
throw new RuntimeException("Invalid variable class: " + cellType);
}
}
use of org.knime.core.data.RowKey in project knime-core by knime.
the class HistogramColumn method constructFromDataArray.
/**
* Constructs the helper data structures from the numeric hostigran models and the data as {@link DataArray}.
*
* @param histograms The numeric histograms.
* @param data The input data.
* @param nominalColumnNames The nominal column names.
* @return The helper data structures.
* @see #construct(Map, DataTable, Set)
*/
protected static Pair<Map<Integer, Map<Integer, Set<RowKey>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> constructFromDataArray(final Map<Integer, HistogramNumericModel> histograms, final DataTable data, final Set<String> nominalColumnNames) {
Map<Integer, Map<Integer, Set<RowKey>>> numericMapping = new HashMap<Integer, Map<Integer, Set<RowKey>>>();
Map<Integer, Map<DataValue, Set<RowKey>>> nominalMapping = new HashMap<Integer, Map<DataValue, Set<RowKey>>>();
DataTableSpec tableSpec = data.getDataTableSpec();
for (DataColumnSpec colSpec : tableSpec) {
int colIndex = tableSpec.findColumnIndex(colSpec.getName());
if (colSpec.getType().isCompatible(DoubleValue.class)) {
// + colIndex;
if (histograms.containsKey(Integer.valueOf(colIndex)) && histograms.get(colIndex) != null) {
numericMapping.put(colIndex, new HashMap<Integer, Set<RowKey>>());
}
}
if (colSpec.getDomain().hasValues() || nominalColumnNames.contains(colSpec.getName())) {
nominalMapping.put(colIndex, new HashMap<DataValue, Set<RowKey>>());
}
}
for (DataRow dataRow : data) {
for (Entry<Integer, Map<Integer, Set<RowKey>>> outer : numericMapping.entrySet()) {
Integer key = outer.getKey();
DataCell cell = dataRow.getCell(key);
if (cell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) cell;
Integer bin = Integer.valueOf(histograms.get(key).findBin(dv));
Map<Integer, Set<RowKey>> inner = outer.getValue();
if (!inner.containsKey(bin)) {
inner.put(bin, new HashSet<RowKey>());
}
inner.get(bin).add(dataRow.getKey());
}
}
for (Entry<Integer, Map<DataValue, Set<RowKey>>> outer : nominalMapping.entrySet()) {
int key = outer.getKey().intValue();
DataCell cell = dataRow.getCell(key);
if (!cell.isMissing()) /* && cell instanceof NominalValue*/
{
Map<DataValue, Set<RowKey>> inner = outer.getValue();
if (!inner.containsKey(cell)) {
inner.put(cell, new HashSet<RowKey>());
}
inner.get(cell).add(dataRow.getKey());
}
}
}
return Pair.create(numericMapping, nominalMapping);
}
use of org.knime.core.data.RowKey in project knime-core by knime.
the class HistogramColumn method loadHistograms.
/**
* Loads the histograms from the saved internal files.
*
* @param histogramsGz The file for the histograms.
* @param dataArrayGz The data array file for the row keys.
* @param nominalColumns The nominal columns.
* @param strategy The strategy used to compute the bins.
* @param means The mean values for the numeric columns.
* @return A triple (Pair(Pair(,),)) of histograms, numeric and nominal row keys.
* @throws IOException Failed to read the files.
* @throws InvalidSettingsException Something went wrong.
*/
public static Pair<Pair<Map<Integer, ? extends HistogramModel<?>>, Map<Integer, Map<Integer, Set<RowKey>>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> loadHistograms(final File histogramsGz, final File dataArrayGz, final Set<String> nominalColumns, final BinNumberSelectionStrategy strategy, final double[] means) throws IOException, InvalidSettingsException {
Map<Integer, Map<Integer, Set<RowKey>>> numericKeys = new HashMap<Integer, Map<Integer, Set<RowKey>>>();
Map<Integer, HistogramNumericModel> histograms = loadHistogramsPrivate(histogramsGz, numericKeys, strategy, means);
Map<Integer, Map<DataValue, Set<RowKey>>> nominalKeys = new HashMap<Integer, Map<DataValue, Set<RowKey>>>();
ContainerTable table = DataContainer.readFromZip(dataArrayGz);
Set<Integer> numericColIndices = numericKeys.keySet();
for (String colName : nominalColumns) {
int colIndex = table.getDataTableSpec().findColumnIndex(colName);
if (colIndex < 0) {
continue;
}
nominalKeys.put(Integer.valueOf(colIndex), new HashMap<DataValue, Set<RowKey>>());
}
for (DataRow dataRow : table) {
for (Integer col : numericColIndices) {
// Integer col = Integer.valueOf(colIdx);
HistogramNumericModel hd = histograms.get(col);
Map<Integer, Set<RowKey>> map = numericKeys.get(col);
DataCell cell = dataRow.getCell(col.intValue());
if (!cell.isMissing() && cell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) cell;
Integer bin = Integer.valueOf(hd.findBin(dv));
if (!map.containsKey(bin)) {
map.put(bin, new HashSet<RowKey>());
}
map.get(bin).add(dataRow.getKey());
}
}
for (Entry<Integer, Map<DataValue, Set<RowKey>>> entry : nominalKeys.entrySet()) {
DataCell value = dataRow.getCell(entry.getKey().intValue());
Map<DataValue, Set<RowKey>> map = entry.getValue();
if (!map.containsKey(value)) {
map.put(value, new HashSet<RowKey>());
}
map.get(value).add(dataRow.getKey());
}
}
return Pair.create(new Pair<Map<Integer, ? extends HistogramModel<?>>, Map<Integer, Map<Integer, Set<RowKey>>>>(histograms, numericKeys), nominalKeys);
}
use of org.knime.core.data.RowKey in project knime-core by knime.
the class CreateDateTimeNodeModel method createByVariableRowNr.
/**
* Create date&time row with a variable number of rows depending on a given starting point, a duration/period and an
* ending point.
*/
private void createByVariableRowNr(final BufferedDataContainer container, final Temporal startDateTime, final Temporal endDateTime, final TemporalAmount durationOrPeriod) throws InvalidSettingsException {
Temporal start = startDateTime;
Temporal end = endDateTime;
// check if duration is zero
if ((durationOrPeriod instanceof Period) && ((Period) durationOrPeriod).isZero()) {
setWarningMessage("Interval is zero! Node created an empty table.");
return;
} else if ((durationOrPeriod instanceof Duration) && ((Duration) durationOrPeriod).isZero()) {
setWarningMessage("Interval is zero! Node created an empty table.");
return;
}
// === check if start date is after end ===
boolean wasLocalTime = start instanceof LocalTime;
final boolean isStartAfterEnd;
if (start instanceof LocalDate) {
isStartAfterEnd = ((LocalDate) start).isAfter((LocalDate) end);
} else if (start instanceof LocalTime) {
// because of the problem that 00:00 is before 23:59, a local time needs to be temporarily converted
// to a local date time
boolean isLocalTimeStartAfterEnd = ((LocalTime) start).isAfter((LocalTime) end);
final boolean isDurationNegative = ((Duration) durationOrPeriod).isNegative();
int daysAddedToEnd = 0;
if (isLocalTimeStartAfterEnd && !isDurationNegative) {
daysAddedToEnd = 1;
}
if (!isLocalTimeStartAfterEnd && isDurationNegative) {
daysAddedToEnd = -1;
}
if (start.equals(end)) {
daysAddedToEnd = 0;
}
final int dayOfYear = 10;
start = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear), (LocalTime) start);
end = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear + daysAddedToEnd), (LocalTime) end);
isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
} else if (start instanceof LocalDateTime) {
isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
} else {
isStartAfterEnd = ((ZonedDateTime) start).isAfter((ZonedDateTime) end);
}
// === check if input is legal: duration/period needs to be positive if end is after start and vice versa ===
final String warningMsgPos = "Interval must be positive, if end is after start date! Node created an empty table.";
final String warningMsgNeg = "Interval must be negative, if end is before start date! Node created an empty table.";
if (start instanceof LocalDate) {
if ((((LocalDate) end).isAfter((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isBefore((LocalDate) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((LocalDate) end).isBefore((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isAfter((LocalDate) start))) {
setWarningMessage(warningMsgNeg);
return;
}
} else if (start instanceof LocalDateTime) {
if ((((LocalDateTime) end).isAfter((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isBefore((LocalDateTime) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((LocalDateTime) end).isBefore((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isAfter((LocalDateTime) start))) {
setWarningMessage(warningMsgNeg);
return;
}
} else if (start instanceof ZonedDateTime) {
if ((((ZonedDateTime) end).isAfter((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isBefore((ZonedDateTime) start))) {
setWarningMessage(warningMsgPos);
return;
}
if ((((ZonedDateTime) end).isBefore((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isAfter((ZonedDateTime) start))) {
setWarningMessage(warningMsgNeg);
return;
}
}
// === create rows ===
Temporal currentDateTime = start;
long row_idx = 0;
while (true) {
final DataCell dataCell;
final boolean isEqual = currentDateTime.equals(end);
final boolean isCurrentAfterEnd;
if (currentDateTime instanceof LocalDate) {
isCurrentAfterEnd = ((LocalDate) currentDateTime).isAfter((LocalDate) end);
dataCell = LocalDateCellFactory.create((LocalDate) currentDateTime);
} else if (currentDateTime instanceof LocalDateTime) {
isCurrentAfterEnd = ((LocalDateTime) currentDateTime).isAfter((LocalDateTime) end);
if (wasLocalTime) {
dataCell = LocalTimeCellFactory.create((((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS)).toLocalTime());
} else {
dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
}
} else {
isCurrentAfterEnd = ((ZonedDateTime) currentDateTime).isAfter((ZonedDateTime) end);
dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
}
if ((isCurrentAfterEnd && !isStartAfterEnd) || (!isCurrentAfterEnd && !isEqual && isStartAfterEnd)) {
break;
}
container.addRowToTable(new DefaultRow(new RowKey("Row" + row_idx++), dataCell));
if (isEqual) {
break;
}
currentDateTime = currentDateTime.plus(durationOrPeriod);
}
}
Aggregations