Search in sources :

Example 51 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class SmoteNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    BufferedDataTable in = inData[0];
    Random rand;
    if (m_seed != null) {
        rand = new Random(m_seed);
    } else {
        rand = new Random();
    }
    Smoter smoter = new Smoter(in, m_class, exec, rand);
    if (m_method.equals(METHOD_ALL)) {
        // count number of rows to add
        int nrRowsToAdd = 0;
        for (Iterator<DataCell> it = smoter.getClassValues(); it.hasNext(); ) {
            int count = smoter.getCount(it.next());
            nrRowsToAdd += (int) (count * m_rate);
        }
        for (Iterator<DataCell> it = smoter.getClassValues(); it.hasNext(); ) {
            DataCell cur = it.next();
            int count = smoter.getCount(cur);
            int newCount = (int) (count * m_rate);
            exec.setMessage("Smoting '" + cur.toString() + "'");
            ExecutionMonitor subExec = exec.createSubProgress(newCount / (double) nrRowsToAdd);
            smoter.smote(cur, newCount, m_kNN, subExec);
        }
    } else if (m_method.equals(METHOD_MAJORITY)) {
        DataCell majority = smoter.getMajorityClass();
        int majorityCount = smoter.getCount(majority);
        Iterator<DataCell> it = smoter.getClassValues();
        int nrRowsToAdd = 0;
        while (it.hasNext()) {
            DataCell cur = it.next();
            nrRowsToAdd += (majorityCount - smoter.getCount(cur));
        }
        it = smoter.getClassValues();
        while (it.hasNext()) {
            DataCell cur = it.next();
            int count = smoter.getCount(cur);
            int newCount = majorityCount - count;
            exec.setMessage("Smoting '" + cur.toString() + "'");
            ExecutionMonitor subExec = exec.createSubProgress(newCount / (double) nrRowsToAdd);
            smoter.smote(cur, newCount, m_kNN, subExec);
        }
    }
    smoter.close();
    DataTable out = smoter.getSmotedTable();
    return new BufferedDataTable[] { exec.createBufferedDataTable(out, exec) };
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) Random(java.util.Random) BufferedDataTable(org.knime.core.node.BufferedDataTable) Iterator(java.util.Iterator) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 52 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class AggregateOutputNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // retrieve variables from the stack which the head of this
    // loop hopefully put there:
    int count;
    int maxCount;
    try {
        count = peekFlowVariableInt("currentIteration");
        maxCount = peekFlowVariableInt("maxIterations");
    } catch (NoSuchElementException e) {
        throw new Exception("No matching Loop Start node!", e);
    }
    if (count < 0 || count >= maxCount) {
        throw new Exception("Conflicting loop variables, count is " + count + " and max count is " + maxCount);
    }
    final BufferedDataTable in = inData[0];
    final DataTableSpec inSpec = in.getDataTableSpec();
    if (count == 0) {
        m_firstIterationSpec = in.getDataTableSpec();
        m_predictionTable = exec.createDataContainer(createPredictionSpec(in.getDataTableSpec()));
    } else if (m_predictionTable == null) {
        throw new Exception("Loop Head claims this is NOT the first iteration" + " but the tail believes it is?!");
    } else {
        if (!inSpec.equalStructure(m_firstIterationSpec)) {
            StringBuilder error = new StringBuilder("Input table's structure differs from reference " + "(first iteration) table: ");
            if (inSpec.getNumColumns() != m_firstIterationSpec.getNumColumns()) {
                error.append("different column counts ");
                error.append(inSpec.getNumColumns());
                error.append(" vs. ").append(m_firstIterationSpec.getNumColumns());
            } else {
                for (int i = 0; i < inSpec.getNumColumns(); i++) {
                    DataColumnSpec inCol = inSpec.getColumnSpec(i);
                    DataColumnSpec predCol = m_firstIterationSpec.getColumnSpec(i);
                    if (!inCol.equalStructure(predCol)) {
                        error.append("Column ").append(i).append(" [");
                        error.append(inCol).append("] vs. [");
                        error.append(predCol).append("]");
                    }
                }
            }
            throw new IllegalArgumentException(error.toString());
        }
    }
    final int rowCount = in.getRowCount();
    final int targetColIndex = in.getDataTableSpec().findColumnIndex(m_settings.targetColumn());
    final int predictColIndex = in.getDataTableSpec().findColumnIndex(m_settings.predictionColumn());
    final boolean numericMode = in.getDataTableSpec().getColumnSpec(predictColIndex).getType().isCompatible(DoubleValue.class);
    ExecutionMonitor subExec = exec.createSubProgress(count == maxCount - 1 ? 0.9 : 1);
    final DataCell foldNumber = new IntCell(m_foldStatistics.size());
    if (numericMode) {
        double errorSum = 0;
        int r = 0;
        for (DataRow row : in) {
            RowKey key = row.getKey();
            DoubleValue target = (DoubleValue) row.getCell(targetColIndex);
            DoubleValue predict = (DoubleValue) row.getCell(predictColIndex);
            double d = (target.getDoubleValue() - predict.getDoubleValue());
            errorSum += d * d;
            r++;
            if (m_settings.addFoldId()) {
                m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
            } else {
                m_predictionTable.addRowToTable(row);
            }
            subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
            subExec.checkCanceled();
        }
        DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(errorSum), new DoubleCell(errorSum / rowCount), new IntCell(rowCount));
        m_foldStatistics.add(stats);
    } else {
        int incorrect = 0;
        int r = 0;
        for (DataRow row : in) {
            RowKey key = row.getKey();
            DataCell target = row.getCell(targetColIndex);
            DataCell predict = row.getCell(predictColIndex);
            if (!target.equals(predict)) {
                incorrect++;
            }
            r++;
            if (m_settings.addFoldId()) {
                m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
            } else {
                m_predictionTable.addRowToTable(row);
            }
            subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
            subExec.checkCanceled();
        }
        DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(100.0 * incorrect / rowCount), new IntCell(rowCount), new IntCell(incorrect));
        m_foldStatistics.add(stats);
    }
    if (count < maxCount - 1) {
        continueLoop();
        return new BufferedDataTable[2];
    } else {
        BufferedDataContainer cont = exec.createDataContainer(numericMode ? NUMERIC_STATISTICS_SPEC : NOMINAL_STATISTICS_SPEC);
        for (DataRow row : m_foldStatistics) {
            cont.addRowToTable(row);
        }
        cont.close();
        m_predictionTable.close();
        return new BufferedDataTable[] { m_predictionTable.getTable(), cont.getTable() };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) IntCell(org.knime.core.data.def.IntCell) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) AppendedColumnRow(org.knime.base.data.append.column.AppendedColumnRow)

