Search in sources :

Example 1 with RowKey

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 };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) RowKey(org.knime.core.data.RowKey) BufferedDataTable(org.knime.core.node.BufferedDataTable) TSMissVHandler(org.knime.timeseries.node.timemissvaluehandler.tshandler.TSMissVHandler) DataRow(org.knime.core.data.DataRow)

Example 2 with RowKey

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);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) JavaScriptingSettings(org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 3 with RowKey

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);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Set(java.util.Set) HashSet(java.util.HashSet) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataValue(org.knime.core.data.DataValue) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with RowKey

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataValue(org.knime.core.data.DataValue) DataRow(org.knime.core.data.DataRow) ContainerTable(org.knime.core.data.container.ContainerTable) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with RowKey

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) RowKey(org.knime.core.data.RowKey) Period(java.time.Period) Duration(java.time.Duration) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate) Temporal(java.time.temporal.Temporal) ZonedDateTime(java.time.ZonedDateTime) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

RowKey (org.knime.core.data.RowKey)237 DataCell (org.knime.core.data.DataCell)101 DataRow (org.knime.core.data.DataRow)96 DefaultRow (org.knime.core.data.def.DefaultRow)87 DataTableSpec (org.knime.core.data.DataTableSpec)62 BufferedDataTable (org.knime.core.node.BufferedDataTable)57 HashSet (java.util.HashSet)50 DataColumnSpec (org.knime.core.data.DataColumnSpec)40 DoubleCell (org.knime.core.data.def.DoubleCell)40 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)40 StringCell (org.knime.core.data.def.StringCell)39 LinkedHashMap (java.util.LinkedHashMap)35 Set (java.util.Set)35 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)29 LinkedHashSet (java.util.LinkedHashSet)26 HashMap (java.util.HashMap)25 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)23 IOException (java.io.IOException)22 IntCell (org.knime.core.data.def.IntCell)22 Map (java.util.Map)21