Program: 22739
Project: 22739
Buggy source:
/** 
 * Apply current classifier to a set of feature vectors (given in a feature stack array)
 * @param fsa feature stack array
 * @param numThreads The number of threads to use. Set to zero for auto-detection.
 * @param probabilityMaps probability flag. Tue: probability maps are calculated, false: binary classification 
 * @return result image containing the probability maps or the binary classification
 */
public ImagePlus applyClassifier(final FeatureStackArray fsa,int numThreads,boolean probabilityMaps){
  if (numThreads == 0)   numThreads=Prefs.getThreads();
  ArrayList<String> classNames=null;
  if (null != loadedClassNames)   classNames=loadedClassNames;
 else {
    classNames=new ArrayList<String>();
    for (int j=0; j < trainingImage.getImageStackSize(); j++)     for (int i=0; i < numOfClasses; i++)     if (examples[j].get(i).size() > 0)     if (false == classNames.contains(getClassLabels()[i]))     classNames.add(getClassLabels()[i]);
  }
  ArrayList<Attribute> attributes=new ArrayList<Attribute>();
  for (int i=1; i <= fsa.getNumOfFeatures(); i++) {
    String attString=fsa.getLabel(i);
    attributes.add(new Attribute(attString));
  }
  if (fsa.useNeighborhood())   for (int i=0; i < 8; i++) {
    IJ.log("String_Node_Str" + (i + 1) + "String_Node_Str");
    attributes.add(new Attribute(new String("String_Node_Str" + (i + 1))));
  }
  attributes.add(new Attribute("String_Node_Str",classNames));
  Instances dataInfo=new Instances("String_Node_Str",attributes,1);
  dataInfo.setClassIndex(dataInfo.numAttributes() - 1);
  final int numClasses=classNames.size();
  final int numInstances=fsa.getSize() * trainingImage.getWidth() * trainingImage.getHeight();
  final int numChannels=(probabilityMaps ? numClasses : 1);
  final int numSlices=(numChannels * numInstances) / (trainingImage.getWidth() * trainingImage.getHeight());
  IJ.showStatus("String_Node_Str");
  final long start=System.currentTimeMillis();
  exe=Executors.newFixedThreadPool(numThreads);
  final double[][][] results=new double[numThreads][][];
  final int partialSize=numInstances / numThreads;
  Future<double[][]> fu[]=new Future[numThreads];
  final AtomicInteger counter=new AtomicInteger();
  for (int i=0; i < numThreads; i++) {
    if (Thread.currentThread().isInterrupted())     return null;
    int first=i * partialSize;
    int size=(i == numThreads - 1) ? numInstances - i * partialSize : partialSize;
    AbstractClassifier classifierCopy=null;
    try {
      if (classifier instanceof FastRandomForest || classifier instanceof RandomForest)       classifierCopy=classifier;
 else       classifierCopy=(AbstractClassifier)(AbstractClassifier.makeCopy(classifier));
    }
 catch (    Exception e) {
      IJ.log("String_Node_Str");
      e.printStackTrace();
    }
    fu[i]=exe.submit(classifyInstances(fsa,dataInfo,first,size,classifierCopy,counter,probabilityMaps));
  }
  ScheduledExecutorService monitor=Executors.newScheduledThreadPool(1);
  ScheduledFuture task=monitor.scheduleWithFixedDelay(new Runnable(){
    public void run(){
      IJ.showProgress(counter.get(),numInstances);
    }
  }
,0,1,TimeUnit.SECONDS);
  for (int i=0; i < numThreads; i++) {
    try {
      results[i]=fu[i].get();
    }
 catch (    InterruptedException e) {
      return null;
    }
catch (    ExecutionException e) {
      e.printStackTrace();
      return null;
    }
 finally {
      exe.shutdown();
      task.cancel(true);
      monitor.shutdownNow();
      IJ.showProgress(1);
    }
  }
  exe.shutdown();
  double[][] classificationResult=new double[numChannels][numInstances];
  for (int i=0; i < numThreads; i++)   for (int c=0; c < numChannels; c++)   System.arraycopy(results[i][c],0,classificationResult[c],i * partialSize,results[i][c].length);
  IJ.showProgress(1.0);
  final long end=System.currentTimeMillis();
  IJ.log("String_Node_Str" + (end - start) + "String_Node_Str");
  double[] classifiedSlice=new double[trainingImage.getWidth() * trainingImage.getHeight()];
  final ImageStack classStack=new ImageStack(trainingImage.getWidth(),trainingImage.getHeight());
  for (int i=0; i < numSlices / numChannels; i++) {
    for (int c=0; c < numChannels; c++) {
      System.arraycopy(classificationResult[c],i * (trainingImage.getWidth() * trainingImage.getHeight()),classifiedSlice,0,trainingImage.getWidth() * trainingImage.getHeight());
      ImageProcessor classifiedSliceProcessor=new FloatProcessor(trainingImage.getWidth(),trainingImage.getHeight(),classifiedSlice);
      classStack.addSlice(probabilityMaps ? getClassLabels()[c] : "String_Node_Str",classifiedSliceProcessor);
    }
  }
  ImagePlus classImg=new ImagePlus(probabilityMaps ? "String_Node_Str" : "String_Node_Str",classStack);
  return classImg;
}