Example 53 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class PCAReverseNodeModel method execute.

/**
 * Performs the PCA.
 *
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final PCAModelPortObject model = (PCAModelPortObject) inData[MODEL_INPORT];
    final Matrix eigenvectors = EigenValue.getSortedEigenVectors(model.getEigenVectors(), model.getEigenvalues(), m_inputColumnIndices.length);
    if (m_failOnMissingValues.getBooleanValue()) {
        for (final DataRow row : (DataTable) inData[DATA_INPORT]) {
            for (int i = 0; i < m_inputColumnIndices.length; i++) {
                if (row.getCell(m_inputColumnIndices[i]).isMissing()) {
                    throw new IllegalArgumentException("data table contains missing values");
                }
            }
        }
    }
    final String[] origColumnNames = ((PCAModelPortObjectSpec) ((PCAModelPortObject) inData[MODEL_INPORT]).getSpec()).getColumnNames();
    final DataColumnSpec[] specs = createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), origColumnNames);
    final CellFactory fac = new CellFactory() {

        @Override
        public DataCell[] getCells(final DataRow row) {
            return convertInputRow(eigenvectors, row, model.getCenter(), m_inputColumnIndices, origColumnNames.length);
        }

        @Override
        public DataColumnSpec[] getColumnSpecs() {
            return specs;
        }

        @Override
        public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor texec) {
            texec.setProgress((double) curRowNr / rowCount);
        }
    };
    final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[DATA_INPORT].getSpec());
    cr.append(fac);
    if (m_removePCACols.getBooleanValue()) {
        cr.remove(m_inputColumnIndices);
    }
    final BufferedDataTable result = exec.createColumnRearrangeTable((BufferedDataTable) inData[DATA_INPORT], cr, exec);
    final PortObject[] out = { result };
    return out;
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) RowKey(org.knime.core.data.RowKey) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) DataRow(org.knime.core.data.DataRow) Matrix(Jama.Matrix) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory) PortObject(org.knime.core.node.port.PortObject)

Example 54 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class PolyRegLearnerNodeModel method getCellFactory.

private CellFactory getCellFactory(final int dependentIndex) {
    final int degree = m_settings.getDegree();
    return new CellFactory() {

        @Override
        public DataCell[] getCells(final DataRow row) {
            double sum = m_betas[0];
            int betaCount = 1;
            double y = 0;
            for (int col = 0; col < row.getNumCells(); col++) {
                if ((col == dependentIndex || m_colSelected[col]) && row.getCell(col).isMissing()) {
                    switch(m_settings.getMissingValueHandling()) {
                        case ignore:
                            return new DataCell[] { DataType.getMissingCell(), DataType.getMissingCell() };
                        case fail:
                            throw new IllegalStateException("Should failed earlier!");
                        default:
                            throw new UnsupportedOperationException("Not supported missing handling strategy: " + m_settings.getMissingValueHandling());
                    }
                }
                if ((col != dependentIndex) && m_colSelected[col]) {
                    final double value = ((DoubleValue) row.getCell(col)).getDoubleValue();
                    double poly = 1;
                    for (int d = 1; d <= degree; d++) {
                        poly *= value;
                        sum += m_betas[betaCount++] * poly;
                    }
                } else if (col == dependentIndex) {
                    y = ((DoubleValue) row.getCell(col)).getDoubleValue();
                }
            }
            double err = Math.abs(sum - y);
            m_squaredError += err * err;
            return new DataCell[] { new DoubleCell(sum), new DoubleCell(err) };
        }

        @Override
        public DataColumnSpec[] getColumnSpecs() {
            DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
            DataColumnSpec col1 = crea.createSpec();
            crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
            DataColumnSpec col2 = crea.createSpec();
            return new DataColumnSpec[] { col1, col2 };
        }

        @Override
        public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor execMon) {
        // do nothing
        }
    };
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory)

Example 55 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class MDSProjectionManager method train.

/**
 * Does the training by adjusting the lower dimensional data points
 * according to their distances and the distances of the original data.
 *
 * @param epochs The number of epochs to train.
 * @param learningrate The learn rate, specifying the step size of
 * adjustment.
 * @throws CanceledExecutionException If execution was canceled by the user.
 */
