Search in sources :

Example 1 with TrackedParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter in project elki by elki-project.

the class SettingsVisualization method makeVisualization.

@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
    SettingsResult sr = task.getResult();
    Collection<TrackedParameter> settings = sr.getSettings();
    Element layer = plot.svgElement(SVGConstants.SVG_G_TAG);
    // FIXME: use CSSClass and StyleLibrary
    int i = 0;
    Object last = null;
    for (TrackedParameter setting : settings) {
        if (setting.getOwner() != last && setting.getOwner() != null) {
            String name;
            try {
                if (setting.getOwner() instanceof Class) {
                    name = ((Class<?>) setting.getOwner()).getName();
                } else {
                    name = setting.getOwner().getClass().getName();
                }
                if (ClassParameter.class.isInstance(setting.getOwner())) {
                    name = ((ClassParameter<?>) setting.getOwner()).getValue().getName();
                }
            } catch (NullPointerException e) {
                name = "[null]";
            }
            Element object = plot.svgText(0, i + 0.7, name);
            object.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6; font-weight: bold");
            layer.appendChild(object);
            i++;
            last = setting.getOwner();
        }
        // get name and value
        String name = setting.getParameter().getOptionID().getName();
        String value = "[unset]";
        try {
            if (setting.getParameter().isDefined()) {
                value = setting.getParameter().getValueAsString();
            }
        } catch (NullPointerException e) {
            value = "[null]";
        }
        Element label = plot.svgText(0, i + 0.7, name);
        label.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6");
        layer.appendChild(label);
        Element vale = plot.svgText(7.5, i + 0.7, value);
        vale.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6");
        layer.appendChild(vale);
        // only advance once, since we want these two to be in the same line.
        i++;
    }
    int cols = Math.max(30, (int) (i * height / width));
    int rows = i;
    final double margin = context.getStyleLibrary().getSize(StyleLibrary.MARGIN);
    final String transform = SVGUtil.makeMarginTransform(width, height, cols, rows, margin / StyleLibrary.SCALE);
    SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
    return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
Also used : Element(org.w3c.dom.Element) SettingsResult(de.lmu.ifi.dbs.elki.result.SettingsResult) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) StaticVisualizationInstance(de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)

Example 2 with TrackedParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter in project elki by elki-project.

the class OptionUtil method describeParameterizable.

/**
 * Format a description of a Parameterizable (including recursive options).
 *
 * @param buf Buffer to append to.
 * @param pcls Parameterizable class to describe
 * @param width Width
 * @param indent Text indent
 * @return Formatted description
 */
public static StringBuilder describeParameterizable(StringBuilder buf, Class<?> pcls, int width, String indent) {
    try {
        println(buf, width, "Description for class " + pcls.getName(), "");
        String title = DocumentationUtil.getTitle(pcls);
        if (title != null && title.length() > 0) {
            println(buf, width, title, "");
        }
        String desc = DocumentationUtil.getDescription(pcls);
        if (desc != null && desc.length() > 0) {
            println(buf, width, desc, "  ");
        }
        Reference ref = DocumentationUtil.getReference(pcls);
        if (ref != null) {
            if (ref.prefix().length() > 0) {
                println(buf, width, ref.prefix(), "");
            }
            println(buf, width, ref.authors() + ":", "");
            println(buf, width, ref.title(), "  ");
            println(buf, width, "in: " + ref.booktitle(), "");
            if (ref.url().length() > 0) {
                println(buf, width, "see also: " + ref.url(), "");
            }
        }
        SerializedParameterization config = new SerializedParameterization();
        TrackParameters track = new TrackParameters(config);
        @SuppressWarnings("unused") Object p = ClassGenericsUtil.tryInstantiate(Object.class, pcls, track);
        Collection<TrackedParameter> options = track.getAllParameters();
        if (!options.isEmpty()) {
            OptionUtil.formatForConsole(buf, width, indent, options);
        }
        return buf;
    } catch (Exception e) {
        LoggingUtil.exception("Error instantiating class to describe.", e.getCause());
        return buf.append("No description available: ").append(e);
    }
}
Also used : Reference(de.lmu.ifi.dbs.elki.utilities.documentation.Reference) SerializedParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)

