Skip to content

Commit

Permalink
Adds Jupyter Notebook for examples (RascalSoftware#85)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
DrPaulSharp and alexhroom authored Nov 12, 2024
1 parent c512c19 commit 11ab487
Show file tree
Hide file tree
Showing 16 changed files with 3,687 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ dist/*

# Local pre-commit hooks
.pre-commit-config.yaml

# Jupyter notebook checkpoints
.ipynb_checkpoints/*
305 changes: 305 additions & 0 deletions RATapi/examples/absorption/absorption.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pathlib\n",
"\n",
"import numpy as np\n",
"from IPython.display import Code\n",
"\n",
"import RATapi as RAT\n",
"from RATapi.models import Parameter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Absorption (imaginary SLD) - effect below the critical edge\n",
"\n",
"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",
"\n",
"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",
"\n",
"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",
"\n",
"For all calculation types, to activate this functionality it is necessary to set the 'absorption' flag when creating the project."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"problem = RAT.Project(name=\"Absorption example\", calculation=\"non polarised\", model=\"custom layers\", geometry=\"substrate/liquid\", absorption=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now define our parameters, noting that each SLD parameter has both a real and imaginary component:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"parameter_list = [\n",
" Parameter(name=\"Alloy Thickness\", min=100.0, value=135.6, max=200.0, fit=True),\n",
" Parameter(name=\"Alloy SLD up\", min=6.0e-6, value=9.87e-6, max=1.2e-5, fit=True),\n",
" Parameter(name=\"Alloy SLD imaginary up\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True),\n",
" Parameter(name=\"Alloy SLD down\", min=6.0e-6, value=7.05e-6, max=1.3e-5, fit=True),\n",
" Parameter(name=\"Alloy SLD imaginary down\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True),\n",
" Parameter(name=\"Alloy Roughness\", min=2.0, value=5.71, max=10.0, fit=True),\n",
" #\n",
" Parameter(name=\"Gold Thickness\", min=100.0, value=154.7, max=200.0, fit=True),\n",
" Parameter(name=\"Gold Roughness\", min=0.1, value=5.42, max=10.0, fit=True),\n",
" Parameter(name=\"Gold SLD\", min=4.0e-6, value=4.49e-6, max=5.0e-6, fit=True),\n",
" Parameter(name=\"Gold SLD imaginary\", min=1.0e-9, value=4.20e-8, max=1.0e-7, fit=True),\n",
" #\n",
" Parameter(name=\"Thiol APM\", min=40.0, value=56.27, max=100.0, fit=True),\n",
" Parameter(name=\"Thiol Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True),\n",
" Parameter(name=\"Thiol Coverage\", min=0.5, value=0.9, max=1.0, fit=True),\n",
" #\n",
" Parameter(name=\"CW Thickness\", min=1.0, value=12.87, max=25.0, fit=True),\n",
" #\n",
" Parameter(name=\"Bilayer APM\", min=48.0, value=65.86, max=90.0, fit=True),\n",
" Parameter(name=\"Bilayer Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True),\n",
" Parameter(name=\"Bilayer Roughness\", min=1.0, value=3.87, max=10.0, fit=True),\n",
" Parameter(name=\"Bilayer Coverage\", min=0.5, value=0.94, max=1.0, fit=True)\n",
"]\n",
"\n",
"problem.parameters.extend(parameter_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the bulk in and bulk out parameters:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"problem.bulk_in.set_fields(0, name=\"Silicon\", min=2.0e-6, value=2.073e-6, max=2.1e-6)\n",
"\n",
"problem.bulk_out.set_fields(0, name=\"D2O\", min=5.8e-06, value=6.21e-06, max=6.35e-06, fit=True)\n",
"problem.bulk_out.append(name=\"H2O\", min=-5.6e-07, value=-3.15e-07, max=0.0, fit=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use a different scalefactor for each dataset:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"del problem.scalefactors[0]\n",
"problem.scalefactors.append(name=\"Scalefactor 1\", min=0.5, value=1, max=1.5, fit=True)\n",
"problem.scalefactors.append(name=\"Scalefactor 2\", min=0.5, value=1, max=1.5, fit=True)\n",
"problem.scalefactors.append(name=\"Scalefactor 3\", min=0.5, value=1, max=1.5, fit=True)\n",
"problem.scalefactors.append(name=\"Scalefactor 4\", min=0.5, value=1, max=1.5, fit=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the backgrounds and resolutions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"del problem.backgrounds[0]\n",
"del problem.background_parameters[0]\n",
"\n",
"problem.background_parameters.append(name=\"Background parameter 1\", min=5.0e-08, value=7.88e-06, max=9.0e-05, fit=True)\n",
"problem.background_parameters.append(name=\"Background parameter 2\", min=1.0e-08, value=5.46e-06, max=9.0e-05, fit=True)\n",
"problem.background_parameters.append(name=\"Background parameter 3\", min=1.0e-06, value=9.01e-06, max=9.0e-05, fit=True)\n",
"problem.background_parameters.append(name=\"Background parameter 4\", min=1.0e-06, value=5.61e-06, max=9.0e-05, fit=True)\n",
"\n",
"problem.backgrounds.append(name=\"Background 1\", type=\"constant\", value_1=\"Background parameter 1\")\n",
"problem.backgrounds.append(name=\"Background 2\", type=\"constant\", value_1=\"Background parameter 2\")\n",
"problem.backgrounds.append(name=\"Background 3\", type=\"constant\", value_1=\"Background parameter 3\")\n",
"problem.backgrounds.append(name=\"Background 4\", type=\"constant\", value_1=\"Background parameter 4\")\n",
"\n",
"# Make the resolution fittable\n",
"problem.resolution_parameters.set_fields(0, fit=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add the datasets:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_path = pathlib.Path(\"../data\")\n",
"\n",
"data_1 = np.loadtxt(data_path / \"D2O_spin_down.dat\")\n",
"problem.data.append(name=\"D2O_dn\", data=data_1)\n",
"\n",
"data_2 = np.loadtxt(data_path / \"D2O_spin_up.dat\")\n",
"problem.data.append(name=\"D2O_up\", data=data_2)\n",
"\n",
"data_3 = np.loadtxt(data_path / \"H2O_spin_down.dat\")\n",
"problem.data.append(name=\"H2O_dn\", data=data_3)\n",
"\n",
"data_4 = np.loadtxt(data_path / \"H2O_spin_up.dat\")\n",
"problem.data.append(name=\"H2O_up\", data=data_4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add the custom file. We can see that we add an extra column for the output in our custom function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"problem.custom_files.append(\n",
" name=\"DPPC absorption\",\n",
" filename=\"volume_thiol_bilayer.py\",\n",
" language=\"python\",\n",
" path=pathlib.Path.cwd().resolve(),\n",
")\n",
"Code(filename='volume_thiol_bilayer.py', language='python')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, add the contrasts:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"problem.contrasts.append(\n",
" name=\"D2O Down\",\n",
" data=\"D2O_dn\",\n",
" background=\"Background 1\",\n",
" bulk_in=\"Silicon\",\n",
" bulk_out=\"D2O\",\n",
" scalefactor=\"Scalefactor 1\",\n",
" resolution=\"Resolution 1\",\n",
" resample=True,\n",
" model=[\"DPPC absorption\"],\n",
")\n",
"\n",
"problem.contrasts.append(\n",
" name=\"D2O Up\",\n",
" data=\"D2O_up\",\n",
" background=\"Background 2\",\n",
" bulk_in=\"Silicon\",\n",
" bulk_out=\"D2O\",\n",
" scalefactor=\"Scalefactor 2\",\n",
" resolution=\"Resolution 1\",\n",
" resample=True,\n",
" model=[\"DPPC absorption\"],\n",
")\n",
"\n",
"problem.contrasts.append(\n",
" name=\"H2O Down\",\n",
" data=\"H2O_dn\",\n",
" background=\"Background 3\",\n",
" bulk_in=\"Silicon\",\n",
" bulk_out=\"H2O\",\n",
" scalefactor=\"Scalefactor 3\",\n",
" resolution=\"Resolution 1\",\n",
" resample=True,\n",
" model=[\"DPPC absorption\"],\n",
")\n",
"\n",
"problem.contrasts.append(\n",
" name=\"H2O Up\",\n",
" data=\"H2O_up\",\n",
" background=\"Background 4\",\n",
" bulk_in=\"Silicon\",\n",
" bulk_out=\"H2O\",\n",
" scalefactor=\"Scalefactor 4\",\n",
" resolution=\"Resolution 1\",\n",
" resample=True,\n",
" model=[\"DPPC absorption\"],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now run RAT and plot the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"controls = RAT.Controls(parallel=\"contrasts\", resampleMinAngle=0.9, resampleNPoints=150.0)\n",
"problem, results = RAT.run(problem, controls)\n",
"\n",
"RAT.plotting.plot_ref_sld(problem, results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
11 changes: 5 additions & 6 deletions RATapi/examples/absorption/absorption.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import pathlib

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

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

data_1 = np.loadtxt(os.path.join(data_path, "D2O_spin_down.dat"))
data_1 = np.loadtxt(data_path / "D2O_spin_down.dat")
problem.data.append(name="D2O_dn", data=data_1)

data_2 = np.loadtxt(os.path.join(data_path, "D2O_spin_up.dat"))
data_2 = np.loadtxt(data_path / "D2O_spin_up.dat")
problem.data.append(name="D2O_up", data=data_2)

data_3 = np.loadtxt(os.path.join(data_path, "H2O_spin_down.dat"))
data_3 = np.loadtxt(data_path / "H2O_spin_down.dat")
problem.data.append(name="H2O_dn", data=data_3)

data_4 = np.loadtxt(os.path.join(data_path, "H2O_spin_up.dat"))
data_4 = np.loadtxt(data_path / "H2O_spin_up.dat")
problem.data.append(name="H2O_up", data=data_4)

# Add the custom file
Expand Down
2 changes: 1 addition & 1 deletion RATapi/examples/absorption/volume_thiol_bilayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def volume_thiol_bilayer(params, bulk_in, bulk_out, contrast):

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

if contrast == 1 or contrast == 3:
if contrast == 0 or contrast == 2:
output = [alloyUp, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
else:
output = [alloyDown, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
Expand Down
Loading

0 comments on commit 11ab487

Please sign in to comment.