Search in sources :

Example 46 with StringCell

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

the class StringReplacerNodeModel method createColumnRearranger.

/**
 * Creates the column rearranger that computes the new cells.
 *
 * @param spec the spec of the input table
 * @return a column rearranger
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    final Pattern pattern = createPattern(m_settings);
    DataColumnSpec colSpec;
    if (m_settings.createNewColumn()) {
        colSpec = new DataColumnSpecCreator(m_settings.newColumnName(), StringCell.TYPE).createSpec();
    } else {
        colSpec = new DataColumnSpecCreator(m_settings.columnName(), StringCell.TYPE).createSpec();
    }
    final String replacement;
    if (m_settings.patternIsRegex()) {
        replacement = m_settings.replacement();
    } else {
        replacement = m_settings.replacement().replaceAll("(\\$\\d+)", "\\\\$1");
    }
    final int index = spec.findColumnIndex(m_settings.columnName());
    SingleCellFactory cf = new SingleCellFactory(colSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(index);
            if (cell.isMissing()) {
                return cell;
            }
            final String stringValue = ((StringValue) cell).getStringValue();
            Matcher m = pattern.matcher(stringValue);
            if (m_settings.replaceAllOccurrences()) {
                return new StringCell(m.replaceAll(replacement));
            } else if (m.matches()) {
                if (".*".equals(pattern.pattern())) {
                    // therefore the replacement value is doubled
                    return new StringCell(replacement);
                } else {
                    return new StringCell(m.replaceAll(replacement));
                }
            } else {
                return new StringCell(stringValue);
            }
        }
    };
    ColumnRearranger crea = new ColumnRearranger(spec);
    if (m_settings.createNewColumn()) {
        if (spec.containsName(m_settings.newColumnName())) {
            throw new InvalidSettingsException("Duplicate column name: " + m_settings.newColumnName());
        }
        crea.append(cf);
    } else {
        crea.replace(cf, m_settings.columnName());
    }
    return crea;
}
Also used : Pattern(java.util.regex.Pattern) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) WildcardMatcher(org.knime.base.util.WildcardMatcher) Matcher(java.util.regex.Matcher) 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) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 47 with StringCell

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

the class AdapterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    if (getNrInPorts() == 0 && getNrOutPorts() == 1) {
        // assume simple source node with one table output
        BufferedDataContainer cnt = exec.createDataContainer(createDefaultOutputSpec());
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new DataCell[] { new StringCell("Cell-1.1"), new IntCell(12), new DoubleCell(1.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(1), new DataCell[] { new StringCell("Cell-2.1"), new IntCell(22), new DoubleCell(2.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(2), new DataCell[] { new StringCell("Cell-3.1"), new IntCell(32), new DoubleCell(3.3) }));
        cnt.close();
        return new BufferedDataTable[] { cnt.getTable() };
    }
    return inObjects;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 48 with StringCell

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

the class NodeSettingsTest method testStringDataCell.

/**
 * Test write/read of StringCells.
 *
 * @throws Exception Should not happen.
 */
@Test
public void testStringDataCell() throws Exception {
    try {
        m_settings.addDataCell(null, new StringCell("null"));
        fail();
    } catch (IllegalArgumentException iae) {
        assertTrue(true);
    }
    String key = "nullDataCell";
    m_settings.addDataCell(key, null);
    assertTrue(m_settings.containsKey(key));
    assertTrue(m_settings.getDataCell(key) == null);
    DataCell nullCell = new StringCell("null");
    assertTrue(m_settings.getDataCell(key, nullCell) == null);
    key = "kDataCell";
    m_settings.addDataCell(key, new StringCell("B"));
    assertTrue(m_settings.containsKey(key));
    assertTrue(m_settings.getDataCell(key).equals(new StringCell("B")));
    assertTrue(m_settings.getDataCell(key, null).equals(new StringCell("B")));
    key += "array";
    m_settings.addDataCellArray(key, new DataCell[] { new StringCell("T"), new StringCell("P"), new StringCell("M") });
    assertTrue(m_settings.containsKey(key));
    DataCell[] a = m_settings.getDataCellArray(key);
    assertTrue(a[0].equals(new StringCell("T")));
    assertTrue(a[1].equals(new StringCell("P")));
    assertTrue(a[2].equals(new StringCell("M")));
    a = m_settings.getDataCellArray(key, new DataCell[0]);
    assertTrue(a[0].equals(new StringCell("T")));
    assertTrue(a[1].equals(new StringCell("P")));
    assertTrue(a[2].equals(new StringCell("M")));
    key = "kDataCell_array_0";
    m_settings.addDataCellArray(key, new DataCell[0]);
    assertTrue(m_settings.containsKey(key));
    assertTrue(m_settings.getDataCellArray(key).length == 0);
    assertTrue(m_settings.getDataCellArray(key, new DataCell[1]).length == 0);
    key = "kDataCell-";
    m_settings.addDataCellArray(key, (DataCell[]) null);
    assertTrue(m_settings.containsKey(key));
    assertTrue(m_settings.getDataCellArray(key) == null);
    key = "unknownDataCell";
    DataCell unknownCell = new FuzzyNumberCell(0.0, 1.0, 2.0);
    m_settings.addDataCell(key, unknownCell);
    assertTrue(m_settings.containsKey(key));
    assertTrue(unknownCell.equals(m_settings.getDataCell(key)));
}
Also used : StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) FuzzyNumberCell(org.knime.core.data.def.FuzzyNumberCell) Test(org.junit.Test)

