Skip to content

Commit 09f7f84

Browse files
author
Lisa Glover
authored
Merge pull request #7 from VirtualPhotonics/feature/3-create-additional-plot-examples-for-monte-carlo
Feature/3 create additional plot examples for monte carlo
2 parents df78a57 + fa070a9 commit 09f7f84

File tree

4 files changed

+254
-35
lines changed

4 files changed

+254
-35
lines changed

README.md

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
11
# Using VTS in a Jupyter notebook
22

3-
This is likely to change once `python` scripting is formally supported by `VTS`.
3+
This information will likely change as more support for `python` and `.NET` is introduced.
44

5-
## macOS installation details
6-
7-
This is a record of how I got `vts` working in `JupyterLab`.
5+
## Installation details
86

97
### Step 1: Install .NET 6
108

119
You will need version 6 of .NET which is available from Microsoft below
1210

1311
https://dotnet.microsoft.com/en-us/download/dotnet/6.0
1412

15-
I saved everything to `$HOME/Documents/Code/dotnet6`
13+
14+
***On MacOS:***
15+
Save everything to `$HOME/Documents/Code/dotnet6`
1616

1717
Do not forget to update your `~/.bash_profile` or whatever you use so that this directory is in your path. Also define `DOTNET_ROOT`
1818

1919
PATH=$PATH:$HOME/Documents/Code/dotnet6
2020

2121
export DOTNET_ROOT=$HOME/Documents/Code/dotnet6
2222

23-
### Step 2: Install VTS
24-
25-
Follow the guidelines at https://github.com/VirtualPhotonics/VTS/wiki/Getting-Started-on-Mac Briefly, clone the `.git` repo
23+
### Step 2: Clone the scripting repository
2624

2725
git clone https://github.com/VirtualPhotonics/vts.git
2826

