Skip to content

Commit 11ab487

Browse files
Adds Jupyter Notebook for examples (#85)
* Adds jupyter notebooks for non polarised examples * Adds jupyter notebooks for absorption and domains examples * Uses extend method in jupyter notebooks * Updates notebooks * More changes to notebooks * remove ipynb checkpoints * Addresses review comments --------- Co-authored-by: alexhroom <[email protected]>
1 parent c512c19 commit 11ab487

16 files changed

+3687
-57
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ dist/*
3131

3232
# Local pre-commit hooks
3333
.pre-commit-config.yaml
34+
35+
# Jupyter notebook checkpoints
36+
.ipynb_checkpoints/*
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import pathlib\n",
10+
"\n",
11+
"import numpy as np\n",
12+
"from IPython.display import Code\n",
13+
"\n",
14+
"import RATapi as RAT\n",
15+
"from RATapi.models import Parameter"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"# Absorption (imaginary SLD) - effect below the critical edge\n",
23+
"\n",
24+
"RAT allows the use of an imaginary, as well as real part of the SLD. The effect of this is usually seen below the critical edge, and must sometimes be accounted for.\n",
25+
"\n",
26+
"The example used here is Custom Layers. It analyses a bilayer sample on a permalloy / gold substrate, measured using polarised neutrons, against D2O and H2O, leading to 4 contrasts in total. Absorption (i.e. imaginary SLD) is defined for Gold and the Permalloy, to account for non-flat data below the critical edge.\n",
27+
"\n",
28+
"For absorption with standard layers, an additional column appears in the layers block to accommodate the imagainary component of the SLD. For custom functions, we add an extra column to the output.\n",
29+
"\n",
30+
"For all calculation types, to activate this functionality it is necessary to set the 'absorption' flag when creating the project."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"problem = RAT.Project(name=\"Absorption example\", calculation=\"non polarised\", model=\"custom layers\", geometry=\"substrate/liquid\", absorption=True)"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"We now define our parameters, noting that each SLD parameter has both a real and imaginary component:"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"parameter_list = [\n",
56+
" Parameter(name=\"Alloy Thickness\", min=100.0, value=135.6, max=200.0, fit=True),\n",
57+
" Parameter(name=\"Alloy SLD up\", min=6.0e-6, value=9.87e-6, max=1.2e-5, fit=True),\n",
58+
" Parameter(name=\"Alloy SLD imaginary up\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True),\n",
59+
" Parameter(name=\"Alloy SLD down\", min=6.0e-6, value=7.05e-6, max=1.3e-5, fit=True),\n",
60+
" Parameter(name=\"Alloy SLD imaginary down\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True),\n",
61+
" Parameter(name=\"Alloy Roughness\", min=2.0, value=5.71, max=10.0, fit=True),\n",
62+
" #\n",
63+
" Parameter(name=\"Gold Thickness\", min=100.0, value=154.7, max=200.0, fit=True),\n",
64+
" Parameter(name=\"Gold Roughness\", min=0.1, value=5.42, max=10.0, fit=True),\n",
65+
" Parameter(name=\"Gold SLD\", min=4.0e-6, value=4.49e-6, max=5.0e-6, fit=True),\n",
66+
" Parameter(name=\"Gold SLD imaginary\", min=1.0e-9, value=4.20e-8, max=1.0e-7, fit=True),\n",
67+
" #\n",
68+
" Parameter(name=\"Thiol APM\", min=40.0, value=56.27, max=100.0, fit=True),\n",
69+
" Parameter(name=\"Thiol Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True),\n",
70+
" Parameter(name=\"Thiol Coverage\", min=0.5, value=0.9, max=1.0, fit=True),\n",
71+
" #\n",
72+
" Parameter(name=\"CW Thickness\", min=1.0, value=12.87, max=25.0, fit=True),\n",
73+
" #\n",
74+
" Parameter(name=\"Bilayer APM\", min=48.0, value=65.86, max=90.0, fit=True),\n",
75+
" Parameter(name=\"Bilayer Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True),\n",
76+
" Parameter(name=\"Bilayer Roughness\", min=1.0, value=3.87, max=10.0, fit=True),\n",
77+
" Parameter(name=\"Bilayer Coverage\", min=0.5, value=0.94, max=1.0, fit=True)\n",
78+
"]\n",
79+
"\n",
80+
"problem.parameters.extend(parameter_list)"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"metadata": {},
86+
"source": [
87+
"Set the bulk in and bulk out parameters:"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"problem.bulk_in.set_fields(0, name=\"Silicon\", min=2.0e-6, value=2.073e-6, max=2.1e-6)\n",
97+
"\n",
98+
"problem.bulk_out.set_fields(0, name=\"D2O\", min=5.8e-06, value=6.21e-06, max=6.35e-06, fit=True)\n",
99+
"problem.bulk_out.append(name=\"H2O\", min=-5.6e-07, value=-3.15e-07, max=0.0, fit=True)"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"Use a different scalefactor for each dataset:"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"del problem.scalefactors[0]\n",
116+
"problem.scalefactors.append(name=\"Scalefactor 1\", min=0.5, value=1, max=1.5, fit=True)\n",
117+
"problem.scalefactors.append(name=\"Scalefactor 2\", min=0.5, value=1, max=1.5, fit=True)\n",
118+
"problem.scalefactors.append(name=\"Scalefactor 3\", min=0.5, value=1, max=1.5, fit=True)\n",
119+
"problem.scalefactors.append(name=\"Scalefactor 4\", min=0.5, value=1, max=1.5, fit=True)"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"Set the backgrounds and resolutions:"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"del problem.backgrounds[0]\n",
136+
"del problem.background_parameters[0]\n",
137+
"\n",
138+
"problem.background_parameters.append(name=\"Background parameter 1\", min=5.0e-08, value=7.88e-06, max=9.0e-05, fit=True)\n",
139+
"problem.background_parameters.append(name=\"Background parameter 2\", min=1.0e-08, value=5.46e-06, max=9.0e-05, fit=True)\n",
140+
"problem.background_parameters.append(name=\"Background parameter 3\", min=1.0e-06, value=9.01e-06, max=9.0e-05, fit=True)\n",
141+
"problem.background_parameters.append(name=\"Background parameter 4\", min=1.0e-06, value=5.61e-06, max=9.0e-05, fit=True)\n",
142+
"\n",
143+
"problem.backgrounds.append(name=\"Background 1\", type=\"constant\", value_1=\"Background parameter 1\")\n",
144+
"problem.backgrounds.append(name=\"Background 2\", type=\"constant\", value_1=\"Background parameter 2\")\n",
145+
"problem.backgrounds.append(name=\"Background 3\", type=\"constant\", value_1=\"Background parameter 3\")\n",
146+
"problem.backgrounds.append(name=\"Background 4\", type=\"constant\", value_1=\"Background parameter 4\")\n",
147+
"\n",
148+
"# Make the resolution fittable\n",
149+
"problem.resolution_parameters.set_fields(0, fit=True)\n"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"Add the datasets:"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"metadata": {},
163+
"outputs": [],
164+
"source": [
165+
"data_path = pathlib.Path(\"../data\")\n",
166+
"\n",
167+
"data_1 = np.loadtxt(data_path / \"D2O_spin_down.dat\")\n",
168+
"problem.data.append(name=\"D2O_dn\", data=data_1)\n",
169+
"\n",
170+
"data_2 = np.loadtxt(data_path / \"D2O_spin_up.dat\")\n",
171+
"problem.data.append(name=\"D2O_up\", data=data_2)\n",
172+
"\n",
173+
"data_3 = np.loadtxt(data_path / \"H2O_spin_down.dat\")\n",
174+
"problem.data.append(name=\"H2O_dn\", data=data_3)\n",
175+
"\n",
176+
"data_4 = np.loadtxt(data_path / \"H2O_spin_up.dat\")\n",
177+
"problem.data.append(name=\"H2O_up\", data=data_4)"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {},
183+
"source": [
184+
"Add the custom file. We can see that we add an extra column for the output in our custom function."
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {},
191+
"outputs": [],
192+
"source": [
193+
"problem.custom_files.append(\n",
194+
" name=\"DPPC absorption\",\n",
195+
" filename=\"volume_thiol_bilayer.py\",\n",
196+
" language=\"python\",\n",
197+
" path=pathlib.Path.cwd().resolve(),\n",
198+
")\n",
199+
"Code(filename='volume_thiol_bilayer.py', language='python')"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"Finally, add the contrasts:"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"problem.contrasts.append(\n",
216+
" name=\"D2O Down\",\n",
217+
" data=\"D2O_dn\",\n",
218+
" background=\"Background 1\",\n",
219+
" bulk_in=\"Silicon\",\n",
220+
" bulk_out=\"D2O\",\n",
221+
" scalefactor=\"Scalefactor 1\",\n",
222+
" resolution=\"Resolution 1\",\n",
223+
" resample=True,\n",
224+
" model=[\"DPPC absorption\"],\n",
225+
")\n",
226+
"\n",
227+
"problem.contrasts.append(\n",
228+
" name=\"D2O Up\",\n",
229+
" data=\"D2O_up\",\n",
230+
" background=\"Background 2\",\n",
231+
" bulk_in=\"Silicon\",\n",
232+
" bulk_out=\"D2O\",\n",
233+
" scalefactor=\"Scalefactor 2\",\n",
234+
" resolution=\"Resolution 1\",\n",
235+
" resample=True,\n",
236+
" model=[\"DPPC absorption\"],\n",
237+
")\n",
238+
"\n",
239+
"problem.contrasts.append(\n",
240+
" name=\"H2O Down\",\n",
241+
" data=\"H2O_dn\",\n",
242+
" background=\"Background 3\",\n",
243+
" bulk_in=\"Silicon\",\n",
244+
" bulk_out=\"H2O\",\n",
245+
" scalefactor=\"Scalefactor 3\",\n",
246+
" resolution=\"Resolution 1\",\n",
247+
" resample=True,\n",
248+
" model=[\"DPPC absorption\"],\n",
249+
")\n",
250+
"\n",
251+
"problem.contrasts.append(\n",
252+
" name=\"H2O Up\",\n",
253+
" data=\"H2O_up\",\n",
254+
" background=\"Background 4\",\n",
255+
" bulk_in=\"Silicon\",\n",
256+
" bulk_out=\"H2O\",\n",
257+
" scalefactor=\"Scalefactor 4\",\n",
258+
" resolution=\"Resolution 1\",\n",
259+
" resample=True,\n",
260+
" model=[\"DPPC absorption\"],\n",
261+
")"
262+
]
263+
},
264+
{
265+
"cell_type": "markdown",
266+
"metadata": {},
267+
"source": [
268+
"Now run RAT and plot the results."
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"metadata": {},
275+
"outputs": [],
276+
"source": [
277+
"controls = RAT.Controls(parallel=\"contrasts\", resampleMinAngle=0.9, resampleNPoints=150.0)\n",
278+
"problem, results = RAT.run(problem, controls)\n",
279+
"\n",
280+
"RAT.plotting.plot_ref_sld(problem, results)"
281+
]
282+
}
283+
],
284+
"metadata": {
285+
"kernelspec": {
286+
"display_name": "Python 3 (ipykernel)",
287+
"language": "python",
288+
"name": "python3"
289+
},
290+
"language_info": {
291+
"codemirror_mode": {
292+
"name": "ipython",
293+
"version": 3
294+
},
295+
"file_extension": ".py",
296+
"mimetype": "text/x-python",
297+
"name": "python",
298+
"nbconvert_exporter": "python",
299+
"pygments_lexer": "ipython3",
300+
"version": "3.10.12"
301+
}
302+
},
303+
"nbformat": 4,
304+
"nbformat_minor": 4
305+
}

RATapi/examples/absorption/absorption.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import pathlib
32

43
import numpy as np
@@ -78,18 +77,18 @@ def absorption():
7877
problem.resolution_parameters.set_fields(0, fit=True)
7978

8079
# Now add the data we need
81-
data_path = os.path.join(pathlib.Path(__file__).parents[1].resolve(), "data")
80+
data_path = pathlib.Path(__file__).parents[1] / "data"
8281

83-
data_1 = np.loadtxt(os.path.join(data_path, "D2O_spin_down.dat"))
82+
data_1 = np.loadtxt(data_path / "D2O_spin_down.dat")
8483
problem.data.append(name="D2O_dn", data=data_1)
8584

86-
data_2 = np.loadtxt(os.path.join(data_path, "D2O_spin_up.dat"))
85+
data_2 = np.loadtxt(data_path / "D2O_spin_up.dat")
8786
problem.data.append(name="D2O_up", data=data_2)
8887

89-
data_3 = np.loadtxt(os.path.join(data_path, "H2O_spin_down.dat"))
88+
data_3 = np.loadtxt(data_path / "H2O_spin_down.dat")
9089
problem.data.append(name="H2O_dn", data=data_3)
9190

92-
data_4 = np.loadtxt(os.path.join(data_path, "H2O_spin_up.dat"))
91+
data_4 = np.loadtxt(data_path / "H2O_spin_up.dat")
9392
problem.data.append(name="H2O_up", data=data_4)
9493

9594
# Add the custom file

RATapi/examples/absorption/volume_thiol_bilayer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def volume_thiol_bilayer(params, bulk_in, bulk_out, contrast):
130130

131131
CW = [cwThick, bulk_out[contrast], 0, bilayerRough]
132132

133-
if contrast == 1 or contrast == 3:
133+
if contrast == 0 or contrast == 2:
134134
output = [alloyUp, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
135135
else:
136136
output = [alloyDown, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]

0 commit comments

Comments
 (0)