Saving table from command line

Hi everyone,

I have made a lung segmentation on an MRI and used the Segment Statistics module to create a table with measurements about the segmentations (such as the volume of the segments). I know how to save this table as a .csv file by going to Save at the top of the Slicer window.

However, I would want to automate certain of the processes I go through, such as the saving of this table. I wondered if there was anything I could do from command line or with I have found some existing blogposts about how to do this, for example this one: How to programmatically export data from segment statistics?.

I could however not work out how to do this. I don’t know where I have to run this command. I am working in Windows and I tried running it in a cmd terminal, but to no avail. I explored my filesystem a bit and found some directories that according to me allude to the path that is used in the command in the blogpost. I can navigate to AppData/Local/NA-MIC/Slicer 5.0.3/bin/Python/slicer and this directory indeed contains the file util.py.

I do not know however how to go about saving the table now? What terminal should I run the command in?

Thank you a lot in advance!

Kind regards

Lukas

You should run that python code using the Python Interactor in the 3D Slicer application. You can toggle it to show/hide by a python button in the toolbar area, View->Python Interactor, or by keyboard shortcuts Ctrl+3/Ctrl+`

Make sure to use saveNode to save the file. In your code above you used getNode. Also if you just specify the filename and not the full absolute path it will attempt to save in some last known position or working directly I believe. To be more explicit to where you save, specify the full path. It would be as follows.

tablenode = getNode('T')
slicer.util.saveNode(tablenode, "C:/Users/James/Downloads/Table.csv")
1 Like

Yes, sorry that was a typo of mine. I indeed found the files I was looking for now.
I was wondering if I could accomplish the same result by using an external python file. I made such a file with only the following contents:

def saveTable():
	tablenode = slicer.util.getNode('T')
	slicer.util.saveNode(tablenode,"grap.csv")

I loaded this python file into Slicer’s Python environment with sys.path.append(filepath) and then import. This all seems to work without problems. If I now want to execute the function above (with the command file.saveTable()), I get the following error:

Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\lukas\OneDrive\Documenten\saveAsGrap.py”, line 2, in saveTable
tablenode = slicer.util.getNode(‘T’)
NameError: name ‘getNode’ is not defined

Is there any way around this?

Thank you in advance!

See the notes linked below. slicer.util is imported to the interactive console as a convenience.

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-mrml-node-from-the-scene

Hi, thank you for your response!

However, I don’t see how this can help me to solve my issue.
Sorry for the inconvienence, I’m a beginner at Slicer.

Thank you!

If I remove the slicer.util-part in front of getNode, I still have the same issue.

Try running using this form instead:

exec(open(f"{path_to_script}").read())

Hi, thank you for your answer!

This indeed seems to work.

Thank you very much!

1 Like