From 30f37e0b335c34d181592863678a9eb29154c77b Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 3 Nov 2023 15:26:02 +0100 Subject: [PATCH] Moved client code back into repo; items in imagej directory won't run in server environment --- clients/__init__.py | 0 clients/examples/__init__.py | 0 clients/examples/run_simple_ilastik.py | 19 ++++++++ clients/ilastik_map_objects_simple.py | 67 ++++++++++++++++++++++++++ clients/imagej/__init__.py | 0 clients/imagej/adapter.py | 32 ++++++++++++ clients/util.py | 18 +++++++ 7 files changed, 136 insertions(+) create mode 100644 clients/__init__.py create mode 100644 clients/examples/__init__.py create mode 100644 clients/examples/run_simple_ilastik.py create mode 100644 clients/ilastik_map_objects_simple.py create mode 100644 clients/imagej/__init__.py create mode 100644 clients/imagej/adapter.py create mode 100644 clients/util.py diff --git a/clients/__init__.py b/clients/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/clients/examples/__init__.py b/clients/examples/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/clients/examples/run_simple_ilastik.py b/clients/examples/run_simple_ilastik.py new file mode 100644 index 00000000..de420baa --- /dev/null +++ b/clients/examples/run_simple_ilastik.py @@ -0,0 +1,19 @@ +from clients.util import get_client +from clients import ilastik_map_objects_simple + +if __name__ == '__main__': + + img_abspath = 'C:\\Users\\rhodes\\projects\\proj0004-marine-photoactivation\\data\\exp0021\\B2\\20230320-153317\\Selection\\Selection--W0000--P0001-T0001.czi' + + params = { + 'pixel_classifier_path': 'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0019/px-02.ilp', + 'object_classifier_path': 'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0019/obj-06.ilp', + 'channel': 4, + } + + outfile = ilastik_map_objects_simple.main( + get_client(port=6221), + img_abspath, + params + ) + print(f'Success: {outfile}') \ No newline at end of file diff --git a/clients/ilastik_map_objects_simple.py b/clients/ilastik_map_objects_simple.py new file mode 100644 index 00000000..3fae8a87 --- /dev/null +++ b/clients/ilastik_map_objects_simple.py @@ -0,0 +1,67 @@ +from os.path import basename, dirname + +def main(request_func, in_abspath, params): + + where = dirname(in_abspath) + in_file = basename(in_abspath) + + px_ilp = params['pixel_classifier_path'] + ob_ilp = params['object_classifier_path'] + channel = params['channel'] + + # configure input and output paths + resp = request_func( + 'PUT', + '/paths/watch_input', + { + 'path': where, + } + ) + assert resp['status'] == 200, 'Error setting up image directory' + + resp = request_func( + 'PUT', + '/paths/watch_output', + { + 'path': where, + } + ) + assert resp['status'] == 200, 'Error setting up image directory' + + # load pixel classifier + resp = request_func( + 'PUT', + '/ilastik/px/load/', + { + 'project_file': px_ilp, + 'duplicate': False, + }, + ) + assert resp['status'], 'Error loading classifier: ' + px_ilp + id_px_mod = resp['content']['model_id'] + + # load object classifier + resp = request_func( + 'PUT', '/ilastik/pxmap_to_obj/load/', + { + 'project_file': ob_ilp, + 'duplicate': False, + }, + ) + assert resp['status'] == 200, 'Error loading object classifier: ' + {ob_ilp} + id_ob_mod = resp['content']['model_id'] + + # run inference + resp = request_func( + 'PUT', + '/ilastik/pixel_then_object_classification/infer', + { + 'px_model_id': id_px_mod, + 'ob_model_id': id_ob_mod, + 'input_filename': in_file, + 'channel': channel, + } + ) + assert resp['status'] == 200, 'Error calling workfow' + return resp['content']['object_map_filepath'] + diff --git a/clients/imagej/__init__.py b/clients/imagej/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/clients/imagej/adapter.py b/clients/imagej/adapter.py new file mode 100644 index 00000000..1bdd36de --- /dev/null +++ b/clients/imagej/adapter.py @@ -0,0 +1,32 @@ +import httplib +import json +import urllib + +from ij import ImagePlus + +HOST = '127.0.0.1' +PORT = 6221 +uri = 'http://{}:{}/'.format(HOST, PORT) + +def hit_endpoint(method, endpoint, params=None): + connection = httplib.HTTPConnection(HOST, PORT) + if not method in ['GET', 'PUT']: + raise Exception('Can only handle GET and PUT requests') + if params: + url = endpoint + '?' + urllib.urlencode(params) + else: + url = endpoint + connection.request(method, url) + resp = connection.getresponse() + resp_str = resp.read() + try: + content = json.loads(resp_str) + except Exception: + content = {'str': str(resp_str)} + return {'status': resp.status, 'content': content +} + +def run_request_sequence(imp, module, params): + in_path = imp.getProp('Location') + out_path = func(hit_endpoint, in_path, params) + return ImagePlus(out_path) \ No newline at end of file diff --git a/clients/util.py b/clients/util.py new file mode 100644 index 00000000..2038bbcf --- /dev/null +++ b/clients/util.py @@ -0,0 +1,18 @@ +import requests + +def get_client(host='127.0.0.1', port=8000): + """Return a client wrapper for testing in same python 3.9 environment as server""" + uri = f'http://{host}:{port}' + + def hit_endpoint(method, endpoint, params=None): + if method == 'GET': + resp = requests.get(uri + endpoint) + elif method == 'PUT': + resp = requests.put(uri + endpoint, params=params) + else: + raise Exception('Can only handle GET and PUT requests') + if resp.status_code != 200: + return {'status': resp.status_code, 'content': resp.text} + else: + return {'status': resp.status_code, 'content': resp.json()} + return hit_endpoint \ No newline at end of file -- GitLab