Search in sources :

Example 16 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]) {
                    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 : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleValue(org.knime.core.data.DoubleValue) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory) DataRow(org.knime.core.data.DataRow)

Example 17 with ExecutionMonitor

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

the class MissingValueHandling3Table method createMissingValueHandlingTable.

/**
 * Does missing value handling to the argument table given the col settings in an array and also reports progress.
 *
 * @param table the table to do missing value handling on
 * @param colSettings the settings
 * @param exec for progress/cancel and to create the buffered data table
 * @param warningBuffer To which potential warning messages are added.
 * @return a cache table, cleaned up
 * @throws CanceledExecutionException if canceled
 * @since 2.10
 */
public static BufferedDataTable createMissingValueHandlingTable(final BufferedDataTable table, final MissingValueHandling2ColSetting[] colSettings, final ExecutionContext exec, final StringBuilder warningBuffer) throws CanceledExecutionException {
    MissingValueHandling2ColSetting[] colSetting;
    try {
        colSetting = getColSetting(table.getDataTableSpec(), colSettings, false, warningBuffer);
    } catch (InvalidSettingsException ise) {
        LOGGER.coding("getColSetting method is not supposed to throw an exception, ignoring settings", ise);
        DataTableSpec s = table.getDataTableSpec();
        colSetting = new MissingValueHandling2ColSetting[s.getNumColumns()];
        for (int i = 0; i < s.getNumColumns(); i++) {
            colSetting[i] = new MissingValueHandling2ColSetting(s.getColumnSpec(i));
            colSetting[i].setMethod(MissingValueHandling2ColSetting.METHOD_NO_HANDLING);
        }
    }
    boolean needStatistics = false;
    final Set<Integer> mostFrequentColumns = new HashSet<Integer>();
    for (int i = 0; i < colSetting.length; i++) {
        MissingValueHandling2ColSetting c = colSetting[i];
        switch(c.getMethod()) {
            case MissingValueHandling2ColSetting.METHOD_MOST_FREQUENT:
                mostFrequentColumns.add(i);
            case MissingValueHandling2ColSetting.METHOD_MAX:
            case MissingValueHandling2ColSetting.METHOD_MIN:
            case MissingValueHandling2ColSetting.METHOD_MEAN:
                needStatistics = true;
                break;
            default:
        }
    }
    MyStatisticsTable myT;
    ExecutionMonitor e;
    if (needStatistics) {
        // for creating statistics table
        ExecutionContext subExec = exec.createSubExecutionContext(0.5);
        myT = new MyStatisticsTable(table, subExec) {

            // do not try to get this Iterable in the constructor, it will not work, as long as
            // Statistics3Table does the statistical computation in the constructor.
            @Override
            protected Iterable<Integer> getMostFrequentColumns() {
                return mostFrequentColumns;
            }
        };
        if (myT.m_warningMessage != null) {
            if (warningBuffer.length() > 0) {
                warningBuffer.append('\n');
            }
            warningBuffer.append(myT.m_warningMessage);
        }
        // for the iterator
        e = exec.createSubProgress(0.5);
    } else {
        myT = null;
        e = exec;
    }
    MissingValueHandling3Table mvht = new MissingValueHandling3Table(table, myT, colSetting);
    BufferedDataContainer container = exec.createDataContainer(mvht.getDataTableSpec());
    e.setMessage("Adding rows...");
    int count = 0;
    try {
        MissingValueHandling3TableIterator it = new MissingValueHandling3TableIterator(mvht, e);
        while (it.hasNext()) {
            DataRow next;
            next = it.next();
            e.setMessage("Adding row " + (count + 1) + " (\"" + next.getKey() + "\")");
            container.addRowToTable(next);
            count++;
        }
    } catch (MissingValueHandling3TableIterator.RuntimeCanceledExecutionException rcee) {
        throw rcee.getCause();
    } finally {
        container.close();
    }
    return container.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataRow(org.knime.core.data.DataRow) ExecutionContext(org.knime.core.node.ExecutionContext) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 18 with ExecutionMonitor

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

the class DatabasePortObject method getDataTable.

/**
 * @return underlying data
 */
private DataTable getDataTable(final int cacheNoRows) {
    try {
        DatabaseQueryConnectionSettings connSettings = getConnectionSettings(m_credentials);
        DBReader load = connSettings.getUtility().getReader(connSettings);
        return load.getTable(new ExecutionMonitor(), m_credentials, false, cacheNoRows);
    } catch (Throwable t) {
        LOGGER.error("Could not fetch data from database, reason: " + t.getMessage(), t);
        return null;
    }
}
Also used : DBReader(org.knime.core.node.port.database.reader.DBReader) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 19 with ExecutionMonitor

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

the class AbstractTableSorter method sortInMemory.

private DataTable sortInMemory(final ExecutionMonitor exec) throws CanceledExecutionException {
    final DataTable dataTable = m_inputTable;
    List<DataRow> rowList = new ArrayList<DataRow>();
    int progress = 0;
    final long rowCount = m_rowsInInputTable;
    exec.setMessage("Reading data");
    ExecutionMonitor readExec = exec.createSubProgress(0.5);
    for (final DataRow r : dataTable) {
        readExec.checkCanceled();
        if (rowCount > 0) {
            readExec.setProgress(progress / (double) rowCount, r.getKey().getString());
        } else {
            readExec.setMessage(r.getKey() + " (row " + progress + ")");
        }
        rowList.add(r);
        progress++;
    }
    // "rowCount" as it might not be set)
    if (rowList.size() <= 1) {
        return m_inputTable;
    }
    exec.setMessage("Sorting");
    Collections.sort(rowList, m_rowComparator);
    exec.setMessage("Creating sorted table");
    final DataContainer dc = createDataContainer(dataTable.getDataTableSpec(), false);
    ExecutionMonitor writeExec = exec.createSubProgress(0.5);
    progress = 0;
    for (DataRow r : rowList) {
        exec.checkCanceled();
        if (rowCount > 0) {
            writeExec.setProgress(progress / (double) rowCount, r.getKey().getString());
        } else {
            writeExec.setMessage(r.getKey() + " (row " + progress + ")");
        }
        dc.addRowToTable(r);
        progress++;
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataContainer(org.knime.core.data.container.DataContainer) ArrayList(java.util.ArrayList) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DataRow(org.knime.core.data.DataRow)

Example 20 with ExecutionMonitor

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

the class SandboxedNodeCreator method copyIntoSandboxContainerRecursive.

/**
 * @param sourceWFM
 * @param targetWFM
 * @param wfmResult
 * @param progressMon
 * @param copyDataIntoNewContext
 * @throws CanceledExecutionException
 * @throws IOException
 */
private static void copyIntoSandboxContainerRecursive(final WorkflowManager sourceWFM, final WorkflowManager targetWFM, final WorkflowExecutionResult wfmResult, final ExecutionMonitor progressMon, final boolean copyDataIntoNewContext) throws CanceledExecutionException, IOException {
    assert wfmResult.getBaseID().equals(sourceWFM.getID());
    Map<NodeID, NodeContainerExecutionResult> resultMap = wfmResult.getExecutionResultMap();
    for (Map.Entry<NodeID, NodeContainerExecutionResult> e : resultMap.entrySet()) {
        ExecutionMonitor sub = progressMon.createSubProgress(1.0 / resultMap.size());
        NodeID sourceID = e.getKey();
        NodeContainerExecutionResult r = e.getValue();
        NodeID targetID = new NodeID(targetWFM.getID(), sourceID.getIndex());
        NodeContainer nextTarget = targetWFM.getNodeContainer(targetID);
        NodeContainer nextSource = sourceWFM.getNodeContainer(sourceID);
        progressMon.setMessage(nextSource.getNameWithID());
        copyExistingTablesIntoSandboxContainer(r, nextSource, nextTarget, sub, copyDataIntoNewContext);
        sub.setProgress(1.0);
    }
}
Also used : NativeNodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NativeNodeContainerExecutionResult) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) NodeID(org.knime.core.node.workflow.NodeID) NodeContainer(org.knime.core.node.workflow.NodeContainer) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Map(java.util.Map)

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