Search in sources :

Example 1 with SettingsModelDouble

use of org.knime.core.node.defaultnodesettings.SettingsModelDouble in project knime-core by knime.

the class NaiveBayesPredictorNodeDialog method addOtherControls.

/**
 * {@inheritDoc}
 */
@Override
protected void addOtherControls(final JPanel panel) {
    super.addOtherControls(panel);
    final SettingsModelDouble laplaceCorrectorModel = new SettingsModelDoubleBounded(NaiveBayesPredictorNodeModel.CFG_LAPLACE_CORRECTOR_KEY, 0.0, 0.0, Double.MAX_VALUE);
    final DialogComponentNumber laplaceCorrectorComponent = new DialogComponentNumber(laplaceCorrectorModel, "Laplace corrector: ", new Double(0.1), 5);
    laplaceCorrectorComponent.setToolTipText("Set to zero for no correction");
    addDialogComponent(panel, laplaceCorrectorComponent);
}
Also used : SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) DialogComponentNumber(org.knime.core.node.defaultnodesettings.DialogComponentNumber) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelDoubleBounded(org.knime.core.node.defaultnodesettings.SettingsModelDoubleBounded)

Example 2 with SettingsModelDouble

use of org.knime.core.node.defaultnodesettings.SettingsModelDouble in project knime-core by knime.

the class SVMLearnerNodeModel method loadValidatedSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_paramC.loadSettingsFrom(settings);
    m_classcol.loadSettingsFrom(settings);
    if (settings.containsKey(CFG_KERNELTYPE)) {
        m_kernelType = KernelType.valueOf(settings.getString(CFG_KERNELTYPE));
    }
    for (Map.Entry<KernelType, Vector<SettingsModelDouble>> entry : m_kernelParameters.entrySet()) {
        Vector<SettingsModelDouble> kernelsettings = entry.getValue();
        for (SettingsModelDouble smd : kernelsettings) {
            try {
                smd.loadSettingsFrom(settings);
            } catch (InvalidSettingsException ise) {
                // it's not bad if a parameter is missing. This may be
                // an old version, but inform the user.
                LOGGER.warn("Did not find " + smd.toString() + " in the" + " NodeSettings. Using default value instead.");
            }
        }
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KernelType(org.knime.base.node.mine.svm.kernel.KernelFactory.KernelType) Vector(java.util.Vector) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector)

Example 3 with SettingsModelDouble

use of org.knime.core.node.defaultnodesettings.SettingsModelDouble in project knime-core by knime.

