# How to run monailabel autosegmentation using python interactor

**URL:** <https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539>\
**Category:** Support\
**Tags:** python, monailabel\
**Created:** [October 4, 2022, 6:20am UTC](https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539 "2022-10-04T06:20:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [October 4, 2022, 6:20am UTC](https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539/1 "2022-10-04T06:20:53Z")

</div>

Dear community,  
I am trying to run the monailabel autosegmentation for many samples so I would like to learn how to run infer using the python interactor.  
I found [this discussion](https://github.com/Project-MONAI/MONAILabel/discussions/1004#discussioncomment-3684524) on github answered by wonderful @lassoan, informing how logic can be used to run the module on python interactor.

With the [given information](https://github.com/Project-MONAI/MONAILabel/blob/5a7cbd8d5de945a99085d0f6495de21485c47415/plugins/slicer/MONAILabel/MONAILabel.py#L2068-L2167), I was able to connect to the server but I could not make the autosegmentation to work.

I guess the infer can be done with following line but I was not able to figure out what image\_in is for.  
`result_file, params = infer(model, image_in)`

I tried putting my session folder as the image\_in address but it did not worked.

```auto
#MONAILABEL
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

#check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# <FAILED>
# def infer(self, model, image_in, params={}, label_in=None, file=None, session_id=None):
session_image = "C:/Users/username/.cache/monailabel/sessions"
result_file, params = logic.infer("deepedit_seg", image_in = session_image)

```

Can somebody kindly explain to me what the image\_in is supposed to be?

I am always greatful for all your help and thank you in advance.

---

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [October 6, 2022, 8:21am UTC](https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539/3 "2022-10-06T08:21:01Z")

</div>

I figured it out!!

I got confused what image\_in was because I misread [this](https://docs.monai.io/projects/label/en/latest/apidocs/monailabel.client.client.html) and thought the _image\_in_ was supposed to be different from the _file_.

> infer(_model_, _image\_id_, _params_, _label\_in=None_, _file=None_, _session\_id=None_ )[[source]](https://docs.monai.io/projects/label/en/latest/_modules/monailabel/client/client.html#MONAILabelClient.infer)
> 
> Run Infer
> 
> Parameters
> 
> - **model** – Name of Model
> - **image\_id** – Image Id
> - **params** – Additional configs/json params as part of Infer request
> - **label\_in** – File path for label mask which is needed to run Inference (e.g. In case of Scribbles)
> - **file** – File path for Image (use raw image instead of image\_id)
> - **session\_id** – Session ID (use existing session id instead of image\_id)
> 
> Returns
> 
> response\_file (label mask), response\_body (json result/output params)

After all, image\_in represented the path to my volume. (Thank you so much @rbumm!!)  
So something like this would be image\_in,

> image\_in = “C:/Users/username/Desktop/sampleFile.nrrd”

Now, if you want your processed volume to be autosegmented, not the original volume, you have to save currently edited volume as a temporary file.

Monailabel does that for you when you use it on Slicer, but you have to do [this part](https://github.com/Project-MONAI/MONAILabel/blob/5a7cbd8d5de945a99085d0f6495de21485c47415/plugins/slicer/MONAILabel/MONAILabel.py#L1302) manually when using the python interactor.

So, after having the server ready, you can get the server address and do as following.

```auto
# Save currently working scene
import time
import tempfile

def saveVolTemp():
	# Temporary folder path
	tempVolDir = logic.tmpdir
	# Select the volume node you are trying to work with
	volNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
	image_id = volNode.GetName()
	# Absolute path of the temporary volume file
	in_file = tempfile.NamedTemporaryFile(suffix= '.nrrd', dir = tempVolDir).name
	logic.reportProgress(5)
	# save the volume node
	start = time.time()
	slicer.util.saveNode(volNode, in_file)
	logging.info(f"Saved Input Node into {in_file} in {time.time() - start:3.1f}s")
	logic.reportProgress(30)
	return tempVolDir, image_id, in_file

# get MONAILABEL logic
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

# check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# Save the volume and get the path
tempVolDir, image_id, in_file = saveVolTemp()

# infer
result_file, params = logic.infer("deepedit_seg", in_file)

# load the autosegmented segmentation file onto Slicer
slicer.util.loadSegmentation(result_file)

```

I not good at python so please feel free to correct me if there is anything wrong or if this is a hard way aroud.  
Thank you always, and I really hope this can help somebody.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 6, 2022, 10:12am UTC](https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539/4 "2022-10-06T10:12:44Z")

</div>

Great, @hourglassnam, I tested this and it works fine.

Just needed to remove `logic.reportProgress` calls from saveVolTemp() and make a change for `tempVolDir`:

```auto
 # Save currently working scene
import time
import tempfile
def saveVolTemp():
	# Temporary folder path
	tempVolDir = slicer.app.temporaryPath + "/YourProgramName/"
	# Select the volume node you are trying to work with
	volNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
	image_id = volNode.GetName()
	# Absolute path of the temporary volume file
	in_file = tempfile.NamedTemporaryFile(suffix= '.nrrd', dir = tempVolDir).name

	# save the volume node
	start = time.time()
	slicer.util.saveNode(volNode, in_file)
	logging.info(f"Saved Input Node into {in_file} in {time.time() - start:3.1f}s")
	return tempVolDir, image_id, in_file

# get MONAILABEL logic
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

# check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# Save the volume and get the path
tempVolDir, image_id, in_file = saveVolTemp()

# infer
result_file, params = logic.infer("deepedit_seg", in_file)

# load the autosegmented segmentation file onto Slicer
slicer.util.loadSegmentation(result_file)

```

---

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [October 6, 2022, 11:42am UTC](https://discourse.slicer.org/t/how-to-run-monailabel-autosegmentation-using-python-interactor/25539/5 "2022-10-06T11:42:19Z")

</div>

Great!! Thank you so much for checking it:))
