Search in sources :

Example 31 with DoubleValue

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

the class ColorManager2NodeModel method createRangeColorHandler.

private static final ColorHandler createRangeColorHandler(final DataCell lower, final DataCell upper, final Map<DataCell, ColorAttr> map) {
    assert map.size() == 2;
    Color c0 = map.get(MIN_VALUE).getColor();
    Color c1 = map.get(MAX_VALUE).getColor();
    double d0 = Double.NaN;
    if (lower != null && !lower.isMissing() && lower.getType().isCompatible(DoubleValue.class)) {
        d0 = ((DoubleValue) lower).getDoubleValue();
    }
    double d1 = Double.NaN;
    if (upper != null && !upper.isMissing() && upper.getType().isCompatible(DoubleValue.class)) {
        d1 = ((DoubleValue) upper).getDoubleValue();
    }
    return new ColorHandler(new ColorModelRange(d0, c0, d1, c1));
}
Also used : ColorModelRange(org.knime.core.data.property.ColorModelRange) DoubleValue(org.knime.core.data.DoubleValue) Color(java.awt.Color) ColorHandler(org.knime.core.data.property.ColorHandler)

Example 32 with DoubleValue

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

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

Example 34 with DoubleValue

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

the class Distances method getCosinusDistance.

/**
 * Computes the cosinus distance between the given two rows, with given
 * offset.
 *
 * @param row1 first row to compute the cosinus distance of
 * @param row2 second row to compute the cosinus distance of
 * @param offset offset to substract cosinus distance from
 * @param fuzzy if <code>true</code> only fuzzy data is respected, if
 *            <code>false</code> only number data
 * @return the cosinus distance between the given two rows
 */
public static double getCosinusDistance(final DataRow row1, final DataRow row2, final double offset, final boolean fuzzy) {
    double distance = 0;
    double vectorMultRes = 0;
    double vector1Length = 0;
    double vector2Length = 0;
    for (int i = 0; i < row1.getNumCells(); i++) {
        DataType type1 = row1.getCell(i).getType();
        DataType type2 = row2.getCell(i).getType();
        if (SotaUtil.isNumberType(type1) && SotaUtil.isNumberType(type2) && !fuzzy) {
            vectorMultRes += ((DoubleValue) row1.getCell(i)).getDoubleValue() * ((DoubleValue) row2.getCell(i)).getDoubleValue();
            vector1Length += Math.pow(((DoubleValue) row1.getCell(i)).getDoubleValue(), 2);
            vector2Length += Math.pow(((DoubleValue) row2.getCell(i)).getDoubleValue(), 2);
        } else if (SotaUtil.isFuzzyIntervalType(type1) && SotaUtil.isFuzzyIntervalType(type2) && fuzzy) {
            vectorMultRes += SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row1.getCell(i)) * SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row2.getCell(i));
            vector1Length += Math.pow(SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row1.getCell(i)), 2);
            vector2Length += Math.pow(SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row2.getCell(i)), 2);
        }
    }
    vector1Length = Math.sqrt(vector1Length);
    vector2Length = Math.sqrt(vector2Length);
    distance = vectorMultRes / (vector1Length * vector2Length);
    distance = offset - distance;
    return distance;
}
Also used : FuzzyIntervalValue(org.knime.core.data.FuzzyIntervalValue) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType)

Example 35 with DoubleValue

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

the class Distances method getMinkowskiDistance.

/**
 * Calculates the Minkowski distance between a regular <code>DataRow</code>
 * and a <code>SotaTreeCell</code>. If fuzzy is set true only columns with
 * cells containing numbers are used to compute the distance. If the number
 * of columns, which are used to compute the distance, contained in the
 * given <code>DataRow</code> is different to the number of cells contained
 * in the given <code>SotaTreeCell</code>, only the first <i>n</i> columns
 * of the <code>DataRow</code> or <i>n</i> cells of the
 * <code>SotaTreeCell</code> are used to compute the distance. The rest is
 * simply ignored.
 * The given power specifies the distance kind, i.e. if power is set to 2
 * the euclidean distance will be computed.
 *
 * @param power The power to use.
 * @param row The row to compute the distance.
 * @param cell The cell to compute the distance.
 * @param fuzzy If true only fuzzy data is taken into account, if
 * <code>false</code> only number data.
 *
 * @return Minkowski distance between the two rows.
 */
public static double getMinkowskiDistance(final int power, final DataRow row, final SotaTreeCell cell, final boolean fuzzy) {
    int col = 0;
    double distance = 0;
    for (int i = 0; i < row.getNumCells(); i++) {
        DataType type = row.getCell(i).getType();
        if (SotaUtil.isNumberType(type) && !fuzzy) {
            if (col < cell.getData().length) {
                distance += Math.pow((cell.getData()[col].getValue() - ((DoubleValue) row.getCell(i)).getDoubleValue()), power);
                col++;
            }
        } else if (SotaUtil.isFuzzyIntervalType(type) && fuzzy) {
            if (col < cell.getData().length) {
                distance += Math.pow(cell.getData()[col].getValue() - SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row.getCell(i)), power);
                col++;
            }
        }
    }
    return Math.pow(distance, (double) 1 / (double) power);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType)

Aggregations

DoubleValue (org.knime.core.data.DoubleValue)154 DataCell (org.knime.core.data.DataCell)103 DataRow (org.knime.core.data.DataRow)71 DataColumnSpec (org.knime.core.data.DataColumnSpec)38 DataTableSpec (org.knime.core.data.DataTableSpec)38 DoubleCell (org.knime.core.data.def.DoubleCell)32 ArrayList (java.util.ArrayList)26 BufferedDataTable (org.knime.core.node.BufferedDataTable)26 DataType (org.knime.core.data.DataType)23 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)21 LinkedHashMap (java.util.LinkedHashMap)18 IntValue (org.knime.core.data.IntValue)15 HashMap (java.util.HashMap)14 RowIterator (org.knime.core.data.RowIterator)14 RowKey (org.knime.core.data.RowKey)13 DefaultRow (org.knime.core.data.def.DefaultRow)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 LongValue (org.knime.core.data.LongValue)10 StringValue (org.knime.core.data.StringValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10