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

**URL:** <https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [February 1, 2023, 6:27pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586 "2023-02-01T18:27:56Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 28, 2023, 9:40pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/22 "2023-02-28T21:40:11Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [February 28, 2023, 10:22pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/23 "2023-02-28T22:22:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 28, 2023, 10:24pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/24 "2023-02-28T22:24:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [February 28, 2023, 10:29pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/25 "2023-02-28T22:29:52Z")

</div>

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

 ![Screen Shot 2023-02-28 at 5.27.31 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/1/a1151edff7d5d0c5601b154a1667826740ea59fb.jpeg)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/83011_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 28, 2023, 10:35pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/26 "2023-02-28T22:35:15Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [February 28, 2023, 10:48pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/27 "2023-02-28T22:48:45Z")

</div>

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?

 ![Screen Shot 2023-02-28 at 5.46.10 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/3/33cdc974fccbbdddea53088677e825b575a11c0e.png)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 28, 2023, 11:52pm UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/28 "2023-02-28T23:52:52Z")

</div>

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:

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

```

and then the second line would be:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [March 1, 2023, 12:17am UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/29 "2023-03-01T00:17:55Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-of-each-segment))

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

 ![Screen Shot 2023-02-28 at 7.10.28 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/4/f4e46a40f0ffd5d3551a7e2e5f3b5a976452ee6b.png)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 1, 2023, 12:35am UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/30 "2023-03-01T00:35:15Z")

</div>

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

```auto
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:

```auto
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](https://docs.joomla.org/Using_the_Github_UI_to_Make_Pull_Requests) might be useful.

The things that are specific to Slicer are [described here](https://slicer.readthedocs.io/en/latest/developer_guide/contributing.html). 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.

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [March 1, 2023, 1:15am UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/31 "2023-03-01T01:15:38Z")

</div>

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

 ![Screen Shot 2023-02-28 at 8.08.56 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/4/f4ddb33371915de174fd5044cad3213af278aae0.png)

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 1, 2023, 4:51am UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/32 "2023-03-01T04:51:43Z")

</div>

> [@Jaleblanc](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![Jaleblanc](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaleblanc](https://discourse.slicer.org/u/Jaleblanc)\
**Post date:** [March 2, 2023, 1:40am UTC](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586/33 "2023-03-02T01:40:41Z")

</div>

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

[Previous page](https://discourse.slicer.org/t/how-to-calculate-the-surface-area-from-3d-fetal-mri-then-convert-to-python/27586.md?page=1)
