From ebfe69f6d1c6c4bc563777076550ca5f358e7f78 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 30 Dec 2025 11:11:09 -0800 Subject: [PATCH 1/5] update: fix rdf plot on pyodide --- utils/plot.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/utils/plot.py b/utils/plot.py index 238ec608..314efc48 100644 --- a/utils/plot.py +++ b/utils/plot.py @@ -1,5 +1,9 @@ +import io +import sys from typing import Dict, List, Union +import matplotlib +import matplotlib.pyplot as plt from mat3ra.made.material import Material from mat3ra.made.tools.analyze.interface import ZSLMatchHolder from mat3ra.made.tools.analyze.rdf import RadialDistributionFunction @@ -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}
Angle: {angle:.2f}°
Atoms: {size}
") - trace_names.append(f"Interface {i+1}") + hover_texts.append(f"Interface {i + 1}
Angle: {angle:.2f}°
Atoms: {size}
") + trace_names.append(f"Interface {i + 1}") plot_settings = {"x_title": "Twist Angle (°)", "y_title": "Number of Atoms", "title": "Twisted Interface Solutions"} @@ -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: + matplotlib.use("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: + from IPython.display import Image, display + + buf = io.BytesIO() + plt.savefig(buf, format="png") + buf.seek(0) + display(Image(buf.read())) + plt.close() From 6fd5551c740721e22a1b5024cc9fc3134c0ebd17 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 30 Dec 2025 11:12:55 -0800 Subject: [PATCH 2/5] chore: MP source --- other/materials_designer/uploads/0-Ni.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/materials_designer/uploads/0-Ni.json b/other/materials_designer/uploads/0-Ni.json index 2adc9475..cd92b2d0 100644 --- a/other/materials_designer/uploads/0-Ni.json +++ b/other/materials_designer/uploads/0-Ni.json @@ -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 From 5612a986676ca59c5fc1a1d7ef839049aca59b0c Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 30 Dec 2025 11:46:48 -0800 Subject: [PATCH 3/5] fix import --- utils/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/plot.py b/utils/plot.py index 314efc48..8cdb109a 100644 --- a/utils/plot.py +++ b/utils/plot.py @@ -3,7 +3,7 @@ from typing import Dict, List, Union import matplotlib -import matplotlib.pyplot as plt +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 @@ -87,7 +87,7 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) ) if is_pyodide: - from IPython.display import Image, display + import matplotlib.pyplot as plt buf = io.BytesIO() plt.savefig(buf, format="png") From 1d9469e57aea3724a1521b52b7992bdc283ab816 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 30 Dec 2025 12:17:47 -0800 Subject: [PATCH 4/5] fix plt --- utils/plot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/plot.py b/utils/plot.py index 8cdb109a..c44e1068 100644 --- a/utils/plot.py +++ b/utils/plot.py @@ -2,12 +2,12 @@ import sys from typing import Dict, List, Union -import matplotlib 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: @@ -79,7 +79,7 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) """ is_pyodide = sys.platform == "emscripten" if is_pyodide: - matplotlib.use("Agg") + plt.switch_backend("Agg") rdf = RadialDistributionFunction.from_material(material, cutoff=cutoff, bin_size=bin_size) plot_distribution_function( @@ -87,8 +87,6 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) ) if is_pyodide: - import matplotlib.pyplot as plt - buf = io.BytesIO() plt.savefig(buf, format="png") buf.seek(0) From 96bd023d3dc6039d945295164e38e92232b938a4 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 30 Dec 2025 21:41:06 -0800 Subject: [PATCH 5/5] chore: add comment --- utils/plot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/plot.py b/utils/plot.py index c44e1068..c913fef2 100644 --- a/utils/plot.py +++ b/utils/plot.py @@ -79,6 +79,7 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) """ 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) @@ -87,6 +88,7 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) ) if is_pyodide: + # Necessary to display the plot in Pyodide environment buf = io.BytesIO() plt.savefig(buf, format="png") buf.seek(0)