use of org.knime.core.util.binning.numeric.NumericBin in project knime-core by knime.
the class AutoBinnerUtility method translate.
/**
* This method translates a {@link PMMLPreprocDiscretize} object into {@link PMMLPortObject}.
*
* @param pmmlDiscretize {@link PMMLPreprocDiscretize} object
* @param dataTableSpec {@link DataTableSpec} if incoming {@link BufferedDataTable}
* @return a {@link PMMLPortObject} containing required parameters for binning operation
*/
public static PMMLPortObject translate(final PMMLPreprocDiscretize pmmlDiscretize, final DataTableSpec dataTableSpec) {
final Map<String, Bin[]> columnToBins = new HashMap<>();
final Map<String, String> columnToAppend = new HashMap<>();
final List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
for (String replacedColumnName : replacedColumnNames) {
final PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
final List<PMMLDiscretizeBin> bins = discretize.getBins();
final String originalColumnName = discretize.getField();
final boolean replaceColumnTheSame = replacedColumnName.equals(originalColumnName);
columnToAppend.put(originalColumnName, replaceColumnTheSame ? null : replacedColumnName);
final NumericBin[] numericBin = bins.stream().map(AutoBinnerUtility::getNumericBin).toArray(NumericBin[]::new);
columnToBins.put(originalColumnName, numericBin);
}
final DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
final PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
final PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
final PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
final TransformationDictionary exportToTransDict = trans.exportToTransDict();
pmmlPortObject.addGlobalTransformations(exportToTransDict);
return pmmlPortObject;
}
use of org.knime.core.util.binning.numeric.NumericBin in project knime-core by knime.
the class AutoBinnerUtility method getNumericBin.
private static NumericBin getNumericBin(final PMMLDiscretizeBin bin) {
final String binName = bin.getBinValue();
final List<PMMLInterval> intervals = bin.getIntervals();
boolean leftOpen = false;
boolean rightOpen = false;
double leftMargin = 0;
double rightMargin = 0;
// always returns only one interval
for (PMMLInterval interval : intervals) {
final Closure closure = interval.getClosure();
switch(closure) {
case OPEN_CLOSED:
leftOpen = true;
rightOpen = false;
break;
case OPEN_OPEN:
leftOpen = true;
rightOpen = true;
break;
case CLOSED_OPEN:
leftOpen = false;
rightOpen = true;
break;
case CLOSED_CLOSED:
leftOpen = false;
rightOpen = false;
break;
default:
leftOpen = true;
rightOpen = false;
break;
}
leftMargin = interval.getLeftMargin();
rightMargin = interval.getRightMargin();
}
return new NumericBin(binName, leftOpen, leftMargin, rightOpen, rightMargin);
}
Aggregations