Skip to content

Commit af1b6e7

Browse files
committed
update: use made 2
1 parent 7428353 commit af1b6e7

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

other/materials_designer/import_materials_from_files.ipynb

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": null,
4140
"id": "9c1f4e9e",
4241
"metadata": {},
43-
"outputs": [],
4442
"source": [
4543
"# Upload files to this folder\n",
4644
"FOLDER_PATH = \"./uploads\"\n",
@@ -51,7 +49,9 @@
5149
"USE_FILE_NAME_NO_EXTENSION = False\n",
5250
"# If set to true, the supported formats will be printed below\n",
5351
"SHOW_SUPPORTED_FORMATS = False"
54-
]
52+
],
53+
"outputs": [],
54+
"execution_count": null
5555
},
5656
{
5757
"cell_type": "markdown",
@@ -63,20 +63,20 @@
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": null,
6766
"id": "ef2b38a5-de7e-419e-b605-110e9e0095f5",
6867
"metadata": {
6968
"trusted": true
7069
},
71-
"outputs": [],
7270
"source": [
7371
"import sys\n",
7472
"if sys.platform == \"emscripten\":\n",
7573
" import micropip\n",
7674
" await micropip.install('mat3ra-api-examples', deps=False)\n",
77-
"from utils.jupyterlite import install_packages\n",
78-
"await install_packages(\"import_materials_from_files.ipynb\",\"../../config.yml\")"
79-
]
75+
" from utils.jupyterlite import install_packages\n",
76+
" await install_packages(\"import_materials_from_files.ipynb\")"
77+
],
78+
"outputs": [],
79+
"execution_count": null
8080
},
8181
{
8282
"cell_type": "markdown",
@@ -90,13 +90,11 @@
9090
},
9191
{
9292
"cell_type": "code",
93-
"execution_count": null,
9493
"id": "b1ee775d1476f884",
9594
"metadata": {
9695
"collapsed": false,
9796
"trusted": true
9897
},
99-
"outputs": [],
10098
"source": [
10199
"import os\n",
102100
"from pathlib import Path\n",
@@ -118,7 +116,9 @@
118116
" print(e)\n",
119117
" unreadable_files.append(file_name)\n",
120118
" continue"
121-
]
119+
],
120+
"outputs": [],
121+
"execution_count": null
122122
},
123123
{
124124
"cell_type": "markdown",
@@ -130,14 +130,14 @@
130130
},
131131
{
132132
"cell_type": "code",
133-
"execution_count": null,
134133
"id": "0e34472a",
135134
"metadata": {},
136-
"outputs": [],
137135
"source": [
138136
"print(f\"Successfully read {len(materials)} files\")\n",
139137
"print(f\"Unreadable files: {unreadable_files}. \")\n"
140-
]
138+
],
139+
"outputs": [],
140+
"execution_count": null
141141
},
142142
{
143143
"cell_type": "markdown",
@@ -149,10 +149,8 @@
149149
},
150150
{
151151
"cell_type": "code",
152-
"execution_count": null,
153152
"id": "a771a36a",
154153
"metadata": {},
155-
"outputs": [],
156154
"source": [
157155
"# Uncomment to see the list of supported formats and their file extensions\n",
158156
"from ase.io.formats import ioformats\n",
@@ -163,7 +161,9 @@
163161
" data = [[frmt.name, frmt.extensions, frmt.description] for frmt in ioformats.values()]\n",
164162
" dataframe = pd.DataFrame(data, columns=[\"Format Name\", \"File Extensions\", \"Description\"])\n",
165163
" print(dataframe.to_markdown())"
166-
]
164+
],
165+
"outputs": [],
166+
"execution_count": null
167167
},
168168
{
169169
"cell_type": "markdown",
@@ -175,17 +175,34 @@
175175
},
176176
{
177177
"cell_type": "code",
178-
"execution_count": null,
179178
"id": "c41fb68c6d25fe48",
180179
"metadata": {
181180
"collapsed": false,
182181
"trusted": true
183182
},
184-
"outputs": [],
185183
"source": [
186-
"from src.utils import ase_to_poscar\n",
184+
"import io\n",
185+
"from ase import Atoms\n",
186+
"from ase.io import write\n",
187187
"from express import ExPrESS\n",
188188
"\n",
189+
"def ase_to_poscar(atoms: Atoms):\n",
190+
" \"\"\"\n",
191+
" Converts ase.Atoms object to POSCAR format\n",
192+
"\n",
193+
" Args:\n",
194+
" atoms (ase.Atoms): ase.Atoms object\n",
195+
"\n",
196+
" Returns:\n",
197+
" str: POSCAR string\n",
198+
" \"\"\"\n",
199+
" output = io.StringIO()\n",
200+
" write(output, atoms, format=\"vasp\")\n",
201+
" content = output.getvalue()\n",
202+
" output.close()\n",
203+
"\n",
204+
" return content\n",
205+
"\n",
189206
"def convert_ase_entry_to_esse(ase_entry):\n",
190207
" poscar = ase_to_poscar(ase_entry)\n",
191208
" kwargs = {\n",
@@ -201,7 +218,9 @@
201218
" return esse\n",
202219
"\n",
203220
"esse_entries = list(map(convert_ase_entry_to_esse, materials))"
204-
]
221+
],
222+
"outputs": [],
223+
"execution_count": null
205224
},
206225
{
207226
"cell_type": "markdown",
@@ -213,39 +232,18 @@
213232
},
214233
{
215234
"cell_type": "code",
216-
"execution_count": null,
217235
"id": "860b5c1b",
218236
"metadata": {},
219-
"outputs": [],
220237
"source": [
221-
"from src.utils import poscar_to_ase\n",
222-
"from ase.visualize import view\n",
223-
"from ase.io import write\n",
224-
"from ase.build import make_supercell\n",
225-
"from IPython.display import Image\n",
238+
"from utils.visualize import visualize_materials\n",
239+
"from mat3ra.made.material import Material\n",
226240
"\n",
227-
"def visualize_material(material, index: int, number_of_repetitions: int = 3):\n",
228-
" \"\"\"\n",
229-
" Visualize the material using ASE's visualization tool\n",
230-
" Repeat the unit cell to make it easier to see.\n",
241+
"materials = [Material(esse_entry) for esse_entry in esse_entries]\n",
231242
"\n",
232-
" Args:\n",
233-
" material: The material to visualize (Ase.Atoms object)\n",
234-
" index: The index of the material\n",
235-
" number_of_repetitions: The number of unit cell repetitions to visualize\n",
236-
" \"\"\"\n",
237-
" # Set the number of unit cell repetition for the structure to make it easier to see\n",
238-
" n = number_of_repetitions\n",
239-
" material_repeat = make_supercell(material, [[n,0,0],[0,n,0],[0,0,n]])\n",
240-
" filename = f\"material-{index}.png\"\n",
241-
" write(filename, material_repeat)\n",
242-
" img = Image(filename=filename)\n",
243-
" print(filename, \"-\", material.symbols)\n",
244-
" display(img)\n",
245-
"\n",
246-
"for idx, material in enumerate(materials):\n",
247-
" visualize_material(material, idx)"
248-
]
243+
"visualize_materials(materials)"
244+
],
245+
"outputs": [],
246+
"execution_count": null
249247
},
250248
{
251249
"cell_type": "markdown",
@@ -257,16 +255,15 @@
257255
},
258256
{
259257
"cell_type": "code",
260-
"execution_count": null,
261258
"id": "00b187ab",
262259
"metadata": {},
263-
"outputs": [],
264260
"source": [
265-
"from utils.jupyterlite import set_data\n",
261+
"from utils.jupyterlite import set_materials\n",
266262
"\n",
267-
"output_materials = esse_entries\n",
268-
"set_data(\"materials\", output_materials)"
269-
]
263+
"set_materials(materials)"
264+
],
265+
"outputs": [],
266+
"execution_count": null
270267
}
271268
],
272269
"metadata": {

0 commit comments

Comments
 (0)