Export Brainstorm seeg coordinates to 3D Slicer

Hello, I have been using 3D slicer for a while now, I would like to export SEEG contact location generate to brainstorm into 3D slicer and convert each contacts into a segment sphere using the xyz coordinates. Is it possible?

I have tried by changing the xyz naming convention into RAS, and then importing the contacts as points using the markups pluggin and it worked, however i can’t transfrom each point into a segment. The markup to model pliggin can only change the points in a connecting curve of tube shape. I want it to remain as location segmented points. Thanks

Probably the easiest is to describe this using a short Python code snippet. You can generate the code snippet with any AI assistant, you don’t need to know programming or much about Slicer.

For example, I got a working code with giving the following prompts to the free Microsoft Copilot:

write a Python code snippet that converts a markup fiducial list to a segmentation so that at each markup control point position a small sphere shape segment is added

It almost gave correct result, but it hallucinated a non-existent function, so I tried to make correct itself just by copying the error message that I saw in the Python console in Slicer.

I got the error: AttributeError: ‘vtkSlicerSegmentationsModuleLogicPython.vtkSlicerS’ object has no attribute ‘CreateLabelmapVolumeFromModel’

I got the error: AttributeError: module ‘slicer.util’ has no attribute ‘createLabelVolumeFromModel’

I got the error: AttributeError: ‘vtkSegmentationCorePython.vtkSegmentation’ object has no attribute ‘AddSegmentFromClosedSurfaceRepresentation’

I got the error: AttributeError: module ‘slicer.util’ has no attribute ‘updateSegmentFromModelPolyData’

It still did not find the correct converter function, so at this point I decided that I will point it to the Slicer script repository.

Use examples on this page: Script repository — 3D Slicer documentation

It still did not get it right, so I pinpointed the relevant section.

Use the code snippet in the section “Create segmentation from a model node”

This finally generated working code! It was not perfect in that it created an empty segment for each point, so I asked it to correct itself.

It created an extra empty segment for each point. Please update the code so that it does not generate those extra empty segments.

Now this generated code that worked perfectly:

import slicer
import vtk

# Create a segmentation node
segmentation_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentation = segmentation_node.GetSegmentation()

# Get the markup fiducial node
markup_node = slicer.util.getNode('F')
n_control_points = markup_node.GetNumberOfFiducials()

# Define sphere parameters
sphere_radius = 5.0  # Adjust the radius as needed
sphere_resolution = 50

# Create a model node for the sphere
sphere_model_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
sphere_model_node.SetName("SphereModel")

for i in range(n_control_points):
    # Get the control point position
    position = [0.0, 0.0, 0.0]
    markup_node.GetNthFiducialPosition(i, position)
    
    # Create the sphere
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(position)
    sphere.SetRadius(sphere_radius)
    sphere.SetThetaResolution(sphere_resolution)
    sphere.SetPhiResolution(sphere_resolution)
    sphere.Update()
    
    # Update the model node with the sphere polydata
    sphere_model_node.SetAndObservePolyData(sphere.GetOutput())
    
    # Add model to segmentation as a new segment
    slicer.modules.segmentations.logic().ImportModelToSegmentationNode(sphere_model_node, segmentation_node)
    
# Remove temporary model node
slicer.mrmlScene.RemoveNode(sphere_model_node)

# Ensure the segmentation displays correctly
segmentation_node.CreateClosedSurfaceRepresentation()

While having to copy-paste error messages was slightly annoying and ultimately I gave some hints to the chatbot, the whole thing did not take more than 5 minutes and the generated code works and the code quality is not bad at all. And the nice thing is that if you want to do something more or different then you can just keep asking the chatbot to further modify the code.

I would like to thank you for the quick and informative answer. I am going to try it and let you know. Thanks again

1 Like

After using my own data and your advice I found this code worked perfectly

import slicer
import vtk
import math

Volume of the sphere

sphere_volume = 30 # mm³

Calculate the radius

sphere_radius = (3 * sphere_volume / (4 * math.pi)) ** (1/3)

Create a segmentation node

segmentation_node = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)
segmentation = segmentation_node.GetSegmentation()

List of provided positions

positions = [
(35.49447, 35.492788, -5.868997),
(38.983289, 35.725376, -6.024056),
]

Create spheres and add to segmentation

for i, position in enumerate(positions):
# Create a sphere model node
sphere_model_node = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLModelNode”)
sphere_model_node.SetName(f"Sphere_{i}")

# Create the sphere
sphere = vtk.vtkSphereSource()
sphere.SetCenter(position)
sphere.SetRadius(sphere_radius)
sphere.SetThetaResolution(50)
sphere.SetPhiResolution(50)
sphere.Update()

# Update the model node with the sphere polydata
sphere_model_node.SetAndObservePolyData(sphere.GetOutput())

# Add model to segmentation as a new segment
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(sphere_model_node, segmentation_node)

Remove temporary model nodes

for i in range(len(positions)):
node = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLModelNode”)
slicer.mrmlScene.RemoveNode(node)

Ensure the segmentation displays correctly

segmentation_node.CreateClosedSurfaceRepresentation()

the only issue is that the processing time is extremely long, even the saving of the 3d slicer scene is long.
Windows 11, 12th Gen Intel(R) Core™ i9-12900 2.40 GHz
RAM 128 GB
3D slicer version 5.2.2