Search in sources :

Example 6 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class ExtractTimeWindowNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    boolean useTime = m_fromDate.useTime() || m_toDate.useTime();
    BufferedDataTable in = inData[0];
    DataTableSpec outs = in.getDataTableSpec();
    final int colIndex = outs.findColumnIndex(m_columnName.getStringValue());
    BufferedDataContainer t = exec.createDataContainer(outs);
    final long totalRowCount = in.size();
    int currentIteration = 0;
    try {
        for (DataRow r : in) {
            // increment before printing to achieve a 1-based index
            currentIteration++;
            exec.checkCanceled();
            exec.setProgress(currentIteration / (double) totalRowCount, "Processing row " + currentIteration);
            DataCell cell = r.getCell(colIndex);
            if (cell.isMissing()) {
                // do not include missing values -> skip it
                continue;
            }
            Calendar time = ((DateAndTimeValue) cell).getUTCCalendarClone();
            // which is implemented as a real < or >
            if (!useTime) {
                DateAndTimeCell.resetTimeFields(time);
            }
            if (time.compareTo(m_fromDate.getCalendar()) >= 0 && time.compareTo(m_toDate.getCalendar()) <= 0) {
                t.addRowToTable(r);
            }
        }
    } finally {
        t.close();
    }
    return new BufferedDataTable[] { t.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 7 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class MaskTimeNodeModel method createCellFactory.

private SingleCellFactory createCellFactory(final DataColumnSpec spec, final int colIdx, final String maskMode) {
    return new SingleCellFactory(spec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                Calendar time = v.getUTCCalendarClone();
                if (maskMode.equals(MASK_DATE)) {
                    DateAndTimeCell.resetDateFields(time);
                    if (!v.hasTime()) {
                        // date is masked and no time -> missing value
                        m_nrInvalids++;
                        return DataType.getMissingCell();
                    }
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), false, v.hasTime(), v.hasMillis());
                } else if (maskMode.equals(MASK_TIME)) {
                    DateAndTimeCell.resetTimeFields(time);
                    if (!v.hasDate()) {
                        // time is masked and no date -> missing cell
                        m_nrInvalids++;
                        return DataType.getMissingCell();
                    }
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), false, false);
                } else if (maskMode.equals(MASK_MILLIS)) {
                    resetMilliSeconds(time);
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), v.hasTime(), false);
                }
            }
            LOGGER.error("Unsupported data type: " + dc.getType() + "!");
            return DataType.getMissingCell();
        }
    };
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 8 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class String2DateNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 * @throws InvalidSettingsException
 * @since 2.6
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals emptyInternals) throws InvalidSettingsException {
    if (m_formatModel.getStringValue() == null) {
        throw new InvalidSettingsException("No format selected.");
    }
    try {
        m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
    } catch (IllegalArgumentException ex) {
        throw new InvalidSettingsException("Invalid format: " + m_formatModel.getStringValue(), ex);
    }
    m_dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
    String selectedCol = m_selectedColModel.getStringValue();
    if (selectedCol == null || selectedCol.isEmpty()) {
        // try to find first String compatible one and auto-guess it
        for (DataColumnSpec cs : spec) {
            if (cs.getType().isCompatible(StringValue.class)) {
                m_selectedColModel.setStringValue(cs.getName());
                setWarningMessage("Auto-guessing first String compatible column: " + cs.getName());
                break;
            }
        }
    }
    // if still null -> no String compatible column at all
    if (selectedCol == null || selectedCol.isEmpty()) {
        throw new InvalidSettingsException("No String compatible column found!");
    }
    final int colIndex = spec.findColumnIndex(selectedCol);
    if (colIndex < 0) {
        throw new InvalidSettingsException("No such column: " + selectedCol);
    }
    DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    if (!colSpec.getType().isCompatible(StringValue.class)) {
        throw new InvalidSettingsException("Column \"" + selectedCol + "\" does not contain string values: " + colSpec.getType().toString());
    }
    ColumnRearranger result = new ColumnRearranger(spec);
    String uniqueColName = selectedCol;
    if (!m_replace.getBooleanValue()) {
        // if we do not have a default new column name yet
        // create one as done in
        // check whether the new column name is unique...
        uniqueColName = DataTableSpec.getUniqueColumnName(spec, m_newColNameModel.getStringValue());
        m_newColNameModel.setStringValue(uniqueColName);
    }
    DataColumnSpec newColSpec = new DataColumnSpecCreator(uniqueColName, DateAndTimeCell.TYPE).createSpec();
    m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
    m_dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    SingleCellFactory c = new SingleCellFactory(newColSpec) {

        private int m_failCounter = 0;

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIndex);
            if (cell.isMissing() || !(cell instanceof StringValue)) {
                return DataType.getMissingCell();
            }
            try {
                String source = ((StringValue) cell).getStringValue();
                Date date = m_dateFormat.parse(source);
                Calendar calendar = DateAndTimeCell.getUTCCalendar();
                calendar.setTimeInMillis(date.getTime());
                // dependent on the type create the referring cell
                return new DateAndTimeCell(calendar.getTimeInMillis(), m_useDate, m_useTime, m_useMillis);
            } catch (ParseException pe) {
                m_failCounter++;
                if (m_cancelOnFail.getBooleanValue() && m_failCounter >= m_failNumberModel.getIntValue()) {
                    throw new IllegalArgumentException("Maximum number of fails reached: " + m_failNumberModel.getIntValue());
                }
                return DataType.getMissingCell();
            }
        }

        @Override
        public void afterProcessing() {
            setFailMessage(m_failCounter);
            emptyInternals.getConfig().addLong(INTERNALS_KEY_FAIL_COUNT, m_failCounter);
        }
    };
    if (m_replace.getBooleanValue()) {
        result.replace(c, colIndex);
    } else {
        result.append(c);
    }
    return result;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Calendar(java.util.Calendar) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) Date(java.util.Date) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue) SimpleDateFormat(java.text.SimpleDateFormat) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 9 with DataRow

