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?
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.
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:
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