Search in sources :

Example 31 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class TreeEnsembleModelExtractorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    TreeEnsembleModelPortObject treeEnsembleModel = (TreeEnsembleModelPortObject) inObjects[0];
    DataTableSpec outSpec = createOutSpec();
    BufferedDataContainer container = exec.createDataContainer(outSpec, false, 0);
    int nrModels = treeEnsembleModel.getEnsembleModel().getNrModels();
    for (int i = 0; i < nrModels; i++) {
        PMMLPortObject pmmlObject = treeEnsembleModel.createDecisionTreePMMLPortObject(i);
        DataCell cell = PMMLCellFactory.create(pmmlObject.getPMMLValue().toString());
        RowKey key = RowKey.createRowKey(i);
        container.addRowToTable(new DefaultRow(key, cell));
        exec.checkCanceled();
        exec.setProgress(i / (double) nrModels, "Exported model " + (i + 1) + "/" + nrModels);
    }
    container.close();
    return new BufferedDataTable[] { container.getTable() };
}
Also used : TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObject) DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) RowKey(org.knime.core.data.RowKey) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 32 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class EnrichmentPlotterModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final double rowCount = inData[0].size();
    final BufferedDataContainer areaOutCont = exec.createDataContainer(AREA_OUT_SPEC);
    final BufferedDataContainer discrateOutCont = exec.createDataContainer(DISCRATE_OUT_SPEC);
    for (int i = 0; i < m_settings.getCurveCount(); i++) {
        final ExecutionMonitor sexec = exec.createSubProgress(1.0 / m_settings.getCurveCount());
        exec.setMessage("Generating curve " + (i + 1));
        final Curve c = m_settings.getCurve(i);
        final Helper[] curve = new Helper[KnowsRowCountTable.checkRowCount(inData[0].size())];
        final int sortIndex = inData[0].getDataTableSpec().findColumnIndex(c.getSortColumn());
        final int actIndex = inData[0].getDataTableSpec().findColumnIndex(c.getActivityColumn());
        int k = 0, maxK = 0;
        for (DataRow row : inData[0]) {
            DataCell c1 = row.getCell(sortIndex);
            DataCell c2 = row.getCell(actIndex);
            if (k++ % 100 == 0) {
                sexec.checkCanceled();
                sexec.setProgress(k / rowCount);
            }
            if (c1.isMissing()) {
                continue;
            } else {
                curve[maxK] = new Helper(((DoubleValue) c1).getDoubleValue(), c2);
            }
            maxK++;
        }
        Arrays.sort(curve, 0, maxK);
        if (c.isSortDescending()) {
            for (int j = 0; j < maxK / 2; j++) {
                Helper h = curve[j];
                curve[j] = curve[maxK - j - 1];
                curve[maxK - j - 1] = h;
            }
        }
        // this is for down-sampling so that the view is faster;
        // plotting >100,000 points takes quite a long time
        final int size = Math.min(MAX_RESOLUTION, maxK);
        final double downSampleRate = maxK / (double) size;
        final double[] xValues = new double[size + 1];
        final double[] yValues = new double[size + 1];
        xValues[0] = 0;
        yValues[0] = 0;
        int lastK = 0;
        double y = 0, area = 0;
        int nextHitRatePoint = 0;
        final double[] hitRateValues = new double[DISCRATE_POINTS.length];
        final HashMap<DataCell, MutableInteger> clusters = new HashMap<DataCell, MutableInteger>();
        for (k = 1; k <= maxK; k++) {
            final Helper h = curve[k - 1];
            if (m_settings.plotMode() == PlotMode.PlotSum) {
                y += ((DoubleValue) h.b).getDoubleValue();
            } else if (m_settings.plotMode() == PlotMode.PlotHits) {
                if (!h.b.isMissing() && (((DoubleValue) h.b).getDoubleValue() >= m_settings.hitThreshold())) {
                    y++;
                }
            } else if (!h.b.isMissing()) {
                MutableInteger count = clusters.get(h.b);
                if (count == null) {
                    count = new MutableInteger(0);
                    clusters.put(h.b, count);
                }
                if (count.inc() == m_settings.minClusterMembers()) {
                    y++;
                }
            }
            area += y / maxK;
            if ((int) (k / downSampleRate) >= lastK + 1) {
                lastK++;
                xValues[lastK] = k;
                yValues[lastK] = y;
            }
            if ((nextHitRatePoint < DISCRATE_POINTS.length) && (k == (int) Math.floor(maxK * DISCRATE_POINTS[nextHitRatePoint] / 100))) {
                hitRateValues[nextHitRatePoint] = y;
                nextHitRatePoint++;
            }
        }
        xValues[xValues.length - 1] = maxK;
        yValues[yValues.length - 1] = y;
        area /= y;
        m_curves.add(new EnrichmentPlot(c.getSortColumn() + " vs " + c.getActivityColumn(), xValues, yValues, area));
        areaOutCont.addRowToTable(new DefaultRow(new RowKey(c.toString()), new DoubleCell(area)));
        for (int j = 0; j < hitRateValues.length; j++) {
            hitRateValues[j] /= y;
        }
        discrateOutCont.addRowToTable(new DefaultRow(new RowKey(c.toString()), hitRateValues));
    }
    areaOutCont.close();
    discrateOutCont.close();
    return new BufferedDataTable[] { areaOutCont.getTable(), discrateOutCont.getTable() };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) HashMap(java.util.HashMap) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) MutableInteger(org.knime.core.util.MutableInteger) Curve(org.knime.base.node.viz.enrichment.EnrichmentPlotterSettings.Curve) DataRow(org.knime.core.data.DataRow) DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 33 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class LogisticRegressionContent method createTablePortObject.

