Search in sources :

Example 16 with DataValue

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

the class DataCellToJavaConverterRegistry method getConverterFactories.

/**
 * Get all {@link DataCellToJavaConverterFactory} which create {@link DataCellToJavaConverter}s that convert
 * <code>sourceType</code> into <code>destType</code>. If you do not require more than one converter factory, you
 * should consider using {@link #getPreferredConverterFactory(DataType, Class)} instead.
 *
 * @param sourceType Type the created {@link DataCellToJavaConverter}s convert from
 * @param destType Type the created {@link DataCellToJavaConverter}s convert to
 * @return collection of {@link DataCellToJavaConverterFactory converter factories} which create converters which
 *         convert from <code>sourceType</code> into <code>destType</code>.
 */
public <D> Collection<DataCellToJavaConverterFactory<? extends DataValue, D>> getConverterFactories(final DataType sourceType, final Class<D> destType) {
    if (sourceType.equals(DataType.getMissingCell().getType())) {
        return Arrays.asList(MissingToNullConverterFactory.getInstance());
    }
    final Collection<DataCellToJavaConverterFactory<? extends DataValue, D>> allFactories = new ArrayList<>();
    for (final Class<? extends DataValue> curClass : sourceType.getValueClasses()) {
        if (DataValue.class.equals(curClass)) {
            // priority (e.g. DataValue.toString() converter)
            continue;
        }
        final ArrayList<DataCellToJavaConverterFactory<?, ?>> factories = m_converterFactories.get(new ConversionKey(curClass, destType));
        if (factories != null) {
            allFactories.addAll((Collection<? extends DataCellToJavaConverterFactory<DataCell, D>>) factories);
        }
    }
    final ArrayList<DataCellToJavaConverterFactory<?, ?>> factories = m_converterFactories.get(new ConversionKey(DataValue.class, destType));
    if (factories != null) {
        allFactories.addAll((Collection<? extends DataCellToJavaConverterFactory<DataCell, D>>) factories);
    }
    if (sourceType.isCollectionType() && destType.isArray()) {
        allFactories.addAll(getCollectionConverterFactories(sourceType, destType));
    }
    return allFactories;
}
Also used : CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) DataValue(org.knime.core.data.DataValue) ConversionKey(org.knime.core.data.convert.ConversionKey) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

Example 17 with DataValue

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

the class HistogramColumn method appendNominal.

/**
 * Append the nominal rows to the histogram table.
 *
 * @param numericWithHistograms The output from the
 *            {@link Statistics3Table#createStatisticsInColumnsTable(ExecutionContext)}.
 * @param statTable The {@link Statistics3Table}.
 * @param hlHandler The {@link HiLiteHandler} to HiLite parts.
 * @param exec An {@link ExecutionContext}.
 * @param maxBinCount The maximum number of bins till we draw labels.
 * @return The {@link BufferedDataTable} with new rows for selected nominal columns.
 * @throws CanceledExecutionException Execution cancelled.
 */
