Search in sources :

Example 66 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class CAIMDiscretizationNodeModel method createResultTable.

/**
 * Creates {@link BufferedDataTable} from a given input table and an
 * appropriate {@link DiscretizationScheme}. The result table has replaced
 * columns according to the {@link DiscretizationScheme}.
 *
 * @param exec the context from which to create the
 *            {@link BufferedDataTable}
 * @param table the input data table
 * @param discretizationModel the {@link DiscretizationModel} that contains
 *            the mapping from numerical intervals to nominal String values
 *            for the included columns
 * @return the discretized input data
 */
public static BufferedDataTable createResultTable(final ExecutionContext exec, final BufferedDataTable table, final DiscretizationModel discretizationModel) {
    DiscretizationScheme[] dSchemes = discretizationModel.getSchemes();
    final String[] includedColumnNames = discretizationModel.getIncludedColumnNames();
    // filter the schemes so that only schemes for columns are included
    // which are also included in the table
    dSchemes = filterNotKnownSchemes(dSchemes, includedColumnNames, table.getDataTableSpec());
    DataTableSpec originalTableSpec = table.getDataTableSpec();
    DataColumnSpec[] newColumnSpecs = new DataColumnSpec[originalTableSpec.getNumColumns()];
    // remembers if an column index is included or not
    boolean[] included = new boolean[newColumnSpecs.length];
    int counter = 0;
    for (DataColumnSpec originalColumnSpec : originalTableSpec) {
        // if the column is included for discretizing, change the spec
        if (isIncluded(originalColumnSpec, includedColumnNames) > -1) {
            // creat a nominal string column spec
            newColumnSpecs[counter] = new DataColumnSpecCreator(originalColumnSpec.getName(), StringCell.TYPE).createSpec();
            included[counter] = true;
        } else {
            // add it as is
            newColumnSpecs[counter] = originalColumnSpec;
            included[counter] = false;
        }
        counter++;
    }
    // create the new table spec
    DataTableSpec newTableSpec = new DataTableSpec(newColumnSpecs);
    // create the result table
    BufferedDataContainer container = exec.createDataContainer(newTableSpec);
    // discretize the included column values
    double rowCounter = 0;
    double numRows = table.size();
    for (DataRow row : table) {
        if (rowCounter % 200 == 0) {
            exec.setProgress(rowCounter / numRows);
        }
        int i = 0;
        DataCell[] newCells = new DataCell[row.getNumCells()];
        int includedCounter = 0;
        for (DataCell cell : row) {
            if (included[i]) {
                // check for missing values
                if (cell.isMissing()) {
                    newCells[i] = cell;
                } else {
                    // transform the value to the discretized one
                    double value = ((DoubleValue) cell).getDoubleValue();
                    String discreteValue = dSchemes[includedCounter].getDiscreteValue(value);
                    newCells[i] = new StringCell(discreteValue);
                }
                includedCounter++;
            } else {
                newCells[i] = cell;
            }
            i++;
        }
        container.addRowToTable(new DefaultRow(row.getKey(), newCells));
        rowCounter++;
    }
    container.close();
    return container.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DiscretizationScheme(org.knime.base.node.preproc.discretization.caim2.DiscretizationScheme) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 67 with StringCell

use of org.knime.core.data.def.StringCell 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 68 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class ColumnRowFilterPanel method boundsChanged.

/**
 * Called when user changes the values for the lower or upper bounds.
 */
protected void boundsChanged() {
    // check if the entered value somehow goes along with the selected col.
    setErrMsg("");
    if (m_tSpec == null) {
        return;
    }
    if (getSelectedColumnName() == null) {
        return;
    }
    if (!m_useRange.isSelected()) {
        return;
    }
    DataCell lowBound = null;
    DataCell hiBound = null;
    try {
        lowBound = getLowerBoundCell();
        hiBound = getUpperBoundCell();
    } catch (InvalidSettingsException ise) {
        setErrMsg(ise.getMessage());
        return;
    }
    if ((lowBound == null) && (hiBound == null)) {
        setErrMsg("Specify at least one range boundary");
        return;
    }
    if ((lowBound != null) && (hiBound != null)) {
        DataValueComparator comp;
        comp = DataType.getCommonSuperType(lowBound.getType(), hiBound.getType()).getComparator();
        if (comp.compare(hiBound, lowBound) == -1) {
            setErrMsg("The lower bound must be smaller than the" + " upper bound");
            return;
        }
    }
    if (((lowBound != null) && (lowBound instanceof StringCell)) || ((hiBound != null) && (hiBound instanceof StringCell))) {
        setErrMsg("Warning: String comparison is used for " + "range checking. May not work as expected!");
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DataValueComparator(org.knime.core.data.DataValueComparator)

Example 69 with StringCell

use of org.knime.core.data.def.StringCell in project knime-core by knime.

the class ColumnRowFilterPanel method getBoundCell.

/* method used from the above */
private DataCell getBoundCell(final JTextField editField, final String name) throws InvalidSettingsException {
    if (editField.getText().length() <= 0) {
        return null;
    }
    String colName = getSelectedColumnName();
    if ((colName == null) || (colName.length() == 0)) {
        throw new InvalidSettingsException("Invalid columns selection");
    }
    if (m_tSpec != null) {
        final DataType origType = m_tSpec.getColumnSpec(colName).getType();
        final DataType cType;
        if (m_deepFiltering.isSelected() && origType.isCollectionType()) {
            cType = origType.getCollectionElementType();
        } else {
            cType = origType;
        }
        if (cType.isCompatible(IntValue.class)) {
            // first try making of an IntCell
            try {
                int lb = Integer.parseInt(editField.getText());
                return new IntCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid integer.");
            }
        } else if (cType.isCompatible(LongValue.class)) {
            try {
                long lb = Long.parseLong(editField.getText());
                return new LongCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid number.");
            }
        } else if (cType.isCompatible(DoubleValue.class)) {
            try {
                double lb = Double.parseDouble(editField.getText());
                return new DoubleCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: enter a valid " + "float number");
            }
        } else {
            return new StringCell(editField.getText());
        }
    } else {
        // if we got no column type
        return new StringCell(editField.getText());
    }
}
Also used : LongCell(org.knime.core.data.def.LongCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) LongValue(org.knime.core.data.LongValue) DataType(org.knime.core.data.DataType) IntCell(org.knime.core.data.def.IntCell)

Example 70 with StringCell

use of org.knime.core.data.def.StringCell 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)

Aggregations

StringCell (org.knime.core.data.def.StringCell)176 DataCell (org.knime.core.data.DataCell)130 DoubleCell (org.knime.core.data.def.DoubleCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)65 IntCell (org.knime.core.data.def.IntCell)55 DataRow (org.knime.core.data.DataRow)52 DataTableSpec (org.knime.core.data.DataTableSpec)49 ArrayList (java.util.ArrayList)41 DataColumnSpec (org.knime.core.data.DataColumnSpec)37 RowKey (org.knime.core.data.RowKey)36 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)26 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)26 DataType (org.knime.core.data.DataType)22 LinkedHashSet (java.util.LinkedHashSet)21 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)16 LinkedHashMap (java.util.LinkedHashMap)15 Test (org.junit.Test)15 HashMap (java.util.HashMap)11