Example 49 with StringCell

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

the class SubgroupMinerModel2 method createAssociationRulesOutput.

private BufferedDataTable createAssociationRulesOutput(final DataTableSpec inputSpec, final ExecutionContext exec, final AprioriAlgorithm apriori, final List<DataCell> nameMapping) {
    DataTableSpec outSpec = createAssociationRulesSpec(inputSpec);
    BufferedDataContainer ruleRows = exec.createDataContainer(outSpec);
    assert nameMapping != null;
    List<AssociationRule> associationRules = apriori.getAssociationRules(m_confidence.getDoubleValue());
    // for every association rule
    int rowKeyCounter = 0;
    for (AssociationRule r : associationRules) {
        // get the support
        double support = r.getSupport();
        // get the confidence
        double confidence = r.getConfidence();
        // get lift
        double lift = r.getLift();
        // get the antecedence (which is one item) -> cell
        FrequentItemSet antecedent = r.getAntecedent();
        // get the consequence
        FrequentItemSet consequent = r.getConsequent();
        DataCell[] allCells = new DataCell[6];
        allCells[0] = new DoubleCell(support);
        allCells[1] = new DoubleCell(confidence);
        allCells[2] = new DoubleCell(lift);
        // consequent is always only one item -> access with get(0) ok
        if (nameMapping.size() > consequent.getItems().get(0)) {
            allCells[3] = nameMapping.get(consequent.getItems().get(0));
        } else {
            allCells[3] = new StringCell("Item" + consequent.getItems().get(0));
        }
        allCells[4] = new StringCell("<---");
        Set<DataCell> allcells = new HashSet<DataCell>();
        for (int i = 0; i < antecedent.getItems().size() && i < m_maxItemSetLength.getIntValue() + 5; i++) {
            if (nameMapping.size() > antecedent.getItems().get(i)) {
                allcells.add(nameMapping.get(antecedent.getItems().get(i)));
            } else {
                allcells.add(new StringCell("Item" + antecedent.getItems().get(i)));
            }
        }
        allCells[5] = CollectionCellFactory.createSetCell(allcells);
        if (antecedent.getItems().size() > 0) {
            DataRow row = new DefaultRow("rule" + (rowKeyCounter++), allCells);
            ruleRows.addRowToTable(row);
        }
    }
    ruleRows.close();
    return ruleRows.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) DataRow(org.knime.core.data.DataRow) AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet)

Example 50 with StringCell

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

the class SVMPredictor method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    ArrayList<Double> values = new ArrayList<Double>();
    for (int i = 0; i < m_colindices.length; i++) {
        if (row.getCell(m_colindices[i]).isMissing()) {
            if (m_appendProbabilities) {
                DataCell[] ret = new DataCell[1 + m_svms.length];
                Arrays.fill(ret, new MissingCell("Missing value in input data."));
                return ret;
            }
            return new DataCell[] { DataType.getMissingCell() };
        }
        DoubleValue dv = (DoubleValue) row.getCell(m_colindices[i]);
        values.add(dv.getDoubleValue());
    }
    String classvalue = doPredict(values);
    if (m_appendProbabilities) {
        DataCell[] ret = new DataCell[m_svms.length + 1];
        double[] probabilities = computeProbabilities(values);
        assert ret.length == probabilities.length + 1 : ret.length + " vs. " + (probabilities.length + 1);
        for (int i = ret.length - 1; i-- > 0; ) {
            ret[i] = new DoubleCell(probabilities[i]);
        }
        ret[probabilities.length] = new StringCell(classvalue);
        return ret;
    }
    return new DataCell[] { new StringCell(classvalue) };
}
Also used : MissingCell(org.knime.core.data.MissingCell) DoubleValue(org.knime.core.data.DoubleValue) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

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