Skip to content
Snippets Groups Projects

Resolve "(LearnerND) add iso-surface plot feature"

Merged Jorn Hoofwijk requested to merge 112-learnernd-add-iso-surface-plot-feature into master
Compare and Show latest version
1 file
+ 24
14
Compare changes
  • Side-by-side
  • Inline
@@ -600,7 +600,8 @@ class LearnerND(BaseLearner):
db = abs(value_b - level)
dab = da + db
new_pt = db / dab * np.array(vertex_a) + da / dab * np.array(vertex_b)
new_pt = (db / dab * np.array(vertex_a)
+ da / dab * np.array(vertex_b))
new_index = len(vertices)
vertices.append(new_pt)
@@ -633,22 +634,28 @@ class LearnerND(BaseLearner):
raise ValueError(
f"Could not draw isosurface for level={level}, as"
" this value is not inside the function range. Please choose a level"
f" strictly inside interval ({r_min}, {r_max})"
" this value is not inside the function range. Please choose"
f" a level strictly inside interval ({r_min}, {r_max})"
)
return vertices, faces
def plot_isosurface(self, level=0.0, hull_opacity=0.2):
"""Plots the linearly interpolated iso-surface of the function, based on
the currently evaluated points. This is the 3d analog of an iso-line.
"""Plots the linearly interpolated isosurface of the function,
based on the currently evaluated points. This is the 3D analog
of an isoline.
Parameters
----------
level : float
the function value which you are interested in. Defaults to 0.0.
hull_opacity : float
the opacity of the hull of the domain. Defaults to 0.2
level : float, default 0.0
the function value which you are interested in.
hull_opacity : float, default 0.0
the opacity of the hull of the domain.
Returns
-------
plot : plotly.offline.iplot object
The plot object of the isosurface.
"""
plotly = ensure_plotly()
@@ -656,7 +663,8 @@ class LearnerND(BaseLearner):
x, y, z = zip(*vertices)
fig = plotly.figure_factory.create_trisurf(
x=x, y=y, z=z, plot_edges=False, simplices=faces, title="Isosurface")
x=x, y=y, z=z, plot_edges=False,
simplices=faces, title="Isosurface")
if hull_opacity < 1e-3:
# Do not compute the hull_mesh.
@@ -669,8 +677,8 @@ class LearnerND(BaseLearner):
plotly = ensure_plotly()
hull = scipy.spatial.ConvexHull(self._bounds_points)
# Find the colors of each plane, giving triangles which are coplanar the
# same color, such that a square face has the same color.
# Find the colors of each plane, giving triangles which are coplanar
# the same color, such that a square face has the same color.
color_dict = {}
def _get_plane_color(simplex):
@@ -678,12 +686,14 @@ class LearnerND(BaseLearner):
# If the volume of the two triangles combined is zero then they
# belong to the same plane.
for simplex_key, color in color_dict.items():
points = np.array([hull.points[i] for i in set(simplex_key + simplex)])
points = [hull.points[i] for i in set(simplex_key + simplex)]
points = np.array(points)
if np.linalg.matrix_rank(points[1:] - points[0]) < 3:
return color
if scipy.spatial.ConvexHull(points).volume < 1e-5:
return color
color_dict[simplex] = tuple(random.randint(0, 255) for _ in range(3))
color_dict[simplex] = tuple(random.randint(0, 255)
for _ in range(3))
return color_dict[simplex]
colors = [_get_plane_color(simplex) for simplex in hull.simplices]
Loading