Search in sources :

Example 26 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class GroupByTable method createGroupByTableSpec.

/**
 * @param spec the original {@link DataTableSpec}
 * @param groupColNames the name of all columns to group by
 * @param columnAggregators the aggregation columns with the
 * aggregation method to use in the order the columns should be appear
 * in the result table
 * @param colNamePolicy the {@link ColumnNamePolicy} for the aggregation
 * columns
 * @return the result {@link DataTableSpec}
 */
public static final DataTableSpec createGroupByTableSpec(final DataTableSpec spec, final List<String> groupColNames, final ColumnAggregator[] columnAggregators, final ColumnNamePolicy colNamePolicy) {
    if (spec == null) {
        throw new NullPointerException("DataTableSpec must not be null");
    }
    if (groupColNames == null) {
        throw new IllegalArgumentException("groupColNames must not be null");
    }
    if (columnAggregators == null) {
        throw new NullPointerException("colMethods must not be null");
    }
    final int noOfCols = groupColNames.size() + columnAggregators.length;
    final DataColumnSpec[] colSpecs = new DataColumnSpec[noOfCols];
    int colIdx = 0;
    // add the group columns first
    final Map<String, MutableInteger> colNameCount = new HashMap<>(noOfCols);
    for (final String colName : groupColNames) {
        final DataColumnSpec colSpec = spec.getColumnSpec(colName);
        if (colSpec == null) {
            throw new IllegalArgumentException("No column spec found for name: " + colName);
        }
        colSpecs[colIdx++] = colSpec;
        colNameCount.put(colName, new MutableInteger(1));
    }
    // add the aggregation columns
    for (final ColumnAggregator aggrCol : columnAggregators) {
        final DataColumnSpec origSpec = spec.getColumnSpec(aggrCol.getOriginalColName());
        if (origSpec == null) {
            throw new IllegalArgumentException("No column spec found for name: " + aggrCol.getOriginalColName());
        }
        final String colName = colNamePolicy.createColumName(aggrCol);
        // since a column could be used several times create a unique name
        final String uniqueName;
        if (colNameCount.containsKey(colName)) {
            final MutableInteger counter = colNameCount.get(colName);
            uniqueName = colName + "_" + counter.intValue();
            counter.inc();
        } else {
            colNameCount.put(colName, new MutableInteger(1));
            uniqueName = colName;
        }
        final DataColumnSpec newSpec = aggrCol.getMethodTemplate().createColumnSpec(uniqueName, origSpec);
        colSpecs[colIdx++] = newSpec;
    }
    return new DataTableSpec(colSpecs);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnAggregator(org.knime.base.data.aggregation.ColumnAggregator) HashMap(java.util.HashMap) MutableInteger(org.knime.core.util.MutableInteger)

Example 27 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ClassAttributeModel method toString.

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    final StringBuilder buf = new StringBuilder();
    buf.append("Attribute name: ");
    buf.append(getAttributeName());
    buf.append("\t");
    buf.append("No of records: ");
    buf.append(m_totalNoOfRecs);
    buf.append("\n");
    for (final String classVal : m_recsCounterByClassVal.keySet()) {
        final MutableInteger integer = m_recsCounterByClassVal.get(classVal);
        buf.append(classVal);
        buf.append("|");
        buf.append(integer.intValue());
        buf.append("\n");
    }
    return buf.toString();
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 28 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ClassAttributeModel method addValueInternal.

/**
 * {@inheritDoc}
 */
@Override
void addValueInternal(final String classValue, final DataCell attrValue) throws TooManyValuesException {
    if (attrValue.isMissing()) {
        throw new IllegalArgumentException("Missing value not allowed as class value");
    }
    MutableInteger classCounter = m_recsCounterByClassVal.get(classValue);
    if (classCounter == null) {
        if (m_recsCounterByClassVal.size() > m_maxNoOfClassVals) {
            throw new TooManyValuesException("Class value " + classValue + " doesn't fit into model");
        }
        classCounter = new MutableInteger(0);
        m_recsCounterByClassVal.put(classValue, classCounter);
    }
    classCounter.inc();
    m_totalNoOfRecs++;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) TooManyValuesException(org.knime.base.node.mine.bayes.naivebayes.datamodel2.TooManyValuesException)

Example 29 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class NominalAttributeModel method getHTMLView.

/**
 * {@inheritDoc}
 */
@Override
String getHTMLView(final int totalNoOfRecs) {
    final List<String> sortedClassVal = AttributeModel.sortCollection(m_classValues.keySet());
    if (sortedClassVal == null) {
        return "";
    }
    final List<String> sortedAttrValues = AttributeModel.sortCollection(m_attributeVals);
    final String classHeading = "Class/" + getAttributeName();
    final String missingHeading = getMissingValueHeader(m_attributeVals);
    final int arraySize;
    if (missingHeading != null) {
        arraySize = sortedAttrValues.size() + 1;
    } else {
        arraySize = sortedAttrValues.size();
    }
    final StringBuilder buf = new StringBuilder();
    buf.append("<table border='1' width='100%'>");
    buf.append(createTableHeader(classHeading, sortedAttrValues, missingHeading));
    final int[] rowsPerValCounts = new int[arraySize];
    Arrays.fill(rowsPerValCounts, 0);
    // create the value section
    for (final String classVal : sortedClassVal) {
        final NominalClassValue classValue = m_classValues.get(classVal);
        buf.append("<tr>");
        buf.append("<th>");
        buf.append(StringEscapeUtils.escapeHtml(classVal));
        buf.append("</th>");
        for (int i = 0, length = sortedAttrValues.size(); i < length; i++) {
            final String attrVal = sortedAttrValues.get(i);
            final MutableInteger rowCounter = classValue.getNoOfRows4AttributeValue(attrVal);
            final int rowCount;
            if (rowCounter != null) {
                rowCount = rowCounter.intValue();
            } else {
                rowCount = 0;
            }
            rowsPerValCounts[i] += rowCount;
            buf.append("<td align='center'>");
            buf.append(rowCount);
            buf.append("</td>");
        }
        if (missingHeading != null) {
            final MutableInteger rowCounter = classValue.getMissingValueRecs();
            rowsPerValCounts[arraySize - 1] += rowCounter.intValue();
            buf.append("<td align='center'>");
            buf.append(rowCounter);
            buf.append("</td>");
        }
        buf.append("</tr>");
    }
    // create the value summary section
    buf.append(createSummarySection(totalNoOfRecs, rowsPerValCounts));
    buf.append("</table>");
    return buf.toString();
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 30 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class BitVectorAttributeModel method getHTMLView.

/**
 * {@inheritDoc}
 */
@Override
String getHTMLView(final int totalNoOfRecs) {
    final List<String> sortedClassVal = AttributeModel.sortCollection(m_classValues.keySet());
    if (sortedClassVal == null) {
        return "";
    }
    final int bitVectorLength = getVectorLength();
    final String classHeading = "Class/" + getAttributeName();
    final LinkedList<String> columnNames = new LinkedList<String>();
    columnNames.add(getAttributeName());
    final String missingHeading = getMissingValueHeader(columnNames);
    final StringBuilder buf = new StringBuilder();
    buf.append("<table border='1' width='100%'>");
    // first table header section
    buf.append("<tr>");
    buf.append("<th rowspan=2>");
    buf.append(StringEscapeUtils.escapeHtml(classHeading));
    buf.append("</th>");
    // first table header row
    for (int i = 0, length = getVectorLength(); i < length; i++) {
        buf.append("<th colspan=2>");
        buf.append("Pos" + i);
        buf.append("</th>");
    }
    if (missingHeading != null) {
        buf.append("<th rowspan = 2>");
        buf.append(StringEscapeUtils.escapeHtml(missingHeading));
        buf.append("</th>");
    }
    buf.append("</tr>");
    // second table header row
    buf.append("<tr>");
    // create the first header row
    for (int i = 0; i < bitVectorLength; i++) {
        buf.append("<th>");
        buf.append("0");
        buf.append("</th>");
        buf.append("<th>");
        buf.append("1");
        buf.append("</th>");
    }
    buf.append("</tr>");
    // the value rows
    int totalMissingValCounter = 0;
    final int[][] totalValCounter = new int[2][bitVectorLength];
    Arrays.fill(totalValCounter[0], 0);
    Arrays.fill(totalValCounter[1], 0);
    for (final String classVal : sortedClassVal) {
        final BitVectorClassValue classValue = m_classValues.get(classVal);
        buf.append("<tr>");
        buf.append("<th>");
        buf.append(StringEscapeUtils.escapeHtml(classVal));
        buf.append("</th>");
        for (int i = 0; i < bitVectorLength; i++) {
            // the bit vector value section
            buf.append("<td align='center'>");
            final int zeroCount = classValue.getZeroCount(i);
            totalValCounter[0][i] += zeroCount;
            buf.append(zeroCount);
            buf.append("</td>");
            buf.append("<td align='center'>");
            final int bitCount = classValue.getBitCount(i);
            totalValCounter[1][i] += bitCount;
            buf.append(bitCount);
            buf.append("</td>");
            // no of missing values for this class value
            if (missingHeading != null) {
                final MutableInteger missingRowCounter = classValue.getMissingValueRecs();
                totalMissingValCounter += missingRowCounter.intValue();
                buf.append("<td align='center'>");
                buf.append(missingRowCounter);
                buf.append("</td>");
            }
        }
        buf.append("</tr>");
    }
    // the rate row
    final NumberFormat nf = NumberFormat.getPercentInstance();
    buf.append("<tr>");
    buf.append("<th>");
    buf.append("Rate:");
    buf.append("</th>");
    for (int i = 0; i < bitVectorLength; i++) {
        // the bit vector value section
        buf.append("<td align='center'>");
        buf.append(nf.format(totalValCounter[0][i] / (double) totalNoOfRecs));
        buf.append("</td>");
        buf.append("<td align='center'>");
        buf.append(nf.format(totalValCounter[1][i] / (double) totalNoOfRecs));
        buf.append("</td>");
    }
    // no of missing values for this class value
    if (missingHeading != null) {
        buf.append("<td align='center'>");
        buf.append(nf.format(totalMissingValCounter / (double) totalNoOfRecs));
        buf.append("</td>");
    }
    buf.append("</tr>");
    buf.append("</table>");
    return buf.toString();
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) LinkedList(java.util.LinkedList) NumberFormat(java.text.NumberFormat)

Aggregations

MutableInteger (org.knime.core.util.MutableInteger)32 DataCell (org.knime.core.data.DataCell)12 HashMap (java.util.HashMap)11 DataRow (org.knime.core.data.DataRow)8 RowKey (org.knime.core.data.RowKey)7 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 HashSet (java.util.HashSet)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 DefaultRow (org.knime.core.data.def.DefaultRow)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 StringCell (org.knime.core.data.def.StringCell)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2