<commit_before>"use strict";

function dataCallback(opts) {
    return function() {
        d3.select("#logx").property("checked", false);
        d3.select("#reg").property("xlogged", false);
        d3.select("#logy").property("checked", false);
        d3.select("#reg").property("ylogged", false);
        loadData(this.value, d3.select("#diagnostic").property("value"), opts);
    };
}

function diagnosticCallback(opts) {
    return function() {
        plot(d3.selectAll("#reg circle").data(),
             d3.select("#diagnostic").property("value"), opts);
    };
}

function logCallback(opts) {
    return function() {
        plot(d3.selectAll("#reg circle").data(),
             d3.select("#diagnostic").property("value"), opts, true);
    };
}

// name is a filename in the data/ directory, without file extension.
// data files should have no headers. First column will be treated as
// x, second column as y.
function loadData(name, diagnostic, opts) {
    d3.text("data/" + name + ".csv", makePlotCallback(diagnostic, opts));
}

function makePlotCallback(diagnostic, opts) {
    return function(text) {
        var data = d3.csv.parseRows(text, function(row) {
            return [+row[0], +row[1]];
        });

        plot(data, diagnostic, opts, true);
    };
}

// if reset is true, recalculate the x and y range
function plot(data, diagnostic, opts, reset) {
    reset = reset || false;

    var reg = d3.select("#reg");
    var resid = d3.select("#resid");

    if (d3.select("#logx").property("checked") && !reg.property("xlogged")) {
        data = data.map(function(row) {
            return [Math.log(row[0]), row[1]];
        });
        reg.property("xlogged", true);
    } else if (!d3.select("#logx").property("checked")
               && reg.property("xlogged")) {
        data = data.map(function(row) {
            return [Math.exp(row[0]), row[1]];
        });
        reg.property("xlogged", false);
    }
    if (d3.select("#logy").property("checked") && !reg.property("ylogged")) {
        data = data.map(function(row) {
            return [row[0], Math.log(row[1])];
        });
        reg.property("ylogged", true);
    } else if (!d3.select("#logy").property("checked")
               && reg.property("ylogged")) {
        data = data.map(function(row) {
            return [row[0], Math.exp(row[1])];
        });
        reg.property("ylogged", false);
    }
    var xrange, yrange;

    if (reset || reg.property("xrange") === undefined) {
        xrange = d3.extent(data, function(row) { return row[0]; });
        yrange = d3.extent(data, function(row) { return row[1]; });

        // Only allow log scale if all observations have the same sign
        if (yrange[0] * yrange[1] <= 0 && !reg.property("ylogged")) {
            d3.select("#logy").property("disabled", true);
            d3.select("#logylab").attr("class", "disable");
        } else {
            d3.select("#logy").property("disabled", false);
            d3.select("#logylab").attr("class", "");
        }
        if (xrange[0] * xrange[1] <= 0 && !reg.property("xlogged")) {
            d3.select("#logx").property("disabled", true);
            d3.select("#logxlab").attr("class", "disable");
        } else {
            d3.select("#logx").property("disabled", false);
            d3.select("#logxlab").attr("class", "");
        }

        var xwidth = xrange[1] - xrange[0];
        var ywidth = yrange[1] - yrange[0];

        xrange = [xrange[0] - 0.1*xwidth, xrange[1] + 0.1*xwidth];
        yrange = [yrange[1] + 0.1*ywidth, yrange[0] - 0.1*ywidth];

        reg.property("xrange", xrange);
        reg.property("yrange", yrange);
    } else {
        xrange = reg.property("xrange");
        yrange = reg.property("yrange");
    }

    regressionPlots(d3.select("#reg"), d3.select("#resid"), data, opts,
                    xrange, yrange, diagnostic, "X", "Y",
                   {r2: d3.select("#r2"),
                    fdf2: d3.select("#fdf2"), fstat: d3.select("#fstat"),
                    p: d3.select("#pval"), pdir: d3.select("#pdir")});
}
<commit_msg>Don't allow log transform if anything is negative, even if all the data is<commit_after>"use strict";

