Search in sources :

Example 41 with BufferedDataContainer

use of org.knime.core.node.BufferedDataContainer in project knime-core by knime.

the class StatisticCalculatorTest method createRandomTableWithMissingValues.

private static BufferedDataTable createRandomTableWithMissingValues(final int cols, final int rows) {
    long currentTimeMillis = System.currentTimeMillis();
    System.out.println("Using seed: " + currentTimeMillis);
    Random random = new Random(currentTimeMillis);
    DataTableSpecCreator creator = new DataTableSpecCreator();
    for (int i = 0; i < cols; i++) {
        creator.addColumns(new DataColumnSpecCreator("" + i, DoubleCell.TYPE).createSpec());
    }
    final BufferedDataContainer container = EXEC_CONTEXT.createDataContainer(creator.createSpec());
    for (int i = 0; i < rows; i++) {
        DataCell[] rowVals = new DataCell[cols];
        for (int j = 0; j < cols; j++) {
            rowVals[j] = random.nextDouble() > 0.66 ? new DoubleCell(random.nextDouble()) : DataType.getMissingCell();
        }
        container.addRowToTable(new DefaultRow(Integer.toString(i), rowVals));
        if (i % 1000 == 0) {
            System.out.println("Added row: " + i);
        }
    }
    container.close();
    return container.getTable();
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Random(java.util.Random) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 42 with BufferedDataContainer

use of org.knime.core.node.BufferedDataContainer in project knime-core by knime.

the class LogisticRegressionContent method createCoeffStatisticsTablePortObject.

/**
 * Creates a BufferedDataTable with the
 * @param exec The execution context
 * @return a port object
 */
public BufferedDataTable createCoeffStatisticsTablePortObject(final ExecutionContext exec) {
    DataTableSpec tableOutSpec = LogRegCoordinator.createCoeffStatisticsTableSpec();
    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;
        Map<String, Double> zScores;
        Map<String, Double> pValues;
        if (m_covMat == null) {
            HashMap<String, Double> emptyMap = new HashMap<>();
            stdErrs = emptyMap;
            zScores = emptyMap;
            pValues = emptyMap;
        } else {
            stdErrs = this.getStandardErrors(logit);
            zScores = this.getZScores(logit);
            pValues = this.getPValues(logit);
        }
        for (String parameter : parameters) {
            List<DataCell> cells = new ArrayList<>();
            cells.add(new StringCell(logit.toString()));
            cells.add(new StringCell(parameter));
            cells.add(new DoubleCell(coefficients.get(parameter)));
            if (m_covMat != null) {
                cells.add(new DoubleCell(stdErrs.get(parameter)));
                cells.add(new DoubleCell(zScores.get(parameter)));
                cells.add(new DoubleCell(pValues.get(parameter)));
            } else {
                cells.add(NOT_INVERTIBLE_MISSING);
                cells.add(NOT_INVERTIBLE_MISSING);
                cells.add(NOT_INVERTIBLE_MISSING);
            }
            c++;
            dc.addRowToTable(new DefaultRow("Row" + c, cells));
        }
        List<DataCell> cells = new ArrayList<>();
        cells.add(new StringCell(logit.toString()));
        cells.add(new StringCell("Constant"));
        cells.add(new DoubleCell(this.getIntercept(logit)));
        if (m_covMat != null) {
            cells.add(new DoubleCell(this.getInterceptStdErr(logit)));
            cells.add(new DoubleCell(this.getInterceptZScore(logit)));
            cells.add(new DoubleCell(this.getInterceptPValue(logit)));
        } else {
            cells.add(NOT_INVERTIBLE_MISSING);
            cells.add(NOT_INVERTIBLE_MISSING);
            cells.add(NOT_INVERTIBLE_MISSING);
        }
        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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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 43 with BufferedDataContainer

use of org.knime.core.node.BufferedDataContainer in project knime-core by knime.

the class AbstractColumnTableSorterTest method setUp.

/**
 * @throws java.lang.Exception
 */
@SuppressWarnings("rawtypes")
@Before
public void setUp() throws Exception {
    @SuppressWarnings("unchecked") NodeFactory<NodeModel> dummyFactory = (NodeFactory) new VirtualParallelizedChunkPortObjectInNodeFactory(new PortType[0]);
    m_exec = new ExecutionContext(new DefaultNodeProgressMonitor(), new Node(dummyFactory), SingleNodeContainer.MemoryPolicy.CacheOnDisc, new HashMap<Integer, ContainerTable>());
    DataColumnSpec[] colSpecs = new DataColumnSpec[] { new DataColumnSpecCreator(FEATURE1, DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator(FEATURE2, DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator(STRING_FEATURE, StringCell.TYPE).createSpec(), new DataColumnSpecCreator(CLASS, StringCell.TYPE).createSpec() };
    DataTableSpec spec = new DataTableSpec(colSpecs);
    final BufferedDataContainer container = m_exec.createDataContainer(spec);
    int i = 0;
    container.addRowToTable(creatRow(i++, 1, 8, "A", "AClass8"));
    container.addRowToTable(creatRow(i++, 2, 2, "Z", "ZClass2"));
    container.addRowToTable(creatRow(i++, 3, 5, "B", "BClass5"));
    container.addRowToTable(creatRow(i++, 4, 0, "E", "EClass0"));
    container.addRowToTable(creatRow(i++, 5, 1, "F", "FClass1"));
    container.addRowToTable(creatRow(i++, 6, 7, "G", "GClass7"));
    container.addRowToTable(creatRow(i++, 7, 9, "H", "HClass9"));
    container.addRowToTable(creatRow(i++, 8, 8, null, "Class8"));
    container.close();
    testTable = container.getTable();
    final BufferedDataContainer emptyContainer = m_exec.createDataContainer(spec);
    emptyContainer.close();
    emptyTestTable = emptyContainer.getTable();
    MemoryAlertSystemTest.forceGC();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) HashMap(java.util.HashMap) Node(org.knime.core.node.Node) NodeModel(org.knime.core.node.NodeModel) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) NodeFactory(org.knime.core.node.NodeFactory) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) DefaultNodeProgressMonitor(org.knime.core.node.DefaultNodeProgressMonitor) PortType(org.knime.core.node.port.PortType) Before(org.junit.Before)

Example 44 with BufferedDataContainer

use of org.knime.core.node.BufferedDataContainer in project knime-core by knime.

the class EndifNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] rawInData, final ExecutionContext exec) throws Exception {
    if (m_enableHiliting) {
        // create empty hilite translation map (so we correctly
        // handle the internals even if we return with a IBPO:
        Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
        m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
    }
    if (rawInData[0] instanceof InactiveBranchPortObject) {
        return new PortObject[] { rawInData[1] };
    }
    if (rawInData[1] instanceof InactiveBranchPortObject) {
        return new PortObject[] { rawInData[0] };
    }
    // no inactive branch - check compatibility of specs - which in
    // this case must be BFT Specs!
    DataTableSpec spec0 = (DataTableSpec) (rawInData[0].getSpec());
    DataTableSpec spec1 = (DataTableSpec) (rawInData[1].getSpec());
    if (spec0.equalStructure(spec1)) {
        // concatenate tables and return result
        BufferedDataTable[] inData = new BufferedDataTable[2];
        inData[0] = (BufferedDataTable) rawInData[0];
        inData[1] = (BufferedDataTable) rawInData[1];
        int totalRowCount = 0;
        for (BufferedDataTable t : inData) {
            totalRowCount += t.getRowCount();
        }
        AppendedRowsTable out = new AppendedRowsTable((m_isAppendSuffix ? m_suffix : null), inData);
        // note, this iterator throws runtime exceptions when canceled.
        AppendedRowsIterator it = out.iterator(exec, totalRowCount);
        BufferedDataContainer c = exec.createDataContainer(out.getDataTableSpec());
        try {
            while (it.hasNext()) {
                // may throw exception, also sets progress
                c.addRowToTable(it.next());
            }
        } catch (AppendedRowsIterator.RuntimeCanceledExecutionException rcee) {
            throw rcee.getCause();
        } finally {
            c.close();
        }
        if (it.getNrRowsSkipped() > 0) {
            setWarningMessage("Filtered out " + it.getNrRowsSkipped() + " duplicate row id(s).");
        }
        if (m_enableHiliting) {
            // create hilite translation map
            Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
            // map of all RowKeys and duplicate RowKeys in the resulting table
            Map<RowKey, RowKey> dupMap = it.getDuplicateNameMap();
            for (Map.Entry<RowKey, RowKey> e : dupMap.entrySet()) {
                // if a duplicate key
                if (!e.getKey().equals(e.getValue())) {
                    Set<RowKey> set = Collections.singleton(e.getValue());
                    // put duplicate key and original key into map
                    map.put(e.getKey(), set);
                } else {
                    // skip duplicate keys
                    if (!dupMap.containsKey(new RowKey(e.getKey().getString() + m_suffix))) {
                        Set<RowKey> set = Collections.singleton(e.getValue());
                        map.put(e.getKey(), set);
                    }
                }
            }
            m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
        }
        return new BufferedDataTable[] { c.getTable() };
    }
    throw new Exception("Both input ports have data but the tables " + "have incompatible specs");
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Set(java.util.Set) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) AppendedRowsIterator(org.knime.core.data.append.AppendedRowsIterator) AppendedRowsTable(org.knime.core.data.append.AppendedRowsTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortObject(org.knime.core.node.port.PortObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with BufferedDataContainer

use of org.knime.core.node.BufferedDataContainer in project knime-core by knime.

the class AdapterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    if (getNrInPorts() == 0 && getNrOutPorts() == 1) {
        // assume simple source node with one table output
        BufferedDataContainer cnt = exec.createDataContainer(createDefaultOutputSpec());
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new DataCell[] { new StringCell("Cell-1.1"), new IntCell(12), new DoubleCell(1.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(1), new DataCell[] { new StringCell("Cell-2.1"), new IntCell(22), new DoubleCell(2.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(2), new DataCell[] { new StringCell("Cell-3.1"), new IntCell(32), new DoubleCell(3.3) }));
        cnt.close();
        return new BufferedDataTable[] { cnt.getTable() };
    }
    return inObjects;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Aggregations

BufferedDataContainer (org.knime.core.node.BufferedDataContainer)157 BufferedDataTable (org.knime.core.node.BufferedDataTable)96 DefaultRow (org.knime.core.data.def.DefaultRow)93 DataCell (org.knime.core.data.DataCell)88 DataTableSpec (org.knime.core.data.DataTableSpec)88 DataRow (org.knime.core.data.DataRow)80 RowKey (org.knime.core.data.RowKey)38 DoubleCell (org.knime.core.data.def.DoubleCell)37 StringCell (org.knime.core.data.def.StringCell)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)24 ArrayList (java.util.ArrayList)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)21 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 IOException (java.io.IOException)15 ExecutionContext (org.knime.core.node.ExecutionContext)15 LinkedHashMap (java.util.LinkedHashMap)14 HashSet (java.util.HashSet)13 IntCell (org.knime.core.data.def.IntCell)13