Skip to content

Commit

Permalink
Fix a few minor contour()-related issues (#2951)
Browse files Browse the repository at this point in the history
* Check for sane rng in contour filter

* Fix level specification in flying edges example

* Extend contour() scalars type checking

* Make error less confusing by making it less specific

* One more contour example fix

* Check for rng length

* Cover errors with tests

* Cover invalid scalars type check in contour

* Fix the fix in the flying edges example
  • Loading branch information
adeak committed Jul 7, 2022
1 parent 47ddcd9 commit dc3e83c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 4 additions & 4 deletions examples/01-filter/flying_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def spider_cage(x, y, z):

# sample and plot
values = spider_cage(x, y, z)
mesh = grid.contour(1, values, method='marching_cubes', rng=[1, 0])
mesh = grid.contour([1], values, method='marching_cubes')
dist = np.linalg.norm(mesh.points, axis=1)
mesh.plot(scalars=dist, smooth_shading=True, specular=5, cmap="plasma", show_scalar_bar=False)

Expand Down Expand Up @@ -88,7 +88,7 @@ def barth_sextic(x, y, z):

# sample and plot
values = barth_sextic(x, y, z)
mesh = grid.contour(1, values, method='flying_edges', rng=[-0.0, 0])
mesh = grid.contour([0], values, method='flying_edges')
dist = np.linalg.norm(mesh.points, axis=1)
mesh.plot(scalars=dist, smooth_shading=True, specular=5, cmap="plasma", show_scalar_bar=False)

Expand All @@ -104,7 +104,7 @@ def angle_to_range(angle):
return -2 * np.sin(angle)


mesh = grid.contour(1, values, method='flying_edges', rng=[angle_to_range(0), 0])
mesh = grid.contour([angle_to_range(0)], values, method='flying_edges')
dist = np.linalg.norm(mesh.points, axis=1)

pl = pv.Plotter()
Expand All @@ -120,7 +120,7 @@ def angle_to_range(angle):
pl.open_gif('barth_sextic.gif')

for angle in np.linspace(0, np.pi, 15)[:-1]:
new_mesh = grid.contour(1, values, method='flying_edges', rng=[angle_to_range(angle), 0])
new_mesh = grid.contour([angle_to_range(angle)], values, method='flying_edges')
mesh.overwrite(new_mesh)
pl.update_scalars(np.linalg.norm(new_mesh.points, axis=1), render=False)
pl.write_frame()
Expand Down
13 changes: 10 additions & 3 deletions pyvista/core/filters/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,15 @@ def contour(
else:
raise ValueError(f"Method '{method}' is not supported")

if rng is not None:
if not isinstance(rng, (np.ndarray, collections.abc.Sequence)):
raise TypeError(f'Array-like rng expected, got {type(rng).__name__}.')
rng_shape = np.shape(rng)
if rng_shape != (2,):
raise ValueError(f'rng must be a two-length array-like, not {rng}.')
if rng[0] > rng[1]:
raise ValueError(f'rng must be a sorted min-max pair, not {rng}.')

if isinstance(scalars, str):
scalars_name = scalars
elif isinstance(scalars, (collections.abc.Sequence, np.ndarray)):
Expand Down Expand Up @@ -1579,9 +1588,7 @@ def contour(
field = get_array_association(self, scalars_name, preference=preference)
# NOTE: only point data is allowed? well cells works but seems buggy?
if field != FieldAssociation.POINT:
raise TypeError(
f'Contour filter only works on Point data. Array ({scalars}) is in the Cell data.'
)
raise TypeError('Contour filter only works on point data.')
alg.SetInputArrayToProcess(
0,
0,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ def test_contour_errors(uniform):
uniform.contour(method='invalid method')
with pytest.raises(TypeError, match='Invalid type for `scalars`'):
uniform.contour(scalars=1)
with pytest.raises(TypeError):
uniform.contour(rng={})
with pytest.raises(ValueError, match='rng must be a two-length'):
uniform.contour(rng=[1])
with pytest.raises(ValueError, match='rng must be a sorted'):
uniform.contour(rng=[2, 1])


def test_elevation():
Expand Down

0 comments on commit dc3e83c

Please sign in to comment.