Skip to content
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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Create mesh visualization #184

wants to merge 23 commits into from

Conversation

samwaseda
Copy link
Member

@samwaseda samwaseda commented May 17, 2024

Example:

import structuretoolkit as stk
from ase.build import bulk
import numpy as np
structure = bulk("Al", cubic=True)
mesh = stk.create_mesh(structure, n_mesh=20)
y = np.prod(np.sin(2 * np.pi * mesh / structure.cell.diagonal()), axis=-1)
stk.plot_isosurface(mesh, y, surface_count=10, surface_fill=0.5, colorscale="turbo")

Output:

Screenshot 2024-05-17 at 18 34 32

Summary by CodeRabbit

  • New Features
    • Added a new plot_isosurface function for creating detailed mesh plots using plotly.
    • Users can now visualize isosurfaces with customizable parameters such as color, opacity, surface count, and camera perspective.

@samwaseda samwaseda requested a review from jan-janssen as a code owner May 17, 2024 16:35
Copy link

coderabbitai bot commented May 17, 2024

Walkthrough

The recent changes introduce a new function, plot_isosurface, in the structuretoolkit.visualize module to enhance visualization capabilities. This function allows users to create isosurface plots using plotly. The structuretoolkit/__init__.py file has been updated to include the new plot_isosurface function, ensuring it is readily accessible alongside the existing plot3d function.

Changes

Files Change Summary
structuretoolkit/__init__.py Added the plot_isosurface function import from structuretoolkit.visualize alongside plot3d.
structuretoolkit/visualize.py Introduced plot_isosurface function utilizing plotly; updated imports and provided documentation.

Poem

In the realm of code’s grand display,
A new function comes to play,
Isosurfaces now can gleam,
In plotly, they cut a beam.
Visualization takes new flight,
Structure plots in vibrant light. 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@samwaseda samwaseda requested review from pmrv, liamhuber and mbruns91 May 17, 2024 16:35
@liamhuber liamhuber removed their request for review May 17, 2024 16:41
@liamhuber
Copy link
Member

Re-assign me when the CI debacle is resolved and I'll take a look. I can say I'm definitely a fan of the end result though! 🚀

@jan-janssen
Copy link
Member

Re-assign me when the CI debacle is resolved and I'll take a look. I can say I'm definitely a fan of the end result though! 🚀

To trigger the CI just open a pull request directly on main #185

@samwaseda samwaseda removed request for pmrv and mbruns91 May 17, 2024 17:06
@samwaseda
Copy link
Member Author

I got the feeling that I should make an example notebook for this PR. What do you think?

An error occurred while trying to automatically change base from create_mesh to main May 21, 2024 06:36
Copy link
Member

@liamhuber liamhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No major issues, just a bunch of nits from my end.

A bunch of the inputs take a None value; in this case it would be nice to know what the default behaviour is. I see that these basically all passed directly on to plotly.graph_object.Isosurface. From my perspective, it would be sufficient to then have something like "cf. plotly.graph_object.Isosurface docs for default behaviour on optional input". The only thing I don't like about that is that when I go to the docs for that class (sorry, link is just to the whole module page, I don't seem to be able to get a section link...), I don't see any explanations there either! This is not your fault, but if you know where to find an actual description of these parameters, it might be worth linking it...

Finally, would it make sense to directly expose something like **plotly_isosurface_kwargs and pass these on directly, trusting the user to not screw things up if they make these kwargs non-empty?

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
Copy link
Member

Choose a reason for hiding this comment

The 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 (https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html) and not ..._figure.Figure

structuretoolkit/visualize.py Outdated Show resolved Hide resolved
structuretoolkit/visualize.py Outdated Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you...but why? I would have thought it was 2 just like it was until the last commit (at time of writing). This is so weird it might warrant some sort of "I know, I know, but seriously, it's because X" in the docstring.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 isomax and isomin, but that felt a bit like too much manipulation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very fair. Maybe something like , which actually means only min and max because of details of how plotly handles the levels to explain the weirdness just a little?

Copy link
Member

