use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class SmoteNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
BufferedDataTable in = inData[0];
Random rand;
if (m_seed != null) {
rand = new Random(m_seed);
} else {
rand = new Random();
}
Smoter smoter = new Smoter(in, m_class, exec, rand);
if (m_method.equals(METHOD_ALL)) {
// count number of rows to add
int nrRowsToAdd = 0;
for (Iterator<DataCell> it = smoter.getClassValues(); it.hasNext(); ) {
int count = smoter.getCount(it.next());
nrRowsToAdd += (int) (count * m_rate);
}
for (Iterator<DataCell> it = smoter.getClassValues(); it.hasNext(); ) {
DataCell cur = it.next();
int count = smoter.getCount(cur);
int newCount = (int) (count * m_rate);
exec.setMessage("Smoting '" + cur.toString() + "'");
ExecutionMonitor subExec = exec.createSubProgress(newCount / (double) nrRowsToAdd);
smoter.smote(cur, newCount, m_kNN, subExec);
}
} else if (m_method.equals(METHOD_MAJORITY)) {
DataCell majority = smoter.getMajorityClass();
int majorityCount = smoter.getCount(majority);
Iterator<DataCell> it = smoter.getClassValues();
int nrRowsToAdd = 0;
while (it.hasNext()) {
DataCell cur = it.next();
nrRowsToAdd += (majorityCount - smoter.getCount(cur));
}
it = smoter.getClassValues();
while (it.hasNext()) {
DataCell cur = it.next();
int count = smoter.getCount(cur);
int newCount = majorityCount - count;
exec.setMessage("Smoting '" + cur.toString() + "'");
ExecutionMonitor subExec = exec.createSubProgress(newCount / (double) nrRowsToAdd);
smoter.smote(cur, newCount, m_kNN, subExec);
}
}
smoter.close();
DataTable out = smoter.getSmotedTable();
return new BufferedDataTable[] { exec.createBufferedDataTable(out, exec) };
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class AggregateOutputNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
// retrieve variables from the stack which the head of this
// loop hopefully put there:
int count;
int maxCount;
try {
count = peekFlowVariableInt("currentIteration");
maxCount = peekFlowVariableInt("maxIterations");
} catch (NoSuchElementException e) {
throw new Exception("No matching Loop Start node!", e);
}
if (count < 0 || count >= maxCount) {
throw new Exception("Conflicting loop variables, count is " + count + " and max count is " + maxCount);
}
final BufferedDataTable in = inData[0];
final DataTableSpec inSpec = in.getDataTableSpec();
if (count == 0) {
m_firstIterationSpec = in.getDataTableSpec();
m_predictionTable = exec.createDataContainer(createPredictionSpec(in.getDataTableSpec()));
} else if (m_predictionTable == null) {
throw new Exception("Loop Head claims this is NOT the first iteration" + " but the tail believes it is?!");
} else {
if (!inSpec.equalStructure(m_firstIterationSpec)) {
StringBuilder error = new StringBuilder("Input table's structure differs from reference " + "(first iteration) table: ");
if (inSpec.getNumColumns() != m_firstIterationSpec.getNumColumns()) {
error.append("different column counts ");
error.append(inSpec.getNumColumns());
error.append(" vs. ").append(m_firstIterationSpec.getNumColumns());
} else {
for (int i = 0; i < inSpec.getNumColumns(); i++) {
DataColumnSpec inCol = inSpec.getColumnSpec(i);
DataColumnSpec predCol = m_firstIterationSpec.getColumnSpec(i);
if (!inCol.equalStructure(predCol)) {
error.append("Column ").append(i).append(" [");
error.append(inCol).append("] vs. [");
error.append(predCol).append("]");
}
}
}
throw new IllegalArgumentException(error.toString());
}
}
final int rowCount = in.getRowCount();
final int targetColIndex = in.getDataTableSpec().findColumnIndex(m_settings.targetColumn());
final int predictColIndex = in.getDataTableSpec().findColumnIndex(m_settings.predictionColumn());
final boolean numericMode = in.getDataTableSpec().getColumnSpec(predictColIndex).getType().isCompatible(DoubleValue.class);
ExecutionMonitor subExec = exec.createSubProgress(count == maxCount - 1 ? 0.9 : 1);
final DataCell foldNumber = new IntCell(m_foldStatistics.size());
if (numericMode) {
double errorSum = 0;
int r = 0;
for (DataRow row : in) {
RowKey key = row.getKey();
DoubleValue target = (DoubleValue) row.getCell(targetColIndex);
DoubleValue predict = (DoubleValue) row.getCell(predictColIndex);
double d = (target.getDoubleValue() - predict.getDoubleValue());
errorSum += d * d;
r++;
if (m_settings.addFoldId()) {
m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
} else {
m_predictionTable.addRowToTable(row);
}
subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
subExec.checkCanceled();
}
DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(errorSum), new DoubleCell(errorSum / rowCount), new IntCell(rowCount));
m_foldStatistics.add(stats);
} else {
int incorrect = 0;
int r = 0;
for (DataRow row : in) {
RowKey key = row.getKey();
DataCell target = row.getCell(targetColIndex);
DataCell predict = row.getCell(predictColIndex);
if (!target.equals(predict)) {
incorrect++;
}
r++;
if (m_settings.addFoldId()) {
m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
} else {
m_predictionTable.addRowToTable(row);
}
subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
subExec.checkCanceled();
}
DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(100.0 * incorrect / rowCount), new IntCell(rowCount), new IntCell(incorrect));
m_foldStatistics.add(stats);
}
if (count < maxCount - 1) {
continueLoop();
return new BufferedDataTable[2];
} else {
BufferedDataContainer cont = exec.createDataContainer(numericMode ? NUMERIC_STATISTICS_SPEC : NOMINAL_STATISTICS_SPEC);
for (DataRow row : m_foldStatistics) {
cont.addRowToTable(row);
}
cont.close();
m_predictionTable.close();
return new BufferedDataTable[] { m_predictionTable.getTable(), cont.getTable() };
}
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class PCAReverseNodeModel method execute.
/**
* Performs the PCA.
*
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final PCAModelPortObject model = (PCAModelPortObject) inData[MODEL_INPORT];
final Matrix eigenvectors = EigenValue.getSortedEigenVectors(model.getEigenVectors(), model.getEigenvalues(), m_inputColumnIndices.length);
if (m_failOnMissingValues.getBooleanValue()) {
for (final DataRow row : (DataTable) inData[DATA_INPORT]) {
for (int i = 0; i < m_inputColumnIndices.length; i++) {
if (row.getCell(m_inputColumnIndices[i]).isMissing()) {
throw new IllegalArgumentException("data table contains missing values");
}
}
}
}
final String[] origColumnNames = ((PCAModelPortObjectSpec) ((PCAModelPortObject) inData[MODEL_INPORT]).getSpec()).getColumnNames();
final DataColumnSpec[] specs = createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), origColumnNames);
final CellFactory fac = new CellFactory() {
@Override
public DataCell[] getCells(final DataRow row) {
return convertInputRow(eigenvectors, row, model.getCenter(), m_inputColumnIndices, origColumnNames.length);
}
@Override
public DataColumnSpec[] getColumnSpecs() {
return specs;
}
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor texec) {
texec.setProgress((double) curRowNr / rowCount);
}
};
final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[DATA_INPORT].getSpec());
cr.append(fac);
if (m_removePCACols.getBooleanValue()) {
cr.remove(m_inputColumnIndices);
}
final BufferedDataTable result = exec.createColumnRearrangeTable((BufferedDataTable) inData[DATA_INPORT], cr, exec);
final PortObject[] out = { result };
return out;
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class PolyRegLearnerNodeModel method getCellFactory.
private CellFactory getCellFactory(final int dependentIndex) {
final int degree = m_settings.getDegree();
return new CellFactory() {
@Override
public DataCell[] getCells(final DataRow row) {
double sum = m_betas[0];
int betaCount = 1;
double y = 0;
for (int col = 0; col < row.getNumCells(); col++) {
if ((col == dependentIndex || m_colSelected[col]) && row.getCell(col).isMissing()) {
switch(m_settings.getMissingValueHandling()) {
case ignore:
return new DataCell[] { DataType.getMissingCell(), DataType.getMissingCell() };
case fail:
throw new IllegalStateException("Should failed earlier!");
default:
throw new UnsupportedOperationException("Not supported missing handling strategy: " + m_settings.getMissingValueHandling());
}
}
if ((col != dependentIndex) && m_colSelected[col]) {
final double value = ((DoubleValue) row.getCell(col)).getDoubleValue();
double poly = 1;
for (int d = 1; d <= degree; d++) {
poly *= value;
sum += m_betas[betaCount++] * poly;
}
} else if (col == dependentIndex) {
y = ((DoubleValue) row.getCell(col)).getDoubleValue();
}
}
double err = Math.abs(sum - y);
m_squaredError += err * err;
return new DataCell[] { new DoubleCell(sum), new DoubleCell(err) };
}
@Override
public DataColumnSpec[] getColumnSpecs() {
DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
DataColumnSpec col1 = crea.createSpec();
crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
DataColumnSpec col2 = crea.createSpec();
return new DataColumnSpec[] { col1, col2 };
}
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor execMon) {
// do nothing
}
};
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class MDSProjectionManager method train.
/**
* Does the training by adjusting the lower dimensional data points
* according to their distances and the distances of the original data.
*
* @param epochs The number of epochs to train.
* @param learningrate The learn rate, specifying the step size of
* adjustment.
* @throws CanceledExecutionException If execution was canceled by the user.
*/
public void train(final int epochs, final double learningrate) throws CanceledExecutionException {
if (!m_isInit) {
init(DEFAULT_SEED);
}
ExecutionMonitor exec = m_exec.createSubProgress(0.9);
m_learningrate = learningrate;
m_initialLearningrate = learningrate;
m_epochs = epochs;
exec.setMessage("Start training");
for (int e = 1; e <= epochs; e++) {
exec.checkCanceled();
doEpoch(e, exec);
double prog = (double) e / (double) epochs;
exec.setProgress(prog, "Training epoch " + e + " of " + epochs);
}
}
Aggregations