Search in sources :

Example 51 with StringCell

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

the class AddEmptyRowsNodeModel method createNewRowsTable.

private BufferedDataTable createNewRowsTable(final DataTableSpec inSpec, final long rowCount, final ExecutionContext subExec) throws CanceledExecutionException {
    DataCell[] cells = new DataCell[inSpec.getNumColumns()];
    for (int c = 0; c < cells.length; c++) {
        DataType type = inSpec.getColumnSpec(c).getType();
        if (type.isASuperTypeOf(DoubleCell.TYPE)) {
            if (m_config.isUseMissingDouble()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new DoubleCell(m_config.getFillValueDouble());
            }
        } else if (type.isASuperTypeOf(IntCell.TYPE)) {
            if (m_config.isUseMissingInt()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new IntCell(m_config.getFillValueInt());
            }
        } else if (type.isASuperTypeOf(StringCell.TYPE)) {
            if (m_config.isUseMissingString()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new StringCell(m_config.getFillValueString());
            }
        } else {
            cells[c] = DataType.getMissingCell();
        }
    }
    BufferedDataContainer cont = subExec.createDataContainer(inSpec);
    for (long i = 0; i < rowCount; i++) {
        RowKey key = new RowKey(m_config.getNewRowKeyPrefix() + i);
        subExec.setProgress(i / (double) rowCount, "Creating row \"" + key + "\", " + i + "/" + rowCount);
        subExec.checkCanceled();
        cont.addRowToTable(new DefaultRow(key, cells));
    }
    cont.close();
    return cont.getTable();
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 52 with StringCell

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

the class ClassAttributeModel method createDataRows.

/**
 * {@inheritDoc}
 */
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
    final List<String> sortedClassVal = AttributeModel.sortCollection(m_recsCounterByClassVal.keySet());
    if (sortedClassVal == null) {
        return;
    }
    final StringCell attributeNameCell = new StringCell(getAttributeName());
    for (final String classVal : sortedClassVal) {
        final StringCell classCell = new StringCell(classVal);
        final List<DataCell> cells = new LinkedList<>();
        cells.add(attributeNameCell);
        cells.add(DataType.getMissingCell());
        cells.add(classCell);
        cells.add(new IntCell(getNoOfRecs4ClassValue(classVal)));
        if (!ignoreMissing) {
            cells.add(new IntCell(getNoOfMissingVals()));
        }
        cells.add(DataType.getMissingCell());
        cells.add(DataType.getMissingCell());
        dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
    }
}
Also used : StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) LinkedList(java.util.LinkedList) IntCell(org.knime.core.data.def.IntCell)

Example 53 with StringCell

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

the class NominalAttributeModel method createDataRows.

/**
 * {@inheritDoc}
 */
@Override
void createDataRows(final ExecutionMonitor exec, final BufferedDataContainer dc, final boolean ignoreMissing, final AtomicInteger rowId) throws CanceledExecutionException {
    final List<String> sortedClassVal = AttributeModel.sortCollection(m_classValues.keySet());
    if (sortedClassVal == null) {
        return;
    }
    final List<String> sortedAttrValues = AttributeModel.sortCollection(m_attributeVals);
    final StringCell attributeNameCell = new StringCell(getAttributeName());
    for (final String attrVal : sortedAttrValues) {
        final StringCell attributeValueCell = new StringCell(attrVal);
        for (final String classVal : sortedClassVal) {
            final StringCell classCell = new StringCell(classVal);
            final NominalClassValue classValue = m_classValues.get(classVal);
            final List<DataCell> cells = new LinkedList<>();
            cells.add(attributeNameCell);
            cells.add(attributeValueCell);
            cells.add(classCell);
            cells.add(new IntCell(classValue.getNoOfRows4AttributeValue(attrVal)));
            if (!ignoreMissing) {
                cells.add(new IntCell(classValue.getNoOfMissingValueRecs()));
            }
            cells.add(DataType.getMissingCell());
            cells.add(DataType.getMissingCell());
            dc.addRowToTable(new DefaultRow(RowKey.createRowKey(rowId.getAndIncrement()), cells.toArray(new DataCell[0])));
        }
    }
}
Also used : StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) LinkedList(java.util.LinkedList) IntCell(org.knime.core.data.def.IntCell)

Example 54 with StringCell

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

