public void toConsole(int indent) { for (int i = 0; i < indent; i++) { System.out.print(" "); } String name = getName() + (isLeaf() ? " (leaf)" : "") + (distance != null ? " distance: " + distance : ""); System.out.println(name); for (Cluster child : getChildren()) { child.toConsole(indent + 1); } }
public double getTotalDistance() { double dist = getDistance() == null ? 0 : getDistance(); if (getChildren().size() > 0) { dist += children.get(0).getTotalDistance(); } return dist; }
private List<Cluster> createClusters(String[] clusterNames) { List<Cluster> clusters = new ArrayList<Cluster>(); for (String clusterName : clusterNames) { Cluster cluster = new Cluster(clusterName); cluster.addLeafName(clusterName); clusters.add(cluster); } return clusters; }
public int countLeafs(Cluster node, int count) { if (node.isLeaf()) count++; for (Cluster child : node.getChildren()) { count += child.countLeafs(); } return count; }
Cluster cluster = new Cluster(name); cluster.setDistance(new Distance(getLinkageDistance())); cluster.appendLeafNames(lCluster.getLeafNames()); cluster.appendLeafNames(rCluster.getLeafNames()); cluster.addChild(lCluster); cluster.addChild(rCluster); lCluster.setParent(cluster); rCluster.setParent(cluster); Double lWeight = lCluster.getWeightValue(); Double rWeight = rCluster.getWeightValue(); double weight = lWeight + rWeight; cluster.getDistance().setWeight(weight);
@Override public void paint(Graphics2D g, int xDisplayOffset, int yDisplayOffset, double xDisplayFactor, double yDisplayFactor, boolean decorated) { int x1, y1, x2, y2; FontMetrics fontMetrics = g.getFontMetrics(); x1 = (int)(initPoint.getX() * xDisplayFactor + xDisplayOffset); y1 = (int)(initPoint.getY() * yDisplayFactor + yDisplayOffset); x2 = (int)(linkPoint.getX() * xDisplayFactor + xDisplayOffset); y2 = y1; g.fillOval(x1-dotRadius, y1-dotRadius, dotRadius * 2, dotRadius * 2); g.drawLine(x1, y1, x2, y2); if (cluster.isLeaf()) { g.drawString(cluster.getName().toString(), x1 + namePadding, y1 + (fontMetrics.getHeight()/2) - 2); } if (decorated && cluster.getDistance() != null && !cluster.getDistance().isNaN() && cluster.getDistance() > 0) { String s = String.format("%.2f", cluster.getDistance()); Rectangle2D rect = fontMetrics.getStringBounds(s, g); g.drawString(s, x1 - (int)rect.getWidth() , y1 - 2); } x1 = x2; y1 = y2; y2 = (int)(linkPoint.getY() * yDisplayFactor + yDisplayOffset); g.drawLine(x1, y1, x2, y2); for (ClusterComponent child : children) { child.paint(g, xDisplayOffset, yDisplayOffset, xDisplayFactor, yDisplayFactor, decorated); } }
public Cluster agglomerate(String name) { if (name == null) { StringBuilder sb = new StringBuilder(); if (lCluster != null) { sb.append(lCluster.getName()); } if (rCluster != null) { if (sb.length() > 0) { sb.append("&"); } sb.append(rCluster.getName()); } name = sb.toString(); } Cluster cluster = new Cluster(name); cluster.setDistance(getLinkageDistance()); cluster.addChild(lCluster); cluster.addChild(rCluster); lCluster.setParent(cluster); rCluster.setParent(cluster); return cluster; }
private ClusterComponent createComponent(Cluster<T> cluster, VCoord initCoord, double clusterHeight) { ClusterComponent comp = null; if (cluster != null) { comp = new ClusterComponent(cluster, cluster.isLeaf(), initCoord); double leafHeight = clusterHeight / cluster.countLeafs(); double yChild = initCoord.getY() - (clusterHeight / 2); double distance = cluster.getDistance() == null ? 0 : cluster.getDistance(); for (Cluster<T> child : cluster.getChildren()) { int childLeafCount = child.countLeafs(); double childHeight = childLeafCount * leafHeight; double childDistance = child.getDistance() == null ? 0 : child.getDistance(); VCoord childInitCoord = new VCoord(initCoord.getX() + (distance - childDistance), yChild + childHeight / 2.0); yChild += childHeight; /* Traverse cluster node tree */ ClusterComponent childComp = createComponent(child, childInitCoord, childHeight); childComp.setLinkPoint(initCoord); comp.getChildren().add(childComp); } } return comp; }
private ClusterComponent createComponent(Cluster cluster, VCoord initCoord, double clusterHeight) { ClusterComponent comp = null; if (cluster != null) { comp = new ClusterComponent(cluster, cluster.isLeaf(), initCoord); double leafHeight = clusterHeight / cluster.countLeafs(); double yChild = initCoord.getY() - (clusterHeight / 2); double distance = cluster.getDistanceValue() == null ? 0 : cluster.getDistanceValue(); for (Cluster child : cluster.getChildren()) { int childLeafCount = child.countLeafs(); double childHeight = childLeafCount * leafHeight; double childDistance = child.getDistanceValue() == null ? 0 : child.getDistanceValue(); VCoord childInitCoord = new VCoord(initCoord.getX() + (distance - childDistance), yChild + childHeight / 2.0); yChild += childHeight; /* Traverse cluster node tree */ ClusterComponent childComp = createComponent(child, childInitCoord, childHeight); childComp.setLinkPoint(initCoord); comp.getChildren().add(childComp); } } return comp; }
public String toNewickString(int indent) if(!isLeaf()) cdtString+="("; if(isLeaf()) { cdtString+=getName(); List<Cluster> children = getChildren(); cdtString+=child.toNewickString(indent); String distanceString = distance.getDistance().toString().replace(",", "."); String weightString = distance.getWeight().toString().replace(",", "."); if(!isLeaf()) cdtString+=")";
private ArrayList<String> getNameInCluster(Cluster cluster) { ArrayList<String> itemsInCluster = new ArrayList<String>(); String nodeName; if (cluster.isLeaf()) { nodeName = cluster.getName(); itemsInCluster.add(nodeName); } else { // String[] clusterName = cluster.getName().split("#"); // nodeName = clusterName[1]; } for (Cluster child : cluster.getChildren()) { ArrayList<String> childrenNames = getNameInCluster(child); itemsInCluster.addAll(childrenNames); } return itemsInCluster; }
private ArrayList<String[]> findCluster(Cluster cluster, double cutDist, int minPatternPerCls) { ArrayList<String[]> clusterTSIdx = new ArrayList<String[]>(); if (cluster.getDistance() != null) { // if (cluster.getDistance() > cutDist) { if (cluster.getDistanceValue() > cutDist) { if (cluster.getChildren().size() > 0) { clusterTSIdx.addAll(findCluster(cluster.getChildren().get(0), cutDist, minPatternPerCls)); clusterTSIdx.addAll(findCluster(cluster.getChildren().get(1), cutDist, minPatternPerCls)); } } else { // String[] idxes = cluster.getName().split("&"); ArrayList<String> itemsInCluster = getNameInCluster(cluster); String[] idxes = itemsInCluster.toArray(new String[itemsInCluster.size()]); if (idxes.length > minPatternPerCls) { clusterTSIdx.add(idxes); } } } return clusterTSIdx; }
public boolean isLeaf() { return getChildren().size() == 0; }
@Override public boolean equals(Object obj) { if (!(obj instanceof Cluster)) return false; if (obj == this) return true; if (obj == null) return false; return ((Cluster)obj).getName().equals(getName()); // String otherName = obj != null ? obj.toString() : ""; // return toString().equals(otherName); }
private List<Cluster> createClusters(String[] clusterNames, double[] weights) { List<Cluster> clusters = new ArrayList<Cluster>(); for (int i = 0; i < weights.length; i++) { Cluster cluster = new Cluster(clusterNames[i]); cluster.setDistance(new Distance(0.0, weights[i])); clusters.add(cluster); } return clusters; }
if (cluster.getDistance() == null) { double cutDist = cluster.getDistanceValue() * 0.67;
private <T> List<Cluster<T>> createClusters(T[] clusterNames) { List<Cluster<T>> clusters = new ArrayList<Cluster<T>>(); for (T clusterName : clusterNames) { Cluster cluster = new Cluster(clusterName); clusters.add(cluster); } return clusters; }
public int countLeafs() { return countLeafs(this, 0); }
g2.drawLine(x1, y1, x2, y2); double totalDistance = component.getCluster().getTotalDistance(); double xModelInterval; if (scaleValueInterval <= 0) {