Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion other/materials_designer/uploads/0-Ni.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"external": {
"id": "mp-23",
"source": "Materials Project",
"source": "MaterialsProject",
"doi": "10.17188/1199153",
"url": "https://next-gen.materialsproject.org/materials/mp-23",
"origin": true
Expand Down
21 changes: 19 additions & 2 deletions utils/plot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import io
import sys
from typing import Dict, List, Union

from IPython.display import Image, display
from mat3ra.made.material import Material
from mat3ra.made.tools.analyze.interface import ZSLMatchHolder
from mat3ra.made.tools.analyze.rdf import RadialDistributionFunction
from mat3ra.utils.jupyterlite.plot import plot_distribution_function, scatter_plot_2d
from matplotlib import pyplot as plt


def plot_strain_vs_area(matches: List["ZSLMatchHolder"], settings: Dict[str, Union[str, int]]) -> None:
Expand Down Expand Up @@ -60,8 +64,8 @@ def plot_twisted_interface_solutions(interfaces: List["Material"]) -> None:

x_values.append(angle)
y_values.append(size)
hover_texts.append(f"Interface {i+1}<br>Angle: {angle:.2f}°<br>Atoms: {size}<br>")
trace_names.append(f"Interface {i+1}")
hover_texts.append(f"Interface {i + 1}<br>Angle: {angle:.2f}°<br>Atoms: {size}<br>")
trace_names.append(f"Interface {i + 1}")

plot_settings = {"x_title": "Twist Angle (°)", "y_title": "Number of Atoms", "title": "Twisted Interface Solutions"}

Expand All @@ -73,7 +77,20 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1)
"""
Plot RDF for a material.
"""
is_pyodide = sys.platform == "emscripten"
if is_pyodide:
# This is needed so that plt is adjusted before import to work in Pyodide environment
plt.switch_backend("Agg")

rdf = RadialDistributionFunction.from_material(material, cutoff=cutoff, bin_size=bin_size)
plot_distribution_function(
rdf.bin_centers, rdf.rdf, xlabel="Distance (Å)", ylabel="g(r)", title="Radial Distribution Function (RDF)"
)

if is_pyodide:
# Necessary to display the plot in Pyodide environment
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
display(Image(buf.read()))
plt.close()
Copy link
Member

Choose a reason for hiding this comment

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

What happens if it's not pyodide? How do we plot RDF??

Copy link
Member Author

Choose a reason for hiding this comment

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

the previous function plot_distribution from utils

Copy link
Member

Choose a reason for hiding this comment

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

Add a comment explaining why this is needed