use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class KnnNodeModel method classify.
// returns a list where the first value if the winner class, and the
// following values are the class probabilities (if enabled)
private List<DataCell> classify(final DataRow row, final KDTree<DataCell> tree, final List<Integer> featureColumns, final Map<Integer, Integer> firstToSecond, final DataCell[] allClassValues) {
double[] features = createQueryVector(row, featureColumns, firstToSecond);
List<DataCell> output = new ArrayList<DataCell>();
if (features == null) {
for (int i = 0; i < 1 + allClassValues.length; i++) {
output.add(DataType.getMissingCell());
}
return output;
}
HashMap<DataCell, MutableDouble> classWeights = new LinkedHashMap<DataCell, MutableDouble>();
List<NearestNeighbour<DataCell>> nearestN = tree.getKNearestNeighbours(features, Math.min(m_settings.k(), tree.size()));
for (NearestNeighbour<DataCell> n : nearestN) {
MutableDouble count = classWeights.get(n.getData());
if (count == null) {
count = new MutableDouble(0);
classWeights.put(n.getData(), count);
}
if (m_settings.weightByDistance()) {
count.add(1 / n.getDistance());
} else {
count.inc();
}
}
double winnerWeight = 0;
double weightSum = 0;
DataCell winnerCell = DataType.getMissingCell();
for (Map.Entry<DataCell, MutableDouble> e : classWeights.entrySet()) {
double weight = e.getValue().doubleValue();
if (weight > winnerWeight) {
winnerWeight = weight;
winnerCell = e.getKey();
}
weightSum += weight;
}
// check if there are other classes with the same weight
for (Map.Entry<DataCell, MutableDouble> e : classWeights.entrySet()) {
double weight = e.getValue().doubleValue();
if (weight == winnerWeight) {
if (m_classDistribution.get(winnerCell).intValue() < m_classDistribution.get(e.getKey()).intValue()) {
winnerCell = e.getKey();
}
}
}
output.add(winnerCell);
if (m_settings.outputClassProbabilities()) {
for (DataCell classVal : allClassValues) {
MutableDouble v = classWeights.get(classVal);
if (v == null) {
output.add(new DoubleCell(0));
// } else if (Double.isInfinite(v.doubleValue())) { // if distance to prototype is 0
// output.add(new DoubleCell(1));
} else {
output.add(new DoubleCell(v.doubleValue() / weightSum));
}
}
}
return output;
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class PCANodeModel method createCovarianceTable.
/**
* create data table from covariance matrix.
*
* @param exec
* execution context
* @param m
* covariance matrix
* @param inputColumnNames
* names of input columns the matrix was created from
* @return table
*/
public static BufferedDataTable createCovarianceTable(final ExecutionContext exec, final double[][] m, final String[] inputColumnNames) {
final BufferedDataContainer bdt = exec.createDataContainer(createCovarianceMatrixSpec(inputColumnNames));
for (int i = 0; i < m.length; i++) {
final DataCell[] cells = new DataCell[inputColumnNames.length];
for (int j = 0; j < m[i].length; j++) {
cells[j] = new DoubleCell(m[i][j]);
}
bdt.addRowToTable(new DefaultRow(inputColumnNames[i], cells));
}
bdt.close();
final BufferedDataTable covarianceTable = bdt.getTable();
return covarianceTable;
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class BinModelPlotter method updatePaintModel.
/**
* {@inheritDoc}
*/
@Override
public synchronized void updatePaintModel() {
if (m_discretizationModel == null) {
return;
}
// clear the drawing pane
((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(null);
// get the first columns
if (m_selectedColumns == null) {
m_selectedColumns = new LinkedHashSet<String>();
String[] binnedColumnNames = m_discretizationModel.getIncludedColumnNames();
for (int i = 0; i < binnedColumnNames.length; i++) {
// add them to the selected columns
m_selectedColumns.add(binnedColumnNames[i]);
}
((MultiColumnPlotterProperties) getProperties()).updateColumnSelection(m_binnedColumnsSpec, m_selectedColumns);
}
if (m_selectedColumns.size() == 0) {
getDrawingPane().repaint();
return;
}
Set<DataCell> selectedColumnCells = new LinkedHashSet<DataCell>();
m_coordinates = new ArrayList<Coordinate>();
List<Integer> columnIndices = new ArrayList<Integer>();
for (String name : m_selectedColumns) {
int idx = m_binnedColumnsSpec.findColumnIndex(name);
if (idx >= 0) {
selectedColumnCells.add(new StringCell(name));
DataColumnSpec colSpec = m_binnedColumnsSpec.getColumnSpec(idx);
columnIndices.add(idx);
Coordinate coordinate = Coordinate.createCoordinate(colSpec);
m_coordinates.add(coordinate);
}
}
// get the binning schemes for the selected columns
DiscretizationScheme[] selectedSchemes = getSelectedSchemes();
String[] selectedColumnNames = getSelectedColumnNames();
// calculate the display coordinates for the drawing pane
BinRuler[] binRulers = new BinRuler[selectedSchemes.length];
// determine the width available for a bin ruler
int rulerWidth = getDrawingPaneDimension().width - 2 * m_hMargin;
for (int i = 0; i < selectedSchemes.length; i++) {
double[] bounds = selectedSchemes[i].getBounds();
double min = bounds[0];
double max = bounds[bounds.length - 1];
// first create a colum spec from the schemes
DataColumnSpecCreator columnSpecCreator = new DataColumnSpecCreator("", DoubleCell.TYPE);
columnSpecCreator.setDomain(new DataColumnDomainCreator(new DoubleCell(min), new DoubleCell(max)).createDomain());
DoubleCoordinate coordinate = (DoubleCoordinate) Coordinate.createCoordinate(columnSpecCreator.createSpec());
Point leftStart = new Point(m_hMargin, m_vMargin + (i + 1) * m_columnDisplayHeight);
int[] binPositions = new int[bounds.length];
String[] binLabels = new String[bounds.length];
int count = 0;
for (double bound : bounds) {
binPositions[count] = (int) coordinate.calculateMappedValue(new DoubleCell(bound), rulerWidth, true);
binLabels[count] = coordinate.formatNumber(bounds[count]);
count++;
}
binRulers[i] = new BinRuler(leftStart, rulerWidth, binPositions, binLabels, selectedColumnNames[i]);
}
((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(binRulers);
m_hMargin = 10;
m_vMargin = 10;
((BinModelDrawingPane) getDrawingPane()).setHorizontalMargin(m_hMargin);
setHeight(binRulers[binRulers.length - 1].getLeftStartPoint().y + 40);
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class ColumnRowFilterPanel method getBoundCell.
/* method used from the above */
private DataCell getBoundCell(final JTextField editField, final String name) throws InvalidSettingsException {
if (editField.getText().length() <= 0) {
return null;
}
String colName = getSelectedColumnName();
if ((colName == null) || (colName.length() == 0)) {
throw new InvalidSettingsException("Invalid columns selection");
}
if (m_tSpec != null) {
final DataType origType = m_tSpec.getColumnSpec(colName).getType();
final DataType cType;
if (m_deepFiltering.isSelected() && origType.isCollectionType()) {
cType = origType.getCollectionElementType();
} else {
cType = origType;
}
if (cType.isCompatible(IntValue.class)) {
// first try making of an IntCell
try {
int lb = Integer.parseInt(editField.getText());
return new IntCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid integer.");
}
} else if (cType.isCompatible(LongValue.class)) {
try {
long lb = Long.parseLong(editField.getText());
return new LongCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid number.");
}
} else if (cType.isCompatible(DoubleValue.class)) {
try {
double lb = Double.parseDouble(editField.getText());
return new DoubleCell(lb);
} catch (NumberFormatException nfe) {
throw new InvalidSettingsException("Number format error in " + name + " bound number: enter a valid " + "float number");
}
} else {
return new StringCell(editField.getText());
}
} else {
// if we got no column type
return new StringCell(editField.getText());
}
}
use of org.knime.core.data.def.DoubleCell in project knime-core by knime.
the class ConditionalBoxPlotNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
m_statistics = new LinkedHashMap<DataColumnSpec, double[]>();
m_mildOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
m_extremeOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
double nrRows = inData[0].size();
int rowCount = 0;
int numericIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.numericColumn());
int nominalIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.nominalColumn());
Map<String, Map<Double, Set<RowKey>>> data = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
// some default values .. if one column only has missing values.
for (DataCell d : inData[0].getDataTableSpec().getColumnSpec(nominalIndex).getDomain().getValues()) {
String name = ((StringValue) d).getStringValue();
m_mildOutliers.put(name, new HashMap<Double, Set<RowKey>>());
m_extremeOutliers.put(name, new HashMap<Double, Set<RowKey>>());
}
for (DataRow r : inData[0]) {
exec.checkCanceled();
exec.setProgress(rowCount++ / nrRows, "Separating...");
if (!m_settings.showMissingValues()) {
if (r.getCell(nominalIndex).isMissing()) {
// missing cell in nominal values is unwanted?
continue;
}
}
String nominal = replaceSpaces(r.getCell(nominalIndex).toString());
if (r.getCell(numericIndex).isMissing()) {
// ignore missing cells in numeric column
continue;
}
DoubleValue numeric = (DoubleValue) r.getCell(numericIndex);
Map<Double, Set<RowKey>> map = data.get(nominal);
if (map == null) {
map = new LinkedHashMap<Double, Set<RowKey>>();
}
Set<RowKey> set = map.get(numeric.getDoubleValue());
if (set == null) {
set = new HashSet<RowKey>();
}
set.add(r.getKey());
map.put(numeric.getDoubleValue(), set);
data.put(nominal, map);
}
List<String> keys = new ArrayList<String>(data.keySet());
boolean ignoreMissingValues = false;
if (m_settings.showMissingValues() && !keys.contains(DataType.getMissingCell().toString())) {
// we promised to create data for missing values..
// if there aren't any.. we have to create them ourselves
setWarningMessage("No missing values found.");
ignoreMissingValues = true;
}
Collections.sort(keys);
DataColumnSpec[] colSpecs = createColumnSpec(inData[0].getDataTableSpec().getColumnSpec(nominalIndex), ignoreMissingValues);
if (keys.size() == 0) {
setWarningMessage("All classes are empty.");
}
int dataSetNr = 0;
// for (String d : keys) {
for (DataColumnSpec dcs : colSpecs) {
String d = dcs.getName();
if (data.get(d) == null || keys.size() == 0) {
dataSetNr++;
continue;
}
exec.checkCanceled();
exec.setProgress(dataSetNr / (double) keys.size(), "Creating statistics");
Map<Double, Set<RowKey>> extremeOutliers = new LinkedHashMap<Double, Set<RowKey>>();
Map<Double, Set<RowKey>> mildOutliers = new LinkedHashMap<Double, Set<RowKey>>();
double[] stats = calculateStatistic(data.get(d), mildOutliers, extremeOutliers);
double minimum = stats[BoxPlotNodeModel.MIN];
double maximum = stats[BoxPlotNodeModel.MAX];
DataColumnSpecCreator creator = new DataColumnSpecCreator(colSpecs[dataSetNr]);
creator.setDomain(new DataColumnDomainCreator(new DoubleCell(minimum), new DoubleCell(maximum)).createDomain());
colSpecs[dataSetNr] = creator.createSpec();
m_statistics.put(colSpecs[dataSetNr], stats);
m_mildOutliers.put(d, mildOutliers);
m_extremeOutliers.put(d, extremeOutliers);
dataSetNr++;
}
DataTableSpec dts = new DataTableSpec("MyTempTable", colSpecs);
DataContainer cont = new DataContainer(dts);
cont.close();
m_dataArray = new DefaultDataArray(cont.getTable(), 1, 2);
cont.dispose();
if (ignoreMissingValues) {
DataColumnSpec[] temp = new DataColumnSpec[colSpecs.length + 1];
DataColumnSpec missing = new DataColumnSpecCreator(DataType.getMissingCell().toString(), DataType.getMissingCell().getType()).createSpec();
int i = 0;
while (missing.getName().compareTo(colSpecs[i].getName()) > 0) {
temp[i] = colSpecs[i];
i++;
}
temp[i++] = missing;
while (i < temp.length) {
temp[i] = colSpecs[i - 1];
i++;
}
colSpecs = temp;
}
/* Save inSpec of the numeric column to provide the view a way to
* consider the input domain for normalization. */
m_numColSpec = inData[0].getDataTableSpec().getColumnSpec(numericIndex);
return new BufferedDataTable[] { createOutputTable(inData[0].getDataTableSpec(), colSpecs, exec).getTable() };
}
Aggregations