Skip to content

Commit

Permalink
Update variable_function.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkai authored Feb 3, 2023
1 parent 3a08f72 commit da20d13
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions alldata/bblab_site/tools/variable_function/variable_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -112,7 +114,7 @@ def mannwhitneyu_category(result, category):
<th>n-without</th><th>median-with</th><th>median-without</th>
<th>p-value</th><th>p-value-method</th></tr></thead><tbody>""")
for category in unique_categories:
output = mannwhitneyu_category(result, category)
output = mannwhitneyu_category(result, category, True)
out_str += "<tr><td>" + "</td><td>".join(str(v) for v in [category, *output]) + "</td></tr>"
out_str += ("</tbody></table></body></div>")

Expand All @@ -123,12 +125,12 @@ 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")
except ValueError as e:
return(False, f"""<b><span style=\"color:red;\">Error:</span></b> statistical test encountered an error with the given input. <br/>
Common issues are that the input data contains unique categories in each column, <br/>
or that not all sample values are identical. <br/> <br/>
Error message: {e}""")
Error message: {e}""")

0 comments on commit da20d13

Please sign in to comment.