Program: 22740
Project: 22740
Buggy source:
/** 
 * Create training instances out of the user markings
 * @return set of instances (feature vectors in Weka format)
 */
public Instances createTrainingInstances(){
  ArrayList<Attribute> attributes=new ArrayList<Attribute>();
  for (int i=1; i <= featureStackArray.getNumOfFeatures(); i++) {
    String attString=featureStackArray.getLabel(i);
    attributes.add(new Attribute(attString));
  }
  final ArrayList<String> classes;
  int numOfInstances=0;
  int numOfUsedClasses=0;
  if (null == this.loadedTrainingData) {
    classes=new ArrayList<String>();
    for (int i=0; i < numOfClasses; i++) {
      for (int n=0; n < trainingImage.getImageStackSize(); n++) {
        if (examples[n].get(i).size() > 0) {
          if (classes.contains(getClassLabels()[i]) == false)           classes.add(getClassLabels()[i]);
          numOfUsedClasses++;
        }
        numOfInstances+=examples[n].get(i).size();
      }
    }
  }
 else {
    classes=this.loadedClassNames;
  }
  attributes.add(new Attribute("String_Node_Str",classes));
  final Instances trainingData=new Instances("String_Node_Str",attributes,numOfInstances);
  IJ.log("String_Node_Str");
  final boolean colorFeatures=this.trainingImage.getType() == ImagePlus.COLOR_RGB;
  for (int l=0; l < numOfClasses; l++) {
    int nl=0;
    for (int sliceNum=1; sliceNum <= trainingImage.getImageStackSize(); sliceNum++)     for (int j=0; j < examples[sliceNum - 1].get(l).size(); j++) {
      Roi r=examples[sliceNum - 1].get(l).get(j);
      if (r instanceof PolygonRoi && r.getType() != Roi.FREEROI) {
        if (r.getStrokeWidth() == 1) {
          int[] x=r.getPolygon().xpoints;
          int[] y=r.getPolygon().ypoints;
          final int n=r.getPolygon().npoints;
          for (int i=0; i < n; i++) {
            double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
            if (colorFeatures)             for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)             values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixel(x[i],y[i]);
 else             for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)             values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelValue(x[i],y[i]);
            values[featureStackArray.getNumOfFeatures()]=(double)l;
            trainingData.add(new DenseInstance(1.0,values));
            nl++;
          }
        }
 else {
          final int width=(int)Math.round(r.getStrokeWidth());
          FloatPolygon p=r.getFloatPolygon();
          int n=p.npoints;
          double x1, y1;
          double x2=p.xpoints[0] - (p.xpoints[1] - p.xpoints[0]);
          double y2=p.ypoints[0] - (p.ypoints[1] - p.ypoints[0]);
          for (int i=0; i < n; i++) {
            x1=x2;
            y1=y2;
            x2=p.xpoints[i];
            y2=p.ypoints[i];
            double dx=x2 - x1;
            double dy=y1 - y2;
            double length=(float)Math.sqrt(dx * dx + dy * dy);
            dx/=length;
            dy/=length;
            double x=x2 - dy * width / 2.0;
            double y=y2 - dx * width / 2.0;
            int n2=width;
            do {
              if (x >= 0 && x < featureStackArray.get(sliceNum - 1).getWidth() && y >= 0 && y < featureStackArray.get(sliceNum - 1).getHeight()) {
                double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
                if (colorFeatures)                 for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)                 values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelInterpolated(x,y);
 else                 for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)                 values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getInterpolatedValue(x,y);
                values[featureStackArray.getNumOfFeatures()]=(double)l;
                trainingData.add(new DenseInstance(1.0,values));
                nl++;
              }
              x+=dy;
              y+=dx;
            }
 while (--n2 > 0);
          }
        }
      }
 else {
        final ShapeRoi shapeRoi=new ShapeRoi(r);
        final Rectangle rect=shapeRoi.getBounds();
        final int lastX=rect.x + rect.width;
        final int lastY=rect.y + rect.height;
        for (int x=rect.x; x < lastX; x++)         for (int y=rect.y; y < lastY; y++)         if (shapeRoi.contains(x,y)) {
          double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
          if (colorFeatures)           for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)           values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixel(x,y);
 else           for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)           values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelValue(x,y);
          values[featureStackArray.getNumOfFeatures()]=(double)l;
          trainingData.add(new DenseInstance(1.0,values));
          nl++;
        }
      }
    }
    IJ.log("String_Node_Str" + getClassLabels()[l] + "String_Node_Str"+ nl);
  }
  if (trainingData.numInstances() == 0)   return null;
  trainingData.setClassIndex(featureStackArray.getNumOfFeatures());
  return trainingData;
}

Fixed version (ground truth):
/** 
 * Create training instances out of the user markings
 * @return set of instances (feature vectors in Weka format)
 */
