Search in sources :

Example 6 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class ColCombineNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
    ColumnRearranger result = new ColumnRearranger(spec);
    DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
    final int[] indices = new int[m_columns.length];
    List<String> colNames = Arrays.asList(m_columns);
    int j = 0;
    for (int k = 0; k < spec.getNumColumns(); k++) {
        DataColumnSpec cs = spec.getColumnSpec(k);
        if (colNames.contains(cs.getName())) {
            indices[j] = k;
            j++;
        }
    }
    // ", " -> ","
    // "  " -> "  " (do not let the resulting string be empty)
    // " bla bla " -> "bla bla"
    final String delimTrim = trimDelimString(m_delimString);
    result.append(new SingleCellFactory(append) {

        @Override
        public DataCell getCell(final DataRow row) {
            String[] cellContents = new String[indices.length];
            for (int i = 0; i < indices.length; i++) {
                DataCell c = row.getCell(indices[i]);
                String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
                cellContents[i] = s;
            }
            return new StringCell(handleContent(cellContents, delimTrim));
        }
    });
    return result;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 7 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class StringValueRenderer method setValue.

/**
 * Formats the object. If <code>value</code> is instance of
 * <code>StringValue</code>, the object's <code>getStringValue</code>
 * is used. Otherwise the fallback: <code>value.toString()</code>
 * @param value The value to be rendered.
 * @see javax.swing.table.DefaultTableCellRenderer#setValue(Object)
 */
@Override
protected void setValue(final Object value) {
    Object newValue;
    if (value instanceof StringValue) {
        StringValue cell = (StringValue) value;
        newValue = cell.getStringValue();
    } else {
        // missing data cells will also end up here
        newValue = value;
    }
    super.setValue(newValue);
}
Also used : StringValue(org.knime.core.data.StringValue)

Example 8 with StringValue

use of org.knime.core.data.StringValue 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 9 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class SVMLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    LearnColumnsAndColumnRearrangerTuple tuple = createTrainTableColumnRearranger(inSpec);
    // no progress needed as constant operation (column removal only)
    BufferedDataTable trainTable = exec.createColumnRearrangeTable(inTable, tuple.getTrainingRearranger(), exec.createSubProgress(0.0));
    DataTableSpec trainSpec = trainTable.getDataTableSpec();
    int classpos = trainSpec.findColumnIndex(m_classcol.getStringValue());
    CheckUtils.checkArgument(classpos >= 0, "Selected class column not found: " + m_classcol.getStringValue());
    // convert input data
    ArrayList<DoubleVector> inputData = new ArrayList<DoubleVector>();
    List<String> categories = new ArrayList<String>();
    StringValue classvalue = null;
    for (DataRow row : trainTable) {
        exec.checkCanceled();
        ArrayList<Double> values = new ArrayList<Double>();
        boolean add = true;
        for (int i = 0; i < row.getNumCells(); i++) {
            if (row.getCell(i).isMissing()) {
                add = false;
                break;
            }
            if (i != classpos) {
                DoubleValue cell = (DoubleValue) row.getCell(i);
                values.add(cell.getDoubleValue());
            } else {
                classvalue = (StringValue) row.getCell(classpos);
                if (!categories.contains(classvalue.getStringValue())) {
                    categories.add(classvalue.getStringValue());
                }
            }
        }
        if (add) {
            @SuppressWarnings("null") final String nonNullClassValue = classvalue.getStringValue();
            inputData.add(new DoubleVector(row.getKey(), values, nonNullClassValue));
        }
    }
    if (categories.isEmpty()) {
        throw new Exception("No categories found to train SVM. " + "Possibly an empty input table was provided.");
    }
    DoubleVector[] inputDataArr = new DoubleVector[inputData.size()];
    inputDataArr = inputData.toArray(inputDataArr);
    Kernel kernel = KernelFactory.getKernel(m_kernelType);
    Vector<SettingsModelDouble> kernelparams = m_kernelParameters.get(m_kernelType);
    for (int i = 0; i < kernel.getNumberParameters(); ++i) {
        kernel.setParameter(i, kernelparams.get(i).getDoubleValue());
    }
    final Svm[] svms = new Svm[categories.size()];
    exec.setMessage("Training SVM");
    final BinarySvmRunnable[] bst = new BinarySvmRunnable[categories.size()];
    for (int i = 0; i < categories.size(); i++) {
        bst[i] = new BinarySvmRunnable(inputDataArr, categories.get(i), kernel, m_paramC.getDoubleValue(), exec.createSubProgress((1.0 / categories.size())));
    }
    ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
    final Future<?>[] fut = new Future<?>[bst.length];
    KNIMETimer timer = KNIMETimer.getInstance();
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            try {
                exec.checkCanceled();
            } catch (final CanceledExecutionException ce) {
                for (int i = 0; i < fut.length; i++) {
                    if (fut[i] != null) {
                        fut[i].cancel(true);
                    }
                }
                super.cancel();
            }
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 3000);
    for (int i = 0; i < bst.length; i++) {
        fut[i] = pool.enqueue(bst[i]);
    }
    try {
        pool.runInvisible(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                for (int i = 0; i < fut.length; ++i) {
                    fut[i].get();
                    bst[i].ok();
                    if (bst[i].getWarning() != null) {
                        setWarningMessage(bst[i].getWarning());
                    }
                    svms[i] = bst[i].getSvm();
                }
                return null;
            }
        });
    } catch (Exception ex) {
        exec.checkCanceled();
        Throwable t = ex;
        if (ex instanceof ExecutionException) {
            t = ex.getCause();
        }
        if (t instanceof Exception) {
            throw (Exception) t;
        } else {
            throw new Exception(t);
        }
    } finally {
        for (int i = 0; i < fut.length; i++) {
            fut[i].cancel(true);
        }
        timerTask.cancel();
    }
    // the optional PMML input (can be null)
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    // create the outgoing PMML spec
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(inPMMLPort, inSpec);
    specCreator.setLearningCols(trainSpec);
    specCreator.setTargetCol(trainSpec.getColumnSpec(m_classcol.getStringValue()));
    // create the outgoing PMML port object
    PMMLPortObject outPMMLPort = new PMMLPortObject(specCreator.createSpec(), inPMMLPort, inSpec);
    outPMMLPort.addModelTranslater(new PMMLSVMTranslator(categories, Arrays.asList(svms), kernel));
    m_svms = svms;
    return new PortObject[] { outPMMLPort };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLSVMTranslator(org.knime.base.node.mine.svm.PMMLSVMTranslator) ArrayList(java.util.ArrayList) ThreadPool(org.knime.core.util.ThreadPool) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Svm(org.knime.base.node.mine.svm.Svm) DataRow(org.knime.core.data.DataRow) TimerTask(java.util.TimerTask) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) StringValue(org.knime.core.data.StringValue) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Kernel(org.knime.base.node.mine.svm.kernel.Kernel) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) KNIMETimer(org.knime.core.util.KNIMETimer) BinarySvmRunnable(org.knime.base.node.mine.svm.util.BinarySvmRunnable) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Future(java.util.concurrent.Future) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 10 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class SotaManager method assignDataToChildren.

