[Demonstration 1]

[Buggy Method]
public synchronized Double[] getYTextLabelLocations(int scale) {
    return mYTextLabels.get(scale).keySet().toArray(new Double[mYTextLabels.size()]);
}

[Buggy Hunk]
return mYTextLabels.get(scale).keySet().toArray(new Double[mYTextLabels.size()]);

[Patch]
return mYTextLabels.get(scale).keySet().toArray(new Double[mYTextLabels.get(scale).size()]);

[Demonstration 2]

[Buggy Method]
@Override
public void apply(WritableImage im) {
    if (this.referenceImage == null) {
        throw new ReferenceImageRequiredException("LocalBackgroundEstimationFilter requires a reference image.");
    }
    edu.stanford.cfuller.imageanalysistools.image.Histogram h = new edu.stanford.cfuller.imageanalysistools.image.Histogram(this.referenceImage);
    int numPixelsInBox = boxSize * boxSize;
    ImageCoordinate ic = this.referenceImage.getDimensionSizes();
    ImageCoordinate icnew = ImageCoordinate.createCoordXYZCT(ic.get(ImageCoordinate.X) + 2 * boxSize, ic.get(ImageCoordinate.Y) + 2 * boxSize, ic.get(ImageCoordinate.Z), ic.get(ImageCoordinate.C), ic.get(ImageCoordinate.T));
    WritableImage padded = ImageFactory.createWritable(icnew, -1.0f);
    float maxValue = 0;
    for (ImageCoordinate i : this.referenceImage) {
        icnew.quickSet(ImageCoordinate.X, i.quickGet(ImageCoordinate.X) + boxSize);
        icnew.quickSet(ImageCoordinate.Y, i.quickGet(ImageCoordinate.Y) + boxSize);
        icnew.quickSet(ImageCoordinate.Z, i.quickGet(ImageCoordinate.Z));
        icnew.quickSet(ImageCoordinate.C, i.quickGet(ImageCoordinate.C));
        icnew.quickSet(ImageCoordinate.T, i.quickGet(ImageCoordinate.T));
        padded.setValue(icnew, this.referenceImage.getValue(i));
        if (this.referenceImage.getValue(i) > maxValue)
            maxValue = this.referenceImage.getValue(i);
    }
    RealVector overallCounts = new org.apache.commons.math3.linear.ArrayRealVector(h.getMaxValue() + 1);
    RealMatrix countsByRow = new org.apache.commons.math3.linear.Array2DRowRealMatrix(2 * boxSize + 1, h.getMaxValue() + 1);
    //loop over columns
    for (int i = boxSize; i < im.getDimensionSizes().quickGet(ImageCoordinate.X) + boxSize; i++) {
        overallCounts.mapMultiplyToSelf(0.0);
        double[] overallCounts_a = overallCounts.toArray();
        countsByRow = countsByRow.scalarMultiply(0.0);
        double[][] countsByRow_a = countsByRow.getData();
        int countsByRow_rowZero_pointer = 0;
        for (int m = i - boxSize; m <= i + boxSize; m++) {
            for (int n = 0; n < 2 * boxSize + 1; n++) {
                icnew.quickSet(ImageCoordinate.X, m);
                icnew.quickSet(ImageCoordinate.Y, n);
                int value = (int) padded.getValue(icnew);
                if (value == -1)
                    continue;
                overallCounts_a[value]++;
                countsByRow_a[(n + countsByRow_rowZero_pointer) % countsByRow.getRowDimension()][value]++;
            }
        }
        int currMedian = 0;
        int runningSum = 0;
        int k = 0;
        while (runningSum < (numPixelsInBox >> 1)) {
            runningSum += overallCounts_a[k++];
        }
        runningSum -= overallCounts_a[k - 1];
        currMedian = k - 1;
        icnew.quickSet(ImageCoordinate.X, i - boxSize);
        icnew.quickSet(ImageCoordinate.Y, 0);
        im.setValue(icnew, currMedian);
        int num_rows = countsByRow.getRowDimension();
        for (int j = boxSize + 1; j < im.getDimensionSizes().quickGet(ImageCoordinate.Y) + boxSize; j++) {
            double[] toRemove = countsByRow_a[(countsByRow_rowZero_pointer) % num_rows];
            for (int oc_counter = 0; oc_counter < overallCounts_a.length; oc_counter++) {
                overallCounts_a[oc_counter] -= toRemove[oc_counter];
            }
            for (int c = 0; c < toRemove.length; c++) {
                if (c < currMedian) {
                    runningSum -= toRemove[c];
                }
                countsByRow_a[countsByRow_rowZero_pointer % num_rows][c] *= 0.0;
            }
            countsByRow_rowZero_pointer++;
            for (int c = i - boxSize; c <= i + boxSize; c++) {
                icnew.quickSet(ImageCoordinate.X, c);
                icnew.quickSet(ImageCoordinate.Y, j + boxSize);
                int value = (int) padded.getValue(icnew);
                if (value == -1)
                    continue;
                countsByRow_a[(countsByRow_rowZero_pointer + num_rows - 1) % num_rows][value] += 1;
                overallCounts_a[value]++;
                if (value < currMedian) {
                    runningSum++;
                }
            }
            //case 1: runningSum > half of box
            if (runningSum > (numPixelsInBox >> 1)) {
                k = currMedian - 1;
                while (runningSum > (numPixelsInBox >> 1)) {
                    runningSum -= overallCounts_a[k--];
                }
                currMedian = k + 1;
            } else if (runningSum < (numPixelsInBox >> 1)) {
                // case 2: runningSum < half of box
                k = currMedian;
                while (runningSum < (numPixelsInBox >> 1)) {
                    runningSum += overallCounts_a[k++];
                }
                currMedian = k - 1;
                runningSum -= overallCounts_a[k - 1];
            }
            //cast 3: spot on, do nothing
            icnew.quickSet(ImageCoordinate.X, i - boxSize);
            icnew.quickSet(ImageCoordinate.Y, j - boxSize);
            im.setValue(icnew, currMedian);
        }
    }
    icnew.recycle();
}

