|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "075fe817", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Using VTS in Jupyter Notebooks\n", |
| 9 | + "**Lisa Malenfant**\n", |
| 10 | + "\n", |
| 11 | + "**December 2023**\n", |
| 12 | + "\n", |
| 13 | + "This is a simple example of Jupyter notebook using VTS. It is assumed that\n", |
| 14 | + "\n", |
| 15 | + "* [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) has been installed\n", |
| 16 | + "\n", |
| 17 | + "* The latest [VTS libraries](https://github.com/VirtualPhotonics/Vts.Scripting.Python/releases) have been downloaded from the zip file in releases and extracted to the libraries folder" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "id": "1c12174d", |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "#Import the Operating System so we can access the files for the VTS library\n", |
| 28 | + "import os\n", |
| 29 | + "current_directory = os.getcwd()\n", |
| 30 | + "publish_local = current_directory.replace(\"monte_carlo\", \"libraries\\Vts.dll\")" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "id": "7f248374", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "Use pip to install PythonNet Plotly and Numpy" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": null, |
| 44 | + "id": "b08ccbd2", |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "pip install pythonnet plotly numpy" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "id": "82d0ef92", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "Import the Core CLR runtime from PythonNet and add the reference for the VTS library and its dependencies\n", |
| 57 | + "\n", |
| 58 | + "Import the namespaces from the Python libraries and the VTS library" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "id": "38947713", |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "from pythonnet import set_runtime\n", |
| 69 | + "set_runtime(\"coreclr\")\n", |
| 70 | + "import clr\n", |
| 71 | + "clr.AddReference(publish_local)\n", |
| 72 | + "import numpy as np\n", |
| 73 | + "import plotly.graph_objects as go\n", |
| 74 | + "import plotly.express as px\n", |
| 75 | + "from Vts import *\n", |
| 76 | + "from Vts.Common import *\n", |
| 77 | + "from Vts.Extensions import *\n", |
| 78 | + "from Vts.Modeling.Optimizers import *\n", |
| 79 | + "from Vts.Modeling.ForwardSolvers import *\n", |
| 80 | + "from Vts.SpectralMapping import *\n", |
| 81 | + "from Vts.Factories import *\n", |
| 82 | + "from Vts.MonteCarlo import *\n", |
| 83 | + "from Vts.MonteCarlo.Sources import *\n", |
| 84 | + "from Vts.MonteCarlo.Tissues import *\n", |
| 85 | + "from Vts.MonteCarlo.Detectors import *\n", |
| 86 | + "from Vts.MonteCarlo.Factories import *\n", |
| 87 | + "from Vts.MonteCarlo.PhotonData import *\n", |
| 88 | + "from Vts.MonteCarlo.PostProcessing import *\n", |
| 89 | + "from System import Array" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "id": "a2674c8b", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "Setup the values for the simulations and plot the results using Plotly\n", |
| 98 | + "\n", |
| 99 | + "DAW vs CAW" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "id": "b16d74a4", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "# Setup the detector input for the simulation\n", |
| 110 | + "detectorRange = DoubleRange(start=0, stop=40, number=201)\n", |
| 111 | + "detectorInput = ROfRhoDetectorInput()\n", |
| 112 | + "detectorInput.Rho = detectorRange\n", |
| 113 | + "detectorInput.Name = \"ROfRho\"\n", |
| 114 | + "detectors = Array.CreateInstance(IDetectorInput,1)\n", |
| 115 | + "detectors[0] = detectorInput\n", |
| 116 | + "\n", |
| 117 | + "simulationOptions1 = SimulationOptions()\n", |
| 118 | + "simulationOptions1.AbsorptionWeightingType = AbsorptionWeightingType.Discrete\n", |
| 119 | + "# create a SimulationInput object to define the simulation\n", |
| 120 | + "simulationInput1 = SimulationInput()\n", |
| 121 | + "simulationInput1.N=1000\n", |
| 122 | + "simulationInput1.OutputName = \"MonteCarloROfRho-DAW\"\n", |
| 123 | + "simulationInput1.DetectorInputs= detectors\n", |
| 124 | + "simulationInput1.Options = simulationOptions1\n", |
| 125 | + "\n", |
| 126 | + "simulationOptions2 = SimulationOptions()\n", |
| 127 | + "simulationOptions2.AbsorptionWeightingType = AbsorptionWeightingType.Continuous\n", |
| 128 | + "# create a SimulationInput object to define the simulation\n", |
| 129 | + "simulationInput2 = SimulationInput()\n", |
| 130 | + "simulationInput2.N=1000\n", |
| 131 | + "simulationInput2.OutputName = \"MonteCarloROfRho-CAW\"\n", |
| 132 | + "simulationInput2.DetectorInputs = detectors\n", |
| 133 | + "simulationInput2.Options = simulationOptions2\n", |
| 134 | + "\n", |
| 135 | + "# create the simulations\n", |
| 136 | + "simulation1 = MonteCarloSimulation(simulationInput1)\n", |
| 137 | + "simulation2 = MonteCarloSimulation(simulationInput2)\n", |
| 138 | + "\n", |
| 139 | + "# run the simulations\n", |
| 140 | + "simulationOutput1 = simulation1.Run()\n", |
| 141 | + "simulationOutput2 = simulation2.Run()\n", |
| 142 | + "\n", |
| 143 | + "# plot the results using Plotly\n", |
| 144 | + "detectorResults1 = Array.CreateInstance(ROfRhoDetector,1)\n", |
| 145 | + "detectorResults1[0] = simulationOutput1.ResultsDictionary[\"ROfRho\"]\n", |
| 146 | + "logReflectance1 = [r for r in detectorResults1[0].Mean]\n", |
| 147 | + "detectorMidpoints1 = [mp for mp in detectorRange.AsEnumerable()]\n", |
| 148 | + "\n", |
| 149 | + "detectorResults2 = Array.CreateInstance(ROfRhoDetector,1)\n", |
| 150 | + "detectorResults2[0] = simulationOutput2.ResultsDictionary[\"ROfRho\"]\n", |
| 151 | + "logReflectance2 = [r for r in detectorResults2[0].Mean]\n", |
| 152 | + "detectorMidpoints2 = [mp for mp in detectorRange.AsEnumerable()]\n", |
| 153 | + "\n", |
| 154 | + "xLabel = \"ρ [mm]\"\n", |
| 155 | + "yLabel = \"log(R(ρ)) [mm-2]\"\n", |
| 156 | + "\n", |
| 157 | + "chart = go.Figure()\n", |
| 158 | + "chart.add_trace(go.Scatter(x=detectorMidpoints1, y=logReflectance1, mode='markers', name='log(R(ρ)) [mm-2] - DAW'))\n", |
| 159 | + "chart.add_trace(go.Scatter(x=detectorMidpoints2, y=logReflectance2, mode='markers', name='log(R(ρ)) [mm-2] - CAW'))\n", |
| 160 | + "chart.update_layout( title=\"DAW vs CAW\", xaxis_title=xLabel, yaxis_title=yLabel)\n", |
| 161 | + "chart.update_yaxes(type=\"log\")\n", |
| 162 | + "chart.show()" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "metadata": { |
| 167 | + "kernelspec": { |
| 168 | + "display_name": "Python 3 (ipykernel)", |
| 169 | + "language": "python", |
| 170 | + "name": "python3" |
| 171 | + }, |
| 172 | + "language_info": { |
| 173 | + "codemirror_mode": { |
| 174 | + "name": "ipython", |
| 175 | + "version": 3 |
| 176 | + }, |
| 177 | + "file_extension": ".py", |
| 178 | + "mimetype": "text/x-python", |
| 179 | + "name": "python", |
| 180 | + "nbconvert_exporter": "python", |
| 181 | + "pygments_lexer": "ipython3", |
| 182 | + "version": "3.11.5" |
| 183 | + } |
| 184 | + }, |
| 185 | + "nbformat": 4, |
| 186 | + "nbformat_minor": 5 |
| 187 | +} |
0 commit comments