Example 3 with TrackedParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter in project elki by elki-project.

the class VisualizerParameterizer method getTitle.

/**
 * Try to automatically generate a title for this.
 *
 * @param db Database
 * @param result Result object
 * @return generated title
 */
public static String getTitle(Database db, Result result) {
    List<TrackedParameter> settings = new ArrayList<>();
    for (SettingsResult sr : SettingsResult.getSettingsResults(result)) {
        settings.addAll(sr.getSettings());
    }
    String algorithm = null;
    String distance = null;
    String dataset = null;
    for (TrackedParameter setting : settings) {
        Parameter<?> param = setting.getParameter();
        OptionID option = param.getOptionID();
        String value = param.isDefined() ? param.getValueAsString() : null;
        if (option.equals(AlgorithmStep.Parameterizer.ALGORITHM_ID)) {
            algorithm = value;
        }
        if (option.equals(DistanceBasedAlgorithm.DISTANCE_FUNCTION_ID)) {
            distance = value;
        }
        if (option.equals(FileBasedDatabaseConnection.Parameterizer.INPUT_ID)) {
            dataset = value;
        }
    }
    StringBuilder buf = new StringBuilder();
    if (algorithm != null) {
        buf.append(shortenClassname(algorithm.split(",")[0], '.'));
    }
    if (distance != null) {
        if (buf.length() > 0) {
            buf.append(" using ");
        }
        buf.append(shortenClassname(distance, '.'));
    }
    if (dataset != null) {
        if (buf.length() > 0) {
            buf.append(" on ");
        }
        buf.append(shortenClassname(dataset, File.separatorChar));
    }
    if (buf.length() > 0) {
        return buf.toString();
    }
    return null;
}
Also used : OptionID(de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID) ArrayList(java.util.ArrayList) SettingsResult(de.lmu.ifi.dbs.elki.result.SettingsResult) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)

Example 4 with TrackedParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter in project elki by elki-project.

the class TextWriter method writeSettingsResult.

private void writeSettingsResult(StreamFactory streamOpener, List<SettingsResult> rs) throws IOException {
    if (rs.isEmpty()) {
        return;
    }
    SettingsResult r = rs.get(0);
    PrintStream outStream = streamOpener.openStream(getFilename(r, r.getShortName()));
    TextWriterStream out = new TextWriterStream(outStream, writers, fallback);
    // Write settings preamble
    out.commentPrintLn("Settings:");
    if (rs != null) {
        for (SettingsResult settings : rs) {
            Object last = null;
            for (TrackedParameter setting : settings.getSettings()) {
                if (setting.getOwner() != last && setting.getOwner() != null) {
                    if (last != null) {
                        out.commentPrintLn("");
                    }
                    String name;
                    try {
                        if (setting.getOwner() instanceof Class) {
                            name = ((Class<?>) setting.getOwner()).getName();
                        } else {
                            name = setting.getOwner().getClass().getName();
                        }
                        if (ClassParameter.class.isInstance(setting.getOwner())) {
                            name = ((ClassParameter<?>) setting.getOwner()).getValue().getName();
                        }
                    } catch (NullPointerException e) {
                        name = "[null]";
                    }
                    out.commentPrintLn(name);
                    last = setting.getOwner();
                }
                String name = setting.getParameter().getOptionID().getName();
                String value = "[unset]";
                try {
                    if (setting.getParameter().isDefined()) {
                        value = setting.getParameter().getValueAsString();
                    }
                } catch (NullPointerException e) {
                    value = "[null]";
                }
                out.commentPrintLn(SerializedParameterization.OPTION_PREFIX + name + " " + value);
            }
        }
    }
    out.flush();
    streamOpener.closeStream(outStream);
}
Also used : PrintStream(java.io.PrintStream) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)

Example 5 with TrackedParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter in project elki by elki-project.

the class DocumentParameters method buildParameterIndex.

