Skip to content
Snippets Groups Projects
Commit 30f37e0b authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Moved client code back into repo; items in imagej directory won't run in server environment

parent e19f8012
No related branches found
No related tags found
No related merge requests found
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
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']
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment