Search in sources :

Example 71 with CanceledExecutionException

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

the class SubsetMatcherNodeModel method createRunnable.

private Runnable createRunnable(final ExecutionMonitor exec, final long noOfSets, final DataCell setIDCell, final CollectionDataValue setCell, final boolean appendSetCol, final Comparator<DataCell> comparator, final SubsetMatcher[] sortedMatcher, final int maxMismatches) {
    return new Runnable() {

        @Override
        public void run() {
            try {
                // check cancel prior updating the global counter!!!
                exec.checkCanceled();
                final long transCounter = m_setCounter.incrementAndGet();
                exec.setMessage(setIDCell.toString() + " (" + transCounter + " of " + noOfSets + ")");
                if (setCell.size() < 1) {
                    // skip empty collections
                    exec.setProgress(transCounter / (double) noOfSets);
                    m_skipCounter.incrementAndGet();
                    return;
                }
                final DataCell[] sortedItems = collectionCell2SortedArray(setCell, comparator);
                // try to match the sorted transaction items and the sorted
                // matcher until all items or all matchers are processed
                int matcherStartIdx = 0;
                int itemIdx = 0;
                final Collection<SetMissmatches> matchingSets = new LinkedList<>();
                while (itemIdx < sortedItems.length && matcherStartIdx < sortedMatcher.length) {
                    final DataCell subItem = sortedItems[itemIdx];
                    // match the current item with all remaining matchers
                    for (int i = matcherStartIdx; i < sortedMatcher.length; i++) {
                        final SubsetMatcher matcher = sortedMatcher[i];
                        final int result = matcher.compare(subItem);
                        if (result > 0 && maxMismatches == 0) {
                            // the next item
                            break;
                        }
                        // this matcher matches the item (result == 0)
                        // or
                        // the item is bigger than the matcher
                        // since we may allow for mismatches we have to
                        // check if subsequent matchers of the matcher
                        // match the current item
                        matcher.match(sortedItems, itemIdx, matchingSets, new LinkedList<DataCell>(), new MismatchCounter(maxMismatches));
                        matcherStartIdx++;
                    }
                    // go to the next index
                    itemIdx++;
                }
                if (matchingSets.size() < 1) {
                    exec.setProgress(transCounter / (double) noOfSets);
                    m_skipCounter.incrementAndGet();
                    return;
                }
                for (final SetMissmatches matchingSet : matchingSets) {
                    exec.checkCanceled();
                    // create for each matching subset a result row
                    final List<DataCell> cells = new LinkedList<>();
                    cells.add(setIDCell);
                    if (appendSetCol) {
                        cells.add((DataCell) setCell);
                    }
                    // the subset column
                    cells.add(matchingSet.getSet());
                    if (maxMismatches > 0) {
                        // append the mismatch counter if mismatches allowed
                        cells.add(new IntCell(matchingSet.getMismatchCounter()));
                    }
                    final RowKey rowKey = RowKey.createRowKey(m_rowId.getAndIncrement());
                    final DefaultRow row = new DefaultRow(rowKey, cells);
                    synchronized (m_dc) {
                        exec.checkCanceled();
                        m_dc.addRowToTable(row);
                    }
                }
                exec.setProgress(transCounter / (double) noOfSets);
            } catch (final CanceledExecutionException e) {
            // ignore this just exit the run method
            } catch (final Exception e) {
                LOGGER.error("Exception while matching sub sets: " + e.getMessage());
            }
        }
    };
}
Also used : RowKey(org.knime.core.data.RowKey) LinkedList(java.util.LinkedList) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) IntCell(org.knime.core.data.def.IntCell) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 72 with CanceledExecutionException

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

the class DiscretizationModel method load.

/**
 * {@inheritDoc}
 * <p />
 * IMPORTANT NOTE: This method DOES NOT load the table spec of included
 * columns from the provided model. This table spec must be loaded before
 * and must be provided here. It is taken over and stored.
 */
