<commit_before>/*
 * Small image analysis-program for counting cells in microscope images.
 *
 * <NAME>
 * <<EMAIL>>
 */

// {{{ numericSort
function numericSort(a, b) {
    return a - b;
}
// }}}

// {{{ getCursorPosition
function getCursorPosition(e, gCanvasElement) {
    var x;
    var y;
    if (e.pageX !== undefined && e.pageY !== undefined) {
        x = e.pageX;
        y = e.pageY;
    }
    else {
        x = e.clientX + document.body.scrollLeft +
        document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop +
        document.documentElement.scrollTop;
    }
    x -= gCanvasElement.offsetLeft;
    y -= gCanvasElement.offsetTop;

    return {x: x, y: y	};
}
// }}}

// {{{ getConnectedComponents
function getConnectedComponents (imageData) {
    var pixels = imageData.data,
        height = imageData.height,
        width = imageData.width,
        index, label, i, h, w; // Temporary variables used later

    // Create map of which groups are at which position
    var groupMap = new Array(pixels.length/4),
        nextLabel = 1,
        linked = {}; // re-group from → to

    // {{{ Internal helper functions
    function hw2index(h, w) {
        if (h<0 || h> height || w<0 || w>width) {
            return undefined;
        }
        return h*width + w;
    }

    function getGroup(h, w) {
        var index = hw2index(h, w);
        if (index) {
            return groupMap[index];
        }
        return undefined;
    }
    // }}}

    // Loop throuth the image, just looking at the red channel (assumes b/w image)
    for(h=0; h<height; h++) {
        for(w=0; w<width; w++) {
            index = hw2index(h, w);

            // Skip background
            if (pixels[index*4] > 128) {
                continue;
            }

            // Check neighbours; north and west
            var allNeighbours = [
                getGroup(h-1, w+1), // North-East
                getGroup(h-1, w), // North
                getGroup(h-1, w-1), // North-West
                getGroup(h,   w-1) // West
            ];
            var neighbours = [];
            for(i=0; i<allNeighbours.length; i++) {
                if (allNeighbours[i] === undefined || neighbours.indexOf(allNeighbours[i]) !== -1) {
                    continue;
                }

                neighbours.push(allNeighbours[i]);
            }

            if (neighbours.length === 0) {
                // We're in something, but have no neighbours → new area!
                groupMap[index] = nextLabel;
                linked[nextLabel] = [];
                nextLabel += 1;
            } else {
                // Sort neighbour list, so it's easy to pick the smallest label.
                neighbours = neighbours.sort(numericSort);

                // We're in something adjacent to something else
                groupMap[index] = neighbours[0];

                // Discover neighbours' neighbours
                var allNeighbours = neighbours;
                for (i=0; i<neighbours.length; i++) {
                    allNeighbours = _.union(allNeighbours, linked[neighbours[i]]);
                }

                // Update all neighbours' with this + eachothers' neighbours
                for (i=0; i<neighbours.length; i++) {
                    linked[neighbours[i]] = _.union(linked[neighbours[i]], allNeighbours);
                }
            }
        }
    }

    // Create label remap table, so we can look up labels that should be re-mapped to other ones.
    var labelLookupTable = {};
    for(label=1; label<nextLabel; label++) {
        var targetLabel = linked[label].sort(numericSort)[0];
        if (targetLabel && label !== targetLabel) {
            labelLookupTable[label] = targetLabel;
        }
    }

    // Iterate through groupMap and find re-mappable labels + create reverse
    // index of where pixels are...
    var reverseIndex = {};
    for(i=0; i<groupMap.length; i++) {
        label = groupMap[i];

        if (label === undefined) {
            continue;<commit_msg>Removed unnecessary union-operation in hot loop.<commit_after>/*
 * Small image analysis-program for counting cells in microscope images.
 *
 * <NAME>
 * <<EMAIL>>
 */

// {{{ numericSort
function numericSort(a, b) {
    return a - b;
}
// }}}

// {{{ getCursorPosition
function getCursorPosition(e, gCanvasElement) {
    var x;
    var y;
    if (e.pageX !== undefined && e.pageY !== undefined) {
        x = e.pageX;
        y = e.pageY;
    }
    else {
        x = e.clientX + document.body.scrollLeft +
        document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop +
        document.documentElement.scrollTop;
    }
    x -= gCanvasElement.offsetLeft;
    y -= gCanvasElement.offsetTop;

    return {x: x, y: y	};
}
// }}}

// {{{ getConnectedComponents
function getConnectedComponents (imageData) {
    var pixels = imageData.data,
        height = imageData.height,
        width = imageData.width,
        index, label, i, h, w; // Temporary variables used later

    // Create map of which groups are at which position
    var groupMap = new Array(pixels.length/4),
        nextLabel = 1,
        linked = {}; // re-group from → to

    // {{{ Internal helper functions
    function hw2index(h, w) {
        if (h<0 || h> height || w<0 || w>width) {
            return undefined;
        }
        return h*width + w;
    }

    function getGroup(h, w) {
        var index = hw2index(h, w);
        if (index) {
            return groupMap[index];
        }
        return undefined;
    }
    // }}}

    // Loop throuth the image, just looking at the red channel (assumes b/w image)
    for(h=0; h<height; h++) {
        for(w=0; w<width; w++) {
            index = hw2index(h, w);

            // Skip background
            if (pixels[index*4] > 128) {
                continue;
            }

            // Check neighbours; north and west
            var allNeighbours = [
                getGroup(h-1, w+1), // North-East
                getGroup(h-1, w), // North
                getGroup(h-1, w-1), // North-West
                getGroup(h,   w-1) // West
            ];
            var neighbours = [];
            for(i=0; i<allNeighbours.length; i++) {
                if (allNeighbours[i] === undefined || neighbours.indexOf(allNeighbours[i]) !== -1) {
                    continue;
                }

                neighbours.push(allNeighbours[i]);
            }

            if (neighbours.length === 0) {
                // We're in something, but have no neighbours → new area!
                groupMap[index] = nextLabel;
                linked[nextLabel] = [];
                nextLabel += 1;
            } else {
                // Sort neighbour list, so it's easy to pick the smallest label.
                neighbours = neighbours.sort(numericSort);

                // We're in something adjacent to something else
                groupMap[index] = neighbours[0];

                // Discover neighbours' neighbours
                var allNeighbours = neighbours;
                for (i=0; i<neighbours.length; i++) {
                    allNeighbours = _.union(allNeighbours, linked[neighbours[i]]);
                }

                // Update all neighbours' with this + eachothers' neighbours
                for (i=0; i<neighbours.length; i++) {
                    linked[neighbours[i]] = allNeighbours;
                }
            }
        }
    }

    // Create label remap table, so we can look up labels that should be re-mapped to other ones.
    var labelLookupTable = {};
    for(label=1; label<nextLabel; label++) {
        var targetLabel = linked[label].sort(numericSort)[0];
        if (targetLabel && label !== targetLabel) {
            labelLookupTable[label] = targetLabel;
        }
    }

    // Iterate through groupMap and find re-mappable labels + create reverse
    // index of where pixels are...
    var reverseIndex = {};
    for(i=0; i<groupMap.length; i++) {
        label = groupMap[i];

        if (label === undefined) {
            continue;
{'fraction_non_alphanumeric': 0.12303891290920321, 'fraction_numerical': 0.007164916615194565, 'mean_word_length': 1.6474820143884892, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '689539', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2286, 'n_words': 617}