-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create mesh visualization #184
base: main
Are you sure you want to change the base?
Changes from 18 commits
2ce7bef
df7611e
805c75b
393d2ea
63fb47a
979e739
950dc17
f619b6e
28c0839
e7590d9
9e3a3e6
d056bae
9f5c0de
d5834af
6efc237
d555fa0
f1b9e4d
0d6d577
c36b25f
c334c05
c27ab21
aef5a64
ffb67bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,12 +2,14 @@ | |||||||||
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department | ||||||||||
# Distributed under the terms of "New BSD License", see the LICENSE file. | ||||||||||
|
||||||||||
from __future__ import annotations | ||||||||||
import warnings | ||||||||||
|
||||||||||
from ase.atoms import Atoms | ||||||||||
import numpy as np | ||||||||||
from typing import Optional | ||||||||||
from typing import Optional, Union | ||||||||||
from scipy.interpolate import interp1d | ||||||||||
from structuretoolkit.common.helper import get_cell | ||||||||||
|
||||||||||
from structuretoolkit.common.helper import get_cell | ||||||||||
|
||||||||||
|
@@ -807,3 +809,78 @@ def _get_flattened_orientation( | |||||||||
flattened_orientation[:3, :3] = _get_orientation(view_plane) | ||||||||||
|
||||||||||
return (distance_from_camera * flattened_orientation).ravel().tolist() | ||||||||||
|
||||||||||
|
||||||||||
def plot_isosurface( | ||||||||||
mesh, | ||||||||||
value, | ||||||||||
cell: Optional[Union[Atoms, list, np.ndarray, float]] = None, | ||||||||||
structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing import for The +import plotly.graph_objects as go Committable suggestion
Suggested change
ToolsRuff
|
||||||||||
isomin: Optional[float] = None, | ||||||||||
isomax: Optional[float] = None, | ||||||||||
surface_fill: Optional[float] = None, | ||||||||||
opacity: Optional[float] = None, | ||||||||||
surface_count: Optional[int] = 5, | ||||||||||
samwaseda marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
colorbar_nticks: Optional[int] = None, | ||||||||||
caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid mutable default arguments. Using mutable default arguments can lead to unexpected behavior. Replace with - caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False),
+ caps: Optional[dict] = None, Initialize within the function: if caps is None:
caps = dict(x_show=False, y_show=False, z_show=False) ToolsRuff
|
||||||||||
colorscale: Optional[str] = None, | ||||||||||
height: Optional[float] = 600, | ||||||||||
samwaseda marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
camera: Optional[str] = "orthographic", | ||||||||||
**kwargs, | ||||||||||
): | ||||||||||
""" | ||||||||||
Make a mesh plot | ||||||||||
|
||||||||||
Args: | ||||||||||
mesh (numpy.ndarray): Mesh grid. Must have a shape of (3, nx, ny, nz). | ||||||||||
It can be generated from structuretoolkit.create_mesh | ||||||||||
value: (numpy.ndarray): Value to plot. Must have a shape of (nx, ny, nz) | ||||||||||
cell (Atoms|ndarray|list|float|tuple): Cell, ignored if | ||||||||||
`structure_plot` is given | ||||||||||
structure_plot (plotly.graph_objs._figure.Figure): Plot of the | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the type hint should show import directly from the public part of the API ( |
||||||||||
structure to overlay. You should basically always use | ||||||||||
structuretoolkit.plot3d(structure, mode="plotly") | ||||||||||
isomin(float): Min color value | ||||||||||
isomax(float): Max color value | ||||||||||
surface_fill(float): Polygonal filling of the surface to choose between | ||||||||||
0 and 1 | ||||||||||
opacity(float): Opacity | ||||||||||
surface_count(int): Number of isosurfaces, 5 by default, which means | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you...but why? I would have thought it was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I thought about it a little bit XD The problem is that somehow plotly sets the two layers for the highest value and the lowest value. It makes only sense if there are multiple points with the max/min value, but that's rarely the case. So even with 5, in reality you would see only 3 in the middle. I also thought about changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's very fair. Maybe something like |
||||||||||
only min and max | ||||||||||
colorbar_nticks(int): Colorbar ticks correspond to isosurface values | ||||||||||
caps(dict): Whether to set cap on sides or not. Default is False. You | ||||||||||
can set: caps=dict(x_show=True, y_show=True, z_show=True) | ||||||||||
colorscale(str): Colorscale ("turbo", "gnbu" etc.) | ||||||||||
height(float): Height of the figure. 600px by default | ||||||||||
camera(str): Camera perspective to choose from "orthographic" and | ||||||||||
"perspective". Default is "orthographic" | ||||||||||
""" | ||||||||||
try: | ||||||||||
import plotly.graph_objects as go | ||||||||||
except ModuleNotFoundError: | ||||||||||
raise ModuleNotFoundError("plotly not installed") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception handling. Use - raise ModuleNotFoundError("plotly not installed")
+ raise ModuleNotFoundError("plotly not installed") from None Committable suggestion
Suggested change
ToolsRuff
|
||||||||||
x_mesh = np.reshape(mesh, (3, -1)) | ||||||||||
data = go.Isosurface( | ||||||||||
x=x_mesh[0], | ||||||||||
y=x_mesh[1], | ||||||||||
z=x_mesh[2], | ||||||||||
value=np.array(value).flatten(), | ||||||||||
isomin=isomin, | ||||||||||
isomax=isomax, | ||||||||||
surface_fill=surface_fill, | ||||||||||
opacity=opacity, | ||||||||||
surface_count=surface_count, | ||||||||||
colorbar_nticks=colorbar_nticks, | ||||||||||
caps=caps, | ||||||||||
colorscale=colorscale, | ||||||||||
**kwargs, | ||||||||||
) | ||||||||||
fig = go.Figure(data=data) | ||||||||||
if structure_plot is not None: | ||||||||||
fig = go.Figure(data=fig.data + structure_plot.data) | ||||||||||
elif cell is not None: | ||||||||||
fig = _draw_box_plotly(fig, cell) | ||||||||||
fig.update_scenes(aspectmode="data") | ||||||||||
fig.layout.scene.camera.projection.type = camera | ||||||||||
fig.update_layout(autosize=True, height=height) | ||||||||||
return fig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing import for
Union
.The
Union
type hint is used but not imported. Import it from thetyping
module.+from typing import Union
Committable suggestion