public void train(final int epochs, final double learningrate) throws CanceledExecutionException {
    if (!m_isInit) {
        init(DEFAULT_SEED);
    }
    ExecutionMonitor exec = m_exec.createSubProgress(0.9);
    m_learningrate = learningrate;
    m_initialLearningrate = learningrate;
    m_epochs = epochs;
    exec.setMessage("Start training");
    for (int e = 1; e <= epochs; e++) {
        exec.checkCanceled();
        doEpoch(e, exec);
        double prog = (double) e / (double) epochs;
        exec.setProgress(prog, "Training epoch " + e + " of " + epochs);
    }
}
Also used : ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DataPoint(org.knime.base.node.mine.mds.DataPoint)

Aggregations

ExecutionMonitor (org.knime.core.node.ExecutionMonitor)160 BufferedDataTable (org.knime.core.node.BufferedDataTable)50 DataTableSpec (org.knime.core.data.DataTableSpec)43 DataRow (org.knime.core.data.DataRow)39 DataCell (org.knime.core.data.DataCell)35 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)35 Test (org.junit.Test)33 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)33 File (java.io.File)29 IOException (java.io.IOException)25 PortObject (org.knime.core.node.port.PortObject)25 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 RowKey (org.knime.core.data.RowKey)20 ArrayList (java.util.ArrayList)19 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)17 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)16 ExecutionException (java.util.concurrent.ExecutionException)14 ExecutionContext (org.knime.core.node.ExecutionContext)13 FileOutputStream (java.io.FileOutputStream)12