Skip to content

Commit

Permalink
Fix find_closest_cell examples doctest (#1809)
Browse files Browse the repository at this point in the history
* fix flaky doctest example

* format consistently with backticks
  • Loading branch information
MatthewFlamm committed Nov 18, 2021
1 parent 828fa34 commit 82a3135
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pyvista/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1900,32 +1900,34 @@ def find_closest_cell(self, point: Union[int, np.ndarray]) -> Union[int, np.ndar
Examples
--------
Find nearest cell to a point on a sphere, centered on the
origin.
Find nearest cell on a sphere centered on the
origin to the point ``[0.1, 0.2, 0.3]``.
>>> import pyvista
>>> mesh = pyvista.Sphere()
>>> index = mesh.find_closest_cell([0, 0, 0.5])
>>> point = [0.1, 0.2, 0.3]
>>> index = mesh.find_closest_cell(point)
>>> index
30
591
Make sure that this cell indeed is the closest to
``[0.1, 0.2, 0.3]``.
>>> import numpy as np
>>> cell_centers = mesh.cell_centers()
>>> relative_position = cell_centers.points - point
>>> distance = np.linalg.norm(relative_position, axis=1)
>>> np.argmin(distance)
591
Find the nearest cells to several random points that
are centered on the origin.
>>> import numpy as np
>>> points = 2 * np.random.random((5000, 3)) - 1
>>> indices = mesh.find_closest_cell(points)
>>> indices.shape
(5000,)
The average position of all the randomly found cell centers should
be reasonably close to the origin.
>>> cell_center_mesh = mesh.cell_centers()
>>> avg_pos = cell_center_mesh.points[indices, :].mean(axis=0)
>>> np.linalg.norm(avg_pos) < 0.02
True
"""
if isinstance(point, collections.abc.Sequence):
point = np.array(point)
Expand Down

0 comments on commit 82a3135

Please sign in to comment.