File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change 2323import json
2424import logging
2525import os
26+ import time
2627from typing import Any , Optional
2728
2829import anndata as ad
@@ -176,6 +177,33 @@ def segment_ROI(
176177
177178 return ilastik_labels .astype (label_dtype )
178179
180+ def setup_ilastik_with_retries (ilastik_model : str ):
181+ """
182+ Setup Ilastik headless shell with retries to avoid initialization issues.
183+
184+ See #17 for context
185+
186+ """
187+ max_retries = 5
188+ current_round = 0
189+ while current_round < max_retries :
190+ try :
191+ shell = setup_ilastik (ilastik_model )
192+ return shell
193+ except FileNotFoundError :
194+ current_round += 1
195+ logger .warning (
196+ f"Ilastik initialization failed, retrying { current_round = } /"
197+ f"{ max_retries } "
198+ )
199+ sleep_time = 2 ** (current_round + 1 )
200+ time .sleep (sleep_time )
201+
202+ raise FileNotFoundError (
203+ f"Ilastik initialization failed for model { ilastik_model } after "
204+ f"{ max_retries } retries."
205+ )
206+
179207
180208@validate_call
181209def ilastik_pixel_classification_segmentation (
@@ -254,7 +282,7 @@ def ilastik_pixel_classification_segmentation(
254282 )
255283
256284 # Setup Ilastik headless shell
257- shell = setup_ilastik (ilastik_model )
285+ shell = setup_ilastik_with_retries (ilastik_model )
258286
259287 # Check if channel input fits expected number of channels of model
260288 expected_num_channels = get_expected_number_of_channels (shell )
You can’t perform that action at this time.
0 commit comments