Search in sources :

Example 56 with ExecutionMonitor

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;
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) Matrix(Jama.Matrix) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory) PortObject(org.knime.core.node.port.PortObject)

Example 57 with ExecutionMonitor

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;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 58 with ExecutionMonitor

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) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PortObject(org.knime.core.node.port.PortObject) CellFactory(org.knime.core.data.container.CellFactory) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 59 with ExecutionMonitor

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() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) ThreadPool(org.knime.core.util.ThreadPool) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue)

Example 60 with ExecutionMonitor

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);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) Normalizer2(org.knime.base.data.normalize.Normalizer2) DoubleCell(org.knime.core.data.def.DoubleCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedDataTable(org.knime.core.node.BufferedDataTable) AffineTransTable(org.knime.base.data.normalize.AffineTransTable) AffineTransConfiguration(org.knime.base.data.normalize.AffineTransConfiguration) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Aggregations

ExecutionMonitor (org.knime.core.node.ExecutionMonitor)160 BufferedDataTable (org.knime.core.node.BufferedDataTable)50 DataTableSpec (org.knime.core.data.DataTableSpec)43 DataRow (org.knime.core.data.DataRow)39 DataCell (org.knime.core.data.DataCell)35 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)35 Test (org.junit.Test)33 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)33 File (java.io.File)29 IOException (java.io.IOException)25 PortObject (org.knime.core.node.port.PortObject)25 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 RowKey (org.knime.core.data.RowKey)20 ArrayList (java.util.ArrayList)19 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)17 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)16 ExecutionException (java.util.concurrent.ExecutionException)14 ExecutionContext (org.knime.core.node.ExecutionContext)13 FileOutputStream (java.io.FileOutputStream)12