function dataCallback(opts) {
    return function() {
        d3.select("#logx").property("checked", false);
        d3.select("#reg").property("xlogged", false);
        d3.select("#logy").property("checked", false);
        d3.select("#reg").property("ylogged", false);
        loadData(this.value, d3.select("#diagnostic").property("value"), opts);
    };
}

function diagnosticCallback(opts) {
    return function() {
        plot(d3.selectAll("#reg circle").data(),
             d3.select("#diagnostic").property("value"), opts);
    };
}

function logCallback(opts) {
    return function() {
        plot(d3.selectAll("#reg circle").data(),
             d3.select("#diagnostic").property("value"), opts, true);
    };
}

// name is a filename in the data/ directory, without file extension.
// data files should have no headers. First column will be treated as
// x, second column as y.
function loadData(name, diagnostic, opts) {
    d3.text("data/" + name + ".csv", makePlotCallback(diagnostic, opts));
}

function makePlotCallback(diagnostic, opts) {
    return function(text) {
        var data = d3.csv.parseRows(text, function(row) {
            return [+row[0], +row[1]];
        });

        plot(data, diagnostic, opts, true);
    };
}

// if reset is true, recalculate the x and y range
function plot(data, diagnostic, opts, reset) {
    reset = reset || false;

    var reg = d3.select("#reg");
    var resid = d3.select("#resid");

    if (d3.select("#logx").property("checked") && !reg.property("xlogged")) {
        data = data.map(function(row) {
            return [Math.log(row[0]), row[1]];
        });
        reg.property("xlogged", true);
    } else if (!d3.select("#logx").property("checked")
               && reg.property("xlogged")) {
        data = data.map(function(row) {
            return [Math.exp(row[0]), row[1]];
        });
        reg.property("xlogged", false);
    }
    if (d3.select("#logy").property("checked") && !reg.property("ylogged")) {
        data = data.map(function(row) {
            return [row[0], Math.log(row[1])];
        });
        reg.property("ylogged", true);
    } else if (!d3.select("#logy").property("checked")
               && reg.property("ylogged")) {
        data = data.map(function(row) {
            return [row[0], Math.exp(row[1])];
        });
        reg.property("ylogged", false);
    }
    var xrange, yrange;

    if (reset || reg.property("xrange") === undefined) {
        xrange = d3.extent(data, function(row) { return row[0]; });
        yrange = d3.extent(data, function(row) { return row[1]; });

        // Only allow log scale if all observations are positive
        if (yrange[0] <= 0 && !reg.property("ylogged")) {
            d3.select("#logy").property("disabled", true);
            d3.select("#logylab").attr("class", "disable");
        } else {
            d3.select("#logy").property("disabled", false);
            d3.select("#logylab").attr("class", "");
        }
        if (xrange[0] <= 0 && !reg.property("xlogged")) {
            d3.select("#logx").property("disabled", true);
            d3.select("#logxlab").attr("class", "disable");
        } else {
            d3.select("#logx").property("disabled", false);
            d3.select("#logxlab").attr("class", "");
        }

        var xwidth = xrange[1] - xrange[0];
        var ywidth = yrange[1] - yrange[0];

        xrange = [xrange[0] - 0.1*xwidth, xrange[1] + 0.1*xwidth];
        yrange = [yrange[1] + 0.1*ywidth, yrange[0] - 0.1*ywidth];

        reg.property("xrange", xrange);
        reg.property("yrange", yrange);
    } else {
        xrange = reg.property("xrange");
        yrange = reg.property("yrange");
    }

    regressionPlots(d3.select("#reg"), d3.select("#resid"), data, opts,
                    xrange, yrange, diagnostic, "X", "Y",
                   {r2: d3.select("#r2"),
                    fdf2: d3.select("#fdf2"), fstat: d3.select("#fstat"),
                    p: d3.select("#pval"), pdir: d3.select("#pdir")});
}

{'fraction_non_alphanumeric': 0.19324966492019008, 'fraction_numerical': 0.017302302912148165, 'mean_word_length': 2.510692899914457, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '6659788', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2585, 'n_words': 515}