From 92b0121a4004619211ee708089a1570cad57356d Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 21 Mar 2024 15:40:41 +0100
Subject: [PATCH] Raise error if ilastik data not embedded in project file; but
 it appears that most test classifiers fail this

---
 model_server/extensions/ilastik/models.py | 28 +++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/model_server/extensions/ilastik/models.py b/model_server/extensions/ilastik/models.py
index de25566a..00224805 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
-- 
GitLab