Search in sources :

Example 76 with CanceledExecutionException

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

the class TreeEnsembleLearner method learnEnsemble.

public TreeEnsembleModel learnEnsemble(final ExecutionMonitor exec) throws CanceledExecutionException, ExecutionException {
    final int nrModels = m_config.getNrModels();
    final RandomData rd = m_config.createRandomData();
    final ThreadPool tp = KNIMEConstants.GLOBAL_THREAD_POOL;
    final AtomicReference<Throwable> learnThrowableRef = new AtomicReference<Throwable>();
    @SuppressWarnings("unchecked") final Future<TreeLearnerResult>[] modelFutures = new Future[nrModels];
    final int procCount = 3 * Runtime.getRuntime().availableProcessors() / 2;
    final Semaphore semaphore = new Semaphore(procCount);
    Callable<TreeLearnerResult[]> learnCallable = new Callable<TreeLearnerResult[]>() {

        @Override
        public TreeLearnerResult[] call() throws Exception {
            final TreeLearnerResult[] results = new TreeLearnerResult[nrModels];
            for (int i = 0; i < nrModels; i++) {
                semaphore.acquire();
                finishedTree(i - procCount, exec);
                checkThrowable(learnThrowableRef);
                RandomData rdSingle = TreeEnsembleLearnerConfiguration.createRandomData(rd.nextLong(Long.MIN_VALUE, Long.MAX_VALUE));
                ExecutionMonitor subExec = exec.createSubProgress(0.0);
                modelFutures[i] = tp.enqueue(new TreeLearnerCallable(subExec, rdSingle, learnThrowableRef, semaphore));
            }
            for (int i = 0; i < procCount; i++) {
                semaphore.acquire();
                finishedTree(nrModels - 1 + i - procCount, exec);
            }
            for (int i = 0; i < nrModels; i++) {
                try {
                    results[i] = modelFutures[i].get();
                } catch (Exception e) {
                    learnThrowableRef.compareAndSet(null, e);
                }
            }
            return results;
        }

        private void finishedTree(final int treeIndex, final ExecutionMonitor progMon) {
            if (treeIndex > 0) {
                progMon.setProgress(treeIndex / (double) nrModels, "Tree " + treeIndex + "/" + nrModels);
            }
        }
    };
    TreeLearnerResult[] modelResults = tp.runInvisible(learnCallable);
    checkThrowable(learnThrowableRef);
    AbstractTreeModel[] models = new AbstractTreeModel[nrModels];
    m_rowSamples = new RowSample[nrModels];
    m_columnSampleStrategies = new ColumnSampleStrategy[nrModels];
    for (int i = 0; i < nrModels; i++) {
        models[i] = modelResults[i].m_treeModel;
        m_rowSamples[i] = modelResults[i].m_rowSample;
        m_columnSampleStrategies[i] = modelResults[i].m_rootColumnSampleStrategy;
    }
    m_ensembleModel = new TreeEnsembleModel(m_config, m_data.getMetaData(), models, m_data.getTreeType());
    return m_ensembleModel;
}
Also used : RandomData(org.apache.commons.math.random.RandomData) TreeEnsembleModel(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModel) ThreadPool(org.knime.core.util.ThreadPool) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractTreeModel(org.knime.base.node.mine.treeensemble2.model.AbstractTreeModel) Semaphore(java.util.concurrent.Semaphore) Callable(java.util.concurrent.Callable) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 77 with CanceledExecutionException

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

the class RearrangeColumnsTable method calcNewColsASynchronously.

/**
 * Processes input concurrently using a {@link ConcurrentNewColCalculator}.
 */
private static void calcNewColsASynchronously(final BufferedDataTable table, final ExecutionMonitor subProgress, final NewColumnsProducerMapping newColsProducerMapping, final DataContainer container) throws CanceledExecutionException {
    long finalRowCount = table.size();
    CellFactory facForProgress = null;
    int workers = Integer.MAX_VALUE;
    int queueSize = Integer.MAX_VALUE;
    Collection<SpecAndFactoryObject> newColsFactories = newColsProducerMapping.getAllNewColumnsList();
    for (SpecAndFactoryObject specAndFac : newColsFactories) {
        if (specAndFac.getFactory() instanceof AbstractCellFactory) {
            AbstractCellFactory acf = (AbstractCellFactory) specAndFac.getFactory();
            workers = Math.min(workers, acf.getMaxParallelWorkers());
            queueSize = Math.min(queueSize, acf.getMaxQueueSize());
        } else {
            throw new IllegalStateException("Coding problem: This method" + " should not have been called as the cell factories do not allow parallel processing");
        }
        if ((facForProgress == null) || !specAndFac.isNewColumn()) {
            facForProgress = specAndFac.getFactory();
        }
    }
    assert facForProgress != null;
    assert workers > 0 : "Nr workers <= 0: " + workers;
    assert queueSize > 0 : "queue size <= 0: " + queueSize;
    ConcurrentNewColCalculator calculator = new ConcurrentNewColCalculator(queueSize, workers, container, subProgress, finalRowCount, newColsProducerMapping, facForProgress);
    try {
        calculator.run(table);
    } catch (InterruptedException e) {
        CanceledExecutionException cee = new CanceledExecutionException(e.getMessage());
        cee.initCause(e);
        throw cee;
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause == null) {
            cause = e;
        }
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw new RuntimeException(cause);
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) SpecAndFactoryObject(org.knime.core.data.container.ColumnRearranger.SpecAndFactoryObject) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 78 with CanceledExecutionException

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

