From 2ce7bef316f57047594f675f4b0c7a7711ede2ef Mon Sep 17 00:00:00 2001 From: samwaseda Date: Fri, 17 May 2024 16:25:18 +0000 Subject: [PATCH 01/15] add docstring --- structuretoolkit/visualize.py | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 4f0f67078..730180056 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -800,3 +800,73 @@ 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, + structure_plot=None, + isomin: float = None, + isomax: float = None, + surface_fill: float = None, + opacity: float = None, + surface_count: int = None, + colorbar_nticks: int = None, + caps: dict = dict(x_show=False, y_show=False, z_show=False), + colorscale: str = None, + height: float = 600, + camera: str = "orthographic", + **kwargs +): + """ + Make a mesh plot + + Args: + mesh (numpy.ndarray): Mesh grid. Must have a shape of (nx, ny, nz, 3). + It can be generated from structuretoolkit.create_mesh + value: (numpy.ndarray): Value to plot. Must have a shape of (nx, ny, nz) + structure_plot (plotly.graph_objs._figure.Figure): Plot of the + 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, 2 by default, which means + 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", "Gnuplot" 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 - use plot3d instead") + x_mesh = np.reshape(mesh, (-1, 3)).T + 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) + fig.update_scenes(aspectmode='data') + fig.layout.scene.camera.projection.type = "camera" + fig.update_layout(autosize=True, height=height) + return fig From df7611e974e36517d28dc29e717c788f6547acf9 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Fri, 17 May 2024 16:26:49 +0000 Subject: [PATCH 02/15] add it in __init__ --- structuretoolkit/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/__init__.py b/structuretoolkit/__init__.py index b4b80cff9..4e13e0680 100644 --- a/structuretoolkit/__init__.py +++ b/structuretoolkit/__init__.py @@ -55,7 +55,7 @@ ) # Visualize -from structuretoolkit.visualize import plot3d +from structuretoolkit.visualize import plot3d, plot_isosurface # Analyse - for backwards compatibility from structuretoolkit.analyse import ( From 805c75b03f2987e26619be482112a33ffbbe97f1 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Fri, 17 May 2024 16:33:02 +0000 Subject: [PATCH 03/15] small fixes --- structuretoolkit/visualize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 730180056..d789af45a 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -838,7 +838,7 @@ def plot_isosurface( 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", "Gnuplot" etc.) + 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" @@ -867,6 +867,6 @@ def plot_isosurface( if structure_plot is not None: fig = go.Figure(data=fig.data + structure_plot.data) fig.update_scenes(aspectmode='data') - fig.layout.scene.camera.projection.type = "camera" + fig.layout.scene.camera.projection.type = camera fig.update_layout(autosize=True, height=height) return fig From 63fb47a52bf47c42ece5b9b3ff5f1ede77057552 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Fri, 17 May 2024 16:54:01 +0000 Subject: [PATCH 04/15] update type hinting --- structuretoolkit/visualize.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index d789af45a..56d599e90 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -806,16 +806,16 @@ def plot_isosurface( mesh, value, structure_plot=None, - isomin: float = None, - isomax: float = None, - surface_fill: float = None, - opacity: float = None, - surface_count: int = None, - colorbar_nticks: int = None, - caps: dict = dict(x_show=False, y_show=False, z_show=False), - colorscale: str = None, - height: float = 600, - camera: str = "orthographic", + isomin: Optional[float] = None, + isomax: Optional[float] = None, + surface_fill: Optional[float] = None, + opacity: Optional[float] = None, + surface_count: Optional[int] = None, + colorbar_nticks: Optional[int] = None, + caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), + colorscale: Optional[str] = None, + height: Optional[float] = 600, + camera: Optional[str] = "orthographic", **kwargs ): """ From 979e7396cec46a7685fba624918be2c8740ca677 Mon Sep 17 00:00:00 2001 From: pyiron-runner Date: Fri, 17 May 2024 17:01:32 +0000 Subject: [PATCH 05/15] Format black --- structuretoolkit/visualize.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 56d599e90..6db79c0c9 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -816,7 +816,7 @@ def plot_isosurface( colorscale: Optional[str] = None, height: Optional[float] = 600, camera: Optional[str] = "orthographic", - **kwargs + **kwargs, ): """ Make a mesh plot @@ -861,12 +861,12 @@ def plot_isosurface( colorbar_nticks=colorbar_nticks, caps=caps, colorscale=colorscale, - **kwargs + **kwargs, ) fig = go.Figure(data=data) if structure_plot is not None: fig = go.Figure(data=fig.data + structure_plot.data) - fig.update_scenes(aspectmode='data') + fig.update_scenes(aspectmode="data") fig.layout.scene.camera.projection.type = camera fig.update_layout(autosize=True, height=height) return fig From f619b6e6009aca5a4ea3820659626e8a8145c9d7 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 18 May 2024 07:47:21 +0000 Subject: [PATCH 06/15] update the mesh definition --- structuretoolkit/visualize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 56d599e90..cf8f74584 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -822,7 +822,7 @@ def plot_isosurface( Make a mesh plot Args: - mesh (numpy.ndarray): Mesh grid. Must have a shape of (nx, ny, nz, 3). + 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) structure_plot (plotly.graph_objs._figure.Figure): Plot of the @@ -847,7 +847,7 @@ def plot_isosurface( import plotly.graph_objects as go except ModuleNotFoundError: raise ModuleNotFoundError("plotly not installed - use plot3d instead") - x_mesh = np.reshape(mesh, (-1, 3)).T + x_mesh = np.reshape(mesh, (3, -1)) data = go.Isosurface( x=x_mesh[0], y=x_mesh[1], From 28c0839f45c2524005604bd334bec3156309fb86 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 18 May 2024 07:47:41 +0000 Subject: [PATCH 07/15] update error message --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index cf8f74584..310908b43 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -846,7 +846,7 @@ def plot_isosurface( try: import plotly.graph_objects as go except ModuleNotFoundError: - raise ModuleNotFoundError("plotly not installed - use plot3d instead") + raise ModuleNotFoundError("plotly not installed") x_mesh = np.reshape(mesh, (3, -1)) data = go.Isosurface( x=x_mesh[0], From d056baeddcae7256d6ad1804d53284a175a41783 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sun, 19 May 2024 16:31:44 +0000 Subject: [PATCH 08/15] add type hinting --- structuretoolkit/visualize.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 310908b43..1d844331b 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -2,6 +2,7 @@ # 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 @@ -805,7 +806,7 @@ def _get_flattened_orientation( def plot_isosurface( mesh, value, - structure_plot=None, + structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None, isomin: Optional[float] = None, isomax: Optional[float] = None, surface_fill: Optional[float] = None, From d5834af4d15e47b6cc631518e1a9a10cbcb8d238 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sun, 19 May 2024 16:41:57 +0000 Subject: [PATCH 09/15] add cell --- structuretoolkit/visualize.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 3941ca628..be665844f 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -7,8 +7,9 @@ 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 __author__ = "Joerg Neugebauer, Sudarsan Surendralal" __copyright__ = ( @@ -806,6 +807,7 @@ def _get_flattened_orientation( def plot_isosurface( mesh, value, + cell: Optional[Union[Atoms, list, np.ndarray, float]] = None, structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None, isomin: Optional[float] = None, isomax: Optional[float] = None, @@ -826,6 +828,8 @@ def plot_isosurface( 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 structure to overlay. You should basically always use structuretoolkit.plot3d(structure, mode="plotly") @@ -867,6 +871,8 @@ def plot_isosurface( 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: + cell = get_cell(cell) fig.update_scenes(aspectmode="data") fig.layout.scene.camera.projection.type = camera fig.update_layout(autosize=True, height=height) From d555fa0e97f7e10c65e0572ace0441875513ff65 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sun, 19 May 2024 16:49:09 +0000 Subject: [PATCH 10/15] add box drawing functionality --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 1ed74f9c8..b4dbb1e6e 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -875,7 +875,7 @@ def plot_isosurface( if structure_plot is not None: fig = go.Figure(data=fig.data + structure_plot.data) elif cell is not None: - cell = get_cell(cell) + 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) From 0d6d5771420492cb8df709ade0851a0cc88635a9 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Tue, 21 May 2024 13:52:23 +0000 Subject: [PATCH 11/15] change surface count --- structuretoolkit/visualize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 625bf46b1..927587778 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -820,7 +820,7 @@ def plot_isosurface( isomax: Optional[float] = None, surface_fill: Optional[float] = None, opacity: Optional[float] = None, - surface_count: Optional[int] = None, + surface_count: Optional[int] = 5, colorbar_nticks: Optional[int] = None, caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), colorscale: Optional[str] = None, @@ -845,7 +845,7 @@ def plot_isosurface( surface_fill(float): Polygonal filling of the surface to choose between 0 and 1 opacity(float): Opacity - surface_count(int): Number of isosurfaces, 2 by default, which means + surface_count(int): Number of isosurfaces, 5 by default, which means 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 From c36b25f5c6a50763375bf0d656370eeae1eb28fb Mon Sep 17 00:00:00 2001 From: Sam Dareska <37879103+samwaseda@users.noreply.github.com> Date: Tue, 21 May 2024 20:43:43 +0200 Subject: [PATCH 12/15] Update structuretoolkit/visualize.py Co-authored-by: Liam Huber --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 927587778..ef92d416f 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -824,7 +824,7 @@ def plot_isosurface( colorbar_nticks: Optional[int] = None, caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), colorscale: Optional[str] = None, - height: Optional[float] = 600, + height: float = 600., camera: Optional[str] = "orthographic", **kwargs, ): From c334c05f5cc388e8f43f74be144392baa57f61aa Mon Sep 17 00:00:00 2001 From: Sam Dareska <37879103+samwaseda@users.noreply.github.com> Date: Tue, 21 May 2024 20:44:08 +0200 Subject: [PATCH 13/15] Update structuretoolkit/visualize.py Co-authored-by: Liam Huber --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index ef92d416f..e398a2d9e 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -820,7 +820,7 @@ def plot_isosurface( isomax: Optional[float] = None, surface_fill: Optional[float] = None, opacity: Optional[float] = None, - surface_count: Optional[int] = 5, + surface_count: int = 5, colorbar_nticks: Optional[int] = None, caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), colorscale: Optional[str] = None, From c27ab218df4dec8d747fca44357dbb366edeafdb Mon Sep 17 00:00:00 2001 From: pyiron-runner Date: Wed, 22 May 2024 07:16:22 +0000 Subject: [PATCH 14/15] Format black --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index e398a2d9e..66969bc0f 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -824,7 +824,7 @@ def plot_isosurface( colorbar_nticks: Optional[int] = None, caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False), colorscale: Optional[str] = None, - height: float = 600., + height: float = 600.0, camera: Optional[str] = "orthographic", **kwargs, ): From ffb67bf5a6ec4fe7dce67568f4cf5f2f69b6573a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 18 Jul 2024 06:32:17 +0000 Subject: [PATCH 15/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- structuretoolkit/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structuretoolkit/visualize.py b/structuretoolkit/visualize.py index 0281837af..24cfb8b31 100644 --- a/structuretoolkit/visualize.py +++ b/structuretoolkit/visualize.py @@ -3,13 +3,13 @@ # Distributed under the terms of "New BSD License", see the LICENSE file. from __future__ import annotations + import warnings from typing import Optional import numpy as np from ase.atoms import Atoms from scipy.interpolate import interp1d -from structuretoolkit.common.helper import get_cell from structuretoolkit.common.helper import get_cell