use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class LinReg2Learner method init.
/**
* Initialize instance and check if settings are consistent.
*/
private void init(final DataTableSpec inSpec, final PMMLPortObjectSpec pmmlSpec, final Set<String> exclude) throws InvalidSettingsException {
m_warningMessage = null;
// Auto configuration when target is not set
if (m_settings.getTargetColumn() == null) {
List<DataColumnSpec> possibleTargets = new ArrayList<DataColumnSpec>();
for (DataColumnSpec colSpec : inSpec) {
if (colSpec.getType().isCompatible(DoubleValue.class)) {
possibleTargets.add(colSpec);
}
}
if (possibleTargets.size() > 1) {
String colName = possibleTargets.get(possibleTargets.size() - 1).getName();
m_settings.setTargetColumn(colName);
String warning = "The target column is not set. Using " + colName;
m_warningMessage = (m_warningMessage == null ? "" : m_warningMessage + "\n") + warning;
} else if (possibleTargets.size() == 1) {
m_settings.setTargetColumn(possibleTargets.get(0).getName());
} else {
throw new InvalidSettingsException("No column in " + "spec with numeric data.");
}
}
FilterResult colFilter = m_settings.getFilterConfiguration().applyTo(inSpec);
List<String> inputCols = new ArrayList<String>();
inputCols.addAll(Arrays.asList(colFilter.getIncludes()));
inputCols.remove(m_settings.getTargetColumn());
// remove all columns that should not be used
inputCols.removeAll(exclude);
if (inputCols.isEmpty()) {
throw new InvalidSettingsException("At least one column must be included.");
}
DataColumnSpec targetColSpec = null;
List<DataColumnSpec> regressorColSpecs = new ArrayList<DataColumnSpec>();
// Check type of target and input columns
for (int i = 0; i < inSpec.getNumColumns(); i++) {
DataColumnSpec colSpec = inSpec.getColumnSpec(i);
String colName = colSpec.getName();
if (m_settings.getTargetColumn().equals(colName)) {
if (colSpec.getType().isCompatible(DoubleValue.class)) {
targetColSpec = colSpec;
} else {
throw new InvalidSettingsException("Type of column \"" + colName + "\" is not numeric.");
}
} else if (inputCols.contains(colName)) {
if (colSpec.getType().isCompatible(DoubleValue.class) || colSpec.getType().isCompatible(NominalValue.class)) {
regressorColSpecs.add(colSpec);
} else {
throw new InvalidSettingsException("Type of column \"" + colName + "\" is not one of the allowed types, " + "which are numeric or nominal.");
}
}
}
if (targetColSpec != null) {
String[] learnerCols = new String[regressorColSpecs.size() + 1];
for (int i = 0; i < regressorColSpecs.size(); i++) {
learnerCols[i] = regressorColSpecs.get(i).getName();
}
learnerCols[learnerCols.length - 1] = targetColSpec.getName();
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(pmmlSpec, inSpec);
creator.setTargetCols(Arrays.asList(targetColSpec));
creator.setLearningCols(regressorColSpecs);
m_pmmlOutSpec = creator.createSpec();
m_learner = new Learner(m_pmmlOutSpec, m_settings.getIncludeConstant(), m_settings.getOffsetValue(), m_settings.getMissingValueHandling2().equals(MissingValueHandling.fail));
} else {
throw new InvalidSettingsException("The target is " + "not in the input.");
}
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class LinReg2LearnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
final BufferedDataTable data = (BufferedDataTable) inObjects[0];
// cache the entire table as otherwise the color information
// may be lost (filtering out the "colored" column)
m_rowContainer = new DefaultDataArray(data, m_settings.getScatterPlotFirstRow(), m_settings.getScatterPlotRowCount());
DataTableSpec tableSpec = data.getDataTableSpec();
// handle the optional PMML input
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inObjects[1] : null;
PMMLPortObjectSpec inPMMLSpec = null;
if (inPMMLPort != null) {
inPMMLSpec = inPMMLPort.getSpec();
} else {
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(tableSpec);
inPMMLSpec = creator.createSpec();
inPMMLPort = new PMMLPortObject(inPMMLSpec);
}
LinReg2Learner learner = new LinReg2Learner(new PortObjectSpec[] { tableSpec, inPMMLSpec }, m_pmmlInEnabled, m_settings);
m_content = learner.execute(new PortObject[] { data, inPMMLPort }, exec);
if (learner.getWarningMessage() != null && learner.getWarningMessage().length() > 0) {
setWarningMessage(learner.getWarningMessage());
}
// third argument is ignored since we provide a port
PMMLPortObject outPMMLPort = new PMMLPortObject((PMMLPortObjectSpec) learner.getOutputSpec()[0], inPMMLPort, null);
PMMLGeneralRegressionTranslator trans = new PMMLGeneralRegressionTranslator(m_content.createGeneralRegressionContent());
outPMMLPort.addModelTranslater(trans);
final String warningMessage = m_content.getWarningMessage();
if (warningMessage != null) {
setWarningMessage(getWarningMessage() == null ? warningMessage : (getWarningMessage() + "\n" + warningMessage));
}
return new PortObject[] { outPMMLPort, m_content.createTablePortObject(exec) };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class PolyRegLearnerNodeModel method createModelSpec.
private PMMLPortObjectSpec createModelSpec(final PMMLPortObjectSpec inModelSpec, final DataTableSpec inDataSpec) throws InvalidSettingsException {
String[] selectedCols = computeSelectedColumns(inDataSpec);
DataColumnSpec[] usedColumns = new DataColumnSpec[selectedCols.length + 1];
int k = 0;
Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
for (DataColumnSpec dcs : inDataSpec) {
if (hash.contains(dcs.getName())) {
usedColumns[k++] = dcs;
}
}
usedColumns[k++] = inDataSpec.getColumnSpec(m_settings.getTargetColumn());
DataTableSpec tableSpec = new DataTableSpec(usedColumns);
PMMLPortObjectSpecCreator crea = new PMMLPortObjectSpecCreator(inModelSpec, inDataSpec);
crea.setLearningCols(tableSpec);
crea.setTargetCol(usedColumns[k - 1]);
return crea.createSpec();
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class ClusterNodeModel method configure.
/**
* Returns <code>true</code> always and passes the current input spec to
* the output spec which is identical to the input specification - after
* all, we are building cluster centers in the original feature space.
*
* @param inSpecs the specifications of the input port(s) - should be one
* @return the copied input spec
* @throws InvalidSettingsException if PMML incompatible type was found
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec spec = (DataTableSpec) inSpecs[0];
// input is output spec with all double compatible values set to
// Double.
m_dimension = spec.getNumColumns();
// Find out which columns we can use (must be Double compatible)
// Note that, for simplicity, we still use the entire dimensionality
// for cluster prototypes below and simply ignore useless columns.
m_ignoreColumn = new boolean[m_dimension];
m_nrIgnoredColumns = 0;
LinkedList<String> includes = new LinkedList<String>();
includes.addAll(m_usedColumns.getIncludeList());
LinkedList<String> excludes = new LinkedList<String>();
excludes.addAll(m_usedColumns.getExcludeList());
LinkedList<String> includes2 = new LinkedList<String>();
includes2.addAll(m_usedColumns.getIncludeList());
LinkedList<String> excludes2 = new LinkedList<String>();
excludes2.addAll(m_usedColumns.getExcludeList());
// First check if all incoming columns are either excluded or included
for (String col : spec.getColumnNames()) {
if (m_usedColumns.getIncludeList().contains(col)) {
includes2.remove(col);
} else if (m_usedColumns.getExcludeList().contains(col)) {
excludes2.remove(col);
} else {
includes.add(col);
}
}
// Leftover included columns that do not exist in the incoming table
for (String col : includes2) {
includes.remove(col);
}
// Same for excluded columns
for (String col : excludes2) {
excludes.remove(col);
}
m_usedColumns.setExcludeList(excludes);
m_usedColumns.setIncludeList(includes);
if (m_usedColumns.isKeepAllSelected()) {
boolean hasNumericColumn = false;
for (DataColumnSpec colSpec : spec) {
if (colSpec.getType().isCompatible(DoubleValue.class)) {
hasNumericColumn = true;
break;
}
}
if (!hasNumericColumn) {
throw new InvalidSettingsException("No numeric columns in input");
}
} else {
// double compatible columns
if (m_usedColumns.getIncludeList().size() == 0 && m_usedColumns.getExcludeList().size() == 0) {
List<String> includedColumns = new ArrayList<String>();
List<String> excludedColumns = new ArrayList<String>();
for (int i = 0; i < spec.getNumColumns(); i++) {
DataColumnSpec colSpec = spec.getColumnSpec(i);
if (colSpec.getType().isCompatible(DoubleValue.class)) {
includedColumns.add(colSpec.getName());
} else {
excludedColumns.add(colSpec.getName());
}
}
// set all double compatible columns as include list
m_usedColumns.setIncludeList(includedColumns);
m_usedColumns.setExcludeList(excludedColumns);
}
// check if some columns are included
if (m_usedColumns.getIncludeList().size() <= 0) {
setWarningMessage("No column in include list! Produces one huge cluster");
}
}
addExcludeColumnsToIgnoreList(spec);
DataTableSpec appendedSpec = createAppendedSpec(spec);
// return spec for data and model outport!
PMMLPortObjectSpec pmmlSpec;
if (m_pmmlInEnabled) {
pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
} else {
pmmlSpec = new PMMLPortObjectSpecCreator(spec).createSpec();
}
if (m_outputCenters) {
return new PortObjectSpec[] { appendedSpec, createClusterCentersSpec(spec), createPMMLSpec(pmmlSpec, spec) };
} else {
return new PortObjectSpec[] { appendedSpec, createPMMLSpec(pmmlSpec, spec) };
}
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.
the class FuzzyClusterNodeModel method createPMMLPortObjectSpec.
private PMMLPortObjectSpec createPMMLPortObjectSpec(final PMMLPortObjectSpec pmmlSpec, final DataTableSpec inspec, final List<String> learningCols) {
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inspec);
pmmlSpecCreator.setLearningColsNames(learningCols);
return pmmlSpecCreator.createSpec();
}
Aggregations