Program: 10517
Project: 10517
Buggy source:
protected void refinePatternsByClustering(){
  double[] origTS=originalTimeSeries;
  ArrayList<SameLengthMotifs> newAllClassifiedMotifs=new ArrayList<SameLengthMotifs>();
  for (  SameLengthMotifs sameLenMotifs : allClassifiedMotifs) {
    ArrayList<RuleInterval> arrPos=new ArrayList<RuleInterval>();
    ArrayList<SAXMotif> subsequences=sameLenMotifs.getSameLenMotifs();
    for (    SAXMotif ss : subsequences) {
      arrPos.add(ss.getPos());
    }
    int patternNum=arrPos.size();
    double dt[][]=new double[patternNum][patternNum];
    for (int i=0; i < patternNum; i++) {
      RuleInterval saxPos=arrPos.get(i);
      int start1=saxPos.getStart();
      int end1=saxPos.getEnd();
      double[] ts1=Arrays.copyOfRange(origTS,start1,end1);
      for (int j=0; j < arrPos.size(); j++) {
        RuleInterval saxPos2=arrPos.get(j);
        if (dt[i][j] > 0) {
          continue;
        }
        double d=0;
        dt[i][j]=d;
        if (i == j) {
          continue;
        }
        int start2=saxPos2.getStart();
        int end2=saxPos2.getEnd();
        double[] ts2=Arrays.copyOfRange(origTS,start2,end2);
        if (ts1.length > ts2.length)         d=calcDistTSAndPattern(ts1,ts2);
 else         d=calcDistTSAndPattern(ts2,ts1);
        dt[i][j]=d;
      }
    }
    String[] patternsName=new String[patternNum];
    for (int i=0; i < patternNum; i++) {
      patternsName[i]=String.valueOf(i);
    }
    ClusteringAlgorithm alg=new DefaultClusteringAlgorithm();
    Cluster cluster=alg.performClustering(dt,patternsName,new AverageLinkageStrategy());
    int minPatternPerCls=1;
    if (cluster.getDistance() == null) {
      continue;
    }
    double cutDist=cluster.getDistanceValue() * 0.67;
    ArrayList<String[]> clusterTSIdx=findCluster(cluster,cutDist,minPatternPerCls);
    while (clusterTSIdx.size() <= 0) {
      cutDist+=cutDist / 2;
      clusterTSIdx=findCluster(cluster,cutDist,minPatternPerCls);
    }
    newAllClassifiedMotifs.addAll(SeparateMotifsByClustering(clusterTSIdx,sameLenMotifs));
  }
  allClassifiedMotifs=newAllClassifiedMotifs;
}

Fixed version (ground truth):
protected void refinePatternsByClustering(){
  double[] origTS=originalTimeSeries;
  ArrayList<SameLengthMotifs> newAllClassifiedMotifs=new ArrayList<SameLengthMotifs>();
  for (  SameLengthMotifs sameLenMotifs : allClassifiedMotifs) {
    ArrayList<RuleInterval> arrPos=new ArrayList<RuleInterval>();
    ArrayList<SAXMotif> subsequences=sameLenMotifs.getSameLenMotifs();
    for (    SAXMotif ss : subsequences) {
      arrPos.add(ss.getPos());
    }
    int patternNum=arrPos.size();
    if (patternNum < 2) {
      continue;
    }
    double dt[][]=new double[patternNum][patternNum];
    for (int i=0; i < patternNum; i++) {
      RuleInterval saxPos=arrPos.get(i);
      int start1=saxPos.getStart();
      int end1=saxPos.getEnd();
      double[] ts1=Arrays.copyOfRange(origTS,start1,end1);
      for (int j=0; j < arrPos.size(); j++) {
        RuleInterval saxPos2=arrPos.get(j);
        if (dt[i][j] > 0) {
          continue;
        }
        double d=0;
        dt[i][j]=d;
        if (i == j) {
          continue;
        }
        int start2=saxPos2.getStart();
        int end2=saxPos2.getEnd();
        double[] ts2=Arrays.copyOfRange(origTS,start2,end2);
        if (ts1.length > ts2.length)         d=calcDistTSAndPattern(ts1,ts2);
 else         d=calcDistTSAndPattern(ts2,ts1);
        dt[i][j]=d;
      }
    }
    String[] patternsName=new String[patternNum];
    for (int i=0; i < patternNum; i++) {
      patternsName[i]=String.valueOf(i);
    }
    ClusteringAlgorithm alg=new DefaultClusteringAlgorithm();
    Cluster cluster=alg.performClustering(dt,patternsName,new AverageLinkageStrategy());
    int minPatternPerCls=1;
    if (cluster.getDistance() == null) {
      continue;
    }
    double cutDist=cluster.getDistanceValue() * 0.67;
    ArrayList<String[]> clusterTSIdx=findCluster(cluster,cutDist,minPatternPerCls);
    while (clusterTSIdx.size() <= 0) {
      cutDist+=cutDist / 2;
      clusterTSIdx=findCluster(cluster,cutDist,minPatternPerCls);
    }
    newAllClassifiedMotifs.addAll(SeparateMotifsByClustering(clusterTSIdx,sameLenMotifs));
  }
  allClassifiedMotifs=newAllClassifiedMotifs;
}