# Signed distance from mean shape and output csv/txt file for 400 subjects

**URL:** <https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490>\
**Category:** SlicerSALT\
**Created:** [May 19, 2022, 1:47pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490 "2022-05-19T13:47:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [May 19, 2022, 1:47pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/1 "2022-05-19T13:47:14Z")

</div>

Hi all!

I am using SPHARM-PDM to generate shapes of subcortical structures. Using the output files “SubjectName\_pp\_surf\_SPHARM\_procalign.vtk”, I produced a mean vtk file with “Shape Variation Analyzer” module.

My next step is to find the signed shape difference for all of my subjects from the mean shape. Once I have the distance, I would like to get a csv/txt file that has the signed distance values for my 1002 vertices. With that csv/txt file, I will carry out my statistical analysis separately (I have this statistical analysis tool). Could you guide me to the right direction please?

---

<div class="post-metadata">

**Author:** ![styner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/styner/32/1024_2.png) [@styner](https://discourse.slicer.org/u/styner)\
**Post date:** [May 20, 2022, 2:00pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/2 "2022-05-20T14:00:19Z")

</div>

Hi Khalid  
This can be done in 2 ways:

“old” way: Use a helper module/tool called MeshMath (I am sure there are other options as well). It is distributed with SlicerSALT (in the bin folder), but you would need to call it via terminal access.

1. compute difference as a vector field: MeshMath meanInput.vtk outputdiffvector.kwm.txt -subtract subjectInput.vtk  
2a. compute the signed magnitude of the difference vectors: MeshMath meanInput.vtk outputSignedMagnitude.kwm.txt -magdir outputdiffvector.kwm.txt  
2b. alternatively, also map the difference vector to the normal of the mean surface (removes component that runs along the surface, rather than orthogonal to it): MeshMath meanInput.vtk outputNormSignedMagnitude.kwm.txt -magNormDir outputdiffvector.kwm.txt

New way: use Model to Model distance SlicerSalt module : source model =\> mean surface, target model =\> subject surface, distance =\> corresponding point to point, save target name in distance filed =\> check, vtk-output =\> the source model with the difference added as a scalar field.

- this will give you a signed magnitude (please double check), but will not provide an option to map the magnitude to the surface normal

hope this helps  
Martin

---

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [May 22, 2022, 5:44pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/3 "2022-05-22T17:44:07Z")

</div>

Thank you, Martin! It was very helpful.

I am using the “old method” as I can automate this by writing a command line script for my 400 subjects. I have a couple of follow-up questions.

i) It looks like MeshMath shows error with my SPHARM vtk files (e.g. “subjectID\_pp\_surf\_SPHARM\_procalign.vtk”) when I run the command “MeshMath input\_mean.vtk subjectID\_outputdiffvector.kwm.txt -subtract subjectID\_pp\_surf\_SPHARM\_procalign.vtk”.

The error message is:  
“Incomplete file record definition  
NDims required and not defined.  
MetaObject: Read: MET\_Read Failed  
MetaMesh: M\_Read: Error parsing file”

However, it works just fine if I first convert the files from vtk to meta using “VTK2Meta” tool that comes with spharm-pdm and then run the command with those meta files. Please let me know if this is okay.

ii) When I run what you suggested in 2b (as I am interested in the distance values along the surface normal), it generates two text files: “subjectID\_outputNormSignedMagnitude.kwm\_centered.txt” and “subjectID\_outputNormSignedMagnitude.kwm.txt”. Could you let me know which is what and which one should I be using for my case, please?

Thanks in advance!  
Khalid

---

<div class="post-metadata">

**Author:** ![styner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/styner/32/1024_2.png) [@styner](https://discourse.slicer.org/u/styner)\
**Post date:** [May 22, 2022, 7:14pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/4 "2022-05-22T19:14:08Z")

</div>

Hi Khalid  
Re 1) forgot about that. You are correct, the files need to be in meta format (there is no loss of information by going from vtk to meta for the meshes)  
Re 2) centered subtracts the mean across the surface, so rather than the absolute value (of the signed magnitude along the surface normal) this will return values relative to the mean. Generally the centered values are not needed (I cannot remember why we added that option), so you need the outputNormSignedMagnitude.kwm.txt files.  
Best  
Martin

