How to calculate the surface area from 3D fetal MRI, then convert to Python

Be sure to confirm that the file works if you just drag-and-drop it on slicer from that location. You can also check the path by getting the file properties (right click on the desktop icon and check the path - confirm that it’s the same but with just the slashes different).

Ok, I checked the file properties, and the path name matches my code. When I dragged and dropped the file into the console, I got this message:

[Qt] <QNSPanel: 0x7fd441a08ba0; contentView=<QNSView: 0x7fd441a088e0; QCocoaWindow(0x60000024c8f0, window=QWidgetWindow(0x60000115d440, name=“qSlicerDataDialogWindow”))>> has active key-value observers (KVO)! These will stop working now that the window is recreated, and will result in exceptions when the observers are removed. Break in QCocoaWindow::recreateWindowIfNeeded to debug.

I don’t know if that message is related but were you able to load the data? If drag and drop doesn’t work you can browse to the file with the Add Data option.

When I drag and drop, this is the window that opens (I have the description set to Segmentation, not Transform) and then I am able to add the data

C:/ is windows syntax, but apparently you are on Mac.

Just use the path as shown in your data dialog screenshot /Users/…

I’m sorry I should’ve mentioned that before. It no longer gives me the error message about reading the file; thank you!

When I run the Python Console, it says ‘Segmentation’ is not defined (I highlighted the area it is referring to). I got this line of code from the script repository, but do you know how I would define it?

Probably it’s unclear in the script repository, and if so it would be great if you could submit a pull request with a suggestion how to make it clearer to new users (if you don’t know how we can guide you), but this script is missing the first part of the first line. It should say:

segmentationNode = slicer.util.loadSegmentation("...")

and then the second line would be:

segmentationNode.CreateClosedSurfaceRepresentation()

(note the capitalization matters on the variable segmentationNode)

From there you can use segmentationNode and you don’t need the getNode call since you get the loaded node as the return value from loadSegmentation.

Thank you so much, this makes more sense, and I don’t get that error anymore. I don’t mind submitting a pull request to help others, I’ve never made one before, but any help would be appreciated thank you!

And I’m really hoping this is the last error. In my last section of code, I retrieved it from: “Quantifying segments - Get volume of each segment” (Script repository — 3D Slicer documentation)

I changed the volume to the surface area, as this is the measurement I need, but my 3rd last line has a KeyError

Here you need to be sure you access the right name of the calculated statistic. In this case it’s:

stats[segmentId, "ClosedSurfaceSegmentStatisticsPlugin.surface_mm2"]

One of the nice things about the python console is that you can do things step by step and check the values. For example, you could run this command to see all the values in the stats array so you know the valid keys:

for key in stats.keys():
  print(key)

looking at the variables like this can help you debug.

It would be great if you could make improvements to the documentation based on your experience and what you have learned. Mostly we use the github process that is very standard and there are lots of documents. I didn’t search a lot, but this tutorial might be useful.

The things that are specific to Slicer are described here. Much of the complexity is when you suggest code changes, but documentation changes are much easier and can be completed entirely using the web interface.

1 Like

Thank you so much, Steve; it worked! (And thank you @muratmaga and @lassoan for your help)

I will look into those sources as a starting point and see what I can do regarding the pull request, thank you for your help!

I do have one last question if that’s alright. Is it possible to load multiple segmentations at once in my code? I wanted to use my code for a large data set of placentas, and my current code only takes one at a time.

You can load any number of segmentations. To conserve memory, it probably makes sense to process them one by one (load, process, save results, close scene).

Thank you, I will stick to processing them one at a time.