about summary refs log tree commit diff
path: root/wqflask/scripts
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-10-28 11:51:50 +0300
committerFrederick Muriuki Muriithi2022-10-28 15:58:01 +0300
commit0bb49f9873e958cd480f7c9fc20f3a8db6f62003 (patch)
tree1e13625ddb9b581ad16db98ab75619ef1fdacdce /wqflask/scripts
parent2357452ccdcf475bcad3a5da7de43f4d481e296b (diff)
downloadgenenetwork2-0bb49f9873e958cd480f7c9fc20f3a8db6f62003.tar.gz
Refactor: run correlation computation externally
* wqflask/scripts/corr_compute.py: move correlation computation to
  external script.
* wqflask/wqflask/templates/loading_corrs.html: Provide UI to display
  while computation is still not complete.
* wqflask/wqflask/views.py: Dispatch correlation computation to
  external script and display appropriate UI for each valid state of
  the computation.
Diffstat (limited to 'wqflask/scripts')
-rw-r--r--wqflask/scripts/corr_compute.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/wqflask/scripts/corr_compute.py b/wqflask/scripts/corr_compute.py
new file mode 100644
index 00000000..c98a66a4
--- /dev/null
+++ b/wqflask/scripts/corr_compute.py
@@ -0,0 +1,78 @@
+"""Compute the correlations."""
+
+import sys
+import json
+import pickle
+import pathlib
+import datetime
+
+from flask import g
+
+from wqflask import app
+from wqflask.user_session import UserSession
+from wqflask.correlation.show_corr_results import set_template_vars
+from wqflask.correlation.correlation_gn3_api import compute_correlation
+
+class UserSessionSimulator():
+
+    def __init__(self, user_id):
+        self._user_id = user_id
+
+    @property
+    def user_id(self):
+        return self._user_id
+
+error_types = {
+    "WrongCorrelationType": "Wrong Correlation Type",
+    "CalledProcessError": "Called Process Error"
+}
+
+def e_time():
+    return datetime.datetime.utcnow().isoformat()
+
+def compute(form):
+    import subprocess
+    from gn3.settings import CORRELATION_COMMAND
+    try:
+        correlation_results = compute_correlation(form, compute_all=True)
+    except Exception as exc:
+        return {
+            "error-type": error_types[type(exc).__name__],
+            "error-message": exc.args[0]
+        }
+
+    return set_template_vars(form, correlation_results)
+
+if __name__ == "__main__":
+    ARGS_COUNT = 3
+    if len(sys.argv) < ARGS_COUNT:
+        print(f"{e_time()}: You need to pass the file with the picked form",
+              file=sys.stderr)
+        sys.exit(1)
+
+    if len(sys.argv) > ARGS_COUNT:
+        print(f"{e_time()}: Unknown arguments {sys.argv[ARGS_COUNT:]}",
+              file=sys.stderr)
+        sys.exit(1)
+
+    filepath = pathlib.Path(sys.argv[1])
+    if not filepath.exists():
+        print(f"File not found '{filepath}'", file=sys.stderr)
+        sys.exit(2)
+
+    with open(filepath, "rb") as pfile:
+        form = pickle.Unpickler(pfile).load()
+
+    with app.app_context():
+        g.user_session = UserSessionSimulator(sys.argv[2])
+        results = compute(form)
+
+    print(json.dumps(results), file=sys.stdout)
+
+    if "error-type" in results:
+        print(
+            f"{results['error-type']}: {results['error-message']}",
+            file=sys.stderr)
+        sys.exit(3)
+
+    sys.exit(0)