the class UpdateMetaNodeTemplateRunnable method run.

/**
 * {@inheritDoc}
 */
@Override
public void run(final IProgressMonitor pm) throws InterruptedException {
    m_newIDs = new ArrayList<NodeID>();
    m_undoPersistors = new ArrayList<WorkflowPersistor>();
    // create progress monitor
    ProgressHandler progressHandler = new ProgressHandler(pm, 101, "Updating node links...");
    final CheckCancelNodeProgressMonitor progressMonitor = new CheckCancelNodeProgressMonitor(pm);
    progressMonitor.addProgressListener(progressHandler);
    final Display d = Display.getDefault();
    ExecutionMonitor exec = new ExecutionMonitor(progressMonitor);
    IStatus[] stats = new IStatus[m_ids.length];
    for (int i = 0; i < m_ids.length; i++) {
        NodeID id = m_ids[i];
        NodeContainerTemplate tnc = (NodeContainerTemplate) m_parentWFM.findNodeContainer(id);
        LOGGER.debug("Updating " + tnc.getNameWithID() + " from " + tnc.getTemplateInformation().getSourceURI());
        ExecutionMonitor subExec = exec.createSubProgress(1.0 / m_ids.length);
        String progMsg = "Node Link \"" + tnc.getNameWithID() + "\"";
        exec.setMessage(progMsg);
        GUIWorkflowLoadHelper loadHelper = new GUIWorkflowLoadHelper(d, progMsg, null, null, null, true);
        NodeContainerTemplateLinkUpdateResult updateMetaNodeLinkResult;
        try {
            updateMetaNodeLinkResult = tnc.getParent().updateMetaNodeLink(id, subExec, loadHelper);
        } catch (CanceledExecutionException e) {
            String message = "Node update canceled";
            LOGGER.warn(message, e);
            throw new InterruptedException(message);
        }
        WorkflowPersistor p = updateMetaNodeLinkResult.getUndoPersistor();
        if (p != null) {
            // no error
            m_newIDs.add(updateMetaNodeLinkResult.getNCTemplate().getID());
            m_undoPersistors.add(p);
        }
        // metanodes don't have data
        // data load errors are unexpected but OK
        IStatus status = createStatus(updateMetaNodeLinkResult, true);
        subExec.setProgress(1.0);
        switch(status.getSeverity()) {
            case IStatus.OK:
                break;
            case IStatus.WARNING:
                logPreseveLineBreaks("Warnings during load: " + updateMetaNodeLinkResult.getFilteredError("", LoadResultEntryType.Warning), false);
                break;
            default:
                logPreseveLineBreaks("Errors during load: " + updateMetaNodeLinkResult.getFilteredError("", LoadResultEntryType.Warning), true);
        }
        stats[i] = status;
    }
    pm.done();
    final IStatus status = createMultiStatus("Update node links", stats);
    final String message;
    switch(status.getSeverity()) {
        case IStatus.OK:
            message = "No problems during node link update.";
            break;
        case IStatus.WARNING:
            message = "Warnings during node link update";
            break;
        default:
            message = "Errors during node link update";
    }
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            // will not open if status is OK.
            ErrorDialog.openError(Display.getDefault().getActiveShell(), "Update Node Links", message, status);
        }
    });
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) WorkflowPersistor(org.knime.core.node.workflow.WorkflowPersistor) NodeContainerTemplateLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.NodeContainerTemplateLinkUpdateResult) NodeContainerTemplate(org.knime.core.node.workflow.NodeContainerTemplate) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NodeID(org.knime.core.node.workflow.NodeID) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Display(org.eclipse.swt.widgets.Display)

Example 79 with CanceledExecutionException

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