29-
This will create the directory `vts` that is needed for the next step
30-
31-
Install both `powershell` and `nuget` using [Homebrew](https://brew.sh)
32-
33-
brew install powershell
34-
brew install nuget
27+
### Step 3: Download the VTS libraries
3528

36-
Now build VTS. If you don't have `matlab` don't worry, it seemed to work fine without completing the `matlab` tests
29+
Get the latest VTS libraries for the specific platform from [releases](https://github.com/VirtualPhotonics/Vts.Scripting.Python/releases) and extract them into the **libraries** folder under **scripting**.
3730

38-
pwsh
39-
cd vts
40-
./BuildTestRelease.ps1
41-
exit
42-
43-
### Step 3: Install pythonnet
31+
### Step 4: Install pythonnet
4432

4533
pip install pythonnet
4634

47-
Because `pythonnet` under macOS (or linux) defaults to `mono`, two more things need to added to `~/.bash_profile`
35+
***Note for MacOS and Linux:*** Because `pythonnet` under macOS (or linux) defaults to `mono`, two more things need to added to `~/.bash_profile`
4836

4937
export PYTHONNET_RUNTIME=coreclr
5038
export PYTHONNET_PYDLL=/usr/local/bin/python3
@@ -54,15 +42,18 @@ Obviously use the path for python on your system (`which python3` will tell you
5442
Next start a `JupyterLab` notebook to verify that things are installed correctly
5543

5644
import clr
57-
5845
clr.AddReference("System")
5946
from System import Console
6047
Console.WriteLine("Hello from .NET 6!")
6148

6249
The final test is importing from `Vts.dll`
50+
51+
from pythonnet import set_runtime
52+
set_runtime("coreclr")
6353

6454
import clr
65-
clr.AddReference("/path/to/vts/src/Vts/publish/local/Vts.dll")
55+
clr.AddReference("/path/to/libraries/Vts.dll")
56+
6657
from Vts import *
6758

6859
where, of course, "/path/to" above has been adapted to your system
@@ -71,8 +62,12 @@ where, of course, "/path/to" above has been adapted to your system
7162

7263
To run `VTS` programs in `python` include the following the header
7364

65+
```
66+
from pythonnet import set_runtime
67+
set_runtime("coreclr")
68+
7469
import clr
75-
clr.AddReference("/path/to/vts/src/Vts/publish/local/Vts.dll")
70+
clr.AddReference("/path/to/libraries/Vts.dll")
7671
7772
from Vts import *
7873
from Vts.Common import *
@@ -87,4 +82,5 @@ To run `VTS` programs in `python` include the following the header
8782
from Vts.MonteCarlo.Factories import *
8883
from Vts.MonteCarlo.PhotonData import *
8984
from Vts.MonteCarlo.PostProcessing import *
90-
from System import Array, Double
85+
from System import Array, Double
86+
```

scripting/libraries/readme.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The VTS libraries should be copied into this folder. Get the latest VTS library (VTS.dll) and its dependencies from the releases section of this repository https://github.com/VirtualPhotonics/Vts.Scripting.Python/releases

scripting/monte_carlo/demo_01_r_of_rho_simple.ipynb

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Using VTS in Jupyter Notebooks\n",
8+
"**David Cuccia**\n",
9+
"\n",
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 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"
17+
]
18+
},
319
{
420
"cell_type": "code",
521
"execution_count": null,
622
"metadata": {},
723
"outputs": [],
824
"source": [
25+
"#Import the Operating System so we can access the files for the VTS library\n",
926
"import os\n",
1027
"current_directory = os.getcwd()\n",
1128
"publish_local = current_directory.replace(\"monte_carlo\", \"libraries\\Vts.dll\")"
1229
]
1330
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"Use pip to install PythonNet Plotly and Numpy"
36+
]
37+
},
1438
{
1539
"cell_type": "code",
1640
"execution_count": null,
@@ -20,6 +44,15 @@
2044
"pip install pythonnet plotly numpy"
2145
]
2246
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"Import the Core CLR runtime from PythonNet and add the reference for the VTS library and its dependencies\n",
52+
"\n",
53+
"Import the namespaces from the Python libraries and the VTS library"
54+
]
55+
},
2356
{
2457
"cell_type": "code",
2558
"execution_count": null,
@@ -29,7 +62,7 @@
2962
"from pythonnet import set_runtime\n",
3063
"set_runtime(\"coreclr\")\n",
3164
"import clr\n",
32-
"clr.AddReference(publish_local) # Copy the VTS dlls into the libraries folders\n",
65+
"clr.AddReference(publish_local)\n",
3366
"import numpy as np\n",
3467
"import plotly.graph_objects as go\n",
3568
"import plotly.express as px\n",
@@ -50,6 +83,15 @@
5083
"from System import Array"
5184
]
5285
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Setup the values for the simulations and plot the results using Plotly\n",
91+
"\n",
92+
"ROfRho"
93+
]
94+
},
5395
{
5496
"cell_type": "code",
5597
"execution_count": null,
@@ -84,18 +126,11 @@
84126
"yLabel = \"log(R(ρ)) [mm-2]\"\n",
85127
"\n",
86128
"chart = go.Figure()\n",
87-
"chart.add_trace(go.Scatter(x=detectorMidpoints, y=logReflectance, mode='lines+markers'))\n",
129+
"chart.add_trace(go.Scatter(x=detectorMidpoints, y=logReflectance, mode='markers'))\n",
88130
"chart.update_layout( title=\"log(R(ρ)) [mm-2]\", xaxis_title=xLabel, yaxis_title=yLabel)\n",
89131
"chart.update_yaxes(type=\"log\")\n",
90132
"chart.show()\n"
91133
]
92-
},
93-
{
94-
"cell_type": "code",
95-
"execution_count": null,
96-
"metadata": {},
97-
"outputs": [],
98-
"source": []
99134
}
100135
],
101136
"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 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

Comments
 (0)