-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
192 additions
and
0 deletions.
There are no files selected for viewing
192 changes: 192 additions & 0 deletions
192
mpcontribs-portal/notebooks/contribs.materialsproject.org/pycroscopy.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "protective-banana", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from mpcontribs.client import Client, Attachments\n", | ||
"import atomai as aoi\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import torch\n", | ||
"from atomai.utils import graphx\n", | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "rural-frontier", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data_dir = \"/Users/patrick/GoogleDriveLBNL/My Drive/MaterialsProject/gitrepos/mpcontribs-data/pycroscopy\"\n", | ||
"imgdata_path = f\"{data_dir}/Gr_SiCr.npy\"\n", | ||
"imgdata = np.load(imgdata_path)\n", | ||
"model_path = f\"{data_dir}/G_MD.tar\"\n", | ||
"model = aoi.load_model(model_path)\n", | ||
"# model as dict\n", | ||
"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n", | ||
"model_dict = torch.load(model_path, map_location=device)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "horizontal-armenia", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"figsize = (8, 8)\n", | ||
"# plt.figure(figsize=figsize)\n", | ||
"# plt.imshow(imgdata, cmap=\"gray\")\n", | ||
"# img_path = imgdata_path.replace(\".npy\", \".png\")\n", | ||
"# plt.savefig(img_path, bbox_inches='tight')\n", | ||
"# # TODO add img_path as attachment" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "downtown-budget", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"nn_out, coords = model.predict(imgdata)\n", | ||
"# model.predict(imgdata, resize=(new_height, new_width))\n", | ||
"\n", | ||
"map_dict = {0: \"C\", 1: \"Si\"} # classes to chemical elements\n", | ||
"px2ang = 0.104 # pixel-to-angstrom conversion\n", | ||
"coord = coords[0] # take the first (and the only one) frame\n", | ||
"clusters = graphx.find_cycle_clusters(coord, cycles=[5,7], map_dict=map_dict, px2ang=px2ang)\n", | ||
"fig, ax = plt.subplots(1, 1, figsize=figsize)\n", | ||
"ax.imshow(imgdata, cmap='gray', origin='lower')\n", | ||
"\n", | ||
"for i, cl in enumerate(clusters):\n", | ||
" ax.scatter(cl[:, 1], cl[:, 0], s=2, color='red')\n", | ||
" xt = int(np.mean(cl[:, 1]))\n", | ||
" yt = int(np.mean(cl[:, 0]))\n", | ||
" ax.annotate(str(i+1), (xt, yt), size=10, color='white')\n", | ||
" \n", | ||
"img_path_clusters = imgdata_path.replace(\".npy\", \"_clusters.png\")\n", | ||
"plt.savefig(img_path_clusters, bbox_inches='tight')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "premium-intro", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"clusters_mod = []\n", | ||
"#adding a column for C atom as class 0\n", | ||
"pad_ = 1\n", | ||
"for i in range(len(clusters)):\n", | ||
" clusters[i] = np.pad(clusters[i], (0, pad_), 'constant')\n", | ||
" clusters[i] = clusters[i][:-1]\n", | ||
" clusters_mod.append(clusters[i])\n", | ||
" \n", | ||
"#we can also save all the defects per image frame\n", | ||
"defect_num = 15\n", | ||
"coords_def_15 = {0: clusters_mod[defect_num]}\n", | ||
"plt.scatter(coords_def_15[0][:,1], coords_def_15[0][:,0])\n", | ||
"\n", | ||
"img_path_defects = imgdata_path.replace(\".npy\", \"_defects.png\")\n", | ||
"plt.savefig(img_path_defects, bbox_inches='tight')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "important-glucose", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# client = Client()\n", | ||
"# client.create_project(\n", | ||
"# name=\"pycroscopy\",\n", | ||
"# title=\"PyCroscopy\",\n", | ||
"# authors=\"A. Ghosh, S. Kalinin\",\n", | ||
"# description=\"Scientific Analysis of nanoscience Data\",\n", | ||
"# url=\"https://pycroscopy.github.io/pycroscopy/about.html\"\n", | ||
"# )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "common-class", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"client = Client(project=\"pycroscopy\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "pursuant-facility", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"imgdata_list = list(imgdata.tolist())\n", | ||
"model_dict[\"weights\"] = {\n", | ||
" k: v.tolist()\n", | ||
" for k, v in model_dict[\"weights\"].items()\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "corporate-reputation", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"contributions = [{\n", | ||
" \"identifier\": \"mp-7576\", # CrSi on MP\n", | ||
" \"data\": {\"clusters\": len(clusters)},\n", | ||
" \"attachments\": Attachments.from_list([\n", | ||
" img_path_clusters, img_path_defects, #imgdata_list, model_dict,\n", | ||
" ])\n", | ||
"}]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "referenced-glenn", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"client.delete_contributions()\n", | ||
"client.submit_contributions(contributions)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |