Search in sources :

Example 1 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class RuleEngine2PortsNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable data = (BufferedDataTable) inData[DATA_PORT];
    m_rowCount = data.size();
    try {
        Pair<ColumnRearranger, PortObject> rearrangerPair = createColumnRearranger((DataTableSpec) inData[DATA_PORT].getSpec(), new DataTableRowInput((DataTable) inData[RULE_PORT]));
        BufferedDataTable predictedTable = exec.createColumnRearrangeTable(data, rearrangerPair.getFirst(), exec);
        PortObject second = rearrangerPair.getSecond();
        if (m_settings.isPMMLRuleSet()) {
            if (m_settings.isProvideStatistics()) {
                PMMLPortObject po = (PMMLPortObject) rearrangerPair.getSecond();
                PMMLPortObject pmmlPortObject = new PMMLPortObject(m_copy.getSpec(), po);
                // Remove extra model.
                pmmlPortObject.addModelTranslater(new PMMLTranslator() {

                    @Override
                    public void initializeFrom(final PMMLDocument pmmlDoc) {
                    }

                    @Override
                    public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
                        return null;
                    }
                });
                second = pmmlPortObject;
            } else {
                second = m_copy;
            }
        }
        return new PortObject[] { predictedTable, second };
    } finally {
        m_rowCount = -1;
    }
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTable(org.knime.core.data.DataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) PMMLDocument(org.dmg.pmml.PMMLDocument) PMMLTranslator(org.knime.core.node.port.pmml.PMMLTranslator) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) SchemaType(org.apache.xmlbeans.SchemaType)

Example 2 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class RuleEngine2PortsNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        private StreamInternalWithPortObject m_internals;

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            m_internals = (StreamInternalWithPortObject) internals;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            // count number of rows
            long count = 0;
            final RowInput rowInput = (RowInput) inputs[DATA_PORT];
            while (rowInput.poll() != null) {
                count++;
            }
            if (inputs[RULE_PORT] instanceof RowInput) {
                final RowInput ruleInput = (RowInput) inputs[RULE_PORT];
                final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger(rowInput.getDataTableSpec(), ruleInput);
                final ColumnRearranger rearranger = pair.getFirst();
                final DataTableSpec spec = rearranger.createSpec();
                m_internals.setTableSpec(spec);
                if (pair.getSecond() instanceof PMMLPortObject) {
                    PMMLPortObject po = (PMMLPortObject) pair.getSecond();
                    m_internals.setObject(po);
                } else {
                    m_internals.setObject(null);
                }
            }
            m_internals.setRowCount(count);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return m_internals;
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            if (m_internals.getTableSpec() != null) {
                m_rowCount = m_internals.getRowCount();
            }
            final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger((DataTableSpec) inSpecs[DATA_PORT], (RowInput) inputs[RULE_PORT]);
            pair.getFirst().createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
            if (pair.getSecond() != null) {
                ((PortObjectOutput) outputs[1]).setPortObject(pair.getSecond());
            }
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortObjectOutput(org.knime.core.node.streamable.PortObjectOutput)

Example 3 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class MovingAverageNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inSpec = inSpecs[0];
    // define column name on which to apply MA
    if ((m_columnNames.getIncludeList().size() == 0) && (m_columnNames.getExcludeList().size() == 0)) {
        // auto-configure
        List<String> autoConfiguredInclList = new ArrayList<String>();
        for (DataColumnSpec colSpec : inSpec) {
            if (colSpec.getType().isCompatible(DoubleValue.class)) {
                autoConfiguredInclList.add(colSpec.getName());
            }
        }
        m_columnNames.setIncludeList(autoConfiguredInclList);
        setWarningMessage("Auto-configure: selected all double columns!");
    }
    if (m_columnNames.getIncludeList().isEmpty()) {
        setWarningMessage("No double columns selected: input will be same as output!");
    }
    // check for the existence of the selected columns
    for (String colName : m_columnNames.getIncludeList()) {
        if (!inSpecs[0].containsName(colName)) {
            throw new InvalidSettingsException("Column \"" + colName + "\" not found in input data!");
        }
    }
    // define moving average window length
    int winLength = m_winLength.getIntValue();
    if (winLength == -1) {
        throw new InvalidSettingsException("Window length is not selected.");
    }
    // define weight function
    String kindOfMAModelString;
    try {
        kindOfMAModelString = m_kindOfMAModel.getStringValue();
    } catch (IllegalArgumentException e) {
        throw new InvalidSettingsException(e.getMessage(), e);
    }
    if (kindOfMAModelString == null) {
        throw new InvalidSettingsException("No weight function selected.");
    } else {
        // create one MA-compute engine per column (overkill, I know
        // but much easier to reference later on in our DataCellFactory)
        MA_METHODS method = MA_METHODS.getPolicy4Label(kindOfMAModelString);
        if (MA_METHODS.getCenteredMethods().contains(method) && winLength % 2 == 0) {
            throw new InvalidSettingsException("For centered methods, the window size has to be uneven");
        }
        m_mas = new MovingAverage[inSpecs[0].getNumColumns()];
        for (int i = 0; i < inSpecs[0].getNumColumns(); i++) {
            m_mas[i] = method.getMAObject(winLength);
        }
    }
    ColumnRearranger c = createColRearranger(inSpecs[0]);
    return new DataTableSpec[] { c.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 4 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class TimeDifferenceNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // get the selected granularity level
    final Granularity g = Granularity.valueOf(m_granularity.getStringValue());
    // create rearranger
    ColumnRearranger rearranger = new ColumnRearranger(inData[0].getDataTableSpec());
    String typeofref = m_typeofreference.getStringValue();
    if (typeofref.equals(TimeDifferenceNodeDialog.CFG_COLUMN)) {
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.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(m_col1Idx);
                DataCell cell2 = row.getCell(m_col2Idx);
                if ((cell1.isMissing()) || (cell2.isMissing())) {
                    return DataType.getMissingCell();
                }
                long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                long last = ((DateAndTimeValue) cell2).getUTCTimeInMillis();
                return getRoundedTimeDifference(first, last, g);
            }
        });
    } else if (typeofref.equals(TimeDifferenceNodeDialog.CFG_ROW_DIFF)) {
        // option for producing the time difference between current and the
        // previous row.
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.getStringValue())) {

            /**
             * saves the previous time value (contained in the last row)
             */
            private DateAndTimeValue m_previous = null;

            /**
             * Value for the new column is based on the values of the
             * current row and the value of the previous row.
             * Therefore both rows must contain a DateAndTimeValue,
             * 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(m_col1Idx);
                // value
                if ((cell1.isMissing()) || !cell1.getType().isCompatible(DateAndTimeValue.class)) {
                    m_previous = null;
                    return DataType.getMissingCell();
                }
                // (e.g. we are in the first row)
                if (m_previous == null) {
                    m_previous = (DateAndTimeValue) cell1;
                    return DataType.getMissingCell();
                }
                long first = m_previous.getUTCTimeInMillis();
                long last = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                m_previous = (DateAndTimeValue) cell1;
                return getRoundedTimeDifference(first, last, g);
            }
        });
    } else {
        final long time;
        if (typeofref.equals(TimeDifferenceNodeDialog.CFG_FIXDATE)) {
            time = m_timemodel.getCalendar().getTimeInMillis();
        } else {
            time = System.currentTimeMillis() + TimeZone.getDefault().getOffset(System.currentTimeMillis());
        }
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.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(m_col1Idx);
                if ((cell1.isMissing())) {
                    return DataType.getMissingCell();
                }
                long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                return getRoundedTimeDifference(first, time, g);
            }
        });
    }
    BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    return new BufferedDataTable[] { out };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 5 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class DateFieldExtractorNodeModel method createColumnRearranger.

private SingleCellFactoryCompound createColumnRearranger(final DataTableSpec inSpec) {
    final int colIdx = inSpec.findColumnIndex(m_selectedColumn.getStringValue());
    ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    List<AbstractTimeExtractorCellFactory> cellFactories = new ArrayList<AbstractTimeExtractorCellFactory>();
    // year
    if (m_useYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_yearColName.getStringValue());
        AbstractTimeExtractorCellFactory yearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getYear();
            }
        };
        rearranger.append(yearFactory);
        cellFactories.add(yearFactory);
    }
    // quarter
    if (m_useQuarter.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_quarterColName.getStringValue());
        AbstractTimeExtractorCellFactory quarterFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                int month = value.getMonth();
                // hence we add 1
                return month / 3 + 1;
            }
        };
        rearranger.append(quarterFactory);
        cellFactories.add(quarterFactory);
    }
    // month
    AbstractTimeExtractorCellFactory monthFactory = null;
    if (m_useMonth.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_monthColName.getStringValue());
        if (m_monthRepresentation.getStringValue().equals(AbstractFieldExtractorNodeDialog.AS_INT)) {
            monthFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

                @Override
                protected int extractTimeField(final DateAndTimeValue value) {
                    // to java.util.Calendar#MONTH
                    return value.getMonth() + 1;
                }
            };
        } else {
            // extract the display name of the month
            monthFactory = new AbstractTimeExtractorStringCellFactory(colName, colIdx, false) {

                @Override
                protected String extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
                }
            };
        }
        rearranger.append(monthFactory);
        cellFactories.add(monthFactory);
    }
    // day of month
    if (m_useDay.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayColName.getStringValue());
        AbstractTimeExtractorCellFactory dayFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getDayOfMonth();
            }
        };
        rearranger.append(dayFactory);
        cellFactories.add(dayFactory);
    }
    // day of week
    AbstractTimeExtractorCellFactory dayOfWeekFactory = null;
    if (m_useDayOfWeek.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayOfWeekColName.getStringValue());
        if (m_dayOfWeekRepresentationModel.getStringValue().equals(AbstractFieldExtractorNodeDialog.AS_INT)) {
            dayOfWeekFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

                @Override
                protected int extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().get(Calendar.DAY_OF_WEEK);
                }
            };
        } else {
            dayOfWeekFactory = new AbstractTimeExtractorStringCellFactory(colName, colIdx, false) {

                @Override
                protected String extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
                }
            };
        // extract the display name of the day of week
        }
        rearranger.append(dayOfWeekFactory);
        cellFactories.add(dayOfWeekFactory);
    }
    // day of year
    if (m_useDayOfYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayOfYearColName.getStringValue());
        AbstractTimeExtractorCellFactory dayofYearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getUTCCalendarClone().get(Calendar.DAY_OF_YEAR);
            }
        };
        rearranger.append(dayofYearFactory);
        cellFactories.add(dayofYearFactory);
    }
    // week of year
    if (m_useWeekOfYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_weekOfYearColName.getStringValue());
        AbstractTimeExtractorCellFactory weekofYearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getUTCCalendarClone().get(Calendar.WEEK_OF_YEAR);
            }
        };
        rearranger.append(weekofYearFactory);
        cellFactories.add(weekofYearFactory);
    }
    return new SingleCellFactoryCompound(rearranger, cellFactories);
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SingleCellFactoryCompound(org.knime.timeseries.node.extract.SingleCellFactoryCompound) AbstractTimeExtractorIntCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorIntCellFactory) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) ArrayList(java.util.ArrayList) AbstractTimeExtractorStringCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorStringCellFactory) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) AbstractTimeExtractorCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory)

Aggregations

ColumnRearranger (org.knime.core.data.container.ColumnRearranger)393 DataTableSpec (org.knime.core.data.DataTableSpec)221 BufferedDataTable (org.knime.core.node.BufferedDataTable)153 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)116 DataRow (org.knime.core.data.DataRow)79 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)69 DataCell (org.knime.core.data.DataCell)63 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)55 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)49 ExecutionContext (org.knime.core.node.ExecutionContext)46 PortObject (org.knime.core.node.port.PortObject)39 ArrayList (java.util.ArrayList)38 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)36 DataType (org.knime.core.data.DataType)34 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)32 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)27 DoubleValue (org.knime.core.data.DoubleValue)26 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)26 TreeEnsembleModelPortObjectSpec (org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec)25