public Instances createTrainingInstances(){
  ArrayList<Attribute> attributes=new ArrayList<Attribute>();
  for (int i=1; i <= featureStackArray.getNumOfFeatures(); i++) {
    String attString=featureStackArray.getLabel(i);
    attributes.add(new Attribute(attString));
  }
  final ArrayList<String> classes;
  int numOfInstances=0;
  int numOfUsedClasses=0;
  if (null == this.loadedTrainingData) {
    classes=new ArrayList<String>();
    for (int i=0; i < numOfClasses; i++) {
      for (int n=0; n < trainingImage.getImageStackSize(); n++) {
        if (examples[n].get(i).size() > 0) {
          if (classes.contains(getClassLabels()[i]) == false)           classes.add(getClassLabels()[i]);
          numOfUsedClasses++;
        }
        numOfInstances+=examples[n].get(i).size();
      }
    }
  }
 else {
    classes=this.loadedClassNames;
  }
  attributes.add(new Attribute("String_Node_Str",classes));
  final Instances trainingData=new Instances("String_Node_Str",attributes,numOfInstances);
  IJ.log("String_Node_Str");
  final boolean colorFeatures=this.trainingImage.getType() == ImagePlus.COLOR_RGB;
  for (int l=0; l < numOfClasses; l++) {
    int nl=0;
    for (int sliceNum=1; sliceNum <= trainingImage.getImageStackSize(); sliceNum++)     for (int j=0; j < examples[sliceNum - 1].get(l).size(); j++) {
      Roi r=examples[sliceNum - 1].get(l).get(j);
      if (r instanceof PolygonRoi && r.getType() == Roi.FREELINE) {
        if (r.getStrokeWidth() == 1) {
          int[] x=r.getPolygon().xpoints;
          int[] y=r.getPolygon().ypoints;
          final int n=r.getPolygon().npoints;
          for (int i=0; i < n; i++) {
            double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
            if (colorFeatures)             for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)             values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixel(x[i],y[i]);
 else             for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)             values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelValue(x[i],y[i]);
            values[featureStackArray.getNumOfFeatures()]=(double)l;
            trainingData.add(new DenseInstance(1.0,values));
            nl++;
          }
        }
 else {
          final int width=(int)Math.round(r.getStrokeWidth());
          FloatPolygon p=r.getFloatPolygon();
          int n=p.npoints;
          double x1, y1;
          double x2=p.xpoints[0] - (p.xpoints[1] - p.xpoints[0]);
          double y2=p.ypoints[0] - (p.ypoints[1] - p.ypoints[0]);
          for (int i=0; i < n; i++) {
            x1=x2;
            y1=y2;
            x2=p.xpoints[i];
            y2=p.ypoints[i];
            double dx=x2 - x1;
            double dy=y1 - y2;
            double length=(float)Math.sqrt(dx * dx + dy * dy);
            dx/=length;
            dy/=length;
            double x=x2 - dy * width / 2.0;
            double y=y2 - dx * width / 2.0;
            int n2=width;
            do {
              if (x >= 0 && x < featureStackArray.get(sliceNum - 1).getWidth() && y >= 0 && y < featureStackArray.get(sliceNum - 1).getHeight()) {
                double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
                if (colorFeatures)                 for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)                 values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelInterpolated(x,y);
 else                 for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)                 values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getInterpolatedValue(x,y);
                values[featureStackArray.getNumOfFeatures()]=(double)l;
                trainingData.add(new DenseInstance(1.0,values));
                nl++;
              }
              x+=dy;
              y+=dx;
            }
 while (--n2 > 0);
          }
        }
      }
 else {
        final ShapeRoi shapeRoi=new ShapeRoi(r);
        final Rectangle rect=shapeRoi.getBounds();
        final int lastX=rect.x + rect.width;
        final int lastY=rect.y + rect.height;
        for (int x=rect.x; x < lastX; x++)         for (int y=rect.y; y < lastY; y++)         if (shapeRoi.contains(x,y)) {
          double[] values=new double[featureStackArray.getNumOfFeatures() + 1];
          if (colorFeatures)           for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)           values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixel(x,y);
 else           for (int z=1; z <= featureStackArray.getNumOfFeatures(); z++)           values[z - 1]=featureStackArray.get(sliceNum - 1).getProcessor(z).getPixelValue(x,y);
          values[featureStackArray.getNumOfFeatures()]=(double)l;
          trainingData.add(new DenseInstance(1.0,values));
          nl++;
        }
      }
    }
    IJ.log("String_Node_Str" + getClassLabels()[l] + "String_Node_Str"+ nl);
  }
  if (trainingData.numInstances() == 0)   return null;
  trainingData.setClassIndex(featureStackArray.getNumOfFeatures());
  return trainingData;
}