use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class DataColumnSpecFilterPMMLNodeModel method createPMMLOut.
private PMMLPortObject createPMMLOut(final PMMLPortObject pmmlIn, final DataTableSpec outSpec, final FilterResult res) throws XmlException {
StringBuffer warningBuffer = new StringBuffer();
if (pmmlIn == null) {
return new PMMLPortObject(createPMMLSpec(null, outSpec, res));
} else {
PMMLDocument pmmldoc;
try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
pmmldoc = PMMLDocument.Factory.parse(supplier.get());
}
// Inspect models to check if they use any excluded columns
List<PMMLModelWrapper> models = PMMLModelWrapper.getModelListFromPMMLDocument(pmmldoc);
for (PMMLModelWrapper model : models) {
MiningSchema ms = model.getMiningSchema();
for (MiningField mf : ms.getMiningFieldList()) {
if (isExcluded(mf.getName(), res)) {
if (warningBuffer.length() != 0) {
warningBuffer.append("\n");
}
warningBuffer.append(model.getModelType().name() + " uses excluded column " + mf.getName());
}
}
}
ArrayList<String> warningFields = new ArrayList<String>();
PMML pmml = pmmldoc.getPMML();
// Now check the transformations if they exist
if (pmml.getTransformationDictionary() != null) {
for (DerivedField df : pmml.getTransformationDictionary().getDerivedFieldList()) {
FieldRef fr = df.getFieldRef();
if (fr != null && isExcluded(fr.getField(), res)) {
warningFields.add(fr.getField());
}
Aggregate a = df.getAggregate();
if (a != null && isExcluded(a.getField(), res)) {
warningFields.add(a.getField());
}
Apply ap = df.getApply();
if (ap != null) {
for (FieldRef fieldRef : ap.getFieldRefList()) {
if (isExcluded(fieldRef.getField(), res)) {
warningFields.add(fieldRef.getField());
break;
}
}
}
Discretize d = df.getDiscretize();
if (d != null && isExcluded(d.getField(), res)) {
warningFields.add(d.getField());
}
MapValues mv = df.getMapValues();
if (mv != null) {
for (FieldColumnPair fcp : mv.getFieldColumnPairList()) {
if (isExcluded(fcp.getField(), res)) {
warningFields.add(fcp.getField());
}
}
}
NormContinuous nc = df.getNormContinuous();
if (nc != null && isExcluded(nc.getField(), res)) {
warningFields.add(nc.getField());
}
NormDiscrete nd = df.getNormDiscrete();
if (nd != null && isExcluded(nd.getField(), res)) {
warningFields.add(nd.getField());
}
}
}
DataDictionary dict = pmml.getDataDictionary();
List<DataField> fields = dict.getDataFieldList();
// Apply filter to spec
int numFields = 0;
for (int i = fields.size() - 1; i >= 0; i--) {
if (isExcluded(fields.get(i).getName(), res)) {
dict.removeDataField(i);
} else {
numFields++;
}
}
dict.setNumberOfFields(new BigInteger(Integer.toString(numFields)));
pmml.setDataDictionary(dict);
pmmldoc.setPMML(pmml);
// generate warnings and set as warning message
for (String s : warningFields) {
if (warningBuffer.length() != 0) {
warningBuffer.append("\n");
}
warningBuffer.append("Transformation dictionary uses excluded column " + s);
}
if (warningBuffer.length() > 0) {
setWarningMessage(warningBuffer.toString().trim());
}
PMMLPortObject outport = null;
try {
outport = new PMMLPortObject(createPMMLSpec(pmmlIn.getSpec(), outSpec, res), pmmldoc);
} catch (IllegalArgumentException e) {
if (res.getIncludes().length == 0) {
throw new IllegalArgumentException("Excluding all columns produces invalid PMML", e);
} else {
throw e;
}
}
return outport;
}
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class BinnerNodeModel method createPMMLModel.
/**
* Creates the pmml port object.
* @param the in-port pmml object. Can be <code>null</code> (optional in-port)
*/
private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec, final DataTableSpec outSpec) {
PMMLBinningTranslator trans = new PMMLBinningTranslator(m_columnToBins, m_columnToAppended, new DerivedFieldMapper(inPMMLPort));
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(inPMMLPort, outSpec);
PMMLPortObject outPMMLPort = new PMMLPortObject(pmmlSpecCreator.createSpec(), inPMMLPort, inSpec);
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
return outPMMLPort;
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class BinnerNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
ColumnRearranger colre = createColumnRearranger((DataTableSpec) inSpecs[0]);
colre.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
if (m_pmmlOutEnabled) {
// handle the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) ((PortObjectInput) inputs[1]).getPortObject() : null;
PMMLPortObject outPMMLPort = createPMMLModel(inPMMLPort, (DataTableSpec) inSpecs[0], colre.createSpec());
((PortObjectOutput) outputs[1]).setPortObject(outPMMLPort);
}
}
};
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class MissingValueApplyNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[DATA_PORT_IDX];
DataTableSpec inSpec = inTable.getDataTableSpec();
PMMLPortObject pmmlIn = (PMMLPortObject) inData[PMML_PORT_IDX];
MissingCellReplacingDataTable mvTable;
try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
mvTable = new MissingCellReplacingDataTable(inSpec, PMMLDocument.Factory.parse(supplier.get()));
}
// Calculate the statistics
mvTable.init(inTable, exec.createSubExecutionContext(0.5));
long rowCounter = 0;
final long numOfRows = inTable.size();
DataContainer container = exec.createDataContainer(mvTable.getDataTableSpec());
for (DataRow row : mvTable) {
exec.checkCanceled();
if (row != null) {
exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows + " (\"" + row.getKey() + "\")");
container.addRowToTable(row);
} else {
exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows);
}
}
container.close();
// Collect warning messages
String warnings = mvTable.finish();
// Handle the warnings
if (warnings.length() > 0) {
setWarningMessage(warnings);
}
return new PortObject[] { (BufferedDataTable) container.getTable() };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class Many2OneCol2PMMLNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws CanceledExecutionException, Exception {
BufferedDataTable inData = (BufferedDataTable) inObjects[0];
AbstractMany2OneCellFactory cellFactory = getCellFactory(inData.getDataTableSpec());
BufferedDataTable outData = exec.createColumnRearrangeTable(inData, createRearranger(inData.getDataTableSpec(), cellFactory), exec);
if (m_pmmlOutEnabled) {
if (IncludeMethod.valueOf(m_includeMethod.getStringValue()) == IncludeMethod.RegExpPattern) {
setWarningMessage("Regular Expressions are not supported in PMML. " + "The generated PMML document is invalid.");
}
// the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inObjects[1] : null;
/*
PMMLOne2ManyTranslator trans = new PMMLOne2ManyTranslator(
cellFactory.getColumnMapping(),
new DerivedFieldMapper(inPMMLPort));
*/
int[] sourceColIndices = cellFactory.getIncludedColIndices();
String[] sourceColNames = new String[sourceColIndices.length];
for (int i = 0; i < sourceColIndices.length; i++) {
sourceColNames[i] = inData.getDataTableSpec().getColumnSpec(sourceColIndices[i]).getName();
}
PMMLMany2OneTranslator trans = new PMMLMany2OneTranslator(cellFactory.getAppendedColumnName(), sourceColNames, IncludeMethod.valueOf(m_includeMethod.getStringValue()));
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, outData.getDataTableSpec());
PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
return new PortObject[] { outData, outPMMLPort };
} else {
return new PortObject[] { outData };
}
}
Aggregations