tools/perf/experimental/story_clustering/cluster_stories.py
def RunHierarchicalClustering(
  distance_matrix,
  max_cluster_count,
  min_cluster_size):
  """Clusters stories.

  Runs a hierarchical clustering algorithm based on the similarity measures.

  Args:
    distance_matrix: A dataframe containing distance matrix of stories.
    max_cluster_count: number representing the maximum number of clusters
      needed per metric.
    min_cluster_size: number representing the least number of members needed
      to make the cluster valid.

  Returns:
    A tuple containing:
    clusters: A list of cluster objects
    coverage: Ratio(float) of stories covered using this clustering
  """
  stories = distance_matrix.index.values
  remaining_clusters = set([])
  for story in stories:
    remaining_clusters.add(Cluster([story]))

  # The hierarchical clustering relies on a sorted list of possible
  # cluster merges ordered by the distance between them.
  heap = []

  # Initially each story is a cluster on it's own. And story pairs are
  # added all possible merges.
  for cluster1 in remaining_clusters:
    for cluster2 in remaining_clusters:
      if cluster1 == cluster2:
        break
      heapq.heappush(heap,
        (cluster1.GetDistanceFrom(cluster2, distance_matrix),
         cluster1, cluster2))

  # At each step the two clusters will be merged together.
  while (len(remaining_clusters) > max_cluster_count and len(heap) > 0):
    _, cluster1, cluster2 = heapq.heappop(heap)
    if (cluster1 not in remaining_clusters or
      cluster2 not in remaining_clusters):
      continue
    new_cluster = cluster1.Merge(cluster2)
    remaining_clusters.discard(cluster1)
    remaining_clusters.discard(cluster2)

    # Adding all possible merges to the heap
    for cluster in remaining_clusters:
      distance = new_cluster.GetDistanceFrom(cluster, distance_matrix)
      heapq.heappush(heap, (distance, new_cluster, cluster))

    remaining_clusters.add(new_cluster)

  final_clusters = []
  number_of_stories_covered = 0
  for cluster in remaining_clusters:
    cluster.GetRepresentative(distance_matrix)
    if len(cluster) >= min_cluster_size:
      final_clusters.append(cluster)
      number_of_stories_covered += len(cluster)
  coverage = number_of_stories_covered / len(stories)

  return final_clusters, coverage
Clusters stories.

  Runs a hierarchical clustering algorithm based on the similarity measures.

  Args:
    distance_matrix: A dataframe containing distance matrix of stories.
    max_cluster_count: number representing the maximum number of clusters
      needed per metric.
    min_cluster_size: number representing the least number of members needed
      to make the cluster valid.

  Returns:
    A tuple containing:
    clusters: A list of cluster objects
    coverage: Ratio(float) of stories covered using this clustering
  
Clusters stories.
Runs a hierarchical clustering algorithm based on the similarity measures.
Clusters stories . Runs a hierarchical clustering algorithm based on the similarity measures .
def RunHierarchicalClustering(
  distance_matrix,
  max_cluster_count,
  min_cluster_size):
  stories = distance_matrix.index.values
  remaining_clusters = set([])
  for story in stories:
    remaining_clusters.add(Cluster([story]))
  heap = []
  for cluster1 in remaining_clusters:
    for cluster2 in remaining_clusters:
      if cluster1 == cluster2:
        break
      heapq.heappush(heap,
        (cluster1.GetDistanceFrom(cluster2, distance_matrix),
         cluster1, cluster2))
  while (len(remaining_clusters) > max_cluster_count and len(heap) > 0):
    _, cluster1, cluster2 = heapq.heappop(heap)
    if (cluster1 not in remaining_clusters or
      cluster2 not in remaining_clusters):
      continue
    new_cluster = cluster1.Merge(cluster2)
    remaining_clusters.discard(cluster1)
    remaining_clusters.discard(cluster2)
    for cluster in remaining_clusters:
      distance = new_cluster.GetDistanceFrom(cluster, distance_matrix)
      heapq.heappush(heap, (distance, new_cluster, cluster))
    remaining_clusters.add(new_cluster)
  final_clusters = []
  number_of_stories_covered = 0
  for cluster in remaining_clusters:
    cluster.GetRepresentative(distance_matrix)
    if len(cluster) >= min_cluster_size:
      final_clusters.append(cluster)
      number_of_stories_covered += len(cluster)
  coverage = number_of_stories_covered / len(stories)
  return final_clusters, coverage
def RunHierarchicalClustering ( distance_matrix , max_cluster_count , min_cluster_size ) : stories = distance_matrix . index . values remaining_clusters = set ( [ ] ) for story in stories : remaining_clusters . add ( Cluster ( [ story ] ) ) heap = [ ] for cluster1 in remaining_clusters : for cluster2 in remaining_clusters : if cluster1 == cluster2 : break heapq . heappush ( heap , ( cluster1 . GetDistanceFrom ( cluster2 , distance_matrix ) , cluster1 , cluster2 ) ) while ( len ( remaining_clusters ) > max_cluster_count and len ( heap ) > 0 ) : _ , cluster1 , cluster2 = heapq . heappop ( heap ) if ( cluster1 not in remaining_clusters or cluster2 not in remaining_clusters ) : continue new_cluster = cluster1 . Merge ( cluster2 ) remaining_clusters . discard ( cluster1 ) remaining_clusters . discard ( cluster2 ) for cluster in remaining_clusters : distance = new_cluster . GetDistanceFrom ( cluster , distance_matrix ) heapq . heappush ( heap , ( distance , new_cluster , cluster ) ) remaining_clusters . add ( new_cluster ) final_clusters = [ ] number_of_stories_covered = 0 for cluster in remaining_clusters : cluster . GetRepresentative ( distance_matrix ) if len ( cluster ) >= min_cluster_size : final_clusters . append ( cluster ) number_of_stories_covered += len ( cluster ) coverage = number_of_stories_covered / len ( stories ) return final_clusters , coverage
Clusters stories.
Clusters stories .
"""Clusters stories.

  Runs a hierarchical clustering algorithm based on the similarity measures.

  Args:
    distance_matrix: A dataframe containing distance matrix of stories.
    max_cluster_count: number representing the maximum number of clusters
      needed per metric.
    min_cluster_size: number representing the least number of members needed
      to make the cluster valid.

  Returns:
    A tuple containing:
    clusters: A list of cluster objects
    coverage: Ratio(float) of stories covered using this clustering
  """ # The hierarchical clustering relies on a sorted list of possible # cluster merges ordered by the distance between them. # Initially each story is a cluster on it's own. And story pairs are # added all possible merges. # At each step the two clusters will be merged together. # Adding all possible merges to the heap
A tuple containing:
clusters: A list of cluster objects
coverage: Ratio(float) of stories covered using this clustering
A tuple containing : clusters : A list of cluster objects coverage : Ratio ( float ) of stories covered using this clustering
distance_matrix
A dataframe containing distance matrix of stories.
A dataframe containing distance matrix of stories .
max_cluster_count
number representing the maximum number of clusters
needed per metric.
number representing the maximum number of clusters needed per metric .
min_cluster_size
number representing the least number of members needed
to make the cluster valid.
number representing the least number of members needed to make the cluster valid .