|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Convert between RasCAL1 and RAT" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "RasCAL1 (R1) project structs can be converted to RAT `Project` classes, and vice versa.\n", |
| 15 | + "This is done via the functions `r1_to_project_class` and `project_class_to_r1`." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "### RasCAL1 to RAT\n", |
| 23 | + "Converting from R1 to a `Project` is very simple. We use the example R1 project in the file `R1monolayerVolumeModel.mat`, which is a project for analysing a monolayer of DSPC with various deuterations (tail-deuterated, head-deuterated, fully deuterated, hydrogenated)" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "Simply give the file path to the function `r1_to_project_class`, and it returns a RAT `Project` that you can use exactly like any other." |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "from RATapi.utils.convert import r1_to_project_class\n", |
| 40 | + "\n", |
| 41 | + "project = r1_to_project_class(\"R1monolayerVolumeModel.mat\")\n", |
| 42 | + "print(project)" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "Note that there are various features of RAT which do not feature in R1, such as `prior_type`, `mu` and `sigma` for parameters. These are given sensible default values (again e.g. for parameters, `prior_type = uniform`, `mu = 0.0`, `sigma=inf`), but you may change these if you would like to use these new features:" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": null, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "project.parameters[\"Head Thickness\"].prior_type = 'gaussian'\n", |
| 59 | + "project.parameters[\"Theta\"].mu = 2.0\n", |
| 60 | + "project.parameters[\"Area per molecule\"].sigma = 50.0\n", |
| 61 | + "# etc...\n", |
| 62 | + "print(project.parameters)" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "Also note that any custom files must be available to RAT. By default, RAT will assume these files are in the same directory that you are running RAT from, but if they are elsewhere you may change the relevant file location: " |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "# e.g. if our model is in the directory `my_models/`\n", |
| 79 | + "project.custom_files[0].filename = \"my_models/Model_IIb.m\"" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "As well as MATLAB functions, RAT can also run custom files provided in Python or C++ format. This may be beneficial if you do not have access to MATLAB, do not have access to the custom files from your old RasCAL project, or find it more convenient to use Python. This is done similarly to changing the file path: if we have a function defined in the Python custom file `Model_IIb.py`:" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "project.custom_files[0].filename = \"Model_IIb.py\"\n", |
| 96 | + "project.custom_files[0].language = 'python'" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "### RAT to RasCAL1\n", |
| 104 | + "\n", |
| 105 | + "To demonstrate the other way around, we will use the DSPC lipid bilayer model project from another tutorial." |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": null, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "from RATapi.examples import DSPC_standard_layers\n", |
| 115 | + "lipid_bilayer_project = DSPC_standard_layers()[0]\n", |
| 116 | + "print(lipid_bilayer_project)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "`project_class_to_r1` takes parameters `project` and `filename`, which are the `Project` object and filename for the produced .mat file respectively. This .mat file can then be loaded into RasCAL-1.\n", |
| 124 | + "\n", |
| 125 | + "Alternatively, if one sets `return_struct=True`, the struct is returned as a Python dictionary instead of being saved.\n", |
| 126 | + "\n", |
| 127 | + "Note that a MATLAB engine is used to save the project to a .mat file, so the Python library `matlabengine` must be installed." |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [], |
| 135 | + "source": [ |
| 136 | + "from RATapi.utils.convert import project_class_to_r1\n", |
| 137 | + "from pprint import pp # for printing the struct\n", |
| 138 | + "\n", |
| 139 | + "# save to a file called lipid_bilayer.mat\n", |
| 140 | + "project_class_to_r1(lipid_bilayer_project, filename=\"lipid_bilayer.mat\")\n", |
| 141 | + "\n", |
| 142 | + "# return as a Python dictionary\n", |
| 143 | + "struct = project_class_to_r1(lipid_bilayer_project, return_struct=True)\n", |
| 144 | + "pp(struct)" |
| 145 | + ] |
| 146 | + } |
| 147 | + ], |
| 148 | + "metadata": { |
| 149 | + "kernelspec": { |
| 150 | + "display_name": ".venv", |
| 151 | + "language": "python", |
| 152 | + "name": "python3" |
| 153 | + }, |
| 154 | + "language_info": { |
| 155 | + "codemirror_mode": { |
| 156 | + "name": "ipython", |
| 157 | + "version": 3 |
| 158 | + }, |
| 159 | + "file_extension": ".py", |
| 160 | + "mimetype": "text/x-python", |
| 161 | + "name": "python", |
| 162 | + "nbconvert_exporter": "python", |
| 163 | + "pygments_lexer": "ipython3", |
| 164 | + "version": "3.10.12" |
| 165 | + } |
| 166 | + }, |
| 167 | + "nbformat": 4, |
| 168 | + "nbformat_minor": 2 |
| 169 | +} |
0 commit comments