use of org.knime.core.data.DataRow in project knime-core by knime.

the class Time2StringNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 * @since 2.6
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
    // check if input has dateandtime column
    if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
        throw new InvalidSettingsException("Input table must contain at least timestamp column!");
    }
    // currently selected column still there?
    String selectedColName = m_selectedCol.getStringValue();
    if (selectedColName != null && !selectedColName.isEmpty()) {
        if (!inSpec.containsName(selectedColName)) {
            throw new InvalidSettingsException("Column " + selectedColName + " not found in input spec!");
        }
    } else {
        // no value set: auto-configure -> choose first timeseries
        for (DataColumnSpec colSpec : inSpec) {
            if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
                String colName = colSpec.getName();
                m_selectedCol.setStringValue(colName);
                m_newColName.setStringValue(colName + "_" + COL_NAME_SUFFIX);
                setWarningMessage("Auto-selected column: '" + colName + "'");
                break;
            }
        }
    }
    ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    // if replace -> use original column name
    final boolean replace = m_replaceCol.getBooleanValue();
    String colName = DataTableSpec.getUniqueColumnName(inSpec, m_newColName.getStringValue());
    if (replace) {
        colName = m_selectedCol.getStringValue();
    }
    DataColumnSpecCreator specCreator = new DataColumnSpecCreator(colName, StringCell.TYPE);
    final SimpleDateFormat dateFormat = new SimpleDateFormat(m_pattern.getStringValue());
    dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
    final int colIdx = inSpec.findColumnIndex(m_selectedCol.getStringValue());
    SingleCellFactory factory = new SingleCellFactory(specCreator.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                String result = dateFormat.format(v.getUTCCalendarClone().getTime());
                return new StringCell(result);
            }
            LOGGER.error("Encountered unsupported data type: " + dc.getType() + " in row: " + row.getKey());
            return DataType.getMissingCell();
        }
    };
    if (!replace) {
        rearranger.append(factory);
    } else {
        rearranger.replace(factory, m_selectedCol.getStringValue());
    }
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) SimpleDateFormat(java.text.SimpleDateFormat) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 10 with DataRow

use of org.knime.core.data.DataRow 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)

Aggregations

DataRow (org.knime.core.data.DataRow)482 DataCell (org.knime.core.data.DataCell)268 DataTableSpec (org.knime.core.data.DataTableSpec)159 BufferedDataTable (org.knime.core.node.BufferedDataTable)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)109 RowKey (org.knime.core.data.RowKey)88 DefaultRow (org.knime.core.data.def.DefaultRow)88 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)80 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)76 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)73 DoubleValue (org.knime.core.data.DoubleValue)72 ArrayList (java.util.ArrayList)65 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)65 RowIterator (org.knime.core.data.RowIterator)62 DataType (org.knime.core.data.DataType)61 DoubleCell (org.knime.core.data.def.DoubleCell)57 StringCell (org.knime.core.data.def.StringCell)53 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)44 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)43