# Resample Scalar Volumes - Scripting in Python

**URL:** https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736
**Category:** Support
**Created:** [September 18, 2021, 5:07am UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736 "2021-09-18T05:07:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Sharada](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sharada](https://discourse.slicer.org/u/Sharada)
#### Post date: [September 18, 2021, 5:07am UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/1 "2021-09-18T05:07:38Z")

</div>

Hello,

I am trying to generate transformations and resample scalar volumes in Slicer programmatically with Python. And have two questions:

1. I am unable to find a scripted module corresponding to ‘Resample Scalar/Vector/DWI Volume’ on GitHub. The steps I need to implement are - set input volume, create new output volume as, set the transform, reference volume, and ensure linear interpolation. Can someone please help me to get started?
2. I am working in a Jupyter Notebook with Slicer kernel. As an attempt at automation, I am trying to pause my run, prompt user to add fiducials to the image on Slicer, and then continue running the script.  
This is not working as it just freezes my jupyter notebook and fails the kernel. Any suggestions on how I could implement this?

Thank you,  
Sharada

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 18, 2021, 6:03pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/2 "2021-09-18T18:03:32Z")

</div>

> [@Sharada](#):
>
> ‘Resample Scalar/Vector/DWI Volume’ on GitHub

It is implemented in C++, see source code [here](https://github.com/Slicer/Slicer/tree/master/Modules/CLI/ResampleScalarVectorDWIVolume). You can call CLI module from Python as described [here](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-run-a-cli-module-from-python).

> [@Sharada](#):
>
> As an attempt at automation, I am trying to pause my run, prompt user to add fiducials to the image on Slicer, and then continue running the script.

Notebooks are great for development and batch processing but if you want to implement interactive workflows for end users then scripted modules is a much more suitable option. You can create a simple GUI that contains a node selector to place markups points, maybe some more GUI widgets to set parameters, etc. Script in your notebook will become the module logic. I would recommend the [PerkLab bootcamp Slicer programming tutorial](https://slicer.readthedocs.io/en/latest/developer_guide/api.html#tutorials) to learn how to turn your script into a Slicer module.

---

<div class="post-metadata">

### Author: ![Sharada](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sharada](https://discourse.slicer.org/u/Sharada)
#### Post date: [September 18, 2021, 7:16pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/3 "2021-09-18T19:16:11Z")

</div>

Hi Andras,

Thank you for the quick response! I tried following the example you provided for the 1st problem and I still have an error. I have an input volume labeled ‘Axial’ and a transform ‘ACPC Transform’ already created in Slicer.

I don’t understand why there is no data assigned to “Input Volume” when I clearly have.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/e/ae83fc712cf60642ffc9fe7b07c0ba2bf3ebbbfe.png)

I tried looking for similar issues and could not troubleshoot. I might be missing something very minor as I am new to Slicer. Could you please help?

I will follow your advice about my 2nd question - thanks for the input on that.

Thanks,  
Sharada

---

<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: [September 18, 2021, 7:37pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/4 "2021-09-18T19:37:53Z")

</div>

Probably you should do something like

```auto
firstVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
...
parameters["inputVolume"] = firstVolumeNode
...

```

Plus: you need to 100 % sure about each parameter name. At least “Reference Volume (xxxx)” from your example seems not correct.

---

<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: [September 18, 2021, 7:53pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/5 "2021-09-18T19:53:58Z")

</div>

Just checked the source of resampleModule (link above) and it shows that the CLI call needs the following parameter strings: “inputVolume”, “outputVolume”, “referenceVolume”, “transformationFile” and “interpolationType” .

---

<div class="post-metadata">

### Author: ![Sharada](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sharada](https://discourse.slicer.org/u/Sharada)
#### Post date: [September 18, 2021, 8:00pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/6 "2021-09-18T20:00:18Z")

</div>

Thank you, yes. I also found the same thing after your comment about verifying the parameter names. Unfortunately, I have the same error still. I tried pulling the volumes using “slicer.mrmlScene.GetFirstNodeByClass” like you mentioned, instead of “slicer.util.getNode” just to see if it would make a difference. I still have the same error.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/4/64ace2dadb9715784ee4e042f9f8b2377704de47.png)

I am not familiar with C++, so it is a little hard for me to understand the logic. Any other suggestions?

Thanks,  
Sharada

---

<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: [September 18, 2021, 8:57pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/8 "2021-09-18T20:57:52Z")

</div>

[Here](https://nipype.readthedocs.io/en/latest/api/generated/nipype.interfaces.slicer.filtering.resamplescalarvectordwivolume.html) is a complete description of all the parameters.

“inputVolume” (a pathlike object or string representing an existing file) – Input Volume to be resampled.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 18, 2021, 9:02pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/9 "2021-09-18T21:02:35Z")

</div>

As the error message tells, you provided a collection of nodes, instead of a volume node. This is because the method you used provides a collection of nodes. I would recommend to use `slicer.util` helper functions to get nodes from the scene because they are easier to use in Python.

---

<div class="post-metadata">

### Author: ![Sharada](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sharada](https://discourse.slicer.org/u/Sharada)
#### Post date: [September 18, 2021, 9:14pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/10 "2021-09-18T21:14:11Z")

</div>

Thank you so much Rudolf and Andras! I was able to follow your advice and pass the right datatype. I visualized the volume resampled manually using Slicer and the one I did with Python - They match perfectly.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/d/2dc0f34190cbe85e2ef648a371ba6553d8b3297c.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/8/089e7bdf294ea7353cdec14783c7a5dcfcf54482.jpeg)  
My 3D rendering is not working for some reason, but I think this definitely solved my issue.

Thanks again for the wonderful software and all the support you provide!

Best,  
Sharada

---

<div class="post-metadata">

### Author: ![Sharada](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@Sharada](https://discourse.slicer.org/u/Sharada)
#### Post date: [September 18, 2021, 9:43pm UTC](https://discourse.slicer.org/t/resample-scalar-volumes-scripting-in-python/19736/11 "2021-09-18T21:43:52Z")

</div>

Quick update - ‘slicer.util.getNode’ works too! The parameter names were the only thing that changed between what you see below and what I had before.

outputVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLScalarVolumeNode”)  
outputVolumeNode.SetName(‘Axial ACPC’)

resampleModule = slicer.modules.resamplescalarvectordwivolume

parameters = {}  
parameters[‘inputVolume’] = slicer.util.getNode(‘Axial’)  
parameters[‘outputVolume’] = slicer.util.getNode(‘Axial ACPC’)  
parameters[‘referenceVolume’] = slicer.util.getNode(‘Axial’)  
parameters[‘transformationFile’] = slicer.util.getNode(‘ACPC\_Transform’)  
parameters[‘interpolationType’] = ‘linear’

cliNode = slicer.cli.runSync(resampleModule,None,parameters)

if cliNode.GetStatus() & cliNode.ErrorsMask:  
errorText = cliNode.GetErrorText()  
slicer.mrmlScene.RemoveNode(cliNode)  
raise ValueError("CLI execution failed: " + errorText)
