# Point data error when visualize the difference between two volumetric meshes

**URL:** https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972
**Category:** Development
**Tags:** python
**Created:** [December 29, 2022, 10:24am UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972 "2022-12-29T10:24:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [December 29, 2022, 10:24am UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/1 "2022-12-29T10:24:38Z")

</div>

Hi,

I have two volumetric liver meshes in vtk unstructured grid format (ground-truth and predicted versions) and I’d like to plot the error, i.e., the difference between the ground-truth and predicted liver meshes, on the predicted mesh / mesh coordinates with a different colour (colour code based on displacement magnitudes). The N-th point in the ground-truth mesh correspond to the N-th point in the predicted mesh.

I have attempted to do this by following script however I got an error message as attached below.

Code:

```auto
>>> gtMesh = getNode('gtMesh')
>>> predMesh = getNode('predMesh')
>>> diff_arr = gtMesh - predMesh
>>> gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
>>> predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
>>> diff_arr = gtMesh_np_arr - predMesh_np_arr
>>> slicer.util.arrayFromModelPointData(gtMesh, diff_arr)

```

Error Message:

 ![error_msg](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/4/3491e83da211511804915bffce7dc17bd8f5b773.png)

---

<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: [December 31, 2022, 4:38am UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/2 "2022-12-31T04:38:33Z")

</div>

You need to create a vtk.vtkDoubleArray, fill it with zeros, set its name, and add it as a point data array to the polydata. You can then modify it using `slicer.util.arrayFromModelPointData` as you did it above.

---

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [January 4, 2023, 5:18pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/3 "2023-01-04T17:18:51Z")

</div>

Meshes are volumetric and have an unstructured grid vtk format. In that case, how can I assign a poly data object to these meshes, given that they are not surface meshes?

I just created poly data object as below with a vtk.vtkDoubleArray, fill it with zeros. However, I cannot add that to the volumetric mesh. Should I extract surface mesh from the volumetric mesh first ?

```auto
gtMesh = getNode('gtMesh')
predMesh = getNode('predMesh')
diff_arr = gtMesh - predMesh
gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
diff_arr = gtMesh_np_arr - predMesh_np_arr

shape = np.shape(gtMesh_np_arr)
zeros_np_arr = np.zeros(shape)
vtkdubarr.SetArray(zeros_np_arr, 0, 1)

gtMeshPolyData = gtMesh.GetPolyData()
gtMeshPolyData.GetPointData().AddArray(vtkdubarr)

gtMeshPolyData = vtk.vtkPolyData()
gtMeshPolyData.GetPointData().AddArray(vtkdubarr)
gtMeshPolyData.GetPointData().SetActiveScalars("vtkdubarr")

```

---

<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: [January 5, 2023, 1:50pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/4 "2023-01-05T13:50:03Z")

</div>

> [@suranga](#):
>
> `gtMesh.GetPolyData()`

If you work with unstructured grid then you can use the more generic `gtMesh.GetMesh()` method.

---

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [January 5, 2023, 2:30pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/5 "2023-01-05T14:30:29Z")

</div>

I got poly data from the GetMesh() function, but when I tried slicer.util.arrayFromModelPointData by passing the mesh and the displacement field array, I got ‘only integer scalar arrays can be converted to a scalar index’.

slicer.util.arrayFromModelPointData does not accept the numpy arrays as an arguement?

Code:

```auto
gtMesh = getNode('gtMesh')
predMesh = getNode('predMesh')
diff_arr = gtMesh - predMesh
gtMesh_np_arr = slicer.util.arrayFromModelPoints(gtMesh)
predMesh_np_arr = slicer.util.arrayFromModelPoints(predMesh)
diff_arr = gtMesh_np_arr - predMesh_np_arr

shape = np.shape(gtMesh_np_arr)
zeros_np_arr = np.zeros(shape)
vtkdubarr.SetArray(zeros_np_arr, 0, 1)

gtMesh.GetMesh().GetPointData().AddArray(vtkdubarr)
gtMesh.GetMesh().GetPointData().SetActiveScalars("vtkdubarr")

slicer.util.arrayFromModelPointData(gtMesh, diff_arr)

```

Error Message:

```auto
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\mnwoki\AppData\Local\NA-MIC\Slicer 5.1.0-2022-07-08\bin\Python\slicer\util.py", line 1722, in arrayFromModelPointData
    arrayVtk = _vtkArrayFromModelData(modelNode, arrayName, 'point')
  File "C:\Users\mnwoki\AppData\Local\NA-MIC\Slicer 5.1.0-2022-07-08\bin\Python\slicer\util.py", line 1706, in _vtkArrayFromModelData
    arrayVtk = modelData.GetArray(arrayName)
TypeError: GetArray argument 1: only integer scalar arrays can be converted to a scalar index

```

---

<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: [January 5, 2023, 2:33pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/6 "2023-01-05T14:33:39Z")

</div>

Please upload a sample “gtMesh” and “predMesh” files somewhere and post the download link here so that I can test your code snippet.

---

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [January 6, 2023, 1:53pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/8 "2023-01-06T13:53:47Z")

</div>

Can I ask a posibble solution for this please ? Aything missing with the shared files ?

---

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [January 10, 2023, 9:28am UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/9 "2023-01-10T09:28:59Z")

</div>

Hi Lassoan,

Could you please provide an update on this matter? Is there anything I’m doing wrong in the code?

---

<div class="post-metadata">

### Author: ![suranga](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@suranga](https://discourse.slicer.org/u/suranga)
#### Post date: [January 13, 2023, 3:11pm UTC](https://discourse.slicer.org/t/point-data-error-when-visualize-the-difference-between-two-volumetric-meshes/26972/10 "2023-01-13T15:11:52Z")

</div>

I’m still having this problem and am unable to resolve it. Any suggestions, please?
