# Getting the samples out of vtkProbeFilter ...

**URL:** https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996
**Category:** Support
**Created:** [June 6, 2024, 1:45pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996 "2024-06-06T13:45:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 6, 2024, 1:45pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/1 "2024-06-06T13:45:20Z")

</div>

I have a structured grid with a temperature array. I have verified that it has nonzero data stored in this array, checked with:

`vtk_to_numpy(volume.GetPointData().GetArray("Temperature"))`.

I am trying to probe a ray through this volume to get the temperatures along this ray. By construction, I know that the endpoints of the ray are on the surface of the volume (and I have visually verified it in ParaView). The code I am using for the probe is:

```auto
sampler = vtkLineSource()
sampler.SetResolution(30)
sampler.SetPoint1(start)
sampler.SetPoint2(end)

probe = vtkProbeFilter()
probe.SetInputConnection(sampler.GetOutputPort())
probe.SetSourceData(volume)
probe.Update()

temps = vtk_to_numpy(probe.GetOutputDataObject(0).GetPointData().GetArray("Temperature"))

```

However when I inspect the temps array, I get all zeros!  
Any ideas what I am doing wrong?

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 6, 2024, 3:53pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/2 "2024-06-06T15:53:07Z")

</div>

Apparently I have been down this road before, and nobody seemed to know then either:

> [@vtkProbe not passing scalar values?](https://discourse.vtk.org/t/vtkprobe-not-passing-scalar-values/13272/2):
>
> I tried to increase the node density, that didn’t seem to work either: def project\_onto\_sphere(pd): s = vtk.vtkSphereSource() s.SetThetaResolution(60) s.SetPhiResolution(30) s.SetRadius(1) tf = vtk.vtkTriangleFilter() tf.SetInputData(pd) lf = vtk.vtkLoopSubdivisionFilter() lf.SetInputConnection(tf.GetOutputPort()) lf.SetNumberOfSubdivisions(3) lf.Update() pf = vtk.vtkProbeFilter() pf.SetInputConnection(s.GetOutputPort()) pf.SetSourceConnec…

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 6, 2024, 4:01pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/3 "2024-06-06T16:01:30Z")

</div>

A [VTK wiki](https://vtk.org/Wiki/Demystifying_the_vtkProbeFilter) that I found that seemed to have some useful information.  
I guess another hint for me might be this check (no valid points returned):

```auto
(Pdb) p vtk_to_numpy(probe.GetValidPoints())
array([], dtype=int64)

```

I can’t understand why the geometry would be ill-defined - it’s a vtkStructuredGrid? What’s to mess up? If I can see the file in ParaView, how can it be that wrong?

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 6, 2024, 7:08pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/4 "2024-06-06T19:08:36Z")

</div>

To add insult to injury - the “demo code” I wrote works ☹ … I guess that means that there is something wrong with reading the data in from the volume?

EDIT: If I changed volume to be closer to what I’m working with.

```python
#!/usr/bin/env python3
import numpy as np
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkStructuredGrid
from vtkmodules.vtkFiltersCore import vtkProbeFilter
from vtkmodules.vtkFiltersSources import vtkLineSource

X, Y, Z = np.meshgrid(
    np.linspace( 0.120, 0.225, 51),
    np.linspace(-0.006, 0.027, 21),
    np.linspace(-0.012, 0.036, 21))
R = np.hypot(X, np.hypot(Y, Z))
W = np.sin(R) / R
W = numpy_to_vtk(W.ravel())
W.SetName("W")

pts = vtkPoints()
for x, y, z in zip(X.ravel(), Y.ravel(), Z.ravel()):
    pts.InsertNextPoint(x, y, z)

sg = vtkStructuredGrid()
sg.SetPoints(pts)
sg.SetDimensions(51, 21, 21)
sg.GetPointData().AddArray(W)

sampler = vtkLineSource()
sampler.SetResolution(5)
sampler.SetPoint1(0.120, -0.006, -0.012)
sampler.SetPoint2(0.225, 0.027, 0.036)

probe = vtkProbeFilter()
probe.SetInputConnection(sampler.GetOutputPort())
probe.SetSourceData(sg)
probe.Update()

print(f"valid points: {vtk_to_numpy(probe.GetValidPoints())}")
print(f"probed values: {vtk_to_numpy(probe.GetOutput().GetPointData().GetArray('W'))}")

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 6, 2024, 9:42pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/5 "2024-06-06T21:42:24Z")

</div>

The issue is the ordering of numpy multidimensional arrays, and in particular the use of meshgrid.

Try switching your meshgrid around like this:

```python
Z, Y, X = np.meshgrid(
    np.linspace(-0.012, 0.036, 21), # Z space
    np.linspace(-0.006, 0.027, 21), # Y space
    np.linspace( 0.120, 0.225, 51), # X space
    indexing='ij')

```

If your meshgrid is built correctly, then when you print out the points, you should see the X values changing the most rapidly, followed by Y, and then followed by Z:

```python
# Points:
# 0 1 2 ... 22488 22489 22490
[0.12 0.1221 0.1242 ... 0.2208 0.2229 0.225] # X.ravel()
[-0.006 -0.006 -0.006 ... 0.027 0.027 0.027] # Y.ravel()
[-0.012 -0.012 -0.012 ... 0.036 0.036 0.036] # Z.ravel()

```

On the other hand, if Point 0 and Point 1 have the same X value, then you know that meshgrid has produced a grid that’s not ordered correctly for VTK.

Also be sure to switch around the numpy array dimensions before you use them in VTK:

```python
z_size, y_size, x_size = X.shape
sg.SetDimensions(x_size, y_size, z_size)

```

Some people prefer to transpose the numpy arrays before using them with VTK, but I prefer to leave all the arrays in their original ordering, and just re-order the indices instead.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 6, 2024, 10:24pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/6 "2024-06-06T22:24:34Z")

</div>

One minor edit above: use `indexing='ij'` with meshgrid to avoid swapping the first two dimensions.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 7, 2024, 2:23pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/7 "2024-06-07T14:23:44Z")

</div>

EDIT: there is something most definetly wrong with my point locations now … I’m still investigating where it went wrong. Viewing my volume in ParaView shows something is terribly wrong.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 7, 2024, 3:06pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/8 "2024-06-07T15:06:29Z")

</div>

My suspicion is still that there is something wrong with the structure of your vtkStructuredGrid. Check the points along some of the edges of the grid to make sure they’re correct:

```python
dims = [1,1,1] 
sg.GetDimensions(dims)
pts = sg.GetPoints()

if pts.GetNumberOfPoints() != dims[0]*dims[1]*dims[2]:
    print('Either points or dimensions are wrong')

print('xpoints:')
for i in range(dims[0]):
    print(pts.GetPoint(i))
print('ypoints:')
for j in range(dims[1]):
    print(pts.GetPoint(j*dims[0]))
print('zpoints:')
for k in range(dims[2]):
    print(pts.GetPoint(k*dims[0]*dims[1]))

```

For example, if your grid is aligned with the _x_, _y,_ _z_ axes, then in the `xpoints` only _x_ will vary, in `ypoints` only _y_ will vary, and in `zpoints` only _z_ will vary.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [June 7, 2024, 3:08pm UTC](https://discourse.vtk.org/t/getting-the-samples-out-of-vtkprobefilter/13996/9 "2024-06-07T15:08:57Z")

</div>

After I corrected the order (X then Y then Z) - there was a transpose operation in there. After that the shape of the array was incorrectly reported to vtkStructuredGrid. In particular, the data was (21, 21, 51) but I told VTK (21, 51, 21) … so that was a disaster.

Thanks for your help.
