use of org.knime.core.node.port.pmml.preproc.DerivedFieldMapper in project knime-core by knime.
the class PMMLDataDictionaryTranslator method addOrUpdateColSpecsForDerivedFields.
/**
* @param pmmlDoc the PMML document to analyze
* @param colSpecs the list to add the data column specs to
*/
private void addOrUpdateColSpecsForDerivedFields(final PMMLDocument pmmlDoc, final List<DataColumnSpec> colSpecs) {
DerivedField[] derivedFields = DerivedFieldMapper.getDerivedFields(pmmlDoc.getPMML());
DerivedFieldMapper mapper = new DerivedFieldMapper(derivedFields);
Set<String> mappedColumnNames = mapper.getDerivedNames();
for (DerivedField df : derivedFields) {
String name = df.getName();
DataType dataType = getKNIMEDataType(df.getDataType());
if (!mappedColumnNames.contains(name)) {
/* It is a "real" column - not one that is mapping to another
* column. Hence add a data column spec. */
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(name, dataType);
DataColumnSpec newSpec = specCreator.createSpec();
colSpecs.add(newSpec);
m_activeDerivedFields.add(name);
} else {
/* Update the data type of the referenced data column spec
* by replacing it with a spec of the new type. */
String colName = mapper.getColumnName(name);
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(colName, dataType);
DataColumnSpec newSpec = specCreator.createSpec();
for (int i = 0; i < colSpecs.size(); i++) {
if (colSpecs.get(i).getName().equals(colName)) {
colSpecs.remove(i);
colSpecs.add(i, newSpec);
break;
}
}
m_activeDerivedFields.add(colName);
}
}
}
use of org.knime.core.node.port.pmml.preproc.DerivedFieldMapper 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.preproc.DerivedFieldMapper in project knime-core by knime.
the class PMMLBinningTranslator method initializeFrom.
/**
* {@inheritDoc}
*/
@Override
public List<Integer> initializeFrom(final DerivedField[] derivedFields) {
m_mapper = new DerivedFieldMapper(derivedFields);
List<Integer> consumed = new ArrayList(derivedFields.length);
for (int i = 0; i < derivedFields.length; i++) {
DerivedField df = derivedFields[i];
if (!df.isSetDiscretize()) {
// only reading discretize entries other entries are skipped
continue;
}
consumed.add(i);
Discretize discretize = df.getDiscretize();
DiscretizeBin[] pmmlBins = discretize.getDiscretizeBinArray();
NumericBin[] knimeBins = new NumericBin[pmmlBins.length];
for (int j = 0; j < pmmlBins.length; j++) {
DiscretizeBin bin = pmmlBins[j];
String binName = bin.getBinValue();
Interval interval = bin.getInterval();
double leftValue = interval.getLeftMargin();
double rightValue = interval.getRightMargin();
Closure.Enum closure = interval.getClosure();
boolean leftOpen = true;
boolean rightOpen = true;
if (Closure.OPEN_CLOSED == closure) {
rightOpen = false;
} else if (Closure.CLOSED_OPEN == closure) {
leftOpen = false;
} else if (Closure.CLOSED_CLOSED == closure) {
leftOpen = false;
rightOpen = false;
}
knimeBins[j] = new NumericBin(binName, leftOpen, leftValue, rightOpen, rightValue);
}
/**
* This field contains the name of the column in KNIME that
* corresponds to the derived field in PMML. This is necessary if
* derived fields are defined on other derived fields and the
* columns in KNIME are replaced with the preprocessed values.
* In this case KNIME has to know the original names (e.g. A) while
* PMML references to A', A'' etc.
*/
String displayName = df.getDisplayName();
if (displayName != null) {
m_columnToBins.put(displayName, knimeBins);
m_columnToAppend.put(displayName, null);
} else if (df.getName() != null) {
String field = m_mapper.getColumnName(discretize.getField());
m_columnToBins.put(field, knimeBins);
m_columnToAppend.put(field, df.getName());
}
}
return consumed;
}
use of org.knime.core.node.port.pmml.preproc.DerivedFieldMapper in project knime-core by knime.
the class AbstractNormalizerPMMLNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
CalculationResult result = calculate(inObjects, exec);
BufferedDataTable outTable = result.getDataTable();
// the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = m_hasModelIn ? (PMMLPortObject) inObjects[1] : null;
PMMLNormalizeTranslator trans = new PMMLNormalizeTranslator(result.getConfig(), new DerivedFieldMapper(inPMMLPort));
DataTableSpec dataTableSpec = (DataTableSpec) inObjects[0].getSpec();
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, dataTableSpec);
PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
return new PortObject[] { outTable, outPMMLPort };
}
use of org.knime.core.node.port.pmml.preproc.DerivedFieldMapper in project knime-core by knime.
the class AbstractGBTModelPMMLTranslator method exportTo.
/**
* {@inheritDoc}
*/
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
PMML pmml = pmmlDoc.getPMML();
AbstractGBTModelExporter<M> exporter = createExporter(new DerivedFieldMapper(pmmlDoc));
SchemaType st = exporter.writeModelToPMML(pmml.addNewMiningModel(), spec);
if (exporter.hasWarning()) {
addWarning(exporter.getWarning());
}
return st;
}
Aggregations