the class SVMLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    LearnColumnsAndColumnRearrangerTuple tuple = createTrainTableColumnRearranger(inSpec);
    // no progress needed as constant operation (column removal only)
    BufferedDataTable trainTable = exec.createColumnRearrangeTable(inTable, tuple.getTrainingRearranger(), exec.createSubProgress(0.0));
    DataTableSpec trainSpec = trainTable.getDataTableSpec();
    int classpos = trainSpec.findColumnIndex(m_classcol.getStringValue());
    CheckUtils.checkArgument(classpos >= 0, "Selected class column not found: " + m_classcol.getStringValue());
    // convert input data
    ArrayList<DoubleVector> inputData = new ArrayList<DoubleVector>();
    List<String> categories = new ArrayList<String>();
    StringValue classvalue = null;
    for (DataRow row : trainTable) {
        exec.checkCanceled();
        ArrayList<Double> values = new ArrayList<Double>();
        boolean add = true;
        for (int i = 0; i < row.getNumCells(); i++) {
            if (row.getCell(i).isMissing()) {
                add = false;
                break;
            }
            if (i != classpos) {
                DoubleValue cell = (DoubleValue) row.getCell(i);
                values.add(cell.getDoubleValue());
            } else {
                classvalue = (StringValue) row.getCell(classpos);
                if (!categories.contains(classvalue.getStringValue())) {
                    categories.add(classvalue.getStringValue());
                }
            }
        }
        if (add) {
            @SuppressWarnings("null") final String nonNullClassValue = classvalue.getStringValue();
            inputData.add(new DoubleVector(row.getKey(), values, nonNullClassValue));
        }
    }
    if (categories.isEmpty()) {
        throw new Exception("No categories found to train SVM. " + "Possibly an empty input table was provided.");
    }
    DoubleVector[] inputDataArr = new DoubleVector[inputData.size()];
    inputDataArr = inputData.toArray(inputDataArr);
    Kernel kernel = KernelFactory.getKernel(m_kernelType);
    Vector<SettingsModelDouble> kernelparams = m_kernelParameters.get(m_kernelType);
    for (int i = 0; i < kernel.getNumberParameters(); ++i) {
        kernel.setParameter(i, kernelparams.get(i).getDoubleValue());
    }
    final Svm[] svms = new Svm[categories.size()];
    exec.setMessage("Training SVM");
    final BinarySvmRunnable[] bst = new BinarySvmRunnable[categories.size()];
    for (int i = 0; i < categories.size(); i++) {
        bst[i] = new BinarySvmRunnable(inputDataArr, categories.get(i), kernel, m_paramC.getDoubleValue(), exec.createSubProgress((1.0 / categories.size())));
    }
    ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
    final Future<?>[] fut = new Future<?>[bst.length];
    KNIMETimer timer = KNIMETimer.getInstance();
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            try {
                exec.checkCanceled();
            } catch (final CanceledExecutionException ce) {
                for (int i = 0; i < fut.length; i++) {
                    if (fut[i] != null) {
                        fut[i].cancel(true);
                    }
                }
                super.cancel();
            }
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 3000);
    for (int i = 0; i < bst.length; i++) {
        fut[i] = pool.enqueue(bst[i]);
    }
    try {
        pool.runInvisible(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                for (int i = 0; i < fut.length; ++i) {
                    fut[i].get();
                    bst[i].ok();
                    if (bst[i].getWarning() != null) {
                        setWarningMessage(bst[i].getWarning());
                    }
                    svms[i] = bst[i].getSvm();
                }
                return null;
            }
        });
    } catch (Exception ex) {
        exec.checkCanceled();
        Throwable t = ex;
        if (ex instanceof ExecutionException) {
            t = ex.getCause();
        }
        if (t instanceof Exception) {
            throw (Exception) t;
        } else {
            throw new Exception(t);
        }
    } finally {
        for (int i = 0; i < fut.length; i++) {
            fut[i].cancel(true);
        }
        timerTask.cancel();
    }
    // the optional PMML input (can be null)
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    // create the outgoing PMML spec
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(inPMMLPort, inSpec);
    specCreator.setLearningCols(trainSpec);
    specCreator.setTargetCol(trainSpec.getColumnSpec(m_classcol.getStringValue()));
    // create the outgoing PMML port object
    PMMLPortObject outPMMLPort = new PMMLPortObject(specCreator.createSpec(), inPMMLPort, inSpec);
    outPMMLPort.addModelTranslater(new PMMLSVMTranslator(categories, Arrays.asList(svms), kernel));
    m_svms = svms;
    return new PortObject[] { outPMMLPort };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLSVMTranslator(org.knime.base.node.mine.svm.PMMLSVMTranslator) ArrayList(java.util.ArrayList) ThreadPool(org.knime.core.util.ThreadPool) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Svm(org.knime.base.node.mine.svm.Svm) DataRow(org.knime.core.data.DataRow) TimerTask(java.util.TimerTask) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) StringValue(org.knime.core.data.StringValue) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Kernel(org.knime.base.node.mine.svm.kernel.Kernel) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) KNIMETimer(org.knime.core.util.KNIMETimer) BinarySvmRunnable(org.knime.base.node.mine.svm.util.BinarySvmRunnable) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Future(java.util.concurrent.Future) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 4 with SettingsModelDouble

use of org.knime.core.node.defaultnodesettings.SettingsModelDouble in project knime-core by knime.

the class SVMLearnerNodeModel method createKernelParams.