/**
 * Creates a BufferedDataTable with the
 * @param exec The execution context
 * @return a port object
 */
public BufferedDataTable createTablePortObject(final ExecutionContext exec) {
    DataTableSpec tableOutSpec = new DataTableSpec("Coefficients and Statistics", new String[] { "Logit", "Variable", "Coeff.", "Std. Err.", "z-score", "P>|z|" }, new DataType[] { StringCell.TYPE, StringCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE });
    BufferedDataContainer dc = exec.createDataContainer(tableOutSpec);
    List<DataCell> logits = this.getLogits();
    List<String> parameters = this.getParameters();
    int c = 0;
    for (DataCell logit : logits) {
        Map<String, Double> coefficients = this.getCoefficients(logit);
        Map<String, Double> stdErrs = this.getStandardErrors(logit);
        Map<String, Double> zScores = this.getZScores(logit);
        Map<String, Double> pValues = this.getPValues(logit);
        for (String parameter : parameters) {
            List<DataCell> cells = new ArrayList<DataCell>();
            cells.add(new StringCell(logit.toString()));
            cells.add(new StringCell(parameter));
            cells.add(new DoubleCell(coefficients.get(parameter)));
            cells.add(new DoubleCell(stdErrs.get(parameter)));
            cells.add(new DoubleCell(zScores.get(parameter)));
            cells.add(new DoubleCell(pValues.get(parameter)));
            c++;
            dc.addRowToTable(new DefaultRow("Row" + c, cells));
        }
        List<DataCell> cells = new ArrayList<DataCell>();
        cells.add(new StringCell(logit.toString()));
        cells.add(new StringCell("Constant"));
        cells.add(new DoubleCell(this.getIntercept(logit)));
        cells.add(new DoubleCell(this.getInterceptStdErr(logit)));
        cells.add(new DoubleCell(this.getInterceptZScore(logit)));
        cells.add(new DoubleCell(this.getInterceptPValue(logit)));
        c++;
        dc.addRowToTable(new DefaultRow("Row" + c, cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 34 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class LogisticRegressionContent method createTablePortObject.

/**
 * Creates a BufferedDataTable with the
 * @param exec The execution context
 * @return a port object
 */
public BufferedDataTable createTablePortObject(final ExecutionContext exec) {
    DataTableSpec tableOutSpec = new DataTableSpec("Coefficients and Statistics", new String[] { "Logit", "Variable", "Coeff.", "Std. Err.", "z-score", "P>|z|" }, new DataType[] { StringCell.TYPE, StringCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE });
    BufferedDataContainer dc = exec.createDataContainer(tableOutSpec);
    List<DataCell> logits = this.getLogits();
    List<String> parameters = this.getParameters();
    int c = 0;
    for (DataCell logit : logits) {
        Map<String, Double> coefficients = this.getCoefficients(logit);
        Map<String, Double> stdErrs = this.getStandardErrors(logit);
        Map<String, Double> zScores = this.getZScores(logit);
        Map<String, Double> pValues = this.getPValues(logit);
        for (String parameter : parameters) {
            List<DataCell> cells = new ArrayList<DataCell>();
            cells.add(new StringCell(logit.toString()));
            cells.add(new StringCell(parameter));
            cells.add(new DoubleCell(coefficients.get(parameter)));
            cells.add(new DoubleCell(stdErrs.get(parameter)));
            cells.add(new DoubleCell(zScores.get(parameter)));
            cells.add(new DoubleCell(pValues.get(parameter)));
            c++;
            dc.addRowToTable(new DefaultRow("Row" + c, cells));
        }
        List<DataCell> cells = new ArrayList<DataCell>();
        cells.add(new StringCell(logit.toString()));
        cells.add(new StringCell("Constant"));
        cells.add(new DoubleCell(this.getIntercept(logit)));
        cells.add(new DoubleCell(this.getInterceptStdErr(logit)));
        cells.add(new DoubleCell(this.getInterceptZScore(logit)));
        cells.add(new DoubleCell(this.getInterceptPValue(logit)));
        c++;
        dc.addRowToTable(new DefaultRow("Row" + c, cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 35 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class AbstractTreeEnsembleModel method createLearnAttributeRow.

public DataRow createLearnAttributeRow(final DataRow learnRow, final DataTableSpec learnSpec) {
    final TreeType type = getType();
    final DataCell c = learnRow.getCell(0);
    final int nrAttributes = getMetaData().getNrAttributes();
    switch(type) {
        case Ordinary:
            return learnRow;
        case BitVector:
            if (c.isMissing()) {
                return null;
            }
            BitVectorValue bv = (BitVectorValue) c;
            final long length = bv.length();
            if (length != nrAttributes) {
                // TODO indicate error message
                return null;
            }
            DataCell trueCell = new StringCell("1");
            DataCell falseCell = new StringCell("0");
            DataCell[] cells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                cells[i] = bv.get(i) ? trueCell : falseCell;
            }
            return new DefaultRow(learnRow.getKey(), cells);
        case ByteVector:
            if (c.isMissing()) {
                return null;
            }
            ByteVectorValue byteVector = (ByteVectorValue) c;
            final long bvLength = byteVector.length();
            if (bvLength != nrAttributes) {
                return null;
            }
            DataCell[] bvCells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                bvCells[i] = new IntCell(byteVector.get(i));
            }
            return new DefaultRow(learnRow.getKey(), bvCells);
        case DoubleVector:
            if (c.isMissing()) {
                return null;
            }
            DoubleVectorValue doubleVector = (DoubleVectorValue) c;
            final int dvLength = doubleVector.getLength();
            if (dvLength != nrAttributes) {
                return null;
            }
            DataCell[] dvCells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                dvCells[i] = new DoubleCell(doubleVector.getValue(i));
            }
            return new DefaultRow(learnRow.getKey(), dvCells);
        default:
            throw new IllegalStateException("Type unknown (not implemented): " + type);
    }
}
Also used : DoubleVectorValue(org.knime.core.data.vector.doublevector.DoubleVectorValue) DoubleCell(org.knime.core.data.def.DoubleCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

DefaultRow (org.knime.core.data.def.DefaultRow)207 DataCell (org.knime.core.data.DataCell)165 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)94 DataTableSpec (org.knime.core.data.DataTableSpec)92 DataRow (org.knime.core.data.DataRow)88 RowKey (org.knime.core.data.RowKey)80 DoubleCell (org.knime.core.data.def.DoubleCell)66 StringCell (org.knime.core.data.def.StringCell)65 BufferedDataTable (org.knime.core.node.BufferedDataTable)56 IntCell (org.knime.core.data.def.IntCell)46 ArrayList (java.util.ArrayList)26 DataType (org.knime.core.data.DataType)26 DataColumnSpec (org.knime.core.data.DataColumnSpec)22 DataContainer (org.knime.core.data.container.DataContainer)21 HashSet (java.util.HashSet)18 LinkedHashMap (java.util.LinkedHashMap)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 LinkedHashSet (java.util.LinkedHashSet)14 DoubleValue (org.knime.core.data.DoubleValue)14 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)14