@@ -0,0 +1,2759 @@
+package de.lmu.ifi.dbs.elki.algorithm.clustering;%0A%0Aimport java.util.Collection;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;%0A%0Aimport de.lmu.ifi.dbs.elki.algorithm.AbstractAlgorithm;%0Aimport de.lmu.ifi.dbs.elki.algorithm.result.clustering.ClusteringResult;%0Aimport de.lmu.ifi.dbs.elki.algorithm.result.clustering.Clusters;%0Aimport de.lmu.ifi.dbs.elki.data.ClassLabel;%0Aimport de.lmu.ifi.dbs.elki.data.DatabaseObject;%0Aimport de.lmu.ifi.dbs.elki.database.AssociationID;%0Aimport de.lmu.ifi.dbs.elki.database.Database;%0Aimport de.lmu.ifi.dbs.elki.utilities.Description;%0A%0A/**%0A * Pseudo clustering using labels.%0A * %0A * This %22algorithm%22 puts elements into the same cluster when they agree in their labels.%0A * I.e. it just uses a predefined clustering, and is mostly useful for testing and evaluation%0A * (e.g. comparing the result of a real algorithm to a reference result / golden standard).%0A * %0A * @author Erich Schubert%0A *%0A * @param %3CO%3E%0A */%0Apublic class ByLabelClustering%3CO extends DatabaseObject%3E extends AbstractAlgorithm%3CO%3E implements Clustering%3CO%3E %7B%0A  /**%0A   * Holds the result of the algorithm.%0A   */%0A  private ClusteringResult%3CO%3E result;%0A%0A  /**%0A   * Return clustering result%0A   */%0A  public ClusteringResult%3CO%3E getResult() %7B%0A    return result;%0A  %7D%0A%0A  /**%0A   * Obtain a description of the algorithm%0A   */%0A  public Description getDescription() %7B%0A    return new Description(%22ByLabelClustering%22, %22Clustering by label%22,%0A        %22Cluster points by a (pre-assigned!) label. For comparing results with a reference clustering.%22, %22%22);%0A  %7D%0A%0A  /**%0A   * Run the actual clustering algorithm.%0A   * %0A   * @param database The database to process%0A   */%0A  @Override%0A  protected void runInTime(Database%3CO%3E database) throws IllegalStateException %7B%0A    HashMap%3CString, Collection%3CInteger%3E%3E labelmap = new HashMap%3CString, Collection%3CInteger%3E%3E(); %0A    %0A    for (Iterator%3CInteger%3E iter = database.iterator(); iter.hasNext();) %7B%0A      Integer id = iter.next();%0A      String label = null;%0A      %0A      // try class label first%0A      ClassLabel classlabel = database.getAssociation(AssociationID.CLASS, id);%0A      if (classlabel != null) label = classlabel.toString();%0A      %0A      // fall back to other labels%0A      if (label == null)%0A        label = database.getAssociation(AssociationID.LABEL, id);%0A      %0A      if (labelmap.containsKey(label))%0A        labelmap.get(label).add(id);%0A      else %7B%0A        Collection%3CInteger%3E n = new java.util.Vector%3CInteger%3E();%0A        n.add(id);%0A        labelmap.put(label,n);%0A      %7D%0A    %7D%0A%0A    Integer%5B%5D%5B%5D rarray = new Integer%5Blabelmap.size()%5D%5B%5D;%0A    int i = 0;%0A    for (Collection%3CInteger%3E ids : labelmap.values()) %7B%0A      rarray%5Bi%5D = (Integer%5B%5D) ids.toArray(new Integer%5B0%5D);%0A      i++;%0A    %7D%0A    result = new Clusters%3CO%3E(rarray, database);%0A  %7D%0A%7D%0A

Add a new trivial clustering algorithm using existing labels. Useful for comparing results to a golden standard.