use of org.knime.base.node.util.DataArray in project knime-core by knime.
the class PolyRegLearnerNodeModel method loadInternals.
/**
* {@inheritDoc}
*/
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
File f = new File(nodeInternDir, "data.zip");
final DataArray rowContainer;
if (f.exists()) {
ContainerTable t = DataContainer.readFromZip(f);
int rowCount = t.getRowCount();
rowContainer = new DefaultDataArray(t, 1, rowCount, exec);
} else {
throw new FileNotFoundException("Internals do not exist");
}
f = new File(nodeInternDir, "internals.xml");
if (f.exists()) {
NodeSettingsRO internals = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(f)));
try {
double[] betas = internals.getDoubleArray("betas");
String[] columnNames = internals.getStringArray("columnNames");
double squaredError = internals.getDouble("squaredError");
double adjustedR2 = internals.getDouble("adjustedSquaredError", Double.NaN);
double[] meanValues = internals.getDoubleArray("meanValues");
double[] emptyArray = new double[betas.length];
Arrays.fill(emptyArray, Double.NaN);
double[] stdErrs = internals.getDoubleArray("stdErrors", emptyArray);
double[] tValues = internals.getDoubleArray("tValues", emptyArray);
double[] pValues = internals.getDoubleArray("pValues", emptyArray);
m_viewData = new PolyRegViewData(meanValues, betas, stdErrs, tValues, pValues, squaredError, adjustedR2, columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
} catch (InvalidSettingsException ex) {
throw new IOException("Old or corrupt internals", ex);
}
} else {
throw new FileNotFoundException("Internals do not exist");
}
}
use of org.knime.base.node.util.DataArray in project knime-core by knime.
the class PolyRegLearnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[0];
DataTableSpec inSpec = inTable.getDataTableSpec();
final int colCount = inSpec.getNumColumns();
String[] selectedCols = computeSelectedColumns(inSpec);
Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
m_colSelected = new boolean[colCount];
for (int i = 0; i < colCount; i++) {
m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
}
final int rowCount = inTable.getRowCount();
String[] temp = new String[m_columnNames.length + 1];
System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
temp[temp.length - 1] = m_settings.getTargetColumn();
FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
final DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
// handle the optional PMML input
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
PortObjectSpec[] outputSpec = configure((inPMMLPort == null) ? new PortObjectSpec[] { inData[0].getSpec(), null } : new PortObjectSpec[] { inData[0].getSpec(), inPMMLPort.getSpec() });
Learner learner = new Learner((PMMLPortObjectSpec) outputSpec[0], 0d, m_settings.getMissingValueHandling() == MissingValueHandling.fail, m_settings.getDegree());
try {
PolyRegContent polyRegContent = learner.perform(inTable, exec);
m_betas = fillBeta(polyRegContent);
m_meanValues = polyRegContent.getMeans();
ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
PortObject[] bdt = new PortObject[] { createPMMLModel(inPMMLPort, inSpec), exec.createColumnRearrangeTable(inTable, crea, exec.createSilentSubExecutionContext(.2)), polyRegContent.createTablePortObject(exec.createSubExecutionContext(0.2)) };
m_squaredError /= rowCount;
if (polyRegContent.getWarningMessage() != null) {
setWarningMessage(polyRegContent.getWarningMessage());
}
double[] stdErrors = PolyRegViewData.mapToArray(polyRegContent.getStandardErrors(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptStdErr());
double[] tValues = PolyRegViewData.mapToArray(polyRegContent.getTValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptTValue());
double[] pValues = PolyRegViewData.mapToArray(polyRegContent.getPValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptPValue());
m_viewData = new PolyRegViewData(m_meanValues, m_betas, stdErrors, tValues, pValues, m_squaredError, polyRegContent.getAdjustedRSquared(), m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
return bdt;
} catch (ModelSpecificationException e) {
final String origWarning = getWarningMessage();
final String warning = (origWarning != null && !origWarning.isEmpty()) ? (origWarning + "\n") : "" + e.getMessage();
setWarningMessage(warning);
final ExecutionContext subExec = exec.createSubExecutionContext(.1);
final BufferedDataContainer empty = subExec.createDataContainer(STATS_SPEC);
int rowIdx = 1;
for (final String column : m_columnNames) {
for (int d = 1; d <= m_settings.getDegree(); ++d) {
empty.addRowToTable(new DefaultRow("Row" + rowIdx++, new StringCell(column), new IntCell(d), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
}
}
empty.addRowToTable(new DefaultRow("Row" + rowIdx, new StringCell("Intercept"), new IntCell(0), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
double[] nans = new double[m_columnNames.length * m_settings.getDegree() + 1];
Arrays.fill(nans, Double.NaN);
m_betas = new double[nans.length];
// Mean only for the linear tags
m_meanValues = new double[nans.length / m_settings.getDegree()];
m_viewData = new PolyRegViewData(m_meanValues, m_betas, nans, nans, nans, m_squaredError, Double.NaN, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
empty.close();
ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
BufferedDataTable rearrangerTable = exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6));
PMMLPortObject model = createPMMLModel(inPMMLPort, inTable.getDataTableSpec());
PortObject[] bdt = new PortObject[] { model, rearrangerTable, empty.getTable() };
return bdt;
}
}
use of org.knime.base.node.util.DataArray in project knime-core by knime.
the class HierarchicalClusterNodeView method modelChanged.
/**
* {@inheritDoc}
*/
@Override
public void modelChanged() {
if (getNodeModel() == null || ((DataProvider) getNodeModel()).getDataArray(0) == null || ((DataProvider) getNodeModel()).getDataArray(0).size() == 0) {
return;
}
NodeModel model = getNodeModel();
m_dendroPlotter.reset();
m_distancePlotter.reset();
m_dendroPlotter.setHiLiteHandler(model.getInHiLiteHandler(0));
m_dendroPlotter.setAntialiasing(false);
m_dendroPlotter.setDataProvider((DataProvider) model);
m_distancePlotter.setDataProvider((DataProvider) model);
m_distancePlotter.setHiLiteHandler(model.getInHiLiteHandler(0));
DendrogramNode rootNode = getNodeModel().getRootNode();
DataArray distanceTable = ((DataProvider) getNodeModel()).getDataArray(0);
m_dendroPlotter.setRootNode(rootNode);
m_distancePlotter.createXCoordinate(((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(0).getDomain().getLowerBound()).getDoubleValue(), ((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(0).getDomain().getUpperBound()).getDoubleValue());
m_distancePlotter.createYCoordinate(((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(1).getDomain().getLowerBound()).getDoubleValue(), ((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(1).getDomain().getUpperBound()).getDoubleValue());
((BasicDrawingPane) m_distancePlotter.getDrawingPane()).clearPlot();
m_distancePlotter.addLine(distanceTable, 0, 1, Color.BLACK, new BasicStroke(m_thickness));
// m_distancePlotter.getXAxis().getCoordinate().setPolicy(
// DescendingNumericTickPolicyStrategy.getInstance());
m_distancePlotter.updatePaintModel();
m_dendroPlotter.updatePaintModel();
}
use of org.knime.base.node.util.DataArray in project knime-core by knime.
the class LinePlotter method updatePaintModel.
/**
* Creates the color mapping by dividing the hue circle of the HSB color
* model by the number of selected columns, calculates the coordinates by
* determining the overall minimum and maximum values of the selected
* columns and maps the data points to the resulting screen coordinates.
*
* @see org.knime.base.node.viz.plotter.AbstractPlotter#updatePaintModel()
*/
@Override
public void updatePaintModel() {
if (getDataProvider() != null && getDataProvider().getDataArray(getDataArrayIdx()) != null) {
// draw the points and add the lines
DataArray array = getDataProvider().getDataArray(getDataArrayIdx());
DataTableSpec spec = array.getDataTableSpec();
if (m_columnNames == null) {
initColumnNames(array);
initializeColors(spec);
}
// set only displayed columns for the color mapping in the
// color legend and drawing pane....
Map<String, Color> displayedColors = new LinkedHashMap<String, Color>();
// color mapping is initially defined for all columns
for (String colName : m_columnNames) {
Color c = m_colorMapping.get(colName);
displayedColors.put(colName, c);
}
displayedColors.keySet().retainAll(m_columnNames);
calculateCoordinates(array);
calculateDots();
// if we have line plotter properties update them
if (getProperties() instanceof LinePlotterProperties) {
((LinePlotterProperties) getProperties()).updateColumnSelection(array.getDataTableSpec(), m_columnNames);
((LinePlotterProperties) getProperties()).updateColorLegend(displayedColors);
}
getDrawingPane().repaint();
}
}
use of org.knime.base.node.util.DataArray in project knime-core by knime.
the class LinePlotter method calculateDots.
/**
* Calculates the screen coordinates (dots) for the lines and puts them in a
* large {@link org.knime.base.node.viz.plotter.scatter.DotInfoArray}, which
* is passed to the
* {@link org.knime.base.node.viz.plotter.line.LinePlotterDrawingPane}.
*/
protected void calculateDots() {
if (!(getDrawingPane() instanceof ScatterPlotterDrawingPane)) {
return;
}
if (m_columnNames == null) {
return;
}
if (getDataProvider() != null && getDataProvider().getDataArray(getDataArrayIdx()) != null) {
DataArray array = getDataProvider().getDataArray(getDataArrayIdx());
int nrOfRows = array.size();
// set the empty dots to delete the old ones
// if we have no columns to display
((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(new DotInfo[0]));
// first store them in a list to avoid keep tracking of indices
List<DotInfo> dotList = new ArrayList<DotInfo>();
for (String col : m_columnNames) {
int colIdx = array.getDataTableSpec().findColumnIndex(col);
Color c = m_colorMapping.get(col);
if (c == null) {
c = Color.black;
}
ColorAttr color = ColorAttr.getInstance(c);
// store the last point with valid value for interpolation
Point p1 = new Point(-1, -1);
Point p2;
List<DotInfo> missingValues = new ArrayList<DotInfo>();
// create the dots
for (int row = 0; row < nrOfRows; row++) {
DataCell cell = array.getRow(row).getCell(colIdx);
int y = -1;
DotInfo dot;
int x = getMappedXValue(new StringCell(array.getRow(row).getKey().getString()));
if (!cell.isMissing()) {
y = getMappedYValue(cell);
if (missingValues.size() > 0) {
// we have some missing values in between,
// thus we have to interpolate
p2 = new Point(x, y);
DotInfo[] interpolated = interpolate(p1, p2, missingValues);
// and add them
for (DotInfo p : interpolated) {
dotList.add(p);
}
// and clear the list again
missingValues.clear();
}
p1 = new Point(x, y);
dot = new DotInfo(x, y, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
dot.setXDomainValue(new StringCell(array.getRow(row).getKey().getString()));
dot.setYDomainValue(cell);
dotList.add(dot);
} else if (!m_interpolate) {
// LOGGER.debug("missing value");
dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
dotList.add(dot);
} else {
// interpolate
dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
missingValues.add(dot);
}
}
// un-interpolated at the end, we add them anyway
if (!missingValues.isEmpty()) {
DotInfo[] interpolated = interpolate(p1, null, missingValues);
// and add them
for (DotInfo p : interpolated) {
dotList.add(p);
}
// and clear the list again
missingValues.clear();
}
}
DotInfo[] dots = new DotInfo[dotList.size()];
dotList.toArray(dots);
((LinePlotterDrawingPane) getDrawingPane()).setNumberOfLines(nrOfRows);
((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(dots));
}
}
Aggregations