diff --git a/clients/examples/run_simple_ilastik.py b/clients/examples/run_simple_ilastik.py index de420baaf8246432b1d4e4b0ce0517c052366c0f..123874bfeb7dc9b9dc79f52acf5f05d25df25432 100644 --- a/clients/examples/run_simple_ilastik.py +++ b/clients/examples/run_simple_ilastik.py @@ -1,3 +1,7 @@ +""" +Debug a client request sequence from the same environment as the server +""" + from clients.util import get_client from clients import ilastik_map_objects_simple diff --git a/clients/ilastik_map_objects_simple.py b/clients/ilastik_map_objects_simple.py index 3fae8a873b373ad28fd930e19dd3e58eb931ae5c..f5991068cd4aaa0a5e701267a64aa4d41ddef2a5 100644 --- a/clients/ilastik_map_objects_simple.py +++ b/clients/ilastik_map_objects_simple.py @@ -1,6 +1,17 @@ -from os.path import basename, dirname +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: (int) channel of the input image to process + :return: (str) absolute path where a new object map is written + """ where = dirname(in_abspath) in_file = basename(in_abspath) @@ -9,59 +20,59 @@ def main(request_func, in_abspath, params): ob_ilp = params['object_classifier_path'] channel = params['channel'] - # configure input and output paths - resp = request_func( - 'PUT', + # configure input and output paths + resp = request_func( + 'PUT', '/paths/watch_input', - { - 'path': where, - } - ) - assert resp['status'] == 200, 'Error setting up image directory' + { + '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, - }, - ) + 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, - } + + # 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'] - + return resp['content']['object_map_filepath'] + diff --git a/clients/imagej/adapter.py b/clients/imagej/adapter.py index 1bdd36decc860f64bda54715386034c22886c73f..f09942b7460a925d1d0fabed29ee9d3372a56c1d 100644 --- a/clients/imagej/adapter.py +++ b/clients/imagej/adapter.py @@ -1,3 +1,7 @@ +""" +Functionality needed to run a client request sequence (clients.*.main) in the ImageJ python 2.7 script environment +""" + import httplib import json import urllib @@ -9,6 +13,13 @@ PORT = 6221 uri = 'http://{}:{}/'.format(HOST, PORT) def hit_endpoint(method, endpoint, params=None): + """ + 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 required by client request + :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') @@ -23,10 +34,16 @@ def hit_endpoint(method, endpoint, params=None): content = json.loads(resp_str) except Exception: content = {'str': str(resp_str)} - return {'status': resp.status, 'content': content -} + return {'status': resp.status, 'content': content} -def run_request_sequence(imp, module, params): +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