Program: 149545
Project: 149545
Buggy source:
public MultiPathFitResult.FitResult getResultMulti(){
  if (neighbours == 0)   return null;
  if (resultMulti != null)   return resultMulti;
  float background=getMultiFittingBackground();
  int subtractFittedPeaks=0;
  boolean[] subtract=null;
  if (fittedNeighbourCount > 0) {
    subtract=new boolean[fittedNeighbourCount];
    final float xmin=regionBounds.x;
    final float xmax=xmin + regionBounds.width;
    final float ymin=regionBounds.y;
    final float ymax=ymin + regionBounds.height;
    for (int i=0; i < fittedNeighbourCount; i++) {
      final PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
      if (result.getXPosition() < xmin || result.getXPosition() > xmax || result.getYPosition() < ymin || result.getYPosition() > ymax) {
        subtract[i]=true;
        subtractFittedPeaks++;
      }
    }
  }
  if (logger != null)   logger.info("String_Node_Str",slice,neighbours + 1,neighbourCount,fittedNeighbourCount,subtractFittedPeaks);
  neighbours=neighbourCount + fittedNeighbourCount - subtractFittedPeaks;
  int npeaks=1 + neighbours;
  double[] params=new double[1 + npeaks * parametersPerPeak];
  double[] lower=new double[params.length];
  double[] upper=new double[params.length];
  for (int i=0; i < lower.length; i++) {
    lower[i]=Double.NEGATIVE_INFINITY;
    upper[i]=Double.POSITIVE_INFINITY;
  }
  params[Gaussian2DFunction.SIGNAL]=candidates[candidateId].intensity + ((relativeIntensity) ? background : 0);
  params[Gaussian2DFunction.X_POSITION]=candidates[candidateId].x - regionBounds.x;
  params[Gaussian2DFunction.Y_POSITION]=candidates[candidateId].y - regionBounds.y;
  for (int i=0, j=parametersPerPeak; i < neighbourCount; i++, j+=parametersPerPeak) {
    final int n2=neighbourIndices[i];
    final double candidateX=candidates[n2].x - regionBounds.x;
    final double candidateY=candidates[n2].y - regionBounds.y;
    final double[] estimatedParams=getEstimate(n2);
    if (estimatedParams != null) {
      params[j + Gaussian2DFunction.SIGNAL]=estimatedParams[Gaussian2DFunction.SIGNAL] / (2 * Math.PI * estimatedParams[Gaussian2DFunction.X_SD]* estimatedParams[Gaussian2DFunction.Y_SD]);
      params[j + Gaussian2DFunction.X_POSITION]=estimatedParams[Gaussian2DFunction.X_POSITION] - regionBounds.x;
      params[j + Gaussian2DFunction.Y_POSITION]=estimatedParams[Gaussian2DFunction.Y_POSITION] - regionBounds.y;
      params[j + Gaussian2DFunction.ANGLE]=estimatedParams[Gaussian2DFunction.ANGLE];
      params[j + Gaussian2DFunction.X_SD]=estimatedParams[Gaussian2DFunction.X_SD];
      params[j + Gaussian2DFunction.Y_SD]=estimatedParams[Gaussian2DFunction.Y_SD];
    }
 else {
      params[j + Gaussian2DFunction.SIGNAL]=candidates[n2].intensity + ((relativeIntensity) ? background : 0);
      params[j + Gaussian2DFunction.X_POSITION]=candidateX;
      params[j + Gaussian2DFunction.Y_POSITION]=candidateY;
    }
    lower[j + Gaussian2DFunction.X_POSITION]=candidateX - 1;
    upper[j + Gaussian2DFunction.X_POSITION]=candidateX + 1;
    lower[j + Gaussian2DFunction.Y_POSITION]=candidateY - 1;
    upper[j + Gaussian2DFunction.Y_POSITION]=candidateY + 1;
  }
  if (!relativeIntensity) {
    for (int i=0, j=0; i <= neighbourCount; i++, j+=parametersPerPeak) {
      if (params[j + Gaussian2DFunction.SIGNAL] < background) {
        background=0;
        break;
      }
    }
  }
  double[] region=this.region;
  if (fittedNeighbourCount > 0) {
    final double xOffset=regionBounds.x + 0.5;
    final double yOffset=regionBounds.y + 0.5;
    if (subtractFittedPeaks > 0) {
      region=Arrays.copyOf(region,width * height);
      final double[] funcParams=new double[1 + parametersPerPeak * subtractFittedPeaks];
      for (int i=0, j=0; i < fittedNeighbourCount; i++) {
        if (!subtract[i])         continue;
        PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
        for (int k=1; k <= parametersPerPeak; k++)         funcParams[j + k]=result.params[k];
        funcParams[j + Gaussian2DFunction.X_POSITION]-=xOffset;
        funcParams[j + Gaussian2DFunction.Y_POSITION]-=yOffset;
        j+=parametersPerPeak;
      }
      final GaussianFunction func=fitConfig.createGaussianFunction(subtractFittedPeaks,regionBounds.width,funcParams);
      func.initialise(funcParams);
      for (int i=0; i < region.length; i++) {
        region[i]-=func.eval(i);
      }
    }
    for (int i=0, j=(1 + neighbourCount) * parametersPerPeak; i < fittedNeighbourCount; i++) {
      if (subtract[i])       continue;
      final PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
      params[j + Gaussian2DFunction.SIGNAL]=result.getAmplitude();
      for (int k=2; k <= parametersPerPeak; k++)       params[j + k]=result.params[k];
      params[j + Gaussian2DFunction.SIGNAL]+=result.params[Gaussian2DFunction.BACKGROUND];
      params[j + Gaussian2DFunction.X_POSITION]-=xOffset;
      params[j + Gaussian2DFunction.Y_POSITION]-=yOffset;
      lower[j + Gaussian2DFunction.X_POSITION]=params[j + Gaussian2DFunction.X_POSITION] - 1;
      upper[j + Gaussian2DFunction.X_POSITION]=params[j + Gaussian2DFunction.X_POSITION] + 1;
      lower[j + Gaussian2DFunction.Y_POSITION]=params[j + Gaussian2DFunction.Y_POSITION] - 1;
      upper[j + Gaussian2DFunction.Y_POSITION]=params[j + Gaussian2DFunction.Y_POSITION] + 1;
      j+=parametersPerPeak;
    }
  }
  if (background != 0) {
    params[Gaussian2DFunction.BACKGROUND]=background;
    for (int j=Gaussian2DFunction.SIGNAL; j < params.length; j+=parametersPerPeak)     params[j]-=background;
  }
  for (int i=0; i < npeaks; i++) {
    if ((int)params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION] == params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION])     params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION]+=0.001;
    if ((int)params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION] == params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION])     params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION]+=0.001;
  }
  final boolean fitValidation=fitConfig.isFitValidation();
  fitConfig.setFitValidation(false);
  final int maxIterations=fitConfig.getMaxIterations();
  final int maxEvaluations=fitConfig.getMaxFunctionEvaluations();
  fitConfig.setMaxIterations(maxIterations + maxIterations * (npeaks - 1) * ITERATION_INCREASE_FOR_MULTIPLE_PEAKS);
  fitConfig.setMaxIterations(maxEvaluations + maxEvaluations * (npeaks - 1) * EVALUATION_INCREASE_FOR_MULTIPLE_PEAKS);
  gf.setBounds(lower,upper);
  final FitResult fitResult=gf.fit(region,width,height,npeaks,params,true);
  gf.setBounds(null,null);
  fitConfig.setFitValidation(fitValidation);
  fitConfig.setMaxIterations(maxIterations);
  fitConfig.setMaxFunctionEvaluations(maxEvaluations);
  updateError(fitResult);
  PreprocessedPeakResult[] results=null;
  if (fitResult.getStatus() == FitStatus.OK) {
    final double[] fitParams=fitResult.getParameters();
    final double[] initialParams=fitResult.getInitialParameters();
    convertParameters(fitParams);
    results=new PreprocessedPeakResult[npeaks];
    final int flags=fitConfig.getFunctionFlags();
    results[0]=resultFactory.createPreprocessedPeakResult(this.candidateId,0,initialParams,fitParams,getLocalBackground(0,npeaks,fitParams,flags),ResultType.NEW);
    for (int n=neighbourCount + 1; n < npeaks; n++) {
      results[n]=resultFactory.createPreprocessedPeakResult(this.candidateId,n,initialParams,fitParams,getLocalBackground(n,npeaks,fitParams,flags),ResultType.EXISTING);
    }
    for (int n=1; n <= neighbourCount; n++) {
      results[n]=resultFactory.createPreprocessedPeakResult(this.candidateId,n,initialParams,fitParams,getLocalBackground(n,npeaks,fitParams,flags),ResultType.CANDIDATE);
    }
  }
  return resultMulti=createResult(fitResult,results);
}

