use of org.dmg.pmml.DiscretizeDocument.Discretize 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.dmg.pmml.DiscretizeDocument.Discretize 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.dmg.pmml.DiscretizeDocument.Discretize in project knime-core by knime.
the class PMMLBinningTranslator method createDerivedFields.
private DerivedField[] createDerivedFields() {
int num = m_columnToBins.size();
DerivedField[] derivedFields = new DerivedField[num];
int i = 0;
for (Map.Entry<String, Bin[]> entry : m_columnToBins.entrySet()) {
Bin[] bins = entry.getValue();
DerivedField df = DerivedField.Factory.newInstance();
String name = entry.getKey();
/* The field name must be retrieved before creating a new derived
* name for this derived field as the map only contains the
* current mapping. */
String fieldName = m_mapper.getDerivedFieldName(name);
Discretize dis = df.addNewDiscretize();
dis.setField(fieldName);
String derivedName = m_columnToAppend.get(name);
if (derivedName != null) {
df.setName(derivedName);
} else {
df.setName(m_mapper.createDerivedFieldName(name));
df.setDisplayName(name);
}
df.setOptype(OPTYPE.CATEGORICAL);
df.setDataType(DATATYPE.STRING);
for (int j = 0; j < bins.length; j++) {
NumericBin knimeBin = (NumericBin) bins[j];
boolean leftOpen = knimeBin.isLeftOpen();
boolean rightOpen = knimeBin.isRightOpen();
double leftValue = knimeBin.getLeftValue();
double rightValue = knimeBin.getRightValue();
DiscretizeBin pmmlBin = dis.addNewDiscretizeBin();
pmmlBin.setBinValue(knimeBin.getBinName());
Interval interval = pmmlBin.addNewInterval();
if (!Double.isInfinite(leftValue)) {
interval.setLeftMargin(leftValue);
}
if (!Double.isInfinite(rightValue)) {
interval.setRightMargin(rightValue);
}
if (leftOpen && rightOpen) {
interval.setClosure(Closure.OPEN_OPEN);
} else if (leftOpen && !rightOpen) {
interval.setClosure(Closure.OPEN_CLOSED);
} else if (!leftOpen && rightOpen) {
interval.setClosure(Closure.CLOSED_OPEN);
} else if (!leftOpen && !rightOpen) {
interval.setClosure(Closure.CLOSED_CLOSED);
}
}
derivedFields[i++] = df;
}
return derivedFields;
}
Aggregations