Skip to content

Commit

Permalink
Merge pull request #28 from esi-mineset/cluster-names-25-bb
Browse files Browse the repository at this point in the history
Cluster names #25
  • Loading branch information
lbehnke authored Dec 13, 2019
2 parents d38032b + 9d86d5c commit 79b889c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@

public class ClusterPair implements Comparable<ClusterPair> {

private static long globalIndex = 0;

private Cluster lCluster;
private Cluster rCluster;
private Double linkageDistance;
private Double linkageDistance;

public ClusterPair(){
}

public ClusterPair(Cluster left, Cluster right, Double distance) {
public ClusterPair(Cluster left, Cluster right, Double distance) {
lCluster=left;
rCluster=right;
linkageDistance=distance;
Expand All @@ -53,11 +51,11 @@ public void setrCluster(Cluster rCluster) {
this.rCluster = rCluster;
}

public Double getLinkageDistance() {
public Double getLinkageDistance() {
return linkageDistance;
}

public void setLinkageDistance(Double distance) {
public void setLinkageDistance(Double distance) {
this.linkageDistance = distance;
}

Expand All @@ -84,25 +82,11 @@ public int compareTo(ClusterPair o) {
return result;
}

public Cluster agglomerate(int clusterIdx) {
return agglomerate("clstr#" + clusterIdx);
}

public Cluster agglomerate(String name) {
if (name == null) {
name = "clstr#" + (++globalIndex);

/*
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(new Distance(getLinkageDistance()));
//New clusters will track their children's leaf names; i.e. each cluster knows what part of the original data it contains
Expand All @@ -115,7 +99,7 @@ public Cluster agglomerate(String name) {

Double lWeight = lCluster.getWeightValue();
Double rWeight = rCluster.getWeightValue();
double weight = lWeight + rWeight;
double weight = lWeight + rWeight;
cluster.getDistance().setWeight(weight);

return cluster;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class HierarchyBuilder {

private DistanceMap distances;
private List<Cluster> clusters;
private int globalClusterIndex = 0;

public DistanceMap getDistances() {
return distances;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void agglomerate(LinkageStrategy linkageStrategy) {

Cluster oldClusterL = minDistLink.getlCluster();
Cluster oldClusterR = minDistLink.getrCluster();
Cluster newCluster = minDistLink.agglomerate(null);
Cluster newCluster = minDistLink.agglomerate(++globalClusterIndex);

for (Cluster iClust : clusters) {
ClusterPair link1 = findByClusters(iClust, oldClusterL);
Expand All @@ -77,23 +78,22 @@ public void agglomerate(LinkageStrategy linkageStrategy) {
Collection<Distance> distanceValues = new ArrayList<Distance>();

if (link1 != null) {
Double distVal = link1.getLinkageDistance();
Double distVal = link1.getLinkageDistance();
Double weightVal = link1.getOtherCluster(iClust).getWeightValue();
distanceValues.add(new Distance(distVal, weightVal));
distances.remove(link1);
}
if (link2 != null) {
Double distVal = link2.getLinkageDistance();
Double weightVal = link2.getOtherCluster(iClust).getWeightValue();
Double distVal = link2.getLinkageDistance();
Double weightVal = link2.getOtherCluster(iClust).getWeightValue();
distanceValues.add(new Distance(distVal, weightVal));
distances.remove(link2);
}

Distance newDistance = linkageStrategy.calculateDistance(distanceValues);

newLinkage.setLinkageDistance(newDistance.getDistance());
newLinkage.setLinkageDistance(newDistance.getDistance());
distances.add(newLinkage);

}
clusters.add(newCluster);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public void setup() {
public void testCountLeafs() throws Exception {
int leafs = cluster.countLeafs();
assertEquals(6, leafs);
assertEquals("clstr#5", cluster.getName());
}

@Test
public void testGetTotalDistance() throws Exception {
int dist = (int) cluster.getTotalDistance();
assertEquals(10, dist);
assertEquals("clstr#5", cluster.getName());
}
}

0 comments on commit 79b889c

Please sign in to comment.