Fixed version (ground truth):
public MultiPathFitResult.FitResult getResultMulti(){
  if (neighbours == 0)   return null;
  if (resultMulti != null)   return resultMulti;
  float background=getMultiFittingBackground();
  int subtractFittedPeaks=0;
  boolean[] subtract=null;
  if (fittedNeighbourCount > 0) {
    subtract=new boolean[fittedNeighbourCount];
    final float xmin=regionBounds.x;
    final float xmax=xmin + regionBounds.width;
    final float ymin=regionBounds.y;
    final float ymax=ymin + regionBounds.height;
    for (int i=0; i < fittedNeighbourCount; i++) {
      final PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
      if (result.getXPosition() < xmin || result.getXPosition() > xmax || result.getYPosition() < ymin || result.getYPosition() > ymax) {
        subtract[i]=true;
        subtractFittedPeaks++;
      }
    }
  }
  if (logger != null)   logger.info("String_Node_Str",slice,neighbours + 1,neighbourCount,fittedNeighbourCount,subtractFittedPeaks);
  neighbours=neighbourCount + fittedNeighbourCount - subtractFittedPeaks;
  int npeaks=1 + neighbours;
  double[] params=new double[1 + npeaks * parametersPerPeak];
  double[] lower=new double[params.length];
  double[] upper=new double[params.length];
  for (int i=0; i < lower.length; i++) {
    lower[i]=Double.NEGATIVE_INFINITY;
    upper[i]=Double.POSITIVE_INFINITY;
  }
  params[Gaussian2DFunction.SIGNAL]=candidates[candidateId].intensity + ((relativeIntensity) ? background : 0);
  params[Gaussian2DFunction.X_POSITION]=candidates[candidateId].x - regionBounds.x;
  params[Gaussian2DFunction.Y_POSITION]=candidates[candidateId].y - regionBounds.y;
  for (int i=0, j=parametersPerPeak; i < neighbourCount; i++, j+=parametersPerPeak) {
    final int n2=neighbourIndices[i];
    final double candidateX=candidates[n2].x - regionBounds.x;
    final double candidateY=candidates[n2].y - regionBounds.y;
    final double[] estimatedParams=getEstimate(n2);
    if (estimatedParams != null) {
      params[j + Gaussian2DFunction.SIGNAL]=estimatedParams[Gaussian2DFunction.SIGNAL] / (2 * Math.PI * estimatedParams[Gaussian2DFunction.X_SD]* estimatedParams[Gaussian2DFunction.Y_SD]);
      params[j + Gaussian2DFunction.X_POSITION]=estimatedParams[Gaussian2DFunction.X_POSITION] - regionBounds.x;
      params[j + Gaussian2DFunction.Y_POSITION]=estimatedParams[Gaussian2DFunction.Y_POSITION] - regionBounds.y;
      params[j + Gaussian2DFunction.ANGLE]=estimatedParams[Gaussian2DFunction.ANGLE];
      params[j + Gaussian2DFunction.X_SD]=estimatedParams[Gaussian2DFunction.X_SD];
      params[j + Gaussian2DFunction.Y_SD]=estimatedParams[Gaussian2DFunction.Y_SD];
    }
 else {
      params[j + Gaussian2DFunction.SIGNAL]=candidates[n2].intensity + ((relativeIntensity) ? background : 0);
      params[j + Gaussian2DFunction.X_POSITION]=candidateX;
      params[j + Gaussian2DFunction.Y_POSITION]=candidateY;
    }
    lower[j + Gaussian2DFunction.X_POSITION]=candidateX - 1;
    upper[j + Gaussian2DFunction.X_POSITION]=candidateX + 1;
    lower[j + Gaussian2DFunction.Y_POSITION]=candidateY - 1;
    upper[j + Gaussian2DFunction.Y_POSITION]=candidateY + 1;
  }
  if (!relativeIntensity) {
    for (int i=0, j=0; i <= neighbourCount; i++, j+=parametersPerPeak) {
      if (params[j + Gaussian2DFunction.SIGNAL] < background) {
        background=0;
        break;
      }
    }
  }
  double[] region=this.region;
  if (fittedNeighbourCount > 0) {
    final double xOffset=regionBounds.x + 0.5;
    final double yOffset=regionBounds.y + 0.5;
    if (subtractFittedPeaks > 0) {
      region=Arrays.copyOf(region,width * height);
      final double[] funcParams=new double[1 + parametersPerPeak * subtractFittedPeaks];
      for (int i=0, j=0; i < fittedNeighbourCount; i++) {
        if (!subtract[i])         continue;
        PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
        for (int k=1; k <= parametersPerPeak; k++)         funcParams[j + k]=result.params[k];
        funcParams[j + Gaussian2DFunction.X_POSITION]-=xOffset;
        funcParams[j + Gaussian2DFunction.Y_POSITION]-=yOffset;
        j+=parametersPerPeak;
      }
      final GaussianFunction func=fitConfig.createGaussianFunction(subtractFittedPeaks,regionBounds.width,funcParams);
      func.initialise(funcParams);
      for (int i=0; i < region.length; i++) {
        region[i]-=func.eval(i);
      }
    }
    for (int i=0, j=(1 + neighbourCount) * parametersPerPeak; i < fittedNeighbourCount; i++) {
      if (subtract[i])       continue;
      final PeakResult result=sliceResults.get(fittedNeighbourIndices[i]);
      params[j + Gaussian2DFunction.SIGNAL]=result.getAmplitude();
      for (int k=2; k <= parametersPerPeak; k++)       params[j + k]=result.params[k];
      params[j + Gaussian2DFunction.SIGNAL]+=result.params[Gaussian2DFunction.BACKGROUND];
      params[j + Gaussian2DFunction.X_POSITION]-=xOffset;
      params[j + Gaussian2DFunction.Y_POSITION]-=yOffset;
      lower[j + Gaussian2DFunction.X_POSITION]=params[j + Gaussian2DFunction.X_POSITION] - 1;
      upper[j + Gaussian2DFunction.X_POSITION]=params[j + Gaussian2DFunction.X_POSITION] + 1;
      lower[j + Gaussian2DFunction.Y_POSITION]=params[j + Gaussian2DFunction.Y_POSITION] - 1;
      upper[j + Gaussian2DFunction.Y_POSITION]=params[j + Gaussian2DFunction.Y_POSITION] + 1;
      j+=parametersPerPeak;
    }
  }
  if (background != 0) {
    params[Gaussian2DFunction.BACKGROUND]=background;
    for (int j=Gaussian2DFunction.SIGNAL; j < params.length; j+=parametersPerPeak)     params[j]-=background;
  }
  for (int i=0; i < npeaks; i++) {
    if ((int)params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION] == params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION])     params[i * parametersPerPeak + Gaussian2DFunction.X_POSITION]+=0.001;
    if ((int)params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION] == params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION])     params[i * parametersPerPeak + Gaussian2DFunction.Y_POSITION]+=0.001;
  }
  final boolean fitValidation=fitConfig.isFitValidation();
  fitConfig.setFitValidation(false);
  final int maxIterations=fitConfig.getMaxIterations();
  final int maxEvaluations=fitConfig.getMaxFunctionEvaluations();
  fitConfig.setMaxIterations(maxIterations + maxIterations * (npeaks - 1) * ITERATION_INCREASE_FOR_MULTIPLE_PEAKS);
  fitConfig.setMaxFunctionEvaluations(maxEvaluations + maxEvaluations * (npeaks - 1) * EVALUATION_INCREASE_FOR_MULTIPLE_PEAKS);
  gf.setBounds(lower,upper);
  final FitResult fitResult=gf.fit(region,width,height,npeaks,params,true);
  gf.setBounds(null,null);
  fitConfig.setFitValidation(fitValidation);
  fitConfig.setMaxIterations(maxIterations);
  fitConfig.setMaxFunctionEvaluations(maxEvaluations);
  updateError(fitResult);
  PreprocessedPeakResult[] results=null;
  if (fitResult.getStatus() == FitStatus.OK) {
    final double[] fitParams=fitResult.getParameters();
    final double[] initialParams=fitResult.getInitialParameters();
    convertParameters(fitParams);
    results=new PreprocessedPeakResult[npeaks];
    final int flags=fitConfig.getFunctionFlags();
    results[0]=resultFactory.createPreprocessedPeakResult(this.candidateId,0,initialParams,fitParams,getLocalBackground(0,npeaks,fitParams,flags),ResultType.NEW);
    for (int n=neighbourCount + 1; n < npeaks; n++) {
      results[n]=resultFactory.createPreprocessedPeakResult(this.candidateId,n,initialParams,fitParams,getLocalBackground(n,npeaks,fitParams,flags),ResultType.EXISTING);
    }
    for (int n=1; n <= neighbourCount; n++) {
      results[n]=resultFactory.createPreprocessedPeakResult(this.candidateId,n,initialParams,fitParams,getLocalBackground(n,npeaks,fitParams,flags),ResultType.CANDIDATE);
    }
  }
  return resultMulti=createResult(fitResult,results);
}