Skip to content

Commit d2ad4ed

Browse files
authored
Merge pull request #211 from Exabyte-io/update/SOF-7324
Update/SOF-7324 Fix API-notebooks
2 parents cff11ca + 051554a commit d2ad4ed

11 files changed

+176
-216
lines changed

config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ notebooks:
7070
- name: specific_examples
7171
packages_common:
7272
- mat3ra-standata
73+
- name: api_examples
74+
packages_common:
75+
- mat3ra-standata
76+
- exabyte-api-client

examples/job/create_and_submit_job.ipynb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@
9090
" import micropip\n",
9191
"\n",
9292
" await micropip.install(\"mat3ra-api-examples\", deps=False)\n",
93-
" await micropip.install(\"exabyte-api-client\")\n",
9493
" from utils.jupyterlite import install_packages\n",
9594
"\n",
96-
" await install_packages(\"\")"
95+
" await install_packages(\"api_examples\")"
9796
]
9897
},
9998
{
@@ -181,8 +180,7 @@
181180
"default_workflow = workflow_endpoints.list({\"isDefault\": True, \"owner._id\": OWNER_ID})[0]\n",
182181
"\n",
183182
"material_id = default_material[\"_id\"]\n",
184-
"workflow_id = default_workflow[\"_id\"]\n",
185-
"owner_id = default_material[\"owner\"][\"_id\"]"
183+
"workflow_id = default_workflow[\"_id\"]"
186184
]
187185
},
188186
{
@@ -206,7 +204,7 @@
206204
"outputs": [],
207205
"source": [
208206
"config = {\n",
209-
" \"owner\": {\"_id\": owner_id},\n",
207+
" \"owner\": {\"_id\": OWNER_ID},\n",
210208
" \"_material\": {\"_id\": material_id},\n",
211209
" \"workflow\": {\"_id\": workflow_id},\n",
212210
" \"name\": JOB_NAME,\n",

examples/job/get-file-from-job.ipynb

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
" import micropip\n",
110110
"\n",
111111
" await micropip.install(\"mat3ra-api-examples\", deps=False)\n",
112-
" await micropip.install(\"exabyte-api-client\")\n",
113112
" from utils.jupyterlite import install_packages\n",
114113
"\n",
115114
" await install_packages(\"\")"
@@ -320,14 +319,9 @@
320319
"outputId": "e4e2e6b1-8431-439a-8606-ae500534e601"
321320
},
322321
"source": [
323-
"import urllib\n",
322+
"from mat3ra.utils.jupyterlite.url import read_from_url\n",
324323
"\n",
325-
"server_response = urllib.request.urlopen(output_file_metadata[\"signedUrl\"])\n",
326-
"output_file_bytes = server_response.read()\n",
327-
"\n",
328-
"# The server returns us a bytes-string. That's useful for things like binaries or other non-human-readable data, but this should be decoded if we're planning to write to console.\n",
329-
"# Because this is a human-readable text file, we'll decode it to UTF-8.\n",
330-
"output_file = output_file_bytes.decode(encoding=\"UTF-8\")\n",
324+
"output_file = await read_from_url(output_file_metadata[\"signedUrl\"])\n",
331325
"\n",
332326
"# Tail the last 90 lines\n",
333327
"lines = output_file.split(\"\\n\")\n",
@@ -344,50 +338,20 @@
344338
"id": "-z7VqM3K8GIx"
345339
},
346340
"source": [
347-
"### Save the input file and output file to disk.\n",
341+
"### Save the output file to disk.\n",
348342
"\n",
349343
"Now that we've verified the job is done, let's go ahead and save it and its input to disk."
350344
]
351345
},
352-
{
353-
"cell_type": "code",
354-
"metadata": {
355-
"id": "osy2JKpB8GIx"
356-
},
357-
"source": [
358-
"# We've already got an output file, so let's grab the input file we sent to Quantum Espresso\n",
359-
"for file in files:\n",
360-
" if \"pw_scf.in\" == file[\"name\"]:\n",
361-
" input_file_metadata = file\n",
362-
"server_response = urllib.request.urlopen(input_file_metadata[\"signedUrl\"])\n",
363-
"input_file_bytes = server_response.read()"
364-
],
365-
"outputs": [],
366-
"execution_count": null
367-
},
368346
{
369347
"cell_type": "code",
370348
"metadata": {
371349
"id": "COPinBrO8GIx"
372350
},
373351
"source": [
374-
"# Let's write the input file to disk. Note that we get files as a bytes string from the server, which is convenient for binaries, images, and other non-human-readable data.\n",
375-
"# Although we could decode before writing to disk, we can just write it directly with the \"wb\" (write bytes) file mode.\n",
376-
"with open(input_file_metadata[\"name\"], \"wb\") as file_descriptor:\n",
377-
" file_descriptor.write(input_file_bytes)"
378-
],
379-
"outputs": [],
380-
"execution_count": null
381-
},
382-
{
383-
"cell_type": "code",
384-
"metadata": {
385-
"id": "rzDcWyrL8GIx"
386-
},
387-
"source": [
388-
"# Now, let's write our output file to the disk. Note that because we already decoded it, we can just use the 'w' file mode.\n",
389-
"with open(output_file_metadata[\"name\"], \"w\") as file_descriptor:\n",
390-
" file_descriptor.write(output_file)"
352+
"from utils.jupyterlite import download_content_to_file\n",
353+
"\n",
354+
"download_content_to_file(output_file_metadata[\"name\"], output_file)"
391355
],
392356
"outputs": [],
393357
"execution_count": null

examples/job/ml-train-model-predict-properties.ipynb

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@
110110
" import micropip\n",
111111
"\n",
112112
" await micropip.install(\"mat3ra-api-examples\", deps=False)\n",
113-
" await micropip.install(\"exabyte-api-client\")\n",
114113
" from utils.jupyterlite import install_packages\n",
115114
"\n",
116-
" await install_packages(\"\")"
115+
" await install_packages(\"api_examples\")"
117116
],
118117
"outputs": [],
119118
"execution_count": null
@@ -168,11 +167,11 @@
168167
"source": [
169168
"#### Materials\n",
170169
"\n",
171-
"Set queries to import materials from Materials Bank:\n",
170+
"Set parameters for the materials to be imported:\n",
172171
"\n",
173-
"- **TRAIN_MATERIAL_SI_QUERY**: Query to import Si materials.\n",
174-
"- **TRAIN_MATERIAL_SIGE_QUERY**: Query to import SiGe materials.\n",
175-
"- **TARGET_MATERIAL_GE_QUERY**: Query to import Ge materials."
172+
"- **TRAIN_MATERIAL_SI**: 1st material to be used for training the model (\"Si\").\n",
173+
"- **TRAIN_MATERIAL_GE**: 2nd material to be used for training the model (\"Ge\").\n",
174+
"- **TARGET_MATERIAL_SIGE**: Material to be used for predicting the properties (\"SiGe\")."
176175
]
177176
},
178177
{
@@ -181,9 +180,9 @@
181180
"id": "qeZIEenS2Qxb"
182181
},
183182
"source": [
184-
"TRAIN_MATERIAL_SI_QUERY = {\"formula\": \"Si\"}\n",
185-
"TRAIN_MATERIAL_SIGE_QUERY = {\"formula\": \"SiGe\"}\n",
186-
"TARGET_MATERIAL_GE_QUERY = {\"formula\": \"Ge\"}"
183+
"TRAIN_MATERIAL_SI = \"Si\"\n",
184+
"TRAIN_MATERIAL_GE = \"Ge\"\n",
185+
"TARGET_MATERIAL_SIGE = \"SiGe\""
187186
],
188187
"outputs": [],
189188
"execution_count": null
@@ -314,8 +313,10 @@
314313
"id": "_JUT4f9E2Qxe"
315314
},
316315
"source": [
317-
"band_gap_workflow_id = copy_bank_workflow_by_system_name(bank_workflow_endpoints, \"espresso-band-gap\", owner_id)\n",
318-
"ml_train_workflow_id = copy_bank_workflow_by_system_name(bank_workflow_endpoints, \"exabyteml-ml-train-model\", owner_id)"
316+
"band_gap_workflow_id = copy_bank_workflow_by_system_name(bank_workflow_endpoints, \"espresso-band-gap\", OWNER_ID)\n",
317+
"ml_train_workflow_id = copy_bank_workflow_by_system_name(\n",
318+
" bank_workflow_endpoints, \"exabyteml-ml-linear-least-squares-train-model\", OWNER_ID\n",
319+
")"
319320
],
320321
"outputs": [],
321322
"execution_count": null
@@ -329,7 +330,7 @@
329330
"source": [
330331
"### Import materials\n",
331332
"\n",
332-
"Import materials from Materials Bank."
333+
"Import materials from Standata and create them in the account's materials collection."
333334
]
334335
},
335336
{
@@ -338,16 +339,22 @@
338339
"id": "t1dMm9ke2Qxe"
339340
},
340341
"source": [
341-
"bank_train_materials_id_si = bank_material_endpoints.list(TRAIN_MATERIAL_SI_QUERY)[0][\"_id\"]\n",
342-
"bank_train_materials_id_sige = bank_material_endpoints.list(TRAIN_MATERIAL_SIGE_QUERY)[0][\"_id\"]\n",
343-
"bank_target_materials_id_ge = bank_material_endpoints.list(TARGET_MATERIAL_GE_QUERY)[0][\"_id\"]\n",
342+
"from mat3ra.standata.materials import Materials\n",
343+
"\n",
344+
"train_material_json_si = Materials.get_by_name_first_match(TRAIN_MATERIAL_SI)\n",
345+
"train_material_json_ge = Materials.get_by_name_first_match(TRAIN_MATERIAL_GE)\n",
346+
"target_material_json_sige = Materials.get_by_name_first_match(TARGET_MATERIAL_SIGE)\n",
347+
"\n",
348+
"train_material_id_si = material_endpoints.create(train_material_json_si, OWNER_ID)[\"_id\"]\n",
349+
"train_material_id_ge = material_endpoints.create(train_material_json_ge, OWNER_ID)[\"_id\"]\n",
350+
"target_material_id_sige = material_endpoints.create(target_material_json_sige, OWNER_ID)[\"_id\"]\n",
344351
"\n",
345-
"train_materials_id_si = bank_material_endpoints.copy(bank_train_materials_id_si, owner_id)[\"_id\"]\n",
346-
"train_materials_id_sige = bank_material_endpoints.copy(bank_train_materials_id_sige, owner_id)[\"_id\"]\n",
347-
"target_materials_id_ge = bank_material_endpoints.copy(bank_target_materials_id_ge, owner_id)[\"_id\"]\n",
352+
"train_material_si = material_endpoints.list({\"_id\": train_material_id_si})[0]\n",
353+
"train_material_ge = material_endpoints.list({\"_id\": train_material_id_ge})[0]\n",
354+
"target_material_sige = material_endpoints.list({\"_id\": target_material_id_sige})[0]\n",
348355
"\n",
349-
"train_materials = material_endpoints.list({\"_id\": {\"$in\": [train_materials_id_si, train_materials_id_sige]}})\n",
350-
"target_materials = material_endpoints.list({\"_id\": target_materials_id_ge})"
356+
"train_materials = [train_material_ge, train_material_si]\n",
357+
"target_materials = [target_material_sige]"
351358
],
352359
"outputs": [],
353360
"execution_count": null
@@ -372,7 +379,7 @@
372379
"source": [
373380
"compute = job_endpoints.get_compute(CLUSTER, PPN, NODES, QUEUE, TIME_LIMIT)\n",
374381
"jobs = job_endpoints.create_by_ids(\n",
375-
" train_materials, band_gap_workflow_id, project_id, JOB_NAME_PREFIX, owner_id, compute\n",
382+
" train_materials, band_gap_workflow_id, project_id, JOB_NAME_PREFIX, OWNER_ID, compute\n",
376383
")"
377384
],
378385
"outputs": [],

0 commit comments

Comments
 (0)