/**
 * Assigns the cells DataIds to its children. This is needed after a split
 * of a cell.
 *
 * @param cell cell with DataIds to assign to its children
 * @throws CanceledExecutionException if user canceled the process
 */
private void assignDataToChildren(final SotaTreeCell cell) throws CanceledExecutionException {
    if (cell.getDataIds().size() > 2) {
        for (int i = 0; i < cell.getDataIds().size(); i++) {
            DataRow row = m_inDataContainer.getRow(cell.getDataIds().get(i));
            // find winner for current row
            SotaTreeCell winner;
            double tmpDist1, tmpDist2;
            tmpDist1 = m_distanceManager.getDistance(row, cell.getLeft());
            tmpDist2 = m_distanceManager.getDistance(row, cell.getRight());
            if (tmpDist1 > tmpDist2) {
                winner = cell.getRight();
            } else {
                winner = cell.getLeft();
            }
            // add data row id to winners data ids
            if (winner.getDataIds().indexOf(cell.getDataIds().get(i)) == -1) {
                winner.getDataIds().add(cell.getDataIds().get(i));
            }
            // get class string for row
            String cellClass = null;
            if (m_indexOfClassColumn >= 0) {
                DataCell dataCell = row.getCell(m_indexOfClassColumn);
                if (dataCell instanceof StringValue) {
                    cellClass = ((StringValue) dataCell).getStringValue();
                }
            }
            adjustCell(winner, row, cellClass);
            m_exec.checkCanceled();
        }
    } else if (cell.getDataIds().size() == 2) {
        DataRow row1 = m_inDataContainer.getRow(cell.getDataIds().get(0));
        DataRow row2 = m_inDataContainer.getRow(cell.getDataIds().get(1));
        // add data row id to winners data ids
        if (cell.getLeft().getDataIds().indexOf(cell.getDataIds().get(0)) == -1) {
            cell.getLeft().getDataIds().add(cell.getDataIds().get(0));
        }
        if (cell.getRight().getDataIds().indexOf(cell.getDataIds().get(1)) == -1) {
            cell.getRight().getDataIds().add(cell.getDataIds().get(1));
        }
        // get class string for rows
        String cellClass1 = null;
        String cellClass2 = null;
        if (m_indexOfClassColumn >= 0) {
            DataCell dataCell1 = row1.getCell(m_indexOfClassColumn);
            DataCell dataCell2 = row1.getCell(m_indexOfClassColumn);
            if (dataCell1 instanceof StringValue) {
                cellClass1 = ((StringValue) dataCell1).getStringValue();
            }
            if (dataCell2 instanceof StringValue) {
                cellClass2 = ((StringValue) dataCell2).getStringValue();
            }
        }
        adjustCell(cell.getLeft(), row1, cellClass1);
        adjustCell(cell.getRight(), row2, cellClass2);
        m_exec.checkCanceled();
    }
}
Also used : DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) DataRow(org.knime.core.data.DataRow)

Aggregations

StringValue (org.knime.core.data.StringValue)33 DataCell (org.knime.core.data.DataCell)25 DataRow (org.knime.core.data.DataRow)22 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)10 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)10 DoubleValue (org.knime.core.data.DoubleValue)8 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)8 ArrayList (java.util.ArrayList)7 DataTableSpec (org.knime.core.data.DataTableSpec)7 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)7 StringCell (org.knime.core.data.def.StringCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)6 ParseException (java.text.ParseException)5 DataType (org.knime.core.data.DataType)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 PortObject (org.knime.core.node.port.PortObject)5 IOException (java.io.IOException)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)4 BitVectorType (org.knime.core.data.vector.bitvector.BitVectorType)3