@Override
protected void load(final ModelContentRO model, final PortObjectSpec spec, final ExecutionMonitor exec) throws InvalidSettingsException, CanceledExecutionException {
    m_includedColumnNames = (DataTableSpec) spec;
    try {
        Config schemesConfig = model.getConfig(CONFIG_KEY_SCHEMES);
        m_schemes = new DiscretizationScheme[schemesConfig.getChildCount()];
        int i = 0;
        Enumeration<TreeNode> schemeConfigEnum = schemesConfig.children();
        while (schemeConfigEnum.hasMoreElements()) {
            if (exec != null) {
                exec.checkCanceled();
            }
            Config schemeConfig = (Config) schemeConfigEnum.nextElement();
            m_schemes[i] = new DiscretizationScheme(schemeConfig);
            i++;
        }
    } catch (Exception e) {
        m_schemes = null;
        m_includedColumnNames = null;
    }
}
Also used : Config(org.knime.core.node.config.Config) TreeNode(javax.swing.tree.TreeNode) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException)

Example 73 with CanceledExecutionException

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

the class EditNominalDomainDicNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable additionalValues = inData[1];
    final DataTableSpec orgSpec = inData[0].getDataTableSpec();
    final DataTableSpec valueSpec = additionalValues.getDataTableSpec();
    Map<Integer, Set<DataCell>> doCreateSpec = createNewSpec(orgSpec, valueSpec, new NewDomainValuesAdder<CanceledExecutionException>() {

        private long m_currentRowIndex = 0;

        private final long m_size = additionalValues.size();

        @Override
        void addNewDomainValues(final Map<Integer, String> orgIndexToColNameMap, final Map<Integer, Set<DataCell>> orgIndexToNewDomainValuesMap) throws CanceledExecutionException {
            CloseableRowIterator addValuesIterator = additionalValues.iterator();
            try {
                Collection<String> values = orgIndexToColNameMap.values();
                while (addValuesIterator.hasNext()) {
                    DataRow currRow = addValuesIterator.next();
                    // 
                    exec.setProgress(// 
                    m_currentRowIndex++ / (double) m_size, "adding values to domain of row: " + currRow.getKey().getString());
                    for (String addValRowId : values) {
                        DataCell cell = currRow.getCell(valueSpec.findColumnIndex(addValRowId));
                        if (!cell.isMissing()) {
                            orgIndexToNewDomainValuesMap.get(orgSpec.findColumnIndex(addValRowId)).add(cell);
                        }
                    }
                    exec.checkCanceled();
                }
            } finally {
                addValuesIterator.close();
            }
        }
    });
    return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], mergeTableSpecs(orgSpec, doCreateSpec).createSpec()) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) Collection(java.util.Collection) DataCell(org.knime.core.data.DataCell)

Example 74 with CanceledExecutionException

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

the class Learner method perform.

/**
 * @param data The data table.
 * @param exec The execution context used for reporting progress.
 * @return An object which holds the results.
 * @throws CanceledExecutionException when method is cancelled
 * @throws InvalidSettingsException When settings are inconsistent with the data
 */
