use of org.knime.core.data.DataType in project knime-core by knime.
the class AppendedRowsTable method generateDataTableSpec.
/**
* Factory method that determines the final {@link DataTableSpec} given the
* tables.
*
* @param tableSpecs the table specs as in the constructor
* @return the outcoming {qlink DataTableSpec}
* @see #AppendedRowsTable(DataTable[])
*/
public static final DataTableSpec generateDataTableSpec(final DataTableSpec... tableSpecs) {
// memorize the first column spec in the argument array for
// each column name, we use it later on to initialize the column
// spec creator.
LinkedHashMap<String, DataColumnSpec> columnSet = new LinkedHashMap<String, DataColumnSpec>();
LinkedHashMap<String, DataType> typeSet = new LinkedHashMap<String, DataType>();
LinkedHashMap<String, DataColumnDomain> domainSet = new LinkedHashMap<String, DataColumnDomain>();
// create final data table spec
for (int i = 0; i < tableSpecs.length; i++) {
DataTableSpec cur = tableSpecs[i];
for (int c = 0; c < cur.getNumColumns(); c++) {
DataColumnSpec colSpec = cur.getColumnSpec(c);
String colName = colSpec.getName();
// set the spec for this column if not yet done
if (!columnSet.containsKey(colName)) {
columnSet.put(colName, colSpec);
}
DataType colType = colSpec.getType();
DataColumnDomain colDomain = colSpec.getDomain();
// duplicates are welcome - but only if they match the type
if (typeSet.containsKey(colName)) {
DataType oldType = typeSet.get(colName);
DataColumnDomain oldDomain = domainSet.get(colName);
// the base type they share
DataType type = DataType.getCommonSuperType(oldType, colType);
assert type.isASuperTypeOf(oldType);
assert type.isASuperTypeOf(colType);
// that shouldn't happen though, eh: shit happens.
if (!oldType.equals(type)) {
LOGGER.info("Confusing data types for column \"" + colName + "\": " + oldType.toString() + " vs. " + colType.toString() + "\n" + "Using common base type " + type.toString());
// that must not change the order.
typeSet.put(colName, type);
}
DataColumnDomain newDomain = merge(oldDomain, colDomain, type.getComparator());
domainSet.put(colName, newDomain);
} else {
// doesn't contain the key
typeSet.put(colName, colType);
domainSet.put(colName, colDomain);
}
}
// for all columns in the current table spec
}
// for all tables
DataColumnSpec[] colSpecs = new DataColumnSpec[typeSet.size()];
int i = 0;
for (Map.Entry<String, DataType> entry : typeSet.entrySet()) {
String name = entry.getKey();
DataType type = entry.getValue();
// domain is null, if we did not remember it (e.g. "keepDomain" was
// false)
DataColumnDomain domain = domainSet.get(name);
DataColumnSpec initSpec = columnSet.get(name);
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(initSpec);
specCreator.setDomain(domain);
specCreator.setType(type);
colSpecs[i++] = specCreator.createSpec();
}
return new DataTableSpec(colSpecs);
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class SubgroupMinerModel2 method createAssociationRulesSpec.
private DataTableSpec createAssociationRulesSpec(final DataTableSpec spec) {
DataType transType = spec.getColumnSpec(m_transactionColumn.getStringValue()).getType();
// now create the table spec
DataColumnSpec[] colSpecs = new DataColumnSpec[6];
DataColumnSpecCreator creator = new DataColumnSpecCreator("Support", DoubleCell.TYPE);
colSpecs[0] = creator.createSpec();
creator = new DataColumnSpecCreator("Confidence", DoubleCell.TYPE);
colSpecs[1] = creator.createSpec();
creator = new DataColumnSpecCreator("Lift", DoubleCell.TYPE);
colSpecs[2] = creator.createSpec();
DataType transCollType = transType.getCollectionElementType();
creator = new DataColumnSpecCreator("Consequent", transCollType == null ? StringCell.TYPE : transCollType);
colSpecs[3] = creator.createSpec();
creator = new DataColumnSpecCreator("implies", StringCell.TYPE);
colSpecs[4] = creator.createSpec();
creator = new DataColumnSpecCreator("Items", transCollType == null ? SetCell.getCollectionType(StringCell.TYPE) : SetCell.getCollectionType(transCollType));
colSpecs[5] = creator.createSpec();
return new DataTableSpec(colSpecs);
}
use of org.knime.core.data.DataType 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();
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class SotaNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
assert inSpecs.length == 1;
DataTableSpec inDataSpec = (DataTableSpec) inSpecs[SotaNodeModel.INPORT];
int numberCells = 0;
int fuzzyCells = 0;
for (int i = 0; i < inDataSpec.getNumColumns(); i++) {
DataType type = inDataSpec.getColumnSpec(i).getType();
if (SotaUtil.isNumberType(type)) {
numberCells++;
} else if (SotaUtil.isFuzzyIntervalType(type)) {
fuzzyCells++;
}
}
StringBuffer buffer = new StringBuffer();
if (numberCells > 0 && fuzzyCells > 0) {
buffer.append("FuzzyIntervalCells and NumberCells are mixed ! ");
}
if (numberCells <= 0 && fuzzyCells <= 0) {
buffer.append("Number of columns has to be " + "greater than zero ! ");
}
if (fuzzyCells <= 0 && m_sota.isUseHierarchicalFuzzyData()) {
buffer.append("No fuzzy cells selected ! ");
}
if (m_sota.isUseHierarchicalFuzzyData() && m_hierarchieLevelCell == null) {
buffer.append("No hierarchy column selected ! ");
}
// if buffer throw exception
if (buffer.length() > 0) {
throw new InvalidSettingsException(buffer.toString());
}
int classColIndex = inDataSpec.findColumnIndex(m_classCol.getStringValue());
return new PortObjectSpec[] { new SotaPortObjectSpec(inDataSpec, classColIndex) };
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class Distances method getCosinusDistance.
/**
* Computes the cosinus distance between the given two rows, with given
* offset.
*
* @param row1 first row to compute the cosinus distance of
* @param row2 second row to compute the cosinus distance of
* @param offset offset to substract cosinus distance from
* @param fuzzy if <code>true</code> only fuzzy data is respected, if
* <code>false</code> only number data
* @return the cosinus distance between the given two rows
*/
public static double getCosinusDistance(final DataRow row1, final DataRow row2, final double offset, final boolean fuzzy) {
double distance = 0;
double vectorMultRes = 0;
double vector1Length = 0;
double vector2Length = 0;
for (int i = 0; i < row1.getNumCells(); i++) {
DataType type1 = row1.getCell(i).getType();
DataType type2 = row2.getCell(i).getType();
if (SotaUtil.isNumberType(type1) && SotaUtil.isNumberType(type2) && !fuzzy) {
vectorMultRes += ((DoubleValue) row1.getCell(i)).getDoubleValue() * ((DoubleValue) row2.getCell(i)).getDoubleValue();
vector1Length += Math.pow(((DoubleValue) row1.getCell(i)).getDoubleValue(), 2);
vector2Length += Math.pow(((DoubleValue) row2.getCell(i)).getDoubleValue(), 2);
} else if (SotaUtil.isFuzzyIntervalType(type1) && SotaUtil.isFuzzyIntervalType(type2) && fuzzy) {
vectorMultRes += SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row1.getCell(i)) * SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row2.getCell(i));
vector1Length += Math.pow(SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row1.getCell(i)), 2);
vector2Length += Math.pow(SotaFuzzyMath.getCenterOfCoreRegion((FuzzyIntervalValue) row2.getCell(i)), 2);
}
}
vector1Length = Math.sqrt(vector1Length);
vector2Length = Math.sqrt(vector2Length);
distance = vectorMultRes / (vector1Length * vector2Length);
distance = offset - distance;
return distance;
}
Aggregations