use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class ARFFWriterNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
checkFileAccess(m_location, false);
URL url = FileUtil.toURL(m_location);
Path localPath = FileUtil.resolveToPath(url);
DataTableSpec inSpec = inData[0].getDataTableSpec();
int numOfCols = inSpec.getNumColumns();
for (int c = 0; c < numOfCols; c++) {
DataType colType = inSpec.getColumnSpec(c).getType();
if (!colType.isCompatible(IntValue.class) && !colType.isCompatible(DoubleValue.class) && !colType.isCompatible(StringValue.class)) {
throw new IllegalStateException("Can only write Double, Int," + " and String columns to ARFF file.");
}
}
LOGGER.info("ARFF Writer: ARFFing into '" + m_location + "'.");
try (BufferedWriter writer = openWriter(localPath, url)) {
// Write ARFF header
writer.write("%\n");
writer.write("% ARFF data file, generated by KNIME\n");
writer.write("%\n");
writer.write("% Date: " + new Date(System.currentTimeMillis()) + "\n");
try {
writer.write("% User: " + System.getProperty("user.name") + "\n");
} catch (SecurityException se) {
// okay - we don't add the user name.
}
writer.write("%\n");
writer.write("\n@RELATION " + m_relationName + "\n");
// write the attribute part, i.e. the columns' name and type
for (int c = 0; c < numOfCols; c++) {
DataColumnSpec cSpec = inSpec.getColumnSpec(c);
writer.write("@ATTRIBUTE ");
if (needsQuotes(cSpec.getName().toString())) {
writer.write("'" + cSpec.getName().toString() + "'");
} else {
writer.write(cSpec.getName().toString());
}
writer.write("\t");
writer.write(colspecToARFFType(cSpec));
writer.write("\n");
}
// finally add the data
writer.write("\n@DATA\n");
long rowCnt = inData[0].size();
long rowNr = 0;
for (DataRow row : inData[0]) {
rowNr++;
exec.setProgress(rowNr / (double) rowCnt, "Writing row " + rowNr + " ('" + row.getKey() + "') of " + rowCnt);
if (m_sparse) {
writer.write("{");
}
// flag to skip comma in first column
boolean first = true;
for (int c = 0; c < row.getNumCells(); c++) {
DataCell cell = row.getCell(c);
if (m_sparse && !cell.isMissing()) {
// we write only non-zero values in a sparse file
if ((cell instanceof IntValue) && (((IntValue) cell).getIntValue() == 0)) {
continue;
}
if ((cell instanceof DoubleValue) && (Math.abs(((DoubleValue) cell).getDoubleValue()) < 1e-29)) {
continue;
}
}
String data = "?";
if (!cell.isMissing()) {
data = cell.toString();
}
// trigger quotes.
if (needsQuotes(data)) {
data = "'" + data + "'";
}
// now spit it out
if (!first) {
// print column separator
writer.write(",");
} else {
first = false;
}
// data in sparse file must be proceeded by the column number
if (m_sparse) {
writer.write("" + c + " ");
}
writer.write(data);
}
if (m_sparse) {
writer.write("}");
}
writer.write("\n");
// see if user told us to stop.
// Check if execution was canceled !
exec.checkCanceled();
}
// while (!rIter.atEnd())
} catch (CanceledExecutionException ex) {
if (localPath != null) {
Files.deleteIfExists(localPath);
LOGGER.debug("File '" + localPath + "' deleted.");
}
throw ex;
}
// execution successful return empty array
return new BufferedDataTable[0];
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class TableColumnToVariableNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
if (inData[0] instanceof BufferedDataTable) {
final BufferedDataTable table = (BufferedDataTable) inData[0];
final int colIndex = table.getSpec().findColumnIndex(m_column.getStringValue());
assert colIndex >= 0 : colIndex;
for (DataRow dataRow : table) {
DataCell cell = dataRow.getCell(colIndex);
if (cell.isMissing()) {
if (m_ignoreMissing.getBooleanValue()) {
continue;
}
throw new Exception("Missing value in column (" + m_column.getColumnName() + ") in row: " + dataRow.getKey());
}
if (cell instanceof IntValue) {
final IntValue iv = (IntValue) cell;
pushFlowVariableInt(dataRow.getKey().getString(), iv.getIntValue());
} else if (cell instanceof DoubleValue) {
final DoubleValue dv = (DoubleValue) cell;
pushFlowVariableDouble(dataRow.getKey().getString(), dv.getDoubleValue());
} else if (cell instanceof StringValue) {
final StringValue sv = (StringValue) cell;
pushFlowVariableString(dataRow.getKey().getString(), sv.getStringValue());
}
}
}
return new FlowVariablePortObject[] { FlowVariablePortObject.INSTANCE };
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class MissingValueHandling2Panel method getFixTextField.
/*
* Helper in constructor, generates the text field to enter the replacement
* value.
*/
private static JComponent getFixTextField(final MissingValueHandling2ColSetting setting, final DataColumnSpec... specs) {
JComponent fixText;
// FIX text field
DataCell fixCell = setting.getFixCell();
switch(setting.getType()) {
case MissingValueHandling2ColSetting.TYPE_DOUBLE:
fixText = new JFormattedTextField();
((JFormattedTextField) fixText).setColumns(11);
Double doubel;
if (fixCell == null) {
doubel = new Double(0.0);
} else {
double d = ((DoubleValue) fixCell).getDoubleValue();
doubel = new Double(d);
}
((JFormattedTextField) fixText).setValue(doubel);
break;
case MissingValueHandling2ColSetting.TYPE_INT:
fixText = new JFormattedTextField();
((JFormattedTextField) fixText).setColumns(11);
Integer integer;
if (fixCell == null) {
integer = 0;
} else {
integer = ((IntValue) fixCell).getIntValue();
}
((JFormattedTextField) fixText).setValue(integer);
break;
case MissingValueHandling2ColSetting.TYPE_STRING:
final ArrayList<DataCell> vals = new ArrayList<DataCell>();
if (specs != null) {
for (DataColumnSpec spec : specs) {
if (spec != null && spec.getDomain().hasValues()) {
vals.addAll(spec.getDomain().getValues());
}
}
}
DefaultComboBoxModel model = new DefaultComboBoxModel(vals.toArray(new DataCell[0]));
fixText = new JComboBox(model);
((JComboBox) fixText).setPrototypeDisplayValue("#########");
((JComboBox) fixText).setEditable(true);
((JComboBox) fixText).setRenderer(new DefaultListCellRenderer() {
/**
* Overridden to set tooltip text properly.
*
* @see DefaultListCellRenderer#getListCellRendererComponent(JList, Object, int, boolean, boolean)
*/
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (c instanceof JComponent) {
((JComponent) c).setToolTipText(value.toString());
}
return c;
}
});
String string;
if (fixCell == null) {
string = "";
} else {
string = ((StringValue) fixCell).getStringValue();
}
model.setSelectedItem(string);
break;
default:
throw new InternalError("No such type");
}
return fixText;
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class AutoBinner method execute.
/**
* Determine bins.
*
* @param data the input data
* @param exec the execution context
* @return the operation with the discretisation information
* @throws Exception
*/
public PMMLPreprocDiscretize execute(final BufferedDataTable data, final ExecutionContext exec) throws Exception {
// Auto configuration when target is not set
final DataTableSpec spec = data.getDataTableSpec();
if (null == m_settings.getTargetColumn() || m_settings.getIncludeAll()) {
addAllNumericCols(spec);
}
// determine intervals
if (m_settings.getMethod().equals(Method.fixedNumber)) {
BufferedDataTable inData = calcDomainBoundsIfNeccessary(data, exec.createSubExecutionContext(0.9), Arrays.asList(m_settings.getTargetColumn()));
init(inData.getDataTableSpec());
Map<String, double[]> edgesMap = new HashMap<String, double[]>();
for (String target : m_settings.getTargetColumn()) {
DataTableSpec inSpec = inData.getDataTableSpec();
DataColumnSpec targetCol = inSpec.getColumnSpec(target);
// bounds of the domain
double min = ((DoubleValue) targetCol.getDomain().getLowerBound()).getDoubleValue();
double max = ((DoubleValue) targetCol.getDomain().getUpperBound()).getDoubleValue();
// the edges of the bins
double[] edges = new double[m_settings.getBinCount() + 1];
edges[0] = min;
edges[edges.length - 1] = max;
for (int i = 1; i < edges.length - 1; i++) {
edges[i] = min + i / (double) m_settings.getBinCount() * (max - min);
}
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else if (m_settings.getMethod().equals(Method.sampleQuantiles)) {
init(spec);
Map<String, double[]> edgesMap = new LinkedHashMap<String, double[]>();
final int colCount = m_settings.getTargetColumn().length;
// contains all numeric columns if include all is set!
for (String target : m_settings.getTargetColumn()) {
exec.setMessage("Calculating quantiles (column \"" + target + "\")");
ExecutionContext colSortContext = exec.createSubExecutionContext(0.7 / colCount);
ExecutionContext colCalcContext = exec.createSubExecutionContext(0.3 / colCount);
ColumnRearranger singleRearranger = new ColumnRearranger(spec);
singleRearranger.keepOnly(target);
BufferedDataTable singleColSorted = colSortContext.createColumnRearrangeTable(data, singleRearranger, colSortContext);
SortedTable sorted = new SortedTable(singleColSorted, Collections.singletonList(target), new boolean[] { true }, colSortContext);
colSortContext.setProgress(1.0);
double[] edges = createEdgesFromQuantiles(sorted.getBufferedDataTable(), colCalcContext, m_settings.getSampleQuantiles());
colCalcContext.setProgress(1.0);
exec.clearTable(singleColSorted);
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else {
throw new IllegalStateException("Unknown binning method.");
}
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class AutoBinner method execute.
/**
* Determine bins.
*
* @param data the input data
* @param exec the execution context
* @return the operation with the discretisation information
* @throws Exception ...
*/
public PMMLPreprocDiscretize execute(final BufferedDataTable data, final ExecutionContext exec) throws Exception {
// Auto configuration when target is not set
final DataTableSpec spec = data.getDataTableSpec();
if (null == m_settings.getTargetColumn() || m_settings.getIncludeAll()) {
addAllNumericCols(spec);
}
// determine intervals
if (m_settings.getMethod().equals(Method.fixedNumber)) {
if (m_settings.getEqualityMethod().equals(EqualityMethod.width)) {
BufferedDataTable inData = calcDomainBoundsIfNeccessary(data, exec.createSubExecutionContext(0.9), Arrays.asList(m_settings.getTargetColumn()));
init(inData.getDataTableSpec());
Map<String, double[]> edgesMap = new HashMap<String, double[]>();
for (String target : m_settings.getTargetColumn()) {
DataTableSpec inSpec = inData.getDataTableSpec();
DataColumnSpec targetCol = inSpec.getColumnSpec(target);
// bounds of the domain
double min = ((DoubleValue) targetCol.getDomain().getLowerBound()).getDoubleValue();
double max = ((DoubleValue) targetCol.getDomain().getUpperBound()).getDoubleValue();
// the edges of the bins
double[] edges = new double[m_settings.getBinCount() + 1];
edges[0] = min;
edges[edges.length - 1] = max;
for (int i = 1; i < edges.length - 1; i++) {
edges[i] = min + i / (double) m_settings.getBinCount() * (max - min);
}
if (m_settings.getIntegerBounds()) {
edges = toIntegerBounds(edges);
}
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else {
// EqualityMethod.equalCount
Map<String, double[]> edgesMap = new HashMap<String, double[]>();
for (String target : m_settings.getTargetColumn()) {
int colIndex = data.getDataTableSpec().findColumnIndex(target);
List<Double> values = new ArrayList<Double>();
for (DataRow row : data) {
if (!row.getCell(colIndex).isMissing()) {
values.add(((DoubleValue) row.getCell(colIndex)).getDoubleValue());
}
}
edgesMap.put(target, findEdgesForEqualCount(values, m_settings.getBinCount()));
}
return createDisretizeOp(edgesMap);
}
} else if (m_settings.getMethod().equals(Method.sampleQuantiles)) {
init(spec);
Map<String, double[]> edgesMap = new LinkedHashMap<String, double[]>();
final int colCount = m_settings.getTargetColumn().length;
// contains all numeric columns if include all is set!
for (String target : m_settings.getTargetColumn()) {
exec.setMessage("Calculating quantiles (column \"" + target + "\")");
ExecutionContext colSortContext = exec.createSubExecutionContext(0.7 / colCount);
ExecutionContext colCalcContext = exec.createSubExecutionContext(0.3 / colCount);
ColumnRearranger singleRearranger = new ColumnRearranger(spec);
singleRearranger.keepOnly(target);
BufferedDataTable singleColSorted = colSortContext.createColumnRearrangeTable(data, singleRearranger, colSortContext);
SortedTable sorted = new SortedTable(singleColSorted, Collections.singletonList(target), new boolean[] { true }, colSortContext);
colSortContext.setProgress(1.0);
double[] edges = createEdgesFromQuantiles(sorted.getBufferedDataTable(), colCalcContext, m_settings.getSampleQuantiles());
colCalcContext.setProgress(1.0);
exec.clearTable(singleColSorted);
if (m_settings.getIntegerBounds()) {
edges = toIntegerBounds(edges);
}
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else {
throw new IllegalStateException("Unknown binning method.");
}
}
Aggregations