I’ve had a script floating round for a while for doing this, but the python API seems to change every release.
I have a pre-trained 2-stage autocontext ILP which I want to open in python, and then hand it a numpy array and get a numpy array back (i.e. not having to read from a file and then write back to a file). It would also be fine to request a region of the project’s raw data stack, instead of handing it an array.
A very cut-down version of my script looks like this (it’s basically just the example at https://github.com/ilastik/ilastik/blob/0b94ea8104e38bda5eb17b1b23df9ed54f689a8f/examples/example_python_client.py ):
from collections import OrderedDict
import ilastik_main
import numpy as np
import vigra
from ilastik.applets.dataSelection import DatasetInfo
# open the project
args = ilastik_main.parse_args([])
args.headless = True
args.readonly = True
args.project = "path/to/my/project.ilp"
shell = ilastik_main.main(args)
# get the top-level operator for the 2nd stage of the autocontext
pixel_classifier = shell.workflow.pcApplets[-1].topLevelOperator
input_data = vigra.taggedView(np.random.randint(0, 255, (100, 100, 1), dtype=np.uint8), "yxc") # or whatever
role_data_dict = OrderedDict([
("Raw Data", [ DatasetInfo(preloaded_array=input_data1)])
])
predictions = shell.workflow.batchProcessingApplet.run_export(role_data_dict, export_to_array=True)
The output I get is
WARNING dataset.py(313): H5pyDeprecationWarning: dataset.value has been deprecated. Use dataset[()] instead.
WARNING stype.py(187): UserWarning: ArrayLike.isCompatible: FIXME here
WARNING opSimpleBlockedArrayCache.py(43): FutureWarning: Conversion of the second argument of issubdtype from `dtype` to `np.generic` is deprecated. In future, it will be treated as `np.object_ == np.dtype(dtype).type`.
WARNING opSlicedBlockedArrayCache.py(141): FutureWarning: Conversion of the second argument of issubdtype from `dtype` to `np.generic` is deprecated. In future, it will be treated as `np.object_ == np.dtype(dtype).type`.
WARNING opFormattedDataExport.py(149): UserWarning: The ROI you are attempting to export exceeds the extents of your dataset. Clipping to dataset bounds.
Traceback (most recent call last):
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/batchProcessing/batchProcessingApplet.py", line 131, in run_export
export_to_array=export_to_array)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/batchProcessing/batchProcessingApplet.py", line 239, in _run_export_with_empty_batch_lane
opDataSelectionBatchLaneView.DatasetGroup[role_index].setValue(info)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1267, in setValue
self._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1470, in _changed
c._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1470, in _changed
c._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1476, in _changed
self._configureOperator(self)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1490, in _configureOperator
self.operator._setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operator.py", line 469, in _setupOutputs
self.setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/dataExport/opDataExport.py", line 210, in setupOutputs
self._opFormattedExport.Input.connect( self.Inputs[selection_index] )
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 592, in connect
self._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1476, in _changed
self._configureOperator(self)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1490, in _configureOperator
self.operator._setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operator.py", line 469, in _setupOutputs
self.setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operators/ioOperators/opFormattedDataExport.py", line 231, in setupOutputs
known_keys = {"roi": list(self._opSubRegion.Roi.value)}
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1145, in value
temp = self[:].wait()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1068, in __getitem__
raise Slot.SlotNotReadyError(slotInfoMsg)
lazyflow.slot.Slot.SlotNotReadyError: Can't get data from slot <class 'lazyflow.operators.generic.OpSubRegion'>.Roi yet. It isn't ready.First upstream problem slot is: OpSubRegion/OpSubRegion.Roi : {_ready : False, shape : None, has_mask : None, _dirty : False}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/barnesc/.pyenv/versions/ilastik2019/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-0df88d0ccda3>", line 1, in <module>
runfile('/home/barnesc/work/code/ilastik-tst/example_python_client.py', wdir='/home/barnesc/work/code/ilastik-tst')
File "/home/barnesc/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.7479.30/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/home/barnesc/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.7479.30/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/barnesc/work/code/ilastik-tst/example_python_client.py", line 77, in <module>
predictions = shell.workflow.batchProcessingApplet.run_export(role_data_dict, export_to_array=True)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/batchProcessing/batchProcessingApplet.py", line 141, in run_export
batch_lane_index, batch_lane_index)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/dataSelection/opDataSelection.py", line 646, in removeLane
super(OpMultiLaneDataSelectionGroup, self).removeLane(laneIndex, finalLength)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/utility/opMultiLaneWrapper.py", line 43, in removeLane
self._removeInnerOperator(laneIndex, numLanes-1)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operatorWrapper.py", line 285, in _removeInnerOperator
oslot.removeSlot(index, length)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 818, in removeSlot
slot.disconnect()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 674, in disconnect
slot.disconnect()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 699, in disconnect
self._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1470, in _changed
c._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1470, in _changed
c._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1476, in _changed
self._configureOperator(self)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1490, in _configureOperator
self.operator._setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operator.py", line 469, in _setupOutputs
self.setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/ilastik/ilastik/applets/dataExport/opDataExport.py", line 250, in setupOutputs
self._opFormattedExport.TransactionSlot.connect( self.TransactionSlot )
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 124, in call_in_setup_context
return func(self, *args, **kwargs)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 592, in connect
self._changed()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1476, in _changed
self._configureOperator(self)
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1490, in _configureOperator
self.operator._setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operator.py", line 469, in _setupOutputs
self.setupOutputs()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/operators/ioOperators/opFormattedDataExport.py", line 231, in setupOutputs
known_keys = {"roi": list(self._opSubRegion.Roi.value)}
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1145, in value
temp = self[:].wait()
File "/home/barnesc/.pyenv/versions/ilastik2019/ilastik-meta/lazyflow/lazyflow/slot.py", line 1068, in __getitem__
raise Slot.SlotNotReadyError(slotInfoMsg)
lazyflow.slot.Slot.SlotNotReadyError: Can't get data from slot <class 'lazyflow.operators.generic.OpSubRegion'>.Roi yet. It isn't ready.First upstream problem slot is: OpSubRegion/OpSubRegion.Roi : {_ready : False, shape : None, has_mask : None, _dirty : False}
I don’t know if there’s any documentation of the python API; the pages on github.io are pretty old. Not really sure where to begin.