# Find maximum/minimum value in the neighbourhood of point

**URL:** https://discourse.paraview.org/t/find-maximum-minimum-value-in-the-neighbourhood-of-point/10055
**Category:** ParaView Support
**Created:** [July 26, 2022, 1:31pm UTC](https://discourse.paraview.org/t/find-maximum-minimum-value-in-the-neighbourhood-of-point/10055 "2022-07-26T13:31:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![s195t](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@s195t](https://discourse.paraview.org/u/s195t)
#### Post date: [July 26, 2022, 1:31pm UTC](https://discourse.paraview.org/t/find-maximum-minimum-value-in-the-neighbourhood-of-point/10055/1 "2022-07-26T13:31:25Z")

</div>

Dear all,

I would have a list of points that belong to different planes, what I need to do is to find local minima/maxima of various properties around these specific points. I know that maxima and minima can be found also in pvpython, is there a way to get only maxima and minima in the surroundings (eg. within a radius) of a point automatically?

Edit:  
I found something that looks quite promising:

 ![image](https://discourse.paraview.org/uploads/default/original/2X/9/90d4ab82b26e156ea1027ad3a9e0f3f4d9f79bb3.png)  
But I need to automate that in pvpython

Thank you in advance

---

<div class="post-metadata">

### Author: ![Kenichiro-Yoshimi](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.paraview.org/u/Kenichiro-Yoshimi)
#### Post date: [July 28, 2022, 3:26am UTC](https://discourse.paraview.org/t/find-maximum-minimum-value-in-the-neighbourhood-of-point/10055/2 "2022-07-28T03:26:18Z")

</div>

Hi,

I think you can achieve what you want to do by using `vtkStaticPointLocator`’s `FindPointsWithinRadius()` function within a **Programmable Filter**.  
[https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html](https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html)

Below is an example of using `FindPointsWithinRadius()` in vtk, which is a little different from the `Programmable Filter` syntax, but should not be too difficult to translate.

```python
import vtk
from vtk.util.numpy_support import numpy_to_vtk
import numpy as np
import time

# create hexahedron cells
source = vtk.vtkCellTypeSource()
source.SetCellType(vtk.VTK_HEXAHEDRON)
source.SetBlocksDimensions(10, 10, 10)
source.Update()

source_writer = vtk.vtkXMLUnstructuredGridWriter()
source_writer.SetFileName('source.vtu')
source_writer.SetInputConnection(source.GetOutputPort())
source_writer.Write()

# Initialize the locator
locator = vtk.vtkStaticPointLocator()
locator.SetDataSet(source.GetOutput())
locator.BuildLocator()

# Create a point cloud
n = 8000
r = 5.0
p_source = vtk.vtkPointSource()
p_source.SetNumberOfPoints(n)
p_source.SetCenter(5, 5, 5)
p_source.SetRadius(r)
p_source.Update()
seeds = p_source.GetOutput()

scalars = source.GetOutput().GetPointData().GetArray('DistanceToCenter')

# Start measuring the time
time_start = time.time()

# Find the maximum and minimum scalar values within the search radius
search_r = 3.0
max_scalars = []
min_scalars = []
nei_ids = vtk.vtkIdList()
for i in range(seeds.GetNumberOfPoints()):
    point = seeds.GetPoint(i)
    locator.FindPointsWithinRadius(search_r, point, nei_ids)

    nei_scalars = []
    for j in range(nei_ids.GetNumberOfIds()):
        id = nei_ids.GetId(j)
        nei_scalars.append(scalars.GetTuple1(id))

    max_scalar = max(nei_scalars)
    min_scalar = min(nei_scalars)

    max_scalars.append(max_scalar)
    min_scalars.append(min_scalar)

# Add and output an array of maximum and minimum values, respectively
vtk_max_scalars = numpy_to_vtk(num_array=np.array(max_scalars), deep=True, array_type=vtk.VTK_FLOAT)
vtk_max_scalars.SetName('max_scalars')
seeds.GetPointData().AddArray(vtk_max_scalars)

vtk_min_scalars = numpy_to_vtk(num_array=np.array(min_scalars), deep=True, array_type=vtk.VTK_FLOAT)
vtk_min_scalars.SetName('min_scalars')
seeds.GetPointData().AddArray(vtk_min_scalars)

time_end = time.time()
print(f'Search elapsed time for maximum and minimum values: {time_end - time_start} sec')

writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName('seeds.vtp')
writer.SetInputData(seeds)
writer.Write()

```

---

<div class="post-metadata">

### Author: ![s195t](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@s195t](https://discourse.paraview.org/u/s195t)
#### Post date: [August 22, 2022, 8:45am UTC](https://discourse.paraview.org/t/find-maximum-minimum-value-in-the-neighbourhood-of-point/10055/3 "2022-08-22T08:45:48Z")

</div>

Thanks a lot this solved my issue!
