<commit_before>package edu.harvard.mcb.leschziner.analyze;

import java.util.Collection;
import java.util.Iterator;

import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.CvSize;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc;

import edu.harvard.mcb.leschziner.core.Particle;

public class ClassAverager {

    /**
     * Averages a set of particles to generate an average, summing each pixel
     * location across all particles, then dividing by the number of images.
     * Average does segregate colorchannels. Returns null if the particle set is
     * empty
     * 
     * @param particles
     *            : to be averaged
     * @return A single average particle
     */
    public static Particle average(Collection<Particle> particles) {
        Iterator<Particle> iter = particles.iterator();
        int particleCount = particles.size();

        if (iter.hasNext()) {
            Particle particle = iter.next();
            // Get the particle dimensions
            int size = particle.getSize();

            // Allocate a sum buffer with greater depth
            IplImage sumBuffer = IplImage.create(new CvSize(size, size), 32,
                                                 particle.getImage()
                                                         .nChannels());

            for (; iter.hasNext(); particle = iter.next()) {
                // Accumulate the images
                opencv_imgproc.cvAcc(particle.getImage(), sumBuffer, null);
            }

            IplImage mean = IplImage.createCompatible(particle.getImage());

            // Rescale
            opencv_core.cvScale(sumBuffer, mean, 1.0 / particleCount, 0);

            Particle average = new Particle(mean);
            return average;
        } else {
            return null;
        }<commit_msg>fixing regression bug on accumulation and averaging<commit_after>package edu.harvard.mcb.leschziner.analyze;

import java.util.Collection;
import java.util.Iterator;

import edu.harvard.mcb.leschziner.core.Particle;

public class ClassAverager {
    private static final int RED_OFFSET   = 0;
    private static final int GREEN_OFFSET = 1;
    private static final int BLUE_OFFSET  = 2;

    /**
     * Averages a set of particles to generate an average, summing each pixel
     * location across all particles, then dividing by the number of images.
     * Average does segregate colorchannels. Returns null if the particle set is
     * empty
     * 
     * @param particles
     *            : to be averaged
     * @return A single average particle
     */
    public static Particle average(Collection<Particle> particles) {
        Iterator<Particle> iter = particles.iterator();
        int particleCount = particles.size();

        if (iter.hasNext()) {
            Particle particle = iter.next();
            // Get the particle dimensions
            int size = particle.getSize();

            // Allocate a sum buffer
            long[] sums = new long[3 * size * size];
            for (; iter.hasNext(); particle = iter.next()) {
                // Get the pixels (RGB) from the image
                int i = 0;
                for (int x = 0; x < size; x++) {
                    for (int y = 0; y < size; y++) {
                        sums[i + RED_OFFSET] += particle.getPixelRed(x, y);
                        sums[i + GREEN_OFFSET] += particle.getPixelGreen(x, y);
                        sums[i + BLUE_OFFSET] += particle.getPixelBlue(x, y);
                        i += 3;
                    }
                }
            }

            Particle average = particle.createCompatible();
            int i = 0;
            for (int x = 0; x < size; x++) {
                for (int y = 0; y < size; y++) {
                    average.setPixelRed(x,
                                        y,
                                        (int) (sums[i + RED_OFFSET] / particleCount));
                    average.setPixelGreen(x,
                                          y,
                                          (int) (sums[i + GREEN_OFFSET] / particleCount));
                    average.setPixelBlue(x,
                                         y,
                                         (int) (sums[i + BLUE_OFFSET] / particleCount));
                    i += 3;
                }
            }

            return average;
        } else {
            return null;
        }
{'fraction_non_alphanumeric': 0.09105075690115762, 'fraction_numerical': 0.003784505788067676, 'mean_word_length': 1.5227400336889387, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1690204', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1130, 'n_words': 330}