[Buggy Hunk]
if (value == -1) continue;

[Patch]
if (value < 0) continue;

[Demonstration 3]

[Buggy Method]
public double[] minimize(List<DifferentiableFunction> functions, double[] initial, boolean verbose, Callback iterCallbackFunction) {
    Random rand = new Random(0);
    double[] guess = a.copy(initial);
    double[] sqrGradSum = new double[guess.length];
    a.addi(sqrGradSum, delta);
    final double r = 2.0 * eta * regConstant;
    for (int epoch = 0; epoch < epochs; ++epoch) {
        double epochValSum = 0.0;
        double[] epochGradSum = new double[guess.length];
        for (int funcIndex : a.shuffle(a.enumerate(0, functions.size()), rand)) {
            DifferentiableFunction func = functions.get(funcIndex);
            Pair<Double, double[]> valAndGrad = func.calculate(guess);
            epochValSum += valAndGrad.getFirst();
            double[] grad = valAndGrad.getSecond();
            a.combi(epochGradSum, 1.0, grad, 1.0);
            double[] sqrGrad = a.sqr(grad);
            a.combi(sqrGradSum, 1.0, sqrGrad, 1.0);
            for (int i = 0; i < guess.length; ++i) {
                double s = Math.sqrt(sqrGradSum[i]);
                guess[i] = (s * guess[i] - eta * grad[i]) / (r + s);
            }
        }
        if (verbose)
            System.out.println(String.format("[AdaGradMinimizer.minimize] Epoch %d ended with value %.6f", epoch, epochValSum + regConstant * a.innerProd(guess, guess)));
        if (iterCallbackFunction != null)
            iterCallbackFunction.callback(guess, epoch, epochValSum, epochGradSum);
    }
    return guess;
}

[Buggy Hunk]
final double r = 2.0 * eta * regConstant;

[Patch]
final double r = eta * regConstant;