the class PolyRegLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    final int colCount = inSpec.getNumColumns();
    String[] selectedCols = computeSelectedColumns(inSpec);
    Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
    m_colSelected = new boolean[colCount];
    for (int i = 0; i < colCount; i++) {
        m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
    }
    final int rowCount = inTable.getRowCount();
    String[] temp = new String[m_columnNames.length + 1];
    System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
    temp[temp.length - 1] = m_settings.getTargetColumn();
    FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
    final DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    PortObjectSpec[] outputSpec = configure((inPMMLPort == null) ? new PortObjectSpec[] { inData[0].getSpec(), null } : new PortObjectSpec[] { inData[0].getSpec(), inPMMLPort.getSpec() });
    Learner learner = new Learner((PMMLPortObjectSpec) outputSpec[0], 0d, m_settings.getMissingValueHandling() == MissingValueHandling.fail, m_settings.getDegree());
    try {
        PolyRegContent polyRegContent = learner.perform(inTable, exec);
        m_betas = fillBeta(polyRegContent);
        m_meanValues = polyRegContent.getMeans();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        PortObject[] bdt = new PortObject[] { createPMMLModel(inPMMLPort, inSpec), exec.createColumnRearrangeTable(inTable, crea, exec.createSilentSubExecutionContext(.2)), polyRegContent.createTablePortObject(exec.createSubExecutionContext(0.2)) };
        m_squaredError /= rowCount;
        if (polyRegContent.getWarningMessage() != null) {
            setWarningMessage(polyRegContent.getWarningMessage());
        }
        double[] stdErrors = PolyRegViewData.mapToArray(polyRegContent.getStandardErrors(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptStdErr());
        double[] tValues = PolyRegViewData.mapToArray(polyRegContent.getTValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptTValue());
        double[] pValues = PolyRegViewData.mapToArray(polyRegContent.getPValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptPValue());
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, stdErrors, tValues, pValues, m_squaredError, polyRegContent.getAdjustedRSquared(), m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        return bdt;
    } catch (ModelSpecificationException e) {
        final String origWarning = getWarningMessage();
        final String warning = (origWarning != null && !origWarning.isEmpty()) ? (origWarning + "\n") : "" + e.getMessage();
        setWarningMessage(warning);
        final ExecutionContext subExec = exec.createSubExecutionContext(.1);
        final BufferedDataContainer empty = subExec.createDataContainer(STATS_SPEC);
        int rowIdx = 1;
        for (final String column : m_columnNames) {
            for (int d = 1; d <= m_settings.getDegree(); ++d) {
                empty.addRowToTable(new DefaultRow("Row" + rowIdx++, new StringCell(column), new IntCell(d), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
            }
        }
        empty.addRowToTable(new DefaultRow("Row" + rowIdx, new StringCell("Intercept"), new IntCell(0), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
        double[] nans = new double[m_columnNames.length * m_settings.getDegree() + 1];
        Arrays.fill(nans, Double.NaN);
        m_betas = new double[nans.length];
        // Mean only for the linear tags
        m_meanValues = new double[nans.length / m_settings.getDegree()];
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, nans, nans, nans, m_squaredError, Double.NaN, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        empty.close();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        BufferedDataTable rearrangerTable = exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6));
        PMMLPortObject model = createPMMLModel(inPMMLPort, inTable.getDataTableSpec());
        PortObject[] bdt = new PortObject[] { model, rearrangerTable, empty.getTable() };
        return bdt;
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) DoubleCell(org.knime.core.data.def.DoubleCell) FilterColumnTable(org.knime.base.data.filter.column.FilterColumnTable) DataArray(org.knime.base.node.util.DataArray) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ModelSpecificationException(org.apache.commons.math3.stat.regression.ModelSpecificationException) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) HashSet(java.util.HashSet) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StringCell(org.knime.core.data.def.StringCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 55 with StringCell

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

the class ClusterAssignerNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final PMMLPortObject port, final DataTableSpec inSpec) throws InvalidSettingsException {
    List<Node> models = port.getPMMLValue().getModels(PMMLModelType.ClusteringModel);
    if (models.isEmpty()) {
        String msg = "No Clustering Model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLClusterTranslator trans = new PMMLClusterTranslator();
    port.initializeModelTranslator(trans);
    ComparisonMeasure measure = trans.getComparisonMeasure();
    List<Prototype> prototypes = new ArrayList<Prototype>();
    String[] labels = trans.getLabels();
    double[][] protos = trans.getPrototypes();
    for (int i = 0; i < protos.length; i++) {
        double[] prototype = protos[i];
        prototypes.add(new Prototype(prototype, new StringCell(labels[i])));
    }
    ColumnRearranger colre = new ColumnRearranger(inSpec);
    colre.append(new ClusterAssignFactory(measure, prototypes, createNewOutSpec(inSpec), findLearnedColumnIndices(inSpec, trans.getUsedColumns())));
    return colre;
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLClusterTranslator(org.knime.base.node.mine.cluster.PMMLClusterTranslator) StringCell(org.knime.core.data.def.StringCell) ComparisonMeasure(org.knime.base.node.mine.cluster.PMMLClusterTranslator.ComparisonMeasure)

Aggregations

StringCell (org.knime.core.data.def.StringCell)176 DataCell (org.knime.core.data.DataCell)130 DoubleCell (org.knime.core.data.def.DoubleCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)65 IntCell (org.knime.core.data.def.IntCell)55 DataRow (org.knime.core.data.DataRow)52 DataTableSpec (org.knime.core.data.DataTableSpec)49 ArrayList (java.util.ArrayList)41 DataColumnSpec (org.knime.core.data.DataColumnSpec)37 RowKey (org.knime.core.data.RowKey)36 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)26 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)26 DataType (org.knime.core.data.DataType)22 LinkedHashSet (java.util.LinkedHashSet)21 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)16 LinkedHashMap (java.util.LinkedHashMap)15 Test (org.junit.Test)15 HashMap (java.util.HashMap)11