Script to display a table

Sorry if this is a dumb question, but I’m very new to slicer. I’m trying to code a python module that can display coordinates from a csv file. I know that you can do this with properly formatted csv files using the markup module, but I’d like to know how to do this from scratch. The goal is to create a module that allows me to select a csv file I upload to Slicer, append the required header, and then display the points.

Currently, I’m getting stuck on two steps. The first is figuring out how to get script to read whatever file I select with the ctkPathLineEdit widget. The second is figuring out how to display the data points after the csv file has been edited (this is the one I need the most help with). Any tips or hints?

Option A

The most flexible and powerful option is to to save the coordinates in a json file:

{"@schema": "https://raw.githubusercontent.com/Slicer/Slicer/main/Modules/Loadable/Markups/Resources/Schema/markups-schema-v1.0.0.json#",
"markups": [{"type": "Fiducial", "coordinateSystem": "LPS", "controlPoints": [
    { "label": "F-1", "position": [-53.388409961685827, -73.33572796934868, 0.0] },
    { "label": "F-2", "position": [49.8682950191571, -88.58955938697324, 0.0] },
    { "label": "F-3", "position": [-25.22749042145594, 59.255268199233729, 0.0] }
]}]}

You can load using:

markupsNode = slicer.util.loadMarkups("/path/to/MyMarkups.mkp.json")

Option B

If you only need to specify coordinates, then you can create a csv file with a few columns:

label,l,p,s,defined,selected,visible,locked,description
F-1,-19.9067,13.9347,29.443,1,1,1,0,
F-2,-7.3939,-76.9499,17.5525,1,1,1,0,
F-3,81.7333,-42.9415,9.62559,1,1,1,0,

You can load it like this:

markupsNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode")
slicer.modules.markups.logic().ImportControlPointsFromCSV(MRMLMarkupsCurveNode")

More information

1 Like

Thanks this helped a lot!

Is there anyway to edit the text for the label displayed in the scene so it doesn’t say “MarkupsCurve”?