@liamhuber liamhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to keep implementing any suggestions from my end that you think are useful, but the low hanging fruit has been fixed and I'm happy with this now.

Copy link

@mbruns91 mbruns91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With doing the changes/additions in the docstring suggested by @liamhuber , this PR is perfectly fine to merge from my side. Only thing is that I guess usage is a bit obscure to a user without prior knowledge, so I think that you really should add an example notebook (as you already suggested earlier).

@mbruns91 mbruns91 linked an issue Jun 4, 2024 that may be closed by this pull request
Base automatically changed from create_mesh to main June 5, 2024 08:36
jan-janssen and others added 2 commits July 18, 2024 08:32
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range, codebase verification and nitpick comments (2)
structuretoolkit/__init__.py (1)

97-97: Ensure usage of imported functions.

The plot_isosurface function is imported but not used within the codebase. Ensure that it is utilized appropriately or added to __all__ if intended for public API access.

  • structuretoolkit/__init__.py: Import statement at line 97.
Analysis chain

Ensure usage of imported functions.

The plot_isosurface function is imported but not used within this file. Ensure that it is utilized appropriately or added to __all__ if intended for public API access.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of `plot_isosurface` within the codebase.

# Test: Search for the function usage. Expect: At least one occurrence of the function usage.
rg --type python -A 5 $'plot_isosurface'

Length of output: 70


Script:

#!/bin/bash
# Description: Verify usage of `plot_isosurface` within the codebase.

# Test: Search for the function usage. Expect: At least one occurrence of the function usage.
rg 'plot_isosurface'

Length of output: 163

Tools
Ruff

97-97: structuretoolkit.visualize.plot3d imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


97-97: structuretoolkit.visualize.plot_isosurface imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

structuretoolkit/visualize.py (1)

831-857: Enhance function documentation.

The function docstring is clear but can benefit from additional details and formatting improvements.

    """
    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
            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
            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".

    Returns:
        plotly.graph_objs._figure.Figure: The plotly figure object.
    """
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8fc7273 and ffb67bf.

Files selected for processing (2)
  • structuretoolkit/init.py (1 hunks)
  • structuretoolkit/visualize.py (2 hunks)
Additional context used
Ruff
structuretoolkit/__init__.py

97-97: structuretoolkit.visualize.plot3d imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


97-97: structuretoolkit.visualize.plot_isosurface imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

structuretoolkit/visualize.py

817-817: Undefined name Union

(F821)


818-818: Undefined name plotly

(F821)


825-825: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


861-861: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

Additional comments not posted (1)
structuretoolkit/visualize.py (1)

5-5: LGTM!

The import statement for future annotations is correct and follows best practices.

opacity: Optional[float] = None,
surface_count: int = 5,
colorbar_nticks: Optional[int] = None,
caps: Optional[dict] = dict(x_show=False, y_show=False, z_show=False),
Copy link

Choose a reason for hiding this comment

The 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 None and initialize within the function.

-    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)
Tools
Ruff

825-825: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

try:
import plotly.graph_objects as go
except ModuleNotFoundError:
raise ModuleNotFoundError("plotly not installed")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve exception handling.

Use raise ... from None to distinguish exceptions from errors in exception handling.

-        raise ModuleNotFoundError("plotly not installed")
+        raise ModuleNotFoundError("plotly not installed") from None
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
raise ModuleNotFoundError("plotly not installed")
raise ModuleNotFoundError("plotly not installed") from None
Tools
Ruff

861-861: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

@@ -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(
Copy link

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 the typing module.

+from typing import Union
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def plot_isosurface(
+from typing import Union
def plot_isosurface(

mesh,
value,
cell: Optional[Union[Atoms, list, np.ndarray, float]] = None,
structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing import for plotly.

The plotly module is referenced in type hints but not imported. Import it to avoid undefined name errors.

+import plotly.graph_objects as go
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None,
import plotly.graph_objects as go
structure_plot: Optional["plotly.graph_objs._figure.Figure"] = None,
Tools
Ruff

818-818: Undefined name plotly

(F821)

@jan-janssen jan-janssen marked this pull request as draft July 18, 2024 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add visualisation
5 participants