diff options
Diffstat (limited to 'wqflask/wqflask/partial_correlations_views.py')
-rw-r--r-- | wqflask/wqflask/partial_correlations_views.py | 90 |
1 files changed, 73 insertions, 17 deletions
diff --git a/wqflask/wqflask/partial_correlations_views.py b/wqflask/wqflask/partial_correlations_views.py index 41bbe7d7..40c5132b 100644 --- a/wqflask/wqflask/partial_correlations_views.py +++ b/wqflask/wqflask/partial_correlations_views.py @@ -116,8 +116,20 @@ def controls_error(args): "of three control traits"),))} return args -def target_db_error(args): - if not args["target_db"]: +def target_traits_error(args, with_target_traits): + target_traits_present = ( + (args.get("target_traits") is not None) and + (len(args["target_traits"]) > 0)) + if with_target_traits and not target_traits_present: + return { + **args, + "errors": ( + args.get("errors", tuple()) + + (("You must provide at least one target trait"),))} + return args + +def target_db_error(args, with_target_db: bool): + if with_target_db and not args["target_db"]: return { **args, "errors": ( @@ -151,17 +163,24 @@ def criteria_error(args): args.get("errors", tuple()) + ("Invalid return number provided",))} -def errors(args): - return criteria_error(method_error(target_db_error(controls_error( - primary_error(args))))) +def errors(args, with_target_db: bool): + return criteria_error( + method_error( + target_traits_error( + target_db_error( + controls_error(primary_error(args)), + with_target_db), + not with_target_db))) def __classify_args(acc, item): if item[1].startswith("primary_"): return { **acc, - "primary_trait": (acc.get("primary_trait", tuple()) + (item,))} + "primary_trait": (acc.get("primary_trait", tuple()) + (item,))} if item[1].startswith("controls_"): return {**acc, "control_traits": (acc.get("control_traits", tuple()) + (item,))} + if item[1].startswith("targets_"): + return {**acc, "target_traits": (acc.get("target_traits", tuple()) + (item,))} if item[0] == "target_db": return {**acc, "target_db": item[1]} if item[0] == "method": @@ -179,7 +198,10 @@ def __build_args(raw_form, traits): (name[1][8:] for name in args["primary_trait"])], "control_traits": [ item for item in traits if item["trait_name"] in - (name[1][9:] for name in args["control_traits"])] + (name[1][9:] for name in args["control_traits"])], + "target_traits": [ + item for item in traits if item["trait_name"] in + (name[1][8:] for name in args["target_traits"])] } def parse_trait(trait_str): @@ -204,13 +226,31 @@ def render_error(error_message): "partial_correlations/pcorrs_error.html", message = error_message) +def __format_number(num): + if num is None or math.isnan(num): + return "" + if abs(num) <= 1.04E-4: + return f"{num:.2e}" + return f"{num:.5f}" + def handle_200_response(response): - if response["status"] == "success": + if response["status"] == "queued": return redirect( url_for( "poll_partial_correlation_results", command_id=response["results"]), code=303) + if response["status"] == "success": + return render_template( + "partial_correlations/pcorrs_results_with_target_traits.html", + primary = response["results"]["results"]["primary_trait"], + controls = response["results"]["results"]["control_traits"], + pcorrs = sorted( + response["results"]["results"]["correlations"], + key = lambda item: item["partial_corr_p_value"]), + method = response["results"]["results"]["method"], + enumerate = enumerate, + format_number = __format_number) return render_error(response["results"]) def handle_response(response): @@ -227,12 +267,34 @@ def partial_correlations(): parse_trait(trait) for trait in form.get("trait_list").split(";;;")) - if form.get("submit") == "Run Partial Correlations": - args = errors(__build_args(form, traits)) + submit = form.get("submit") + + if submit in ("with_target_pearsons", "with_target_spearmans"): + method = "pearsons" if "pearsons" in submit else "spearmans" + args = { + **errors(__build_args(form, traits), with_target_db=False), + "method": method + } + if len(args.get("errors", [])) == 0: + post_data = { + **args, + "primary_trait": args["primary_trait"][0], + "with_target_db": False + } + return handle_response(requests.post( + url=f"{GN_SERVER_URL}api/correlation/partial", + json=post_data)) + + for error in args["errors"]: + flash(error, "alert-danger") + + if submit == "Run Partial Correlations": + args = errors(__build_args(form, traits), with_target_db=True) if len(args.get("errors", [])) == 0: post_data = { **args, - "primary_trait": args["primary_trait"][0] + "primary_trait": args["primary_trait"][0], + "with_target_db": False } return handle_response(requests.post( url=f"{GN_SERVER_URL}api/correlation/partial", @@ -251,12 +313,6 @@ def partial_correlations(): def process_pcorrs_command_output(result): if result["status"] == "success": - def __format_number(num): - if num is None or math.isnan(num): - return "" - if abs(num) <= 1.04E-4: - return f"{num:.2e}" - return f"{num:.5f}" return render_template( "partial_correlations/pcorrs_results_presentation.html", |