Fixed version (ground truth):
/** 
 * Apply current classifier to a set of feature vectors (given in a feature stack array)
 * @param fsa feature stack array
 * @param numThreads The number of threads to use. Set to zero for auto-detection.
 * @param probabilityMaps probability flag. Tue: probability maps are calculated, false: binary classification 
 * @return result image containing the probability maps or the binary classification
 */
public ImagePlus applyClassifier(final FeatureStackArray fsa,int numThreads,boolean probabilityMaps){
  if (numThreads == 0)   numThreads=Prefs.getThreads();
  ArrayList<String> classNames=null;
  if (null != loadedClassNames)   classNames=loadedClassNames;
 else {
    classNames=new ArrayList<String>();
    for (int j=0; j < trainingImage.getImageStackSize(); j++)     for (int i=0; i < numOfClasses; i++)     if (examples[j].get(i).size() > 0)     if (false == classNames.contains(getClassLabels()[i]))     classNames.add(getClassLabels()[i]);
  }
  ArrayList<Attribute> attributes=new ArrayList<Attribute>();
  for (int i=1; i <= fsa.getNumOfFeatures(); i++) {
    String attString=fsa.getLabel(i);
    attributes.add(new Attribute(attString));
  }
  if (fsa.useNeighborhood())   for (int i=0; i < 8; i++) {
    IJ.log("String_Node_Str" + (i + 1) + "String_Node_Str");
    attributes.add(new Attribute(new String("String_Node_Str" + (i + 1))));
  }
  attributes.add(new Attribute("String_Node_Str",classNames));
  Instances dataInfo=new Instances("String_Node_Str",attributes,1);
  dataInfo.setClassIndex(dataInfo.numAttributes() - 1);
  final int numClasses=classNames.size();
  final int numInstances=fsa.getSize() * trainingImage.getWidth() * trainingImage.getHeight();
  final int numChannels=(probabilityMaps ? numClasses : 1);
  final int numSlices=(numChannels * numInstances) / (trainingImage.getWidth() * trainingImage.getHeight());
  IJ.showStatus("String_Node_Str");
  final long start=System.currentTimeMillis();
  exe=Executors.newFixedThreadPool(numThreads);
  final double[][][] results=new double[numThreads][][];
  final int partialSize=numInstances / numThreads;
  Future<double[][]> fu[]=new Future[numThreads];
  final AtomicInteger counter=new AtomicInteger();
  for (int i=0; i < numThreads; i++) {
    if (Thread.currentThread().isInterrupted())     return null;
    int first=i * partialSize;
    int size=(i == numThreads - 1) ? numInstances - i * partialSize : partialSize;
    AbstractClassifier classifierCopy=null;
    try {
      classifierCopy=(AbstractClassifier)(AbstractClassifier.makeCopy(classifier));
    }
 catch (    Exception e) {
      IJ.log("String_Node_Str");
      e.printStackTrace();
    }
    fu[i]=exe.submit(classifyInstances(fsa,dataInfo,first,size,classifierCopy,counter,probabilityMaps));
  }
  ScheduledExecutorService monitor=Executors.newScheduledThreadPool(1);
  ScheduledFuture task=monitor.scheduleWithFixedDelay(new Runnable(){
    public void run(){
      IJ.showProgress(counter.get(),numInstances);
    }
  }
,0,1,TimeUnit.SECONDS);
  for (int i=0; i < numThreads; i++) {
    try {
      results[i]=fu[i].get();
    }
 catch (    InterruptedException e) {
      return null;
    }
catch (    ExecutionException e) {
      e.printStackTrace();
      return null;
    }
 finally {
      exe.shutdown();
      task.cancel(true);
      monitor.shutdownNow();
      IJ.showProgress(1);
    }
  }
  exe.shutdown();
  double[][] classificationResult=new double[numChannels][numInstances];
  for (int i=0; i < numThreads; i++)   for (int c=0; c < numChannels; c++)   System.arraycopy(results[i][c],0,classificationResult[c],i * partialSize,results[i][c].length);
  IJ.showProgress(1.0);
  final long end=System.currentTimeMillis();
  IJ.log("String_Node_Str" + (end - start) + "String_Node_Str");
  double[] classifiedSlice=new double[trainingImage.getWidth() * trainingImage.getHeight()];
  final ImageStack classStack=new ImageStack(trainingImage.getWidth(),trainingImage.getHeight());
  for (int i=0; i < numSlices / numChannels; i++) {
    for (int c=0; c < numChannels; c++) {
      System.arraycopy(classificationResult[c],i * (trainingImage.getWidth() * trainingImage.getHeight()),classifiedSlice,0,trainingImage.getWidth() * trainingImage.getHeight());
      ImageProcessor classifiedSliceProcessor=new FloatProcessor(trainingImage.getWidth(),trainingImage.getHeight(),classifiedSlice);
      classStack.addSlice(probabilityMaps ? getClassLabels()[c] : "String_Node_Str",classifiedSliceProcessor);
    }
  }
  ImagePlus classImg=new ImagePlus(probabilityMaps ? "String_Node_Str" : "String_Node_Str",classStack);
  return classImg;
}