Search in sources :

Example 1 with NumericBin

use of org.knime.base.node.preproc.pmml.binner.NumericBin in project knime-core by knime.

the class DBAutoBinner 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<>();
    List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
    for (String replacedColumnName : replacedColumnNames) {
        PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
        List<PMMLDiscretizeBin> bins = discretize.getBins();
        String originalColumnName = discretize.getField();
        if (replacedColumnName.equals(originalColumnName)) {
            // wenn replaced, dann nicht anhängen
            columnToAppend.put(originalColumnName, null);
        } else {
            // nicht replaced -> anhängen
            columnToAppend.put(originalColumnName, replacedColumnName);
        }
        NumericBin[] numericBin = new NumericBin[bins.size()];
        int counter = 0;
        for (PMMLDiscretizeBin bin : bins) {
            String binName = bin.getBinValue();
            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) {
                Closure closure = interval.getClosure();
                switch(closure) {
                    case openClosed:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                    case openOpen:
                        leftOpen = true;
                        rightOpen = true;
                        break;
                    case closedOpen:
                        leftOpen = false;
                        rightOpen = true;
                    case closedClosed:
                        leftOpen = false;
                        rightOpen = false;
                        break;
                    default:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                }
                leftMargin = interval.getLeftMargin();
                rightMargin = interval.getRightMargin();
            }
            numericBin[counter] = new NumericBin(binName, leftOpen, leftMargin, rightOpen, rightMargin);
            counter++;
        }
        columnToBins.put(originalColumnName, numericBin);
    }
    // ColumnRearranger createColReg = createColReg(dataTableSpec, columnToBins, columnToAppended);
    DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
    PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
    PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
    TransformationDictionary exportToTransDict = trans.exportToTransDict();
    pmmlPortObject.addGlobalTransformations(exportToTransDict);
    return pmmlPortObject;
}
Also used : NumericBin(org.knime.base.node.preproc.pmml.binner.NumericBin) DataTableSpec(org.knime.core.data.DataTableSpec) Closure(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval.Closure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) PMMLDiscretizeBin(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretizeBin) PMMLInterval(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator) PMMLDiscretize(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretize) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 2 with NumericBin

use of org.knime.base.node.preproc.pmml.binner.NumericBin in project knime-core by knime.

the class DBNumericBinnerNodeModel method loadValidatedSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_columnToBins.clear();
    m_columnToAppended.clear();
    String[] columns = settings.getStringArray(NUMERIC_COLUMNS, new String[0]);
    for (int i = 0; i < columns.length; i++) {
        NodeSettingsRO column = settings.getNodeSettings(columns[i].toString());
        Set<String> bins = column.keySet();
        NumericBin[] binnings = new NumericBin[bins.size()];
        int s = 0;
        for (String binKey : bins) {
            NodeSettingsRO bin = column.getNodeSettings(binKey);
            binnings[s] = new NumericBin(bin);
            s++;
        }
        m_columnToBins.put(columns[i], binnings);
        String appended = settings.getString(columns[i].toString() + IS_APPENDED, null);
        m_columnToAppended.put(columns[i], appended);
    }
}
Also used : NumericBin(org.knime.base.node.preproc.pmml.binner.NumericBin) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 3 with NumericBin

use of org.knime.base.node.preproc.pmml.binner.NumericBin in project knime-core by knime.

the class DBNumericBinnerNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    StringBuffer sb = new StringBuffer();
    String[] columns = settings.getStringArray(NUMERIC_COLUMNS, new String[0]);
    if (columns == null) {
        sb.append("Numeric column array can't be 'null'\n");
    } else {
        for (int i = 0; i < columns.length; i++) {
            // appended or replaced
            settings.getString(columns[i].toString() + IS_APPENDED, null);
            double old = Double.NEGATIVE_INFINITY;
            if (columns[i] == null) {
                sb.append("Column can't be 'null': " + i + "\n");
                continue;
            }
            NodeSettingsRO set = settings.getNodeSettings(columns[i].toString());
            for (String binKey : set.keySet()) {
                NodeSettingsRO bin = set.getNodeSettings(binKey);
                NumericBin theBin = null;
                try {
                    theBin = new NumericBin(bin);
                } catch (InvalidSettingsException ise) {
                    sb.append(columns[i] + ": " + ise.getMessage() + "\n");
                    continue;
                }
                String binName = theBin.getBinName();
                double l = theBin.getLeftValue();
                if (l != old) {
                    sb.append(columns[i] + ": " + binName + " check interval: " + "left=" + l + ",oldright=" + old + "\n");
                }
                double r = theBin.getRightValue();
                boolean lOpen = theBin.isLeftOpen();
                boolean rOpen = theBin.isRightOpen();
                if (r < l) {
                    sb.append(columns[i] + ": " + binName + " check interval: " + "left=" + l + ",right=" + r + "\n");
                } else {
                    if (r == l && !(!lOpen && !rOpen)) {
                        sb.append(columns[i] + ": " + binName + " check borders: " + "left=" + l + ",right=" + r + "\n");
                    }
                }
                old = r;
            }
            if (old != Double.POSITIVE_INFINITY) {
                sb.append(columns[i] + ": check last right interval value=" + old + "\n");
            }
        }
    }
    if (sb.length() > 0) {
        throw new InvalidSettingsException(sb.toString());
    }
}
Also used : NumericBin(org.knime.base.node.preproc.pmml.binner.NumericBin) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Aggregations

NumericBin (org.knime.base.node.preproc.pmml.binner.NumericBin)3 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TransformationDictionary (org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary)1 PMMLDiscretize (org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretize)1 PMMLDiscretizeBin (org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretizeBin)1 PMMLInterval (org.knime.base.node.preproc.autobinner.pmml.PMMLInterval)1 Closure (org.knime.base.node.preproc.autobinner.pmml.PMMLInterval.Closure)1 PMMLBinningTranslator (org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)1 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)1 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)1