use of org.knime.core.util.Pair in project knime-core by knime.
the class LinReg2LearnerNodeView method modelChanged.
/**
* {@inheritDoc}
*/
@Override
protected void modelChanged() {
LinReg2LearnerNodeModel model = getNodeModel();
StringBuilder buffer = new StringBuilder();
buffer.append("<html>\n");
buffer.append("<head>\n");
buffer.append("<style type=\"text/css\">\n");
buffer.append("body {color:#333333;}");
buffer.append("table {width: 100%;margin: 7px 0 7px 0;}");
buffer.append("th {font-weight: bold;background-color: #aaccff;}");
buffer.append("td,th {padding: 4px 5px; }");
buffer.append(".numeric {text-align: right}");
buffer.append(".odd {background-color:#ddeeff;}");
buffer.append(".even {background-color:#ffffff;}");
buffer.append("</style>\n");
buffer.append("</head>\n");
buffer.append("<body>\n");
buffer.append("<h2>Statistics on Linear Regression</h2>");
if (model.isDataAvailable()) {
RegressionContent content = model.getRegressionContent();
List<String> parameters = content.getParameters();
buffer.append("<table>\n");
buffer.append("<tr>");
buffer.append("<th>Variable</th>");
buffer.append("<th>Coeff.</th>");
buffer.append("<th>Std. Err.</th>");
buffer.append("<th>t-value</th>");
buffer.append("<th>P>|t|</th>");
buffer.append("</tr>");
Map<Pair<String, Integer>, Double> coefficients = content.getCoefficients();
Map<Pair<String, Integer>, Double> stdErrs = content.getStandardErrors();
Map<Pair<String, Integer>, Double> tValues = content.getTValues();
Map<Pair<String, Integer>, Double> pValues = content.getPValues();
boolean odd = true;
for (String parameter : parameters) {
if (odd) {
buffer.append("<tr class=\"odd\">\n");
} else {
buffer.append("<tr class=\"even\">\n");
}
odd = !odd;
buffer.append("<td>");
buffer.append(StringEscapeUtils.escapeHtml(parameter));
buffer.append("</td>\n<td class=\"numeric\">");
Pair<String, Integer> pair = Pair.create(parameter, 1);
String coeff = DoubleFormat.formatDouble(coefficients.get(pair));
buffer.append(coeff);
buffer.append("</td>\n<td class=\"numeric\">");
String stdErr = DoubleFormat.formatDouble(stdErrs.get(pair));
buffer.append(stdErr);
buffer.append("</td>\n<td class=\"numeric\">");
String zScore = DoubleFormat.formatDouble(tValues.get(pair));
buffer.append(zScore);
buffer.append("</td>\n<td class=\"numeric\">");
String pValue = DoubleFormat.formatDouble(pValues.get(pair));
buffer.append(pValue);
buffer.append("</td>\n");
buffer.append("</tr>\n");
}
if (content.getIncludeConstant()) {
if (odd) {
buffer.append("<tr class=\"odd\">\n");
} else {
buffer.append("<tr class=\"even\">\n");
}
buffer.append("<td>");
buffer.append("Intercept");
buffer.append("</td>\n<td class=\"numeric\">");
String intercept = DoubleFormat.formatDouble(content.getIntercept());
buffer.append(intercept);
buffer.append("</td>\n<td class=\"numeric\">");
String stdErr = DoubleFormat.formatDouble(content.getInterceptStdErr());
buffer.append(stdErr);
buffer.append("</td>\n<td class=\"numeric\">");
String tValue = DoubleFormat.formatDouble(content.getInterceptTValue());
buffer.append(tValue);
buffer.append("</td>\n<td class=\"numeric\">");
String pValue = DoubleFormat.formatDouble(content.getInterceptPValue());
buffer.append(pValue);
buffer.append("</td>\n");
buffer.append("</tr>\n");
buffer.append("</tr>\n");
}
buffer.append("</table>\n");
if (!content.getIncludeConstant()) {
buffer.append("Offset Value: ");
String offsetValue = DoubleFormat.formatDouble(content.getOffsetValue());
buffer.append(offsetValue);
buffer.append("<br/>");
}
buffer.append("Multiple R-Squared: ");
String rSquared = DoubleFormat.formatDouble(content.getRSquared());
buffer.append(rSquared);
buffer.append("<br/>");
buffer.append("Adjusted R-Squared: ");
String adjustedRSquared = DoubleFormat.formatDouble(content.getAdjustedRSquared());
buffer.append(adjustedRSquared);
buffer.append("<br/>");
} else {
buffer.append("No parameters available.\n");
}
buffer.append("</body>\n");
buffer.append("</html>\n");
m_pane.setText(buffer.toString());
m_pane.revalidate();
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class DecisionTreeNodeSplitPMML method getChildUsedNominalSplitAttributeValues.
/**
* {@inheritDoc}
*/
@Override
protected Pair<String, Set<String>> getChildUsedNominalSplitAttributeValues(final int childIndex) {
PMMLPredicate pmmlPredicate = m_splitPred[childIndex];
Set<String> values = pmmlPredicate.getUsedNominalSplitAttributeValues();
if (values != null) {
String splitAttribute = pmmlPredicate.getSplitAttribute();
return new Pair<String, Set<String>>(splitAttribute, values);
}
return null;
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class BigGroupByTable method createGroupByTable.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable createGroupByTable(final ExecutionContext exec, final BufferedDataTable table, final DataTableSpec resultSpec, final int[] groupColIdx) throws CanceledExecutionException {
LOGGER.debug("Entering createGroupByTable(exec, table) " + "of class BigGroupByTable.");
final DataTableSpec origSpec = table.getDataTableSpec();
// sort the data table in order to process the input table chunk wise
final BufferedDataTable sortedTable;
final ExecutionContext groupExec;
final DataValueComparator[] comparators;
if (groupColIdx.length < 1) {
sortedTable = table;
groupExec = exec;
comparators = new DataValueComparator[0];
} else {
final ExecutionContext sortExec = exec.createSubExecutionContext(0.6);
exec.setMessage("Sorting input table...");
sortedTable = sortTable(sortExec, table, getGroupCols());
sortExec.setProgress(1.0);
groupExec = exec.createSubExecutionContext(0.4);
comparators = new DataValueComparator[groupColIdx.length];
for (int i = 0, length = groupColIdx.length; i < length; i++) {
final DataColumnSpec colSpec = origSpec.getColumnSpec(groupColIdx[i]);
comparators[i] = colSpec.getType().getComparator();
}
}
final BufferedDataContainer dc = exec.createDataContainer(resultSpec);
exec.setMessage("Creating groups");
final DataCell[] previousGroup = new DataCell[groupColIdx.length];
final DataCell[] currentGroup = new DataCell[groupColIdx.length];
final MutableInteger groupCounter = new MutableInteger(0);
boolean firstRow = true;
final double numOfRows = sortedTable.size();
long rowCounter = 0;
// In the rare case that the DataCell comparator return 0 for two
// data cells that are not equal we have to maintain a map with all
// rows with equal cells in the group columns per chunk.
// This variable stores for each chunk these members. A chunk consists
// of rows which return 0 for the pairwise group value comparison.
// Usually only equal data cells return 0 when compared with each other
// but in rare occasions also data cells that are NOT equal return 0 when
// compared to each other
// (such as cells that contain chemical structures).
// In this rare case this map will contain for each group of data cells
// that are pairwise equal in the chunk a separate entry.
final Map<GroupKey, Pair<ColumnAggregator[], Set<RowKey>>> chunkMembers = new LinkedHashMap<>(3);
boolean logUnusualCells = true;
String groupLabel = "";
// cannot put init to the constructor, as the super() constructor directly calls the current function
initMissingValuesMap();
for (final DataRow row : sortedTable) {
// fetch the current group column values
for (int i = 0, length = groupColIdx.length; i < length; i++) {
currentGroup[i] = row.getCell(groupColIdx[i]);
}
if (firstRow) {
groupLabel = createGroupLabelForProgress(currentGroup);
System.arraycopy(currentGroup, 0, previousGroup, 0, currentGroup.length);
firstRow = false;
}
// group column data cells
if (!sameChunk(comparators, previousGroup, currentGroup)) {
groupLabel = createGroupLabelForProgress(currentGroup);
createTableRows(dc, chunkMembers, groupCounter);
// set the current group as previous group
System.arraycopy(currentGroup, 0, previousGroup, 0, currentGroup.length);
if (logUnusualCells && chunkMembers.size() > 1) {
// cause the problem
if (LOGGER.isEnabledFor(LEVEL.INFO)) {
final StringBuilder buf = new StringBuilder();
buf.append("Data chunk with ");
buf.append(chunkMembers.size());
buf.append(" members occured in groupby node. " + "Involved classes are: ");
final GroupKey key = chunkMembers.keySet().iterator().next();
for (final DataCell cell : key.getGroupVals()) {
buf.append(cell.getClass().getCanonicalName());
buf.append(", ");
}
LOGGER.info(buf.toString());
}
logUnusualCells = false;
}
// reset the chunk members map
chunkMembers.clear();
}
// process the row as one of the members of the current chunk
Pair<ColumnAggregator[], Set<RowKey>> member = chunkMembers.get(new GroupKey(currentGroup));
if (member == null) {
Set<RowKey> rowKeys;
if (isEnableHilite()) {
rowKeys = new HashSet<>();
} else {
rowKeys = Collections.emptySet();
}
member = new Pair<>(cloneColumnAggregators(), rowKeys);
final DataCell[] groupKeys = new DataCell[currentGroup.length];
System.arraycopy(currentGroup, 0, groupKeys, 0, currentGroup.length);
chunkMembers.put(new GroupKey(groupKeys), member);
}
// compute the current row values
for (final ColumnAggregator colAggr : member.getFirst()) {
final int colIdx = origSpec.findColumnIndex(colAggr.getOriginalColName());
colAggr.getOperator(getGlobalSettings()).compute(row, colIdx);
}
if (isEnableHilite()) {
member.getSecond().add(row.getKey());
}
groupExec.checkCanceled();
groupExec.setProgress(++rowCounter / numOfRows, groupLabel);
}
// create the final row for the last chunk after processing the last
// table row
createTableRows(dc, chunkMembers, groupCounter);
dc.close();
return dc.getTable();
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class DBAutoBinner method intoBinnerMaps.
/**
* This method translates a {@link PMMLPortObject} into a {@link DBBinnerMaps} object which holds several Maps
* needed to create a binner statement in {@link StatementManipulator}
*
* @param pmmlPortObject A {@link PMMLPortObject} containing all necessary information about binning operation
* @param dataTableSpec Incoming {@link DataTableSpec}
* @return a {@link DBBinnerMaps} object containing required parameters for {@link StatementManipulator}
*/
public static DBBinnerMaps intoBinnerMaps(final PMMLPortObject pmmlPortObject, final DataTableSpec dataTableSpec) {
Map<String, List<Pair<Double, Double>>> boundariesMap = new LinkedHashMap<>();
Map<String, List<Pair<Boolean, Boolean>>> boundariesOpenMap = new LinkedHashMap<>();
Map<String, List<String>> namingMap = new LinkedHashMap<>();
Map<String, String> appendMap = new LinkedHashMap<>();
DerivedField[] derivedFields = pmmlPortObject.getDerivedFields();
for (int i = 0; i < derivedFields.length; i++) {
// each column has its own derived fields
List<Pair<Double, Double>> boundaries = new ArrayList<>();
List<String> names = new ArrayList<>();
List<Pair<Boolean, Boolean>> boundariesOpen = new ArrayList<>();
List<DiscretizeBin> discretizeBinList = derivedFields[i].getDiscretize().getDiscretizeBinList();
String replacedColumnName = DataTableSpec.getUniqueColumnName(dataTableSpec, derivedFields[i].getName());
String originalColumnName = derivedFields[i].getDiscretize().getField();
for (DiscretizeBin discBin : discretizeBinList) {
Interval interval = discBin.getInterval();
double left = interval.isSetLeftMargin() ? interval.getLeftMargin() : Double.NEGATIVE_INFINITY;
double right = interval.isSetRightMargin() ? interval.getRightMargin() : Double.POSITIVE_INFINITY;
boundaries.add(new Pair<>(left, right));
names.add(discBin.getBinValue());
boolean leftOpen;
boolean rightOpen;
int closure = discBin.getInterval().xgetClosure().enumValue().intValue();
/*
*static final int INT_OPEN_CLOSED = 1;
*static final int INT_OPEN_OPEN = 2;
*static final int INT_CLOSED_OPEN = 3;
*static final int INT_CLOSED_CLOSED = 4;
*/
switch(closure) {
case 1:
leftOpen = true;
rightOpen = false;
break;
case 2:
leftOpen = true;
rightOpen = true;
break;
case 3:
leftOpen = false;
rightOpen = true;
break;
case 4:
leftOpen = false;
rightOpen = false;
break;
default:
leftOpen = true;
rightOpen = false;
break;
}
boundariesOpen.add(new Pair<>(leftOpen, rightOpen));
}
boundariesMap.put(originalColumnName, boundaries);
namingMap.put(originalColumnName, names);
boundariesOpenMap.put(originalColumnName, boundariesOpen);
if (replacedColumnName.matches("(.*)" + originalColumnName + "\\*" + "(.*)")) {
appendMap.put(originalColumnName, null);
} else {
appendMap.put(originalColumnName, replacedColumnName);
}
}
DBBinnerMaps maps = new DBBinnerMaps(boundariesMap, boundariesOpenMap, namingMap, appendMap);
return maps;
}
use of org.knime.core.util.Pair in project knime-core by knime.
the class DefaultColumnHandler method getOutSpecAndWarning.
/**
* {@inheritDoc}
* @throws InvalidSettingsException
*/
@Override
public Pair<String, DataTableSpec> getOutSpecAndWarning(final Collection<Integer> features, final DataTableSpec inSpec, final boolean includeConstantColumns) throws InvalidSettingsException {
Collection<String> colNames = getIncludedColumns(features, includeConstantColumns);
int missing = 0;
String missingTxt = "The following columns used in the selected level are missing in the input table: ";
for (String colName : colNames) {
if (!inSpec.containsName(colName)) {
missing++;
missingTxt += colName + ", ";
}
}
final String warning;
final int numCols = includeConstantColumns ? features.size() + 1 : features.size();
if (numCols > 0 && missing == numCols) {
throw new InvalidSettingsException("Input table does not contain " + "any of the columns used in the feature selection ");
} else if (missing > 0) {
warning = missingTxt.substring(0, missingTxt.length() - 2);
} else {
warning = null;
}
final ColumnRearranger cr = createRearranger(features, inSpec, includeConstantColumns);
return new Pair<String, DataTableSpec>(warning, cr.createSpec());
}
Aggregations