use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class PCAApplyNodeModel 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 int dimensions = m_dimSelection.getNeededDimensions();
if (dimensions == -1) {
throw new IllegalArgumentException("Number of dimensions not correct configured");
}
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 Matrix eigenvectors = EigenValue.getSortedEigenVectors(model.getEigenVectors(), model.getEigenvalues(), dimensions);
final DataColumnSpec[] specs = PCANodeModel.createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), dimensions);
final int dim = dimensions;
final CellFactory fac = new CellFactory() {
@Override
public DataCell[] getCells(final DataRow row) {
return PCANodeModel.convertInputRow(eigenvectors, row, model.getCenter(), m_inputColumnIndices, dim, m_failOnMissingValues.getBooleanValue());
}
@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, "converting input row " + curRowNr + " of " + rowCount);
}
};
final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[DATA_INPORT].getSpec());
cr.append(fac);
if (m_removeOriginalCols.getBooleanValue()) {
cr.remove(m_inputColumnNames);
}
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 KnnNodeModel method createRearranger.
/*
* @param maxRows - can be -1 if can't be determined (streaming)
*/
private ColumnRearranger createRearranger(final DataTableSpec in, final DataColumnSpec classColumnSpec, final List<Integer> featureColumns, final Map<Integer, Integer> firstToSecond, final KDTree<DataCell> tree, final double maxRows) {
ColumnRearranger c = new ColumnRearranger(in);
String newName = "Class [kNN]";
while (in.containsName(newName)) {
newName += "_dup";
}
List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
DataColumnSpecCreator crea = new DataColumnSpecCreator(classColumnSpec);
crea.setName(newName);
colSpecs.add(crea.createSpec());
final DataCell[] possibleValues;
if (m_settings.outputClassProbabilities()) {
possibleValues = classColumnSpec.getDomain().getValues().toArray(new DataCell[0]);
Arrays.sort(possibleValues, new Comparator<DataCell>() {
@Override
public int compare(final DataCell o1, final DataCell o2) {
return o1.toString().compareTo(o2.toString());
}
});
for (DataCell posVal : possibleValues) {
newName = posVal.toString();
while (in.containsName(newName)) {
newName += "_dup";
}
crea = new DataColumnSpecCreator(newName, DoubleCell.TYPE);
colSpecs.add(crea.createSpec());
}
} else {
possibleValues = new DataCell[0];
}
final DataColumnSpec[] colSpecArray = colSpecs.toArray(new DataColumnSpec[colSpecs.size()]);
c.append(new AbstractCellFactory(colSpecArray) {
/**
* {@inheritDoc}
*/
@Override
public void setProgress(final long curRowNr, final long rowCount, final RowKey lastKey, final ExecutionMonitor exec) {
if (maxRows > 0) {
exec.setProgress(curRowNr / maxRows, "Classifying row " + lastKey);
} else {
exec.setProgress("Classifying row " + lastKey);
}
}
@Override
public DataCell[] getCells(final DataRow row) {
List<DataCell> output = classify(row, tree, featureColumns, firstToSecond, possibleValues);
return output.toArray(new DataCell[output.size()]);
}
});
return c;
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class InteractiveHiLiteCollectorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
m_data = inData[0];
if (m_annotationMap.isEmpty()) {
return new PortObject[] { m_data };
}
DataTableSpec inSpec = (DataTableSpec) m_data.getSpec();
final DataColumnSpec[] cspecs = createSpecs(inSpec);
ColumnRearranger cr = new ColumnRearranger(inSpec);
cr.append(new CellFactory() {
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
if (m_annotationMap.isEmpty()) {
return new DataCell[0];
}
DataCell[] cells = new DataCell[m_lastIndex + 1];
for (int i = 0; i < cells.length; i++) {
Map<Integer, String> map = m_annotationMap.get(row.getKey());
if (map == null) {
cells[i] = DataType.getMissingCell();
} else {
String str = map.get(i);
if (str == null) {
cells[i] = DataType.getMissingCell();
} else {
cells[i] = new StringCell(str);
}
}
}
return cells;
}
@Override
public DataColumnSpec[] getColumnSpecs() {
return cspecs;
}
/**
* {@inheritDoc}
*/
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor em) {
em.setProgress((double) curRowNr / rowCount);
}
});
return new BufferedDataTable[] { exec.createColumnRearrangeTable((BufferedDataTable) m_data, cr, exec) };
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class SubsetMatcherNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
final BufferedDataTable subsetTable = inData[0];
final DataTableSpec subsetTableSpec = subsetTable.getSpec();
final int subsetColIdx = subsetTableSpec.findColumnIndex(m_subsetCol.getStringValue());
// the comparator that should be used to sort the subset AND the
// set list
final Comparator<DataCell> comparator = subsetTableSpec.getColumnSpec(subsetColIdx).getType().getComparator();
final BufferedDataTable setTable = inData[1];
final DataTableSpec setTableSpec = setTable.getSpec();
final int setIDColIdx;
final DataColumnSpec setIDSpec;
if (m_setIDCol.useRowID()) {
setIDColIdx = -1;
setIDSpec = null;
} else {
setIDColIdx = setTableSpec.findColumnIndex(m_setIDCol.getStringValue());
setIDSpec = setTableSpec.getColumnSpec(setIDColIdx);
}
final int transColIdx = setTableSpec.findColumnIndex(m_setCol.getStringValue());
final boolean appendSetCol = m_appendSetListCol.getBooleanValue();
// create the data container
final DataTableSpec resultSpec = createTableSpec(setIDSpec, setTableSpec.getColumnSpec(transColIdx), subsetTableSpec.getColumnSpec(subsetColIdx), appendSetCol);
m_dc = exec.createDataContainer(resultSpec);
final long subsetRowCount = subsetTable.size();
if (subsetRowCount == 0) {
setWarningMessage("Empty subset table found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final long setRowCount = setTable.size();
if (setRowCount == 0) {
setWarningMessage("Empty set table found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final double totalRowCount = subsetRowCount + setRowCount * SET_PROCESSING_FACTOR;
final ExecutionMonitor subsetExec = exec.createSubProgress(subsetRowCount / totalRowCount);
// create the rule model
exec.setMessage("Generating subset base...");
final SubsetMatcher[] sortedMatcher = createSortedMatcher(subsetExec, subsetTable, subsetColIdx, comparator);
subsetExec.setProgress(1.0);
if (sortedMatcher.length < 1) {
setWarningMessage("No item sets found");
m_dc.close();
return new BufferedDataTable[] { m_dc.getTable() };
}
final ExecutionMonitor setExec = exec.createSubProgress((setRowCount * SET_PROCESSING_FACTOR) / totalRowCount);
// create the matching processes
exec.setMessage("Processing sets... ");
// initialize the thread pool for parallelization of the set
// analysis
final ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL.createSubPool(1);
for (final DataRow row : setTable) {
exec.checkCanceled();
DataCell setIDCell;
if (setIDColIdx < 0) {
final RowKey key = row.getKey();
setIDCell = new StringCell(key.getString());
} else {
setIDCell = row.getCell(setIDColIdx);
}
final DataCell setCell = row.getCell(transColIdx);
if (!(setCell instanceof CollectionDataValue)) {
setExec.setProgress(m_setCounter.incrementAndGet() / (double) setRowCount);
m_skipCounter.incrementAndGet();
continue;
}
final CollectionDataValue setList = (CollectionDataValue) setCell;
if (setList.size() < 1) {
// skip empty sets
setExec.setProgress(m_setCounter.incrementAndGet() / (double) setRowCount);
m_skipCounter.incrementAndGet();
continue;
}
// submit for each set a job in the thread pool
pool.enqueue(createRunnable(setExec, setRowCount, setIDCell, setList, appendSetCol, comparator, sortedMatcher, m_maxMismatches.getIntValue()));
}
// wait until all jobs are finished before closing the container
// and returning the method
pool.waitForTermination();
exec.setMessage("Creating data table...");
m_dc.close();
if (m_skipCounter.intValue() > 0) {
setWarningMessage("No matching subsets found for " + m_skipCounter + " out of " + setRowCount + " sets");
}
exec.setProgress(1.0);
return new BufferedDataTable[] { m_dc.getTable() };
}
use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.
the class Normalizer3NodeModel method calculate.
/**
* New normalized {@link org.knime.core.data.DataTable} is created depending on the mode.
*/
/**
* @param inData The input data.
* @param exec For BufferedDataTable creation and progress.
* @return the result of the calculation
* @throws Exception If the node calculation fails for any reason.
*/
protected CalculationResult calculate(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[0];
DataTableSpec inSpec = inTable.getSpec();
// extract selected numeric columns
String[] includedColumns = getIncludedComlumns(inSpec);
Normalizer2 ntable = new Normalizer2(inTable, includedColumns);
long rowcount = inTable.size();
ExecutionContext prepareExec = exec.createSubExecutionContext(0.3);
AffineTransTable outTable;
boolean fixDomainBounds = false;
switch(m_config.getMode()) {
case MINMAX:
fixDomainBounds = true;
outTable = ntable.doMinMaxNorm(m_config.getMax(), m_config.getMin(), prepareExec);
break;
case Z_SCORE:
outTable = ntable.doZScoreNorm(prepareExec);
break;
case DECIMALSCALING:
outTable = ntable.doDecimalScaling(prepareExec);
break;
default:
throw new InvalidSettingsException("No mode set");
}
if (outTable.getErrorMessage() != null) {
// something went wrong, report and throw an exception
throw new Exception(outTable.getErrorMessage());
}
if (ntable.getErrorMessage() != null) {
// something went wrong during initialization, report.
setWarningMessage(ntable.getErrorMessage());
}
DataTableSpec modelSpec = FilterColumnTable.createFilterTableSpec(inSpec, includedColumns);
AffineTransConfiguration configuration = outTable.getConfiguration();
DataTableSpec spec = outTable.getDataTableSpec();
// the same transformation, which is not guaranteed to snap to min/max)
if (fixDomainBounds) {
DataColumnSpec[] newColSpecs = new DataColumnSpec[spec.getNumColumns()];
for (int i = 0; i < newColSpecs.length; i++) {
newColSpecs[i] = spec.getColumnSpec(i);
}
for (int i = 0; i < includedColumns.length; i++) {
int index = spec.findColumnIndex(includedColumns[i]);
DataColumnSpecCreator creator = new DataColumnSpecCreator(newColSpecs[index]);
DataColumnDomainCreator domCreator = new DataColumnDomainCreator(newColSpecs[index].getDomain());
domCreator.setLowerBound(new DoubleCell(m_config.getMin()));
domCreator.setUpperBound(new DoubleCell(m_config.getMax()));
creator.setDomain(domCreator.createDomain());
newColSpecs[index] = creator.createSpec();
}
spec = new DataTableSpec(spec.getName(), newColSpecs);
}
ExecutionMonitor normExec = exec.createSubProgress(.7);
BufferedDataContainer container = exec.createDataContainer(spec);
long count = 1;
for (DataRow row : outTable) {
normExec.checkCanceled();
normExec.setProgress(count / (double) rowcount, "Normalizing row no. " + count + " of " + rowcount + " (\"" + row.getKey() + "\")");
container.addRowToTable(row);
count++;
}
container.close();
return new CalculationResult(container.getTable(), modelSpec, configuration);
}
Aggregations