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;
}
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());
}
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();
}
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);
}
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();
}
}
}
Aggregations