From f73262a56745de17bdb582474e27726cb19bd95e Mon Sep 17 00:00:00 2001 From: Alexander Kabui Date: Tue, 5 Oct 2021 15:09:10 +0300 Subject: add function to process images --- gn3/computations/wgcna.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'gn3/computations') diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py index 0fb56e8..26040d3 100644 --- a/gn3/computations/wgcna.py +++ b/gn3/computations/wgcna.py @@ -4,6 +4,7 @@ import os import json import uuid import subprocess +import base64 from gn3.settings import TMPDIR from gn3.commands import run_cmd @@ -15,6 +16,8 @@ def dump_wgcna_data(request_data: dict): temp_file_path = os.path.join(TMPDIR, filename) + request_data["TMPDIR"] = TMPDIR + with open(temp_file_path, "w") as output_file: json.dump(request_data, output_file) @@ -31,6 +34,16 @@ def stream_cmd_output(socket, cmd: str): line = line.decode("utf-8").rstrip() +def process_image(image_loc: str) ->bytes: + """encode the image""" + + try: + with open(image_loc, "rb") as image_file: + return base64.b64encode(image_file.read()) + except FileNotFoundError as e: + return b"" + + def compose_wgcna_cmd(rscript_path: str, temp_file_path: str): """function to componse wgcna cmd""" # (todo):issue relative paths to abs paths @@ -47,12 +60,18 @@ def call_wgcna_script(rscript_path: str, request_data: dict): run_cmd_results = run_cmd(cmd) + print(run_cmd_results["output"]) + with open(generated_file, "r") as outputfile: + output_file_data = json.load(outputfile) + output_file_data["image_1"] = process_image( + output_file_data["output"]["imageLoc"]) + if run_cmd_results["code"] != 0: return run_cmd_results return { - "data": json.load(outputfile), + "data": output_file_data, **run_cmd_results } except FileNotFoundError: -- cgit v1.2.3