use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class FileSingleNodeContainerPersistor method loadNodeContainer.
/**
* {@inheritDoc}
*/
@Override
public void loadNodeContainer(final Map<Integer, BufferedDataTable> tblRep, final ExecutionMonitor exec, final LoadResult result) throws InvalidSettingsException, CanceledExecutionException, IOException {
final NodeSettingsRO settingsForNode = loadSettingsForNode(result);
m_sncSettings = new SingleNodeContainerSettings();
exec.checkCanceled();
try {
m_sncSettings.setMemoryPolicy(loadMemoryPolicySettings(m_nodeSettings));
} catch (InvalidSettingsException e) {
String error = "Unable to load SNC settings: " + e.getMessage();
result.addError(error);
getLogger().debug(error, e);
setDirtyAfterLoad();
return;
}
NodeSettingsRO modelSettings = null;
try {
modelSettings = loadModelSettings(settingsForNode);
} catch (InvalidSettingsException ise) {
String error = "Unable to load model settings: " + ise.getMessage();
result.addError(error);
getLogger().debug(error, ise);
setDirtyAfterLoad();
}
try {
modelSettings = loadNCAndWashModelSettings(settingsForNode, modelSettings, tblRep, exec, result);
} catch (InvalidSettingsException ise) {
String error = "Unable to load node container and wash settings: " + ise.getMessage();
result.addError(error);
getLogger().debug(error, ise);
setDirtyAfterLoad();
}
m_sncSettings.setModelSettings(modelSettings);
try {
m_sncSettings.setVariablesSettings(loadVariableSettings(settingsForNode));
} catch (InvalidSettingsException e) {
String msg = "Could load variable settings: " + e.getMessage();
result.addError(msg);
setDirtyAfterLoad();
setNeedsResetAfterLoad();
}
try {
m_flowObjects = loadFlowObjects(m_nodeSettings);
} catch (Exception e) {
m_flowObjects = Collections.emptyList();
String error = "Error loading flow variables: " + e.getMessage();
getLogger().warn(error, e);
result.addError(error);
setDirtyAfterLoad();
setNeedsResetAfterLoad();
}
exec.setProgress(1.0);
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class DecTreePredictorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
exec.setMessage("Decision Tree Predictor: Loading predictor...");
PMMLPortObject port = (PMMLPortObject) inPorts[INMODELPORT];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
if (models.isEmpty()) {
String msg = "Decision Tree evaluation failed: " + "No tree model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
port.initializeModelTranslator(trans);
DecisionTree decTree = trans.getDecisionTree();
decTree.resetColorInformation();
BufferedDataTable inData = (BufferedDataTable) inPorts[INDATAPORT];
// get column with color information
String colorColumn = null;
for (DataColumnSpec s : inData.getDataTableSpec()) {
if (s.getColorHandler() != null) {
colorColumn = s.getName();
break;
}
}
decTree.setColorColumn(colorColumn);
exec.setMessage("Decision Tree Predictor: start execution.");
PortObjectSpec[] inSpecs = new PortObjectSpec[] { inPorts[0].getSpec(), inPorts[1].getSpec() };
DataTableSpec outSpec = createOutTableSpec(inSpecs);
BufferedDataContainer outData = exec.createDataContainer(outSpec);
long coveredPattern = 0;
long nrPattern = 0;
long rowCount = 0;
long numberRows = inData.size();
exec.setMessage("Classifying...");
for (DataRow thisRow : inData) {
DataCell cl = null;
LinkedHashMap<String, Double> classDistrib = null;
try {
Pair<DataCell, LinkedHashMap<DataCell, Double>> pair = decTree.getWinnerAndClasscounts(thisRow, inData.getDataTableSpec());
cl = pair.getFirst();
LinkedHashMap<DataCell, Double> classCounts = pair.getSecond();
classDistrib = getDistribution(classCounts);
if (coveredPattern < m_maxNumCoveredPattern.getIntValue()) {
// remember this one for HiLite support
decTree.addCoveredPattern(thisRow, inData.getDataTableSpec());
coveredPattern++;
} else {
// too many patterns for HiLite - at least remember color
decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
}
nrPattern++;
} catch (Exception e) {
LOGGER.error("Decision Tree evaluation failed: " + e.getMessage());
throw e;
}
if (cl == null) {
LOGGER.error("Decision Tree evaluation failed: result empty");
throw new Exception("Decision Tree evaluation failed.");
}
DataCell[] newCells = new DataCell[outSpec.getNumColumns()];
int numInCells = thisRow.getNumCells();
for (int i = 0; i < numInCells; i++) {
newCells[i] = thisRow.getCell(i);
}
if (m_showDistribution.getBooleanValue()) {
for (int i = numInCells; i < newCells.length - 1; i++) {
String predClass = outSpec.getColumnSpec(i).getName();
if (classDistrib != null && classDistrib.get(predClass) != null) {
newCells[i] = new DoubleCell(classDistrib.get(predClass));
} else {
newCells[i] = new DoubleCell(0.0);
}
}
}
newCells[newCells.length - 1] = cl;
outData.addRowToTable(new DefaultRow(thisRow.getKey(), newCells));
rowCount++;
if (rowCount % 100 == 0) {
exec.setProgress(rowCount / (double) numberRows, "Classifying... Row " + rowCount + " of " + numberRows);
}
exec.checkCanceled();
}
if (coveredPattern < nrPattern) {
// let the user know that we did not store all available pattern
// for HiLiting.
this.setWarningMessage("Tree only stored first " + m_maxNumCoveredPattern.getIntValue() + " (of " + nrPattern + ") rows for HiLiting!");
}
outData.close();
m_decTree = decTree;
exec.setMessage("Decision Tree Predictor: end execution.");
return new BufferedDataTable[] { outData.getTable() };
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class PathProximity method calculatePathProximities.
public ProximityMatrix calculatePathProximities(final ExecutionContext exec) throws InterruptedException, CanceledExecutionException {
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>();
// The path proximity matrix is not symmetric if applied for a single table
// therefore we have to use the two table approach even it is only a single table
ProximityMatrix proximityMatrix = new TwoTablesProximityMatrix(m_tables[0], m_tables[1]);
final int nrTrees = m_modelPO.getEnsembleModel().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);
ExecutionMonitor subExec = exec.createSubProgress(0.0);
calcFutures[i] = tp.enqueue(new PathProximityCalcRunnable(i, proximityMatrix, 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;
}
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, m_specialColumns, 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()) {
DataColumnSpec columnSpec = data.getDataTableSpec().getColumnSpec(i);
if (trainingData.getIsNominal().get(i)) {
String factor = columnSpec.getName();
factorList.add(factor);
List<DataCell> values = trainingData.getDomainValues().get(i);
factorDomainValues.put(factor, values);
} else {
if (columnSpec.getType().isCompatible(BitVectorValue.class) || columnSpec.getType().isCompatible(ByteVectorValue.class)) {
int length = trainingData.getVectorLengths().getOrDefault(i, 0).intValue();
for (int j = 0; j < length; ++j) {
covariateList.add(columnSpec.getName() + "[" + j + "]");
}
} else {
covariateList.add(columnSpec.getName());
}
}
}
final Map<? extends Integer, Integer> vectorIndexLengths = trainingData.getVectorLengths();
final Map<String, Integer> vectorLengths = new LinkedHashMap<String, Integer>();
for (DataColumnSpec spec : m_specialColumns) {
int colIndex = data.getSpec().findColumnIndex(spec.getName());
if (colIndex >= 0) {
vectorLengths.put(spec.getName(), vectorIndexLengths.get(colIndex));
}
}
// create content
LogisticRegressionContent content = new LogisticRegressionContent(m_outSpec, factorList, covariateList, vectorLengths, m_targetReferenceCategory, m_sortTargetCategories, m_sortFactorsCategories, beta, loglike, covMat, iter);
return content;
}
use of org.knime.core.node.CanceledExecutionException in project knime-core by knime.
the class ImagePortObject method load.
/**
* {@inheritDoc}
*/
@Override
protected void load(final PortObjectZipInputStream in, final PortObjectSpec spec, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
ZipEntry nextEntry = in.getNextEntry();
String contentClName = nextEntry.getName();
Class<? extends ImageContent> contentCl;
try {
contentCl = (Class<? extends ImageContent>) Class.forName(contentClName);
} catch (ClassNotFoundException ex) {
throw new IOException("ImageContent class '" + contentClName + "'" + " does not exist", ex);
}
if (!ImageContent.class.isAssignableFrom(contentCl)) {
throw new IOException("Class '" + contentClName + "' is not an ImageContent");
}
Constructor<? extends ImageContent> cons;
try {
cons = contentCl.getConstructor(InputStream.class);
} catch (Exception ex) {
throw new IOException("ImageContent class '" + contentClName + "' " + "is missing a required constructor, see javadoc", ex);
}
try {
m_content = cons.newInstance(in);
} catch (Exception ex) {
throw new IOException("Could not create an instance of '" + contentClName + "'", ex);
}
in.close();
m_spec = (ImagePortObjectSpec) spec;
}
Aggregations