File size: 6,581 Bytes
e6e9684
f121890
1366f26
5d45184
 
e6e9684
 
32ed837
 
59575d2
20d40ec
 
901176a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e6e599
901176a
 
1e6e599
901176a
f121890
901176a
2dd66b7
 
 
 
 
901176a
2dd66b7
 
 
 
 
 
901176a
2dd66b7
901176a
2dd66b7
 
 
901176a
 
 
5d45184
 
f121890
901176a
 
 
 
 
 
 
 
f121890
 
 
901176a
e6e9684
32ed837
 
 
 
 
ddb4a97
 
 
 
 
 
901176a
 
 
 
 
ddb4a97
901176a
ddb4a97
e6e9684
 
901176a
 
 
 
 
 
e6e9684
 
901176a
e6e9684
dd8e803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1366f26
 
 
 
 
 
 
 
2dd66b7
a91474f
2dd66b7
a91474f
 
 
 
 
2dd66b7
 
901176a
 
 
2dd66b7
901176a
f121890
 
 
 
e6e9684
 
 
 
2dd66b7
32ed837
f121890
32ed837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dd66b7
20d40ec
e6e9684
 
 
 
 
 
1e6e599
 
f121890
901176a
f121890
 
901176a
 
e6e9684
f121890
901176a
 
 
 
f121890
 
32ed837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f121890
901176a
f121890
e6e9684
3dde8a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import crystal_toolkit.components as ctc
import dash
import dash_mp_components as dmp
import numpy as np
import periodictable
from crystal_toolkit.settings import SETTINGS
from dash import dcc, html
from dash.dependencies import Input, Output, State
from dash_breakpoints import WindowBreakpoints
from pymatgen.analysis.structure_analyzer import SpacegroupAnalyzer
from pymatgen.core import Structure

from components import (
    get_display_table,
    get_dropdown,
    get_materials_display,
    get_periodic_table,
    get_upload_div,
)
from data_utils import (
    build_embeddings_index,
    build_formula_index,
    get_crystal_plot,
    get_dataset,
    get_properties_table,
    search_materials,
)

EMPTY_DATA = False
CACHE_PATH = None

dataset = get_dataset()

display_columns_query = [
    "chemical_formula_descriptive",
    "functional",
    "immutable_id",
    "energy",
]
display_names_query = {
    "chemical_formula_descriptive": "Formula",
    "functional": "Functional",
    "immutable_id": "Material ID",
    "energy": "Energy (eV)",
}


mapping_table_idx_dataset_idx = {}
available_similar_materials = []

map_periodic_table = {v.symbol: k for k, v in enumerate(periodictable.elements)}

# dataset_index, immutable_id_to_idx = build_formula_index(dataset, cache_path=None)
dataset_index, immutable_id_to_idx = build_formula_index(
    dataset, cache_path=CACHE_PATH, empty_data=EMPTY_DATA
)

# Initialize the Dash app
external_stylesheets = [
    "/assets/styles.css",
]

app = dash.Dash(
    __name__,
    external_stylesheets=external_stylesheets,
)
server = app.server  # Expose the server for deployment

# Define the app layout
app.layout = html.Div(
    [
        WindowBreakpoints(
            id="breakpoints",
            widthBreakpointThresholdsPx=[800, 1200],
            widthBreakpointNames=["sm", "md", "lg"],
        ),
        html.H1(
            html.B("Interactive Crystal Viewer"),
            style={"textAlign": "center", "margin-top": "20px"},
        ),
        html.Div(
            [
                get_materials_display(
                    "",
                    "Structure will be displayed here",
                    "Properties will be displayed here",
                )
            ],
            className="container-row",
        ),
        html.Div(
            [
                get_periodic_table("materials-input", {}),
                get_display_table(
                    "table",
                    display_names_query,
                    display_columns_query,
                    "Select a row to display the material's structure and properties",
                ),
            ],
            className="container-row-periodic",
        ),
        html.Footer(
            [
                html.P(
                    [
                        "Built with ",
                        html.A(
                            "mp-components",
                            href="https://github.com/materialsproject/mp-react-components",
                        ),
                        " and ",
                        html.A(
                            "Crystal Toolkit", href="https://docs.crystaltoolkit.org/"
                        ),
                    ],
                    style={"textAlign": "center"},
                )
            ],
        ),
    ],
    style={
        "margin-left": "10px",
        "margin-right": "10px",
    },
)


# Callback to update the table based on search
@app.callback(
    Output("table", "data"),
    Input("materials-input", "submitButtonClicks"),
    Input("materials-input", "value"),
)
def on_submit_materials_input(n_clicks, query):
    if n_clicks is None or not query:
        return []

    entries = search_materials(
        query, dataset, dataset_index, mapping_table_idx_dataset_idx, map_periodic_table
    )

    return [{col: entry[col] for col in display_columns_query} for entry in entries]


# Callback to display the selected material
@app.callback(
    [
        Output("structure-container", "children"),
        Output("properties-container", "children"),
    ],
    Input("table", "active_cell"),
    Input("table", "derived_virtual_selected_rows"),
)
def display_material(active_cell, selected_rows):
    if not active_cell and not selected_rows:
        return (
            html.Div(
                "Search a material to display its structure and properties",
                style={"textAlign": "center"},
            ),
            html.Div(
                "Properties will be displayed here",
                style={"textAlign": "center"},
            ),
        )

    if len(selected_rows) > 0:
        idx_active = selected_rows[0]
    else:
        idx_active = active_cell["row"]
    row = dataset[mapping_table_idx_dataset_idx[idx_active]]

    structure = Structure(
        [x for y in row["lattice_vectors"] for x in y],
        row["species_at_sites"],
        row["cartesian_site_positions"],
        coords_are_cartesian=True,
    )
    if row["magnetic_moments"]:
        structure.add_site_property("magmom", row["magnetic_moments"])

    structure_layout, sga = get_crystal_plot(structure)

    # Extract key properties
    properties_html = get_properties_table(
        row, structure, sga, [None, None], container_type="results"
    )

    return (
        structure_layout,
        properties_html,
    )


@app.callback(
    Output("materials-input-container", "children"),
    Input("breakpoints", "widthBreakpoint"),
    State("breakpoints", "width"),
)
def update_materials_input_layout(breakpoint_name, width):
    if breakpoint_name in ["lg", "md"]:
        # Default layout if no page size is detected
        return dmp.MaterialsInput(
            allowedInputTypes=["elements", "formula"],
            hidePeriodicTable=False,
            periodicTableMode="toggle",
            hideWildcardButton=True,
            showSubmitButton=True,
            submitButtonText="Search",
            type="elements",
            id="materials-input",
        )
    elif breakpoint_name == "sm":
        return dmp.MaterialsInput(
            allowedInputTypes=["elements", "formula"],
            hidePeriodicTable=True,
            periodicTableMode="none",
            hideWildcardButton=False,
            showSubmitButton=False,
            # submitButtonText="Search",
            type="elements",
            id="materials-input",
        )


# Register crystal toolkit with the app
ctc.register_crystal_toolkit(app, app.layout)

if __name__ == "__main__":
    app.run_server(debug=True, port=7860, host="0.0.0.0")