diff --git a/model_server/extensions/ilastik/models.py b/model_server/extensions/ilastik/models.py index de25566a6571b6a07f8f6e1d453e49a1b13f9c72..00224805f7c7a7106125be53ccf36bafad5ec579 100644 --- a/model_server/extensions/ilastik/models.py +++ b/model_server/extensions/ilastik/models.py @@ -12,8 +12,17 @@ from model_server.base.models import Model, ImageToImageModel, InstanceSegmentat class IlastikModel(Model): - def __init__(self, params, autoload=True): + def __init__(self, params, autoload=True, enforce_embedded=True): + """ + Base class for models that run via ilastik shell API + :param params: + project_file: path to ilastik project file + :param autoload: automatically load model into memory if true + :param enforce_embedded: + raise an error if all input data are not embedded in the project file, i.e. on the filesystem + """ self.project_file = Path(params['project_file']) + self.enforce_embedded = enforce_embedded params['project_file'] = self.project_file.__str__() if self.project_file.is_absolute(): pap = self.project_file @@ -42,6 +51,15 @@ class IlastikModel(Model): args.project = self.project_file_abspath.__str__() shell = app.main(args, init_logging=False) + # validate if inputs are embedded in project file + input_groups = shell.projectManager.currentProjectFile['Input Data']['infos'] + lanes = input_groups.keys() + for ll in lanes: + input_types = input_groups[ll] + for tt in input_types: + ds_loc = input_groups[ll][tt].get('location', False) + if self.enforce_embedded and ds_loc and ds_loc[()] == b'FileSystem': + raise IlastikInputEmbedding('Cannot load ilastik project file where inputs are on filesystem') if not isinstance(shell.workflow, self.get_workflow()): raise ParameterExpectedError( f'Ilastik project file {self.project_file} does not describe an instance of {shell.workflow.__class__}' @@ -216,4 +234,10 @@ class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): [0, 4, 1, 2, 3] ) - return PatchStack(data=pyxcz), {'success': True} \ No newline at end of file + return PatchStack(data=pyxcz), {'success': True} + +class Error(Exception): + pass + +class IlastikInputEmbedding(Error): + pass \ No newline at end of file