Skip to content

Commit 291500e

Browse files
authored
Merge pull request #70 from BlauGroup/demos
merge demos
2 parents 644cdf5 + 7946393 commit 291500e

File tree

5 files changed

+592
-2
lines changed

5 files changed

+592
-2
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
__pycache__/
22
scratch
3-
*.ipynb*
43
*.idea
54
*.egg-info
65
.vscode/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Installation should take around 15 minutes on a normal desktop computer. NanoPar
4343

4444
The functionality to train and use deep learning models to predict UCNP emission intensity using NanoParticleTools is embedded [here](https://github.com/BlauGroup/NanoParticleTools/tree/main/src/NanoParticleTools/machine_learning).
4545

46-
Within the demos folder, we have included Jupyter notebooks with demos for (1) training a deep learning model on pre-compiled datasets of [RNMC](https://github.com/BlauGroup/RNMC) trajectories, (2) loading pre-trained models and predicting emission intensity for an arbitrary UCNP design, and (3) loading pre-trained models and predicting emission intensity on the pre-compiled UCNP datasets.
46+
Within the demos folder, we have included Jupyter notebooks with [demos](https://github.com/BlauGroup/NanoParticleTools/src/NanoParticleTools/machine_learning/demos/) for (1) training a deep learning model on pre-compiled datasets of [RNMC](https://github.com/BlauGroup/RNMC) trajectories, (2) loading pre-trained models and predicting emission intensity for an arbitrary UCNP design, and (3) loading pre-trained models and predicting emission intensity on the pre-compiled UCNP datasets.
4747

4848
Pre-compiled UCNP [datasets](https://figshare.com/s/49222bae78f228363897) (SUNSET) and pre-trained [models](https://figshare.com/articles/dataset/Hetero-GNN_Checkpoints/27941694/1?file=50919813)
4949
can be downloaded from Figshare.

src/NanoParticleTools/machine_learning/demos/demo_predict_SUNSET.ipynb

+155
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook demonstrates how to use the pre-trained models to predict emission intensity on an arbitrary UCNP. "
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 12,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"from NanoParticleTools.machine_learning.data import SummedWavelengthRangeLabelProcessor\n",
17+
"from NanoParticleTools.machine_learning.models.hetero.intra_inter_model import HeteroDCVModel\n",
18+
"from NanoParticleTools.machine_learning.models.hetero.intra_inter_data import HeteroDCVFeatureProcessor\n",
19+
"from NanoParticleTools.inputs.nanoparticle import SphericalConstraint\n",
20+
"from NanoParticleTools.machine_learning.util.wandb import model_from_file"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"First, define the feature processor. In this case, since we are using the HeteroDCVModel, we use the HeteroDCVFeatureProcessor. We also define the label processor, which sums the wavelengths over the UCNP emisison spectra. "
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 7,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"feature_processor_kwargs={'include_zeros': True}\n",
37+
"feature_processor = HeteroDCVFeatureProcessor(feature_processor_kwargs)\n",
38+
"\n",
39+
"label_processor_kwargs={\n",
40+
" 'spectrum_ranges': {\n",
41+
" 'uv': (300, 450)\n",
42+
" },\n",
43+
" 'log_constant': 100\n",
44+
" }\n",
45+
"label_processor = SummedWavelengthRangeLabelProcessor(label_processor_kwargs)"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"Next, define an arbitrary nanoparticle. In this case it will be a core-shell nanoparticle with a 4 nm core and a 10 nm shell. The core will have Erbium doped at 0.01, while the shell will have Erbium and Ytterbium doping at 0.2 and 0.4. "
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 5,
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"constraints = [SphericalConstraint(40), SphericalConstraint(100)]\n",
62+
"dopant_concentrations = [{'Er': 0.01}, {'Er': 0.2, 'Yb': 0.4}]"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"From here, we can transform this definition of a UCNP into a readable data object for the model"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 16,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"dopant_concentration, radii_without_zero = feature_processor.inputs_from_constraints_and_concentration(constraints, dopant_concentrations)\n",
79+
"input_data = feature_processor.graph_from_inputs(dopant_concentration, radii_without_zero)\n",
80+
"data = feature_processor.data_cls(input_data)"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"metadata": {},
86+
"source": [
87+
"Now, we can load a pre-trained model checkpoint, which can be downloaded from [Figshare](https://figshare.com/articles/dataset/Hetero-GNN_Checkpoints/27941694/1?file=50919813). "
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 15,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"name": "stderr",
97+
"output_type": "stream",
98+
"text": [
99+
"Lightning automatically upgraded your loaded checkpoint from v1.7.7 to v2.4.0. To apply the upgrade to your files permanently, run `python -m pytorch_lightning.utilities.upgrade_checkpoint C:\\Users\\ChemeGrad2021\\Desktop\\NanoParticleTools\\SUNSET-1\\model_1.ckpt`\n",
100+
"c:\\users\\chemegrad2021\\desktop\\nanoparticletools\\src\\NanoParticleTools\\machine_learning\\models\\hetero\\intra_inter_model.py:260: UserWarning: Cannot override n_input_nodes for this model. It is inferred fromthe embed_dim.\n",
101+
" warnings.warn(\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"model_path = r\"C:\\Users\\ChemeGrad2021\\Desktop\\NanoParticleTools\\SUNSET-1\\model_1.ckpt\"\n",
107+
"pre_trained_model = model_from_file(model_path, HeteroDCVModel)"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"metadata": {},
113+
"source": [
114+
"Finally, we can pass the data into the model. To get emission intensity, we must undo the log and add back a constant, which were operations used in the label processor."
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 31,
120+
"metadata": {},
121+
"outputs": [
122+
{
123+
"name": "stdout",
124+
"output_type": "stream",
125+
"text": [
126+
"The summed UV emission intensity of this arbitrary UCNP is: 122.25 cps\n"
127+
]
128+
}
129+
],
130+
"source": [
131+
"output = pre_trained_model.predict_step(data).item()\n",
132+
"intensity = 10**(output) + label_processor.log_constant\n",
133+
"print(f\"The summed UV emission intensity of this arbitrary UCNP is: {intensity:.2f} cps\")"
134+
]
135+
}
136+
],
137+
"metadata": {
138+
"kernelspec": {
139+
"display_name": "nanoparticle-env-311",
140+
"language": "python",
141+
"name": "python3"
142+
},
143+
"language_info": {
144+
"codemirror_mode": {
145+
"name": "ipython",
146+
"version": 3
147+
},
148+
"file_extension": ".py",
149+
"mimetype": "text/x-python",
150+
"name": "python",
151+
"nbconvert_exporter": "python",
152+
"pygments_lexer": "ipython3",
153+
"version": "3.11.10"
154+
}
155+
},
156+
"nbformat": 4,
157+
"nbformat_minor": 2
158+
}

0 commit comments

Comments
 (0)