/*
  * Bottom up hierarchical clustering
  */
 private static HashSet<Cluster> findClusters(Collection<Point3d> points, double threshold) {
   HashSet<Cluster> clusters = new HashSet<>(points.size() * 2);
   for (Point3d p : points) {
     Cluster candidate = new Cluster();
     candidate.add(p);
     for (Point3d p2 : points) {
       if (p != p2 && p.distance(p2) <= threshold) {
         candidate.add(p2);
       }
     }
     Iterator<Cluster> iter = clusters.iterator();
     while (iter.hasNext()) {
       Cluster c = iter.next();
       for (Point3d p2 : candidate) {
         if (c.contains(p2)) { // Se os clusters C e candidate se intersectam, fundi-los
           candidate.addAll(c);
           iter.remove();
           break;
         }
       }
     }
     clusters.add(candidate);
   }
   return clusters;
 }
Exemplo n.º 2
0
  /** Generate the hierarchical cluster and display it as a denogram in the graph */
  public TreeNode makeTree(ClutoSolution cs) {
    int[] tsize = cs.getTreeCounts();
    int[][] ftree = cs.getForwardTree();
    int nnrows = tsize.length;
    int nrows = cs.getMatrix().getRowCount();

    // for (int i = 0; i < nnrows-1; i++) {
    // String s = "ftree" + "\t" + i + "\t" + ftree[i][0] + "\t" + ftree[i][1] + "\t" + tsize[i];
    // System.out.println(s);
    // }

    Cluster[] ca = new Cluster[nnrows];
    for (int i = 0; i < nnrows - 1; i++) {

      if (!true) {
        String s = "ftree" + "\t" + i + "\t" + ftree[i][0] + "\t" + ftree[i][1] + "\t" + tsize[i];
        System.out.println(s);
      }

      Cluster cn = i < nrows ? (Cluster) new RowCluster(tm, i, null) : new CompositeCluster();
      cn.setSimilarity(Math.abs(tsize[i]));
      ca[i] = cn;
      if (ftree[i][0] > -1) {
        cn.add(ca[ftree[i][0]]);
      }
      if (ftree[i][0] > -1) {
        cn.add(ca[ftree[i][1]]);
      }
      rootNode = cn;
    }
    return rootNode;
  }
 @Test
 public void testBacked() {
   Cluster<Integer, Integer> c = new BackedClusterImpl<Integer, Integer>(graph);
   Graph<Integer, Integer> inducedGraph = c.getInducedGraph();
   assertEquals(0, inducedGraph.getEdgeCount());
   assertEquals(0, c.size());
   c.add(0);
   c.add(1);
   assertEquals(2, c.size());
   assertEquals(1, inducedGraph.getEdgeCount());
 }
Exemplo n.º 4
0
 /**
  * Create partitions from an input tree - recursive so will be called by many nodes, beginning
  * with root but with calculations actually beginning on leaves.
  *
  * @param partitions Partitions in the form of entries in the hash table
  * @param curInterestPercentage Current percentage of interest that we want splits to occur above
  *     to view in the network later
  */
 private ArrayList<Cluster> constructClusters(
     LinkedList<HashEntry> partitions, double curInterestPercentage) {
   ArrayList<Cluster> clusters = new ArrayList<Cluster>();
   // Thresholds for the current run of the cluster builder (c.f. the threshold for the partitions
   // list).
   double curInterestThreshold = (double) (noOfTrees * (curInterestPercentage / 100.0d));
   int majInterestThreshold = (int) (noOfTrees * (50.0 / 100.0d));
   for (Iterator<HashEntry> it = partitions.iterator(); it.hasNext(); ) {
     HashEntry entry = it.next();
     // Checks if this partition is still above threshold... partition.
     // If not: remove it - O(1).
     if ((double) entry.count <= curInterestThreshold) {
       // correct the isMajority flag to now refer to majority not if of interest or not..
       if (entry.count <= majInterestThreshold) {
         entry.isMajority = false;
       }
       it.remove();
       continue;
     }
     // Constructs clusters (list of TreeNode's for each set bit) from each partition.
     if ((double) entry.count > curInterestThreshold) {
       Cluster cluster = new Cluster();
       if (entry.count > majInterestThreshold) {
         cluster.isMajority = true;
       }
       cluster.aboveSplit = entry.partition;
       cluster.noOfOccurrences = entry.count;
       cluster.edgeLength = entry.edgeLengthsSum / entry.count;
       for (int i = 0; i < entry.partition.size(); i++) {
         if (entry.partition.get(i)) {
           TreeNode node = new TreeNode(taxa.getName(i));
           node.edgeLength = leafEdgeLengths[i] / noOfTrees;
           cluster.add(node);
         }
       }
       clusters.add(cluster);
     }
   }
   // Sort by number of taxa.
   // TODO: This might obviously be optimized a bit, e.g. with a PriorityQueue. - Eiriksson
   Collections.sort(clusters);
   return clusters;
 }