the class ScorerNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File internDir, final ExecutionMonitor exec) throws IOException {
    // save internal values
    NodeSettings sett = new NodeSettings("ignored");
    sett.addInt(CFG_CORRECT_COUNT, m_correctCount);
    sett.addInt(CFG_FALSE_COUNT, m_falseCount);
    sett.addInt(CFG_NUMBER_ROWS, m_nrRows);
    File fSett = new File(internDir, FILE_NAME_INTERNAL_SETT);
    sett.saveToXML(new FileOutputStream(fSett));
    // save scored table
    File fTable = new File(internDir, FILE_NAME_INTERNAL_RESULT);
    try {
        DataContainer.writeToZip(m_lastResult, fTable, exec);
    } catch (CanceledExecutionException cee) {
        IOException ioe = new IOException();
        ioe.initCause(cee);
        throw ioe;
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 80 with CanceledExecutionException

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

the class ScorerNodeModel method execute.

/**
 * Starts the scoring in the scorer.
 *
 * @param data the input data of length one
 * @param exec the execution monitor
 * @return the confusion matrix
 * @throws CanceledExecutionException if user canceled execution
 *
 * @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] data, final ExecutionContext exec) throws CanceledExecutionException {
    // check input data
    assert (data != null && data.length == 1 && data[INPORT] != null);
    // blow away result from last execute (should have been reset anyway)
    m_lastResult = null;
    // first try to figure out what are the different class values
    // in the two respective columns
    BufferedDataTable in = data[INPORT];
    DataTableSpec inSpec = in.getDataTableSpec();
    final int index1 = inSpec.findColumnIndex(m_firstCompareColumn);
    final int index2 = inSpec.findColumnIndex(m_secondCompareColumn);
    // two elements, first is column names, second row names;
    // these arrays are ordered already, i.e. if both columns have
    // cells in common (e.g. both have Iris-Setosa), they get the same
    // index in the array. thus, the high numbers should appear
    // in the diagonal
    String[] values = determineColValues(in, index1, index2, exec);
    List<String> valuesList = Arrays.asList(values);
    m_correctCount = 0;
    m_falseCount = 0;
    int[][] scorerCount = new int[values.length][values.length];
    int rowNr = 0;
    for (Iterator<DataRow> it = in.iterator(); it.hasNext(); rowNr++) {
        DataRow row = it.next();
        exec.setProgress(rowNr / (double) in.getRowCount(), "Computing score, row " + rowNr + " (\"" + row.getKey() + "\") of " + in.getRowCount());
        try {
            exec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            reset();
            throw cee;
        }
        DataCell cell1 = row.getCell(index1);
        DataCell cell2 = row.getCell(index2);
        if (cell1.isMissing() || cell2.isMissing()) {
            continue;
        }
        boolean areEqual = cell1.equals(cell2);
        // need to cast to string (column keys are strings!)
        int i1 = valuesList.indexOf(cell1.toString());
        int i2 = areEqual ? i1 : valuesList.indexOf(cell2.toString());
        assert i1 >= 0 : "column spec lacks possible value " + cell1;
        assert i2 >= 0 : "column spec lacks possible value " + cell2;
        // i2 must be equal to i1 if cells are equal (implication)
        assert (!areEqual || i1 == valuesList.indexOf(cell2.toString()));
        scorerCount[i1][i2]++;
        if (areEqual) {
            m_correctCount++;
        } else {
            m_falseCount++;
        }
    }
    DataType[] colTypes = new DataType[values.length];
    Arrays.fill(colTypes, IntCell.TYPE);
    m_nrRows = rowNr;
    DataRow[] rows = new DataRow[values.length];
    BufferedDataContainer dc = exec.createDataContainer(new DataTableSpec(values, colTypes));
    for (int i = 0; i < rows.length; i++) {
        // need to make a data cell for the row key
        dc.addRowToTable(new DefaultRow(values[i], scorerCount[i]));
    }
    dc.close();
    // print info
    int correct = getCorrectCount();
    int incorrect = getFalseCount();
    double error = getError();
    int nrRows = getNrRows();
    int missing = nrRows - correct - incorrect;
    LOGGER.info("error=" + error + ", #correct=" + correct + ", #false=" + incorrect + ", #rows=" + nrRows + ", #missing=" + missing);
    // our view displays the table - we must keep a reference in the model.
    BufferedDataTable bufTable = dc.getTable();
    m_lastResult = bufTable;
    return new BufferedDataTable[] { bufTable };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataRow(org.knime.core.data.DataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

CanceledExecutionException (org.knime.core.node.CanceledExecutionException)82 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)34 IOException (java.io.IOException)32 File (java.io.File)21 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)21 DataRow (org.knime.core.data.DataRow)20 DataTableSpec (org.knime.core.data.DataTableSpec)20 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 DataCell (org.knime.core.data.DataCell)19 ArrayList (java.util.ArrayList)11 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)10 LinkedHashMap (java.util.LinkedHashMap)9 ExecutionException (java.util.concurrent.ExecutionException)9 DefaultRow (org.knime.core.data.def.DefaultRow)9 RowKey (org.knime.core.data.RowKey)8 BufferedWriter (java.io.BufferedWriter)7 FileInputStream (java.io.FileInputStream)7 Map (java.util.Map)7 Future (java.util.concurrent.Future)7