use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class RuleSetToTable method createRow.
/**
* Creates a row, {@link DataCell} values based on {@code rule} and the other parameters.
*
* @param rule A PMML {@link Rule}.
* @param outcomeType The expected outcome.
* @param types The types of the input column.
* @return The cells for the {@code rule}.
*/
private DataCell[] createRow(final Rule rule, final DataType outcomeType, final Map<String, DataType> types, final List<String> scoreValues) {
List<DataCell> ret = new ArrayList<>();
boolean usePrecedence = !m_settings.getAdditionalParentheses().getBooleanValue();
if (m_settings.getSplitRules().getBooleanValue()) {
ret.add(new StringCell(convertToString(rule.getCondition(), usePrecedence, types)));
ret.add(convertToExpectedType(rule.getOutcome(), outcomeType));
} else {
ret.add(new StringCell(convertToString(rule.getCondition(), usePrecedence, types) + " => " + toString(convertToExpectedType(rule.getOutcome(), outcomeType))));
}
if (m_settings.getConfidenceAndWeight().getBooleanValue()) {
ret.add(toCell(rule.getConfidence()));
ret.add(toCell(rule.getWeight()));
}
if (m_settings.getProvideStatistics().getBooleanValue()) {
ret.add(toCell(rule.getRecordCount()));
ret.add(toCell(rule.getNbCorrect()));
}
final Map<String, ScoreProbabilityAndRecordCount> scoreDistribution = rule.getScoreDistribution();
if (m_settings.getScoreTableRecordCount().isEnabled() && m_settings.getScoreTableRecordCount().getBooleanValue()) {
for (final String value : scoreValues) {
if (scoreDistribution.containsKey(value)) {
ret.add(new DoubleCell(scoreDistribution.get(value).getRecordCount()));
} else {
ret.add(DataType.getMissingCell());
}
}
}
if (m_settings.getScoreTableProbability().isEnabled() && m_settings.getScoreTableProbability().getBooleanValue()) {
for (final String value : scoreValues) {
if (scoreDistribution.containsKey(value)) {
final BigDecimal probability = scoreDistribution.get(value).getProbability();
ret.add(probability == null ? DataType.getMissingCell() : new DoubleCell(probability.doubleValue()));
} else {
ret.add(DataType.getMissingCell());
}
}
}
return ret.toArray(new DataCell[ret.size()]);
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class TimeDifferenceNodeModel method getRoundedTimeDifference.
/**
* @param first the older time
* @param last the newer time
* @param g the granularity
* @return a double cell containing the time difference between the first
* and the last, already granulated and rounded.
*/
private DoubleCell getRoundedTimeDifference(final long first, final long last, final Granularity g) {
double diffTime = (last - first) / g.getFactor();
BigDecimal bd = new BigDecimal(diffTime);
bd = bd.setScale(m_rounding.getIntValue(), BigDecimal.ROUND_CEILING);
return new DoubleCell(bd.doubleValue());
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class TSAverageHandler method getMean.
private DataCell getMean() {
if (m_previous instanceof IntValue) {
// get an int, create an int
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new IntCell((int) Math.round(mean));
}
if (m_previous instanceof LongValue) {
// get an int, create an int
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new LongCell(Math.round(mean));
}
if (m_previous instanceof DoubleValue) {
// get an double, create an double
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new DoubleCell(mean);
}
if (m_previous instanceof DateAndTimeValue) {
// get an int, create an int
DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
double d = dataCell1.getUTCTimeInMillis() + dataCell2.getUTCTimeInMillis();
d *= 0.5;
return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
}
return DataType.getMissingCell();
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class TSLinearHandler method getLinearInterpolation.
private DataCell getLinearInterpolation(final double stepnumber, final double stepcount) {
if (m_previous instanceof DoubleValue || m_previous instanceof IntValue || m_previous instanceof LongValue) {
double prev = ((DoubleValue) m_previous).getDoubleValue();
double next = ((DoubleValue) m_next).getDoubleValue();
double lin = prev + 1.0 * stepnumber / (1.0 * stepcount) * (next - prev);
if (m_previous instanceof IntValue) {
// get an int, create an int
return new IntCell((int) Math.round(lin));
}
if (m_previous instanceof LongValue) {
// get an long, create an long
return new LongCell(Math.round(lin));
}
return new DoubleCell(lin);
}
if (m_previous instanceof DateAndTimeValue) {
// get an int, create an int
DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
double prev = dataCell1.getUTCTimeInMillis();
double next = dataCell2.getUTCTimeInMillis();
double d = prev + stepnumber / stepcount * (next - prev);
return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
}
return DataType.getMissingCell();
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class StringManipulationVariableNodeModel method calculate.
/**
* @throws CompilationFailedException
* @throws InstantiationException
* @throws Exception
*/
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
if (m_settings == null || m_settings.getExpression() == null) {
throw new InvalidSettingsException("No expression has been set.");
}
JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
settings.setInputAndCompile(new DataTableSpec());
// calculate the result
ColumnCalculator cc = new ColumnCalculator(settings, this);
DataCell calculate = null;
try {
calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
} catch (NoSuchElementException e) {
throw new InvalidSettingsException(e.getMessage());
}
String newVariableName;
Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
if (m_settings.isReplace()) {
newVariableName = m_settings.getColName();
CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
} else {
newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
}
// convert and push result as flow variable
CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
Class<? extends DataCell> cellType = calculate.getClass();
if (cellType.equals(IntCell.class)) {
pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
} else if (cellType.equals(DoubleCell.class)) {
pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
} else if (cellType.equals(StringCell.class)) {
pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
} else {
throw new RuntimeException("Invalid variable class: " + cellType);
}
}
Aggregations