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));
}
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 };
}
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) };
}
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;
}
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);
}
Aggregations