Search in sources :

Example 71 with DataTableSpec

use of org.knime.core.data.DataTableSpec in project knime-core by knime.

the class Statistics2Table method createNominalValueTable.

/**
 * Create nominal value table containing all possible values together with
 * their occurrences.
 * @param nominal value output table
 * @return data table with nominal values for each column
 */
public DataTable createNominalValueTable(final List<String> nominal) {
    DataTableSpec outSpec = createOutSpecNominal(m_spec, nominal);
    Iterator[] it = new Iterator[outSpec.getNumColumns() / 2];
    int idx = 0;
    for (int i = 0; i < m_nominalValues.length; i++) {
        if (m_nominalValues[i] != null) {
            it[idx++] = m_nominalValues[i].entrySet().iterator();
        }
    }
    DataContainer cont = new DataContainer(outSpec);
    int rowIndex = 0;
    do {
        boolean addEnd = true;
        DataCell[] cells = new DataCell[2 * it.length];
        for (int i = 0; i < it.length; i++) {
            if (it[i] != null && it[i].hasNext()) {
                Map.Entry<DataCell, Integer> e = (Map.Entry<DataCell, Integer>) it[i].next();
                cells[2 * i] = e.getKey();
                cells[2 * i + 1] = new IntCell(e.getValue());
                addEnd = false;
            } else {
                cells[2 * i] = DataType.getMissingCell();
                cells[2 * i + 1] = DataType.getMissingCell();
            }
        }
        if (addEnd) {
            break;
        }
        cont.addRowToTable(new DefaultRow(RowKey.createRowKey(rowIndex++), cells));
    } while (true);
    cont.close();
    return cont.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) IntCell(org.knime.core.data.def.IntCell) MutableInteger(org.knime.core.util.MutableInteger) DataContainer(org.knime.core.data.container.DataContainer) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 72 with DataTableSpec

use of org.knime.core.data.DataTableSpec in project knime-core by knime.

the class Statistics2Table method load.

/**
 * Load a new statistic table by the given settings object.
 * @param sett to load this table from
 * @return a new statistic table
 * @throws InvalidSettingsException if the settings are corrupt
 */
public static Statistics2Table load(final NodeSettingsRO sett) throws InvalidSettingsException {
    DataTableSpec spec = DataTableSpec.load(sett.getConfig("spec"));
    Map<DataCell, Integer>[] nominalValues = new Map[spec.getNumColumns()];
    for (int c = 0; c < nominalValues.length; c++) {
        String name = spec.getColumnSpec(c).getName();
        if (!sett.containsKey(name)) {
            nominalValues[c] = null;
        } else {
            nominalValues[c] = new LinkedHashMap<DataCell, Integer>();
            NodeSettingsRO subSett = sett.getNodeSettings(name);
            for (String key : subSett.keySet()) {
                NodeSettingsRO nomSett = subSett.getNodeSettings(key);
                nominalValues[c].put(nomSett.getDataCell("key"), nomSett.getInt("value"));
            }
        }
    }
    double[] min = sett.getDoubleArray("minimum");
    double[] max = sett.getDoubleArray("maximum");
    double[] mean = sett.getDoubleArray("mean");
    double[] var = sett.getDoubleArray("variance");
    double[] median = sett.getDoubleArray("median");
    double[] missings = sett.getDoubleArray("missings");
    double[] sums = sett.getDoubleArray("sums");
    // added with 2.7, fallback -1
    int rowCount = sett.getInt("row_count", -1);
    return new Statistics2Table(spec, min, max, mean, median, var, sums, missings, nominalValues, rowCount);
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 73 with DataTableSpec

use of org.knime.core.data.DataTableSpec in project knime-core by knime.

the class ReadPNGFromURLNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    ColumnRearranger rearranger = createColumnRearranger(inSpecs[0], new AtomicLong());
    DataTableSpec out = rearranger.createSpec();
    return new DataTableSpec[] { out };
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger)

Example 74 with DataTableSpec

use of org.knime.core.data.DataTableSpec in project knime-core by knime.

the class ReadPNGFromURLNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    DataTableSpec spec = inData[0].getDataTableSpec();
    AtomicLong failCount = new AtomicLong();
    ColumnRearranger rearranger = createColumnRearranger(spec, failCount);
    BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    long rowCount = out.size();
    long fail = failCount.get();
    if (rowCount > 0 && rowCount == fail) {
        throw new Exception("None of the URLs could be read " + "as PNG (see log for details)");
    } else if (fail > 0) {
        setWarningMessage("Failed to read " + fail + "/" + rowCount + " files");
    }
    return new BufferedDataTable[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) AtomicLong(java.util.concurrent.atomic.AtomicLong) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 75 with DataTableSpec

use of org.knime.core.data.DataTableSpec in project knime-core by knime.

the class AppendVariableToTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable t = (BufferedDataTable) inData[1];
    DataTableSpec ts = t.getSpec();
    ColumnRearranger ar = createColumnRearranger(ts);
    BufferedDataTable out = exec.createColumnRearrangeTable(t, ar, exec);
    return new BufferedDataTable[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Aggregations

DataTableSpec (org.knime.core.data.DataTableSpec)938 DataColumnSpec (org.knime.core.data.DataColumnSpec)340 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)306 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)228 BufferedDataTable (org.knime.core.node.BufferedDataTable)226 DataCell (org.knime.core.data.DataCell)186 DataRow (org.knime.core.data.DataRow)170 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)136 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)129 DataType (org.knime.core.data.DataType)109 ArrayList (java.util.ArrayList)106 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)98 DoubleValue (org.knime.core.data.DoubleValue)94 DefaultRow (org.knime.core.data.def.DefaultRow)92 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)90 ExecutionContext (org.knime.core.node.ExecutionContext)68 PortObject (org.knime.core.node.port.PortObject)66 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)62 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)61 RowKey (org.knime.core.data.RowKey)59