Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/3 create additional plot examples for monte carlo #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
# Using VTS in a Jupyter notebook

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

## macOS installation details

This is a record of how I got `vts` working in `JupyterLab`.
## Installation details

### Step 1: Install .NET 6

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

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

I saved everything to `$HOME/Documents/Code/dotnet6`

***On MacOS:***
Save everything to `$HOME/Documents/Code/dotnet6`

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

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

export DOTNET_ROOT=$HOME/Documents/Code/dotnet6

### Step 2: Install VTS

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

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

This will create the directory `vts` that is needed for the next step

Install both `powershell` and `nuget` using [Homebrew](https://brew.sh)

brew install powershell
brew install nuget
### Step 3: Download the VTS libraries

Now build VTS. If you don't have `matlab` don't worry, it seemed to work fine without completing the `matlab` tests
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**.

pwsh
cd vts
./BuildTestRelease.ps1
exit

### Step 3: Install pythonnet
### Step 4: Install pythonnet

pip install pythonnet

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

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

import clr

clr.AddReference("System")
from System import Console
Console.WriteLine("Hello from .NET 6!")

The final test is importing from `Vts.dll`

from pythonnet import set_runtime
set_runtime("coreclr")

import clr
clr.AddReference("/path/to/vts/src/Vts/publish/local/Vts.dll")
clr.AddReference("/path/to/libraries/Vts.dll")

from Vts import *

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

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

```
from pythonnet import set_runtime
set_runtime("coreclr")

import clr
clr.AddReference("/path/to/vts/src/Vts/publish/local/Vts.dll")
clr.AddReference("/path/to/libraries/Vts.dll")

from Vts import *
from Vts.Common import *
Expand All @@ -87,4 +82,5 @@ To run `VTS` programs in `python` include the following the header
from Vts.MonteCarlo.Factories import *
from Vts.MonteCarlo.PhotonData import *
from Vts.MonteCarlo.PostProcessing import *
from System import Array, Double
from System import Array, Double
```
1 change: 1 addition & 0 deletions scripting/libraries/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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
53 changes: 44 additions & 9 deletions scripting/monte_carlo/demo_01_r_of_rho_simple.ipynb
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using VTS in Jupyter Notebooks\n",
"**David Cuccia**\n",
"\n",
"**December 2023**\n",
"\n",
"This is a simple example of Jupyter notebook using VTS. It is assumed that\n",
"\n",
"* [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) has been installed\n",
"\n",
"* 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Import the Operating System so we can access the files for the VTS library\n",
"import os\n",
"current_directory = os.getcwd()\n",
"publish_local = current_directory.replace(\"monte_carlo\", \"libraries\\Vts.dll\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use pip to install PythonNet Plotly and Numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -20,6 +44,15 @@
"pip install pythonnet plotly numpy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import the Core CLR runtime from PythonNet and add the reference for the VTS library and its dependencies\n",
"\n",
"Import the namespaces from the Python libraries and the VTS library"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -29,7 +62,7 @@
"from pythonnet import set_runtime\n",
"set_runtime(\"coreclr\")\n",
"import clr\n",
"clr.AddReference(publish_local) # Copy the VTS dlls into the libraries folders\n",
"clr.AddReference(publish_local)\n",
"import numpy as np\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
Expand All @@ -50,6 +83,15 @@
"from System import Array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setup the values for the simulations and plot the results using Plotly\n",
"\n",
"ROfRho"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -84,18 +126,11 @@
"yLabel = \"log(R(ρ)) [mm-2]\"\n",
"\n",
"chart = go.Figure()\n",
"chart.add_trace(go.Scatter(x=detectorMidpoints, y=logReflectance, mode='lines+markers'))\n",
"chart.add_trace(go.Scatter(x=detectorMidpoints, y=logReflectance, mode='markers'))\n",
"chart.update_layout( title=\"log(R(ρ)) [mm-2]\", xaxis_title=xLabel, yaxis_title=yLabel)\n",
"chart.update_yaxes(type=\"log\")\n",
"chart.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
187 changes: 187 additions & 0 deletions scripting/monte_carlo/demo_02_DAW_vs_CAW.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "075fe817",
"metadata": {},
"source": [
"# Using VTS in Jupyter Notebooks\n",
"**Lisa Malenfant**\n",
"\n",
"**December 2023**\n",
"\n",
"This is a simple example of Jupyter notebook using VTS. It is assumed that\n",
"\n",
"* [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) has been installed\n",
"\n",
"* 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c12174d",
"metadata": {},
"outputs": [],
"source": [
"#Import the Operating System so we can access the files for the VTS library\n",
"import os\n",
"current_directory = os.getcwd()\n",
"publish_local = current_directory.replace(\"monte_carlo\", \"libraries\\Vts.dll\")"
]
},
{
"cell_type": "markdown",
"id": "7f248374",
"metadata": {},
"source": [
"Use pip to install PythonNet Plotly and Numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b08ccbd2",
"metadata": {},
"outputs": [],
"source": [
"pip install pythonnet plotly numpy"
]
},
{
"cell_type": "markdown",
"id": "82d0ef92",
"metadata": {},
"source": [
"Import the Core CLR runtime from PythonNet and add the reference for the VTS library and its dependencies\n",
"\n",
"Import the namespaces from the Python libraries and the VTS library"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38947713",
"metadata": {},
"outputs": [],
"source": [
"from pythonnet import set_runtime\n",
"set_runtime(\"coreclr\")\n",
"import clr\n",
"clr.AddReference(publish_local)\n",
"import numpy as np\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
"from Vts import *\n",
"from Vts.Common import *\n",
"from Vts.Extensions import *\n",
"from Vts.Modeling.Optimizers import *\n",
"from Vts.Modeling.ForwardSolvers import *\n",
"from Vts.SpectralMapping import *\n",
"from Vts.Factories import *\n",
"from Vts.MonteCarlo import *\n",
"from Vts.MonteCarlo.Sources import *\n",
"from Vts.MonteCarlo.Tissues import *\n",
"from Vts.MonteCarlo.Detectors import *\n",
"from Vts.MonteCarlo.Factories import *\n",
"from Vts.MonteCarlo.PhotonData import *\n",
"from Vts.MonteCarlo.PostProcessing import *\n",
"from System import Array"
]
},
{
"cell_type": "markdown",
"id": "a2674c8b",
"metadata": {},
"source": [
"Setup the values for the simulations and plot the results using Plotly\n",
"\n",
"DAW vs CAW"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b16d74a4",
"metadata": {},
"outputs": [],
"source": [
"# Setup the detector input for the simulation\n",
"detectorRange = DoubleRange(start=0, stop=40, number=201)\n",
"detectorInput = ROfRhoDetectorInput()\n",
"detectorInput.Rho = detectorRange\n",
"detectorInput.Name = \"ROfRho\"\n",
"detectors = Array.CreateInstance(IDetectorInput,1)\n",
"detectors[0] = detectorInput\n",
"\n",
"simulationOptions1 = SimulationOptions()\n",
"simulationOptions1.AbsorptionWeightingType = AbsorptionWeightingType.Discrete\n",
"# create a SimulationInput object to define the simulation\n",
"simulationInput1 = SimulationInput()\n",
"simulationInput1.N=1000\n",
"simulationInput1.OutputName = \"MonteCarloROfRho-DAW\"\n",
"simulationInput1.DetectorInputs= detectors\n",
"simulationInput1.Options = simulationOptions1\n",
"\n",
"simulationOptions2 = SimulationOptions()\n",
"simulationOptions2.AbsorptionWeightingType = AbsorptionWeightingType.Continuous\n",
"# create a SimulationInput object to define the simulation\n",
"simulationInput2 = SimulationInput()\n",
"simulationInput2.N=1000\n",
"simulationInput2.OutputName = \"MonteCarloROfRho-CAW\"\n",
"simulationInput2.DetectorInputs = detectors\n",
"simulationInput2.Options = simulationOptions2\n",
"\n",
"# create the simulations\n",
"simulation1 = MonteCarloSimulation(simulationInput1)\n",
"simulation2 = MonteCarloSimulation(simulationInput2)\n",
"\n",
"# run the simulations\n",
"simulationOutput1 = simulation1.Run()\n",
"simulationOutput2 = simulation2.Run()\n",
"\n",
"# plot the results using Plotly\n",
"detectorResults1 = Array.CreateInstance(ROfRhoDetector,1)\n",
"detectorResults1[0] = simulationOutput1.ResultsDictionary[\"ROfRho\"]\n",
"logReflectance1 = [r for r in detectorResults1[0].Mean]\n",
"detectorMidpoints1 = [mp for mp in detectorRange.AsEnumerable()]\n",
"\n",
"detectorResults2 = Array.CreateInstance(ROfRhoDetector,1)\n",
"detectorResults2[0] = simulationOutput2.ResultsDictionary[\"ROfRho\"]\n",
"logReflectance2 = [r for r in detectorResults2[0].Mean]\n",
"detectorMidpoints2 = [mp for mp in detectorRange.AsEnumerable()]\n",
"\n",
"xLabel = \"ρ [mm]\"\n",
"yLabel = \"log(R(ρ)) [mm-2]\"\n",
"\n",
"chart = go.Figure()\n",
"chart.add_trace(go.Scatter(x=detectorMidpoints1, y=logReflectance1, mode='markers', name='log(R(ρ)) [mm-2] - DAW'))\n",
"chart.add_trace(go.Scatter(x=detectorMidpoints2, y=logReflectance2, mode='markers', name='log(R(ρ)) [mm-2] - CAW'))\n",
"chart.update_layout( title=\"DAW vs CAW\", xaxis_title=xLabel, yaxis_title=yLabel)\n",
"chart.update_yaxes(type=\"log\")\n",
"chart.show()"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}