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);
}
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);
}
}
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;
}
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);
}
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));
}
}
}
}
Aggregations