Skip to content

Commit b3ec23c

Browse files
author
Lisa Malenfant
committed
Separated the 2 examples into 2 scripts and added documentation
1 parent 5a9c6fe commit b3ec23c

File tree

2 files changed

+198
-79
lines changed

2 files changed

+198
-79
lines changed

scripting/monte_carlo/demo_01_r_of_rho_simple.ipynb

+11-79
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"Import the Operating System so we can access the files for the VTS library\n",
7+
"# Using VTS in Jupyter Notebooks\n",
8+
"**David Cuccia**\n",
89
"\n",
9-
"Before we can run the Python code we need to download the latest zip file from the downloads and put the dll files into the libraries folder."
10+
"**December 2023**\n",
11+
"\n",
12+
"This is a simple example of Jupyter notebook using VTS. It is assumed that\n",
13+
"\n",
14+
"* [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) has been installed\n",
15+
"\n",
16+
"* The [VTS libraries](https://github.com/VirtualPhotonics/VTS) have been downloaded from the zip file in downloads and extracted to the libraries folder"
1017
]
1118
},
1219
{
@@ -15,6 +22,7 @@
1522
"metadata": {},
1623
"outputs": [],
1724
"source": [
25+
"#Import the Operating System so we can access the files for the VTS library\n",
1826
"import os\n",
1927
"current_directory = os.getcwd()\n",
2028
"publish_local = current_directory.replace(\"monte_carlo\", \"libraries\\Vts.dll\")"
@@ -54,7 +62,7 @@
5462
"from pythonnet import set_runtime\n",
5563
"set_runtime(\"coreclr\")\n",
5664
"import clr\n",
57-
"clr.AddReference(publish_local) # Copy the VTS dlls into the libraries folders\n",
65+
"clr.AddReference(publish_local)\n",
5866
"import numpy as np\n",
5967
"import plotly.graph_objects as go\n",
6068
"import plotly.express as px\n",
@@ -123,82 +131,6 @@
123131
"chart.update_yaxes(type=\"log\")\n",
124132
"chart.show()\n"
125133
]
126-
},
127-
{
128-
"cell_type": "markdown",
129-
"metadata": {},
130-
"source": [
131-
"DAW vs CAW"
132-
]
133-
},
134-
{
135-
"cell_type": "code",
136-
"execution_count": null,
137-
"metadata": {},
138-
"outputs": [],
139-
"source": [
140-
"# Setup the detector input for the simulation\n",
141-
"detectorRange = DoubleRange(start=0, stop=40, number=201)\n",
142-
"detectorInput = ROfRhoDetectorInput()\n",
143-
"detectorInput.Rho = detectorRange\n",
144-
"detectorInput.Name = \"ROfRho\"\n",
145-
"detectors = Array.CreateInstance(IDetectorInput,1)\n",
146-
"detectors[0] = detectorInput\n",
147-
"\n",
148-
"simulationOptions1 = SimulationOptions()\n",
149-
"simulationOptions1.AbsorptionWeightingType = AbsorptionWeightingType.Discrete\n",
150-
"# create a SimulationInput object to define the simulation\n",
151-
"simulationInput1 = SimulationInput()\n",
152-
"simulationInput1.N=1000\n",
153-
"simulationInput1.OutputName = \"MonteCarloROfRho-DAW\"\n",
154-
"simulationInput1.DetectorInputs= detectors\n",
155-
"simulationInput1.Options = simulationOptions1\n",
156-
"\n",
157-
"simulationOptions2 = SimulationOptions()\n",
158-
"simulationOptions2.AbsorptionWeightingType = AbsorptionWeightingType.Continuous\n",
159-
"# create a SimulationInput object to define the simulation\n",
160-
"simulationInput2 = SimulationInput()\n",
161-
"simulationInput2.N=1000\n",
162-
"simulationInput2.OutputName = \"MonteCarloROfRho-CAW\"\n",
163-
"simulationInput2.DetectorInputs = detectors\n",
164-
"simulationInput2.Options = simulationOptions2\n",
165-
"\n",
166-
"# create the simulations\n",
167-
"simulation1 = MonteCarloSimulation(simulationInput1)\n",
168-
"simulation2 = MonteCarloSimulation(simulationInput2)\n",
169-
"\n",
170-
"# run the simulations\n",
171-
"simulationOutput1 = simulation1.Run()\n",
172-
"simulationOutput2 = simulation2.Run()\n",
173-
"\n",
174-
"# plot the results using Plotly\n",
175-
"detectorResults1 = Array.CreateInstance(ROfRhoDetector,1)\n",
176-
"detectorResults1[0] = simulationOutput1.ResultsDictionary[\"ROfRho\"]\n",
177-
"logReflectance1 = [r for r in detectorResults1[0].Mean]\n",
178-
"detectorMidpoints1 = [mp for mp in detectorRange.AsEnumerable()]\n",
179-
"\n",
180-
"detectorResults2 = Array.CreateInstance(ROfRhoDetector,1)\n",
181-
"detectorResults2[0] = simulationOutput2.ResultsDictionary[\"ROfRho\"]\n",
182-
"logReflectance2 = [r for r in detectorResults[0].Mean]\n",
183-
"detectorMidpoints2 = [mp for mp in detectorRange.AsEnumerable()]\n",
184-
"\n",
185-
"xLabel = \"ρ [mm]\"\n",
186-
"yLabel = \"log(R(ρ)) [mm-2]\"\n",
187-
"\n",
188-
"chart = go.Figure()\n",
189-
"chart.add_trace(go.Scatter(x=detectorMidpoints1, y=logReflectance1, mode='markers', name='log(R(ρ)) [mm-2] - DAW'))\n",
190-
"chart.add_trace(go.Scatter(x=detectorMidpoints2, y=logReflectance2, mode='markers', name='log(R(ρ)) [mm-2] - CAW'))\n",
191-
"chart.update_layout( title=\"DAW vs CAW\", xaxis_title=xLabel, yaxis_title=yLabel)\n",
192-
"chart.update_yaxes(type=\"log\")\n",
193-
"chart.show()"
194-
]
195-
},
196-
{
197-
"cell_type": "code",
198-
"execution_count": null,
199-
"metadata": {},
200-
"outputs": [],
201-
"source": []
202134
}
203135
],
204136
"metadata": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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 [VTS libraries](https://github.com/VirtualPhotonics/VTS) have been downloaded from the zip file in downloads 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

Comments
 (0)