/**
 * creates the kernel parameter SettingsModels.
 *
 * @return HashMap containing the kernel and its assigned SettingsModels.
 */
static LinkedHashMap<KernelType, Vector<SettingsModelDouble>> createKernelParams() {
    LinkedHashMap<KernelType, Vector<SettingsModelDouble>> kernelParameters = new LinkedHashMap<KernelType, Vector<SettingsModelDouble>>();
    for (KernelType kerneltype : KernelType.values()) {
        Kernel kernel = KernelFactory.getKernel(kerneltype);
        Vector<SettingsModelDouble> settings = new Vector<SettingsModelDouble>();
        for (int i = 0; i < kernel.getNumberParameters(); i++) {
            settings.add(new SettingsModelDouble(CFG_KERNELPARAM + "_" + kernel.getParameterName(i), kernel.getDefaultParameter(i)));
        }
        kernelParameters.put(kerneltype, settings);
    }
    return kernelParameters;
}
Also used : SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) KernelType(org.knime.base.node.mine.svm.kernel.KernelFactory.KernelType) Vector(java.util.Vector) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector) Kernel(org.knime.base.node.mine.svm.kernel.Kernel) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SettingsModelDouble

use of org.knime.core.node.defaultnodesettings.SettingsModelDouble in project knime-core by knime.

the class NumericRowSplitterNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_columnSelection.validateSettings(settings);
    m_lowerBound.validateSettings(settings);
    m_lowerBoundCheck.validateSettings(settings);
    m_lowerBoundValue.validateSettings(settings);
    m_upperBound.validateSettings(settings);
    m_upperBoundCheck.validateSettings(settings);
    m_upperBoundValue.validateSettings(settings);
    SettingsModelBoolean lowerBoundCheck = m_lowerBoundCheck.createCloneWithValidatedValue(settings);
    SettingsModelBoolean upperBoundCheck = m_upperBoundCheck.createCloneWithValidatedValue(settings);
    if (lowerBoundCheck.getBooleanValue() && upperBoundCheck.getBooleanValue()) {
        SettingsModelDouble lowerBoundValue = m_lowerBoundValue.createCloneWithValidatedValue(settings);
        double low = lowerBoundValue.getDoubleValue();
        SettingsModelDouble upperBoundValue = m_upperBoundValue.createCloneWithValidatedValue(settings);
        double upp = upperBoundValue.getDoubleValue();
        if (low > upp) {
            throw new InvalidSettingsException("Check lower and upper " + "bound values: " + low + " > " + upp);
        }
        if (low == upp) {
            SettingsModelString lowerBound = m_lowerBound.createCloneWithValidatedValue(settings);
            boolean inclLow = NumericRowSplitterNodeDialogPane.includeLowerBound(lowerBound);
            SettingsModelString upperBound = m_upperBound.createCloneWithValidatedValue(settings);
            boolean inclUpp = NumericRowSplitterNodeDialogPane.includeUpperBound(upperBound);
            if ((inclLow ^ inclUpp) || (!inclLow & !inclUpp)) {
                throw new InvalidSettingsException("Lower and upper bounds " + "are inconsistent with borders!");
            }
        }
    }
}
Also used : SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Aggregations

SettingsModelDouble (org.knime.core.node.defaultnodesettings.SettingsModelDouble)9 DoubleVector (org.knime.base.node.mine.svm.util.DoubleVector)5 LinkedHashMap (java.util.LinkedHashMap)4 Vector (java.util.Vector)4 KernelType (org.knime.base.node.mine.svm.kernel.KernelFactory.KernelType)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 Kernel (org.knime.base.node.mine.svm.kernel.Kernel)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TimerTask (java.util.TimerTask)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ChangeListener (javax.swing.event.ChangeListener)1 PMMLSVMTranslator (org.knime.base.node.mine.svm.PMMLSVMTranslator)1 Svm (org.knime.base.node.mine.svm.Svm)1 BinarySvmRunnable (org.knime.base.node.mine.svm.util.BinarySvmRunnable)1 DataRow (org.knime.core.data.DataRow)1