From 2a1ef087fbc80e2e08e921581150e9e809facbb9 Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Thu, 26 Sep 2024 15:07:52 +0200 Subject: [PATCH] Removed demo client for now, since these are not covered by unit testing --- model_server/clients/__init__.py | 0 model_server/clients/ilastik_map_objects.py | 80 -------------------- model_server/clients/imagej/__init__.py | 0 model_server/clients/imagej/adapter.py | 82 --------------------- model_server/clients/util.py | 18 ----- 5 files changed, 180 deletions(-) delete mode 100644 model_server/clients/__init__.py delete mode 100644 model_server/clients/ilastik_map_objects.py delete mode 100644 model_server/clients/imagej/__init__.py delete mode 100644 model_server/clients/imagej/adapter.py delete mode 100644 model_server/clients/util.py diff --git a/model_server/clients/__init__.py b/model_server/clients/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/model_server/clients/ilastik_map_objects.py b/model_server/clients/ilastik_map_objects.py deleted file mode 100644 index c395a31f..00000000 --- a/model_server/clients/ilastik_map_objects.py +++ /dev/null @@ -1,80 +0,0 @@ -from os.path import basename, dirname - - -def main(request_func, in_abspath, params): - """ - Execute a sequence of client requests that load ilastik pixel and object classifiers, then infer on an image file - :param request_func: (func) function that implements HTTP client, dependent on which environment request are called from - :param in_abspath: (str) absolute path to image file to infer - :param params: - pixel_classifier_path: (str) absolute path to ilastik project file that defines a pixel classifier - object_classifier_path: (str) absolute path to ilastik project file that defines an object classifier - channel (optional): (int) channel of the input image to process, use all channels if not specified - :return: (str) absolute path where a new object map is written - """ - - where = dirname(in_abspath) - in_file = basename(in_abspath) - - px_ilp = params['pixel_classifier_path'] - ob_ilp = params['object_classifier_path'] - channel = params.get('channel', None) - mip = params.get('mip', False) - - # 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/seg/load/', - body={ - '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/', - body={ - '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, - 'mip': mip, - } - ) - assert resp['status'] == 200, 'Error calling workfow' - return resp['content']['object_map_filepath'] - diff --git a/model_server/clients/imagej/__init__.py b/model_server/clients/imagej/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/model_server/clients/imagej/adapter.py b/model_server/clients/imagej/adapter.py deleted file mode 100644 index 237aa7ab..00000000 --- a/model_server/clients/imagej/adapter.py +++ /dev/null @@ -1,82 +0,0 @@ -""" -Functionality needed to run a client request sequence (clients.*.main) in the ImageJ model_server 2.7 script environment -""" - -import httplib -import json -import urllib - -from ij import IJ -from ij import ImagePlus - -HOST = '127.0.0.1' -PORT = 6221 -uri = 'http://{}:{}/'.format(HOST, PORT) - -def hit_endpoint(method, endpoint, params=None, body=None, drop_none=True): - """ - Python 2.7 implementation of HTTP client - :param method: (str) either 'GET' or 'PUT' - :param endpoint: (str) endpoint of HTTP request - :param params: (dict) of parameters that are embedded in client request URL - :param body: (dict) of parameters that JSON-encoded and attached as payload in request - :param drop_none: (bool) remove (presumably optional) parameters with value equal to None - :return: (dict) of response status and content, formatted as dict if request is successful - """ - connection = httplib.HTTPConnection(HOST, PORT) - if not method in ['GET', 'PUT']: - raise Exception('Can only handle GET and PUT requests') - k_pop = [] - if drop_none and params is not None: - for k, v in params.items(): - if v is None: - k_pop.append(k) - for ki in k_pop: - params.pop(ki) - - if params: - url = endpoint + '?' + urllib.urlencode(params) - else: - url = endpoint - connection.request(method, url, body=json.dumps(body)) - 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 verify_server(popup=True): - try: - resp = hit_endpoint('GET', '/') - except Exception as e: - print(e) - msg = 'Could not find server at: ' + uri - IJ.log(msg) - if popup: - IJ.error(msg) - raise e - return False - if resp['status'] != 200: - msg = 'Unknown error verifying server at: ' + uri - if popup: - IJ.error(msg) - raise Exception(msg) - return False - else: - IJ.log('Verified server is online at: ' + uri) - return True - -def run_request_sequence(imp, func, params): - """ - Execute a sequence of client requests in the ImageJ scripting environment - :param imp: (ij.ImagePlus) input image - :param func: (func) function that implements client request sequence - :param params: (dict) parameters specific to client request - :return: (ij.ImagePlus) output image - """ - 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/model_server/clients/util.py b/model_server/clients/util.py deleted file mode 100644 index 423e7380..00000000 --- a/model_server/clients/util.py +++ /dev/null @@ -1,18 +0,0 @@ -import requests - -def get_client(host='127.0.0.1', port=8000): - """Return a client wrapper for testing in same model_server 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