---

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [June 9, 2022, 3:12pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/5 "2022-06-09T15:12:28Z")

</div>

Hi @styner,

Thank you Martin for taking the time in answering all the questions. I wanted to ask you two questions.

1. For my subjects, if I am using gaussian filtering of 0.75 in 3 axes in the advanced post processed segmentation tab of SlicerSALT, and at the same time giving a Reg Temp and a Flip Temp as my input (i.e. running SPHARM-PDM on my template at the very beginning), do I need to apply the same gaussian filtering of 0.75 in 3 axes on the template while running SPHARM-PDM on the template and use that version as my Reg Temp and Flip Temp (instead of the version without gaussian filter)?
2. Does gaussian filtering of 0.75 in 3 axes sound reasonable enough not to cause any errors in my distance calculation and subsequent statistical analysis?

Regards,  
Khalid

---

<div class="post-metadata">

**Author:** ![styner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/styner/32/1024_2.png) [@styner](https://discourse.slicer.org/u/styner)\
**Post date:** [June 10, 2022, 1:56pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/6 "2022-06-10T13:56:09Z")

</div>

If possible, do not use the gaussian smoothing, as it can remove parts. On the other hand, if you have medium sized holes in your data, then gaussian smoothing can fill these in. We usually process our shape data without the Gaussian smoothing, unless the segmentation have consistently holed or handles that need filling via an automated approach, such as the Gaussian smoothing.

---

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [August 25, 2022, 8:40pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/7 "2022-08-25T20:40:40Z")

</div>

Hi @styner,

Could you also tell me what this 0.75 or 1 gaussian smoothing value in 3 axes mean? Is it in mm or voxel size?

Regards,  
Khalid Saifullah

---

<div class="post-metadata">

**Author:** ![styner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/styner/32/1024_2.png) [@styner](https://discourse.slicer.org/u/styner)\
**Post date:** [August 25, 2022, 9:43pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/8 "2022-08-25T21:43:47Z")

</div>

it’s in millimeter (mm)

---

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [September 10, 2022, 7:35pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/9 "2022-09-10T19:35:16Z")

</div>

Hi @styner,

> [@styner](#):
>
> it’s in millimeter (mm)

For a variance of of 2.25, the FWHM is 3.5mm [2.355\*sqrt(2.25)]. Is my calculation correct?

 ![Screen Shot 2022-09-10 at 2.23.05 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/2/b2b45f964e18d9e1ee6bfe24e12c476db2e94e0d.png)  
Link: [Full width at half maximum - Wikipedia](https://en.wikipedia.org/wiki/Full_width_at_half_maximum)

Regards,  
Khalid Saifullah

---

<div class="post-metadata">

**Author:** ![styner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/styner/32/1024_2.png) [@styner](https://discourse.slicer.org/u/styner)\
**Post date:** [September 12, 2022, 7:31pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/10 "2022-09-12T19:31:16Z")

</div>

I think so, but I don’t use FWHM

---

<div class="post-metadata">

**Author:** ![Khalid\_Saifullah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/khalid_saifullah/32/15324_2.png) [@Khalid\_Saifullah](https://discourse.slicer.org/u/Khalid_Saifullah)\
**Post date:** [September 12, 2022, 11:45pm UTC](https://discourse.slicer.org/t/signed-distance-from-mean-shape-and-output-csv-txt-file-for-400-subjects/23490/11 "2022-09-12T23:45:50Z")

</div>

Hi @styner,

Thank you! I would also like to ask you about something else. Is there a way to find out the node to node distance (within one subject)? Basically, I would like to know about the distance from one node to another so that I can make a better choice about the variance of gaussian smoothing I am going to use to incorporate all my subjects in a generalized fashion.

I apologize going over the same issue again. Actually, I have got meaningful results for my subjects without using any smoothing but because I want to include most of my subjects, I need to use this smoothing.

Thank you for your support throughout the process! Much appreciated!

Regards,  
Khalid Saifullah
