From da20d13aec691ceea2ac959830fd91206c362c39 Mon Sep 17 00:00:00 2001 From: Jamie Kai <31858695+jwkai@users.noreply.github.com> Date: Thu, 2 Feb 2023 21:57:47 -0800 Subject: [PATCH] Update variable_function.py --- .../tools/variable_function/variable_function.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/alldata/bblab_site/tools/variable_function/variable_function.py b/alldata/bblab_site/tools/variable_function/variable_function.py index 11e6dd2..da0b58f 100644 --- a/alldata/bblab_site/tools/variable_function/variable_function.py +++ b/alldata/bblab_site/tools/variable_function/variable_function.py @@ -84,13 +84,15 @@ def mwu_choose_method(x, y): return "asymptotic" return "exact" - def mannwhitneyu_category(result, category): + def mannwhitneyu_category(result, category, stringify_p): pos = [float(x[-1]) for x in result if category in x[:-1]] pos_median = median(pos) neg = [float(x[-1]) for x in result if category not in x[:-1]] neg_median = median(neg) mwu_method = mwu_choose_method(pos, neg) _, p = stats.mannwhitneyu(pos, neg, alternative='two-sided', method=mwu_method) + if stringify_p: + p = "{:.8f}".format(p) return [len(pos), len(neg), pos_median, neg_median, p, mwu_method] try: @@ -112,7 +114,7 @@ def mannwhitneyu_category(result, category): n-withoutmedian-withmedian-without p-valuep-value-method""") for category in unique_categories: - output = mannwhitneyu_category(result, category) + output = mannwhitneyu_category(result, category, True) out_str += "" + "".join(str(v) for v in [category, *output]) + "" out_str += ("") @@ -123,7 +125,7 @@ def mannwhitneyu_category(result, category): # The following is to print csv style out_str += ("category,n-with,n-without,median-with,median-without,p-value,p-value-method\r\n") for category in unique_categories: - output = mannwhitneyu_category(result, category) + output = mannwhitneyu_category(result, category, False) out_str += ",".join(str(v) for v in [category, *output]) + "\r\n" return (is_download, out_str, "variable_function_output.csv") @@ -131,4 +133,4 @@ def mannwhitneyu_category(result, category): return(False, f"""Error: statistical test encountered an error with the given input.
Common issues are that the input data contains unique categories in each column,
or that not all sample values are identical.

- Error message: {e}""") \ No newline at end of file + Error message: {e}""")