@Deprecated
public BufferedDataTable appendNominal(final BufferedDataTable numericWithHistograms, final Statistics3Table statTable, final HiLiteHandler hlHandler, final ExecutionContext exec, final int maxBinCount) throws CanceledExecutionException {
    DataTableSpec tableSpec = numericWithHistograms.getDataTableSpec();
    BufferedDataContainer nominals = exec.createDataContainer(tableSpec);
    for (int i = 0; i < statTable.getNominalValues().size(); ++i) {
        Map<DataCell, Integer> nominalValues = statTable.getNominalValues(i);
        if (nominalValues == null) {
            continue;
        }
        final String colName = statTable.getColumnNames()[i];
        DataCell[] row = new DataCell[tableSpec.getNumColumns()];
        for (int u = row.length; u-- > 0; ) {
            row[u] = DataType.getMissingCell();
        }
        row[0] = new StringCell(colName);
        HistogramNominalModel model = new HistogramNominalModel(new LinkedHashMap<DataValue, Integer>(nominalValues), i, colName, statTable.getRowCount());
        row[row.length - 1] = createImageCell(model, false);
        nominals.addRowToTable(new DefaultRow(colName + " (as nominal)", row));
    }
    nominals.close();
    return exec.createConcatenateTable(exec, numericWithHistograms, nominals.getTable());
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataValue(org.knime.core.data.DataValue) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 18 with DataValue

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

the class HistogramColumn method nominalTable.

/**
 * Creates the nominal table (histogram with number of missing values).
 *
 * @param statTable The statistics table.
 * @param hlHandler The {@link HiLiteHandler}.
 * @param exec An {@link ExecutionContext}.
 * @param maxBinCount The maximum number of bins when we put labels to the histogram.
 * @return The table containing the histograms.
 * @see #createNominalHistogramTableSpec()
 */
public BufferedDataTable nominalTable(final Statistics3Table statTable, final HiLiteHandler hlHandler, final ExecutionContext exec, final int maxBinCount) {
    DataTableSpec tableSpec = createNominalHistogramTableSpec();
    BufferedDataContainer nominals = exec.createDataContainer(tableSpec);
    for (int i = 0; i < statTable.getNominalValues().size(); ++i) {
        Map<DataCell, Integer> nominalValues = statTable.getNominalValues(i);
        if (nominalValues == null) {
            continue;
        }
        final String colName = statTable.getColumnNames()[i];
        DataCell[] row = new DataCell[tableSpec.getNumColumns()];
        for (int u = row.length; u-- > 0; ) {
            row[u] = DataType.getMissingCell();
        }
        row[0] = new StringCell(colName);
        row[1] = new IntCell(statTable.getNumberMissingValues()[i]);
        HistogramNominalModel model = new HistogramNominalModel(new LinkedHashMap<DataValue, Integer>(nominalValues), i, colName, statTable.getRowCount());
        row[row.length - 1] = createImageCell(model, maxBinCount >= model.getBins().size());
        nominals.addRowToTable(new DefaultRow(colName, row));
    }
    nominals.close();
    return nominals.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataValue(org.knime.core.data.DataValue) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 19 with DataValue

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

the class HistogramColumn method saveNominalHistogramData.

/**
 * @param histograms The nominal histogram models associated to the column indices.
 * @param histogramsFile The output file.
 * @throws IOException File write problem.
 */
public static void saveNominalHistogramData(final Map<Integer, HistogramModel<?>> histograms, final File histogramsFile) throws IOException {
    Config histogramData = new NodeSettings(HISTOGRAMS);
    final FileOutputStream os = new FileOutputStream(histogramsFile);
    final GZIPOutputStream dataOS = new GZIPOutputStream(os);
    List<Integer> colIndices = new ArrayList<Integer>(histograms.keySet());
    Collections.sort(colIndices);
    int[] nominalColumnIndices = new int[colIndices.size()];
    for (int i = colIndices.size(); i-- > 0; ) {
        nominalColumnIndices[i] = colIndices.get(i).intValue();
    }
    histogramData.addIntArray(NOMINAL_COLUMNS, nominalColumnIndices);
    for (Integer colIdx : colIndices) {
        Object object = histograms.get(colIdx);
        if (object instanceof HistogramNominalModel) {
            HistogramNominalModel hd = (HistogramNominalModel) object;
            assert hd.getColIndex() == colIdx.intValue() : "colIdx: " + colIdx + ", but: " + hd.getColIndex();
            Config h = histogramData.addConfig(HISTOGRAM + colIdx);
            h.addInt(COL_INDEX, hd.getColIndex());
            h.addString(COL_NAME, hd.getColName());
            h.addInt(MAX_COUNT, hd.getMaxCount());
            h.addInt(ROW_COUNT, hd.getRowCount());
            int[] counts = new int[hd.getBins().size()];
            String[] values = new String[hd.getBins().size()];
            for (int c = 0; c < hd.getBins().size(); c++) {
                HistogramModel.Bin<DataValue> bin = hd.getBins().get(c);
                if (bin.getDef() instanceof StringCell) {
                    values[c] = ((StringCell) bin.getDef()).getStringValue();
                } else {
                    values[c] = "?";
                }
                counts[c] = bin.getCount();
            }
            h.addStringArray(BIN_VALUES, values);
            h.addIntArray(BIN_COUNTS, counts);
        } else {
            throw new IllegalStateException("Illegal argument: " + colIdx + ": " + object.getClass() + "\n   " + object);
        }
    }
    histogramData.saveToXML(dataOS);
}
Also used : DataValue(org.knime.core.data.DataValue) Config(org.knime.core.node.config.Config) ArrayList(java.util.ArrayList) NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) StringCell(org.knime.core.data.def.StringCell) FileOutputStream(java.io.FileOutputStream)

Example 20 with DataValue

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

the class ExtendedStatisticsNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec) throws IOException {
    NodeSettingsRO sett = NodeSettings.loadFromXML(new FileInputStream(new File(internDir, "statistic.xml.gz")));
    try {
        m_statTable = Statistics3Table.load(sett);
    } catch (InvalidSettingsException ise) {
        throw new IOException(ise);
    }
    double[] means = getStatTable().getMean();
    if (m_enableHiLite.getBooleanValue()) {
        File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
        File dataArrayGz = new File(internDir, DATA_ARRAY_GZ);
        try {
            Set<String> nominalColumnNames = new LinkedHashSet<String>();
            String[] columnNames = getStatTable().getColumnNames();
            for (int i = 0; i < columnNames.length; ++i) {
                if (getStatTable().getNominalValues(i) != null) {
                    nominalColumnNames.add(columnNames[i]);
                }
            }
            Pair<Pair<Map<Integer, ? extends HistogramModel<?>>, Map<Integer, Map<Integer, Set<RowKey>>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> ppair = HistogramColumn.loadHistograms(histogramsGz, dataArrayGz, nominalColumnNames, BIN_SELECTION_STRATEGY, means);
            m_histograms = ppair.getFirst().getFirst();
            m_buckets = ppair.getFirst().getSecond();
            m_nominalKeys = ppair.getSecond();
        // m_nominalTypes = ppair.getSecond().getSecond();
        } catch (InvalidSettingsException e) {
            getLogger().error("Failed to load settings for the HiLite, please rerun.", e);
            m_histograms = Collections.emptyMap();
            m_buckets = Collections.emptyMap();
            m_nominalKeys = Collections.emptyMap();
        // m_nominalTypes = Collections.emptyMap();
        }
    } else {
        File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
        m_nominalKeys = Collections.emptyMap();
        try {
            m_buckets = new HashMap<Integer, Map<Integer, Set<RowKey>>>();
            m_histograms = HistogramColumn.loadHistograms(histogramsGz, m_buckets, BIN_SELECTION_STRATEGY, means);
            m_buckets.clear();
        } catch (InvalidSettingsException e) {
            m_histograms = Collections.emptyMap();
            m_buckets = Collections.emptyMap();
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) RowKey(org.knime.core.data.RowKey) DataValue(org.knime.core.data.DataValue) IOException(java.io.IOException) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) FileInputStream(java.io.FileInputStream) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) Pair(org.knime.core.util.Pair)

Aggregations

DataValue (org.knime.core.data.DataValue)28 DataCell (org.knime.core.data.DataCell)9 DoubleValue (org.knime.core.data.DoubleValue)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 DataType (org.knime.core.data.DataType)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 StringCell (org.knime.core.data.def.StringCell)5 LinkedHashMap (java.util.LinkedHashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 DataRow (org.knime.core.data.DataRow)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 FileInputStream (java.io.FileInputStream)2 JMenu (javax.swing.JMenu)2