<commit_before>// sort.js
// This contains the logic for retrieving the layout-aware and layout-ignore
// sort attribute stats

var app = app || {}; // jshint ignore:line

(function (hex) { // jshint ignore:line
    //'use strict';

    var computingText = 'Computing statistics now...';

    function finalCompare(a, b) {

        // After the other compares, do these last compares for all sorts.
        // Compare clumpiness,
        // then alphabetically by name if all else fails.

        // By clumpiness
        if(layers[a].clumpiness > layers[b].clumpiness) {
            // a has a higher clumpiness score, so put it first.
            return -1;
        } else if(layers[b].clumpiness > layers[a].clumpiness) {
            // b has a higher clumpiness score. Put it first instead.
            return 1;
        } else if(isNaN(layers[b].clumpiness) && !isNaN(layers[a].clumpiness)) {
            // a has a clumpiness score and b doesn't, so put a first
            return -1;
        } else if(!isNaN(layers[b].clumpiness) && isNaN(layers[a].clumpiness)) {
            // b has a clumpiness score and a doesn't, so put b first.
            return 1;
        }			

        // Use lexicographic ordering on the name
        return a.localeCompare(b);
    }

    function variableCompare(a, b, v) {

        // Compare the variable: 'v'

        if(layers[a][v] < layers[b][v]) {
            // a has a lower value, so put it first.
            return -1;
        } else if(layers[b][v] < layers[a][v]) {
            // b has a lower value. Put it first instead.
            return 1;
        } else if(isNaN(layers[b][v]) && !isNaN(layers[a][v])) {
            // a has a value and b doesn't, so put a first
            return -1;
        } else if(!isNaN(layers[b][v]) && isNaN(layers[a][v])) {
            // b has a value and a doesn't, so put b first.
            return 1;
        }
        return 0
    }

    function pValueCompare(a, b) {

        // Compare p_values, then do the final compare
        
        var result = variableCompare(a, b, 'p_value');
        if (result !== 0) return result;

        return finalCompare(a, b);
    }

    function differentialCompare(a, b) {

        // Compare  differential values, then do the final compare
        
        var result = variableCompare(a, b, 'Differential');
        if (result !== 0) return result;

        return finalCompare(a, b);
    }

    function correlationCompare(a, b, pos) {

        // Compare correlation sign, then p-value, then do the final compare

        // Compare correlation signs with positives first or negatives first,
        // depending on pos, where true indicates positive, false negative
        var aSign = layers[a].correlation < 0 ? -1 : 1,
            bSign = layers[b].correlation < 0 ? -1 : 1;

        if (aSign < bSign && pos) {
            return 1;
        } else if (bSign < aSign && pos) {
            return -1
        } else if (aSign < bSign && !pos) {
            return -1;
        } else if (bSign < aSign && !pos) {
            return 1
        }

        // Compare p_values
        result = variableCompare(a, b, 'p_value');
        if (result !== 0) return result;

        // The final compare
        return finalCompare(a, b);
    }

    function positiveCorrelationCompare(a, b) {

        return correlationCompare(a, b, true);
    }

    function negativeCorrelationCompare(a, b) {

        return correlationCompare(a, b, false);
    }

    sort_layers = function  () {

        // Given an array of layer names, sort the array in place as we want
        // layers to appear to the user.

        /*
        The compare order of sorts:
        - sort options are optional and one of:
            - ignore layout: by p-value in ascending order
            - layout-aware positive: by positive correlation sign first, then p-value
            - layout-aware negative: by negative correlation sign first, then p-value
        - by clumpiness/density
        - alphabetically

        The compare functions return the usual values:
            <0 if A belongs before B
            >0 if A belongs after B
            0 if equal or their order doesn't matter
        */

        var type_value = Session.get('sort').type,
            layer_array = Session.get('sortedLayers');

        if (layer_array.length === 0) return;

        if (type_value == "layout-aware-positive") {
            layer_array.sort(positiveCorrelationCompare);

        } else if (type_value == "layout-aware-negative") {
            layer_array.sort(negativeCorrelationCompare);

        } else if (type_value == "p_value") {
            layer_array.sort(pValueCompare);

        } else if (type_value == "Differential") {
            layer_array.sort(differentialCompare);

        } else {
            // The default sort, by density/clumpiness
            layer_array.sort(finalCompare);

            // If we don't have a first layer by now assign the one with the
            // highest density score as the first layer.
            if (_.isUndefined(Session.get('first_layer'))
                && !_.isNull(layer_array[0])) {
                Session.set('first_layer', layer_array[0]);
            } else {
            
                // move the 'First' attribute to the top
                var first = Session.get('first_layer');
                layer_array.splice(layer_array.indexOf(first), 1);
                layer_array.unshift(first);
            }
        }
    }

    clearStats = function () {

        // Clear stats for each layer before updating with the new stats
        for (var layer_name in layers) {
            delete layers[layer_name].clumpiness;
            delete layers[layer_name].p_value;
            delete layers[layer_name].correlation;
            delete layers[layer_name].Differential;
         }
    }

    updateUi = function (type, text, focus_attr, opts) {

        // If we were computing dynamic stats,
        // include the elapsed time in the banner
        var elapsed = '';
        if (opts && opts.hasOwnProperty('startDate')
            && $("#banner").text() === computingText) {
            var endDate = new Date;
            elapsed = ' ('
                + Math.ceil((endDate.getTime() - opts.startDate.getTime()) / 1000)
                + ' secs)';
        }

        // Set the sort properties and update the UI to sort them
        if (type === 'default') {
            Session.set('sort', ctx.defaultSort());
            text = Session.get('sort').text;<commit_msg>fixed the sort bug where it was not actually sorting<commit_after>// sort.js
// This contains the logic for retrieving the layout-aware and layout-ignore
// sort attribute stats

var app = app || {}; // jshint ignore:line

(function (hex) { // jshint ignore:line
    //'use strict';

    var computingText = 'Computing statistics now...';

    function finalCompare(a, b) {

        // After the other compares, do these last compares for all sorts.
        // Compare clumpiness,
        // then alphabetically by name if all else fails.

        // By clumpiness
        if(layers[a].clumpiness > layers[b].clumpiness) {
            // a has a higher clumpiness score, so put it first.
            return -1;
        } else if(layers[b].clumpiness > layers[a].clumpiness) {
            // b has a higher clumpiness score. Put it first instead.
            return 1;
        } else if(isNaN(layers[b].clumpiness) && !isNaN(layers[a].clumpiness)) {
            // a has a clumpiness score and b doesn't, so put a first
            return -1;
        } else if(!isNaN(layers[b].clumpiness) && isNaN(layers[a].clumpiness)) {
            // b has a clumpiness score and a doesn't, so put b first.
            return 1;
        }			

        // Use lexicographic ordering on the name
        return a.localeCompare(b);
    }

    function variableCompare(a, b, v) {

        // Compare the variable: 'v'

        if(layers[a][v] < layers[b][v]) {
            // a has a lower value, so put it first.
            return -1;
        } else if(layers[b][v] < layers[a][v]) {
            // b has a lower value. Put it first instead.
            return 1;
        } else if(isNaN(layers[b][v]) && !isNaN(layers[a][v])) {
            // a has a value and b doesn't, so put a first
            return -1;
        } else if(!isNaN(layers[b][v]) && isNaN(layers[a][v])) {
            // b has a value and a doesn't, so put b first.
            return 1;
        }
        return 0
    }

    function pValueCompare(a, b) {

        // Compare p_values, then do the final compare
        
        var result = variableCompare(a, b, 'p_value');
        if (result !== 0) return result;

        return finalCompare(a, b);
    }

    function differentialCompare(a, b) {

        // Compare  differential values, then do the final compare
        
        var result = variableCompare(a, b, 'Differential');
        if (result !== 0) return result;

        return finalCompare(a, b);
    }

    function correlationCompare(a, b, pos) {

        // Compare correlation sign, then p-value, then do the final compare

        // Compare correlation signs with positives first or negatives first,
        // depending on pos, where true indicates positive, false negative
        var aSign = layers[a].correlation < 0 ? -1 : 1,
            bSign = layers[b].correlation < 0 ? -1 : 1;

        if (aSign < bSign && pos) {
            return 1;
        } else if (bSign < aSign && pos) {
            return -1
        } else if (aSign < bSign && !pos) {
            return -1;
        } else if (bSign < aSign && !pos) {
            return 1
        }

        // Compare p_values
        result = variableCompare(a, b, 'p_value');
        if (result !== 0) return result;

        // The final compare
        return finalCompare(a, b);
    }

    function positiveCorrelationCompare(a, b) {

        return correlationCompare(a, b, true);
    }

    function negativeCorrelationCompare(a, b) {

        return correlationCompare(a, b, false);
    }

    sort_layers = function  () {

        // Given an array of layer names, sort the array in place as we want
        // layers to appear to the user.

        /*
        The compare order of sorts:
        - sort options are optional and one of:
            - ignore layout: by p-value in ascending order
            - layout-aware positive: by positive correlation sign first, then p-value
            - layout-aware negative: by negative correlation sign first, then p-value
        - by clumpiness/density
        - alphabetically

        The compare functions return the usual values:
            <0 if A belongs before B
            >0 if A belongs after B
            0 if equal or their order doesn't matter
        */

        var type_value = Session.get('sort').type,
            layer_array = Session.get('sortedLayers');

        if (layer_array.length === 0) return;

        if (type_value == "layout-aware-positive") {
            layer_array.sort(positiveCorrelationCompare);

        } else if (type_value == "layout-aware-negative") {
            layer_array.sort(negativeCorrelationCompare);

        } else if (type_value == "p_value") {
            layer_array.sort(pValueCompare);

        } else if (type_value == "Differential") {
            layer_array.sort(differentialCompare);

        } else {
            // The default sort, by density/clumpiness
            layer_array.sort(finalCompare);

            // If we don't have a first layer by now assign the one with the
            // highest density score as the first layer.
            if (_.isUndefined(Session.get('first_layer'))
                && !_.isNull(layer_array[0])) {
                Session.set('first_layer', layer_array[0]);
            } else {
            
                // move the 'First' attribute to the top
                var first = Session.get('first_layer');
                layer_array.splice(layer_array.indexOf(first), 1);
                layer_array.unshift(first);
            }
            Session.set('sortedLayers', layer_array);
        }
    }

    clearStats = function () {

        // Clear stats for each layer before updating with the new stats
        for (var layer_name in layers) {
            delete layers[layer_name].clumpiness;
            delete layers[layer_name].p_value;
            delete layers[layer_name].correlation;
            delete layers[layer_name].Differential;
         }
    }

    updateUi = function (type, text, focus_attr, opts) {

        // If we were computing dynamic stats,
        // include the elapsed time in the banner
        var elapsed = '';
        if (opts && opts.hasOwnProperty('startDate')
            && $("#banner").text() === computingText) {
            var endDate = new Date;
            elapsed = ' ('
                + Math.ceil((endDate.getTime() - opts.startDate.getTime()) / 1000)
                + ' secs)';
        }

        // Set the sort properties and update the UI to sort them
        if (type === 'default') {
            Session.set('sort', ctx.defaultSort());
            text = Session.get('sort').text;
{'fraction_non_alphanumeric': 0.10795842500569001, 'fraction_numerical': 0.0050072073439041045, 'mean_word_length': 1.8005098789037604, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 9, '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': '2827740', 'n_tokens_mistral': 3749, 'n_tokens_neox': 3545, 'n_words': 1186}