private static void buildParameterIndex(Map<Class<?>, List<Parameter<?>>> byclass, Map<OptionID, List<Pair<Parameter<?>, Class<?>>>> byopt) {
    final ArrayList<TrackedParameter> options = new ArrayList<>();
    ExecutorService es = Executors.newSingleThreadExecutor();
    List<Class<?>> objs = ELKIServiceRegistry.findAllImplementations(Object.class, false, true);
    Collections.sort(objs, new ELKIServiceScanner.ClassSorter());
    Class<?> appc = appBaseClass();
    for (final Class<?> cls : objs) {
        // Doesn't have a proper name?
        if (cls.getCanonicalName() == null) {
            continue;
        }
        // constructors / parameterizers and may start AWT threads - skip them.
        if (appc != null && appc.isAssignableFrom(cls)) {
            continue;
        }
        UnParameterization config = new UnParameterization();
        TrackParameters track = new TrackParameters(config, cls);
        try {
            // Wait up to one second.
            es.submit(new // 
            FutureTask<Object>(new Instancer(cls, track, options), // 
            null)).get(1L, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            LOG.warning("Timeout on instantiating " + cls.getName());
            es.shutdownNow();
            throw new RuntimeException(e);
        } catch (Exception e) {
            LOG.warning("Error instantiating " + cls.getName(), e.getCause());
            continue;
        }
    }
    LOG.debug("Documenting " + options.size() + " parameter instances.");
    for (TrackedParameter pp : options) {
        if (pp.getOwner() == null || pp.getParameter() == null) {
            LOG.debugFiner("Null: " + pp.getOwner() + " " + pp.getParameter());
            continue;
        }
        Class<?> c = (Class<?>) ((pp.getOwner() instanceof Class) ? pp.getOwner() : pp.getOwner().getClass());
        Parameter<?> o = pp.getParameter();
        // just collect unique occurrences
        {
            List<Parameter<?>> byc = byclass.get(c);
            boolean inlist = false;
            if (byc != null) {
                for (Parameter<?> par : byc) {
                    if (par.getOptionID() == o.getOptionID()) {
                        inlist = true;
                        break;
                    }
                }
            }
            if (!inlist) {
                List<Parameter<?>> ex = byclass.get(c);
                if (ex == null) {
                    byclass.put(c, ex = new ArrayList<>());
                }
                ex.add(o);
            }
        }
        {
            List<Pair<Parameter<?>, Class<?>>> byo = byopt.get(o.getOptionID());
            boolean inlist = false;
            if (byo != null) {
                for (Pair<Parameter<?>, Class<?>> pair : byo) {
                    if (pair.second.equals(c)) {
                        inlist = true;
                        break;
                    }
                }
            }
            if (!inlist) {
                List<Pair<Parameter<?>, Class<?>>> ex = byopt.get(o.getOptionID());
                if (ex == null) {
                    byopt.put(o.getOptionID(), ex = new ArrayList<>());
                }
                ex.add(new Pair<Parameter<?>, Class<?>>(o, c));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.UnParameterization) FutureTask(java.util.concurrent.FutureTask) ELKIServiceScanner(de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner) ExecutorService(java.util.concurrent.ExecutorService) RandomParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) Parameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter) ClassListParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) ArrayList(java.util.ArrayList) List(java.util.List) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) TimeoutException(java.util.concurrent.TimeoutException) Pair(de.lmu.ifi.dbs.elki.utilities.pairs.Pair)

Aggregations

TrackedParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)7 TrackParameters (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)3 ClassParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)3 SettingsResult (de.lmu.ifi.dbs.elki.result.SettingsResult)2 ArrayList (java.util.ArrayList)2 ELKIServiceScanner (de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner)1 Reference (de.lmu.ifi.dbs.elki.utilities.documentation.Reference)1 OptionID (de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID)1 ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)1 SerializedParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization)1 UnParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.UnParameterization)1 ClassListParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter)1 Flag (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag)1 Parameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter)1 RandomParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter)1 Pair (de.lmu.ifi.dbs.elki.utilities.pairs.Pair)1 StaticVisualizationInstance (de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 List (java.util.List)1