From 5fb161ed9accc49f3d36671809a017ed21657ed0 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 20 Sep 2012 16:22:34 -0500 Subject: Created a class in coffeescript for stats and got several to display dynamically --- wqflask/wqflask/show_trait/DataEditingPage.py | 2 +- wqflask/wqflask/static/new/javascript/stats.coffee | 46 +++++++++++ wqflask/wqflask/static/new/javascript/stats.js | 81 +++++++++++++++++++ .../new/javascript/trait_data_and_analysis.coffee | 93 +++++++++++----------- .../new/javascript/trait_data_and_analysis.js | 87 +++++++++----------- .../wqflask/templates/trait_data_and_analysis.html | 8 +- 6 files changed, 215 insertions(+), 102 deletions(-) create mode 100644 wqflask/wqflask/static/new/javascript/stats.coffee create mode 100644 wqflask/wqflask/static/new/javascript/stats.js diff --git a/wqflask/wqflask/show_trait/DataEditingPage.py b/wqflask/wqflask/show_trait/DataEditingPage.py index 8ee1d9c8..32ecc732 100755 --- a/wqflask/wqflask/show_trait/DataEditingPage.py +++ b/wqflask/wqflask/show_trait/DataEditingPage.py @@ -1814,7 +1814,7 @@ class DataEditingPage(templatePage): print("No strain %s, let's create it now" % strainName) strain = webqtlCaseData.webqtlCaseData(strainName) print("zyt - strainNameOrig:", strainNameOrig) - + if strains == 'primary': strain.this_id = "Primary_" + str(counter) diff --git a/wqflask/wqflask/static/new/javascript/stats.coffee b/wqflask/wqflask/static/new/javascript/stats.coffee new file mode 100644 index 00000000..677dc258 --- /dev/null +++ b/wqflask/wqflask/static/new/javascript/stats.coffee @@ -0,0 +1,46 @@ +class Stats + constructor: (@the_values) -> + + add_value: (value) -> + @the_values.push(value) + + n_of_samples: -> + return @the_values.length + + sum: -> + total = 0 + total += value for value in @the_values + return total + + mean: -> + return @sum() / @n_of_samples() + + median: -> + is_odd = @the_values.length % 2 + median_position = Math.floor(@the_values.length / 2) + the_values_sorted = @the_values.sort((a,b) -> return a - b) + if is_odd + return the_values_sorted[median_position] + else + return (the_values_sorted[median_position] + + the_values_sorted[median_position - 1]) / 2 + + std_dev: -> + sum = 0 + for value in @the_values + step_a = Math.pow(value - @mean(), 2) + sum += step_a + step_b = sum / @the_values.length + return Math.sqrt(step_b) + + std_error: -> + return @std_dev() / Math.sqrt(@n_of_samples()) + + +bxd_only = new Stats([3, 5, 7, 8]) +console.log("[red] bxd_only mean:", bxd_only.mean()) +console.log("[green] bxd_only median:", bxd_only.median()) +console.log("[purple] bxd_only std_dev:", bxd_only.std_dev()) +console.log("[magenta] bxd_only std_error:", bxd_only.std_error()) + +window.Stats = Stats \ No newline at end of file diff --git a/wqflask/wqflask/static/new/javascript/stats.js b/wqflask/wqflask/static/new/javascript/stats.js new file mode 100644 index 00000000..f95d03d4 --- /dev/null +++ b/wqflask/wqflask/static/new/javascript/stats.js @@ -0,0 +1,81 @@ +// Generated by CoffeeScript 1.3.3 +(function() { + var Stats, bxd_only; + + Stats = (function() { + + function Stats(the_values) { + this.the_values = the_values; + } + + Stats.prototype.add_value = function(value) { + return this.the_values.push(value); + }; + + Stats.prototype.n_of_samples = function() { + return this.the_values.length; + }; + + Stats.prototype.sum = function() { + var total, value, _i, _len, _ref; + total = 0; + _ref = this.the_values; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + value = _ref[_i]; + total += value; + } + return total; + }; + + Stats.prototype.mean = function() { + return this.sum() / this.n_of_samples(); + }; + + Stats.prototype.median = function() { + var is_odd, median_position, the_values_sorted; + is_odd = this.the_values.length % 2; + median_position = Math.floor(this.the_values.length / 2); + the_values_sorted = this.the_values.sort(function(a, b) { + return a - b; + }); + if (is_odd) { + return the_values_sorted[median_position]; + } else { + return (the_values_sorted[median_position] + the_values_sorted[median_position - 1]) / 2; + } + }; + + Stats.prototype.std_dev = function() { + var step_a, step_b, sum, value, _i, _len, _ref; + sum = 0; + _ref = this.the_values; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + value = _ref[_i]; + step_a = Math.pow(value - this.mean(), 2); + sum += step_a; + } + step_b = sum / this.the_values.length; + return Math.sqrt(step_b); + }; + + Stats.prototype.std_error = function() { + return this.std_dev() / Math.sqrt(this.n_of_samples()); + }; + + return Stats; + + })(); + + bxd_only = new Stats([3, 5, 7, 8]); + + console.log("[red] bxd_only mean:", bxd_only.mean()); + + console.log("[green] bxd_only median:", bxd_only.median()); + + console.log("[purple] bxd_only std_dev:", bxd_only.std_dev()); + + console.log("[magenta] bxd_only std_error:", bxd_only.std_error()); + + window.Stats = Stats; + +}).call(this); diff --git a/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.coffee b/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.coffee index 2952bef5..c44582db 100644 --- a/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.coffee +++ b/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.coffee @@ -19,54 +19,56 @@ $ -> $(".stats_mdp").change(stats_mdp_change) - update_stat_values = (the_values)-> + change_stats_value = (category, value_type, the_value)-> + id = "#" + process_id(category, value_type) + in_box = $(id).html + + current_value = parseFloat($(in_box)).toFixed(2) + + if the_value != current_value + $(id).html(the_value).effect("highlight") + + update_stat_values = (sample_sets)-> for category in ['primary_only', 'other_only', 'all_cases'] - # Mean - id = "#" + process_id(category, "mean") - total = 0 - total += value for value in the_values[category] - the_mean = total / the_values[category].length - the_mean = the_mean.toFixed(2) - in_box = $(id).html - - current_mean = parseFloat($(in_box)).toFixed(2) - - if the_mean != current_mean - $(id).html(the_mean).effect("highlight") - # Number of samples - n_of_samples = the_values[category].length + n_of_samples = sample_sets[category].n_of_samples() id = "#" + process_id(category, "n_of_samples") current_n_of_samples = $(id).html() if n_of_samples != current_n_of_samples $(id).html(n_of_samples).effect("highlight") + + # Mean + #id = "#" + process_id(category, "mean") + + the_mean = sample_sets[category].mean() + the_mean = the_mean.toFixed(2) + change_stats_value(category, "mean", the_mean) + #in_box = $(id).html + #current_mean = parseFloat($(in_box)).toFixed(2) + + #if the_mean != current_mean + # $(id).html(the_mean).effect("highlight") # Median id = "#" + process_id(category, "median") + the_median = sample_sets[category].median() + the_median = the_median.toFixed(2) + in_box = $(id).html + + current_median = parseFloat($(in_box)).toFixed(2) - is_odd = the_values[category].length % 2 - median_position = Math.floor(the_values[category].length / 2) - - # sort numerically - the_values_sorted = the_values[category].sort((a, b) -> return a - b) - if is_odd - the_median = the_values_sorted[median_position] - else - the_median = (the_values_sorted[median_position] + - the_values_sorted[median_position + 1]) / 2 - current_median = $(id).html() if the_median != current_median $(id).html(the_median).effect("highlight") # Todo: Compare stat values to genenetwork.org current code / sample vs. population # Standard deviation sum = 0 - for value in the_values[category] + for value in sample_sets[category] step_a = Math.pow(value - the_mean, 2) sum += step_a - step_b = sum / the_values[category].length + step_b = sum / sample_sets[category].length sd = Math.sqrt(step_b) sd = sd.toFixed(2) @@ -76,35 +78,32 @@ $ -> $(id).html(sd).effect("highlight") - edit_data_change = -> - the_values = - primary_only: [] - other_only: [] - all_cases: [] - console.log("at beginning:", the_values) + edit_data_change = -> + sample_sets = + primary_only: new Stats([]) + other_only: new Stats([]) + all_cases: new Stats([]) + + console.log("at beginning:", sample_sets) values = $('#value_table').find(".edit_strain_value") - #console.log("values are:", values) + for value in values real_value = $(value).val() - #console.log("parent is:", $(value).closest("tr")) row = $(value).closest("tr") - console.log("row is:", row) - console.log("row[0].id is:", row[0].id) category = row[0].id checkbox = $(row).find(".edit_strain_checkbox") checked = $(checkbox).attr('checked') - if not checked - console.log("Not checked") - continue - if is_number(real_value) and real_value != "" + + if checked and is_number(real_value) and real_value != "" real_value = parseFloat(real_value) if _(category).startsWith("Primary") - the_values.primary_only.push(real_value) + sample_sets.primary_only.add_value(real_value) else if _(category).startsWith("Other") - the_values.other_only.push(real_value) - the_values.all_cases.push(real_value) - console.log("towards end:", the_values) - update_stat_values(the_values) + sample_sets.other_only.add_value(real_value) + sample_sets.all_cases.add_value(real_value) + console.log("towards end:", sample_sets) + update_stat_values(sample_sets) + make_table = -> header = " " diff --git a/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.js b/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.js index 04f43822..b4eed350 100644 --- a/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.js +++ b/wqflask/wqflask/static/new/javascript/trait_data_and_analysis.js @@ -10,7 +10,7 @@ }; $(function() { - var edit_data_change, hide_tabs, make_table, on_corr_method_change, process_id, show_hide_outliers, stats_mdp_change, update_stat_values; + var change_stats_value, edit_data_change, hide_tabs, make_table, on_corr_method_change, process_id, show_hide_outliers, stats_mdp_change, update_stat_values; hide_tabs = function(start) { var x, _i, _results; _results = []; @@ -27,55 +27,46 @@ return $("#stats_tabs" + selected).show(); }; $(".stats_mdp").change(stats_mdp_change); - update_stat_values = function(the_values) { - var category, current_mean, current_median, current_n_of_samples, current_sd, id, in_box, is_odd, median_position, n_of_samples, sd, step_a, step_b, sum, the_mean, the_median, the_values_sorted, total, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results; + change_stats_value = function(category, value_type, the_value) { + var current_value, id, in_box; + id = "#" + process_id(category, value_type); + in_box = $(id).html; + current_value = parseFloat($(in_box)).toFixed(2); + if (the_value !== current_value) { + return $(id).html(the_value).effect("highlight"); + } + }; + update_stat_values = function(sample_sets) { + var category, current_median, current_n_of_samples, current_sd, id, in_box, n_of_samples, sd, step_a, step_b, sum, the_mean, the_median, value, _i, _j, _len, _len1, _ref, _ref1, _results; _ref = ['primary_only', 'other_only', 'all_cases']; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { category = _ref[_i]; - id = "#" + process_id(category, "mean"); - total = 0; - _ref1 = the_values[category]; - for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { - value = _ref1[_j]; - total += value; - } - the_mean = total / the_values[category].length; - the_mean = the_mean.toFixed(2); - in_box = $(id).html; - current_mean = parseFloat($(in_box)).toFixed(2); - if (the_mean !== current_mean) { - $(id).html(the_mean).effect("highlight"); - } - n_of_samples = the_values[category].length; + n_of_samples = sample_sets[category].n_of_samples(); id = "#" + process_id(category, "n_of_samples"); current_n_of_samples = $(id).html(); if (n_of_samples !== current_n_of_samples) { $(id).html(n_of_samples).effect("highlight"); } + the_mean = sample_sets[category].mean(); + the_mean = the_mean.toFixed(2); + change_stats_value(category, "mean", the_mean); id = "#" + process_id(category, "median"); - is_odd = the_values[category].length % 2; - median_position = Math.floor(the_values[category].length / 2); - the_values_sorted = the_values[category].sort(function(a, b) { - return a - b; - }); - if (is_odd) { - the_median = the_values_sorted[median_position]; - } else { - the_median = (the_values_sorted[median_position] + the_values_sorted[median_position + 1]) / 2; - } - current_median = $(id).html(); + the_median = sample_sets[category].median(); + the_median = the_median.toFixed(2); + in_box = $(id).html; + current_median = parseFloat($(in_box)).toFixed(2); if (the_median !== current_median) { $(id).html(the_median).effect("highlight"); } sum = 0; - _ref2 = the_values[category]; - for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { - value = _ref2[_k]; + _ref1 = sample_sets[category]; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + value = _ref1[_j]; step_a = Math.pow(value - the_mean, 2); sum += step_a; } - step_b = sum / the_values[category].length; + step_b = sum / sample_sets[category].length; sd = Math.sqrt(step_b); sd = sd.toFixed(2); id = "#" + process_id(category, "sd"); @@ -89,39 +80,33 @@ return _results; }; edit_data_change = function() { - var category, checkbox, checked, real_value, row, the_values, value, values, _i, _len; - the_values = { - primary_only: [], - other_only: [], - all_cases: [] + var category, checkbox, checked, real_value, row, sample_sets, value, values, _i, _len; + sample_sets = { + primary_only: new Stats([]), + other_only: new Stats([]), + all_cases: new Stats([]) }; - console.log("at beginning:", the_values); + console.log("at beginning:", sample_sets); values = $('#value_table').find(".edit_strain_value"); for (_i = 0, _len = values.length; _i < _len; _i++) { value = values[_i]; real_value = $(value).val(); row = $(value).closest("tr"); - console.log("row is:", row); - console.log("row[0].id is:", row[0].id); category = row[0].id; checkbox = $(row).find(".edit_strain_checkbox"); checked = $(checkbox).attr('checked'); - if (!checked) { - console.log("Not checked"); - continue; - } - if (is_number(real_value) && real_value !== "") { + if (checked && is_number(real_value) && real_value !== "") { real_value = parseFloat(real_value); if (_(category).startsWith("Primary")) { - the_values.primary_only.push(real_value); + sample_sets.primary_only.add_value(real_value); } else if (_(category).startsWith("Other")) { - the_values.other_only.push(real_value); + sample_sets.other_only.add_value(real_value); } - the_values.all_cases.push(real_value); + sample_sets.all_cases.add_value(real_value); } } - console.log("towards end:", the_values); - return update_stat_values(the_values); + console.log("towards end:", sample_sets); + return update_stat_values(sample_sets); }; make_table = function() { var header, key, row, row_line, rows, table, the_id, the_rows, value, _i, _len, _ref, _ref1; diff --git a/wqflask/wqflask/templates/trait_data_and_analysis.html b/wqflask/wqflask/templates/trait_data_and_analysis.html index 16f2e2be..1f33dee5 100644 --- a/wqflask/wqflask/templates/trait_data_and_analysis.html +++ b/wqflask/wqflask/templates/trait_data_and_analysis.html @@ -1310,13 +1310,15 @@ --> + js_data = {{ js_data | safe }} + --> + + - + -- cgit v1.2.3