use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class AppendedRowsIterator method initNextRow.
/**
* Get next row internally.
*/
private void initNextRow() {
// reached end of table's iterator - take next
if (!m_curIterator.hasNext()) {
do {
if (m_curItIndex < m_iteratorSuppliers.length - 1) {
initNextTable();
} else {
// final end
m_nextRow = null;
// reached end of this table
return;
}
} while (!m_curIterator.hasNext());
}
// row from table
DataRow baseRow = m_curIterator.next();
m_curRowIndex++;
boolean keyHasChanged = false;
RowKey origKey = baseRow.getKey();
RowKey key = origKey;
while (m_duplicateMap.containsKey(key)) {
if (m_exec != null) {
try {
m_exec.checkCanceled();
} catch (CanceledExecutionException cee) {
throw new RuntimeCanceledExecutionException(cee);
}
}
switch(m_duplPolicy) {
case Fail:
assert false : "Duplicate checking is done in the BDT";
throw new RuntimeException("Duplicate key \"" + key + "\"");
case Skip:
if (!m_hasPrintedError) {
LOGGER.warn("Table contains duplicate entry \"" + key.toString() + "\", skipping this row. " + "Suppress further warnings.");
m_hasPrintedError = true;
}
if (!m_curIterator.hasNext()) {
// end of one table reached
// note, this causes one more call on the stack
// (but who wants to concatenate 60000 tables...)
initNextRow();
return;
}
if (m_exec != null) {
m_nrRowsSkipped++;
String message = "Skipping row " + m_curRowIndex + " (\"" + key.toString() + "\")";
if (m_totalRowCount > 0L) {
m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
} else {
m_exec.setMessage(message);
}
}
// row from table
baseRow = m_curIterator.next();
m_curRowIndex++;
// stays false! rows have been skipped.
keyHasChanged = false;
origKey = baseRow.getKey();
key = origKey;
break;
case AppendSuffix:
// first time we come here
if (!keyHasChanged && m_exec != null) {
String message = "Unifying row " + m_curRowIndex + " (\"" + key.toString() + "\")";
if (m_totalRowCount > 0L) {
m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
} else {
m_exec.setMessage(message);
}
}
keyHasChanged = true;
String newId = key.toString() + m_suffix;
key = new RowKey(newId);
// to do duplicate handling.
break;
default:
throw new RuntimeException("Unknown policy: " + m_duplPolicy);
}
}
switch(m_duplPolicy) {
case Fail:
// to do a efficient duplicate checking
break;
default:
m_duplicateMap.put(key, origKey);
}
if (m_exec != null) {
try {
m_exec.checkCanceled();
} catch (CanceledExecutionException cee) {
throw new RuntimeCanceledExecutionException(cee);
}
String message = "Adding row " + m_curRowIndex + " (\"" + key.toString() + "\"" + (keyHasChanged ? " uniquified)" : ")");
if (m_totalRowCount > 0L) {
m_exec.setProgress(m_curRowIndex / (double) m_totalRowCount, message);
} else {
m_exec.setMessage(message);
}
}
DataRow nextRow;
if (m_curMissingCells != null) {
// no missing cells implies the base row is complete
assert (m_curMissingCells.length + baseRow.getNumCells() == m_spec.getNumColumns());
// row enlarged by "missing" columns
DataRow filledBaseRow = new AppendedColumnRow(baseRow, m_curMissingCells);
nextRow = new ResortedCellsRow(filledBaseRow, m_curMapping);
} else {
nextRow = baseRow;
}
if (keyHasChanged) {
DataCell[] cells = new DataCell[nextRow.getNumCells()];
for (int i = 0; i < cells.length; i++) {
cells[i] = nextRow.getCell(i);
}
m_nextRow = new DefaultRow(key, cells);
} else {
m_nextRow = nextRow;
}
}
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;
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class IrlsLearner method learn.
/**
* {@inheritDoc}
*/
@Override
public LogRegLearnerResult learn(final TrainingData<ClassificationTrainingRow> trainingData, final ExecutionMonitor exec) throws CanceledExecutionException, InvalidSettingsException {
exec.checkCanceled();
int iter = 0;
boolean converged = false;
final int tcC = trainingData.getTargetDimension() + 1;
final int rC = trainingData.getFeatureCount() - 1;
final RealMatrix beta = MatrixUtils.createRealMatrix(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.getColumnDimension(); k++) {
if (abs(beta.getEntry(0, k) - betaOld.getEntry(0, k)) > m_eps * abs(betaOld.getEntry(0, k))) {
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.getColumnDimension(); k++) {
if (abs(beta.getEntry(0, k) - betaOld.getEntry(0, k)) > m_eps * abs(betaOld.getEntry(0, k))) {
converged = false;
break;
}
}
iter++;
LOGGER.debug("#Iterations: " + iter);
LOGGER.debug("Log Likelihood: " + loglike);
StringBuilder betaBuilder = new StringBuilder();
for (int i = 0; i < beta.getColumnDimension() - 1; i++) {
betaBuilder.append(Double.toString(beta.getEntry(0, i)));
betaBuilder.append(", ");
}
if (beta.getColumnDimension() > 0) {
betaBuilder.append(Double.toString(beta.getEntry(0, beta.getColumnDimension() - 1)));
}
LOGGER.debug("beta: " + betaBuilder.toString());
exec.checkCanceled();
exec.setMessage("Iterative optimization. #Iterations: " + iter + " | Log-likelihood: " + DoubleFormat.formatDouble(loglike) + ". Processing iteration " + (iter + 1) + ".");
}
StringBuilder warnBuilder = new StringBuilder();
if (iter >= m_maxIter) {
warnBuilder.append("The algorithm did not reach convergence after the specified number of epochs. " + "Setting the epoch limit higher might result in a better model.");
}
// The covariance matrix
RealMatrix covMat = null;
if (m_calcCovMatrix) {
try {
covMat = new QRDecomposition(A).getSolver().getInverse().scalarMultiply(-1);
} catch (SingularMatrixException sme) {
if (warnBuilder.length() > 0) {
warnBuilder.append("\n");
}
warnBuilder.append("The covariance matrix could not be calculated because the" + " observed fisher information matrix was singular.");
}
}
RealMatrix betaMat = MatrixUtils.createRealMatrix(tcC - 1, rC + 1);
for (int i = 0; i < beta.getColumnDimension(); i++) {
int r = i / (rC + 1);
int c = i % (rC + 1);
betaMat.setEntry(r, c, beta.getEntry(0, i));
}
m_warning = warnBuilder.length() > 0 ? warnBuilder.toString() : null;
return new LogRegLearnerResult(betaMat, covMat, iter, loglike);
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class ARFFTableTest method testCreateDataTableSpecFromARFFfileFOO.
/**
* tests the creatoion of a table spec from a nice ARFF file.
*
* @throws IOException if.
* @throws InvalidSettingsException when.
*/
public void testCreateDataTableSpecFromARFFfileFOO() throws IOException, InvalidSettingsException {
File tempFile = File.createTempFile("ARFFReaderUnitTest", "mini");
tempFile.deleteOnExit();
Writer out = new BufferedWriter(new FileWriter(tempFile));
out.write(ARFF_FOO);
out.close();
try {
DataTableSpec tSpec = ARFFTable.createDataTableSpecFromARFFfile(tempFile.toURI().toURL(), null);
assertEquals(tSpec.getNumColumns(), 8);
assertEquals(tSpec.getColumnSpec(0).getName().toString(), "col1");
assertEquals(tSpec.getColumnSpec(1).getName().toString(), "COL1");
assertEquals(tSpec.getColumnSpec(2).getName().toString(), "col2");
assertEquals(tSpec.getColumnSpec(3).getName().toString(), "COL2");
assertEquals(tSpec.getColumnSpec(4).getName().toString(), "col3");
assertEquals(tSpec.getColumnSpec(5).getName().toString(), "col4");
assertEquals(tSpec.getColumnSpec(6).getName().toString(), "col5");
assertEquals(tSpec.getColumnSpec(7).getName().toString(), "col6");
assertEquals(tSpec.getColumnSpec(0).getType(), DoubleCell.TYPE);
assertEquals(tSpec.getColumnSpec(1).getType(), DoubleCell.TYPE);
assertEquals(tSpec.getColumnSpec(2).getType(), DoubleCell.TYPE);
assertEquals(tSpec.getColumnSpec(3).getType(), DoubleCell.TYPE);
assertEquals(tSpec.getColumnSpec(4).getType(), IntCell.TYPE);
assertEquals(tSpec.getColumnSpec(5).getType(), StringCell.TYPE);
assertEquals(tSpec.getColumnSpec(6).getType(), StringCell.TYPE);
assertEquals(tSpec.getColumnSpec(7).getType(), StringCell.TYPE);
assertNull(tSpec.getColumnSpec(0).getDomain().getValues());
assertNull(tSpec.getColumnSpec(1).getDomain().getValues());
assertNull(tSpec.getColumnSpec(2).getDomain().getValues());
assertNull(tSpec.getColumnSpec(3).getDomain().getValues());
assertNull(tSpec.getColumnSpec(4).getDomain().getValues());
assertNull(tSpec.getColumnSpec(5).getDomain().getValues());
assertNull(tSpec.getColumnSpec(6).getDomain().getValues());
assertEquals(tSpec.getColumnSpec(7).getDomain().getValues().size(), 5);
} catch (CanceledExecutionException cee) {
// if you cancel during the test I will not fail!
}
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class ARFFTableTest method testARFFwithMissingSpace.
/**
* Customer file. Weka is able to read it. We failed on the missing space
* in the last "@attribute" line.
*
* @throws IOException some time.
* @throws InvalidSettingsException sometimes.
*/
public void testARFFwithMissingSpace() throws IOException, InvalidSettingsException {
final String missingSpace = "@relation kredit_bereinigt\n" + "\n" + "@attribute REPAYMENT_PROBLEM {0,1}\n" + /* Col 0*/
"@attribute RSV {0,1}\n" + /* Col 1*/
"@attribute GENDER {0,1}\n" + /* Col 2*/
"@attribute AGE real\n" + /* Col 3*/
"@attribute PHONE {0,1}\n" + /* Col 4*/
"@attribute NUMBER_CHILDREN real\n" + /* Col 5*/
"@attribute ADDRESS_CHANGED {0,1}\n" + /* Col 6*/
"@attribute GUARANTOR {0,1}\n" + /* Col 7*/
"@attribute JOB_DURATION real\n" + /* Col 8*/
"@attribute INCOME real\n" + /* Col 9*/
"@attribute DISP_INCOME real\n" + /* Col 10*/
"@attribute RENTAL_FEE real\n" + /* Col 11*/
"@attribute CAR {0,1}\n" + /* Col 12*/
"@attribute OTHER_CONTRACTS {0,1}\n" + /* Col 13*/
"@attribute OTHER_LOANS {0,1}\n" + /* Col 14*/
"@attribute EXPENSE real\n" + /* Col 15*/
"@attribute SAVINGS {0,1}\n" + /* following line is missing a space */
"@attribute STOCK{0,1}\n" + /* Col 17*/
"\n" + "@data\n" + "1,1,1,27,0,1,1,1,2,2900,1335,330,1,0,0,1565,1,0\n" + "1,1,0,28,1,0,1,0,20,2000,1100,150,0,1,0,900,1,0\n";
File tempFile = File.createTempFile("ARFFReaderUnitTest", "missSpace");
tempFile.deleteOnExit();
Writer out = new BufferedWriter(new FileWriter(tempFile));
out.write(missingSpace);
out.close();
try {
ARFFTable table = new ARFFTable(tempFile.toURI().toURL(), ARFFTable.createDataTableSpecFromARFFfile(tempFile.toURI().toURL(), null), "Row");
assertEquals(table.getDataTableSpec().getNumColumns(), 18);
assertEquals(table.getDataTableSpec().getColumnSpec(0).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(1).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(2).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(3).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(4).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(5).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(6).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(7).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(8).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(9).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(10).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(11).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(12).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(13).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(14).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(15).getType(), DoubleCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(16).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(17).getType(), StringCell.TYPE);
/*
+ "1,1,1,27,0,1,1,1,2,2900,1335,330,1,0,0,1565,1,0\n"
*/
DataRow row;
RowIterator rIter = table.iterator();
assertTrue(rIter.hasNext());
row = rIter.next();
assertEquals(row.getKey().toString(), "Row0");
assertEquals(row.getCell(0).toString(), "1");
assertEquals(row.getCell(1).toString(), "1");
assertEquals(row.getCell(2).toString(), "1");
assertEquals(row.getCell(3).toString(), "27.0");
assertEquals(row.getCell(4).toString(), "0");
assertEquals(row.getCell(5).toString(), "1.0");
assertEquals(row.getCell(6).toString(), "1");
assertEquals(row.getCell(7).toString(), "1");
assertEquals(row.getCell(8).toString(), "2.0");
assertEquals(row.getCell(9).toString(), "2900.0");
assertEquals(row.getCell(10).toString(), "1335.0");
assertEquals(row.getCell(11).toString(), "330.0");
assertEquals(row.getCell(12).toString(), "1");
assertEquals(row.getCell(13).toString(), "0");
assertEquals(row.getCell(14).toString(), "0");
assertEquals(row.getCell(15).toString(), "1565.0");
assertEquals(row.getCell(16).toString(), "1");
assertEquals(row.getCell(17).toString(), "0");
/*
* + "1,1,0,28,1,0,1,0,20,2000,1100,150,0,1,0,900,1,0\n";
*/
assertTrue(rIter.hasNext());
row = rIter.next();
assertEquals(row.getKey().toString(), "Row1");
assertEquals(row.getCell(0).toString(), "1");
assertEquals(row.getCell(1).toString(), "1");
assertEquals(row.getCell(2).toString(), "0");
assertEquals(row.getCell(3).toString(), "28.0");
assertEquals(row.getCell(4).toString(), "1");
assertEquals(row.getCell(5).toString(), "0.0");
assertEquals(row.getCell(6).toString(), "1");
assertEquals(row.getCell(7).toString(), "0");
assertEquals(row.getCell(8).toString(), "20.0");
assertEquals(row.getCell(9).toString(), "2000.0");
assertEquals(row.getCell(10).toString(), "1100.0");
assertEquals(row.getCell(11).toString(), "150.0");
assertEquals(row.getCell(12).toString(), "0");
assertEquals(row.getCell(13).toString(), "1");
assertEquals(row.getCell(14).toString(), "0");
assertEquals(row.getCell(15).toString(), "900.0");
assertEquals(row.getCell(16).toString(), "1");
assertEquals(row.getCell(17).toString(), "0");
assertFalse(rIter.hasNext());
} catch (CanceledExecutionException cee) {
// no exec monitor, no cancel
}
}
Aggregations