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

Moved ilastik configuration into its own file

parent 6ce61a64
No related branches found
No related tags found
No related merge requests found
from pathlib import Path from pathlib import Path
root = Path('c:/Users/rhodes/projects/proj0015-model-server/resources') root = Path.home() / 'model-server' / 'resources'
# paths = {
# 'logs': root / 'logs' / 'session',
# 'inbound_images': root / 'images' / 'inbound',
# 'outbound_images': root / 'images' / 'outbound',
# 'ilastik_projects': root / 'ilastik',
# }
subdirectories = { subdirectories = {
'logs': 'logs/session', 'logs': 'logs',
'inbound_images': 'images/inbound', 'inbound_images': 'images/inbound',
'outbound_images': 'images/outbound', 'outbound_images': 'images/outbound',
'ilastik_projects': 'ilastik',
} }
\ No newline at end of file
from pathlib import Path
paths = {
'project_files': Path.home() / 'model-server' / 'ilastik'
}
\ No newline at end of file
...@@ -11,11 +11,6 @@ czifile = { ...@@ -11,11 +11,6 @@ czifile = {
'z': 1, 'z': 1,
} }
# ilastik = {
# 'pixel_classifier': root / 'testdata' / 'ilastik' / 'demo_px.ilp',
# 'object_classifier': root / 'testdata' / 'ilastik' / 'demo_obj.ilp',
# }
ilastik = { ilastik = {
'pixel_classifier': 'demo_px.ilp', 'pixel_classifier': 'demo_px.ilp',
'object_classifier': 'demo_obj.ilp', 'object_classifier': 'demo_obj.ilp',
......
import os import os
import pathlib
import numpy as np import numpy as np
import vigra import vigra
import conf.defaults import conf.ilastik
from model_server.image import GenericImageDataAccessor, InMemoryDataAccessor from model_server.image import GenericImageDataAccessor, InMemoryDataAccessor
from model_server.model import ImageToImageModel, ParameterExpectedError from model_server.model import ImageToImageModel, ParameterExpectedError
...@@ -13,9 +12,10 @@ class IlastikImageToImageModel(ImageToImageModel): ...@@ -13,9 +12,10 @@ class IlastikImageToImageModel(ImageToImageModel):
def __init__(self, params, autoload=True): def __init__(self, params, autoload=True):
self.project_file = str(params['project_file']) self.project_file = str(params['project_file'])
self.project_file_abspath = pathlib.Path( pap = conf.ilastik.paths['project_files'] / self.project_file
conf.defaults.paths['ilastik_projects'] / self.project_file, self.project_file_abspath = pap
) if not pap.exists():
raise FileNotFoundError(f'Project file does not exist: {pap}')
if 'project_file' not in params or not self.project_file_abspath.exists(): if 'project_file' not in params or not self.project_file_abspath.exists():
raise ParameterExpectedError('Ilastik model expects a project (*.ilp) file') raise ParameterExpectedError('Ilastik model expects a project (*.ilp) file')
......
...@@ -31,10 +31,11 @@ class Session(object): ...@@ -31,10 +31,11 @@ class Session(object):
self.models = {} # model_id : model object self.models = {} # model_id : model object
self.manifest = [] # paths to data as well as other metadata from each inference run self.manifest = [] # paths to data as well as other metadata from each inference run
self.paths = self.make_paths(root) self.paths = self.make_paths(root)
self.session_id = self.create_session_id(self.paths['logs']) # self.session_id = self.create_session_id(self.paths['logs'])
self.session_log = self.paths['logs'] / f'{self.session_id}.log' # self.session_id = self.create_session_id(self.paths)
self.session_log = self.paths['logs'] / f'session.log'
self.log_event('Initialized session') self.log_event('Initialized session')
self.manifest_json = self.paths['logs'] / f'{self.session_id}-manifest.json' self.manifest_json = self.paths['logs'] / f'manifest.json'
open(self.manifest_json, 'w').close() # instantiate empty json file open(self.manifest_json, 'w').close() # instantiate empty json file
def get_paths(self): def get_paths(self):
...@@ -47,12 +48,12 @@ class Session(object): ...@@ -47,12 +48,12 @@ class Session(object):
:param root: absolute path to top-level directory :param root: absolute path to top-level directory
:return: dictionary of session paths :return: dictionary of session paths
""" """
paths = {}
if root is None: if root is None:
root_path = Path(conf.defaults.root) root_path = Path(conf.defaults.root)
else: else:
root_path = Path(root) root_path = Path(root)
sid = Session.create_session_id(root_path) sid = Session.create_session_id(root_path)
paths = {'root': root_path}
for pk in ['inbound_images', 'outbound_images', 'logs']: for pk in ['inbound_images', 'outbound_images', 'logs']:
pa = root_path / sid / conf.defaults.subdirectories[pk] pa = root_path / sid / conf.defaults.subdirectories[pk]
paths[pk] = pa paths[pk] = pa
...@@ -69,7 +70,7 @@ class Session(object): ...@@ -69,7 +70,7 @@ class Session(object):
""" """
yyyymmdd = strftime('%Y%m%d', localtime()) yyyymmdd = strftime('%Y%m%d', localtime())
idx = 0 idx = 0
while os.path.exists(look_where / f'{yyyymmdd}-{idx:04d}.log'): while os.path.exists(look_where / f'{yyyymmdd}-{idx:04d}'):
idx += 1 idx += 1
return f'{yyyymmdd}-{idx:04d}' return f'{yyyymmdd}-{idx:04d}'
......
...@@ -21,6 +21,17 @@ class TestServerBaseClass(unittest.TestCase): ...@@ -21,6 +21,17 @@ class TestServerBaseClass(unittest.TestCase):
self.uri = f'http://{host}:{port}/' self.uri = f'http://{host}:{port}/'
self.server_process.start() self.server_process.start()
def copy_input_file_to_server(self):
from shutil import copyfile
resp = requests.get(self.uri + 'paths')
pa = resp.json()['inbound_images']
outpath = Path(pa) / czifile['filename']
copyfile(
czifile['path'],
outpath
)
def tearDown(self) -> None: def tearDown(self) -> None:
self.server_process.terminate() self.server_process.terminate()
...@@ -89,17 +100,6 @@ class TestApiFromAutomatedClient(TestServerBaseClass): ...@@ -89,17 +100,6 @@ class TestApiFromAutomatedClient(TestServerBaseClass):
) )
self.assertEqual(resp.status_code, 409, resp.content.decode()) self.assertEqual(resp.status_code, 409, resp.content.decode())
def copy_input_file_to_server(self):
from shutil import copyfile
resp = requests.get(self.uri + 'paths')
pa = resp.json()['inbound_images']
outpath = Path(pa) / czifile['filename']
copyfile(
czifile['path'],
outpath
)
def test_i2i_dummy_inference_by_api(self): def test_i2i_dummy_inference_by_api(self):
model_id = self.test_load_dummy_model() model_id = self.test_load_dummy_model()
self.copy_input_file_to_server() self.copy_input_file_to_server()
......
...@@ -139,7 +139,7 @@ class TestIlastikOverApi(TestServerBaseClass): ...@@ -139,7 +139,7 @@ class TestIlastikOverApi(TestServerBaseClass):
self.assertEqual(rj[model_id]['class'], 'IlastikObjectClassifierModel') self.assertEqual(rj[model_id]['class'], 'IlastikObjectClassifierModel')
def test_ilastik_infer_pixel_probability(self): def test_ilastik_infer_pixel_probability(self):
TestServerBaseClass.copy_input_file_to_server() self.copy_input_file_to_server()
model_id = self.test_load_ilastik_pixel_model() model_id = self.test_load_ilastik_pixel_model()
resp_infer = requests.put( resp_infer = requests.put(
......
...@@ -25,12 +25,14 @@ class TestGetSessionObject(unittest.TestCase): ...@@ -25,12 +25,14 @@ class TestGetSessionObject(unittest.TestCase):
for k in old_paths.keys(): for k in old_paths.keys():
self.assertTrue(new_paths[k].__str__().startswith(newroot.__str__())) self.assertTrue(new_paths[k].__str__().startswith(newroot.__str__()))
def test_restart_session(self): def test_restart_session(self):
sesh = Session() sesh = Session()
logfile1 = sesh.session_log logfile1 = sesh.session_log
sesh.restart() sesh.restart()
logfile2 = sesh.session_log logfile2 = sesh.session_log
self.assertIsNot(logfile1, logfile2, 'Restarting session does not generate new logfile') self.assertNotEqual(logfile1, logfile2, 'Restarting session does not generate new logfile')
def test_session_records_workflow(self): def test_session_records_workflow(self):
import json import json
...@@ -55,7 +57,6 @@ class TestGetSessionObject(unittest.TestCase): ...@@ -55,7 +57,6 @@ class TestGetSessionObject(unittest.TestCase):
success = sesh.load_model(MC) success = sesh.load_model(MC)
self.assertTrue(success) self.assertTrue(success)
loaded_models = sesh.describe_loaded_models() loaded_models = sesh.describe_loaded_models()
print(loaded_models)
self.assertTrue( self.assertTrue(
(MC.__name__ + '_00') in loaded_models.keys() (MC.__name__ + '_00') in loaded_models.keys()
) )
...@@ -69,7 +70,6 @@ class TestGetSessionObject(unittest.TestCase): ...@@ -69,7 +70,6 @@ class TestGetSessionObject(unittest.TestCase):
MC = DummyImageToImageModel MC = DummyImageToImageModel
sesh.load_model(MC) sesh.load_model(MC)
sesh.load_model(MC) sesh.load_model(MC)
print(sesh.models.keys())
self.assertIn(MC.__name__ + '_00', sesh.models.keys()) self.assertIn(MC.__name__ + '_00', sesh.models.keys())
self.assertIn(MC.__name__ + '_01', sesh.models.keys()) self.assertIn(MC.__name__ + '_01', sesh.models.keys())
......
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