Spaces:
Runtime error
Runtime error
fomat isort, ruff
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from pymatgen.ext.matproj import MPRester
|
3 |
-
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter
|
4 |
-
from pymatgen.core.composition import Composition
|
5 |
-
import plotly.graph_objs as go
|
6 |
import os
|
7 |
-
from pymatgen.entries.computed_entries import ComputedEntry
|
8 |
-
from pymatgen.core import Composition
|
9 |
|
|
|
|
|
10 |
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Load only the train split of the dataset
|
13 |
dataset = load_dataset("LeMaterial/leDataset", split="train")
|
@@ -16,26 +16,38 @@ dataset = load_dataset("LeMaterial/leDataset", split="train")
|
|
16 |
train_df = dataset.to_pandas()
|
17 |
|
18 |
|
19 |
-
def create_phase_diagram(
|
|
|
|
|
20 |
# Split elements and remove any whitespace
|
21 |
-
element_list = [el.strip() for el in elements.split(
|
22 |
|
23 |
# Fetch all entries from the Materials Project database
|
24 |
-
entries = [
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
# Fetch elemental entries (they are usually GGA calculations)
|
32 |
elemental_entries = [e for e in entries if e.composition.is_element]
|
33 |
|
34 |
# Filter entries based on functional
|
35 |
if functional == "GGA":
|
36 |
-
entries = [
|
|
|
|
|
|
|
|
|
37 |
elif functional == "GGA+U":
|
38 |
-
entries = [
|
|
|
|
|
39 |
# Add elemental entries to ensure they are included
|
40 |
entries.extend([e for e in elemental_entries if e not in entries])
|
41 |
|
@@ -52,10 +64,14 @@ def create_phase_diagram(elements, max_e_above_hull, color_scheme, plot_style, f
|
|
52 |
else:
|
53 |
# For 3D plots, limit to ternary systems
|
54 |
if len(element_list) == 3:
|
55 |
-
plotter = PDPlotter(
|
|
|
|
|
56 |
fig = plotter.get_plot()
|
57 |
else:
|
58 |
-
return go.Figure().add_annotation(
|
|
|
|
|
59 |
|
60 |
# Adjust the maximum energy above hull
|
61 |
# (This is a placeholder as PDPlotter does not support direct filtering)
|
@@ -65,10 +81,17 @@ def create_phase_diagram(elements, max_e_above_hull, color_scheme, plot_style, f
|
|
65 |
|
66 |
|
67 |
# Define Gradio interface components
|
68 |
-
elements_input = gr.Textbox(
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
plot_style_dropdown = gr.Dropdown(choices=["2D", "3D"], label="Plot Style")
|
73 |
functional_dropdown = gr.Dropdown(choices=["GGA", "GGA+U", "Both"], label="Functional")
|
74 |
finite_temp_toggle = gr.Checkbox(label="Enable Finite Temperature Estimation")
|
@@ -82,11 +105,11 @@ iface = gr.Interface(
|
|
82 |
color_scheme_dropdown,
|
83 |
plot_style_dropdown,
|
84 |
functional_dropdown,
|
85 |
-
finite_temp_toggle
|
86 |
],
|
87 |
outputs=gr.Plot(label="Phase Diagram"),
|
88 |
title="Materials Project Phase Diagram",
|
89 |
-
description="Generate a phase diagram for a set of elements using Materials Project data."
|
90 |
)
|
91 |
|
92 |
# Launch the app
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
import plotly.graph_objs as go
|
5 |
from datasets import load_dataset
|
6 |
+
from pymatgen.analysis.phase_diagram import PDPlotter, PhaseDiagram
|
7 |
+
from pymatgen.core import Composition
|
8 |
+
from pymatgen.core.composition import Composition
|
9 |
+
from pymatgen.entries.computed_entries import ComputedEntry
|
10 |
+
from pymatgen.ext.matproj import MPRester
|
11 |
|
12 |
# Load only the train split of the dataset
|
13 |
dataset = load_dataset("LeMaterial/leDataset", split="train")
|
|
|
16 |
train_df = dataset.to_pandas()
|
17 |
|
18 |
|
19 |
+
def create_phase_diagram(
|
20 |
+
elements, max_e_above_hull, color_scheme, plot_style, functional, finite_temp
|
21 |
+
):
|
22 |
# Split elements and remove any whitespace
|
23 |
+
element_list = [el.strip() for el in elements.split("-")]
|
24 |
|
25 |
# Fetch all entries from the Materials Project database
|
26 |
+
entries = [
|
27 |
+
ComputedEntry(
|
28 |
+
Composition(row["chemical_formula_descriptive"]),
|
29 |
+
energy=row["energy"],
|
30 |
+
correction=row["energy_corrected"] - row["energy"],
|
31 |
+
entry_id=row["immutable_id"],
|
32 |
+
)
|
33 |
+
for n, row in train_df.iterrows()
|
34 |
+
if len(set(row["elements"]).intersection(element_list)) > 0
|
35 |
+
and set(row["elements"]).issubset(element_list)
|
36 |
+
]
|
37 |
# Fetch elemental entries (they are usually GGA calculations)
|
38 |
elemental_entries = [e for e in entries if e.composition.is_element]
|
39 |
|
40 |
# Filter entries based on functional
|
41 |
if functional == "GGA":
|
42 |
+
entries = [
|
43 |
+
e
|
44 |
+
for e in entries
|
45 |
+
if not e.parameters.get("run_type", "").startswith("GGA+U")
|
46 |
+
]
|
47 |
elif functional == "GGA+U":
|
48 |
+
entries = [
|
49 |
+
e for e in entries if e.parameters.get("run_type", "").startswith("GGA+U")
|
50 |
+
]
|
51 |
# Add elemental entries to ensure they are included
|
52 |
entries.extend([e for e in elemental_entries if e not in entries])
|
53 |
|
|
|
64 |
else:
|
65 |
# For 3D plots, limit to ternary systems
|
66 |
if len(element_list) == 3:
|
67 |
+
plotter = PDPlotter(
|
68 |
+
phase_diagram, show_unstable=True, backend="plotly", ternary_style="3d"
|
69 |
+
)
|
70 |
fig = plotter.get_plot()
|
71 |
else:
|
72 |
+
return go.Figure().add_annotation(
|
73 |
+
text="3D plots are only available for ternary systems."
|
74 |
+
)
|
75 |
|
76 |
# Adjust the maximum energy above hull
|
77 |
# (This is a placeholder as PDPlotter does not support direct filtering)
|
|
|
81 |
|
82 |
|
83 |
# Define Gradio interface components
|
84 |
+
elements_input = gr.Textbox(
|
85 |
+
label="Elements (e.g., 'Li-Fe-O')",
|
86 |
+
placeholder="Enter elements separated by '-'",
|
87 |
+
value="Li-Fe-O",
|
88 |
+
)
|
89 |
+
max_e_above_hull_slider = gr.Slider(
|
90 |
+
minimum=0, maximum=1, value=0.1, label="Maximum Energy Above Hull (eV)"
|
91 |
+
)
|
92 |
+
color_scheme_dropdown = gr.Dropdown(
|
93 |
+
choices=["Energy Above Hull", "Formation Energy"], label="Color Scheme"
|
94 |
+
)
|
95 |
plot_style_dropdown = gr.Dropdown(choices=["2D", "3D"], label="Plot Style")
|
96 |
functional_dropdown = gr.Dropdown(choices=["GGA", "GGA+U", "Both"], label="Functional")
|
97 |
finite_temp_toggle = gr.Checkbox(label="Enable Finite Temperature Estimation")
|
|
|
105 |
color_scheme_dropdown,
|
106 |
plot_style_dropdown,
|
107 |
functional_dropdown,
|
108 |
+
finite_temp_toggle,
|
109 |
],
|
110 |
outputs=gr.Plot(label="Phase Diagram"),
|
111 |
title="Materials Project Phase Diagram",
|
112 |
+
description="Generate a phase diagram for a set of elements using Materials Project data.",
|
113 |
)
|
114 |
|
115 |
# Launch the app
|