public LogisticRegressionContent perform(final BufferedDataTable data, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    exec.checkCanceled();
    int iter = 0;
    boolean converged = false;
    final RegressionTrainingData trainingData = new RegressionTrainingData(data, m_outSpec, true, m_targetReferenceCategory, m_sortTargetCategories, m_sortFactorsCategories);
    int targetIndex = data.getDataTableSpec().findColumnIndex(m_outSpec.getTargetCols().get(0).getName());
    final int tcC = trainingData.getDomainValues().get(targetIndex).size();
    final int rC = trainingData.getRegressorCount();
    final RealMatrix beta = new Array2DRowRealMatrix(1, (tcC - 1) * (rC + 1));
    Double loglike = 0.0;
    Double loglikeOld = 0.0;
    exec.setMessage("Iterative optimization. Processing iteration 1.");
    // main loop
    while (iter < m_maxIter && !converged) {
        RealMatrix betaOld = beta.copy();
        loglikeOld = loglike;
        // Do heavy work in a separate thread which allows to interrupt it
        // note the queue may block if no more threads are available (e.g. thread count = 1)
        // as soon as we stall in 'get' this thread reduces the number of running thread
        Future<Double> future = ThreadPool.currentPool().enqueue(new Callable<Double>() {

            @Override
            public Double call() throws Exception {
                final ExecutionMonitor progMon = exec.createSubProgress(1.0 / m_maxIter);
                irlsRls(trainingData, beta, rC, tcC, progMon);
                progMon.setProgress(1.0);
                return likelihood(trainingData.iterator(), beta, rC, tcC, exec);
            }
        });
        try {
            loglike = future.get();
        } catch (InterruptedException e) {
            future.cancel(true);
            exec.checkCanceled();
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            } else {
                throw new RuntimeException(e.getCause());
            }
        }
        if (Double.isInfinite(loglike) || Double.isNaN(loglike)) {
            throw new RuntimeException(FAILING_MSG);
        }
        exec.checkCanceled();
        // test for decreasing likelihood
        while ((Double.isInfinite(loglike) || Double.isNaN(loglike) || loglike < loglikeOld) && iter > 0) {
            converged = true;
            for (int k = 0; k < beta.getRowDimension(); k++) {
                if (abs(beta.getEntry(k, 0) - betaOld.getEntry(k, 0)) > m_eps * abs(betaOld.getEntry(k, 0))) {
                    converged = false;
                    break;
                }
            }
            if (converged) {
                break;
            }
            // half the step size of beta
            beta.setSubMatrix((beta.add(betaOld)).scalarMultiply(0.5).getData(), 0, 0);
            exec.checkCanceled();
            loglike = likelihood(trainingData.iterator(), beta, rC, tcC, exec);
            exec.checkCanceled();
        }
        // test for convergence
        converged = true;
        for (int k = 0; k < beta.getRowDimension(); k++) {
            if (abs(beta.getEntry(k, 0) - betaOld.getEntry(k, 0)) > m_eps * abs(betaOld.getEntry(k, 0))) {
                converged = false;
                break;
            }
        }
        iter++;
        LOGGER.debug("#Iterations: " + iter);
        LOGGER.debug("Log Likelihood: " + loglike);
        StringBuilder betaBuilder = new StringBuilder();
        for (int i = 0; i < beta.getRowDimension() - 1; i++) {
            betaBuilder.append(Double.toString(beta.getEntry(i, 0)));
            betaBuilder.append(", ");
        }
        if (beta.getRowDimension() > 0) {
            betaBuilder.append(Double.toString(beta.getEntry(beta.getRowDimension() - 1, 0)));
        }
        LOGGER.debug("beta: " + betaBuilder.toString());
        exec.checkCanceled();
        exec.setMessage("Iterative optimization. #Iterations: " + iter + " | Log-likelihood: " + DoubleFormat.formatDouble(loglike) + ". Processing iteration " + (iter + 1) + ".");
    }
    // The covariance matrix
    RealMatrix covMat = new QRDecomposition(A).getSolver().getInverse().scalarMultiply(-1);
    List<String> factorList = new ArrayList<String>();
    List<String> covariateList = new ArrayList<String>();
    Map<String, List<DataCell>> factorDomainValues = new HashMap<String, List<DataCell>>();
    for (int i : trainingData.getActiveCols()) {
        if (trainingData.getIsNominal().get(i)) {
            String factor = data.getDataTableSpec().getColumnSpec(i).getName();
            factorList.add(factor);
            List<DataCell> values = trainingData.getDomainValues().get(i);
            factorDomainValues.put(factor, values);
        } else {
            covariateList.add(data.getDataTableSpec().getColumnSpec(i).getName());
        }
    }
    Matrix betaJama = new Matrix(beta.getData());
    Matrix covMatJama = new Matrix(covMat.getData());
    // create content
    LogisticRegressionContent content = new LogisticRegressionContent(m_outSpec, factorList, covariateList, m_targetReferenceCategory, m_sortTargetCategories, m_sortFactorsCategories, betaJama, loglike, covMatJama, iter);
    return content;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Matrix(Jama.Matrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RegressionTrainingData(org.knime.base.node.mine.regression.RegressionTrainingData) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) QRDecomposition(org.apache.commons.math3.linear.QRDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DataCell(org.knime.core.data.DataCell)

Example 75 with CanceledExecutionException

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

the class Proximity method calcProximities.

public static ProximityMatrix calcProximities(final BufferedDataTable[] tables, final TreeEnsembleModelPortObject modelPortObject, final ExecutionContext exec) throws InvalidSettingsException, InterruptedException, ExecutionException, CanceledExecutionException {
    ProximityMatrix proximityMatrix = null;
    boolean optionalTable = false;
    switch(tables.length) {
        case 1:
            if (tables[0].size() <= 65500) {
                proximityMatrix = new SingleTableProximityMatrix(tables[0]);
            } else {
                // this is unfortunate and we should maybe think of a different solution
                proximityMatrix = new TwoTablesProximityMatrix(tables[0], tables[0]);
            }
            break;
        case 2:
            optionalTable = true;
            proximityMatrix = new TwoTablesProximityMatrix(tables[0], tables[1]);
            break;
        default:
            throw new IllegalArgumentException("Currently only up to two tables are supported.");
    }
    final TreeEnsembleModelPortObjectSpec modelSpec = modelPortObject.getSpec();
    final TreeEnsembleModel ensembleModel = modelPortObject.getEnsembleModel();
    int[][] learnColIndicesInTables = null;
    if (optionalTable) {
        learnColIndicesInTables = new int[][] { modelSpec.calculateFilterIndices(tables[0].getDataTableSpec()), modelSpec.calculateFilterIndices(tables[1].getDataTableSpec()) };
    } else {
        learnColIndicesInTables = new int[][] { modelSpec.calculateFilterIndices(tables[0].getDataTableSpec()) };
    }
    final ThreadPool tp = KNIMEConstants.GLOBAL_THREAD_POOL;
    final int procCount = 3 * Runtime.getRuntime().availableProcessors() / 2;
    final Semaphore semaphore = new Semaphore(procCount);
    final AtomicReference<Throwable> proxThrowableRef = new AtomicReference<Throwable>();
    final int nrTrees = ensembleModel.getNrModels();
    final Future<?>[] calcFutures = new Future<?>[nrTrees];
    exec.setProgress(0, "Starting proximity calculation per tree.");
    for (int i = 0; i < nrTrees; i++) {
        semaphore.acquire();
        finishedTree(i, exec, nrTrees);
        checkThrowable(proxThrowableRef);
        AbstractTreeModel treeModel = ensembleModel.getTreeModel(i);
        ExecutionMonitor subExec = exec.createSubProgress(0.0);
        if (optionalTable) {
            calcFutures[i] = tp.enqueue(new TwoTablesProximityCalcRunnable(proximityMatrix, tables, learnColIndicesInTables, treeModel, modelPortObject, semaphore, proxThrowableRef, subExec));
        } else {
            calcFutures[i] = tp.enqueue(new SingleTableProximityCalcRunnable(proximityMatrix, tables, learnColIndicesInTables, treeModel, modelPortObject, semaphore, proxThrowableRef, subExec));
        }
    }
    for (int i = 0; i < procCount; i++) {
        semaphore.acquire();
        finishedTree(nrTrees - procCount + i, exec, nrTrees);
    }
    for (Future<?> future : calcFutures) {
        try {
            future.get();
        } catch (Exception e) {
            proxThrowableRef.compareAndSet(null, e);
        }
    }
    checkThrowable(proxThrowableRef);
    proximityMatrix.normalize(1.0 / nrTrees);
    return proximityMatrix;
}
Also used : TreeEnsembleModel(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModel) TreeEnsembleModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec) 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) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

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