Create markups lines from .csv file

Hi,

I have a csv file with the columns ‘label’, ‘start’ and ‘end’. Where ‘start’ and ‘end’ are coordinates for a line. How can I in Python from this csv file create markups lines which corresponds to the ‘start’ and ‘end’ values and display them in Slicer. Furthermore, how can I change the color of each line?

Thank you

There are lots of examples of working with Markups in python here:

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#markups

Thank you, I have solved this now. A new problem has arised, The csv file that I have is large with over 10000 rows (which should result in 10000 lines), I used the function “AddControlPointWorld” in order to make a markups line, however with this large amount of data the script gets really slow, is there any other faster way to create markups lines from a large csv file?

Thank you

If you need to add more than a few dozens of points then do all that in a batch using StartModify/EndModify. If you show thousands of points then you may also consider disabling interactive picking and label display.

These are all demonstrated in this test:

However, if you don’t need labels and interaction then markups is not the right way to render the points. Instead, you can show them as glyphs in a model node. You can create a 3D model using a few lines of Python code (add your points into points of a polydata and turn the points into glyphs using vtkGlyph3D) - probably ChatGPT can write the code for you but if you get stuck then let us know.

Reading and writing point coordinates in a text file is also very slow. You can read/write the file about 100x faster if you write into a binary format (e.g., PLY or VTK). It is sufficient to write out just the points, Slicer will read it as a VTK polydata, and you can easily apply the glyph filter in Slicer.

Thank you for your reply.

I have managed to display a smaller amount of points (about 100) with both of the methods you have given me. But I still have the same problem.

Maybe I need to formulate my question a bit more detailed. I want to divide these 10000 points into groups of two, this since I want these groups to be displayed with different colors, so for example if I should do it with the first methods you presented to me, then I would assume that “numberOfNodes” would be 5000 and “numberOfControlPoints” would be 2, and then I could change the color of each node. The problem then is that it takes a really long time to execute the script and to create these nodes (the same would happen if i would make 5000 glyphs).

So my question now is if it is possible to display these data points and be able to individually change properties of each point (or in groups of two), so that at the end i will display these points with different colors?

Thank you

Each node will increase the complexity of the screen, so adding hundreds of them will start to impact the performance.

You can use a single point list markup node to display 10000 points with two colors. You can use the “selected” property of the control points to set the group.

You do not need to write any custom script for all these. You can read the CSV file containing point coordinates and “selected” flag as a table and then import the table into a markup node using Markups module.

Thank you, I managed to reduce the number of markup nodes to about 50 so everything worked fine.

Furthermore, I have a csv file which represents a vascular tree where each row in the file is a branch, which has an id and start and end coordinates for that branch. I wish to display this tree in the viewer. The tree consists of about 2000 branches. What is the best way to display this tree? I tried creating markup lines, however it seems that 3Dslicer has a hard time creating 2000 different markups lines. (Note, it is important to represent the branches as lines and not just nodes).

Thank you

We usually display such trees using model nodes. You can add each branch as a line cell in a vtkPolyData object and then set that object into a model node.

What software creates the CSV file? Very likely it already has the option to save the tree in a standard mesh file format that you can load into Slicer for visualization as is.