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());
}
}
};
}
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;
}
}
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()) };
}
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;
}
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;
}
Aggregations